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