Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-10 22:50:05

0001 #! /usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import sys
0005 import os
0006 import re
0007 from ROOT import *
0008 import MultipleCompare as MultipleCompare
0009 
0010 
0011 __author__  = "Lars Perchalla (lars.perchalla@cern.ch)"
0012 __doc__ = """Script to execute multiple plotting commands via MultipleCompare.py. Switch between massiveMode producing a set of plots comparing each one by one, and defaultMode producing a smaller set of default plot combinations by adding the commandline option massiveMode:\n\n
0013 Usage: SteerMultipleCompare.py -T testFile -R refFile [options] [search strings that you want to apply '*' is supported as special character]
0014   see MultiCompare.py for details
0015   """
0016 
0017 def StripPath(name):
0018   path = ''
0019   plot = ''
0020   matches = re.match(r'(.*)\/(.*)$', name)
0021   if matches:
0022     path = matches.group(1)
0023     plot = matches.group(2)
0024   return [path, plot]
0025 
0026 def CreateDirectory(dir,addToExisting=False):
0027   if os.path.exists(dir) and not addToExisting:
0028     print("Output directory %s already exists!  OK to overwrite?" % dir)
0029     while True:
0030       input = raw_input("Please enter [y/n] ")
0031       if (input == 'y'):
0032         break
0033       elif (input == 'n'):
0034         print(" ...exiting.")
0035         sys.exit()
0036   if not os.path.exists(dir):
0037     os.makedirs(dir)
0038 
0039 def CreateBaseDirectory(options):
0040   if options.out == 'MultipleCompare.png' or options.out.find('.')!=-1:
0041     #default case, so no directory was given
0042     #or a filename was given
0043     outputDirName = 'MultipleCompareOutput'
0044   else:
0045     outputDirName = options.out
0046   outputDir = os.path.join(os.getcwd(), outputDirName)
0047   CreateDirectory(outputDir)
0048   return outputDir
0049 
0050 def CreateSubDirectory(basedir, path):
0051   outputDir = os.path.join(basedir, path)
0052   CreateDirectory(outputDir,True)
0053 
0054 def CleanArguments(argv, option):
0055   #remove existing output arguments
0056   while argv.count(option) > 0:
0057     index = argv.index(option)
0058     if index < len(argv)-1:
0059       argv.pop(index+1)#drop the corresponding value
0060     argv.pop(index)#drop the option itself
0061 
0062 
0063 #execute Multicompare for each plot as a comparison one by one
0064 #argv was modified to contain only one plot each
0065 def plotOneByOne(argv, outputDir, histoList, histoSubNames, paths):
0066   for hist, name, path in zip(histoList, histoSubNames, paths):
0067     CreateSubDirectory(outputDir, path)
0068     #now give modified arguments to MultipleCompare
0069     tmpArgv = argv[:]
0070     tmpArgv.append('-o')
0071     tmpArgv.append(outputDir+'/'+path+'/'+name+'.png')
0072     tmpArgv.append(hist)
0073     MultipleCompare.main(tmpArgv)
0074 
0075 def plotDefault(argv, outputDir, name, type, plots, addArgv=[]):
0076   tmpArgv = argv[:]
0077   tmpArgv.append('-o')  
0078   tmpArgv.append(outputDir+'/'+name+type)
0079   tmpArgv.extend(addArgv)
0080   tmpArgv.extend(plots)
0081   MultipleCompare.main(tmpArgv)
0082 
0083 #make some default plots grouping several histograms
0084 def plotDefaults(argv, options, outputDir):
0085   name = 'Validation_'
0086   if options.testLabel != None:
0087     name += options.testLabel+'_'
0088   else:
0089     name += options.test+'_vs_'
0090   if options.refLabel != None:
0091     name += options.refLabel+'_'
0092   else:
0093     name += options.ref+'_'
0094   outputType = '.eps'
0095   additionalArgv = []
0096   if outputDir.find('QCD')!=-1:
0097     additionalArgv.append('-f') #fakerate
0098   plotDefault(argv, outputDir, name, 'LeptonRejectionEffphi'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*Rejection/*Effphi'], additionalArgv)
0099   plotDefault(argv, outputDir, name, 'LeptonRejectionEffeta'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*Rejection/*Effeta'], additionalArgv)
0100   plotDefault(argv, outputDir, name, 'LeptonRejectionEffpt'+outputType,  ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*Rejection/*Effpt'], additionalArgv)
0101 
0102   if outputDir.find('QCD')!=-1:
0103     additionalArgv.append('--logScale')
0104   plotDefault(argv, outputDir, name, 'Effphi'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*Effphi', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*Effphi'], additionalArgv)
0105   plotDefault(argv, outputDir, name, 'Effeta'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*Effeta', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*Effeta'], additionalArgv)
0106   plotDefault(argv, outputDir, name, 'Effpt'+outputType,  ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*Effpt', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*Effpt'], additionalArgv)
0107 
0108   plotDefault(argv, outputDir, name, 'pTRatio_allHadronic'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_pTRatio_allHadronic', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_pTRatio_allHadronic'])
0109   plotDefault(argv, outputDir, name, 'pTRatio_oneProng1Pi0'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_pTRatio_oneProng1Pi0', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_pTRatio_oneProng1Pi0'])
0110   plotDefault(argv, outputDir, name, 'pTRatio_threeProng0Pi0'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_pTRatio_threeProng0Pi0', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_pTRatio_threeProng0Pi0'])
0111 
0112   plotDefault(argv, outputDir, name, 'Size_isolationPFChargedHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_Size_isolationPFChargedHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_Size_isolationPFChargedHadrCands'])
0113   plotDefault(argv, outputDir, name, 'Size_isolationPFNeutrHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_Size_isolationPFNeutrHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_Size_isolationPFNeutrHadrCands'])
0114   plotDefault(argv, outputDir, name, 'Size_isolationPFGammaCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_Size_isolationPFGammaCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_Size_isolationPFGammaCands'])
0115 
0116   plotDefault(argv, outputDir, name, 'SumPt_isolationPFChargedHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_SumPt_isolationPFChargedHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_SumPt_isolationPFChargedHadrCands'])
0117   plotDefault(argv, outputDir, name, 'SumPt_isolationPFNeutrHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_SumPt_isolationPFNeutrHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_SumPt_isolationPFNeutrHadrCands'])
0118   plotDefault(argv, outputDir, name, 'SumPt_isolationPFGammaCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_SumPt_isolationPFGammaCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_SumPt_isolationPFGammaCands'])
0119 
0120 
0121 def main(argv=None):
0122   if argv is None:
0123     argv = sys.argv
0124   
0125   options, toPlot = MultipleCompare.LoadCommandlineOptions(argv)
0126       
0127   gROOT.SetBatch()
0128 
0129   testFile = TFile(options.test)
0130   refFile = None
0131   if options.ref != '':
0132     refFile = TFile(options.ref)
0133 
0134   plotList = []
0135   MultipleCompare.MapDirStructure( testFile,'',plotList)
0136 
0137   if len(plotList)<1:
0138     print('\tError: Please specify at least one histogram. The following ones are available in the root file.')
0139     print(plotList)
0140     sys.exit()
0141 
0142   histoList = []
0143   histoSubNames = []
0144   paths = []
0145   massiveMode = False
0146   for plot in toPlot:
0147     #clean the arguments. toPlot contains the list of positional arguments leftover after parsing options
0148     argv.remove(plot)
0149     for path in plotList:
0150       if MultipleCompare.Match(plot.lower(),path.lower()):
0151         histoList.append(path)
0152         strippedPath, strippedPlot = StripPath(path)
0153         paths.append(strippedPath)
0154         histoSubNames.append(strippedPlot)
0155         #print histoSubNames[-1]
0156       elif plot.find('massiveMode') != -1:
0157         massiveMode = True
0158 
0159   CleanArguments(argv,'--output')
0160   CleanArguments(argv,'-o')
0161           
0162   outputDir = CreateBaseDirectory(options)
0163 
0164   if massiveMode:
0165     print("Massive mode: scan all subdirs and make plots comparing each histogram one by one.")
0166     plotOneByOne(argv, outputDir, histoList, histoSubNames, paths)          
0167   else:
0168     print("Default mode: Make default plot combinations.")
0169     plotDefaults(argv, options, outputDir)
0170 
0171 
0172 #only execute main() if manually run
0173 if __name__ == '__main__':
0174   #main(*sys.argv[1:])
0175   # the calls to sys.exit(n) inside main() all become return n.
0176   sys.exit(main())
0177 else:
0178   print("This is ",__name__)
0179