-
Notifications
You must be signed in to change notification settings - Fork 2
/
plotSTDistributionComparisons.py
executable file
·492 lines (447 loc) · 28.9 KB
/
plotSTDistributionComparisons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python
from __future__ import print_function, division
import os, sys, argparse, array, pdb, math
import ROOT
import tdrstyle, CMS_lumi
import tmGeneralUtils, tmROOTUtils, tmProgressBar
ROOT.gROOT.SetBatch(ROOT.kTRUE)
# Register command line options
inputArgumentsParser = argparse.ArgumentParser(description='Generate plot of comparison of ST distributions at various jet multiplicities.')
inputArgumentsParser.add_argument('--inputFilePath', required=True, help='Path to input file.',type=str)
inputArgumentsParser.add_argument('--outputDirectory_plots', default="publicationPlots", help='Output directory in which to store plots.',type=str)
inputArgumentsParser.add_argument('--outputDirectory_dataSystematics', default="dataSystematics", help='Output directory in which to store deviations from ST scaling.',type=str)
inputArgumentsParser.add_argument('--outputFilePrefix', required=True, help='Prefix for output files.',type=str)
inputArgumentsParser.add_argument('--inputFile_STRegionBoundaries', default="STRegionBoundaries.dat", help='Path to file with ST region boundaries. First bin is the normalization bin, and the last bin is the last boundary to infinity.', type=str)
inputArgumentsParser.add_argument('--EMSTMin', default=-1., help='Min threshold for EM ST.', type=float)
inputArgumentsParser.add_argument('--EMSTMax', default=-1., help='Max threshold for EM ST.', type=float)
inputArgumentsParser.add_argument('--nJetsMin', default=2, help='Min nJets bin.',type=int)
inputArgumentsParser.add_argument('--nJetsMax', default=6, help='Max nJets bin.',type=int)
inputArgumentsParser.add_argument('--nJetsMaxPlot', default=6, help='Max nJets bin to plot.',type=int)
inputArgumentsParser.add_argument('--nJetsNorm', default=2, help='Norm nJets bin.',type=int)
inputArgumentsParser.add_argument('--plotHTScaling', action='store_true', help="If this flag is set, only print how HT scales... useful for single-medium samples.")
inputArgumentsParser.add_argument('--doNotSaveSystematics', action='store_true', help="If this flag is set, do not print out scaling deviations to a file.")
inputArgumentsParser.add_argument('--weighted', action='store_true', help="If this flag is set, event weights are found from the branch \"b_MCXSecWeight\".")
inputArguments = inputArgumentsParser.parse_args()
histColors = {
2: ROOT.kBlack,
3: ROOT.kBlue+2,
4: ROOT.kRed+1,
5: ROOT.kGreen+3,
6: ROOT.kViolet,
7: ROOT.kYellow+2,
8: ROOT.kCyan+2
}
STComponentNames = ["photon", "jet", "MET"]
STMakeupColors = {
"photon": ROOT.kRed,
"jet": ROOT.kBlue,
"MET": ROOT.kGreen
}
STHistogramScales = {
"photon": 0.35,
"jet": 1.0,
"MET": 0.05
}
STHistogramTypes = ["total"] + STComponentNames
dataSpecialDescription = ""
if (inputArguments.outputFilePrefix == "control_STComparisons"): dataSpecialDescription = "fake #gamma + fake #gamma"
elif (inputArguments.outputFilePrefix == "control_singlemedium_STComparisons"): dataSpecialDescription = "single medium #gamma"
STBoundaries = {}
STRegionsAxes = {}
targetSTNorms = {}
n_STBins = {}
for STHistogramType in STHistogramTypes:
STBoundaries[STHistogramType] = []
STRegionBoundariesFileObject = open(inputArguments.inputFile_STRegionBoundaries)
for STBoundaryString in STRegionBoundariesFileObject:
if (STBoundaryString.strip()):
STBoundary = float(STBoundaryString.strip())
if (STHistogramType == "total"):
STBoundaries[STHistogramType].append(STBoundary)
else:
STBoundaries[STHistogramType].append(STHistogramScales[STHistogramType]*STBoundary) # For the various components of ST, scale the bin sizes by their approximate makeup in the 2-jets norm bin
if (STHistogramType == "total"):
STBoundaries[STHistogramType].append(3500.0) # Instead of infinity
else:
STBoundaries[STHistogramType].append(STHistogramScales[STHistogramType]*3500.0) # Instead of infinity
n_STBins[STHistogramType] = len(STBoundaries[STHistogramType]) - 1
STRegionsAxes[STHistogramType] = ROOT.TAxis(n_STBins[STHistogramType], array.array('d', STBoundaries[STHistogramType]))
targetSTNorms[STHistogramType] = 0.5*(STBoundaries[STHistogramType][0] + STBoundaries[STHistogramType][1])
print("For histogram type: {t}, target norm: {tN}".format(t=STHistogramType, tN=targetSTNorms[STHistogramType]))
# Load input TTrees into TChain
inputChain = ROOT.TChain("ggNtuplizer/EventTree")
inputChain.Add(inputArguments.inputFilePath)
nEvents = inputChain.GetEntries()
if (nEvents == 0): sys.exit("Number of available events is 0.")
outputFile = ROOT.TFile.Open("{oD}/{oFP}_savedSTShapes.root".format(oD=inputArguments.outputDirectory_plots, oFP=inputArguments.outputFilePrefix), "RECREATE")
STHistograms = {}
STHistogramsScaled = {}
ratioHistograms = {}
STMakeupProfiles = {}
for STHistogramType in STHistogramTypes:
STHistograms[STHistogramType] = {}
STHistogramsScaled[STHistogramType] = {}
ratioHistograms[STHistogramType] = {}
STMakeupProfiles[STHistogramType] = {}
for nJetsBin in range(inputArguments.nJetsMin, 1 + inputArguments.nJetsMax):
STMakeupProfiles[STHistogramType][nJetsBin] = ROOT.TProfile("h_STMakeup_{t}_{n}Jets".format(t=STHistogramType, n=nJetsBin), "ST Makeup, {t}: {n} Jets".format(t=STHistogramType, n=nJetsBin), n_STBins["total"], array.array('d', STBoundaries["total"]))
STHistograms[STHistogramType][nJetsBin] = ROOT.TH1F("h_STDistribution_{t}_{n}Jets".format(t=STHistogramType, n=nJetsBin), "", n_STBins[STHistogramType], array.array('d', STBoundaries[STHistogramType]))
STHistograms[STHistogramType][nJetsBin].Sumw2()
STHistogramsScaled[STHistogramType][nJetsBin] = ROOT.TH1F("h_STDistribution_{t}_scaled_{n}Jets".format(t=STHistogramType, n=nJetsBin), "", n_STBins[STHistogramType], array.array('d', STBoundaries[STHistogramType]))
STHistogramsScaled[STHistogramType][nJetsBin].Sumw2()
if (nJetsBin == inputArguments.nJetsNorm): continue
ratioHistograms[STHistogramType][nJetsBin] = ROOT.TH1F("h_ratioDistribution_{t}_{n}Jets".format(t=STHistogramType, n=nJetsBin), "", n_STBins[STHistogramType], array.array('d', STBoundaries[STHistogramType]))
ratioHistograms[STHistogramType][nJetsBin].Sumw2()
progressBar = tmProgressBar.tmProgressBar(nEvents)
progressBarUpdatePeriod = max(1, (nEvents//50))
progressBar.initializeTimer()
for eventIndex in range(0,nEvents):
treeStatus = inputChain.LoadTree(eventIndex)
if treeStatus < 0:
# sys.exit("Tree unreadable.")
break
evtStatus = inputChain.GetEntry(eventIndex)
if evtStatus <= 0:
# sys.exit("Event in tree unreadable.")
continue
if (eventIndex%progressBarUpdatePeriod == 0 or eventIndex == (nEvents - 1)): progressBar.updateBar(eventIndex/nEvents, eventIndex)
nJetsBin = inputChain.b_nJetsDR
if (nJetsBin > inputArguments.nJetsMax): nJetsBin = inputArguments.nJetsMax
if (nJetsBin < inputArguments.nJetsMin):
print("WARNING: Unexpected nJetsBin = {nJetsBin} at entry index = {eI}".format(nJetsBin=nJetsBin, eI=eventIndex))
continue
eventWeight = 1.0
if inputArguments.weighted:
eventWeight = (inputChain.b_MCXSecWeight*inputChain.genWeight*inputChain.b_evtPrefiringWeight*inputChain.b_evtphotonMCScaleFactor*inputChain.b_PUWeightNoSelection)
sT = inputChain.b_evtST
sT_EM = inputChain.b_evtST_electromagnetic
sT_hadronic = inputChain.b_evtST_hadronic
sT_MET = inputChain.b_evtST_MET
to_fill = True
if (inputArguments.EMSTMin > 0.):
to_fill_tmp = (to_fill and (sT_EM >= inputArguments.EMSTMin))
to_fill = to_fill_tmp
if (inputArguments.EMSTMax > 0.):
to_fill_tmp = (to_fill and (sT_EM <= inputArguments.EMSTMax))
to_fill = to_fill_tmp
if to_fill:
STHistograms["photon"][nJetsBin].Fill(sT_EM, eventWeight)
STMakeupProfiles["photon"][nJetsBin].Fill(sT, sT_EM/sT, eventWeight)
STHistograms["jet"][nJetsBin].Fill(sT_hadronic, eventWeight)
STMakeupProfiles["jet"][nJetsBin].Fill(sT, sT_hadronic/sT, eventWeight)
STHistograms["MET"][nJetsBin].Fill(sT_MET, eventWeight)
STMakeupProfiles["MET"][nJetsBin].Fill(sT, sT_MET/sT, eventWeight)
STHistograms["total"][nJetsBin].Fill(sT, eventWeight)
progressBar.terminate()
scalingSystematicsList = []
maxValueToPlot = None
minValueToPlot = None
for STHistogramType in ["total"]:
if inputArguments.plotHTScaling: continue
for nJetsBin in range(inputArguments.nJetsMin, 1 + inputArguments.nJetsMaxPlot):
tmROOTUtils.rescale1DHistogramByBinWidth(STHistograms[STHistogramType][nJetsBin])
normBinIndex = STHistograms[STHistogramType][nJetsBin].GetXaxis().FindFixBin(targetSTNorms[STHistogramType])
normalizationFactor = 1
try:
normalizationFactor = STHistograms[STHistogramType][inputArguments.nJetsNorm].GetBinContent(normBinIndex)/STHistograms[STHistogramType][nJetsBin].GetBinContent(normBinIndex)
except ZeroDivisionError:
continue
for binIndex in range(1, 1+STHistograms[STHistogramType][nJetsBin].GetXaxis().GetNbins()):
scaledContent = normalizationFactor*STHistograms[STHistogramType][nJetsBin].GetBinContent(binIndex)
if ((maxValueToPlot is None) or (scaledContent > maxValueToPlot)): maxValueToPlot = scaledContent
if ((minValueToPlot is None) or (scaledContent < minValueToPlot)): minValueToPlot = scaledContent
STHistogramsScaled[STHistogramType][nJetsBin].SetBinContent(binIndex, scaledContent)
scaledError = normalizationFactor*STHistograms[STHistogramType][nJetsBin].GetBinError(binIndex)
STHistogramsScaled[STHistogramType][nJetsBin].SetBinError(binIndex, scaledError)
if (nJetsBin == inputArguments.nJetsNorm): continue
for binIndex in range(1, 1+STHistograms[STHistogramType][nJetsBin].GetXaxis().GetNbins()):
numerator = STHistograms[STHistogramType][nJetsBin].GetBinContent(binIndex)
numeratorError = STHistograms[STHistogramType][nJetsBin].GetBinError(binIndex)
denominator = STHistograms[STHistogramType][inputArguments.nJetsNorm].GetBinContent(binIndex)
denominatorError = STHistograms[STHistogramType][inputArguments.nJetsNorm].GetBinError(binIndex)
ratioHistograms[STHistogramType][nJetsBin].SetBinContent(binIndex, 1.)
ratioHistograms[STHistogramType][nJetsBin].SetBinError(binIndex, 1.)
scalingUncertaintyDown = -0.9
scalingUncertaintyUp = 9.
if (denominator > 0):
ratio = numerator/denominator
ratioError = (1.0/denominator)*math.sqrt(math.pow(numeratorError,2) + math.pow(denominatorError*ratio,2))
ratioHistograms[STHistogramType][nJetsBin].SetBinContent(binIndex, normalizationFactor*ratio)
ratioHistograms[STHistogramType][nJetsBin].SetBinError(binIndex, normalizationFactor*ratioError)
scalingDeviation = normalizationFactor*ratio
# scalingDeviation = (NEvents(normJetsBin, norm ST bin)/NEvents(nJetsBin, norm ST bin))*(NEvents(nJetsBin, ST bin)/(NEvents(normJetsBin, ST bin)))
if (binIndex > 1):
if (scalingDeviation < 0.9):
scalingUncertaintyDown = scalingDeviation - 1.0 # lnN (1+delta) = scalingDeviation
scalingUncertaintyUp = 0.1 # lnN (1+delta) = 1.1
elif (scalingDeviation < 1.1):
scalingUncertaintyDown = -0.1 # lnN (1+delta) = 0.9
scalingUncertaintyUp = 0.1 # lnN (1+delta) = 0.9
else: # scalingDeviation > 1.1
scalingUncertaintyDown = -0.1 # lnN (1+delta) = 0.9
scalingUncertaintyUp = scalingDeviation - 1.0 # lnN (1+delta) = scalingDeviation
if (binIndex > 1):
# print("At binIndex = {bI}, nJetsBin = {nJB}, scalingDeviation = {sD}, scalingUncertaintyDown = {sUD}, scalingUncertaintyUp = {sUU}".format(bI=binIndex, nJB=nJetsBin, sD=scalingDeviation, sUD=scalingUncertaintyDown, sUU=scalingUncertaintyUp))
scalingSystematicsList.append(tuple(["float", "fractionalUncertaintyDown_scaling_STRegion{r}_{n}Jets".format(r=binIndex, n=nJetsBin), scalingUncertaintyDown]))
scalingSystematicsList.append(tuple(["float", "fractionalUncertaintyUp_scaling_STRegion{r}_{n}Jets".format(r=binIndex, n=nJetsBin), scalingUncertaintyUp]))
if not(inputArguments.doNotSaveSystematics):
tmGeneralUtils.writeConfigurationParametersToFile(configurationParametersList=scalingSystematicsList, outputFilePath=("{oD}/{oP}_scalingSystematics.dat".format(oD=inputArguments.outputDirectory_dataSystematics, oP=inputArguments.outputFilePrefix)))
if inputArguments.plotHTScaling:
STHistogramType = "jet"
for nJetsBin in range(inputArguments.nJetsMin, 1 + inputArguments.nJetsMaxPlot):
tmROOTUtils.rescale1DHistogramByBinWidth(STHistograms[STHistogramType][nJetsBin])
normBinIndex = STHistograms[STHistogramType][nJetsBin].GetXaxis().FindFixBin(targetSTNorms[STHistogramType])
normalizationFactor = 1
try:
normalizationFactor = STHistograms[STHistogramType][inputArguments.nJetsNorm].GetBinContent(normBinIndex)/STHistograms[STHistogramType][nJetsBin].GetBinContent(normBinIndex)
except ZeroDivisionError:
print("WARNING: zero events found in normalization bin at nJetsBin={nJB} for histogramType: {hT}".format(nJB=nJetsBin, hT=STHistogramType))
continue
for binIndex in range(1, 1+STHistograms[STHistogramType][nJetsBin].GetXaxis().GetNbins()):
scaledContent = normalizationFactor*STHistograms[STHistogramType][nJetsBin].GetBinContent(binIndex)
if ((maxValueToPlot is None) or (scaledContent > maxValueToPlot)): maxValueToPlot = scaledContent
if ((minValueToPlot is None) or (scaledContent < minValueToPlot)): minValueToPlot = scaledContent
STHistogramsScaled[STHistogramType][nJetsBin].SetBinContent(binIndex, scaledContent)
scaledError = normalizationFactor*STHistograms[STHistogramType][nJetsBin].GetBinError(binIndex)
STHistogramsScaled[STHistogramType][nJetsBin].SetBinError(binIndex, scaledError)
if (nJetsBin == inputArguments.nJetsNorm): continue
for binIndex in range(1, 1+STHistograms[STHistogramType][nJetsBin].GetXaxis().GetNbins()):
numerator = STHistograms[STHistogramType][nJetsBin].GetBinContent(binIndex)
numeratorError = STHistograms[STHistogramType][nJetsBin].GetBinError(binIndex)
denominator = STHistograms[STHistogramType][inputArguments.nJetsNorm].GetBinContent(binIndex)
denominatorError = STHistograms[STHistogramType][inputArguments.nJetsNorm].GetBinError(binIndex)
ratioHistograms[STHistogramType][nJetsBin].SetBinContent(binIndex, 1.)
ratioHistograms[STHistogramType][nJetsBin].SetBinError(binIndex, 1.)
if (denominator > 0):
ratio = numerator/denominator
ratioError = (1.0/denominator)*math.sqrt(math.pow(numeratorError,2) + math.pow(denominatorError*ratio,2))
ratioHistograms[STHistogramType][nJetsBin].SetBinContent(binIndex, normalizationFactor*ratio)
ratioHistograms[STHistogramType][nJetsBin].SetBinError(binIndex, normalizationFactor*ratioError)
tdrstyle.setTDRStyle()
commonTitleOffset = 0.7
commonLineWidth = 3
commonTitleSize = 0.06
commonLabelSize = 0.05
# for STHistogramType in STHistogramTypes:
STHistogramTypesToPlot = ["total"]
if inputArguments.plotHTScaling:
STHistogramTypesToPlot = ["jet"]
for STHistogramType in STHistogramTypesToPlot:
H_ref = 600
W_ref = 800
W = W_ref
H = H_ref
T = 0.08*H_ref
B = 0.12*H_ref
L = 0.12*W_ref
R = 0.04*W_ref
canvas = ROOT.TCanvas("c_{oFP}_{t}_{n}Jets".format(oFP=inputArguments.outputFilePrefix, t=STHistogramType, n=nJetsBin), "c_{oFP}_{t}_{n}Jets".format(oFP=inputArguments.outputFilePrefix, t=STHistogramType, n=nJetsBin), 50, 50, W, H)
canvas.SetFillColor(0)
canvas.SetBorderMode(0)
canvas.SetFrameFillStyle(0)
canvas.SetFrameBorderMode(0)
canvas.SetLeftMargin( L/W )
canvas.SetRightMargin( R/W )
canvas.SetTopMargin( T/H )
canvas.SetBottomMargin( B/H )
canvas.SetTickx(0)
canvas.SetTicky(0)
canvas.Draw()
bottomFraction = 0.25
bottomToTopRatio = bottomFraction/(1.0 - bottomFraction)
upperPad = ROOT.TPad("upperPad_{t}_{n}Jets".format(t=STHistogramType, n=nJetsBin), "upperPad_{t}_{n}Jets".format(t=STHistogramType, n=nJetsBin), 0., bottomFraction, 0.97, 0.97)
upperPad.SetMargin(0.12, 0.03, 0.025, 0.08) # left, right, bottom, top
lowerPad = ROOT.TPad("lowerPad_{t}_{n}Jets".format(t=STHistogramType, n=nJetsBin), "lowerPad_{t}_{n}Jets".format(t=STHistogramType, n=nJetsBin), 0., 0., 0.97, bottomFraction)
lowerPad.SetMargin(0.12, 0.03, 0.38, 0.03) # left, right, bottom, top
upperPad.Draw()
lowerPad.Draw()
upperPad.cd()
upperPad.SetLogy()
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].SetLineColor(histColors[inputArguments.nJetsNorm])
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].SetLineWidth(commonLineWidth)
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetXaxis().SetTitleSize(commonTitleSize)
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetXaxis().SetLabelSize(commonLabelSize)
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetXaxis().SetTickLength(0)
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetXaxis().SetLabelOffset(999)
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().SetTitle("Events/GeV")
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().SetTitleSize(commonTitleSize)
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().SetLabelSize(commonLabelSize)
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().SetTitleOffset(commonTitleOffset)
for nJetsBin in range(inputArguments.nJetsMin, 1 + inputArguments.nJetsMaxPlot):
if (nJetsBin == inputArguments.nJetsNorm): continue
STHistogramsScaled[STHistogramType][nJetsBin].SetLineColor(histColors[nJetsBin])
STHistogramsScaled[STHistogramType][nJetsBin].SetLineWidth(commonLineWidth)
CMS_lumi.writeExtraText = False
CMS_lumi.lumi_sqrtS = "13 TeV" # used with iPeriod = 0, e.g. for simulation-only plots (default is an empty string)
CMS_lumi.lumi_13TeV = "138 fb^{-1}"
CMS_lumi.relPosX = 0.15
legend = ROOT.TLegend(0.8, 0.475, 0.95, 0.9)
legend.SetBorderSize(commonLineWidth)
legend.SetFillStyle(0)
ROOT.gStyle.SetLegendTextSize(0.05)
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].Draw("P0")
norm_legendEntry = legend.AddEntry(STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm], "N_{{Jets}} = {n}".format(n=inputArguments.nJetsNorm), "LPE")
norm_legendEntry.SetTextColor(histColors[inputArguments.nJetsNorm])
norm_legendEntry.SetMarkerColor(histColors[inputArguments.nJetsNorm])
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetXaxis().SetRangeUser(STBoundaries[STHistogramType][0], STBoundaries[STHistogramType][-1])
# if (inputArguments.outputFilePrefix == "control_singlemedium_STComparisons"): STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().SetRangeUser(0.05, 200.)
# else: STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().SetRangeUser(0.001, 10.)
STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().SetRangeUser(max(maxValueToPlot/2000.0, minValueToPlot/2.0), maxValueToPlot*2.0)
upperPad.Update()
if not(dataSpecialDescription == ""):
latex = ROOT.TLatex()
latex.SetTextFont(42)
latex.SetTextAngle(0)
latex.SetTextColor(ROOT.kBlack)
latex.SetTextSize(0.07)
latex.SetTextAlign(22)
latex.DrawLatexNDC(0.5, 0.8, dataSpecialDescription)
for nJetsBin in range(inputArguments.nJetsMin, 1 + inputArguments.nJetsMaxPlot):
if (nJetsBin == inputArguments.nJetsNorm): continue
STHistogramsScaled[STHistogramType][nJetsBin].Draw("AP0 SAME")
legendText = "N_{{Jets}} = {n}".format(n=nJetsBin)
if (nJetsBin == inputArguments.nJetsMax): legendText = "N_{{Jets}} #geq {n}".format(n=nJetsBin)
legendEntry = legend.AddEntry(STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm], legendText, "LPE")
legendEntry.SetTextColor(histColors[nJetsBin])
legendEntry.SetMarkerColor(histColors[nJetsBin])
legend.Draw()
CMS_lumi.CMS_lumi(canvas, 4, 0)
upperPad.cd()
upperPad.Update()
upperPad.RedrawAxis()
frame = upperPad.GetFrame()
frame.Draw()
yTitleSize_upper = STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().GetTitleSize()
yLabelSize_upper = STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().GetLabelSize()
yTickLength_upper = STHistogramsScaled[STHistogramType][inputArguments.nJetsNorm].GetYaxis().GetTickLength()
upperPad.Update()
lowerPad.cd()
isSet = False
for nJetsBin in range(inputArguments.nJetsMin, 1 + inputArguments.nJetsMaxPlot):
if (nJetsBin == inputArguments.nJetsNorm): continue
ratioHistograms[STHistogramType][nJetsBin].SetLineColor(histColors[nJetsBin])
ratioHistograms[STHistogramType][nJetsBin].SetLineWidth(commonLineWidth)
if (isSet):
ratioHistograms[STHistogramType][nJetsBin].Draw("AP0 SAME")
continue
xLabel = "S_{T} (GeV)"
if (inputArguments.plotHTScaling): xLabel = "H_{T} (GeV)"
ratioHistograms[STHistogramType][nJetsBin].GetXaxis().SetTitle(xLabel)
ratioHistograms[STHistogramType][nJetsBin].GetXaxis().SetTitleSize(yTitleSize_upper/bottomToTopRatio)
ratioHistograms[STHistogramType][nJetsBin].GetXaxis().SetLabelSize(yLabelSize_upper/bottomToTopRatio)
ratioHistograms[STHistogramType][nJetsBin].GetXaxis().SetTickLength(yTickLength_upper)
ratioHistograms[STHistogramType][nJetsBin].GetXaxis().SetTitleOffset(0.86)
ratioHistograms[STHistogramType][nJetsBin].GetYaxis().SetTitle("#frac{N_{Jets} bin}{N_{Jets} = " + str(inputArguments.nJetsNorm) + "}")
ratioHistograms[STHistogramType][nJetsBin].GetYaxis().SetTitleOffset(1.4*bottomToTopRatio*commonTitleOffset)
ratioHistograms[STHistogramType][nJetsBin].GetYaxis().SetTitleSize(0.75*yTitleSize_upper/bottomToTopRatio)
ratioHistograms[STHistogramType][nJetsBin].GetYaxis().SetLabelSize(yLabelSize_upper/bottomToTopRatio)
ratioHistograms[STHistogramType][nJetsBin].GetYaxis().SetTickLength(yTickLength_upper)
ratioHistograms[STHistogramType][nJetsBin].GetYaxis().SetNdivisions(2, 0, 0)
ratioHistograms[STHistogramType][nJetsBin].Draw("P0")
ratioHistograms[STHistogramType][nJetsBin].GetXaxis().SetRangeUser(STBoundaries[STHistogramType][0], STBoundaries[STHistogramType][-1])
ratioHistograms[STHistogramType][nJetsBin].GetYaxis().SetRangeUser(0., 5.)
isSet = True
lineAt1 = ROOT.TLine(STBoundaries[STHistogramType][0], 1., STBoundaries[STHistogramType][-1], 1.)
lineAt1.SetLineColor(histColors[inputArguments.nJetsNorm])
lineAt1.SetLineWidth(commonLineWidth)
lineAt1.Draw()
lowerPad.cd()
lowerPad.Update()
lowerPad.RedrawAxis()
frame = lowerPad.GetFrame()
frame.Draw()
canvas.Update()
canvas.SaveAs("{oD}/{oFP}_{t}.pdf".format(oD=inputArguments.outputDirectory_plots, oFP=inputArguments.outputFilePrefix, t=STHistogramType))
for nJetsBin in range(inputArguments.nJetsMin, 1 + inputArguments.nJetsMaxPlot):
H_ref = 600
W_ref = 800
W = W_ref
H = H_ref
T = 0.08*H_ref
B = 0.12*H_ref
L = 0.12*W_ref
R = 0.04*W_ref
canvas = ROOT.TCanvas("c_{oFP}_STMakeup_{n}Jets".format(oFP=inputArguments.outputFilePrefix, n=nJetsBin), "c_{oFP}_STMakeup_{n}Jets".format(oFP=inputArguments.outputFilePrefix, n=nJetsBin), 50, 50, W, H)
canvas.SetFillColor(0)
canvas.SetBorderMode(0)
canvas.SetFrameFillStyle(0)
canvas.SetFrameBorderMode(0)
canvas.SetLeftMargin( L/W )
canvas.SetRightMargin( R/W )
canvas.SetTopMargin( T/H )
canvas.SetBottomMargin( B/H )
canvas.SetTickx(0)
canvas.SetTicky(0)
canvas.Draw()
# outputStack = ROOT.THStack("h_makeupStack_{n}Jets".format(n=nJetsBin), "ST makeup, {n} Jets".format(n=nJetsBin))
legend = ROOT.TLegend(0.2, 0.8, 0.8, 0.9)
legend.SetBorderSize(commonLineWidth)
legend.SetFillStyle(0)
ROOT.gStyle.SetLegendTextSize(0.05)
legend.SetNColumns(3)
makeupProfileProjections = {}
cumulativeHistograms = {}
cumulativeGraphs = {}
runningCumulativeBinContents = {}
for STBinIndex in range(1, 1+n_STBins[STHistogramType]):
runningCumulativeBinContents[STBinIndex] = 0.
for STComponentName in STComponentNames:
makeupProfileProjections[STComponentName] = STMakeupProfiles[STComponentName][nJetsBin].ProjectionX("projection_makeup_{t}_{n}Jets".format(t=STComponentName, n=nJetsBin))
cumulativeHistograms[STComponentName] = ROOT.TH1D("h_cumulative_{t}_{n}Jets".format(t=STComponentName, n=nJetsBin), "", n_STBins["total"], array.array('d', STBoundaries["total"]))
cumulativeGraphs[STComponentName] = ROOT.TGraphAsymmErrors(n_STBins["total"])
for STBinIndex in range(1, 1+n_STBins[STHistogramType]):
binContent = makeupProfileProjections[STComponentName].GetBinContent(STBinIndex)
binError = makeupProfileProjections[STComponentName].GetBinError(STBinIndex)
runningCumulativeBinContents[STBinIndex] += binContent
cumulativeHistograms[STComponentName].SetBinContent(STBinIndex, runningCumulativeBinContents[STBinIndex])
cumulativeGraphs[STComponentName].SetPoint(STBinIndex-1, STRegionsAxes["total"].GetBinCenter(STBinIndex), runningCumulativeBinContents[STBinIndex])
cumulativeGraphs[STComponentName].SetPointEXlow(STBinIndex-1, 0.5*STRegionsAxes["total"].GetBinWidth(STBinIndex))
cumulativeGraphs[STComponentName].SetPointEXhigh(STBinIndex-1, 0.5*STRegionsAxes["total"].GetBinWidth(STBinIndex))
cumulativeGraphs[STComponentName].SetPointEYlow(STBinIndex-1, 0.5*binError)
cumulativeGraphs[STComponentName].SetPointEYhigh(STBinIndex-1, 0.5*binError)
cumulativeHistograms[STComponentName].SetFillColorAlpha(STMakeupColors[STComponentName], 0.4)
cumulativeGraphs[STComponentName].SetFillColor(STMakeupColors[STComponentName])
cumulativeGraphs[STComponentName].SetFillStyle(3011)
legendEntry = legend.AddEntry(cumulativeHistograms[STComponentName], "{name}".format(name=STComponentName))
legendEntry.SetTextColor(STMakeupColors[STComponentName])
legendEntry.SetLineColor(STMakeupColors[STComponentName])
# First draw the axis
STMakeupProfiles["total"][nJetsBin].SetTitle("")
xLabel = "S_{T} (GeV)"
if (inputArguments.plotHTScaling): xLabel = "H_{T} (GeV)"
STMakeupProfiles["total"][nJetsBin].GetXaxis().SetTitle(xLabel)
STMakeupProfiles["total"][nJetsBin].GetXaxis().SetTitleOffset(0.86)
STMakeupProfiles["total"][nJetsBin].GetXaxis().SetLabelSize(commonLabelSize)
STMakeupProfiles["total"][nJetsBin].GetXaxis().SetTitleSize(commonTitleSize)
STMakeupProfiles["total"][nJetsBin].GetYaxis().SetTitleSize(commonTitleSize)
STMakeupProfiles["total"][nJetsBin].GetYaxis().SetTitle("Makeup")
STMakeupProfiles["total"][nJetsBin].GetYaxis().SetLabelSize(commonLabelSize)
STMakeupProfiles["total"][nJetsBin].GetYaxis().SetTitleOffset(1.2*commonTitleOffset)
STMakeupProfiles["total"][nJetsBin].SetMinimum(0.)
STMakeupProfiles["total"][nJetsBin].SetMaximum(1.4)
STMakeupProfiles["total"][nJetsBin].Draw("AXIS")
# Next draw the "filled" histograms in reversed order
for STComponentName in reversed(STComponentNames):
cumulativeHistograms[STComponentName].Draw("SAME")
# Next draw the error graphs
for STComponentName in STComponentNames:
cumulativeGraphs[STComponentName].Draw("2")
# Finally draw the axis again
STMakeupProfiles["total"][nJetsBin].Draw("AXIS SAME")
CMS_lumi.writeExtraText = False
CMS_lumi.lumi_sqrtS = "13 TeV" # used with iPeriod = 0, e.g. for simulation-only plots (default is an empty string)
CMS_lumi.lumi_13TeV = "138 fb^{-1}"
CMS_lumi.relPosX = 0.15
CMS_lumi.CMS_lumi(canvas, 4, 0)
canvas.Update()
legend.Draw()
canvas.Update()
canvas.SaveAs("{oD}/{oFP}_STMakeup_{n}Jets.pdf".format(oD=inputArguments.outputDirectory_plots, oFP=inputArguments.outputFilePrefix, n=nJetsBin))
for STHistogramType in STHistogramTypesToPlot:
for nJetsBin in range(inputArguments.nJetsMin, 1 + inputArguments.nJetsMaxPlot):
outputFile.WriteTObject(STMakeupProfiles[STHistogramType][nJetsBin])
outputFile.WriteTObject(STHistograms[STHistogramType][nJetsBin])
if (nJetsBin == inputArguments.nJetsNorm): continue
outputFile.WriteTObject(ratioHistograms[STHistogramType][nJetsBin])
outputFile.Close()
print("All done!")