ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.14
Committed: Tue Feb 26 10:53:35 2013 UTC (12 years, 2 months ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.13: +45 -23 lines
Log Message:
*** empty log message ***

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 lantonel 1.14 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 lantonel 1.13
31 lantonel 1.1 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 lantonel 1.3 gROOT.ForceStyle()
41 lantonel 1.1 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 lantonel 1.14 if testFile.IsZombie() or not testFile.GetNkeys():
53     continue
54     processed_datasets.append(sample)
55 lantonel 1.1
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 ahart 1.6 weight = intLumi / 10000.0
79     for dataset in processed_datasets:
80     dataset_file = "%s/%s.root" % (condor_dir,dataset)
81 ahart 1.9 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 lantonel 1.1
86     for channel in channels: # loop over final states, which each have their own directory
87    
88     testFile.cd(rootDirectory+"/"+channel)
89 lantonel 1.3
90 lantonel 1.10 for key in gDirectory.GetListOfKeys(): # loop over histograms in the current directory
91     histogramName = key.GetName()
92 lantonel 1.1
93 lantonel 1.10 if re.match ('TH1', key.GetClassName()): # plot a 1-D histogram
94    
95     numBgMCSamples = 0
96     numDataSamples = 0
97     numSignalSamples = 0
98 lantonel 1.1
99 lantonel 1.10 Stack = THStack("stack",histogramName)
100 lantonel 1.1
101 lantonel 1.10 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 lantonel 1.1
113 lantonel 1.10 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 lantonel 1.1
141 lantonel 1.10 if( types[sample] == "bgMC"):
142    
143     numBgMCSamples += 1
144    
145 lantonel 1.14 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 lantonel 1.10 Histogram.SetLineStyle(1)
157    
158     backgroundIntegral += Histogram.Integral()
159    
160     BgMCHistograms.append(Histogram)
161    
162     elif( types[sample] == "signalMC"):
163    
164     numSignalSamples += 1
165 lantonel 1.11
166 lantonel 1.10 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 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 lantonel 1.12 if(options.normalizeToUnitArea and Histogram.Integral() > 0):
184 lantonel 1.11 Histogram.Scale(1./Histogram.Integral())
185 lantonel 1.10
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 lantonel 1.11 if options.normalizeToData:
195 lantonel 1.10 bgMCHist.Scale(scaleFactor)
196 lantonel 1.14 if options.normalizeToUnitArea and not options.noStack and backgroundIntegral > 0:
197 lantonel 1.11 bgMCHist.Scale(1./backgroundIntegral)
198 lantonel 1.14 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 lantonel 1.3
203 lantonel 1.14
204 lantonel 1.11
205 lantonel 1.14 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 lantonel 1.10 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 lantonel 1.4
219 lantonel 1.14
220    
221 lantonel 1.10 if len(DataHistograms) is 1:
222     dataIntegral += DataHistograms[0].Integral()
223    
224     outputFile.cd(rootDirectory+"/"+channel)
225 lantonel 1.3
226 lantonel 1.10 if(numBgMCSamples is not 0):
227 lantonel 1.14 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 lantonel 1.10 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 lantonel 1.1
255 lantonel 1.10 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 lantonel 1.3
264 lantonel 1.10
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 lantonel 1.11 if options.normalizeToData and numBgMCSamples > 0 and numDataSamples > 0:
272 lantonel 1.10 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 lantonel 1.13 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 lantonel 1.1
284 lantonel 1.4
285 lantonel 1.10 Canvas.Write()
286 lantonel 1.1
287 lantonel 1.4
288 lantonel 1.10 if re.match ('TH2', key.GetClassName()): # plot a 2-D histogram
289 lantonel 1.4
290 lantonel 1.10 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 lantonel 1.4
334 lantonel 1.10 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 lantonel 1.3
342 lantonel 1.10 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 lantonel 1.3
369 lantonel 1.10 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 lantonel 1.3
389 lantonel 1.8
390 lantonel 1.10 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 lantonel 1.4
397 lantonel 1.10 Canvas.Write()
398    
399 lantonel 1.4
400 lantonel 1.1
401 ahart 1.6 for dataset in processed_datasets:
402     dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
403     os.remove(dataset_file)
404 lantonel 1.1
405     outputFile.Close()