ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.24
Committed: Wed Mar 27 02:13:38 2013 UTC (12 years, 1 month ago) by biliu
Content type: text/x-python
Branch: MAIN
Changes since 1.23: +5 -4 lines
Log Message:
Now could do -b "Num" to set rebin ngroups and -d to only make the data-mc plots not the ratio.

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