CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/Validation/RecoTau/Tools/SteerMultipleCompare.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import sys
00004 import os
00005 import re
00006 from ROOT import *
00007 import MultipleCompare as MultipleCompare
00008 
00009 
00010 __author__  = "Lars Perchalla (lars.perchalla@cern.ch)"
00011 __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
00012 Usage: SteerMultipleCompare.py -T testFile -R refFile [options] [search strings that you want to apply '*' is supported as special character]
00013   see MultiCompare.py for details
00014   """
00015 
00016 def StripPath(name):
00017   path = ''
00018   plot = ''
00019   matches = re.match(r'(.*)\/(.*)$', name)
00020   if matches:
00021     path = matches.group(1)
00022     plot = matches.group(2)
00023   return [path, plot]
00024 
00025 def CreateDirectory(dir,addToExisting=False):
00026   if os.path.exists(dir) and not addToExisting:
00027     print "Output directory %s already exists!  OK to overwrite?" % dir
00028     while True:
00029       input = raw_input("Please enter [y/n] ")
00030       if (input == 'y'):
00031         break
00032       elif (input == 'n'):
00033         print " ...exiting."
00034         sys.exit()
00035   if not os.path.exists(dir):
00036     os.makedirs(dir)
00037 
00038 def CreateBaseDirectory(options):
00039   if options.out == 'MultipleCompare.png' or options.out.find('.')!=-1:
00040     #default case, so no directory was given
00041     #or a filename was given
00042     outputDirName = 'MultipleCompareOutput'
00043   else:
00044     outputDirName = options.out
00045   outputDir = os.path.join(os.getcwd(), outputDirName)
00046   CreateDirectory(outputDir)
00047   return outputDir
00048 
00049 def CreateSubDirectory(basedir, path):
00050   outputDir = os.path.join(basedir, path)
00051   CreateDirectory(outputDir,True)
00052 
00053 def CleanArguments(argv, option):
00054   #remove existing output arguments
00055   while argv.count(option) > 0:
00056     index = argv.index(option)
00057     if index < len(argv)-1:
00058       argv.pop(index+1)#drop the corresponding value
00059     argv.pop(index)#drop the option itself
00060 
00061 
00062 #execute Multicompare for each plot as a comparison one by one
00063 #argv was modified to contain only one plot each
00064 def plotOneByOne(argv, outputDir, histoList, histoSubNames, paths):
00065   for hist, name, path in zip(histoList, histoSubNames, paths):
00066     CreateSubDirectory(outputDir, path)
00067     #now give modified arguments to MultipleCompare
00068     tmpArgv = argv[:]
00069     tmpArgv.append('-o')
00070     tmpArgv.append(outputDir+'/'+path+'/'+name+'.png')
00071     tmpArgv.append(hist)
00072     MultipleCompare.main(tmpArgv)
00073 
00074 def plotDefault(argv, outputDir, name, type, plots, addArgv=[]):
00075   tmpArgv = argv[:]
00076   tmpArgv.append('-o')  
00077   tmpArgv.append(outputDir+'/'+name+type)
00078   tmpArgv.extend(addArgv)
00079   tmpArgv.extend(plots)
00080   MultipleCompare.main(tmpArgv)
00081 
00082 #make some default plots grouping several histograms
00083 def plotDefaults(argv, options, outputDir):
00084   name = 'Validation_'
00085   if options.testLabel != None:
00086     name += options.testLabel+'_'
00087   else:
00088     name += options.test+'_vs_'
00089   if options.refLabel != None:
00090     name += options.refLabel+'_'
00091   else:
00092     name += options.ref+'_'
00093   outputType = '.eps'
00094   additionalArgv = []
00095   if outputDir.find('QCD')!=-1:
00096     additionalArgv.append('-f') #fakerate
00097   plotDefault(argv, outputDir, name, 'LeptonRejectionEffphi'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*Rejection/*Effphi'], additionalArgv)
00098   plotDefault(argv, outputDir, name, 'LeptonRejectionEffeta'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*Rejection/*Effeta'], additionalArgv)
00099   plotDefault(argv, outputDir, name, 'LeptonRejectionEffpt'+outputType,  ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*Rejection/*Effpt'], additionalArgv)
00100 
00101   if outputDir.find('QCD')!=-1:
00102     additionalArgv.append('--logScale')
00103   plotDefault(argv, outputDir, name, 'Effphi'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*Effphi', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*Effphi'], additionalArgv)
00104   plotDefault(argv, outputDir, name, 'Effeta'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*Effeta', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*Effeta'], additionalArgv)
00105   plotDefault(argv, outputDir, name, 'Effpt'+outputType,  ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*Effpt', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*Effpt'], additionalArgv)
00106 
00107   plotDefault(argv, outputDir, name, 'pTRatio_allHadronic'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_pTRatio_allHadronic', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_pTRatio_allHadronic'])
00108   plotDefault(argv, outputDir, name, 'pTRatio_oneProng1Pi0'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_pTRatio_oneProng1Pi0', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_pTRatio_oneProng1Pi0'])
00109   plotDefault(argv, outputDir, name, 'pTRatio_threeProng0Pi0'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_pTRatio_threeProng0Pi0', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_pTRatio_threeProng0Pi0'])
00110 
00111   plotDefault(argv, outputDir, name, 'Size_isolationPFChargedHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_Size_isolationPFChargedHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_Size_isolationPFChargedHadrCands'])
00112   plotDefault(argv, outputDir, name, 'Size_isolationPFNeutrHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_Size_isolationPFNeutrHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_Size_isolationPFNeutrHadrCands'])
00113   plotDefault(argv, outputDir, name, 'Size_isolationPFGammaCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_Size_isolationPFGammaCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_Size_isolationPFGammaCands'])
00114 
00115   plotDefault(argv, outputDir, name, 'SumPt_isolationPFChargedHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_SumPt_isolationPFChargedHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_SumPt_isolationPFChargedHadrCands'])
00116   plotDefault(argv, outputDir, name, 'SumPt_isolationPFNeutrHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_SumPt_isolationPFNeutrHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_SumPt_isolationPFNeutrHadrCands'])
00117   plotDefault(argv, outputDir, name, 'SumPt_isolationPFGammaCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_SumPt_isolationPFGammaCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_SumPt_isolationPFGammaCands'])
00118 
00119 
00120 def main(argv=None):
00121   if argv is None:
00122     argv = sys.argv
00123   
00124   options, toPlot = MultipleCompare.LoadCommandlineOptions(argv)
00125       
00126   gROOT.SetBatch()
00127 
00128   testFile = TFile(options.test)
00129   refFile = None
00130   if options.ref != '':
00131     refFile = TFile(options.ref)
00132 
00133   plotList = []
00134   MultipleCompare.MapDirStructure( testFile,'',plotList)
00135 
00136   if len(plotList)<1:
00137     print '\tError: Please specify at least one histogram. The following ones are available in the root file.'
00138     print plotList
00139     sys.exit()
00140 
00141   histoList = []
00142   histoSubNames = []
00143   paths = []
00144   massiveMode = False
00145   for plot in toPlot:
00146     #clean the arguments. toPlot contains the list of positional arguments leftover after parsing options
00147     argv.remove(plot)
00148     for path in plotList:
00149       if MultipleCompare.Match(plot.lower(),path.lower()):
00150         histoList.append(path)
00151         strippedPath, strippedPlot = StripPath(path)
00152         paths.append(strippedPath)
00153         histoSubNames.append(strippedPlot)
00154         #print histoSubNames[-1]
00155       elif plot.find('massiveMode') != -1:
00156         massiveMode = True
00157 
00158   CleanArguments(argv,'--output')
00159   CleanArguments(argv,'-o')
00160           
00161   outputDir = CreateBaseDirectory(options)
00162 
00163   if massiveMode:
00164     print "Massive mode: scan all subdirs and make plots comparing each histogram one by one."
00165     plotOneByOne(argv, outputDir, histoList, histoSubNames, paths)          
00166   else:
00167     print "Default mode: Make default plot combinations."
00168     plotDefaults(argv, options, outputDir)
00169 
00170 
00171 #only execute main() if manually run
00172 if __name__ == '__main__':
00173   #main(*sys.argv[1:])
00174   # the calls to sys.exit(n) inside main() all become return n.
00175   sys.exit(main())
00176 else:
00177   print "This is ",__name__
00178