ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.13
Committed: Thu Feb 21 13:47:39 2013 UTC (12 years, 2 months ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.12: +10 -0 lines
Log Message:
but not today

File Contents

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