CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
compare_using_files.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 ################################################################################
3 # https://twiki.cern.ch/twiki/bin/view/CMSPublic/RelMon
4 #
5 # $Author: anorkus $
6 # $Date: 2012/10/25 16:10:22 $
7 # $Revision: 1.6 $
8 #
9 #
10 # Danilo Piparo CERN - danilo.piparo@cern.ch
11 #
12 ################################################################################
13 
14 
15 def getInfoFromFilename(filename):
16  prefix,sample,cmssw_release,tier = filename[:-5].split("__")[:5]
17  run=int(prefix.split("_")[-1][1:])
18  return run,sample,cmssw_release,tier
19 
20 from sys import argv,exit
21 import os
22 
23 # Default Configuration Parameters ---------------------------------------------
24 
25 stat_test="Chi2"
26 test_threshold=1e-5
27 
28 
29 #run="1"
30 
31 dir_name=""
32 outdir_name=""
33 
34 compare=False
35 report=False
36 
37 do_pngs=False
38 
39 black_list_str=""
40 
41 #-------------------------------------------------------------------------------
42 
43 from optparse import OptionParser
44 
45 parser = OptionParser(usage="usage: %prog file1 file2 [options]")
46 
47 
48 #parser.add_option("-r","--run ",
49  #action="store",
50  #dest="run",
51  #default=run,
52  #help="The run to be checked \n(default is %s)" %run)
53 
54 parser.add_option("-d","--dir_name",
55  action="store",
56  dest="dir_name",
57  default=dir_name,
58  help="The 'directory' to be checked in the DQM \n(default is %s)" %dir_name)
59 
60 parser.add_option("-o","--outdir_name",
61  action="store",
62  dest="outdir_name",
63  default=outdir_name,
64  help="The directory where the output will be stored \n(default is %s)" %outdir_name)
65 
66 parser.add_option("-p","--do_pngs",
67  action="store_true",
68  dest="do_pngs",
69  default=False,
70  help="Do the pngs of the comparison (takes 50%% of the total running time) \n(default is %s)" %False)
71 
72 parser.add_option("--no_successes",
73  action="store_true",
74  dest="no_successes",
75  default=False,
76  help="Do not draw successes. Default is False.")
77 
78 parser.add_option("-P","--pickle",
79  action="store",
80  dest="pklfile",
81  default="",
82  help="Pkl file of the dir structure ")
83 
84 parser.add_option("--sample",
85  action="store",
86  dest="sample",
87  default="Sample",
88  help="The name of the sample to be displayed")
89 
90 parser.add_option("--metas",
91  action="store",
92  dest="metas",
93  default="",
94  help="The Metas describing the two files (separated by @@@)")
95 
96 parser.add_option("-t","--test_threshold",
97  action="store",
98  dest="test_threshold",
99  default=test_threshold,
100  help="Threshold for the statistical test \n(default is %s)" %test_threshold)
101 
102 parser.add_option("-s","--stat_test",
103  action="store",
104  dest="stat_test",
105  default=stat_test,
106  help="Statistical test (KS or Chi2) \n(default is %s)" %stat_test)
107 
108 parser.add_option("-C","--compare",
109  action="store_true",
110  dest="compare",
111  default=compare,
112  help="Make the comparison \n(default is %s)" %compare)
113 
114 parser.add_option("-R","--Report",
115  action="store_true",
116  dest="report",
117  default=report,
118  help="Make the html report \n(default is %s)" %report)
119 
120 parser.add_option("--specify_run",
121  action="store_true",
122  dest="specify_run",
123  default=False,
124  help="Append the run number to the output dir for data")
125 
126 
127 parser.add_option("-B","--black_list",
128  action="store",
129  dest="black_list",
130  default=black_list_str,
131  help="Blacklist elements. form is name@hierarchy_level (i.e. HLT@1) \n(default is %s)" %black_list_str)
132 
133 ##---HASHING---##
134 parser.add_option("--hash_name",
135  action="store_true",
136  dest="hash_name",
137  default=False,
138  help="Set if you want to minimize & hash the output HTML files.")
139 ##--Blacklist File --##
140 parser.add_option("--use_black_file",
141  action="store_true",
142  dest="blacklist_file",
143  default=False,
144  help="Use a black list file of histograms located @ /RelMon/data")
145 
147  ##GET a black-list file of histograms##
148  if os.environ.has_key("RELMON_SA"):
149  black_list_file="../data/blacklist.txt"
150  else:
151  black_list_file="%s/src/Utilities/RelMon/data/blacklist.txt"%(os.environ["CMSSW_BASE"])
152  bListFile = open(black_list_file,'r')
153  black_listed_histograms = bListFile.read()
154  bListFile.close()
155 
156  histogramArray = black_listed_histograms.split("\n")
157  histogramArray.remove("") #remove the last element which is empty line
158  newarray = []
159  for elem in histogramArray:
160  tmp = elem.split("/") #screw windows as it is being run on lxbuild machines with Linux
161  tmp.insert(1,"Run summary") #insert "Run summary" dir in path as in ROOT files they exists but user haven't defined them
162  newarray.append(("/").join(tmp))
163  return newarray
164  ##------##
165 
166 (options, args) = parser.parse_args()
167 
168 if len(args)!=2 and options.compare:
169  print "Wrong number of RootFiles specified (%s)" %len(args)
170  print args
171 
172 #-------------------------------------------------------------------------------
173 original_pickle_name=""
174 if options.compare:
175 
176  if os.environ.has_key("RELMON_SA"):
177  import definitions
178  from dqm_interfaces import DirID,DirWalkerFile,string2blacklist
179  from dirstructure import Directory
180  else:
181  import Utilities.RelMon.definitions as definitions
182  from Utilities.RelMon.dqm_interfaces import DirID,DirWalkerFile,string2blacklist
183  from Utilities.RelMon.dirstructure import Directory
184 
185  import cPickle
186  from os import mkdir,chdir,getcwd
187  from os.path import exists
188 
189  #-------------------------------------------------------------------------------
190  # Guess Releases and sample from filename
191  rootfilename1,rootfilename2 = args
192 
193  run1=-1
194  sample1=''
195  cmssw_release1=''
196  tier1=''
197  run2=-1
198  sample2=''
199  cmssw_release2=''
200  tier2=''
201 
202  if options.metas=='':
203  run1,sample1,cmssw_release1,tier1= getInfoFromFilename(rootfilename1)
204  run2,sample2,cmssw_release2,tier2= getInfoFromFilename(rootfilename2)
205  else:
206  print "Reading meta from commandline"
207  sample1=sample2=options.sample
208  cmssw_release1,cmssw_release2=options.metas.split('@@@')
209 
210  # check if the sample is the same
211  if sample1!=sample2:
212  print "I am puzzled. Did you choose two different samples?"
213  exit(1)
214  sample = sample1
215 
216  # check if the run is the same
217  if run1!=run2:
218  print "I am puzzled. Did you choose two different runs?"
219 # exit(1)
220  run=run1
221 
222  fulldirname=options.outdir_name
223  if len(fulldirname)==0:
224  fulldirname=options.dir_name
225  if len(fulldirname)==0:
226  fulldirname="%s_%s_%s" %(sample1,cmssw_release1,cmssw_release2)
227 
228 
229  black_list=string2blacklist(options.black_list)
230 
231  if options.blacklist_file:
232  black_listed = blackListedHistos()
233  else:
234  black_listed = []
235 
236 #-------------------------------------------------------------------------------
237 
238  print "Analysing Histograms located in directory %s at: " %options.dir_name
239  for filename in rootfilename1,rootfilename2:
240  print " o %s" %filename
241 
242 
243  if len(black_list)>0:
244  print "We have a Blacklist:"
245  for dirid in black_list:
246  print " o %s" %dirid
247 
248  # Set up the fake directory structure
249  directory=Directory(options.dir_name)
250  dirwalker=DirWalkerFile(fulldirname,
251  options.dir_name,
252  rootfilename1,rootfilename2,
253  run,
254  black_list,
255  options.stat_test,
256  options.test_threshold,
257  not options.no_successes,
258  options.do_pngs,
259  set(black_listed)
260  )
261 
262  # Start the walker
263  outdir_name=options.outdir_name
264  if run>1 and options.specify_run:
265  outdir_name+="_%s" %run
266  fulldirname+="_%s" %run
267  print "+"*30
268  print "Output Directory will be ", outdir_name
269  options.outdir_name=outdir_name
270  if not exists(outdir_name) and len(outdir_name )>0:
271  mkdir(outdir_name)
272  if len(outdir_name)>0:
273  chdir(outdir_name)
274  dirwalker.walk()
275 
276  run = dirwalker.run
277 
278 
279  # Fetch the directory from the walker
280  directory=dirwalker.directory
281 
282  # Set some meta for the page generation
283  directory.meta.sample=sample
284  directory.meta.run1=run1
285  directory.meta.run2=run2
286  directory.meta.release1=cmssw_release1
287  directory.meta.release2=cmssw_release2
288  directory.meta.tier1=tier1
289  directory.meta.tier2=tier2
290 
291  # Print a summary Report on screen
292  directory.print_report(verbose=True)
293 
294  # Remove this DQM FW reminescence.
295  directory.prune("Run summary")
296 
297  # Dump the directory structure on disk in a pickle
298  original_pickle_name="%s.pkl" %fulldirname
299  print "Pickleing the directory as %s in dir %s" %(original_pickle_name,getcwd())
300  output = open(original_pickle_name,"w")
301  cPickle.dump(directory, output, -1)# use highest protocol available for the pickle
302  output.close()
303 
304 #-------------------------------------------------------------------------------
305 if options.report:
306 
307  if os.environ.has_key("RELMON_SA"):
308  from directories2html import directory2html
309  from dirstructure import Directory
310  else:
311  from Utilities.RelMon.directories2html import directory2html
312  from Utilities.RelMon.dirstructure import Directory
313 
314  from os.path import exists
315  from os import chdir,mkdir
316  import os
317  import cPickle
318 
319  pickle_name=options.pklfile
320  if len(options.pklfile)==0:
321  pickle_name=original_pickle_name
322 
323  print "Reading directory from %s" %(pickle_name)
324  ifile=open(pickle_name,"rb")
325  directory=cPickle.load(ifile)
326  ifile.close()
327 
328  if not options.compare:
329  if not os.path.exists(options.outdir_name):
330  mkdir(options.outdir_name)
331 
332  if exists(options.outdir_name) and len(directory.name)==0:
333  chdir(options.outdir_name)
334 
335  # Calculate the results of the tests for each directory
336  print "Calculating stats for the directory..."
337  directory.calcStats()
338 
339  print "Producing html..."
340  directory2html(directory, options.hash_name)
341 
342 if not (options.report or options.compare):
343  print "Neither comparison nor report to be executed. A typo?"
344 
345 
def getInfoFromFilename
https://twiki.cern.ch/twiki/bin/view/CMSPublic/RelMon
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
double split
Definition: MVATrainer.cc:139
void set(const std::string &name, int value)
set the flag, with a run-time name