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: dpiparo $
6 # $Date: 2013/04/22 13:30:05 $
7 # $Revision: 1.8 $
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 ##-- USE CSS files in web access, for stand-alone usage --##
146 parser.add_option("--standalone",
147  action="store_true",
148  dest="standalone",
149  default=False,
150  help="Makes CSS files accessible over HTTP")
151 
153  ##GET a black-list file of histograms##
154  if os.environ.has_key("RELMON_SA"):
155  black_list_file="../data/blacklist.txt"
156  else:
157  black_list_file="%s/src/Utilities/RelMon/data/blacklist.txt"%(os.environ["CMSSW_BASE"])
158  bListFile = open(black_list_file,'r')
159  black_listed_histograms = bListFile.read()
160  bListFile.close()
161 
162  histogramArray = black_listed_histograms.split("\n")
163  histogramArray.remove("") #remove the last element which is empty line
164  newarray = []
165  for elem in histogramArray:
166  tmp = elem.split("/") #screw windows as it is being run on lxbuild machines with Linux
167  tmp.insert(1,"Run summary") #insert "Run summary" dir in path as in ROOT files they exists but user haven't defined them
168  newarray.append(("/").join(tmp))
169  return newarray
170  ##------##
171 
172 (options, args) = parser.parse_args()
173 
174 if len(args)!=2 and options.compare:
175  print "Wrong number of RootFiles specified (%s)" %len(args)
176  print args
177 
178 #-------------------------------------------------------------------------------
179 original_pickle_name=""
180 if options.compare:
181 
182  if os.environ.has_key("RELMON_SA"):
183  import definitions
184  from dqm_interfaces import DirID,DirWalkerFile,string2blacklist
185  from dirstructure import Directory
186  else:
187  import Utilities.RelMon.definitions as definitions
188  from Utilities.RelMon.dqm_interfaces import DirID,DirWalkerFile,string2blacklist
189  from Utilities.RelMon.dirstructure import Directory
190 
191  import cPickle
192  from os import mkdir,chdir,getcwd
193  from os.path import exists
194 
195  #-------------------------------------------------------------------------------
196  # Guess Releases and sample from filename
197  rootfilename1,rootfilename2 = args
198 
199  run1=-1
200  sample1=''
201  cmssw_release1=''
202  tier1=''
203  run2=-1
204  sample2=''
205  cmssw_release2=''
206  tier2=''
207 
208  if options.metas=='':
209  run1,sample1,cmssw_release1,tier1= getInfoFromFilename(rootfilename1)
210  run2,sample2,cmssw_release2,tier2= getInfoFromFilename(rootfilename2)
211  else:
212  print "Reading meta from commandline"
213  sample1=sample2=options.sample
214  cmssw_release1,cmssw_release2=options.metas.split('@@@')
215  options.standalone = True
216 
217  # check if the sample is the same
218  if sample1!=sample2:
219  print "I am puzzled. Did you choose two different samples?"
220  #exit(1)
221  sample = sample1
222 
223  # check if the run is the same
224  if run1!=run2:
225  print "I am puzzled. Did you choose two different runs?"
226 # exit(1)
227  run=run1
228 
229  fulldirname=options.outdir_name
230  if len(fulldirname)==0:
231  fulldirname=options.dir_name
232  if len(fulldirname)==0:
233  fulldirname="%s_%s_%s" %(sample1,cmssw_release1,cmssw_release2)
234 
235 
236  black_list=string2blacklist(options.black_list)
237 
238  if options.blacklist_file:
239  black_listed = blackListedHistos()
240  else:
241  black_listed = []
242 
243 #-------------------------------------------------------------------------------
244 
245  print "Analysing Histograms located in directory %s at: " %options.dir_name
246  for filename in rootfilename1,rootfilename2:
247  print " o %s" %filename
248 
249 
250  if len(black_list)>0:
251  print "We have a Blacklist:"
252  for dirid in black_list:
253  print " o %s" %dirid
254 
255  # Set up the fake directory structure
256  directory=Directory(options.dir_name)
257  dirwalker=DirWalkerFile(fulldirname,
258  options.dir_name,
259  rootfilename1,rootfilename2,
260  run,
261  black_list,
262  options.stat_test,
263  options.test_threshold,
264  not options.no_successes,
265  options.do_pngs,
266  set(black_listed)
267  )
268 
269  # Start the walker
270  outdir_name=options.outdir_name
271  if run>1 and options.specify_run:
272  outdir_name+="_%s" %run
273  fulldirname+="_%s" %run
274  print "+"*30
275  print "Output Directory will be ", outdir_name
276  options.outdir_name=outdir_name
277  if not exists(outdir_name) and len(outdir_name )>0:
278  mkdir(outdir_name)
279  if len(outdir_name)>0:
280  chdir(outdir_name)
281  dirwalker.walk()
282 
283  run = dirwalker.run
284 
285 
286  # Fetch the directory from the walker
287  directory=dirwalker.directory
288 
289  # Set some meta for the page generation
290  directory.meta.sample1=sample1
291  directory.meta.sample2=sample2
292  directory.meta.run1=run1
293  directory.meta.run2=run2
294  directory.meta.release1=cmssw_release1
295  directory.meta.release2=cmssw_release2
296  directory.meta.tier1=tier1
297  directory.meta.tier2=tier2
298 
299  # Print a summary Report on screen
300  directory.print_report(verbose=True)
301 
302  # Remove this DQM FW reminescence.
303  directory.prune("Run summary")
304 
305  # Dump the directory structure on disk in a pickle
306  original_pickle_name="%s.pkl" %fulldirname
307  print "Pickleing the directory as %s in dir %s" %(original_pickle_name,getcwd())
308  output = open(original_pickle_name,"w")
309  cPickle.dump(directory, output, -1)# use highest protocol available for the pickle
310  output.close()
311 
312 #-------------------------------------------------------------------------------
313 if options.report:
314 
315  if os.environ.has_key("RELMON_SA"):
316  from directories2html import directory2html
317  from dirstructure import Directory
318  else:
319  from Utilities.RelMon.directories2html import directory2html
320  from Utilities.RelMon.dirstructure import Directory
321 
322  from os.path import exists
323  from os import chdir,mkdir
324  import os
325  import cPickle
326 
327  pickle_name=options.pklfile
328  if len(options.pklfile)==0:
329  pickle_name=original_pickle_name
330 
331  print "Reading directory from %s" %(pickle_name)
332  ifile=open(pickle_name,"rb")
333  directory=cPickle.load(ifile)
334  ifile.close()
335 
336  if not options.compare:
337  if not os.path.exists(options.outdir_name):
338  mkdir(options.outdir_name)
339 
340  if exists(options.outdir_name) and len(directory.name)==0:
341  chdir(options.outdir_name)
342 
343  # Calculate the results of the tests for each directory
344  print "Calculating stats for the directory..."
345  directory.calcStats()
346 
347  print "Producing html..."
348  directory2html(directory, options.hash_name, options.standalone)
349 
350 if not (options.report or options.compare):
351  print "Neither comparison nor report to be executed. A typo?"
352 
353 
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