Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
#!/usr/bin/env python3

#========================================================================
#
# This script is used to generate a web page which superpose two
# sets of similar histograms.
#
# Command-line options :
#
#   -c <configuration> : description of the histograms to be displayed and how.
#   -t <title> : general title of the page.
#   -r <name> : short name of the red histograms.
#   -b <name> : short name of the blue histograms.
# 
# Command-line arguments :
#
#   $1 : path of the ROOT file containing the red histograms.
#   $2 : path of the ROOT file containing the blue histograms.
#   $3 : destination directory.
#
#=========================================================================


import os, sys, datetime, shutil, optparse


#============================================
# display a command and eventually executes
#============================================

def mysystem(command,apply=1):
  print(command)
  if apply==1: return os.system(command)
  elif apply==0:  return 0
  else:
    print('[electronStore.py] UNSUPPORTED ARGUMENT VALUE FOR mysystem(,apply):',apply)
    exit(1)
  

#============================================
# force immediate flushing of stdout
#============================================

class flushfile(object):
  def __init__(self,f):
    self.f = f
  def write(self,x):
    self.f.write(x)
    self.f.flush()

sys.stdout = flushfile(sys.stdout)


#===================================================================
# when called as an independant executable
#===================================================================

if __name__ == "__main__":


  #============================================
  # command-line arguments
  #============================================
    
  parser = optparse.OptionParser()
  parser.add_option("-c", "--cfg", dest="config", action="store", default="electronCompare.txt",
    help="the configuration file describe which histogram must be displayed and how")  
  parser.add_option("-t", "--title", dest="title", action="store", default="",
    help="the title of the page")  
  parser.add_option("-r", "--red-name", dest="red", action="store", default="",
    help="short name of the red histograms")  
  parser.add_option("-b", "--blue-name", dest="blue", action="store", default="",
    help="short name of the blue histograms")  
  (options, args) = parser.parse_args()
  
  if len(args)<2:
    print("[electronStore.py] I NEED AT LEAST TWO ARGUMENTS.")
    exit(2)
    
  red_file = args.pop(0)
  web_dir = args.pop()
#  print 'WEB DIR 1 =',web_dir
  if not '/afs/cern.ch/cms/' in web_dir:
    print("local : ", web_dir)
    web_url = web_dir
  else:
    web_url = web_dir.replace('/afs/cern.ch/cms/','http://cmsdoc.cern.ch/',1)
  if len(args)>0 :
    blue_file = args.pop(0)
  else :
    blue_file = ''

  #===================================================
  # prepare output directories and check input files
  #===================================================

  # destination dir
#  print 'WEB DIR =',web_dir
  if os.path.exists(web_dir+'/gifs')==False:
    os.makedirs(web_dir+'/gifs')
    
  # red file
  red_base = os.path.basename(red_file)
  if os.path.isfile(red_file)==True :
    print('RED FILE =',red_file)
    if os.path.isfile(red_base)==True and os.path.getmtime(red_base)>os.path.getmtime(red_file) :
      print('[electronCompare.py] did you forget to store '+red_base+' ?')
  else :
    print("[electronCompare.py] FILE NOT FOUND :",red_file)
    if os.path.isfile(red_base)==True :
      print('[electronCompare.py] did you forget to store '+red_base+' ?')
    exit(3)
 
  # blue file
  if blue_file!='' :
    if os.path.isfile(blue_file)==True :
      print('BLUE FILE =',blue_file)
    else :
      print('[electronCompare.py] file not found :',blue_file)
      blue_file = ''
  else :
    print("[electronCompare.py] no blue histograms to compare with.")
         
      
  #===================================================
  # improved default options
  #===================================================

  (red_head,red_tail) = os.path.split(red_file)
  red_long_name = os.path.basename(red_head)+'/'+red_tail
  (blue_head,blue_tail) = os.path.split(blue_file)
  blue_long_name = os.path.basename(blue_head)+'/'+blue_tail
  if options.red=='' :
    options.red = red_long_name
  if options.blue=='' :
    options.blue = blue_long_name
  if options.title=='' :
    options.title = red_long_name+' vs '+blue_long_name
         
  (red_hd, red_release) = os.path.split(red_head)
  (blue_hd, blue_release) = os.path.split(blue_head)

  #============================================
  # final commands
  #============================================

  mysystem('cp -f electronCompare.C '+options.config+' '+web_dir)
  
  os.environ['CMP_DIR'] = web_dir
  os.environ['CMP_URL'] = web_url
  os.environ['CMP_TITLE'] = options.title
  os.environ['CMP_RED_FILE'] = red_file
  os.environ['CMP_BLUE_FILE'] = blue_file
  os.environ['CMP_RED_NAME'] = options.red
  os.environ['CMP_BLUE_NAME'] = options.blue
  os.environ['CMP_RED_COMMENT'] = red_file+'.comment'
  os.environ['CMP_BLUE_COMMENT'] = blue_file+'.comment'
  os.environ['CMP_CONFIG'] = options.config
  os.environ['CMP_RED_RELEASE'] = red_release
  os.environ['CMP_BLUE_RELEASE'] = blue_release
  
  mysystem('root -b -l -q electronCompare.C')
  
  print("You can access the files here:",web_dir)
  print("You can browse your validation plots here:",web_url+'/')