CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
compare_using_db.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 ################################################################################
3 # RelMon: a tool for automatic Release Comparison
4 # https://twiki.cern.ch/twiki/bin/view/CMSPublic/RelMon
5 #
6 # $Author: dpiparo $
7 # $Date: 2012/06/13 07:25:01 $
8 # $Revision: 1.2 $
9 #
10 #
11 # Danilo Piparo CERN - danilo.piparo@cern.ch
12 #
13 ################################################################################
14 
15 from sys import argv,exit
16 from optparse import OptionParser
17 import cPickle
18 import os
19 
20 # Default Configuration Parameters ---------------------------------------------
21 dqm_server='https://cmsweb.cern.ch/dqm/relval'
22 
23 cmssw_release1="CMSSW_5_3_0-START53_V4-v1"
24 cmssw_release2="CMSSW_5_3_1-START53_V5-v1"
25 
26 stat_test="Chi2"
27 test_threshold=0.00001
28 
29 sample = "RelValZMM"
30 
31 run1="1"
32 run2="1"
33 
34 dir_name="00 Shift"
35 outdir_name=""
36 
37 do_pngs=False
38 
39 compare=False
40 report=False
41 
42 black_list_str=""
43 
44 tiers="DQM,DQM"
45 
46 #-------------------------------------------------------------------------------
47 
48 parser = OptionParser(usage="usage: %prog [options]")
49 
50 parser.add_option("-1","--release1",
51  action="store",
52  dest="cmssw_release1",
53  default=cmssw_release1,
54  help="The main CMSSW release \n(default is %s)" %cmssw_release1)
55 
56 parser.add_option("-2","--release2",
57  action="store",
58  dest="cmssw_release2",
59  default=cmssw_release2,
60  help="The CMSSW release for the regression \n(default is %s)" %cmssw_release2)
61 
62 parser.add_option("-S","--sample",
63  action="store",
64  dest="sample",
65  default=sample,
66  help="The Sample upon which you want to run \n(default is %s)" %sample)
67 
68 parser.add_option("-o","--outdir_name",
69  action="store",
70  dest="outdir_name",
71  default=outdir_name,
72  help="The directory where the output will be stored \n(default is %s)" %outdir_name)
73 
74 parser.add_option("-D","--dqm_server",
75  action="store",
76  dest="dqm_server",
77  default=dqm_server,
78  help="The DQM server \n(default is %s)" %dqm_server)
79 
80 parser.add_option("-a","--run1 ",
81  action="store",
82  dest="run1",
83  default=run1,
84  help="The run of the first sample to be checked \n(default is %s)" %run1)
85 
86 parser.add_option("-b","--run2",
87  action="store",
88  dest="run2",
89  default=run2,
90  help="The run of the second sample to be checked \n(default is %s)" %run2)
91 
92 parser.add_option("-d","--dir_name",
93  action="store",
94  dest="dir_name",
95  default=dir_name,
96  help="The 'directory' to be checked in the DQM \n(default is %s)" %dir_name)
97 
98 parser.add_option("-p","--do_pngs",
99  action="store_true",
100  dest="do_pngs",
101  default=False,
102  help="EXPERIMENTAL!!! Do the pngs of the comparison (takes 50%% of the total running time) \n(default is %s)" %False)
103 
104 parser.add_option("-P","--pickle",
105  action="store",
106  dest="pklfile",
107  default="",
108  help="Pkl file of the dir structure ")
109 parser.add_option("-t","--test_threshold",
110  action="store",
111  dest="test_threshold",
112  default=test_threshold,
113  help="Threshold for the statistical test \n(default is %s)" %test_threshold)
114 
115 parser.add_option("-s","--stat_test",
116  action="store",
117  dest="stat_test",
118  default=stat_test,
119  help="Statistical test (KS or Chi2) \n(default is %s)" %stat_test)
120 
121 parser.add_option("-C","--compare",
122  action="store_true",
123  dest="compare",
124  default=compare,
125  help="Make the comparison \n(default is %s)" %compare)
126 
127 parser.add_option("-R","--Report",
128  action="store_true",
129  dest="report",
130  default=report,
131  help="Make the html report \n(default is %s)" %report)
132 
133 parser.add_option("-T","--Tiers",
134  action="store",
135  dest="tiers",
136  default=tiers,
137  help="Data tiers (comma separated list) \n(default is %s)" %tiers)
138 
139 parser.add_option("-B","--black_list",
140  action="store",
141  dest="black_list",
142  default=black_list_str,
143  help="Blacklist elements. form is name@hierarchy_level (i.e. HLT@1) \n(default is %s)" %black_list_str)
144 
145 (options, args) = parser.parse_args()
146 
147 #-------------------------------------------------------------------------------
148 original_pickle_name=""
149 if options.compare:
150 
151  if os.environ.has_key("RELMON_SA"):
152  from dqm_interfaces import DirID,DQMcommunicator,DirWalkerDB
153  from dirstructure import Directory
154  else:
155  from Utilities.RelMon.dqm_interfaces import DirID,DQMcommunicator,DirWalkerDB
156  from Utilities.RelMon.dirstructure import Directory
157 
158 
159  # Pre-process the inputs
160  fulldirname=options.outdir_name
161  if len(fulldirname)==0:
162  fulldirname=options.dir_name
163  if len(fulldirname)==0:
164  fulldirname="%s_%s_%s" %(sample1,cmssw_release1,cmssw_release2)
165 
166 
167  black_list=[]
168  black_list_str=options.black_list
169  if len(black_list_str)>0:
170  for ele in black_list_str.split(","):
171  dirname,level=ele.split("@")
172  level=int(level)
173  black_list.append(DirID(dirname,level))
174 
175  db_base_url="/data/json/archive/"
176  base1="%s/%s/%s/%s/DQM/" %(db_base_url,options.run1,options.sample,options.cmssw_release1)
177  base2="%s/%s/%s/%s/DQM/" %(db_base_url,options.run2,options.sample,options.cmssw_release2)
178 
179 
180  print "Analysing Histograms located in directory %s at: " %options.dir_name
181  for base in base1,base2:
182  print " o %s (server= %s)" %(base,options.dqm_server)
183 
184  # Set up the communicators
185  comm1 = DQMcommunicator(server=options.dqm_server)
186  comm2 = DQMcommunicator(server=options.dqm_server)
187 
188  # Set up the fake directory structure
189  directory=Directory(options.dir_name)
190  dirwalker=DirWalkerDB(comm1,comm2,base1,base2,directory)
191 
192  # Set the production of pngs on and off
193  dirwalker.do_pngs=options.do_pngs
194 
195  # set the stat test
196  dirwalker.stat_test=options.stat_test
197  dirwalker.test_threshold=options.test_threshold
198 
199  # Set the blacklist, if needed
200  if len(black_list)>0:
201  print "We have a Blacklist:"
202  for dirid in black_list:
203  print " o %s" %dirid
204  dirwalker.black_list=black_list
205 
206  # Start the walker
207  if not os.path.exists(options.outdir_name) and len(options.outdir_name )>0:
208  os.mkdir(options.outdir_name)
209  if len(options.outdir_name)>0:
210  os.chdir(options.outdir_name)
211 
212  # Since the walker is a thread, run it!
213  dirwalker.start()
214  # And wait until it is finished :)
215  dirwalker.join()
216 
217  # Fetch the directory from the walker
218  directory=dirwalker.directory
219 
220  # Set some meta for the page generation
221  directory.meta.sample=options.sample
222  directory.meta.run1=options.run1
223  directory.meta.run2=options.run2
224  directory.meta.release1=options.cmssw_release1
225  directory.meta.release2=options.cmssw_release2
226 
227  directory.meta.tier1,directory.meta.tier2 = options.tiers.split(",")
228 
229  # Print a summary Report on screen
230  directory.print_report()
231 
232  # Dump the directory structure on disk in a pickle
233  original_pickle_name="%s.pkl" %fulldirname
234  print "Pickleing the directory as %s in dir %s" %(original_pickle_name,os.getcwd())
235  output = open(original_pickle_name,"w")
236  cPickle.dump(directory, output, -1)# use highest protocol available for the pickle
237  output.close()
238 
239 #-------------------------------------------------------------------------------
240 if options.report:
241  if os.environ.has_key("RELMON_SA"):
242  from directories2html import directory2html
243  from dirstructure import Directory
244  else:
245  from Utilities.RelMon.directories2html import directory2html
246  from Utilities.RelMon.dirstructure import Directory
247 
248  pickle_name=options.pklfile
249  if len(options.pklfile)==0:
250  pickle_name=original_pickle_name
251 
252  print "Reading directory from %s" %(pickle_name)
253  ifile=open(pickle_name,"rb")
254  directory=cPickle.load(ifile)
255  ifile.close()
256 
257  if os.path.exists(options.outdir_name) and len(directory.name)==0:
258  os.chdir(options.outdir_name)
259 
260  # Calculate the results of the tests for each directory
261  print "Calculating stats for the directory..."
262  directory.calcStats()
263 
264  print "Producing html..."
265  directory2html(directory)
266 
267 if not (options.report or options.compare):
268  print "Neither comparison nor report to be executed. A typo?"
269