14 from array
import array
15 ROOT.gROOT.SetBatch(
True )
22 if 'TH2' in ratio.IsA().GetName():
23 for j
in range(1,ratio.GetNbinsY()+1):
24 for i
in range(1,ratio.GetNbinsX()+1):
25 n = den.Integral(i,i,j,j)
26 p = ratio.GetBinContent(i,j)
28 ratio.SetBinError( i, j, 0 )
31 ratio.SetBinError( i, j, (p*(1-p)/n)**0.5 )
32 elif 'TH1' in ratio.IsA().GetName():
33 for i
in range(1,ratio.GetNbinsX()+1):
35 p = ratio.GetBinContent(i)
37 ratio.SetBinError( i, 0 )
40 ratio.SetBinError( i, (p*(1.-p)/n)**0.5 )
42 print(
'WARNING ' + ratio.GetName() +
'is not a TH1 or TH2' )
48 def CreateRatioPlot( infile, numHist, denHist, var, projXorY=doProjY, x1=None, x2=None, xtitle = '', plottype = '', doAverage = False ):
52 typeName =
'efficiency'
53 elif plottype ==
'RecoFrac':
54 typeName =
'Reco Fraction'
55 elif plottype ==
'purity':
58 print(
'plottype must be one of the following: \'eff\', \'RecoFrac\', \'purity\'' )
61 PlotNamePrefix = numHist.GetName()[:numHist.GetName().
find(
'_kinematics')] +
'_' + typeName.replace(
' ',
'')
64 if (x1 !=
None and x2 !=
None):
67 xvar=var.split(
'_')[1]
68 yvar=var.split(
'_')[0]
70 xvar=var.split(
'_')[0]
71 yvar=var.split(
'_')[1]
74 PlotName = PlotNamePrefix +
'_' + yvar +
'_'+xvar+
'Range_{0}_{1}'.
format(x1,x2).
replace(
'-',
'm').
replace(
'.',
'p')
75 PlotTitle = yvar.capitalize() +
' ' + typeName
79 ibin1 = numHist.GetYaxis().
FindBin(x1)
80 ibin2 = numHist.GetYaxis().
FindBin(x2)
81 num = numHist.ProjectionX(
'num_'+xvar+
"_"+yvar+
"Range",ibin1,ibin2)
82 den = denHist.ProjectionX(
'den_'+xvar+
"_"+yvar+
"Range",ibin1,ibin2)
84 ibin1 = numHist.GetXaxis().
FindBin(x1)
85 ibin2 = numHist.GetXaxis().
FindBin(x2)
86 num = numHist.ProjectionY(
'num_'+xvar+
"_"+yvar+
"Range",ibin1,ibin2)
87 den = denHist.ProjectionY(
'den_'+xvar+
"_"+yvar+
"Range",ibin1,ibin2)
91 ibin1 = numHist.GetYaxis().
FindBin(-1*x2)
92 ibin2 = numHist.GetYaxis().
FindBin(-1*x1)
93 num2 = numHist.ProjectionX(
'num2_'+xvar+
"_"+yvar+
"Range",ibin1,ibin2)
94 den2 = denHist.ProjectionX(
'den2_'+xvar+
"_"+yvar+
"Range",ibin1,ibin2)
96 ibin1 = numHist.GetXaxis().
FindBin(-1*x2)
97 ibin2 = numHist.GetXaxis().
FindBin(-1*x1)
98 num2 = numHist.ProjectionY(
'num2_'+xvar+
"_"+yvar+
"Range",ibin1,ibin2)
99 den2 = denHist.ProjectionY(
'den2_'+xvar+
"_"+yvar+
"Range",ibin1,ibin2)
102 PlotTitle = PlotTitle +
' ({0}<|eta|<{1})'.
format(x1,x2)
106 PlotTitle = PlotTitle +
' ({0}<pt<{1})'.
format(x1,x2)
123 PlotTitle = var.capitalize() +
' ' + typeName
124 PlotName = PlotNamePrefix +
'_' + var
125 num = numHist.Clone()
126 den = denHist.Clone()
129 if (
'_pt' in var
and projXorY==doProjY)
or var==
'pt':
131 ptBins = [ 0, 5, 10, 15, 20, 25, 30, 40, 50, 65, 80, 100, 200 ]
133 numRebinned = ROOT.TH1D(
'num_'+var,
'', len(ptBins)-1,
array(
'f', ptBins ) )
134 denRebinned = ROOT.TH1D(
'den_'+var,
'', len(ptBins)-1,
array(
'f', ptBins ) )
135 for i
in range( 1, numHist.GetNbinsX() + 1 ):
136 numRebinned.Fill( num.GetXaxis().GetBinCenter(i), num.GetBinContent(i) )
137 denRebinned.Fill( den.GetXaxis().GetBinCenter(i), den.GetBinContent(i) )
140 if 'TH1' in den.IsA().GetName():
141 numRebinned = num.Clone()
142 denRebinned = den.Clone()
152 ratio = numRebinned.Clone( PlotName )
153 ratio.Divide( numRebinned, denRebinned, 1, 1 )
154 ratio.SetTitle( PlotTitle )
158 if 'TH2' in ratio.IsA().GetName():
160 ratio.GetXaxis().SetTitle( var.split(
'_')[0] )
161 ratio.GetYaxis().SetTitle( var.split(
'_')[1] )
162 ratio.GetZaxis().SetTitle( typeName.lower() )
164 print 'WARNING Could not get axes name from histogram name'
165 elif 'TH1' in ratio.IsA().GetName():
166 ymax = ratio.GetBinContent( ratio.GetMaximumBin() ) + ratio.GetBinError( ratio.GetMaximumBin() )
167 ratio.GetYaxis().SetRangeUser(0, ymax*1.2 )
168 ratio.GetYaxis().SetTitle( typeName.lower() )
171 ratio.GetXaxis().SetTitle( xtitle )
173 if doAverage
and den.Integral() > 0
and num.Integral() > 0:
174 tot_eff = ROOT.TF1(
'aveline',
"[0]", ratio.GetXaxis().GetBinLowEdge(1), ratio.GetXaxis().GetBinUpEdge( ratio.GetNbinsX() ) )
175 ratio.GetListOfFunctions().Add( tot_eff )
176 f1 = ratio.GetFunction(
"aveline" )
177 f1.SetParameter( 0, num.Integral()/den.Integral() )
178 f1.SetLineColor( ROOT.kRed )
181 PlotDirName = PlotNamePrefix.replace(
'__',
'_').
split(
'_')
182 if not infile.GetDirectory(
'/'.
join( PlotDirName ) ):
183 PlotDir = infile.Get(
'/'.
join( PlotDirName[:-1] ) ).
mkdir( PlotDirName[-1] )
185 PlotDir = infile.Get(
'/'.
join( PlotDirName ) )
186 PlotDirName =
'/'.
join( PlotDirName )
188 if not infile.GetDirectory(PlotDirName).WriteTObject( ratio, PlotName,
"Overwrite" ):
189 print(
'WARNING failed to write histogram to file: ' + PlotDirName +
'/' + PlotName )
195 Main function to be executed when starting the code.
199 print(
'No filename given' )
200 print(
'Usage: python '+argv[0]+
' physval_filename [doAverage]' )
204 if not os.path.exists( filename ):
205 print (
'File not found: ' + filename )
208 if len(argv) > 2
and argv[2] ==
'doAverage':
213 infile = ROOT.TFile.Open( filename,
'update' )
215 muonTypesEff = [
'All',
'Prompt',
'InFlight',
'NonIsolated' ]
216 muonTypesReco = [
'Prompt',
'InFlight',
'NonIsolated',
'Rest' ]
217 Variables = [
'pt',
'eta',
'phi',
'eta_phi',
'eta_pt' ]
220 'pt' :
'p_{T} [GeV]',
224 'eta_pt' :
'p_{T} [GeV]' }
228 for muType
in muonTypesEff:
229 if not infile.Get(
'Muons/' + muType ):
230 print(
'INFO TDirectory not found: Muons/' + muType )
233 AuthDir = infile.Get(
'Muons/{0}/matched'.
format( muType ) )
235 Authors = [ i.GetName()
for i
in AuthDir.GetListOfKeys()
if AuthDir.Get( i.GetName() ).InheritsFrom(
'TDirectory' ) ]
236 for author
in Authors:
237 truthDirName =
'Muons/{0}/truth/all'.
format( muType )
238 matchDirName =
'Muons/{0}/matched/{1}/kinematics'.
format( muType, author )
239 truthDir = infile.GetDirectory( truthDirName )
240 matchDir = infile.GetDirectory( matchDirName )
242 print(
'WARNING Directory not found: '+truthDirName )
245 print(
'WARNING Directory not found: '+matchDirName )
247 for var
in Variables:
248 truthHistName = truthDirName.replace(
'/',
'_') +
'_' + var
249 truthHist = truthDir.Get( truthHistName )
250 matchHistName = matchDirName.replace(
'/',
'_') +
'_' + var
251 matchHist = matchDir.Get( matchHistName )
253 print(
'WARNING histogram not found: '+truthHistName )
256 print(
'WARNING histogram not found: '+matchHistName )
258 CreateRatioPlot( infile, matchHist, truthHist, var, xtitle = muType+
' Muon '+Xtitles[var], plottype =
'eff', doAverage = doAverage )
259 if var ==
'eta_phi' or var ==
'eta_pt':
260 CreateRatioPlot( infile, matchHist, truthHist, var, doProjY, 0, 2.5, muType+
' Muon '+Xtitles[var], plottype =
'eff', doAverage = doAverage )
261 CreateRatioPlot( infile, matchHist, truthHist, var, doProjY, 0, 0.1, muType+
' Muon '+Xtitles[var], plottype =
'eff', doAverage = doAverage )
262 CreateRatioPlot( infile, matchHist, truthHist, var, doProjY, 0.1, 1.05, muType+
' Muon '+Xtitles[var], plottype =
'eff', doAverage = doAverage )
263 CreateRatioPlot( infile, matchHist, truthHist, var, doProjY, 1.05, 2.0, muType+
' Muon '+Xtitles[var], plottype =
'eff', doAverage = doAverage )
264 CreateRatioPlot( infile, matchHist, truthHist, var, doProjY, 2.0, 2.5, muType+
' Muon '+Xtitles[var], plottype =
'eff', doAverage = doAverage )
265 CreateRatioPlot( infile, matchHist, truthHist, var, doProjY, 2.5, 2.7, muType+
' Muon '+Xtitles[var], plottype =
'eff', doAverage = doAverage )
266 if var ==
'eta_pt' and author ==
'CaloTag' :
267 CreateRatioPlot( infile, matchHist, truthHist, var, doProjX, 10, 1000, muType+
' Muon '+Xtitles[
'eta'], plottype =
'eff', doAverage = doAverage )
268 CreateRatioPlot( infile, matchHist, truthHist, var, doProjX, 15, 1000, muType+
' Muon '+Xtitles[
'eta'], plottype =
'eff', doAverage = doAverage )
269 CreateRatioPlot( infile, matchHist, truthHist, var, doProjX, 20, 1000, muType+
' Muon '+Xtitles[
'eta'], plottype =
'eff', doAverage = doAverage )
270 CreateRatioPlot( infile, matchHist, truthHist, var, doProjX, 25, 1000, muType+
' Muon '+Xtitles[
'eta'], plottype =
'eff', doAverage = doAverage )
272 for muType
in muonTypesReco:
273 if not infile.Get(
'Muons/' + muType ):
274 print(
'INFO TDirectory not found: Muons/' + muType )
277 AuthDir = infile.Get(
'Muons/{0}/matched'.
format( muType ) )
279 Authors = [ i.GetName()
for i
in AuthDir.GetListOfKeys()
if AuthDir.Get( i.GetName() ).InheritsFrom(
'TDirectory' ) ]
280 for author
in Authors:
281 typedir =
'Muons/{0}/reco/{1}/kinematics'.
format( muType, author )
282 alldir =
'Muons/All/reco/{0}/kinematics'.
format( author )
283 typeRecoDir = infile.Get( typedir )
284 allRecoDir = infile.Get( alldir )
286 print(
'INFO TDirectory not found: '+typedir )
289 print(
'INFO TDirectory not found: '+alldir )
291 for var
in Variables:
292 typeplot = typedir.replace(
'/',
'_') +
'_' + var
293 allplot = alldir.replace(
'/',
'_') +
'_' + var
294 typeRecoHist = typeRecoDir.Get( typeplot )
295 allRecoHist = allRecoDir.Get( allplot )
297 print(
'WARNING plot not found: ' + typeplot )
300 print(
'WARNING plot not found: ' + allplot )
302 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, xtitle = muType +
' Muon ' + Xtitles[var], plottype =
'RecoFrac', doAverage = doAverage )
307 if not infile.Get(
'Muons/' + muType ):
308 print(
'INFO TDirectory not found: Muons/' + muType )
311 AuthDir = infile.Get(
'Muons/{0}/matched'.
format( muType ) )
313 Authors = [ i.GetName()
for i
in AuthDir.GetListOfKeys()
if AuthDir.Get( i.GetName() ).InheritsFrom(
'TDirectory' ) ]
314 for author
in Authors:
315 typedir =
'Muons/{0}/matched/{1}/kinematicsReco'.
format( muType, author )
316 alldir =
'Muons/{0}/reco/{1}/kinematics'.
format( muType, author )
317 typeRecoDir = infile.Get( typedir )
318 allRecoDir = infile.Get( alldir )
320 print(
'INFO TDirectory not found: '+typedir )
323 print(
'INFO TDirectory not found: '+alldir )
325 for var
in Variables:
326 typeplot = typedir.replace(
'/',
'_') +
'_' + var
327 allplot = alldir.replace(
'/',
'_') +
'_' + var
328 typeRecoHist = typeRecoDir.Get( typeplot )
329 allRecoHist = allRecoDir.Get( allplot )
331 print(
'WARNING plot not found: ' + typeplot )
334 print(
'WARNING plot not found: ' + allplot )
336 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, xtitle = muType +
' Muon ' + Xtitles[var], plottype =
'purity', doAverage = doAverage )
338 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjX, 4, 500, muType+
' Muon '+Xtitles[
'pt'], plottype =
'purity', doAverage = doAverage )
339 if var ==
'eta_pt' and author ==
'CaloTag' :
340 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjX, 10, 500, muType+
' Muon '+Xtitles[
'pt'], plottype =
'purity', doAverage = doAverage )
341 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjX, 15, 500, muType+
' Muon '+Xtitles[
'pt'], plottype =
'purity', doAverage = doAverage )
342 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjX, 20, 500, muType+
' Muon '+Xtitles[
'pt'], plottype =
'purity', doAverage = doAverage )
343 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjX, 25, 500, muType+
' Muon '+Xtitles[
'pt'], plottype =
'purity', doAverage = doAverage )
344 if (var ==
'eta_phi' or var ==
'eta_pt')
and author
is not 'CaloTag' :
345 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjY, 0, 0.1, muType+
' Muon '+Xtitles[var], plottype =
'purity', doAverage = doAverage )
346 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjY, 0.1, 1.05, muType+
' Muon '+Xtitles[var], plottype =
'purity', doAverage = doAverage )
347 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjY, 1.05, 2.0, muType+
' Muon '+Xtitles[var], plottype =
'purity', doAverage = doAverage )
348 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjY, 2.0, 2.5, muType+
' Muon '+Xtitles[var], plottype =
'purity', doAverage = doAverage )
349 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjY, 0, 2.5, muType+
' Muon '+Xtitles[var], plottype =
'purity', doAverage = doAverage )
350 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, doProjY, 2.5, 2.7, muType+
' Muon '+Xtitles[var], plottype =
'purity', doAverage = doAverage )
353 muType =
'UnmatchedRecoMuons'
354 typedir =
'Muons/{0}/kinematics'.
format( muType )
355 allnames = [ i
for i
in Authors
if i ==
'AllMuons' or i ==
'AllAuthors' ]
356 if len(allnames) == 0:
358 alldir =
'Muons/All/reco/{0}/kinematics'.
format( allnames[0] )
359 if not infile.GetDirectory( typedir ):
360 print(
'INFO directory not found: ' + typedir )
361 elif not infile.GetDirectory( alldir ):
362 print(
'INFO directory not found: ' + alldir )
364 for var
in Variables:
365 typeplot =
'Muons_{0}__kinematics_{1}'.
format( muType, var )
366 allplot = alldir.replace(
'/',
'_') +
'_{0}'.
format( var )
368 typeRecoHist = infile.GetDirectory( typedir ).
Get( typeplot )
369 allRecoHist = infile.GetDirectory( alldir ).
Get( allplot )
370 if typeRecoHist
and allRecoHist:
371 CreateRatioPlot( infile, typeRecoHist, allRecoHist, var, xtitle =
'Unmatched Reco Muon '+Xtitles[var], plottype =
'RecoFrac', doAverage = doAverage )
376 if __name__ ==
"__main__":
378 Here the code should appear that is executed when running the plotter directly
379 (and not import it in another python file via 'import Plotter')