1 |
#!/usr/bin/env python
|
2 |
import sys
|
3 |
import os
|
4 |
import re
|
5 |
from array import *
|
6 |
from decimal import *
|
7 |
from optparse import OptionParser
|
8 |
from OSUT3Analysis.Configuration.configurationOptions import *
|
9 |
from OSUT3Analysis.Configuration.processingUtilities import *
|
10 |
|
11 |
parser = OptionParser()
|
12 |
parser = set_commandline_arguments(parser)
|
13 |
|
14 |
(options, args) = parser.parse_args()
|
15 |
|
16 |
if options.localConfig:
|
17 |
sys.path.append(os.getcwd())
|
18 |
exec("from " + options.localConfig.rstrip('.py') + " import *")
|
19 |
|
20 |
condor_dir = set_condor_output_dir(options)
|
21 |
|
22 |
|
23 |
if options.normalizeToData and options.normalizeToUnitArea:
|
24 |
print "Conflicting normalizations requsted, will normalize to unit area"
|
25 |
options.normalizeToData = False
|
26 |
if options.normalizeToData and options.noStack:
|
27 |
print "You have asked to scale non-stacked backgrounds to data. This is a very strange request. Will normalize to unit area instead"
|
28 |
options.normalizeToData = False
|
29 |
options.normalizeToUnitArea = True
|
30 |
|
31 |
from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TArrow, THStack, TIter, TKey, TPaveLabel
|
32 |
|
33 |
gROOT.SetBatch()
|
34 |
gStyle.SetOptStat(0)
|
35 |
gStyle.SetCanvasBorderMode(0)
|
36 |
gStyle.SetPadBorderMode(0)
|
37 |
gStyle.SetPadColor(0)
|
38 |
gStyle.SetCanvasColor(0)
|
39 |
gStyle.SetTextFont(42)
|
40 |
gROOT.ForceStyle()
|
41 |
outputFile = TFile(condor_dir + "/stacked_histograms.root", "RECREATE")
|
42 |
|
43 |
channels = []
|
44 |
processed_datasets = []
|
45 |
|
46 |
#### check which input datasets have valid output files
|
47 |
for sample in datasets:
|
48 |
fileName = condor_dir + "/" + sample + ".root"
|
49 |
if not os.path.exists(fileName):
|
50 |
continue
|
51 |
testFile = TFile(fileName)
|
52 |
if testFile.IsZombie() or not testFile.GetNkeys():
|
53 |
continue
|
54 |
processed_datasets.append(sample)
|
55 |
|
56 |
if len(processed_datasets) is 0:
|
57 |
sys.exit("No datasets have been processed")
|
58 |
|
59 |
#### open first input file and re-make its directory structure in the output file
|
60 |
testFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
|
61 |
testFile.cd()
|
62 |
for key in testFile.GetListOfKeys():
|
63 |
if (key.GetClassName() != "TDirectoryFile"):
|
64 |
continue
|
65 |
outputFile.cd()
|
66 |
outputFile.mkdir(key.GetName())
|
67 |
rootDirectory = key.GetName()
|
68 |
|
69 |
testFile.cd(key.GetName())
|
70 |
for key2 in gDirectory.GetListOfKeys():
|
71 |
if (key2.GetClassName() != "TDirectoryFile"):
|
72 |
continue
|
73 |
outputFile.cd(key.GetName())
|
74 |
gDirectory.mkdir(key2.GetName())
|
75 |
channels.append(key2.GetName())
|
76 |
|
77 |
|
78 |
weight = intLumi / 10000.0
|
79 |
for dataset in processed_datasets:
|
80 |
dataset_file = "%s/%s.root" % (condor_dir,dataset)
|
81 |
if types[dataset] != "data":
|
82 |
os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", weight))
|
83 |
else:
|
84 |
os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", 1.0))
|
85 |
|
86 |
for channel in channels: # loop over final states, which each have their own directory
|
87 |
|
88 |
testFile.cd(rootDirectory+"/"+channel)
|
89 |
|
90 |
for key in gDirectory.GetListOfKeys(): # loop over histograms in the current directory
|
91 |
histogramName = key.GetName()
|
92 |
|
93 |
if re.match ('TH1', key.GetClassName()): # plot a 1-D histogram
|
94 |
|
95 |
numBgMCSamples = 0
|
96 |
numDataSamples = 0
|
97 |
numSignalSamples = 0
|
98 |
|
99 |
Stack = THStack("stack",histogramName)
|
100 |
|
101 |
if(intLumi < 1000.):
|
102 |
LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
|
103 |
else:
|
104 |
getcontext().prec = 2
|
105 |
LumiInFb = intLumi/1000.
|
106 |
LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
|
107 |
|
108 |
LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
|
109 |
LumiLabel.SetBorderSize(0)
|
110 |
LumiLabel.SetFillColor(0)
|
111 |
LumiLabel.SetFillStyle(0)
|
112 |
|
113 |
BgMCLegend = TLegend(0.70,0.65,0.99,0.89, "Data & Bkgd. MC")
|
114 |
BgMCLegend.SetBorderSize(0)
|
115 |
BgMCLegend.SetFillColor(0)
|
116 |
BgMCLegend.SetFillStyle(0)
|
117 |
SignalMCLegend = TLegend(0.45,0.65,0.70,0.89,"Signal MC")
|
118 |
SignalMCLegend.SetBorderSize(0)
|
119 |
SignalMCLegend.SetFillColor(0)
|
120 |
SignalMCLegend.SetFillStyle(0)
|
121 |
|
122 |
outputFile.cd(rootDirectory+"/"+channel)
|
123 |
Canvas = TCanvas(histogramName)
|
124 |
BgMCHistograms = []
|
125 |
SignalMCHistograms = []
|
126 |
DataHistograms = []
|
127 |
|
128 |
backgroundIntegral = 0
|
129 |
dataIntegral = 0
|
130 |
scaleFactor = 1
|
131 |
|
132 |
for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
|
133 |
dataset_file = "%s/%s.root_tmp" % (condor_dir,sample)
|
134 |
inputFile = TFile(dataset_file)
|
135 |
Histogram = inputFile.Get(rootDirectory+"/"+channel+"/"+histogramName).Clone()
|
136 |
Histogram.SetDirectory(0)
|
137 |
inputFile.Close()
|
138 |
xAxisLabel = Histogram.GetXaxis().GetTitle()
|
139 |
histoTitle = Histogram.GetTitle()
|
140 |
|
141 |
if( types[sample] == "bgMC"):
|
142 |
|
143 |
numBgMCSamples += 1
|
144 |
|
145 |
if(options.noStack):
|
146 |
Histogram.SetFillStyle(0)
|
147 |
Histogram.SetLineColor(colors[sample])
|
148 |
Histogram.SetLineWidth(2)
|
149 |
BgMCLegend.AddEntry(Histogram,labels[sample],"L")
|
150 |
else:
|
151 |
Histogram.SetFillStyle(1001)
|
152 |
Histogram.SetFillColor(colors[sample])
|
153 |
Histogram.SetLineColor(1)
|
154 |
Histogram.SetLineWidth(1)
|
155 |
BgMCLegend.AddEntry(Histogram,labels[sample],"F")
|
156 |
Histogram.SetLineStyle(1)
|
157 |
|
158 |
backgroundIntegral += Histogram.Integral()
|
159 |
|
160 |
BgMCHistograms.append(Histogram)
|
161 |
|
162 |
elif( types[sample] == "signalMC"):
|
163 |
|
164 |
numSignalSamples += 1
|
165 |
|
166 |
Histogram.SetFillStyle(0)
|
167 |
Histogram.SetLineColor(colors[sample])
|
168 |
Histogram.SetLineStyle(1)
|
169 |
Histogram.SetLineWidth(2)
|
170 |
if(options.normalizeToUnitArea and Histogram.Integral() > 0):
|
171 |
Histogram.Scale(1./Histogram.Integral())
|
172 |
SignalMCLegend.AddEntry(Histogram,labels[sample],"L")
|
173 |
SignalMCHistograms.append(Histogram)
|
174 |
|
175 |
elif( types[sample] == "data"):
|
176 |
|
177 |
numDataSamples += 1
|
178 |
|
179 |
Histogram.SetFillStyle(0)
|
180 |
Histogram.SetLineColor(colors[sample])
|
181 |
Histogram.SetLineStyle(1)
|
182 |
Histogram.SetLineWidth(2)
|
183 |
if(options.normalizeToUnitArea and Histogram.Integral() > 0):
|
184 |
Histogram.Scale(1./Histogram.Integral())
|
185 |
|
186 |
dataIntegral += Histogram.Integral()
|
187 |
|
188 |
BgMCLegend.AddEntry(Histogram,labels[sample],"LEP")
|
189 |
DataHistograms.append(Histogram)
|
190 |
|
191 |
if dataIntegral > 0 and backgroundIntegral > 0:
|
192 |
scaleFactor = dataIntegral/backgroundIntegral
|
193 |
for bgMCHist in BgMCHistograms:
|
194 |
if options.normalizeToData:
|
195 |
bgMCHist.Scale(scaleFactor)
|
196 |
if options.normalizeToUnitArea and not options.noStack and backgroundIntegral > 0:
|
197 |
bgMCHist.Scale(1./backgroundIntegral)
|
198 |
elif options.normalizeToUnitArea and options.noStack and bgMCHist.Integral() > 0:
|
199 |
bgMCHist.Scale(1./bgMCHist.Integral())
|
200 |
if not options.noStack:
|
201 |
Stack.Add(bgMCHist)
|
202 |
|
203 |
|
204 |
|
205 |
finalMax = 0
|
206 |
if not options.noStack:
|
207 |
finalMax = Stack.GetMaximum()
|
208 |
else:
|
209 |
for bgMCHist in BgMCHistograms:
|
210 |
if(bgMCHist.GetMaximum() > finalMax):
|
211 |
finalMax = bgMCHist.GetMaximum()
|
212 |
for signalMCHist in SignalMCHistograms:
|
213 |
if(signalMCHist.GetMaximum() > finalMax):
|
214 |
finalMax = signalMCHist.GetMaximum()
|
215 |
for dataHist in DataHistograms:
|
216 |
if(dataHist.GetMaximum() > finalMax):
|
217 |
finalMax = dataHist.GetMaximum()
|
218 |
|
219 |
|
220 |
|
221 |
if len(DataHistograms) is 1:
|
222 |
dataIntegral += DataHistograms[0].Integral()
|
223 |
|
224 |
outputFile.cd(rootDirectory+"/"+channel)
|
225 |
|
226 |
if(numBgMCSamples is not 0):
|
227 |
if not options.noStack:
|
228 |
Stack.SetTitle(histoTitle)
|
229 |
Stack.Draw("HIST")
|
230 |
Stack.GetXaxis().SetTitle(xAxisLabel)
|
231 |
Stack.SetMaximum(1.1*finalMax)
|
232 |
else:
|
233 |
BgMCHistograms[0].SetTitle(histoTitle)
|
234 |
BgMCHistograms[0].Draw("HIST")
|
235 |
BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
236 |
BgMCHistograms[0].SetMaximum(1.1*finalMax)
|
237 |
for bgMCHist in BgMCHistograms:
|
238 |
bgMCHist.Draw("HIST SAME")
|
239 |
for signalMCHist in SignalMCHistograms:
|
240 |
signalMCHist.Draw("HIST SAME")
|
241 |
for dataHist in DataHistograms:
|
242 |
dataHist.Draw("E SAME")
|
243 |
|
244 |
elif(numSignalSamples is not 0):
|
245 |
SignalMCHistograms[0].SetTitle(histoTitle)
|
246 |
SignalMCHistograms[0].Draw("HIST")
|
247 |
SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
248 |
SignalMCHistograms[0].SetMaximum(1.1*finalMax)
|
249 |
for signalMCHist in SignalMCHistograms:
|
250 |
if(signalMCHist is not SignalMCHistograms[0]):
|
251 |
signalMCHist.Draw("HIST SAME")
|
252 |
for dataHist in DataHistograms:
|
253 |
dataHist.Draw("E SAME")
|
254 |
|
255 |
elif(numDataSamples is not 0):
|
256 |
DataHistograms[0].SetTitle(histoTitle)
|
257 |
DataHistograms[0].Draw("E")
|
258 |
DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
259 |
DataHistograms[0].SetMaximum(1.1*finalMax)
|
260 |
for dataHist in DataHistograms:
|
261 |
if(dataHist is not DataHistograms[0]):
|
262 |
dataHist.Draw("E SAME")
|
263 |
|
264 |
|
265 |
if(numBgMCSamples is not 0 or numDataSamples is not 0):
|
266 |
BgMCLegend.Draw()
|
267 |
if(numSignalSamples is not 0):
|
268 |
SignalMCLegend.Draw()
|
269 |
|
270 |
LumiLabel.Draw()
|
271 |
if options.normalizeToData and numBgMCSamples > 0 and numDataSamples > 0:
|
272 |
NormLabel = TPaveLabel(0.1,0.75,0.35,0.85,"MC scaled to data","NDC")
|
273 |
NormLabel.SetBorderSize(0)
|
274 |
NormLabel.SetFillColor(0)
|
275 |
NormLabel.SetFillStyle(0)
|
276 |
NormLabel.Draw()
|
277 |
elif options.normalizeToUnitArea:
|
278 |
NormLabel = TPaveLabel(0.1,0.75,0.35,0.85,"Scaled to unit area","NDC")
|
279 |
NormLabel.SetBorderSize(0)
|
280 |
NormLabel.SetFillColor(0)
|
281 |
NormLabel.SetFillStyle(0)
|
282 |
NormLabel.Draw()
|
283 |
|
284 |
|
285 |
Canvas.Write()
|
286 |
|
287 |
|
288 |
if re.match ('TH2', key.GetClassName()): # plot a 2-D histogram
|
289 |
|
290 |
numBgMCSamples = 0
|
291 |
numDataSamples = 0
|
292 |
numSignalSamples = 0
|
293 |
|
294 |
if(intLumi < 1000.):
|
295 |
LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
|
296 |
else:
|
297 |
getcontext().prec = 2
|
298 |
LumiInFb = intLumi/1000.
|
299 |
LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
|
300 |
|
301 |
LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
|
302 |
LumiLabel.SetBorderSize(0)
|
303 |
LumiLabel.SetFillColor(0)
|
304 |
LumiLabel.SetFillStyle(0)
|
305 |
|
306 |
BgMCLegend = TLegend(0.76,0.65,0.99,0.9, "Data & Bkgd. MC")
|
307 |
BgMCLegend.SetBorderSize(0)
|
308 |
BgMCLegend.SetFillColor(0)
|
309 |
BgMCLegend.SetFillStyle(0)
|
310 |
SignalMCLegend = TLegend(0.76,0.135,0.99,0.377,"Signal MC")
|
311 |
SignalMCLegend.SetBorderSize(0)
|
312 |
SignalMCLegend.SetFillColor(0)
|
313 |
SignalMCLegend.SetFillStyle(0)
|
314 |
|
315 |
outputFile.cd(rootDirectory+"/"+channel)
|
316 |
Canvas = TCanvas(histogramName)
|
317 |
Canvas.SetRightMargin(0.2413793);
|
318 |
BgMCHistograms = []
|
319 |
SignalMCHistograms = []
|
320 |
DataHistograms = []
|
321 |
|
322 |
for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
|
323 |
dataset_file = "%s/%s.root_tmp" % (condor_dir,sample)
|
324 |
inputFile = TFile(dataset_file)
|
325 |
Histogram = inputFile.Get(rootDirectory+"/"+channel+"/"+histogramName).Clone()
|
326 |
Histogram.SetDirectory(0)
|
327 |
inputFile.Close()
|
328 |
xAxisLabel = Histogram.GetXaxis().GetTitle()
|
329 |
yAxisLabel = Histogram.GetYaxis().GetTitle()
|
330 |
histoTitle = Histogram.GetTitle()
|
331 |
|
332 |
if( types[sample] == "bgMC"):
|
333 |
|
334 |
numBgMCSamples += 1
|
335 |
Histogram.SetMarkerColor(colors[sample])
|
336 |
Histogram.SetFillColor(colors[sample])
|
337 |
BgMCLegend.AddEntry(Histogram,labels[sample],"F")
|
338 |
BgMCHistograms.append(Histogram)
|
339 |
|
340 |
elif( types[sample] == "signalMC"):
|
341 |
|
342 |
numSignalSamples += 1
|
343 |
Histogram.SetMarkerColor(colors[sample])
|
344 |
Histogram.SetFillColor(colors[sample])
|
345 |
SignalMCLegend.AddEntry(Histogram,labels[sample],"F")
|
346 |
SignalMCHistograms.append(Histogram)
|
347 |
|
348 |
elif( types[sample] == "data"):
|
349 |
|
350 |
numDataSamples += 1
|
351 |
Histogram.SetMarkerColor(colors[sample])
|
352 |
Histogram.SetFillColor(colors[sample])
|
353 |
BgMCLegend.AddEntry(Histogram,labels[sample],"F")
|
354 |
DataHistograms.append(Histogram)
|
355 |
|
356 |
|
357 |
outputFile.cd(rootDirectory+"/"+channel)
|
358 |
|
359 |
if(numBgMCSamples is not 0):
|
360 |
BgMCHistograms[0].SetTitle(histoTitle)
|
361 |
BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
362 |
BgMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
|
363 |
BgMCHistograms[0].Draw()
|
364 |
for signalMCHist in SignalMCHistograms:
|
365 |
signalMCHist.Draw("SAME")
|
366 |
for dataHist in DataHistograms:
|
367 |
dataHist.Draw("SAME")
|
368 |
|
369 |
elif(numSignalSamples is not 0):
|
370 |
SignalMCHistograms[0].SetTitle(histoTitle)
|
371 |
SignalMCHistograms[0].Draw()
|
372 |
SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
373 |
SignalMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
|
374 |
for signalMCHist in SignalMCHistograms:
|
375 |
if(signalMCHist is not SignalMCHistograms[0]):
|
376 |
signalMCHist.Draw("SAME")
|
377 |
for dataHist in DataHistograms:
|
378 |
dataHist.Draw("SAME")
|
379 |
|
380 |
elif(numDataSamples is not 0):
|
381 |
DataHistograms[0].SetTitle(histoTitle)
|
382 |
DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
|
383 |
DataHistograms[0].GetYaxis().SetTitle(yAxisLabel)
|
384 |
DataHistograms[0].Draw()
|
385 |
for dataHist in DataHistograms:
|
386 |
if(dataHist is not DataHistograms[0]):
|
387 |
dataHist.Draw("SAME")
|
388 |
|
389 |
|
390 |
if(numBgMCSamples is not 0 or numDataSamples is not 0):
|
391 |
BgMCLegend.Draw()
|
392 |
if(numSignalSamples is not 0):
|
393 |
SignalMCLegend.Draw()
|
394 |
|
395 |
LumiLabel.Draw()
|
396 |
|
397 |
Canvas.Write()
|
398 |
|
399 |
|
400 |
|
401 |
for dataset in processed_datasets:
|
402 |
dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
|
403 |
os.remove(dataset_file)
|
404 |
|
405 |
outputFile.Close()
|