ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.12
Committed: Thu Feb 21 13:20:26 2013 UTC (12 years, 2 months ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.11: +2 -2 lines
Log Message:
one of these days i'm gonna learn to test things before i commit them...

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