CMS 3D CMS Logo

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