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
|
119 |
dataHist.GetName().find("CutFlow") is not -1 or
|
120 |
dataHist.GetName().find("GenMatch") is not -1):
|
121 |
ratio = dataHist.Clone()
|
122 |
ratio.Add(mcHist,-1)
|
123 |
ratio.Divide(mcHist)
|
124 |
ratio.SetTitle("")
|
125 |
else:
|
126 |
groups = regroup( [(i,) for i in range(1,1+dataHist.GetNbinsX())] )
|
127 |
ratio = TH1F("ratio","",len(groups), array('d', [dataHist.GetBinLowEdge(min(g)) for g in groups ] + [dataHist.GetXaxis().GetBinUpEdge(dataHist.GetNbinsX())]) )
|
128 |
for i,g in enumerate(groups) :
|
129 |
ratio.SetBinContent(i+1,groupR(g))
|
130 |
ratio.SetBinError(i+1,groupErr(g))
|
131 |
|
132 |
ratio.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
|
133 |
ratio.SetLineColor(1)
|
134 |
ratio.SetLineWidth(2)
|
135 |
return ratio
|
136 |
|
137 |
##########################################################################################################################################
|
138 |
##########################################################################################################################################
|
139 |
##########################################################################################################################################
|
140 |
|
141 |
|
142 |
def MakeOneDHist(pathToDir,histogramName):
|
143 |
|
144 |
|
145 |
numBgMCSamples = 0
|
146 |
numDataSamples = 0
|
147 |
numSignalSamples = 0
|
148 |
|
149 |
Stack = THStack("stack",histogramName)
|
150 |
|
151 |
LumiLabel = TPaveLabel(topLeft_x_left,topLeft_y_bottom,topLeft_x_right,topLeft_y_top,LumiText,"NDC")
|
152 |
LumiLabel.SetBorderSize(0)
|
153 |
LumiLabel.SetFillColor(0)
|
154 |
LumiLabel.SetFillStyle(0)
|
155 |
|
156 |
NormLabel = TPaveLabel()
|
157 |
NormLabel.SetDrawOption("NDC")
|
158 |
NormLabel.SetX1NDC(topLeft_x_left)
|
159 |
NormLabel.SetX2NDC(topLeft_x_right)
|
160 |
|
161 |
NormLabel.SetBorderSize(0)
|
162 |
NormLabel.SetFillColor(0)
|
163 |
NormLabel.SetFillStyle(0)
|
164 |
|
165 |
NormText = ""
|
166 |
if arguments.normalizeToUnitArea:
|
167 |
NormText = "Scaled to unit area"
|
168 |
elif arguments.normalizeToData:
|
169 |
NormText = "MC scaled to data"
|
170 |
NormLabel.SetLabel(NormText)
|
171 |
|
172 |
|
173 |
BgMCLegend = TLegend()
|
174 |
BgTitle = BgMCLegend.AddEntry(0, "Data & Bkgd. MC", "H")
|
175 |
BgTitle.SetTextAlign(22)
|
176 |
BgTitle.SetTextFont(62)
|
177 |
BgMCLegend.SetBorderSize(0)
|
178 |
BgMCLegend.SetFillColor(0)
|
179 |
BgMCLegend.SetFillStyle(0)
|
180 |
SignalMCLegend = TLegend()
|
181 |
SignalTitle = SignalMCLegend.AddEntry(0, "Signal MC", "H")
|
182 |
SignalTitle.SetTextAlign(22)
|
183 |
SignalTitle.SetTextFont(62)
|
184 |
SignalMCLegend.SetBorderSize(0)
|
185 |
SignalMCLegend.SetFillColor(0)
|
186 |
SignalMCLegend.SetFillStyle(0)
|
187 |
|
188 |
outputFile.cd(pathToDir)
|
189 |
Canvas = TCanvas(histogramName)
|
190 |
BgMCHistograms = []
|
191 |
BgMCLegendEntries = []
|
192 |
SignalMCHistograms = []
|
193 |
SignalMCLegendEntries = []
|
194 |
DataHistograms = []
|
195 |
DataLegendEntries = []
|
196 |
|
197 |
|
198 |
backgroundIntegral = 0
|
199 |
dataIntegral = 0
|
200 |
scaleFactor = 1
|
201 |
|
202 |
for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
|
203 |
dataset_file = "%s/%s.root" % (condor_dir,sample)
|
204 |
inputFile = TFile(dataset_file)
|
205 |
Histogram = inputFile.Get(pathToDir+"/"+histogramName).Clone()
|
206 |
Histogram.SetDirectory(0)
|
207 |
inputFile.Close()
|
208 |
if arguments.rebinFactor:
|
209 |
RebinFactor = int(arguments.rebinFactor)
|
210 |
#don't rebin histograms which will have less than 5 bins or any gen-matching histograms
|
211 |
if Histogram.GetNbinsX() >= RebinFactor*5 and Histogram.GetTitle().find("GenMatch") is -1:
|
212 |
Histogram.Rebin(RebinFactor)
|
213 |
|
214 |
xAxisLabel = Histogram.GetXaxis().GetTitle()
|
215 |
histoTitle = Histogram.GetTitle()
|
216 |
|
217 |
legLabel = labels[sample]
|
218 |
if (arguments.printYields):
|
219 |
yieldHist = Histogram.Integral()
|
220 |
legLabel = legLabel + " (%.1f)" % yieldHist
|
221 |
|
222 |
if( types[sample] == "bgMC"):
|
223 |
|
224 |
numBgMCSamples += 1
|
225 |
backgroundIntegral += Histogram.Integral()
|
226 |
|
227 |
Histogram.SetLineStyle(1)
|
228 |
if(arguments.noStack):
|
229 |
Histogram.SetFillStyle(0)
|
230 |
Histogram.SetLineColor(colors[sample])
|
231 |
Histogram.SetLineWidth(2)
|
232 |
else:
|
233 |
Histogram.SetFillStyle(1001)
|
234 |
Histogram.SetFillColor(colors[sample])
|
235 |
Histogram.SetLineColor(1)
|
236 |
Histogram.SetLineWidth(1)
|
237 |
|
238 |
BgMCLegendEntries.append(legLabel)
|
239 |
BgMCHistograms.append(Histogram)
|
240 |
|
241 |
|
242 |
elif( types[sample] == "signalMC"):
|
243 |
|
244 |
numSignalSamples += 1
|
245 |
|
246 |
Histogram.SetFillStyle(0)
|
247 |
Histogram.SetLineColor(colors[sample])
|
248 |
Histogram.SetLineStyle(1)
|
249 |
Histogram.SetLineWidth(2)
|
250 |
if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
|
251 |
Histogram.Scale(1./Histogram.Integral())
|
252 |
|
253 |
SignalMCLegendEntries.append(legLabel)
|
254 |
SignalMCHistograms.append(Histogram)
|
255 |
|
256 |
elif( types[sample] == "data"):
|
257 |
|
258 |
numDataSamples += 1
|
259 |
dataIntegral += Histogram.Integral()
|
260 |
|
261 |
Histogram.SetFillStyle(0)
|
262 |
Histogram.SetLineColor(colors[sample])
|
263 |
Histogram.SetLineStyle(1)
|
264 |
Histogram.SetLineWidth(2)
|
265 |
if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
|
266 |
Histogram.Scale(1./Histogram.Integral())
|
267 |
|
268 |
DataLegendEntries.append(legLabel)
|
269 |
DataHistograms.append(Histogram)
|
270 |
|
271 |
#scaling histograms as per user's specifications
|
272 |
if dataIntegral > 0 and backgroundIntegral > 0:
|
273 |
scaleFactor = dataIntegral/backgroundIntegral
|
274 |
for bgMCHist in BgMCHistograms:
|
275 |
if arguments.normalizeToData:
|
276 |
bgMCHist.Scale(scaleFactor)
|
277 |
|
278 |
if arguments.normalizeToUnitArea and not arguments.noStack and backgroundIntegral > 0:
|
279 |
bgMCHist.Scale(1./backgroundIntegral)
|
280 |
elif arguments.normalizeToUnitArea and arguments.noStack and bgMCHist.Integral() > 0:
|
281 |
bgMCHist.Scale(1./bgMCHist.Integral())
|
282 |
|
283 |
if not arguments.noStack:
|
284 |
Stack.Add(bgMCHist)
|
285 |
|
286 |
|
287 |
|
288 |
### formatting data histograms and adding to legend
|
289 |
legendIndex = 0
|
290 |
for Histogram in DataHistograms:
|
291 |
BgMCLegend.AddEntry(Histogram,DataLegendEntries[legendIndex],"LEP")
|
292 |
legendIndex = legendIndex+1
|
293 |
|
294 |
|
295 |
### creating the histogram to represent the statistical errors on the stack
|
296 |
if numBgMCSamples is not 0 and not arguments.noStack:
|
297 |
ErrorHisto = BgMCHistograms[0].Clone("errors")
|
298 |
ErrorHisto.SetFillStyle(3001)
|
299 |
ErrorHisto.SetFillColor(13)
|
300 |
ErrorHisto.SetLineWidth(0)
|
301 |
BgMCLegend.AddEntry(ErrorHisto,"Stat. Errors","F")
|
302 |
for Histogram in BgMCHistograms:
|
303 |
if Histogram is not BgMCHistograms[0]:
|
304 |
ErrorHisto.Add(Histogram)
|
305 |
|
306 |
|
307 |
### formatting bgMC histograms and adding to legend
|
308 |
legendIndex = numBgMCSamples-1
|
309 |
for Histogram in reversed(BgMCHistograms):
|
310 |
if(arguments.noStack):
|
311 |
BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"L")
|
312 |
else:
|
313 |
BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"F")
|
314 |
legendIndex = legendIndex-1
|
315 |
|
316 |
|
317 |
### formatting signalMC histograms and adding to legend
|
318 |
legendIndex = 0
|
319 |
for Histogram in SignalMCHistograms:
|
320 |
SignalMCLegend.AddEntry(Histogram,SignalMCLegendEntries[legendIndex],"L")
|
321 |
legendIndex = legendIndex+1
|
322 |
|
323 |
|
324 |
### finding the maximum value of anything going on the canvas, so we know how to set the y-axis
|
325 |
finalMax = 0
|
326 |
if numBgMCSamples is not 0 and not arguments.noStack:
|
327 |
finalMax = ErrorHisto.GetMaximum() + ErrorHisto.GetBinError(ErrorHisto.GetMaximumBin())
|
328 |
else:
|
329 |
for bgMCHist in BgMCHistograms:
|
330 |
if(bgMCHist.GetMaximum() > finalMax):
|
331 |
finalMax = bgMCHist.GetMaximum()
|
332 |
for signalMCHist in SignalMCHistograms:
|
333 |
if(signalMCHist.GetMaximum() > finalMax):
|
334 |
finalMax = signalMCHist.GetMaximum()
|
335 |
for dataHist in DataHistograms:
|
336 |
if(dataHist.GetMaximum() > finalMax):
|
337 |
finalMax = dataHist.GetMaximum() + dataHist.GetBinError(dataHist.GetMaximumBin())
|
338 |
finalMax = 1.15*finalMax
|
339 |
|
340 |
|
341 |
### Drawing histograms to canvas
|
342 |
|
343 |
outputFile.cd(pathToDir)
|
344 |
|
345 |
makeRatioPlots = arguments.makeRatioPlots
|
346 |
makeDiffPlots = arguments.makeDiffPlots
|
347 |
|
348 |
if numBgMCSamples is 0 or numDataSamples is not 1:
|
349 |
makeRatioPlots = False
|
350 |
makeDiffPlots = False
|
351 |
if makeRatioPlots or makeDiffPlots:
|
352 |
Canvas.SetFillStyle(0)
|
353 |
Canvas.Divide(1,2)
|
354 |
Canvas.cd(1)
|
355 |
gPad.SetPad(0.01,0.25,0.99,0.99)
|
356 |
gPad.SetMargin(0.1,0.05,0.02,0.07)
|
357 |
gPad.SetFillStyle(0)
|
358 |
gPad.Update()
|
359 |
gPad.Draw()
|
360 |
Canvas.cd(2)
|
361 |
gPad.SetPad(0.01,0.01,0.99,0.25)
|
362 |
#format: gPad.SetMargin(l,r,b,t)
|
363 |
gPad.SetMargin(0.1,0.05,0.4,0.02)
|
364 |
gPad.SetFillStyle(0)
|
365 |
gPad.SetGridy(1)
|
366 |
gPad.Update()
|
367 |
gPad.Draw()
|
368 |
|
369 |
Canvas.cd(1)
|
370 |
|
371 |
if numBgMCSamples is not 0: # the first thing to draw to the canvas is a bgMC sample
|
372 |
|
373 |
|
374 |
if not arguments.noStack: # draw unstacked background samples
|
375 |
Stack.SetTitle(histoTitle)
|
376 |
Stack.Draw("HIST")
|
377 |
Stack.GetXaxis().SetTitle(xAxisLabel)
|
378 |
Stack.SetMaximum(finalMax)
|
379 |
Stack.SetMinimum(0.0001)
|
380 |
if makeRatioPlots or makeDiffPlots:
|
381 |
Stack.GetHistogram().GetXaxis().SetLabelSize(0)
|
382 |
#draw shaded error bands
|
383 |
ErrorHisto.Draw("A E2 SAME")
|
384 |
|
385 |
else: #draw the stacked backgrounds
|
386 |
BgMCHistograms[0].SetTitle(histoTitle)
|
387 |
BgMCHistograms[0].Draw("HIST")
|
388 |
BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
389 |
BgMCHistograms[0].SetMaximum(finalMax)
|
390 |
BgMCHistograms[0].SetMinimum(0.0001)
|
391 |
for bgMCHist in BgMCHistograms:
|
392 |
bgMCHist.Draw("A HIST SAME")
|
393 |
|
394 |
for signalMCHist in SignalMCHistograms:
|
395 |
signalMCHist.Draw("A HIST SAME")
|
396 |
for dataHist in DataHistograms:
|
397 |
dataHist.Draw("A E SAME")
|
398 |
|
399 |
|
400 |
elif numSignalSamples is not 0: # the first thing to draw to the canvas is a signalMC sample
|
401 |
SignalMCHistograms[0].SetTitle(histoTitle)
|
402 |
SignalMCHistograms[0].Draw("HIST")
|
403 |
SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
404 |
SignalMCHistograms[0].SetMaximum(finalMax)
|
405 |
SignalMCHistograms[0].SetMinimum(0.0001)
|
406 |
|
407 |
for signalMCHist in SignalMCHistograms:
|
408 |
if(signalMCHist is not SignalMCHistograms[0]):
|
409 |
signalMCHist.Draw("A HIST SAME")
|
410 |
for dataHist in DataHistograms:
|
411 |
dataHist.Draw("A E SAME")
|
412 |
|
413 |
|
414 |
elif(numDataSamples is not 0): # the first thing to draw to the canvas is a data sample
|
415 |
DataHistograms[0].SetTitle(histoTitle)
|
416 |
DataHistograms[0].Draw("E")
|
417 |
DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
418 |
DataHistograms[0].SetMaximum(finalMax)
|
419 |
DataHistograms[0].SetMinimum(0.0001)
|
420 |
for dataHist in DataHistograms:
|
421 |
if(dataHist is not DataHistograms[0]):
|
422 |
dataHist.Draw("A E SAME")
|
423 |
|
424 |
|
425 |
|
426 |
#legend coordinates, empirically determined :-)
|
427 |
x_left = 0.6761745
|
428 |
x_right = 0.9328859
|
429 |
x_width = x_right - x_left
|
430 |
y_max = 0.9335664
|
431 |
entry_height = 0.05
|
432 |
|
433 |
if(numBgMCSamples is not 0 or numDataSamples is not 0): #then draw the data & bgMC legend
|
434 |
|
435 |
numExtraEntries = 1 # count the legend title
|
436 |
BgMCLegend.SetX1NDC(x_left)
|
437 |
if numBgMCSamples > 0:
|
438 |
numExtraEntries = numExtraEntries + 1 # count the stat. errors entry
|
439 |
|
440 |
BgMCLegend.SetY1NDC(y_max-entry_height*(numExtraEntries+numBgMCSamples+numDataSamples))
|
441 |
BgMCLegend.SetX2NDC(x_right)
|
442 |
BgMCLegend.SetY2NDC(y_max)
|
443 |
BgMCLegend.Draw()
|
444 |
|
445 |
if(numSignalSamples is not 0): #then draw the signalMC legend to the left of the other one
|
446 |
SignalMCLegend.SetX1NDC(x_left-x_width)
|
447 |
SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
|
448 |
SignalMCLegend.SetX2NDC(x_left)
|
449 |
SignalMCLegend.SetY2NDC(y_max)
|
450 |
SignalMCLegend.Draw()
|
451 |
|
452 |
elif numSignalSamples is not 0: #draw the signalMC legend in the upper right corner
|
453 |
SignalMCLegend.SetX1NDC(x_left)
|
454 |
SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
|
455 |
SignalMCLegend.SetX2NDC(x_right)
|
456 |
SignalMCLegend.SetY2NDC(y_max)
|
457 |
SignalMCLegend.Draw()
|
458 |
|
459 |
|
460 |
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
|
461 |
LumiLabel.Draw()
|
462 |
if arguments.normalizeToUnitArea or arguments.normalizeToData:
|
463 |
#move the normalization label down before drawing if we drew the lumi. label
|
464 |
NormLabel.SetY1NDC(topLeft_y_bottom-topLeft_y_offset)
|
465 |
NormLabel.SetY2NDC(topLeft_y_top-topLeft_y_offset)
|
466 |
NormLabel.Draw()
|
467 |
|
468 |
elif arguments.normalizeToUnitArea or arguments.normalizeToData:
|
469 |
NormLabel.SetY1NDC(topLeft_y_bottom)
|
470 |
NormLabel.SetY2NDC(topLeft_y_top)
|
471 |
NormLabel.Draw()
|
472 |
|
473 |
|
474 |
if makeRatioPlots or makeDiffPlots:
|
475 |
Canvas.cd(2)
|
476 |
BgSum = Stack.GetStack().Last()
|
477 |
if makeRatioPlots:
|
478 |
Comparison = ratioHistogram(DataHistograms[0],BgSum)
|
479 |
elif makeDiffPlots:
|
480 |
Comparison = DataHistograms[0].Clone("diff")
|
481 |
Comparison.Add(BgSum,-1)
|
482 |
Comparison.SetTitle("")
|
483 |
Comparison.GetYaxis().SetTitle("Data-MC")
|
484 |
Comparison.GetXaxis().SetTitle(xAxisLabel)
|
485 |
Comparison.GetYaxis().CenterTitle()
|
486 |
Comparison.GetYaxis().SetTitleSize(0.1)
|
487 |
Comparison.GetYaxis().SetTitleOffset(0.4)
|
488 |
Comparison.GetXaxis().SetTitleSize(0.15)
|
489 |
Comparison.GetYaxis().SetLabelSize(0.1)
|
490 |
Comparison.GetXaxis().SetLabelSize(0.15)
|
491 |
if makeRatioPlots:
|
492 |
RatioYRange = 1.15
|
493 |
if arguments.ratioYRange:
|
494 |
RatioYRange = float(arguments.ratioYRange)
|
495 |
Comparison.GetYaxis().SetRangeUser(-1*RatioYRange, RatioYRange)
|
496 |
elif makeDiffPlots:
|
497 |
YMax = Comparison.GetMaximum()
|
498 |
YMin = Comparison.GetMinimum()
|
499 |
if YMax <= 0 and YMin <= 0:
|
500 |
Comparison.GetYaxis().SetRangeUser(-1.2*YMin,0)
|
501 |
elif YMax >= 0 and YMin >= 0:
|
502 |
Comparison.GetYaxis().SetRangeUser(0,1.2*YMax)
|
503 |
else: #axis crosses y=0
|
504 |
if abs(YMax) > abs(YMin):
|
505 |
Comparison.GetYaxis().SetRangeUser(-1.2*YMax,1.2*YMax)
|
506 |
else:
|
507 |
Comparison.GetYaxis().SetRangeUser(-1.2*YMin,1.2*YMin)
|
508 |
|
509 |
Comparison.GetYaxis().SetNdivisions(205)
|
510 |
Comparison.Draw()
|
511 |
|
512 |
Canvas.Write()
|
513 |
if arguments.savePDFs:
|
514 |
pathToDirString = pathToDir.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')
|
515 |
Canvas.SaveAs(condor_dir+"/stacked_histograms_pdfs/"+pathToDirString+"/"+histogramName+".pdf")
|
516 |
|
517 |
|
518 |
##########################################################################################################################################
|
519 |
##########################################################################################################################################
|
520 |
##########################################################################################################################################
|
521 |
|
522 |
def MakeTwoDHist(pathToDir,histogramName):
|
523 |
numBgMCSamples = 0
|
524 |
numDataSamples = 0
|
525 |
numSignalSamples = 0
|
526 |
|
527 |
LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
|
528 |
LumiLabel.SetBorderSize(0)
|
529 |
LumiLabel.SetFillColor(0)
|
530 |
LumiLabel.SetFillStyle(0)
|
531 |
|
532 |
BgMCLegend = TLegend(0.76,0.65,0.99,0.9)
|
533 |
BgMCLegend.AddEntry (0, "Data & Bkgd. MC", "H").SetTextFont (62)
|
534 |
BgMCLegend.SetBorderSize(0)
|
535 |
BgMCLegend.SetFillColor(0)
|
536 |
BgMCLegend.SetFillStyle(0)
|
537 |
SignalMCLegend = TLegend(0.76,0.135,0.99,0.377)
|
538 |
SignalMCLegend.AddEntry (0, "Signal MC", "H").SetTextFont (62)
|
539 |
SignalMCLegend.SetBorderSize(0)
|
540 |
SignalMCLegend.SetFillColor(0)
|
541 |
SignalMCLegend.SetFillStyle(0)
|
542 |
|
543 |
outputFile.cd(pathToDir)
|
544 |
Canvas = TCanvas(histogramName)
|
545 |
Canvas.SetRightMargin(0.2413793);
|
546 |
BgMCHistograms = []
|
547 |
SignalMCHistograms = []
|
548 |
DataHistograms = []
|
549 |
|
550 |
for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
|
551 |
dataset_file = "%s/%s.root" % (condor_dir,sample)
|
552 |
inputFile = TFile(dataset_file)
|
553 |
Histogram = inputFile.Get(pathToDir+"/"+histogramName).Clone()
|
554 |
Histogram.SetDirectory(0)
|
555 |
RebinFactor = int(arguments.rebinFactor)
|
556 |
if arguments.rebinFactor and Histogram.GetNbinsX() >= RebinFactor*10 and Histogram.GetNbinsY() >= RebinFactor*10:
|
557 |
Histogram.Rebin2D(RebinFactor)
|
558 |
inputFile.Close()
|
559 |
xAxisLabel = Histogram.GetXaxis().GetTitle()
|
560 |
yAxisLabel = Histogram.GetYaxis().GetTitle()
|
561 |
histoTitle = Histogram.GetTitle()
|
562 |
|
563 |
if( types[sample] == "bgMC"):
|
564 |
|
565 |
numBgMCSamples += 1
|
566 |
Histogram.SetMarkerColor(colors[sample])
|
567 |
Histogram.SetFillColor(colors[sample])
|
568 |
BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
|
569 |
BgMCHistograms.append(Histogram)
|
570 |
|
571 |
elif( types[sample] == "signalMC"):
|
572 |
|
573 |
numSignalSamples += 1
|
574 |
Histogram.SetMarkerColor(colors[sample])
|
575 |
Histogram.SetFillColor(colors[sample])
|
576 |
SignalMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
|
577 |
SignalMCHistograms.append(Histogram)
|
578 |
|
579 |
elif( types[sample] == "data"):
|
580 |
|
581 |
numDataSamples += 1
|
582 |
Histogram.SetMarkerColor(colors[sample])
|
583 |
Histogram.SetFillColor(colors[sample])
|
584 |
BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
|
585 |
DataHistograms.append(Histogram)
|
586 |
|
587 |
|
588 |
outputFile.cd(pathToDir)
|
589 |
|
590 |
if(numBgMCSamples is not 0):
|
591 |
BgMCHistograms[0].SetTitle(histoTitle)
|
592 |
BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
593 |
BgMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
|
594 |
BgMCHistograms[0].Draw()
|
595 |
for signalMCHist in SignalMCHistograms:
|
596 |
signalMCHist.Draw("SAME")
|
597 |
for dataHist in DataHistograms:
|
598 |
dataHist.Draw("SAME")
|
599 |
|
600 |
elif(numSignalSamples is not 0):
|
601 |
SignalMCHistograms[0].SetTitle(histoTitle)
|
602 |
SignalMCHistograms[0].Draw()
|
603 |
SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
604 |
SignalMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
|
605 |
for signalMCHist in SignalMCHistograms:
|
606 |
if(signalMCHist is not SignalMCHistograms[0]):
|
607 |
signalMCHist.Draw("SAME")
|
608 |
for dataHist in DataHistograms:
|
609 |
dataHist.Draw("SAME")
|
610 |
|
611 |
elif(numDataSamples is not 0):
|
612 |
DataHistograms[0].SetTitle(histoTitle)
|
613 |
DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
614 |
DataHistograms[0].GetYaxis().SetTitle(yAxisLabel)
|
615 |
DataHistograms[0].Draw()
|
616 |
for dataHist in DataHistograms:
|
617 |
if(dataHist is not DataHistograms[0]):
|
618 |
dataHist.Draw("SAME")
|
619 |
|
620 |
|
621 |
if(numBgMCSamples is not 0 or numDataSamples is not 0):
|
622 |
BgMCLegend.Draw()
|
623 |
if(numSignalSamples is not 0):
|
624 |
SignalMCLegend.Draw()
|
625 |
if not arguments.normalizeToUnitArea or numDataSamples > 0:
|
626 |
LumiLabel.Draw()
|
627 |
|
628 |
Canvas.Write()
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
##########################################################################################################################################
|
634 |
##########################################################################################################################################
|
635 |
##########################################################################################################################################
|
636 |
|
637 |
processed_datasets = []
|
638 |
|
639 |
condor_dir = set_condor_output_dir(arguments)
|
640 |
|
641 |
#### check which input datasets have valid output files
|
642 |
for sample in datasets:
|
643 |
fileName = condor_dir + "/" + sample + ".root"
|
644 |
if not os.path.exists(fileName):
|
645 |
continue
|
646 |
testFile = TFile(fileName)
|
647 |
if testFile.IsZombie() or not testFile.GetNkeys():
|
648 |
continue
|
649 |
processed_datasets.append(sample)
|
650 |
|
651 |
if len(processed_datasets) is 0:
|
652 |
sys.exit("No datasets have been processed")
|
653 |
|
654 |
|
655 |
#### make output file
|
656 |
outputFileName = "stacked_histograms.root"
|
657 |
if arguments.outputFileName:
|
658 |
outputFileName = arguments.outputFileName
|
659 |
|
660 |
outputFile = TFile(condor_dir + "/" + outputFileName, "RECREATE")
|
661 |
|
662 |
|
663 |
|
664 |
#### use the first input file as a template and make stacked versions of all its histograms
|
665 |
inputFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
|
666 |
inputFile.cd()
|
667 |
outputFile.cd()
|
668 |
|
669 |
if arguments.savePDFs:
|
670 |
os.system("rm -r %s/stacked_histograms_pdfs" % (condor_dir))
|
671 |
os.system("mkdir %s/stacked_histograms_pdfs" % (condor_dir))
|
672 |
|
673 |
|
674 |
#get root directory in the first layer, generally "OSUAnalysis"
|
675 |
for key in inputFile.GetListOfKeys():
|
676 |
if (key.GetClassName() != "TDirectoryFile"):
|
677 |
continue
|
678 |
rootDirectory = key.GetName()
|
679 |
outputFile.mkdir(rootDirectory)
|
680 |
if arguments.savePDFs:
|
681 |
os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,rootDirectory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
|
682 |
|
683 |
#cd to root directory and look for histograms
|
684 |
inputFile.cd(rootDirectory)
|
685 |
for key2 in gDirectory.GetListOfKeys():
|
686 |
|
687 |
if re.match ('TH1', key2.GetClassName()): # found a 1-D histogram
|
688 |
MakeOneDHist(rootDirectory,key2.GetName())
|
689 |
elif re.match ('TH2', key2.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
|
690 |
MakeTwoDHist(rootDirectory,key2.GetName())
|
691 |
|
692 |
elif (key2.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
|
693 |
level2Directory = rootDirectory+"/"+key2.GetName()
|
694 |
|
695 |
#make a corresponding directory in the output file
|
696 |
outputFile.cd(rootDirectory)
|
697 |
gDirectory.mkdir(key2.GetName())
|
698 |
if arguments.savePDFs:
|
699 |
os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,level2Directory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
|
700 |
|
701 |
#####################################################
|
702 |
### This layer is typically the "channels" layer ###
|
703 |
#####################################################
|
704 |
|
705 |
inputFile.cd(level2Directory)
|
706 |
for key3 in gDirectory.GetListOfKeys():
|
707 |
if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
|
708 |
MakeOneDHist(level2Directory,key3.GetName())
|
709 |
elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
|
710 |
MakeTwoDHist(level2Directory,key3.GetName())
|
711 |
|
712 |
elif (key3.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
|
713 |
level3Directory = level2Directory+"/"+key3.GetName()
|
714 |
|
715 |
#make a corresponding directory in the output file
|
716 |
outputFile.cd(level2Directory)
|
717 |
gDirectory.mkdir(key3.GetName())
|
718 |
if arguments.savePDFs:
|
719 |
os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,level3Directory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
|
720 |
|
721 |
#################################################
|
722 |
### This layer is typically the "cuts" layer ###
|
723 |
#################################################
|
724 |
|
725 |
inputFile.cd(level3Directory)
|
726 |
for key3 in gDirectory.GetListOfKeys():
|
727 |
if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
|
728 |
MakeOneDHist(level3Directory,key3.GetName())
|
729 |
elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
|
730 |
MakeTwoDHist(level3Directory,key3.GetName())
|
731 |
|
732 |
|
733 |
outputFile.Close()
|