ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.20
Committed: Tue Mar 19 18:43:26 2013 UTC (12 years, 1 month ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.19: +4 -4 lines
Log Message:
changed default minimum to 0.0001 which is better when normalizing to unit area

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