ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.21
Committed: Mon Mar 25 18:17:40 2013 UTC (12 years, 1 month ago) by ahart
Content type: text/x-python
Branch: MAIN
Changes since 1.20: +20 -1 lines
Log Message:
Added an option for turning off cross section weighting and added a histogram to the output files for keeping track of certain flags passed to mergeOutput.py.

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.21 fin = TFile (dataset_file)
88     flags = fin.Get ("flags")
89     noWeights = flags and flags.GetBinContent (1)
90     fin.Close ()
91     if types[dataset] != "data" and not noWeights:
92 ahart 1.9 os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", weight))
93     else:
94     os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", 1.0))
95 lantonel 1.1
96     for channel in channels: # loop over final states, which each have their own directory
97    
98     testFile.cd(rootDirectory+"/"+channel)
99 lantonel 1.3
100 lantonel 1.10 for key in gDirectory.GetListOfKeys(): # loop over histograms in the current directory
101     histogramName = key.GetName()
102 lantonel 1.1
103 lantonel 1.10 if re.match ('TH1', key.GetClassName()): # plot a 1-D histogram
104    
105     numBgMCSamples = 0
106     numDataSamples = 0
107     numSignalSamples = 0
108 lantonel 1.1
109 lantonel 1.10 Stack = THStack("stack",histogramName)
110 lantonel 1.1
111 lantonel 1.10 if(intLumi < 1000.):
112     LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
113     else:
114     getcontext().prec = 2
115     LumiInFb = intLumi/1000.
116     LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
117    
118     LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
119     LumiLabel.SetBorderSize(0)
120     LumiLabel.SetFillColor(0)
121     LumiLabel.SetFillStyle(0)
122 lantonel 1.1
123 lantonel 1.15 BgMCLegend = TLegend(0.70,0.65,0.94,0.89, "Data & Bkgd. MC")
124 lantonel 1.10 BgMCLegend.SetBorderSize(0)
125     BgMCLegend.SetFillColor(0)
126     BgMCLegend.SetFillStyle(0)
127     SignalMCLegend = TLegend(0.45,0.65,0.70,0.89,"Signal MC")
128     SignalMCLegend.SetBorderSize(0)
129     SignalMCLegend.SetFillColor(0)
130     SignalMCLegend.SetFillStyle(0)
131    
132     outputFile.cd(rootDirectory+"/"+channel)
133     Canvas = TCanvas(histogramName)
134     BgMCHistograms = []
135     SignalMCHistograms = []
136     DataHistograms = []
137    
138     backgroundIntegral = 0
139     dataIntegral = 0
140     scaleFactor = 1
141    
142     for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
143     dataset_file = "%s/%s.root_tmp" % (condor_dir,sample)
144     inputFile = TFile(dataset_file)
145     Histogram = inputFile.Get(rootDirectory+"/"+channel+"/"+histogramName).Clone()
146     Histogram.SetDirectory(0)
147 ahart 1.21 # if (Histogram.GetName () == "muonD0Beamspot" or Histogram.GetName () == "electronD0Beamspot"):
148     # Histogram.Rebin (8)
149     # Histogram.GetXaxis ().SetRangeUser (-0.04, 0.04)
150     if (Histogram.GetName () == "muonAbsD0Beamspot" or Histogram.GetName () == "electronAbsD0Beamspot"):
151     Histogram.Rebin (4)
152     # Histogram.GetXaxis ().SetRangeUser (0.0, 0.04)
153     # if (Histogram.GetName () == "electronMuonInvMass"):
154     # Histogram.GetXaxis ().SetRangeUser (0, 120)
155 lantonel 1.10 inputFile.Close()
156     xAxisLabel = Histogram.GetXaxis().GetTitle()
157     histoTitle = Histogram.GetTitle()
158 lantonel 1.1
159 lantonel 1.10 if( types[sample] == "bgMC"):
160    
161     numBgMCSamples += 1
162    
163 lantonel 1.16 if(arguments.noStack):
164 lantonel 1.14 Histogram.SetFillStyle(0)
165     Histogram.SetLineColor(colors[sample])
166     Histogram.SetLineWidth(2)
167     BgMCLegend.AddEntry(Histogram,labels[sample],"L")
168     else:
169     Histogram.SetFillStyle(1001)
170     Histogram.SetFillColor(colors[sample])
171     Histogram.SetLineColor(1)
172     Histogram.SetLineWidth(1)
173     BgMCLegend.AddEntry(Histogram,labels[sample],"F")
174 lantonel 1.10 Histogram.SetLineStyle(1)
175    
176     backgroundIntegral += Histogram.Integral()
177    
178     BgMCHistograms.append(Histogram)
179    
180     elif( types[sample] == "signalMC"):
181    
182     numSignalSamples += 1
183 lantonel 1.11
184 lantonel 1.10 Histogram.SetFillStyle(0)
185     Histogram.SetLineColor(colors[sample])
186     Histogram.SetLineStyle(1)
187     Histogram.SetLineWidth(2)
188 lantonel 1.16 if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
189 lantonel 1.11 Histogram.Scale(1./Histogram.Integral())
190 lantonel 1.10 SignalMCLegend.AddEntry(Histogram,labels[sample],"L")
191     SignalMCHistograms.append(Histogram)
192    
193     elif( types[sample] == "data"):
194    
195     numDataSamples += 1
196    
197     Histogram.SetFillStyle(0)
198     Histogram.SetLineColor(colors[sample])
199     Histogram.SetLineStyle(1)
200     Histogram.SetLineWidth(2)
201 lantonel 1.16 if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
202 lantonel 1.11 Histogram.Scale(1./Histogram.Integral())
203 lantonel 1.10
204     dataIntegral += Histogram.Integral()
205    
206     BgMCLegend.AddEntry(Histogram,labels[sample],"LEP")
207     DataHistograms.append(Histogram)
208    
209     if dataIntegral > 0 and backgroundIntegral > 0:
210     scaleFactor = dataIntegral/backgroundIntegral
211     for bgMCHist in BgMCHistograms:
212 lantonel 1.16 if arguments.normalizeToData:
213 lantonel 1.10 bgMCHist.Scale(scaleFactor)
214 lantonel 1.16 if arguments.normalizeToUnitArea and not arguments.noStack and backgroundIntegral > 0:
215 lantonel 1.11 bgMCHist.Scale(1./backgroundIntegral)
216 lantonel 1.16 elif arguments.normalizeToUnitArea and arguments.noStack and bgMCHist.Integral() > 0:
217 lantonel 1.14 bgMCHist.Scale(1./bgMCHist.Integral())
218 lantonel 1.16 if not arguments.noStack:
219 lantonel 1.14 Stack.Add(bgMCHist)
220 lantonel 1.3
221 lantonel 1.14
222 lantonel 1.11
223 lantonel 1.14 finalMax = 0
224 lantonel 1.16 if not arguments.noStack:
225 lantonel 1.14 finalMax = Stack.GetMaximum()
226     else:
227     for bgMCHist in BgMCHistograms:
228     if(bgMCHist.GetMaximum() > finalMax):
229     finalMax = bgMCHist.GetMaximum()
230 lantonel 1.10 for signalMCHist in SignalMCHistograms:
231     if(signalMCHist.GetMaximum() > finalMax):
232     finalMax = signalMCHist.GetMaximum()
233     for dataHist in DataHistograms:
234     if(dataHist.GetMaximum() > finalMax):
235     finalMax = dataHist.GetMaximum()
236 lantonel 1.4
237 lantonel 1.14
238    
239 lantonel 1.10 if len(DataHistograms) is 1:
240     dataIntegral += DataHistograms[0].Integral()
241 lantonel 1.15
242    
243     ### Drawing histograms to canvas
244    
245 lantonel 1.10
246     outputFile.cd(rootDirectory+"/"+channel)
247 lantonel 1.3
248 lantonel 1.16 makeRatioPlots = arguments.makeRatioPlots
249 lantonel 1.15 if numBgMCSamples is 0 or numDataSamples is not 1:
250     makeRatioPlots = False
251     if makeRatioPlots:
252     Canvas.SetFillStyle(0)
253     Canvas.Divide(1,2)
254     Canvas.cd(1)
255     gPad.SetPad(0.01,0.25,0.99,0.99)
256     gPad.SetMargin(0.1,0.05,0.02,0.07)
257     gPad.SetFillStyle(0)
258     gPad.Update()
259     gPad.Draw()
260     Canvas.cd(2)
261     gPad.SetPad(0.01,0.01,0.99,0.25)
262     #gPad.SetMargin(l,r,b,t)
263     gPad.SetMargin(0.1,0.05,0.4,0.02)
264     gPad.SetFillStyle(0)
265     gPad.SetGridy(1)
266     gPad.Update()
267     gPad.Draw()
268    
269     Canvas.cd(1)
270    
271 lantonel 1.10 if(numBgMCSamples is not 0):
272 lantonel 1.16 if not arguments.noStack:
273 lantonel 1.14 Stack.SetTitle(histoTitle)
274     Stack.Draw("HIST")
275 ahart 1.21 # if (histogramName == "muonD0Beamspot" or histogramName == "electronD0Beamspot"):
276     # Stack.GetXaxis ().SetRangeUser (-0.04, 0.04)
277     # if (histogramName == "muonAbsD0Beamspot" or histogramName == "electronAbsD0Beamspot"):
278     # Stack.GetXaxis ().SetRangeUser (0.0, 0.04)
279     # if (histogramName == "electronMuonInvMass"):
280     # Stack.GetXaxis ().SetRangeUser (0, 120)
281     Stack.Draw("HIST")
282 lantonel 1.14 Stack.GetXaxis().SetTitle(xAxisLabel)
283     Stack.SetMaximum(1.1*finalMax)
284 lantonel 1.20 Stack.SetMinimum(0.0001)
285 lantonel 1.15 if makeRatioPlots:
286     Stack.GetHistogram().GetXaxis().SetLabelSize(0)
287 lantonel 1.14 else:
288     BgMCHistograms[0].SetTitle(histoTitle)
289     BgMCHistograms[0].Draw("HIST")
290     BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
291     BgMCHistograms[0].SetMaximum(1.1*finalMax)
292 lantonel 1.20 BgMCHistograms[0].SetMinimum(0.0001)
293 lantonel 1.14 for bgMCHist in BgMCHistograms:
294     bgMCHist.Draw("HIST SAME")
295 lantonel 1.10 for signalMCHist in SignalMCHistograms:
296     signalMCHist.Draw("HIST SAME")
297     for dataHist in DataHistograms:
298     dataHist.Draw("E SAME")
299    
300     elif(numSignalSamples is not 0):
301     SignalMCHistograms[0].SetTitle(histoTitle)
302     SignalMCHistograms[0].Draw("HIST")
303     SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
304     SignalMCHistograms[0].SetMaximum(1.1*finalMax)
305 lantonel 1.20 SignalMCHistograms[0].SetMinimum(0.0001)
306 lantonel 1.10 for signalMCHist in SignalMCHistograms:
307     if(signalMCHist is not SignalMCHistograms[0]):
308     signalMCHist.Draw("HIST SAME")
309     for dataHist in DataHistograms:
310     dataHist.Draw("E SAME")
311 lantonel 1.1
312 lantonel 1.10 elif(numDataSamples is not 0):
313     DataHistograms[0].SetTitle(histoTitle)
314     DataHistograms[0].Draw("E")
315     DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
316     DataHistograms[0].SetMaximum(1.1*finalMax)
317 lantonel 1.20 DataHistograms[0].SetMinimum(0.0001)
318 lantonel 1.10 for dataHist in DataHistograms:
319     if(dataHist is not DataHistograms[0]):
320     dataHist.Draw("E SAME")
321 lantonel 1.3
322 lantonel 1.10
323     if(numBgMCSamples is not 0 or numDataSamples is not 0):
324     BgMCLegend.Draw()
325     if(numSignalSamples is not 0):
326     SignalMCLegend.Draw()
327    
328     LumiLabel.Draw()
329 lantonel 1.16 if arguments.normalizeToData and numBgMCSamples > 0 and numDataSamples > 0:
330 lantonel 1.10 NormLabel = TPaveLabel(0.1,0.75,0.35,0.85,"MC scaled to data","NDC")
331     NormLabel.SetBorderSize(0)
332     NormLabel.SetFillColor(0)
333     NormLabel.SetFillStyle(0)
334     NormLabel.Draw()
335 lantonel 1.16 elif arguments.normalizeToUnitArea:
336 lantonel 1.13 NormLabel = TPaveLabel(0.1,0.75,0.35,0.85,"Scaled to unit area","NDC")
337     NormLabel.SetBorderSize(0)
338     NormLabel.SetFillColor(0)
339     NormLabel.SetFillStyle(0)
340     NormLabel.Draw()
341 lantonel 1.15
342    
343     if makeRatioPlots:
344     Canvas.cd(2)
345     BgSum = Stack.GetStack().Last()
346     Ratio = DataHistograms[0].Clone()
347     Ratio.Add(BgSum,-1)
348     Ratio.Divide(BgSum)
349 lantonel 1.1
350 lantonel 1.15 Ratio.SetTitle("")
351     Ratio.GetXaxis().SetTitle(xAxisLabel)
352     Ratio.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
353     Ratio.GetYaxis().CenterTitle()
354     Ratio.GetYaxis().SetTitleSize(0.1)
355     Ratio.GetYaxis().SetTitleOffset(0.3)
356     Ratio.GetXaxis().SetTitleSize(0.15)
357     Ratio.GetYaxis().SetLabelSize(0.1)
358     Ratio.GetXaxis().SetLabelSize(0.15)
359     Ratio.GetYaxis().SetRangeUser(-1,1)
360     Ratio.GetYaxis().SetNdivisions(205)
361     Ratio.Draw()
362 lantonel 1.4
363 lantonel 1.10 Canvas.Write()
364 lantonel 1.1
365 lantonel 1.4
366 lantonel 1.10 if re.match ('TH2', key.GetClassName()): # plot a 2-D histogram
367 lantonel 1.4
368 lantonel 1.10 numBgMCSamples = 0
369     numDataSamples = 0
370     numSignalSamples = 0
371    
372     if(intLumi < 1000.):
373     LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
374     else:
375     getcontext().prec = 2
376     LumiInFb = intLumi/1000.
377     LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
378    
379     LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
380     LumiLabel.SetBorderSize(0)
381     LumiLabel.SetFillColor(0)
382     LumiLabel.SetFillStyle(0)
383    
384     BgMCLegend = TLegend(0.76,0.65,0.99,0.9, "Data & Bkgd. MC")
385     BgMCLegend.SetBorderSize(0)
386     BgMCLegend.SetFillColor(0)
387     BgMCLegend.SetFillStyle(0)
388     SignalMCLegend = TLegend(0.76,0.135,0.99,0.377,"Signal MC")
389     SignalMCLegend.SetBorderSize(0)
390     SignalMCLegend.SetFillColor(0)
391     SignalMCLegend.SetFillStyle(0)
392    
393     outputFile.cd(rootDirectory+"/"+channel)
394     Canvas = TCanvas(histogramName)
395     Canvas.SetRightMargin(0.2413793);
396     BgMCHistograms = []
397     SignalMCHistograms = []
398     DataHistograms = []
399    
400     for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
401     dataset_file = "%s/%s.root_tmp" % (condor_dir,sample)
402     inputFile = TFile(dataset_file)
403     Histogram = inputFile.Get(rootDirectory+"/"+channel+"/"+histogramName).Clone()
404     Histogram.SetDirectory(0)
405     inputFile.Close()
406     xAxisLabel = Histogram.GetXaxis().GetTitle()
407     yAxisLabel = Histogram.GetYaxis().GetTitle()
408     histoTitle = Histogram.GetTitle()
409    
410     if( types[sample] == "bgMC"):
411 lantonel 1.4
412 lantonel 1.10 numBgMCSamples += 1
413     Histogram.SetMarkerColor(colors[sample])
414     Histogram.SetFillColor(colors[sample])
415     BgMCLegend.AddEntry(Histogram,labels[sample],"F")
416     BgMCHistograms.append(Histogram)
417    
418     elif( types[sample] == "signalMC"):
419 lantonel 1.3
420 lantonel 1.10 numSignalSamples += 1
421     Histogram.SetMarkerColor(colors[sample])
422     Histogram.SetFillColor(colors[sample])
423     SignalMCLegend.AddEntry(Histogram,labels[sample],"F")
424     SignalMCHistograms.append(Histogram)
425    
426     elif( types[sample] == "data"):
427    
428     numDataSamples += 1
429     Histogram.SetMarkerColor(colors[sample])
430     Histogram.SetFillColor(colors[sample])
431     BgMCLegend.AddEntry(Histogram,labels[sample],"F")
432     DataHistograms.append(Histogram)
433    
434    
435     outputFile.cd(rootDirectory+"/"+channel)
436    
437     if(numBgMCSamples is not 0):
438     BgMCHistograms[0].SetTitle(histoTitle)
439     BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
440     BgMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
441     BgMCHistograms[0].Draw()
442     for signalMCHist in SignalMCHistograms:
443     signalMCHist.Draw("SAME")
444     for dataHist in DataHistograms:
445     dataHist.Draw("SAME")
446 lantonel 1.3
447 lantonel 1.10 elif(numSignalSamples is not 0):
448     SignalMCHistograms[0].SetTitle(histoTitle)
449     SignalMCHistograms[0].Draw()
450     SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
451     SignalMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
452     for signalMCHist in SignalMCHistograms:
453     if(signalMCHist is not SignalMCHistograms[0]):
454     signalMCHist.Draw("SAME")
455     for dataHist in DataHistograms:
456     dataHist.Draw("SAME")
457    
458     elif(numDataSamples is not 0):
459     DataHistograms[0].SetTitle(histoTitle)
460     DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
461     DataHistograms[0].GetYaxis().SetTitle(yAxisLabel)
462     DataHistograms[0].Draw()
463     for dataHist in DataHistograms:
464     if(dataHist is not DataHistograms[0]):
465     dataHist.Draw("SAME")
466 lantonel 1.3
467 lantonel 1.8
468 lantonel 1.10 if(numBgMCSamples is not 0 or numDataSamples is not 0):
469     BgMCLegend.Draw()
470     if(numSignalSamples is not 0):
471     SignalMCLegend.Draw()
472    
473     LumiLabel.Draw()
474 lantonel 1.4
475 lantonel 1.10 Canvas.Write()
476    
477 lantonel 1.4
478 lantonel 1.1
479 ahart 1.6 for dataset in processed_datasets:
480     dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
481     os.remove(dataset_file)
482 lantonel 1.1
483     outputFile.Close()