CMS 3D CMS Logo

electronCompare.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #========================================================================
4 #
5 # This script is used to generate a web page which superpose two
6 # sets of similar histograms.
7 #
8 # Command-line options :
9 #
10 # -c <configuration> : description of the histograms to be displayed and how.
11 # -t <title> : general title of the page.
12 # -r <name> : short name of the red histograms.
13 # -b <name> : short name of the blue histograms.
14 #
15 # Command-line arguments :
16 #
17 # $1 : path of the ROOT file containing the red histograms.
18 # $2 : path of the ROOT file containing the blue histograms.
19 # $3 : destination directory.
20 #
21 #=========================================================================
22 
23 
24 from __future__ import print_function
25 import os, sys, datetime, shutil, optparse
26 
27 
28 #============================================
29 # display a command and eventually executes
30 #============================================
31 
32 def mysystem(command,apply=1):
33  print(command)
34  if apply==1: return os.system(command)
35  elif apply==0: return 0
36  else:
37  print('[electronStore.py] UNSUPPORTED ARGUMENT VALUE FOR mysystem(,apply):',apply)
38  exit(1)
39 
40 
41 #============================================
42 # force immediate flushing of stdout
43 #============================================
44 
46  def __init__(self,f):
47  self.f = f
48  def write(self,x):
49  self.f.write(x)
50  self.f.flush()
51 
52 sys.stdout = flushfile(sys.stdout)
53 
54 
55 #===================================================================
56 # when called as an independant executable
57 #===================================================================
58 
59 if __name__ == "__main__":
60 
61 
62  #============================================
63  # command-line arguments
64  #============================================
65 
66  parser = optparse.OptionParser()
67  parser.add_option("-c", "--cfg", dest="config", action="store", default="electronCompare.txt",
68  help="the configuration file describe which histogram must be displayed and how")
69  parser.add_option("-t", "--title", dest="title", action="store", default="",
70  help="the title of the page")
71  parser.add_option("-r", "--red-name", dest="red", action="store", default="",
72  help="short name of the red histograms")
73  parser.add_option("-b", "--blue-name", dest="blue", action="store", default="",
74  help="short name of the blue histograms")
75  (options, args) = parser.parse_args()
76 
77  if len(args)<2:
78  print("[electronStore.py] I NEED AT LEAST TWO ARGUMENTS.")
79  exit(2)
80 
81  red_file = args.pop(0)
82  web_dir = args.pop()
83 # print 'WEB DIR 1 =',web_dir
84  if not '/afs/cern.ch/cms/' in web_dir:
85  print("local : ", web_dir)
86  web_url = web_dir
87  else:
88  web_url = web_dir.replace('/afs/cern.ch/cms/','http://cmsdoc.cern.ch/',1)
89  if len(args)>0 :
90  blue_file = args.pop(0)
91  else :
92  blue_file = ''
93 
94  #===================================================
95  # prepare output directories and check input files
96  #===================================================
97 
98  # destination dir
99 # print 'WEB DIR =',web_dir
100  if os.path.exists(web_dir+'/gifs')==False:
101  os.makedirs(web_dir+'/gifs')
102 
103  # red file
104  red_base = os.path.basename(red_file)
105  if os.path.isfile(red_file)==True :
106  print('RED FILE =',red_file)
107  if os.path.isfile(red_base)==True and os.path.getmtime(red_base)>os.path.getmtime(red_file) :
108  print('[electronCompare.py] did you forget to store '+red_base+' ?')
109  else :
110  print("[electronCompare.py] FILE NOT FOUND :",red_file)
111  if os.path.isfile(red_base)==True :
112  print('[electronCompare.py] did you forget to store '+red_base+' ?')
113  exit(3)
114 
115  # blue file
116  if blue_file!='' :
117  if os.path.isfile(blue_file)==True :
118  print('BLUE FILE =',blue_file)
119  else :
120  print('[electronCompare.py] file not found :',blue_file)
121  blue_file = ''
122  else :
123  print("[electronCompare.py] no blue histograms to compare with.")
124 
125 
126  #===================================================
127  # improved default options
128  #===================================================
129 
130  (red_head,red_tail) = os.path.split(red_file)
131  red_long_name = os.path.basename(red_head)+'/'+red_tail
132  (blue_head,blue_tail) = os.path.split(blue_file)
133  blue_long_name = os.path.basename(blue_head)+'/'+blue_tail
134  if options.red=='' :
135  options.red = red_long_name
136  if options.blue=='' :
137  options.blue = blue_long_name
138  if options.title=='' :
139  options.title = red_long_name+' vs '+blue_long_name
140 
141  (red_hd, red_release) = os.path.split(red_head)
142  (blue_hd, blue_release) = os.path.split(blue_head)
143 
144  #============================================
145  # final commands
146  #============================================
147 
148  mysystem('cp -f electronCompare.C '+options.config+' '+web_dir)
149 
150  os.environ['CMP_DIR'] = web_dir
151  os.environ['CMP_URL'] = web_url
152  os.environ['CMP_TITLE'] = options.title
153  os.environ['CMP_RED_FILE'] = red_file
154  os.environ['CMP_BLUE_FILE'] = blue_file
155  os.environ['CMP_RED_NAME'] = options.red
156  os.environ['CMP_BLUE_NAME'] = options.blue
157  os.environ['CMP_RED_COMMENT'] = red_file+'.comment'
158  os.environ['CMP_BLUE_COMMENT'] = blue_file+'.comment'
159  os.environ['CMP_CONFIG'] = options.config
160  os.environ['CMP_RED_RELEASE'] = red_release
161  os.environ['CMP_BLUE_RELEASE'] = blue_release
162 
163  mysystem('root -b -l -q electronCompare.C')
164 
165  print("You can access the files here:",web_dir)
166  print("You can browse your validation plots here:",web_url+'/')
electronCompare.flushfile.write
def write(self, x)
Definition: electronCompare.py:48
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
electronCompare.flushfile
Definition: electronCompare.py:45
electronCompare.flushfile.__init__
def __init__(self, f)
Definition: electronCompare.py:46
edm::print
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
electronCompare.mysystem
def mysystem(command, apply=1)
Definition: electronCompare.py:32
electronCompare.flushfile.f
f
Definition: electronCompare.py:47
beamvalidation.exit
def exit(msg="")
Definition: beamvalidation.py:53