CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/Utilities/RelMon/scripts/compare_using_files.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 ################################################################################
00003 # https://twiki.cern.ch/twiki/bin/view/CMSPublic/RelMon
00004 #
00005 # $Author: anorkus $
00006 # $Date: 2012/10/25 16:10:22 $
00007 # $Revision: 1.6 $
00008 #
00009 #
00010 # Danilo Piparo CERN - danilo.piparo@cern.ch
00011 #
00012 ################################################################################
00013 
00014 
00015 def getInfoFromFilename(filename):
00016   prefix,sample,cmssw_release,tier = filename[:-5].split("__")[:5]
00017   run=int(prefix.split("_")[-1][1:])
00018   return run,sample,cmssw_release,tier
00019 
00020 from sys import argv,exit
00021 import os
00022 
00023 # Default Configuration Parameters ---------------------------------------------
00024 
00025 stat_test="Chi2"
00026 test_threshold=1e-5
00027 
00028 
00029 #run="1"
00030 
00031 dir_name=""
00032 outdir_name=""
00033 
00034 compare=False
00035 report=False
00036 
00037 do_pngs=False
00038 
00039 black_list_str=""
00040 
00041 #-------------------------------------------------------------------------------
00042 
00043 from optparse import OptionParser
00044 
00045 parser = OptionParser(usage="usage: %prog file1 file2 [options]")
00046 
00047 
00048 #parser.add_option("-r","--run ",
00049                   #action="store",
00050                   #dest="run",
00051                   #default=run,
00052                   #help="The run to be checked \n(default is %s)" %run)
00053 
00054 parser.add_option("-d","--dir_name",
00055                   action="store",
00056                   dest="dir_name",
00057                   default=dir_name,
00058                   help="The 'directory' to be checked in the DQM \n(default is %s)" %dir_name)
00059 
00060 parser.add_option("-o","--outdir_name",
00061                   action="store",
00062                   dest="outdir_name",
00063                   default=outdir_name,
00064                   help="The directory where the output will be stored \n(default is %s)" %outdir_name)
00065 
00066 parser.add_option("-p","--do_pngs",
00067                   action="store_true",
00068                   dest="do_pngs",
00069                   default=False,
00070                   help="Do the pngs of the comparison (takes 50%% of the total running time) \n(default is %s)" %False)
00071 
00072 parser.add_option("--no_successes",
00073                   action="store_true",
00074                   dest="no_successes",
00075                   default=False,
00076                   help="Do not draw successes. Default is False.")
00077 
00078 parser.add_option("-P","--pickle",
00079                   action="store",
00080                   dest="pklfile",
00081                   default="",
00082                   help="Pkl file of the dir structure ")
00083 
00084 parser.add_option("--sample",
00085                   action="store",
00086                   dest="sample",
00087                   default="Sample",
00088                   help="The name of the sample to be displayed")
00089 
00090 parser.add_option("--metas",
00091                   action="store",
00092                   dest="metas",
00093                   default="",
00094                   help="The Metas describing the two files (separated by @@@)")
00095 
00096 parser.add_option("-t","--test_threshold",
00097                   action="store",
00098                   dest="test_threshold",
00099                   default=test_threshold,
00100                   help="Threshold for the statistical test \n(default is %s)" %test_threshold)
00101 
00102 parser.add_option("-s","--stat_test",
00103                   action="store",
00104                   dest="stat_test",
00105                   default=stat_test,
00106                   help="Statistical test (KS or Chi2) \n(default is %s)" %stat_test)  
00107 
00108 parser.add_option("-C","--compare",
00109                   action="store_true",
00110                   dest="compare",
00111                   default=compare,
00112                   help="Make the comparison \n(default is %s)" %compare)
00113 
00114 parser.add_option("-R","--Report",
00115                   action="store_true",
00116                   dest="report",
00117                   default=report,
00118                   help="Make the html report \n(default is %s)" %report)
00119 
00120 parser.add_option("--specify_run",
00121                   action="store_true",
00122                   dest="specify_run",
00123                   default=False,
00124                   help="Append the run number to the output dir for data")
00125 
00126 
00127 parser.add_option("-B","--black_list",
00128                   action="store",
00129                   dest="black_list",
00130                   default=black_list_str,
00131                   help="Blacklist elements. form is name@hierarchy_level (i.e. HLT@1) \n(default is %s)" %black_list_str)
00132                   
00133 ##---HASHING---##
00134 parser.add_option("--hash_name",
00135                   action="store_true",
00136                   dest="hash_name",
00137                   default=False,
00138                   help="Set if you want to minimize & hash the output HTML files.")
00139 ##--Blacklist File --##                  
00140 parser.add_option("--use_black_file",
00141                   action="store_true",
00142                   dest="blacklist_file",
00143                   default=False,
00144                   help="Use a black list file of histograms located @ /RelMon/data")
00145 
00146 def blackListedHistos():
00147         ##GET a black-list file of histograms##
00148     if os.environ.has_key("RELMON_SA"):
00149         black_list_file="../data/blacklist.txt"
00150     else:
00151         black_list_file="%s/src/Utilities/RelMon/data/blacklist.txt"%(os.environ["CMSSW_BASE"])
00152     bListFile = open(black_list_file,'r')
00153     black_listed_histograms = bListFile.read()
00154     bListFile.close()
00155 
00156     histogramArray = black_listed_histograms.split("\n")
00157     histogramArray.remove("")  #remove the last element which is empty line
00158     newarray = []
00159     for elem in histogramArray:
00160         tmp = elem.split("/")  #screw windows as it is being run on lxbuild machines with Linux
00161         tmp.insert(1,"Run summary")  #insert "Run summary" dir in path as in ROOT files they exists but user haven't defined them
00162         newarray.append(("/").join(tmp))
00163     return newarray
00164     ##------##
00165     
00166 (options, args) = parser.parse_args()
00167 
00168 if len(args)!=2 and options.compare:
00169   print "Wrong number of RootFiles specified (%s)" %len(args)
00170   print args
00171   
00172 #-------------------------------------------------------------------------------
00173 original_pickle_name=""
00174 if options.compare:
00175   
00176   if os.environ.has_key("RELMON_SA"):
00177     import definitions  
00178     from dqm_interfaces import DirID,DirWalkerFile,string2blacklist
00179     from dirstructure import Directory
00180   else:
00181     import Utilities.RelMon.definitions as definitions  
00182     from Utilities.RelMon.dqm_interfaces import DirID,DirWalkerFile,string2blacklist
00183     from Utilities.RelMon.dirstructure import Directory
00184 
00185   import cPickle
00186   from os import mkdir,chdir,getcwd
00187   from os.path import exists
00188 
00189   #-------------------------------------------------------------------------------
00190   # Guess Releases and sample from filename
00191   rootfilename1,rootfilename2 = args
00192 
00193   run1=-1
00194   sample1=''
00195   cmssw_release1=''
00196   tier1=''
00197   run2=-1
00198   sample2=''
00199   cmssw_release2=''
00200   tier2=''
00201 
00202   if options.metas=='':
00203     run1,sample1,cmssw_release1,tier1= getInfoFromFilename(rootfilename1)
00204     run2,sample2,cmssw_release2,tier2= getInfoFromFilename(rootfilename2)
00205   else:
00206     print "Reading meta from commandline"
00207     sample1=sample2=options.sample
00208     cmssw_release1,cmssw_release2=options.metas.split('@@@')
00209     
00210   # check if the sample is the same
00211   if sample1!=sample2:
00212     print "I am puzzled. Did you choose two different samples?"
00213     exit(1)
00214   sample = sample1
00215 
00216   # check if the run is the same
00217   if run1!=run2:
00218     print "I am puzzled. Did you choose two different runs?"
00219 #    exit(1)  
00220   run=run1
00221 
00222   fulldirname=options.outdir_name
00223   if len(fulldirname)==0:
00224     fulldirname=options.dir_name
00225   if len(fulldirname)==0:
00226     fulldirname="%s_%s_%s" %(sample1,cmssw_release1,cmssw_release2)
00227 
00228 
00229   black_list=string2blacklist(options.black_list)
00230   
00231   if options.blacklist_file:
00232     black_listed = blackListedHistos()
00233   else:
00234     black_listed = []
00235       
00236 #-------------------------------------------------------------------------------
00237 
00238   print "Analysing Histograms located in directory %s at: " %options.dir_name
00239   for filename in rootfilename1,rootfilename2:
00240     print " o %s" %filename
00241 
00242 
00243   if len(black_list)>0:
00244     print "We have a Blacklist:"
00245     for dirid in black_list:
00246       print " o %s" %dirid
00247 
00248   # Set up the fake directory structure
00249   directory=Directory(options.dir_name)
00250   dirwalker=DirWalkerFile(fulldirname,
00251                           options.dir_name,
00252                           rootfilename1,rootfilename2,
00253                           run,
00254                           black_list,
00255                           options.stat_test,
00256                           options.test_threshold,
00257                           not options.no_successes,
00258                           options.do_pngs,
00259                           set(black_listed)
00260                           )
00261                           
00262   # Start the walker
00263   outdir_name=options.outdir_name
00264   if run>1 and options.specify_run:
00265     outdir_name+="_%s" %run
00266     fulldirname+="_%s" %run
00267   print "+"*30
00268   print "Output Directory will be ", outdir_name
00269   options.outdir_name=outdir_name
00270   if not exists(outdir_name) and len(outdir_name )>0:
00271     mkdir(outdir_name)
00272   if len(outdir_name)>0:
00273     chdir(outdir_name)
00274   dirwalker.walk()
00275 
00276   run =  dirwalker.run
00277 
00278 
00279   # Fetch the directory from the walker
00280   directory=dirwalker.directory
00281 
00282   # Set some meta for the page generation
00283   directory.meta.sample=sample
00284   directory.meta.run1=run1
00285   directory.meta.run2=run2
00286   directory.meta.release1=cmssw_release1
00287   directory.meta.release2=cmssw_release2
00288   directory.meta.tier1=tier1
00289   directory.meta.tier2=tier2
00290 
00291   # Print a summary Report on screen
00292   directory.print_report(verbose=True)
00293 
00294   # Remove this DQM FW reminescence.
00295   directory.prune("Run summary")
00296 
00297   # Dump the directory structure on disk in a pickle
00298   original_pickle_name="%s.pkl" %fulldirname
00299   print "Pickleing the directory as %s in dir %s" %(original_pickle_name,getcwd())
00300   output = open(original_pickle_name,"w")
00301   cPickle.dump(directory, output, -1)# use highest protocol available for the pickle
00302   output.close()
00303 
00304 #-------------------------------------------------------------------------------
00305 if options.report:
00306   
00307   if os.environ.has_key("RELMON_SA"):
00308     from directories2html import directory2html
00309     from dirstructure import Directory
00310   else:
00311     from Utilities.RelMon.directories2html import directory2html
00312     from Utilities.RelMon.dirstructure import Directory
00313 
00314   from os.path import exists
00315   from os import chdir,mkdir
00316   import os
00317   import cPickle    
00318   
00319   pickle_name=options.pklfile
00320   if len(options.pklfile)==0:
00321     pickle_name=original_pickle_name  
00322 
00323   print "Reading directory from %s" %(pickle_name)
00324   ifile=open(pickle_name,"rb")
00325   directory=cPickle.load(ifile)
00326   ifile.close()
00327 
00328   if not options.compare:
00329     if not os.path.exists(options.outdir_name):
00330       mkdir(options.outdir_name)
00331 
00332   if exists(options.outdir_name) and len(directory.name)==0:
00333     chdir(options.outdir_name)
00334   
00335   # Calculate the results of the tests for each directory
00336   print "Calculating stats for the directory..."
00337   directory.calcStats()
00338   
00339   print "Producing html..."
00340   directory2html(directory, options.hash_name)
00341 
00342 if not (options.report or options.compare):
00343   print "Neither comparison nor report to be executed. A typo?"
00344 
00345