CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SteerMultipleCompare.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import sys
4 import os
5 import re
6 from ROOT import *
7 import MultipleCompare as MultipleCompare
8 
9 
10 __author__ = "Lars Perchalla (lars.perchalla@cern.ch)"
11 __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
12 Usage: SteerMultipleCompare.py -T testFile -R refFile [options] [search strings that you want to apply '*' is supported as special character]
13  see MultiCompare.py for details
14  """
15 
16 def StripPath(name):
17  path = ''
18  plot = ''
19  matches = re.match(r'(.*)\/(.*)$', name)
20  if matches:
21  path = matches.group(1)
22  plot = matches.group(2)
23  return [path, plot]
24 
25 def CreateDirectory(dir,addToExisting=False):
26  if os.path.exists(dir) and not addToExisting:
27  print "Output directory %s already exists! OK to overwrite?" % dir
28  while True:
29  input = raw_input("Please enter [y/n] ")
30  if (input == 'y'):
31  break
32  elif (input == 'n'):
33  print " ...exiting."
34  sys.exit()
35  if not os.path.exists(dir):
36  os.makedirs(dir)
37 
38 def CreateBaseDirectory(options):
39  if options.out == 'MultipleCompare.png' or options.out.find('.')!=-1:
40  #default case, so no directory was given
41  #or a filename was given
42  outputDirName = 'MultipleCompareOutput'
43  else:
44  outputDirName = options.out
45  outputDir = os.path.join(os.getcwd(), outputDirName)
46  CreateDirectory(outputDir)
47  return outputDir
48 
49 def CreateSubDirectory(basedir, path):
50  outputDir = os.path.join(basedir, path)
51  CreateDirectory(outputDir,True)
52 
53 def CleanArguments(argv, option):
54  #remove existing output arguments
55  while argv.count(option) > 0:
56  index = argv.index(option)
57  if index < len(argv)-1:
58  argv.pop(index+1)#drop the corresponding value
59  argv.pop(index)#drop the option itself
60 
61 
62 #execute Multicompare for each plot as a comparison one by one
63 #argv was modified to contain only one plot each
64 def plotOneByOne(argv, outputDir, histoList, histoSubNames, paths):
65  for hist, name, path in zip(histoList, histoSubNames, paths):
66  CreateSubDirectory(outputDir, path)
67  #now give modified arguments to MultipleCompare
68  tmpArgv = argv[:]
69  tmpArgv.append('-o')
70  tmpArgv.append(outputDir+'/'+path+'/'+name+'.png')
71  tmpArgv.append(hist)
72  MultipleCompare.main(tmpArgv)
73 
74 def plotDefault(argv, outputDir, name, type, plots, addArgv=[]):
75  tmpArgv = argv[:]
76  tmpArgv.append('-o')
77  tmpArgv.append(outputDir+'/'+name+type)
78  tmpArgv.extend(addArgv)
79  tmpArgv.extend(plots)
80  MultipleCompare.main(tmpArgv)
81 
82 #make some default plots grouping several histograms
83 def plotDefaults(argv, options, outputDir):
84  name = 'Validation_'
85  if options.testLabel != None:
86  name += options.testLabel+'_'
87  else:
88  name += options.test+'_vs_'
89  if options.refLabel != None:
90  name += options.refLabel+'_'
91  else:
92  name += options.ref+'_'
93  outputType = '.eps'
94  additionalArgv = []
95  if outputDir.find('QCD')!=-1:
96  additionalArgv.append('-f') #fakerate
97  plotDefault(argv, outputDir, name, 'LeptonRejectionEffphi'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*Rejection/*Effphi'], additionalArgv)
98  plotDefault(argv, outputDir, name, 'LeptonRejectionEffeta'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*Rejection/*Effeta'], additionalArgv)
99  plotDefault(argv, outputDir, name, 'LeptonRejectionEffpt'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*Rejection/*Effpt'], additionalArgv)
100 
101  if outputDir.find('QCD')!=-1:
102  additionalArgv.append('--logScale')
103  plotDefault(argv, outputDir, name, 'Effphi'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*Effphi', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*Effphi'], additionalArgv)
104  plotDefault(argv, outputDir, name, 'Effeta'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*Effeta', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*Effeta'], additionalArgv)
105  plotDefault(argv, outputDir, name, 'Effpt'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*Effpt', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*Effpt'], additionalArgv)
106 
107  plotDefault(argv, outputDir, name, 'pTRatio_allHadronic'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_pTRatio_allHadronic', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_pTRatio_allHadronic'])
108  plotDefault(argv, outputDir, name, 'pTRatio_oneProng1Pi0'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_pTRatio_oneProng1Pi0', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_pTRatio_oneProng1Pi0'])
109  plotDefault(argv, outputDir, name, 'pTRatio_threeProng0Pi0'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_pTRatio_threeProng0Pi0', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_pTRatio_threeProng0Pi0'])
110 
111  plotDefault(argv, outputDir, name, 'Size_isolationPFChargedHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_Size_isolationPFChargedHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_Size_isolationPFChargedHadrCands'])
112  plotDefault(argv, outputDir, name, 'Size_isolationPFNeutrHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_Size_isolationPFNeutrHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_Size_isolationPFNeutrHadrCands'])
113  plotDefault(argv, outputDir, name, 'Size_isolationPFGammaCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_Size_isolationPFGammaCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_Size_isolationPFGammaCands'])
114 
115  plotDefault(argv, outputDir, name, 'SumPt_isolationPFChargedHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_SumPt_isolationPFChargedHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_SumPt_isolationPFChargedHadrCands'])
116  plotDefault(argv, outputDir, name, 'SumPt_isolationPFNeutrHadrCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_SumPt_isolationPFNeutrHadrCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_SumPt_isolationPFNeutrHadrCands'])
117  plotDefault(argv, outputDir, name, 'SumPt_isolationPFGammaCands'+outputType, ['DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationByDecayModeFinding/*_SumPt_isolationPFGammaCands', 'DQMData/RecoTauV/hpsPFTauProducer_hpsPFTauDiscriminationBy*CombinedIsolationDBSumPtCorr/*_SumPt_isolationPFGammaCands'])
118 
119 
120 def main(argv=None):
121  if argv is None:
122  argv = sys.argv
123 
124  options, toPlot = MultipleCompare.LoadCommandlineOptions(argv)
125 
126  gROOT.SetBatch()
127 
128  testFile = TFile(options.test)
129  refFile = None
130  if options.ref != '':
131  refFile = TFile(options.ref)
132 
133  plotList = []
134  MultipleCompare.MapDirStructure( testFile,'',plotList)
135 
136  if len(plotList)<1:
137  print '\tError: Please specify at least one histogram. The following ones are available in the root file.'
138  print plotList
139  sys.exit()
140 
141  histoList = []
142  histoSubNames = []
143  paths = []
144  massiveMode = False
145  for plot in toPlot:
146  #clean the arguments. toPlot contains the list of positional arguments leftover after parsing options
147  argv.remove(plot)
148  for path in plotList:
149  if MultipleCompare.Match(plot.lower(),path.lower()):
150  histoList.append(path)
151  strippedPath, strippedPlot = StripPath(path)
152  paths.append(strippedPath)
153  histoSubNames.append(strippedPlot)
154  #print histoSubNames[-1]
155  elif plot.find('massiveMode') != -1:
156  massiveMode = True
157 
158  CleanArguments(argv,'--output')
159  CleanArguments(argv,'-o')
160 
161  outputDir = CreateBaseDirectory(options)
162 
163  if massiveMode:
164  print "Massive mode: scan all subdirs and make plots comparing each histogram one by one."
165  plotOneByOne(argv, outputDir, histoList, histoSubNames, paths)
166  else:
167  print "Default mode: Make default plot combinations."
168  plotDefaults(argv, options, outputDir)
169 
170 
171 #only execute main() if manually run
172 if __name__ == '__main__':
173  #main(*sys.argv[1:])
174  # the calls to sys.exit(n) inside main() all become return n.
175  sys.exit(main())
176 else:
177  print "This is ",__name__
178 
Definition: main.py:1