CMS 3D CMS Logo

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