CMS 3D CMS Logo

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