Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:35

0001 #! /usr/bin/env python3
0002 ################################################################################
0003 # https://twiki.cern.ch/twiki/bin/view/CMSPublic/RelMon
0004 #
0005 #
0006 #
0007 # Danilo Piparo CERN - danilo.piparo@cern.ch
0008 #
0009 ################################################################################
0010 
0011 
0012 def getInfoFromFilename(filename):
0013   prefix,sample,cmssw_release,tier = filename[:-5].split("__")[:5]
0014   run=int(prefix.split("_")[-1][1:])
0015   return run,sample,cmssw_release,tier
0016 
0017 from sys import argv,exit
0018 import os
0019 
0020 # Default Configuration Parameters ---------------------------------------------
0021 
0022 stat_test="Chi2"
0023 test_threshold=1e-5
0024 
0025 
0026 #run="1"
0027 
0028 dir_name=""
0029 outdir_name=""
0030 
0031 compare=False
0032 report=False
0033 
0034 do_pngs=False
0035 
0036 black_list_str=""
0037 
0038 #-------------------------------------------------------------------------------
0039 
0040 from optparse import OptionParser
0041 
0042 parser = OptionParser(usage="usage: %prog file1 file2 [options]")
0043 
0044 
0045 #parser.add_option("-r","--run ",
0046                   #action="store",
0047                   #dest="run",
0048                   #default=run,
0049                   #help="The run to be checked \n(default is %s)" %run)
0050 
0051 parser.add_option("-d","--dir_name",
0052                   action="store",
0053                   dest="dir_name",
0054                   default=dir_name,
0055                   help="The 'directory' to be checked in the DQM \n(default is %s)" %dir_name)
0056 
0057 parser.add_option("-o","--outdir_name",
0058                   action="store",
0059                   dest="outdir_name",
0060                   default=outdir_name,
0061                   help="The directory where the output will be stored \n(default is %s)" %outdir_name)
0062 
0063 parser.add_option("-p","--do_pngs",
0064                   action="store_true",
0065                   dest="do_pngs",
0066                   default=False,
0067                   help="Do the pngs of the comparison (takes 50%% of the total running time) \n(default is %s)" %False)
0068 
0069 parser.add_option("--no_successes",
0070                   action="store_true",
0071                   dest="no_successes",
0072                   default=False,
0073                   help="Do not draw successes. Default is False.")
0074 
0075 parser.add_option("-P","--pickle",
0076                   action="store",
0077                   dest="pklfile",
0078                   default="",
0079                   help="Pkl file of the dir structure ")
0080 
0081 parser.add_option("--sample",
0082                   action="store",
0083                   dest="sample",
0084                   default="Sample",
0085                   help="The name of the sample to be displayed")
0086 
0087 parser.add_option("--metas",
0088                   action="store",
0089                   dest="metas",
0090                   default="",
0091                   help="The Metas describing the two files (separated by @@@)")
0092 
0093 parser.add_option("-t","--test_threshold",
0094                   action="store",
0095                   dest="test_threshold",
0096                   default=test_threshold,
0097                   help="Threshold for the statistical test \n(default is %s)" %test_threshold)
0098 
0099 parser.add_option("-s","--stat_test",
0100                   action="store",
0101                   dest="stat_test",
0102                   default=stat_test,
0103                   help="Statistical test (KS or Chi2) \n(default is %s)" %stat_test)  
0104 
0105 parser.add_option("-C","--compare",
0106                   action="store_true",
0107                   dest="compare",
0108                   default=compare,
0109                   help="Make the comparison \n(default is %s)" %compare)
0110 
0111 parser.add_option("-R","--Report",
0112                   action="store_true",
0113                   dest="report",
0114                   default=report,
0115                   help="Make the html report \n(default is %s)" %report)
0116 
0117 parser.add_option("--specify_run",
0118                   action="store_true",
0119                   dest="specify_run",
0120                   default=False,
0121                   help="Append the run number to the output dir for data")
0122 
0123 
0124 parser.add_option("-B","--black_list",
0125                   action="store",
0126                   dest="black_list",
0127                   default=black_list_str,
0128                   help="Blacklist elements. form is name@hierarchy_level (i.e. HLT@1) \n(default is %s)" %black_list_str)
0129                   
0130 ##---HASHING---##
0131 parser.add_option("--hash_name",
0132                   action="store_true",
0133                   dest="hash_name",
0134                   default=False,
0135                   help="Set if you want to minimize & hash the output HTML files.")
0136 ##--Blacklist File --##                  
0137 parser.add_option("--use_black_file",
0138                   action="store_true",
0139                   dest="blacklist_file",
0140                   default=False,
0141                   help="Use a black list file of histograms located @ /RelMon/data")
0142 ##-- USE CSS files in web access, for stand-alone usage --##
0143 parser.add_option("--standalone",
0144                   action="store_true",
0145                   dest="standalone",
0146                   default=False,
0147                   help="Makes CSS files accessible over HTTP")
0148 
0149 def blackListedHistos():
0150         ##GET a black-list file of histograms##
0151     if "RELMON_SA" in os.environ:
0152         black_list_file="../data/blacklist.txt"
0153     else:
0154         black_list_file="%s/src/Utilities/RelMon/data/blacklist.txt"%(os.environ["CMSSW_BASE"])
0155     bListFile = open(black_list_file,'r')
0156     black_listed_histograms = bListFile.read()
0157     bListFile.close()
0158 
0159     histogramArray = black_listed_histograms.split("\n")
0160     histogramArray.remove("")  #remove the last element which is empty line
0161     newarray = []
0162     for elem in histogramArray:
0163         tmp = elem.split("/")  #screw windows as it is being run on lxbuild machines with Linux
0164         tmp.insert(1,"Run summary")  #insert "Run summary" dir in path as in ROOT files they exists but user haven't defined them
0165         newarray.append(("/").join(tmp))
0166     return newarray
0167     ##------##
0168     
0169 (options, args) = parser.parse_args()
0170 
0171 if len(args)!=2 and options.compare:
0172   print("Wrong number of RootFiles specified (%s)" %len(args))
0173   print(args)
0174   
0175 #-------------------------------------------------------------------------------
0176 original_pickle_name=""
0177 if options.compare:
0178   
0179   if "RELMON_SA" in os.environ:
0180     import definitions  
0181     from dqm_interfaces import DirID,DirWalkerFile,string2blacklist
0182     from dirstructure import Directory
0183   else:
0184     import Utilities.RelMon.definitions as definitions  
0185     from Utilities.RelMon.dqm_interfaces import DirID,DirWalkerFile,string2blacklist
0186     from Utilities.RelMon.dirstructure import Directory
0187 
0188   import pickle
0189   from os import mkdir,chdir,getcwd
0190   from os.path import exists
0191 
0192   #-------------------------------------------------------------------------------
0193   # Guess Releases and sample from filename
0194   rootfilename1,rootfilename2 = args
0195 
0196   run1=-1
0197   sample1=''
0198   cmssw_release1=''
0199   tier1=''
0200   run2=-1
0201   sample2=''
0202   cmssw_release2=''
0203   tier2=''
0204 
0205   if options.metas=='':
0206     run1,sample1,cmssw_release1,tier1= getInfoFromFilename(rootfilename1)
0207     run2,sample2,cmssw_release2,tier2= getInfoFromFilename(rootfilename2)
0208   else:
0209     print("Reading meta from commandline")
0210     sample1=sample2=options.sample
0211     cmssw_release1,cmssw_release2=options.metas.split('@@@')
0212     options.standalone = True
0213     
0214   # check if the sample is the same
0215   if sample1!=sample2:
0216     print("I am puzzled. Did you choose two different samples?")
0217     #exit(1)
0218   sample = sample1
0219 
0220   # check if the run is the same
0221   if run1!=run2:
0222     print("I am puzzled. Did you choose two different runs?")
0223 #    exit(1)  
0224   run=run1
0225 
0226   fulldirname=options.outdir_name
0227   if len(fulldirname)==0:
0228     fulldirname=options.dir_name
0229   if len(fulldirname)==0:
0230     fulldirname="%s_%s_%s" %(sample1,cmssw_release1,cmssw_release2)
0231 
0232 
0233   black_list=string2blacklist(options.black_list)
0234   
0235   if options.blacklist_file:
0236     black_listed = blackListedHistos()
0237   else:
0238     black_listed = []
0239       
0240 #-------------------------------------------------------------------------------
0241 
0242   print("Analysing Histograms located in directory %s at: " %options.dir_name)
0243   for filename in rootfilename1,rootfilename2:
0244     print(" o %s" %filename)
0245 
0246 
0247   if len(black_list)>0:
0248     print("We have a Blacklist:")
0249     for dirid in black_list:
0250       print(" o %s" %dirid)
0251 
0252   # Set up the fake directory structure
0253   directory=Directory(options.dir_name)
0254   dirwalker=DirWalkerFile(fulldirname,
0255                           options.dir_name,
0256                           rootfilename1,rootfilename2,
0257                           run,
0258                           black_list,
0259                           options.stat_test,
0260                           options.test_threshold,
0261                           not options.no_successes,
0262                           options.do_pngs,
0263                           set(black_listed)
0264                           )
0265                           
0266   # Start the walker
0267   outdir_name=options.outdir_name
0268   if run>1 and options.specify_run:
0269     outdir_name+="_%s" %run
0270     fulldirname+="_%s" %run
0271   print("+"*30)
0272   print("Output Directory will be ", outdir_name)
0273   options.outdir_name=outdir_name
0274   if not exists(outdir_name) and len(outdir_name )>0:
0275     mkdir(outdir_name)
0276   if len(outdir_name)>0:
0277     chdir(outdir_name)
0278   dirwalker.walk()
0279 
0280   run =  dirwalker.run
0281 
0282 
0283   # Fetch the directory from the walker
0284   directory=dirwalker.directory
0285 
0286   # Set some meta for the page generation
0287   directory.meta.sample1=sample1
0288   directory.meta.sample2=sample2
0289   directory.meta.run1=run1
0290   directory.meta.run2=run2
0291   directory.meta.release1=cmssw_release1
0292   directory.meta.release2=cmssw_release2
0293   directory.meta.tier1=tier1
0294   directory.meta.tier2=tier2
0295 
0296   # Print a summary Report on screen
0297   directory.print_report(verbose=True)
0298 
0299   # Remove this DQM FW reminescence.
0300   directory.prune("Run summary")
0301 
0302   # Dump the directory structure on disk in a pickle
0303   original_pickle_name="%s.pkl" %fulldirname
0304   print("Pickleing the directory as %s in dir %s" %(original_pickle_name,getcwd()))
0305   output = open(original_pickle_name,"wb")
0306   pickle.dump(directory, output, -1)# use highest protocol available for the pickle
0307   output.close()
0308 
0309 #-------------------------------------------------------------------------------
0310 if options.report:
0311   
0312   if "RELMON_SA" in os.environ:
0313     from directories2html import directory2html
0314     from dirstructure import Directory
0315   else:
0316     from Utilities.RelMon.directories2html import directory2html
0317     from Utilities.RelMon.dirstructure import Directory
0318 
0319   from os.path import exists
0320   from os import chdir,mkdir
0321   import os
0322   import pickle    
0323   
0324   pickle_name=options.pklfile
0325   if len(options.pklfile)==0:
0326     pickle_name=original_pickle_name  
0327 
0328   print("Reading directory from %s" %(pickle_name))
0329   ifile=open(pickle_name,"rb")
0330   directory=pickle.load(ifile)
0331   ifile.close()
0332 
0333   if not options.compare:
0334     if not os.path.exists(options.outdir_name):
0335       mkdir(options.outdir_name)
0336 
0337   if exists(options.outdir_name) and len(directory.name)==0:
0338     chdir(options.outdir_name)
0339   
0340   # Calculate the results of the tests for each directory
0341   print("Calculating stats for the directory...")
0342   directory.calcStats()
0343   
0344   print("Producing html...")
0345   directory2html(directory, options.hash_name, options.standalone)
0346 
0347 if not (options.report or options.compare):
0348   print("Neither comparison nor report to be executed. A typo?")
0349 
0350