1 |
#!/usr/bin/env python
|
2 |
import sys
|
3 |
import os
|
4 |
import re
|
5 |
from math import *
|
6 |
from array import *
|
7 |
from decimal import *
|
8 |
from optparse import OptionParser
|
9 |
from OSUT3Analysis.Configuration.configurationOptions import *
|
10 |
from OSUT3Analysis.Configuration.processingUtilities import *
|
11 |
|
12 |
|
13 |
|
14 |
### parse the command-line options
|
15 |
|
16 |
parser = OptionParser()
|
17 |
parser = set_commandline_arguments(parser)
|
18 |
(arguments, args) = parser.parse_args()
|
19 |
|
20 |
if arguments.localConfig:
|
21 |
sys.path.append(os.getcwd())
|
22 |
exec("from " + arguments.localConfig.rstrip('.py') + " import *")
|
23 |
|
24 |
|
25 |
#### deal with conflicting arguments
|
26 |
if arguments.normalizeToData and arguments.normalizeToUnitArea:
|
27 |
print "Conflicting normalizations requsted, will normalize to unit area"
|
28 |
arguments.normalizeToData = False
|
29 |
if arguments.normalizeToData and arguments.noStack:
|
30 |
print "You have asked to scale non-stacked backgrounds to data. This is a very strange request. Will normalize to unit area instead"
|
31 |
arguments.normalizeToData = False
|
32 |
arguments.normalizeToUnitArea = True
|
33 |
if arguments.makeRatioPlots and arguments.makeDiffPlots:
|
34 |
print "You have requested both ratio and difference plots. Will make just ratio plots instead"
|
35 |
arguments.makeRatioPlots = False
|
36 |
|
37 |
|
38 |
from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TLegendEntry, THStack, TIter, TKey, TPaveLabel, gPad
|
39 |
|
40 |
|
41 |
### setting ROOT options so our plots will look awesome and everyone will love us
|
42 |
|
43 |
gROOT.SetBatch()
|
44 |
gStyle.SetOptStat(0)
|
45 |
gStyle.SetCanvasBorderMode(0)
|
46 |
gStyle.SetPadBorderMode(0)
|
47 |
gStyle.SetPadColor(0)
|
48 |
gStyle.SetCanvasColor(0)
|
49 |
gStyle.SetTextFont(42)
|
50 |
gStyle.SetCanvasDefH(600)
|
51 |
gStyle.SetCanvasDefW(600)
|
52 |
gStyle.SetCanvasDefX(0)
|
53 |
gStyle.SetCanvasDefY(0)
|
54 |
gStyle.SetPadTopMargin(0.05)
|
55 |
gStyle.SetPadBottomMargin(0.13)
|
56 |
gStyle.SetPadLeftMargin(0.15)
|
57 |
gStyle.SetPadRightMargin(0.05)
|
58 |
gStyle.SetTitleColor(1, "XYZ")
|
59 |
gStyle.SetTitleFont(42, "XYZ")
|
60 |
gStyle.SetTitleSize(0.05, "XYZ")
|
61 |
gStyle.SetTitleXOffset(0.95)
|
62 |
gStyle.SetTitleYOffset(1.25)
|
63 |
gStyle.SetTextAlign(12)
|
64 |
gStyle.SetLabelColor(1, "XYZ")
|
65 |
gStyle.SetLabelFont(42, "XYZ")
|
66 |
gStyle.SetLabelOffset(0.007, "XYZ")
|
67 |
gStyle.SetLabelSize(0.05, "XYZ")
|
68 |
gStyle.SetAxisColor(1, "XYZ")
|
69 |
gStyle.SetStripDecimals(True)
|
70 |
gStyle.SetTickLength(0.03, "XYZ")
|
71 |
gStyle.SetNdivisions(510, "XYZ")
|
72 |
gStyle.SetPadTickX(1)
|
73 |
gStyle.SetPadTickY(1)
|
74 |
gROOT.ForceStyle()
|
75 |
|
76 |
|
77 |
#set the text for the luminosity label
|
78 |
if(intLumi < 1000.):
|
79 |
LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
|
80 |
LumiText = "L_{int} = " + str.format('{0:.1f}', LumiInPb) + " pb^{-1}"
|
81 |
else:
|
82 |
LumiInFb = intLumi/1000.
|
83 |
LumiText = "L_{int} = " + str.format('{0:.1f}', LumiInFb) + " fb^{-1}"
|
84 |
|
85 |
|
86 |
#bestest place for lumi. label, in top left corner
|
87 |
topLeft_x_left = 0.1375839
|
88 |
topLeft_x_right = 0.4580537
|
89 |
topLeft_y_bottom = 0.8479021
|
90 |
topLeft_y_top = 0.9475524
|
91 |
topLeft_y_offset = 0.035
|
92 |
|
93 |
|
94 |
##########################################################################################################################################
|
95 |
##########################################################################################################################################
|
96 |
##########################################################################################################################################
|
97 |
|
98 |
# some fancy-ass code from Andrzej Zuranski to merge bins in the ratio plot until the error goes below some threshold
|
99 |
def ratioHistogram( dataHist, mcHist, relErrMax=0.10):
|
100 |
|
101 |
def groupR(group):
|
102 |
Data,MC = [float(sum(hist.GetBinContent(i) for i in group)) for hist in [dataHist,mcHist]]
|
103 |
return (Data-MC)/MC if MC else 0
|
104 |
|
105 |
def groupErr(group):
|
106 |
Data,MC = [float(sum(hist.GetBinContent(i) for i in group)) for hist in [dataHist,mcHist]]
|
107 |
dataErr2,mcErr2 = [sum(hist.GetBinError(i)**2 for i in group) for hist in [dataHist,mcHist]]
|
108 |
return abs(math.sqrt( (dataErr2+mcErr2)/(Data-MC)**2 + mcErr2/MC**2 ) * (Data-MC)/MC) if Data and MC else 0
|
109 |
|
110 |
def regroup(groups):
|
111 |
err,iG = max( (groupErr(g),groups.index(g)) for g in groups )
|
112 |
if err < relErrMax or len(groups)<3 : return groups
|
113 |
iH = max( [iG-1,iG+1], key = lambda i: groupErr(groups[i]) if 0<=i<len(groups) else -1 )
|
114 |
iLo,iHi = sorted([iG,iH])
|
115 |
return regroup(groups[:iLo] + [groups[iLo]+groups[iHi]] + groups[iHi+1:])
|
116 |
|
117 |
#don't rebin the histograms of the number of a given object (except for the pileup ones)
|
118 |
if (dataHist.GetName().find("num") is not -1 and dataHist.GetName().find("Primaryvertexs") is -1) or dataHist.GetName().find("CutFlow") is not -1:
|
119 |
ratio = dataHist.Clone()
|
120 |
ratio.Add(mcHist,-1)
|
121 |
ratio.Divide(mcHist)
|
122 |
ratio.SetTitle("")
|
123 |
else:
|
124 |
groups = regroup( [(i,) for i in range(1,1+dataHist.GetNbinsX())] )
|
125 |
ratio = TH1F("ratio","",len(groups), array('d', [dataHist.GetBinLowEdge(min(g)) for g in groups ] + [dataHist.GetXaxis().GetBinUpEdge(dataHist.GetNbinsX())]) )
|
126 |
for i,g in enumerate(groups) :
|
127 |
ratio.SetBinContent(i+1,groupR(g))
|
128 |
ratio.SetBinError(i+1,groupErr(g))
|
129 |
|
130 |
ratio.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
|
131 |
ratio.SetLineColor(1)
|
132 |
ratio.SetLineWidth(2)
|
133 |
return ratio
|
134 |
|
135 |
##########################################################################################################################################
|
136 |
##########################################################################################################################################
|
137 |
##########################################################################################################################################
|
138 |
|
139 |
|
140 |
def MakeOneDHist(pathToDir,histogramName):
|
141 |
|
142 |
|
143 |
numBgMCSamples = 0
|
144 |
numDataSamples = 0
|
145 |
numSignalSamples = 0
|
146 |
|
147 |
Stack = THStack("stack",histogramName)
|
148 |
|
149 |
LumiLabel = TPaveLabel(topLeft_x_left,topLeft_y_bottom,topLeft_x_right,topLeft_y_top,LumiText,"NDC")
|
150 |
LumiLabel.SetBorderSize(0)
|
151 |
LumiLabel.SetFillColor(0)
|
152 |
LumiLabel.SetFillStyle(0)
|
153 |
|
154 |
NormLabel = TPaveLabel()
|
155 |
NormLabel.SetDrawOption("NDC")
|
156 |
NormLabel.SetX1NDC(topLeft_x_left)
|
157 |
NormLabel.SetX2NDC(topLeft_x_right)
|
158 |
|
159 |
NormLabel.SetBorderSize(0)
|
160 |
NormLabel.SetFillColor(0)
|
161 |
NormLabel.SetFillStyle(0)
|
162 |
|
163 |
NormText = ""
|
164 |
if arguments.normalizeToUnitArea:
|
165 |
NormText = "Scaled to unit area"
|
166 |
elif arguments.normalizeToData:
|
167 |
NormText = "MC scaled to data"
|
168 |
NormLabel.SetLabel(NormText)
|
169 |
|
170 |
|
171 |
BgMCLegend = TLegend()
|
172 |
BgTitle = BgMCLegend.AddEntry(0, "Data & Bkgd. MC", "H")
|
173 |
BgTitle.SetTextAlign(22)
|
174 |
BgTitle.SetTextFont(62)
|
175 |
BgMCLegend.SetBorderSize(0)
|
176 |
BgMCLegend.SetFillColor(0)
|
177 |
BgMCLegend.SetFillStyle(0)
|
178 |
SignalMCLegend = TLegend()
|
179 |
SignalTitle = SignalMCLegend.AddEntry(0, "Signal MC", "H")
|
180 |
SignalTitle.SetTextAlign(22)
|
181 |
SignalTitle.SetTextFont(62)
|
182 |
SignalMCLegend.SetBorderSize(0)
|
183 |
SignalMCLegend.SetFillColor(0)
|
184 |
SignalMCLegend.SetFillStyle(0)
|
185 |
|
186 |
outputFile.cd(pathToDir)
|
187 |
Canvas = TCanvas(histogramName)
|
188 |
BgMCHistograms = []
|
189 |
BgMCLegendEntries = []
|
190 |
SignalMCHistograms = []
|
191 |
SignalMCLegendEntries = []
|
192 |
DataHistograms = []
|
193 |
DataLegendEntries = []
|
194 |
|
195 |
|
196 |
backgroundIntegral = 0
|
197 |
dataIntegral = 0
|
198 |
scaleFactor = 1
|
199 |
|
200 |
for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
|
201 |
dataset_file = "%s/%s.root" % (condor_dir,sample)
|
202 |
inputFile = TFile(dataset_file)
|
203 |
Histogram = inputFile.Get(pathToDir+"/"+histogramName).Clone()
|
204 |
Histogram.SetDirectory(0)
|
205 |
inputFile.Close()
|
206 |
if arguments.rebinFactor:
|
207 |
RebinFactor = int(arguments.rebinFactor)
|
208 |
#don't rebin histograms which will have less than 5 bins or any gen-matching histograms
|
209 |
if Histogram.GetNbinsX() >= RebinFactor*5 and Histogram.GetTitle().find("GenMatch") is -1:
|
210 |
Histogram.Rebin(RebinFactor)
|
211 |
|
212 |
xAxisLabel = Histogram.GetXaxis().GetTitle()
|
213 |
histoTitle = Histogram.GetTitle()
|
214 |
|
215 |
legLabel = labels[sample]
|
216 |
if (arguments.printYields):
|
217 |
yieldHist = Histogram.Integral()
|
218 |
legLabel = legLabel + " (%.1f)" % yieldHist
|
219 |
|
220 |
if( types[sample] == "bgMC"):
|
221 |
|
222 |
numBgMCSamples += 1
|
223 |
backgroundIntegral += Histogram.Integral()
|
224 |
|
225 |
Histogram.SetLineStyle(1)
|
226 |
if(arguments.noStack):
|
227 |
Histogram.SetFillStyle(0)
|
228 |
Histogram.SetLineColor(colors[sample])
|
229 |
Histogram.SetLineWidth(2)
|
230 |
else:
|
231 |
Histogram.SetFillStyle(1001)
|
232 |
Histogram.SetFillColor(colors[sample])
|
233 |
Histogram.SetLineColor(1)
|
234 |
Histogram.SetLineWidth(1)
|
235 |
|
236 |
BgMCLegendEntries.append(legLabel)
|
237 |
BgMCHistograms.append(Histogram)
|
238 |
|
239 |
|
240 |
elif( types[sample] == "signalMC"):
|
241 |
|
242 |
numSignalSamples += 1
|
243 |
|
244 |
Histogram.SetFillStyle(0)
|
245 |
Histogram.SetLineColor(colors[sample])
|
246 |
Histogram.SetLineStyle(1)
|
247 |
Histogram.SetLineWidth(2)
|
248 |
if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
|
249 |
Histogram.Scale(1./Histogram.Integral())
|
250 |
|
251 |
SignalMCLegendEntries.append(legLabel)
|
252 |
SignalMCHistograms.append(Histogram)
|
253 |
|
254 |
elif( types[sample] == "data"):
|
255 |
|
256 |
numDataSamples += 1
|
257 |
dataIntegral += Histogram.Integral()
|
258 |
|
259 |
Histogram.SetFillStyle(0)
|
260 |
Histogram.SetLineColor(colors[sample])
|
261 |
Histogram.SetLineStyle(1)
|
262 |
Histogram.SetLineWidth(2)
|
263 |
if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
|
264 |
Histogram.Scale(1./Histogram.Integral())
|
265 |
|
266 |
DataLegendEntries.append(legLabel)
|
267 |
DataHistograms.append(Histogram)
|
268 |
|
269 |
#scaling histograms as per user's specifications
|
270 |
if dataIntegral > 0 and backgroundIntegral > 0:
|
271 |
scaleFactor = dataIntegral/backgroundIntegral
|
272 |
for bgMCHist in BgMCHistograms:
|
273 |
if arguments.normalizeToData:
|
274 |
bgMCHist.Scale(scaleFactor)
|
275 |
|
276 |
if arguments.normalizeToUnitArea and not arguments.noStack and backgroundIntegral > 0:
|
277 |
bgMCHist.Scale(1./backgroundIntegral)
|
278 |
elif arguments.normalizeToUnitArea and arguments.noStack and bgMCHist.Integral() > 0:
|
279 |
bgMCHist.Scale(1./bgMCHist.Integral())
|
280 |
|
281 |
if not arguments.noStack:
|
282 |
Stack.Add(bgMCHist)
|
283 |
|
284 |
|
285 |
|
286 |
### formatting data histograms and adding to legend
|
287 |
legendIndex = 0
|
288 |
for Histogram in DataHistograms:
|
289 |
BgMCLegend.AddEntry(Histogram,DataLegendEntries[legendIndex],"LEP")
|
290 |
legendIndex = legendIndex+1
|
291 |
|
292 |
|
293 |
### creating the histogram to represent the statistical errors on the stack
|
294 |
if numBgMCSamples is not 0 and not arguments.noStack:
|
295 |
ErrorHisto = BgMCHistograms[0].Clone("errors")
|
296 |
ErrorHisto.SetFillStyle(3001)
|
297 |
ErrorHisto.SetFillColor(13)
|
298 |
ErrorHisto.SetLineWidth(0)
|
299 |
BgMCLegend.AddEntry(ErrorHisto,"Stat. Errors","F")
|
300 |
for Histogram in BgMCHistograms:
|
301 |
if Histogram is not BgMCHistograms[0]:
|
302 |
ErrorHisto.Add(Histogram)
|
303 |
|
304 |
|
305 |
### formatting bgMC histograms and adding to legend
|
306 |
legendIndex = numBgMCSamples-1
|
307 |
for Histogram in reversed(BgMCHistograms):
|
308 |
if(arguments.noStack):
|
309 |
BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"L")
|
310 |
else:
|
311 |
BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"F")
|
312 |
legendIndex = legendIndex-1
|
313 |
|
314 |
|
315 |
### formatting signalMC histograms and adding to legend
|
316 |
legendIndex = 0
|
317 |
for Histogram in SignalMCHistograms:
|
318 |
SignalMCLegend.AddEntry(Histogram,SignalMCLegendEntries[legendIndex],"L")
|
319 |
legendIndex = legendIndex+1
|
320 |
|
321 |
|
322 |
### finding the maximum value of anything going on the canvas, so we know how to set the y-axis
|
323 |
finalMax = 0
|
324 |
if numBgMCSamples is not 0 and not arguments.noStack:
|
325 |
finalMax = ErrorHisto.GetMaximum() + ErrorHisto.GetBinError(ErrorHisto.GetMaximumBin())
|
326 |
else:
|
327 |
for bgMCHist in BgMCHistograms:
|
328 |
if(bgMCHist.GetMaximum() > finalMax):
|
329 |
finalMax = bgMCHist.GetMaximum()
|
330 |
for signalMCHist in SignalMCHistograms:
|
331 |
if(signalMCHist.GetMaximum() > finalMax):
|
332 |
finalMax = signalMCHist.GetMaximum()
|
333 |
for dataHist in DataHistograms:
|
334 |
if(dataHist.GetMaximum() > finalMax):
|
335 |
finalMax = dataHist.GetMaximum() + dataHist.GetBinError(dataHist.GetMaximumBin())
|
336 |
finalMax = 1.15*finalMax
|
337 |
|
338 |
|
339 |
### Drawing histograms to canvas
|
340 |
|
341 |
outputFile.cd(pathToDir)
|
342 |
|
343 |
makeRatioPlots = arguments.makeRatioPlots
|
344 |
makeDiffPlots = arguments.makeDiffPlots
|
345 |
|
346 |
if numBgMCSamples is 0 or numDataSamples is not 1:
|
347 |
makeRatioPlots = False
|
348 |
makeDiffPlots = False
|
349 |
if makeRatioPlots or makeDiffPlots:
|
350 |
Canvas.SetFillStyle(0)
|
351 |
Canvas.Divide(1,2)
|
352 |
Canvas.cd(1)
|
353 |
gPad.SetPad(0.01,0.25,0.99,0.99)
|
354 |
gPad.SetMargin(0.1,0.05,0.02,0.07)
|
355 |
gPad.SetFillStyle(0)
|
356 |
gPad.Update()
|
357 |
gPad.Draw()
|
358 |
Canvas.cd(2)
|
359 |
gPad.SetPad(0.01,0.01,0.99,0.25)
|
360 |
#format: gPad.SetMargin(l,r,b,t)
|
361 |
gPad.SetMargin(0.1,0.05,0.4,0.02)
|
362 |
gPad.SetFillStyle(0)
|
363 |
gPad.SetGridy(1)
|
364 |
gPad.Update()
|
365 |
gPad.Draw()
|
366 |
|
367 |
Canvas.cd(1)
|
368 |
|
369 |
if numBgMCSamples is not 0: # the first thing to draw to the canvas is a bgMC sample
|
370 |
|
371 |
|
372 |
if not arguments.noStack: # draw unstacked background samples
|
373 |
Stack.SetTitle(histoTitle)
|
374 |
Stack.Draw("HIST")
|
375 |
Stack.GetXaxis().SetTitle(xAxisLabel)
|
376 |
Stack.SetMaximum(finalMax)
|
377 |
Stack.SetMinimum(0.0001)
|
378 |
if makeRatioPlots or makeDiffPlots:
|
379 |
Stack.GetHistogram().GetXaxis().SetLabelSize(0)
|
380 |
#draw shaded error bands
|
381 |
ErrorHisto.Draw("A E2 SAME")
|
382 |
|
383 |
else: #draw the stacked backgrounds
|
384 |
BgMCHistograms[0].SetTitle(histoTitle)
|
385 |
BgMCHistograms[0].Draw("HIST")
|
386 |
BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
387 |
BgMCHistograms[0].SetMaximum(finalMax)
|
388 |
BgMCHistograms[0].SetMinimum(0.0001)
|
389 |
for bgMCHist in BgMCHistograms:
|
390 |
bgMCHist.Draw("A HIST SAME")
|
391 |
|
392 |
for signalMCHist in SignalMCHistograms:
|
393 |
signalMCHist.Draw("A HIST SAME")
|
394 |
for dataHist in DataHistograms:
|
395 |
dataHist.Draw("A E SAME")
|
396 |
|
397 |
|
398 |
elif numSignalSamples is not 0: # the first thing to draw to the canvas is a signalMC sample
|
399 |
SignalMCHistograms[0].SetTitle(histoTitle)
|
400 |
SignalMCHistograms[0].Draw("HIST")
|
401 |
SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
402 |
SignalMCHistograms[0].SetMaximum(finalMax)
|
403 |
SignalMCHistograms[0].SetMinimum(0.0001)
|
404 |
|
405 |
for signalMCHist in SignalMCHistograms:
|
406 |
if(signalMCHist is not SignalMCHistograms[0]):
|
407 |
signalMCHist.Draw("A HIST SAME")
|
408 |
for dataHist in DataHistograms:
|
409 |
dataHist.Draw("A E SAME")
|
410 |
|
411 |
|
412 |
elif(numDataSamples is not 0): # the first thing to draw to the canvas is a data sample
|
413 |
DataHistograms[0].SetTitle(histoTitle)
|
414 |
DataHistograms[0].Draw("E")
|
415 |
DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
416 |
DataHistograms[0].SetMaximum(finalMax)
|
417 |
DataHistograms[0].SetMinimum(0.0001)
|
418 |
for dataHist in DataHistograms:
|
419 |
if(dataHist is not DataHistograms[0]):
|
420 |
dataHist.Draw("A E SAME")
|
421 |
|
422 |
|
423 |
|
424 |
#legend coordinates, empirically determined :-)
|
425 |
x_left = 0.6761745
|
426 |
x_right = 0.9328859
|
427 |
x_width = x_right - x_left
|
428 |
y_max = 0.9335664
|
429 |
entry_height = 0.05
|
430 |
|
431 |
if(numBgMCSamples is not 0 or numDataSamples is not 0): #then draw the data & bgMC legend
|
432 |
|
433 |
numExtraEntries = 1 # count the legend title
|
434 |
BgMCLegend.SetX1NDC(x_left)
|
435 |
if numBgMCSamples > 0:
|
436 |
numExtraEntries = numExtraEntries + 1 # count the stat. errors entry
|
437 |
|
438 |
BgMCLegend.SetY1NDC(y_max-entry_height*(numExtraEntries+numBgMCSamples+numDataSamples))
|
439 |
BgMCLegend.SetX2NDC(x_right)
|
440 |
BgMCLegend.SetY2NDC(y_max)
|
441 |
BgMCLegend.Draw()
|
442 |
|
443 |
if(numSignalSamples is not 0): #then draw the signalMC legend to the left of the other one
|
444 |
SignalMCLegend.SetX1NDC(x_left-x_width)
|
445 |
SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
|
446 |
SignalMCLegend.SetX2NDC(x_left)
|
447 |
SignalMCLegend.SetY2NDC(y_max)
|
448 |
SignalMCLegend.Draw()
|
449 |
|
450 |
elif numSignalSamples is not 0: #draw the signalMC legend in the upper right corner
|
451 |
SignalMCLegend.SetX1NDC(x_left)
|
452 |
SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
|
453 |
SignalMCLegend.SetX2NDC(x_right)
|
454 |
SignalMCLegend.SetY2NDC(y_max)
|
455 |
SignalMCLegend.Draw()
|
456 |
|
457 |
|
458 |
if not arguments.normalizeToUnitArea or numDataSamples > 0: #don't draw the lumi label if there's no data and it's scaled to unit area
|
459 |
LumiLabel.Draw()
|
460 |
if arguments.normalizeToUnitArea or arguments.normalizeToData:
|
461 |
#move the normalization label down before drawing if we drew the lumi. label
|
462 |
NormLabel.SetY1NDC(topLeft_y_bottom-topLeft_y_offset)
|
463 |
NormLabel.SetY2NDC(topLeft_y_top-topLeft_y_offset)
|
464 |
NormLabel.Draw()
|
465 |
|
466 |
elif arguments.normalizeToUnitArea or arguments.normalizeToData:
|
467 |
NormLabel.SetY1NDC(topLeft_y_bottom)
|
468 |
NormLabel.SetY2NDC(topLeft_y_top)
|
469 |
NormLabel.Draw()
|
470 |
|
471 |
|
472 |
if makeRatioPlots or makeDiffPlots:
|
473 |
Canvas.cd(2)
|
474 |
BgSum = Stack.GetStack().Last()
|
475 |
if makeRatioPlots:
|
476 |
Comparison = ratioHistogram(DataHistograms[0],BgSum)
|
477 |
elif makeDiffPlots:
|
478 |
Comparison = DataHistograms[0].Clone("diff")
|
479 |
Comparison.Add(BgSum,-1)
|
480 |
Comparison.SetTitle("")
|
481 |
Comparison.GetYaxis().SetTitle("Data-MC")
|
482 |
Comparison.GetXaxis().SetTitle(xAxisLabel)
|
483 |
Comparison.GetYaxis().CenterTitle()
|
484 |
Comparison.GetYaxis().SetTitleSize(0.1)
|
485 |
Comparison.GetYaxis().SetTitleOffset(0.4)
|
486 |
Comparison.GetXaxis().SetTitleSize(0.15)
|
487 |
Comparison.GetYaxis().SetLabelSize(0.1)
|
488 |
Comparison.GetXaxis().SetLabelSize(0.15)
|
489 |
if makeRatioPlots:
|
490 |
RatioYRange = 1.15
|
491 |
if arguments.ratioYRange:
|
492 |
RatioYRange = float(arguments.ratioYRange)
|
493 |
Comparison.GetYaxis().SetRangeUser(-1*RatioYRange, RatioYRange)
|
494 |
elif makeDiffPlots:
|
495 |
YMax = Comparison.GetMaximum()
|
496 |
YMin = Comparison.GetMinimum()
|
497 |
if YMax <= 0 and YMin <= 0:
|
498 |
Comparison.GetYaxis().SetRangeUser(-1.2*YMin,0)
|
499 |
elif YMax >= 0 and YMin >= 0:
|
500 |
Comparison.GetYaxis().SetRangeUser(0,1.2*YMax)
|
501 |
else: #axis crosses y=0
|
502 |
if abs(YMax) > abs(YMin):
|
503 |
Comparison.GetYaxis().SetRangeUser(-1.2*YMax,1.2*YMax)
|
504 |
else:
|
505 |
Comparison.GetYaxis().SetRangeUser(-1.2*YMin,1.2*YMin)
|
506 |
|
507 |
Comparison.GetYaxis().SetNdivisions(205)
|
508 |
Comparison.Draw()
|
509 |
|
510 |
Canvas.Write()
|
511 |
if arguments.savePDFs:
|
512 |
pathToDirString = pathToDir.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')
|
513 |
Canvas.SaveAs(condor_dir+"/stacked_histograms_pdfs/"+pathToDirString+"/"+histogramName+".pdf")
|
514 |
|
515 |
|
516 |
##########################################################################################################################################
|
517 |
##########################################################################################################################################
|
518 |
##########################################################################################################################################
|
519 |
|
520 |
def MakeTwoDHist(pathToDir,histogramName):
|
521 |
numBgMCSamples = 0
|
522 |
numDataSamples = 0
|
523 |
numSignalSamples = 0
|
524 |
|
525 |
LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
|
526 |
LumiLabel.SetBorderSize(0)
|
527 |
LumiLabel.SetFillColor(0)
|
528 |
LumiLabel.SetFillStyle(0)
|
529 |
|
530 |
BgMCLegend = TLegend(0.76,0.65,0.99,0.9)
|
531 |
BgMCLegend.AddEntry (0, "Data & Bkgd. MC", "H").SetTextFont (62)
|
532 |
BgMCLegend.SetBorderSize(0)
|
533 |
BgMCLegend.SetFillColor(0)
|
534 |
BgMCLegend.SetFillStyle(0)
|
535 |
SignalMCLegend = TLegend(0.76,0.135,0.99,0.377)
|
536 |
SignalMCLegend.AddEntry (0, "Signal MC", "H").SetTextFont (62)
|
537 |
SignalMCLegend.SetBorderSize(0)
|
538 |
SignalMCLegend.SetFillColor(0)
|
539 |
SignalMCLegend.SetFillStyle(0)
|
540 |
|
541 |
outputFile.cd(pathToDir)
|
542 |
Canvas = TCanvas(histogramName)
|
543 |
Canvas.SetRightMargin(0.2413793);
|
544 |
BgMCHistograms = []
|
545 |
SignalMCHistograms = []
|
546 |
DataHistograms = []
|
547 |
|
548 |
for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
|
549 |
dataset_file = "%s/%s.root" % (condor_dir,sample)
|
550 |
inputFile = TFile(dataset_file)
|
551 |
Histogram = inputFile.Get(pathToDir+"/"+histogramName).Clone()
|
552 |
Histogram.SetDirectory(0)
|
553 |
RebinFactor = int(arguments.rebinFactor)
|
554 |
if arguments.rebinFactor and Histogram.GetNbinsX() >= RebinFactor*10 and Histogram.GetNbinsY() >= RebinFactor*10:
|
555 |
Histogram.Rebin2D(RebinFactor)
|
556 |
inputFile.Close()
|
557 |
xAxisLabel = Histogram.GetXaxis().GetTitle()
|
558 |
yAxisLabel = Histogram.GetYaxis().GetTitle()
|
559 |
histoTitle = Histogram.GetTitle()
|
560 |
|
561 |
if( types[sample] == "bgMC"):
|
562 |
|
563 |
numBgMCSamples += 1
|
564 |
Histogram.SetMarkerColor(colors[sample])
|
565 |
Histogram.SetFillColor(colors[sample])
|
566 |
BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
|
567 |
BgMCHistograms.append(Histogram)
|
568 |
|
569 |
elif( types[sample] == "signalMC"):
|
570 |
|
571 |
numSignalSamples += 1
|
572 |
Histogram.SetMarkerColor(colors[sample])
|
573 |
Histogram.SetFillColor(colors[sample])
|
574 |
SignalMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
|
575 |
SignalMCHistograms.append(Histogram)
|
576 |
|
577 |
elif( types[sample] == "data"):
|
578 |
|
579 |
numDataSamples += 1
|
580 |
Histogram.SetMarkerColor(colors[sample])
|
581 |
Histogram.SetFillColor(colors[sample])
|
582 |
BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
|
583 |
DataHistograms.append(Histogram)
|
584 |
|
585 |
|
586 |
outputFile.cd(pathToDir)
|
587 |
|
588 |
if(numBgMCSamples is not 0):
|
589 |
BgMCHistograms[0].SetTitle(histoTitle)
|
590 |
BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
591 |
BgMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
|
592 |
BgMCHistograms[0].Draw()
|
593 |
for signalMCHist in SignalMCHistograms:
|
594 |
signalMCHist.Draw("SAME")
|
595 |
for dataHist in DataHistograms:
|
596 |
dataHist.Draw("SAME")
|
597 |
|
598 |
elif(numSignalSamples is not 0):
|
599 |
SignalMCHistograms[0].SetTitle(histoTitle)
|
600 |
SignalMCHistograms[0].Draw()
|
601 |
SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
602 |
SignalMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
|
603 |
for signalMCHist in SignalMCHistograms:
|
604 |
if(signalMCHist is not SignalMCHistograms[0]):
|
605 |
signalMCHist.Draw("SAME")
|
606 |
for dataHist in DataHistograms:
|
607 |
dataHist.Draw("SAME")
|
608 |
|
609 |
elif(numDataSamples is not 0):
|
610 |
DataHistograms[0].SetTitle(histoTitle)
|
611 |
DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
612 |
DataHistograms[0].GetYaxis().SetTitle(yAxisLabel)
|
613 |
DataHistograms[0].Draw()
|
614 |
for dataHist in DataHistograms:
|
615 |
if(dataHist is not DataHistograms[0]):
|
616 |
dataHist.Draw("SAME")
|
617 |
|
618 |
|
619 |
if(numBgMCSamples is not 0 or numDataSamples is not 0):
|
620 |
BgMCLegend.Draw()
|
621 |
if(numSignalSamples is not 0):
|
622 |
SignalMCLegend.Draw()
|
623 |
if not arguments.normalizeToUnitArea or numDataSamples > 0:
|
624 |
LumiLabel.Draw()
|
625 |
|
626 |
Canvas.Write()
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
##########################################################################################################################################
|
632 |
##########################################################################################################################################
|
633 |
##########################################################################################################################################
|
634 |
|
635 |
processed_datasets = []
|
636 |
|
637 |
condor_dir = set_condor_output_dir(arguments)
|
638 |
|
639 |
#### check which input datasets have valid output files
|
640 |
for sample in datasets:
|
641 |
fileName = condor_dir + "/" + sample + ".root"
|
642 |
if not os.path.exists(fileName):
|
643 |
continue
|
644 |
testFile = TFile(fileName)
|
645 |
if testFile.IsZombie() or not testFile.GetNkeys():
|
646 |
continue
|
647 |
processed_datasets.append(sample)
|
648 |
|
649 |
if len(processed_datasets) is 0:
|
650 |
sys.exit("No datasets have been processed")
|
651 |
|
652 |
|
653 |
#### make output file
|
654 |
outputFileName = "stacked_histograms.root"
|
655 |
if arguments.outputFileName:
|
656 |
outputFileName = arguments.outputFileName
|
657 |
|
658 |
outputFile = TFile(condor_dir + "/" + outputFileName, "RECREATE")
|
659 |
|
660 |
|
661 |
|
662 |
#### use the first input file as a template and make stacked versions of all its histograms
|
663 |
inputFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
|
664 |
inputFile.cd()
|
665 |
outputFile.cd()
|
666 |
|
667 |
if arguments.savePDFs:
|
668 |
os.system("rm -r %s/stacked_histograms_pdfs" % (condor_dir))
|
669 |
os.system("mkdir %s/stacked_histograms_pdfs" % (condor_dir))
|
670 |
|
671 |
|
672 |
#get root directory in the first layer, generally "OSUAnalysis"
|
673 |
for key in inputFile.GetListOfKeys():
|
674 |
if (key.GetClassName() != "TDirectoryFile"):
|
675 |
continue
|
676 |
rootDirectory = key.GetName()
|
677 |
outputFile.mkdir(rootDirectory)
|
678 |
if arguments.savePDFs:
|
679 |
os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,rootDirectory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
|
680 |
|
681 |
#cd to root directory and look for histograms
|
682 |
inputFile.cd(rootDirectory)
|
683 |
for key2 in gDirectory.GetListOfKeys():
|
684 |
|
685 |
if re.match ('TH1', key2.GetClassName()): # found a 1-D histogram
|
686 |
MakeOneDHist(rootDirectory,key2.GetName())
|
687 |
elif re.match ('TH2', key2.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
|
688 |
MakeTwoDHist(rootDirectory,key2.GetName())
|
689 |
|
690 |
elif (key2.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
|
691 |
level2Directory = rootDirectory+"/"+key2.GetName()
|
692 |
|
693 |
#make a corresponding directory in the output file
|
694 |
outputFile.cd(rootDirectory)
|
695 |
gDirectory.mkdir(key2.GetName())
|
696 |
if arguments.savePDFs:
|
697 |
os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,level2Directory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
|
698 |
|
699 |
#####################################################
|
700 |
### This layer is typically the "channels" layer ###
|
701 |
#####################################################
|
702 |
|
703 |
inputFile.cd(level2Directory)
|
704 |
for key3 in gDirectory.GetListOfKeys():
|
705 |
if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
|
706 |
MakeOneDHist(level2Directory,key3.GetName())
|
707 |
elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
|
708 |
MakeTwoDHist(level2Directory,key3.GetName())
|
709 |
|
710 |
elif (key3.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
|
711 |
level3Directory = level2Directory+"/"+key3.GetName()
|
712 |
|
713 |
#make a corresponding directory in the output file
|
714 |
outputFile.cd(level2Directory)
|
715 |
gDirectory.mkdir(key3.GetName())
|
716 |
if arguments.savePDFs:
|
717 |
os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,level3Directory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
|
718 |
|
719 |
#################################################
|
720 |
### This layer is typically the "cuts" layer ###
|
721 |
#################################################
|
722 |
|
723 |
inputFile.cd(level3Directory)
|
724 |
for key3 in gDirectory.GetListOfKeys():
|
725 |
if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
|
726 |
MakeOneDHist(level3Directory,key3.GetName())
|
727 |
elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
|
728 |
MakeTwoDHist(level3Directory,key3.GetName())
|
729 |
|
730 |
|
731 |
outputFile.Close()
|