ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.36
Committed: Thu May 30 14:46:27 2013 UTC (11 years, 11 months ago) by lantonel
Content type: text/x-python
Branch: MAIN
CVS Tags: V02-02-00
Changes since 1.35: +501 -439 lines
Log Message:
changed some formatting and added the -p option to save canvases as pdf files

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.31 from math import *
6 lantonel 1.1 from array import *
7 lantonel 1.3 from decimal import *
8 lantonel 1.17 from optparse import OptionParser
9 lantonel 1.2 from OSUT3Analysis.Configuration.configurationOptions import *
10 lantonel 1.7 from OSUT3Analysis.Configuration.processingUtilities import *
11 lantonel 1.1
12 biliu 1.35
13 lantonel 1.36
14     ### parse the command-line options
15    
16 lantonel 1.17 parser = OptionParser()
17 lantonel 1.7 parser = set_commandline_arguments(parser)
18 lantonel 1.17 (arguments, args) = parser.parse_args()
19 lantonel 1.1
20 lantonel 1.16 if arguments.localConfig:
21 lantonel 1.1 sys.path.append(os.getcwd())
22 lantonel 1.16 exec("from " + arguments.localConfig.rstrip('.py') + " import *")
23 lantonel 1.1
24 lantonel 1.15
25 lantonel 1.25 #### deal with conflicting arguments
26 lantonel 1.16 if arguments.normalizeToData and arguments.normalizeToUnitArea:
27 lantonel 1.13 print "Conflicting normalizations requsted, will normalize to unit area"
28 lantonel 1.16 arguments.normalizeToData = False
29     if arguments.normalizeToData and arguments.noStack:
30 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"
31 lantonel 1.16 arguments.normalizeToData = False
32     arguments.normalizeToUnitArea = True
33 lantonel 1.25 if arguments.makeRatioPlots and arguments.makeDiffPlots:
34     print "You have requested both ratio and difference plots. Will make just ratio plots instead"
35     arguments.makeRatioPlots = False
36 lantonel 1.13
37 lantonel 1.36
38     from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TLegendEntry, THStack, TIter, TKey, TPaveLabel, gPad
39    
40    
41     ### setting ROOT options so our plots will look awesome and everyone will love us
42 lantonel 1.1
43     gROOT.SetBatch()
44     gStyle.SetOptStat(0)
45     gStyle.SetCanvasBorderMode(0)
46     gStyle.SetPadBorderMode(0)
47     gStyle.SetPadColor(0)
48     gStyle.SetCanvasColor(0)
49     gStyle.SetTextFont(42)
50 lantonel 1.36 gStyle.SetCanvasDefH(600)
51     gStyle.SetCanvasDefW(600)
52     gStyle.SetCanvasDefX(0)
53     gStyle.SetCanvasDefY(0)
54     gStyle.SetPadTopMargin(0.05)
55     gStyle.SetPadBottomMargin(0.13)
56     gStyle.SetPadLeftMargin(0.15)
57     gStyle.SetPadRightMargin(0.05)
58     gStyle.SetTitleColor(1, "XYZ")
59     gStyle.SetTitleFont(42, "XYZ")
60     gStyle.SetTitleSize(0.05, "XYZ")
61     gStyle.SetTitleXOffset(0.95)
62     gStyle.SetTitleYOffset(1.25)
63     gStyle.SetTextAlign(22)
64     gStyle.SetLabelColor(1, "XYZ")
65     gStyle.SetLabelFont(42, "XYZ")
66     gStyle.SetLabelOffset(0.007, "XYZ")
67     gStyle.SetLabelSize(0.05, "XYZ")
68     gStyle.SetAxisColor(1, "XYZ")
69     gStyle.SetStripDecimals(True)
70     gStyle.SetTickLength(0.03, "XYZ")
71     gStyle.SetNdivisions(510, "XYZ")
72     gStyle.SetPadTickX(1)
73     gStyle.SetPadTickY(1)
74 lantonel 1.3 gROOT.ForceStyle()
75 lantonel 1.1
76 biliu 1.35
77 lantonel 1.36 #set the text for the luminosity label
78     if(intLumi < 1000.):
79     LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
80     LumiText = "L_{int} = " + str.format('{0:.1f}', LumiInPb) + " pb^{-1}"
81     else:
82     LumiInFb = intLumi/1000.
83     LumiText = "L_{int} = " + str.format('{0:.1f}', LumiInFb) + " fb^{-1}"
84 lantonel 1.1
85 lantonel 1.36
86     #bestest place for lumi. label, in top left corner
87     topLeft_x_left = 0.1375839
88     topLeft_x_right = 0.4580537
89     topLeft_y_bottom = 0.8479021
90     topLeft_y_top = 0.9475524
91     topLeft_y_offset = 0.035
92    
93    
94     ##########################################################################################################################################
95     ##########################################################################################################################################
96     ##########################################################################################################################################
97 lantonel 1.1
98 lantonel 1.36 def MakeOneDHist(pathToDir,histogramName):
99 biliu 1.35
100 lantonel 1.36
101     numBgMCSamples = 0
102     numDataSamples = 0
103     numSignalSamples = 0
104    
105     Stack = THStack("stack",histogramName)
106 lantonel 1.1
107 lantonel 1.36 LumiLabel = TPaveLabel(topLeft_x_left,topLeft_y_bottom,topLeft_x_right,topLeft_y_top,LumiText,"NDC")
108     LumiLabel.SetBorderSize(0)
109     LumiLabel.SetTextSize(0.32)
110     LumiLabel.SetFillColor(0)
111     LumiLabel.SetFillStyle(0)
112    
113     NormLabel = TPaveLabel()
114     NormLabel.SetDrawOption("NDC")
115     NormLabel.SetX1NDC(topLeft_x_left)
116     NormLabel.SetX2NDC(topLeft_x_right)
117    
118     NormLabel.SetBorderSize(0)
119     NormLabel.SetTextSize(0.32)
120     NormLabel.SetFillColor(0)
121     NormLabel.SetFillStyle(0)
122    
123     NormText = ""
124     if arguments.normalizeToUnitArea:
125     NormText = "Scaled to unit area"
126     elif arguments.normalizeToData:
127     NormText = "MC scaled to data"
128     NormLabel.SetLabel(NormText)
129 lantonel 1.1
130 lantonel 1.36
131     BgMCLegend = TLegend()
132     BgTitle = BgMCLegend.AddEntry(0, "Data & Bkgd. MC", "H")
133     BgTitle.SetTextAlign(22)
134     BgTitle.SetTextFont(62)
135     BgMCLegend.SetBorderSize(0)
136     BgMCLegend.SetFillColor(0)
137     BgMCLegend.SetFillStyle(0)
138     SignalMCLegend = TLegend()
139     SignalTitle = SignalMCLegend.AddEntry(0, "Signal MC", "H")
140     SignalTitle.SetTextAlign(22)
141     SignalTitle.SetTextFont(62)
142     SignalMCLegend.SetBorderSize(0)
143     SignalMCLegend.SetFillColor(0)
144     SignalMCLegend.SetFillStyle(0)
145    
146     outputFile.cd(pathToDir)
147     Canvas = TCanvas(histogramName)
148     BgMCHistograms = []
149     BgMCLegendEntries = []
150     SignalMCHistograms = []
151     SignalMCLegendEntries = []
152     DataHistograms = []
153     DataLegendEntries = []
154    
155    
156     backgroundIntegral = 0
157     dataIntegral = 0
158     scaleFactor = 1
159    
160     for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
161     dataset_file = "%s/%s.root" % (condor_dir,sample)
162     inputFile = TFile(dataset_file)
163     Histogram = inputFile.Get(pathToDir+"/"+histogramName).Clone()
164     Histogram.SetDirectory(0)
165     inputFile.Close()
166     if arguments.rebinFactor:
167     RebinFactor = int(arguments.rebinFactor)
168     #don't rebin histograms which will have less than 5 bins or any gen-matching histograms
169     if Histogram.GetNbinsX() >= RebinFactor*5 and Histogram.GetTitle.find("GenMatch") is -1:
170     Histogram.Rebin(RebinFactor)
171    
172     xAxisLabel = Histogram.GetXaxis().GetTitle()
173     histoTitle = Histogram.GetTitle()
174    
175     legLabel = labels[sample]
176     if (arguments.printYields):
177     yieldHist = Histogram.Integral()
178     legLabel = legLabel + " (%.1f)" % yieldHist
179 lantonel 1.10
180 lantonel 1.36 if( types[sample] == "bgMC"):
181 lantonel 1.10
182 lantonel 1.36 numBgMCSamples += 1
183     backgroundIntegral += Histogram.Integral()
184 lantonel 1.32
185 lantonel 1.36 Histogram.SetLineStyle(1)
186     if(arguments.noStack):
187     Histogram.SetFillStyle(0)
188     Histogram.SetLineColor(colors[sample])
189     Histogram.SetLineWidth(2)
190     else:
191     Histogram.SetFillStyle(1001)
192     Histogram.SetFillColor(colors[sample])
193     Histogram.SetLineColor(1)
194     Histogram.SetLineWidth(1)
195 wulsin 1.29
196 lantonel 1.36 BgMCLegendEntries.append(legLabel)
197     BgMCHistograms.append(Histogram)
198 wulsin 1.29
199 lantonel 1.10
200 lantonel 1.36 elif( types[sample] == "signalMC"):
201    
202     numSignalSamples += 1
203    
204     Histogram.SetFillStyle(0)
205     Histogram.SetLineColor(colors[sample])
206     Histogram.SetLineStyle(1)
207     Histogram.SetLineWidth(2)
208     if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
209     Histogram.Scale(1./Histogram.Integral())
210    
211     SignalMCLegendEntries.append(legLabel)
212     SignalMCHistograms.append(Histogram)
213 lantonel 1.10
214 lantonel 1.36 elif( types[sample] == "data"):
215 lantonel 1.11
216 lantonel 1.36 numDataSamples += 1
217     dataIntegral += Histogram.Integral()
218    
219     Histogram.SetFillStyle(0)
220     Histogram.SetLineColor(colors[sample])
221     Histogram.SetLineStyle(1)
222     Histogram.SetLineWidth(2)
223     if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
224     Histogram.Scale(1./Histogram.Integral())
225 lantonel 1.32
226 lantonel 1.36 DataLegendEntries.append(legLabel)
227     DataHistograms.append(Histogram)
228 lantonel 1.10
229 lantonel 1.36 #scaling histograms as per user's specifications
230     if dataIntegral > 0 and backgroundIntegral > 0:
231     scaleFactor = dataIntegral/backgroundIntegral
232     for bgMCHist in BgMCHistograms:
233     if arguments.normalizeToData:
234     bgMCHist.Scale(scaleFactor)
235    
236     if arguments.normalizeToUnitArea and not arguments.noStack and backgroundIntegral > 0:
237     bgMCHist.Scale(1./backgroundIntegral)
238     elif arguments.normalizeToUnitArea and arguments.noStack and bgMCHist.Integral() > 0:
239     bgMCHist.Scale(1./bgMCHist.Integral())
240 lantonel 1.10
241 lantonel 1.36 if not arguments.noStack:
242     Stack.Add(bgMCHist)
243 lantonel 1.3
244 lantonel 1.14
245    
246 lantonel 1.36 ### formatting data histograms and adding to legend
247     legendIndex = 0
248     for Histogram in DataHistograms:
249     BgMCLegend.AddEntry(Histogram,DataLegendEntries[legendIndex],"LEP").SetTextFont (42)
250     legendIndex = legendIndex+1
251    
252    
253     ### creating the histogram to represent the statistical errors on the stack
254     if numBgMCSamples is not 0 and not arguments.noStack:
255     ErrorHisto = BgMCHistograms[0].Clone("errors")
256     ErrorHisto.SetFillStyle(3001)
257     ErrorHisto.SetFillColor(13)
258     ErrorHisto.SetLineWidth(0)
259     BgMCLegend.AddEntry(ErrorHisto,"Stat. Errors","F").SetTextFont (42)
260     for Histogram in BgMCHistograms:
261     if Histogram is not BgMCHistograms[0]:
262     ErrorHisto.Add(Histogram)
263    
264    
265     ### formatting bgMC histograms and adding to legend
266     legendIndex = numBgMCSamples-1
267     for Histogram in reversed(BgMCHistograms):
268     if(arguments.noStack):
269     BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"L").SetTextFont (42)
270     else:
271     BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"F").SetTextFont (42)
272     legendIndex = legendIndex-1
273    
274    
275     ### formatting signalMC histograms and adding to legend
276     legendIndex = 0
277     for Histogram in SignalMCHistograms:
278     SignalMCLegend.AddEntry(Histogram,SignalMCLegendEntries[legendIndex],"L").SetTextFont (42)
279     legendIndex = legendIndex+1
280    
281    
282     ### finding the maximum value of anything going on the canvas, so we know how to set the y-axis
283     finalMax = 0
284     if numBgMCSamples is not 0 and not arguments.noStack:
285     finalMax = ErrorHisto.GetMaximum() + ErrorHisto.GetBinError(ErrorHisto.GetMaximumBin())
286     else:
287     for bgMCHist in BgMCHistograms:
288     if(bgMCHist.GetMaximum() > finalMax):
289     finalMax = bgMCHist.GetMaximum()
290     for signalMCHist in SignalMCHistograms:
291     if(signalMCHist.GetMaximum() > finalMax):
292     finalMax = signalMCHist.GetMaximum()
293     for dataHist in DataHistograms:
294     if(dataHist.GetMaximum() > finalMax):
295     finalMax = dataHist.GetMaximum() + dataHist.GetBinError(dataHist.GetMaximumBin())
296     finalMax = 1.15*finalMax
297 lantonel 1.15
298    
299 lantonel 1.36 ### Drawing histograms to canvas
300 lantonel 1.32
301 lantonel 1.36 outputFile.cd(pathToDir)
302    
303     makeRatioPlots = arguments.makeRatioPlots
304     makeDiffPlots = arguments.makeDiffPlots
305 lantonel 1.32
306 lantonel 1.36 if numBgMCSamples is 0 or numDataSamples is not 1:
307     makeRatioPlots = False
308     makeDiffPlots = False
309     if makeRatioPlots or makeDiffPlots:
310     Canvas.SetFillStyle(0)
311     Canvas.Divide(1,2)
312     Canvas.cd(1)
313     gPad.SetPad(0.01,0.25,0.99,0.99)
314     gPad.SetMargin(0.1,0.05,0.02,0.07)
315     gPad.SetFillStyle(0)
316     gPad.Update()
317     gPad.Draw()
318     Canvas.cd(2)
319     gPad.SetPad(0.01,0.01,0.99,0.25)
320     #format: gPad.SetMargin(l,r,b,t)
321     gPad.SetMargin(0.1,0.05,0.4,0.02)
322     gPad.SetFillStyle(0)
323     gPad.SetGridy(1)
324     gPad.Update()
325     gPad.Draw()
326    
327     Canvas.cd(1)
328 lantonel 1.32
329 lantonel 1.36 if numBgMCSamples is not 0: # the first thing to draw to the canvas is a bgMC sample
330 lantonel 1.33
331 lantonel 1.36
332     if not arguments.noStack: # draw unstacked background samples
333     Stack.SetTitle(histoTitle)
334     Stack.Draw("HIST")
335     Stack.GetXaxis().SetTitle(xAxisLabel)
336     Stack.SetMaximum(finalMax)
337     Stack.SetMinimum(0.0001)
338     if makeRatioPlots or makeDiffPlots:
339     Stack.GetHistogram().GetXaxis().SetLabelSize(0)
340     #draw shaded error bands
341     ErrorHisto.Draw("A E2 SAME")
342    
343     else: #draw the stacked backgrounds
344     BgMCHistograms[0].SetTitle(histoTitle)
345     BgMCHistograms[0].Draw("HIST")
346     BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
347     BgMCHistograms[0].SetMaximum(finalMax)
348     BgMCHistograms[0].SetMinimum(0.0001)
349     for bgMCHist in BgMCHistograms:
350     bgMCHist.Draw("A HIST SAME")
351 lantonel 1.33
352 lantonel 1.36 for signalMCHist in SignalMCHistograms:
353     signalMCHist.Draw("A HIST SAME")
354     for dataHist in DataHistograms:
355     dataHist.Draw("A E SAME")
356 lantonel 1.32
357 lantonel 1.36
358     elif numSignalSamples is not 0: # the first thing to draw to the canvas is a signalMC sample
359     SignalMCHistograms[0].SetTitle(histoTitle)
360     SignalMCHistograms[0].Draw("HIST")
361     SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
362     SignalMCHistograms[0].SetMaximum(finalMax)
363     SignalMCHistograms[0].SetMinimum(0.0001)
364    
365     for signalMCHist in SignalMCHistograms:
366     if(signalMCHist is not SignalMCHistograms[0]):
367     signalMCHist.Draw("A HIST SAME")
368     for dataHist in DataHistograms:
369     dataHist.Draw("A E SAME")
370    
371    
372     elif(numDataSamples is not 0): # the first thing to draw to the canvas is a data sample
373     DataHistograms[0].SetTitle(histoTitle)
374     DataHistograms[0].Draw("E")
375     DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
376     DataHistograms[0].SetMaximum(finalMax)
377     DataHistograms[0].SetMinimum(0.0001)
378     for dataHist in DataHistograms:
379     if(dataHist is not DataHistograms[0]):
380     dataHist.Draw("A E SAME")
381 lantonel 1.32
382 lantonel 1.1
383 lantonel 1.3
384 lantonel 1.36 #legend coordinates, empirically determined :-)
385     x_left = 0.6761745
386     x_right = 0.9328859
387     x_width = x_right - x_left
388     y_max = 0.9335664
389     entry_height = 0.05
390    
391     if(numBgMCSamples is not 0 or numDataSamples is not 0): #then draw the data & bgMC legend
392    
393     numExtraEntries = 1 # count the legend title
394     BgMCLegend.SetX1NDC(x_left)
395     if numBgMCSamples > 0:
396     numExtraEntries = numExtraEntries + 1 # count the stat. errors entry
397 lantonel 1.10
398 lantonel 1.36 BgMCLegend.SetY1NDC(y_max-entry_height*(numExtraEntries+numBgMCSamples+numDataSamples))
399     BgMCLegend.SetX2NDC(x_right)
400     BgMCLegend.SetY2NDC(y_max)
401     BgMCLegend.Draw()
402    
403     if(numSignalSamples is not 0): #then draw the signalMC legend to the left of the other one
404     SignalMCLegend.SetX1NDC(x_left-x_width)
405     SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
406     SignalMCLegend.SetX2NDC(x_left)
407     SignalMCLegend.SetY2NDC(y_max)
408     SignalMCLegend.Draw()
409    
410     elif numSignalSamples is not 0: #draw the signalMC legend in the upper right corner
411     SignalMCLegend.SetX1NDC(x_left)
412     SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
413     SignalMCLegend.SetX2NDC(x_right)
414     SignalMCLegend.SetY2NDC(y_max)
415     SignalMCLegend.Draw()
416    
417    
418     if not arguments.normalizeToUnitArea or numDataSamples > 0: #don't draw the lumi label if there's no data and it's scaled to unit area
419     LumiLabel.Draw()
420     if arguments.normalizeToUnitArea or arguments.normalizeToData:
421     #move the normalization label down before drawing if we drew the lumi. label
422     NormLabel.SetY1NDC(topLeft_y_bottom-topLeft_y_offset)
423     NormLabel.SetY2NDC(topLeft_y_top-topLeft_y_offset)
424     NormLabel.Draw()
425    
426     elif arguments.normalizeToUnitArea or arguments.normalizeToData:
427     NormLabel.SetY1NDC(topLeft_y_bottom)
428     NormLabel.SetY2NDC(topLeft_y_top)
429     NormLabel.Draw()
430    
431    
432     if makeRatioPlots or makeDiffPlots:
433     Canvas.cd(2)
434     BgSum = Stack.GetStack().Last()
435     Comparison = DataHistograms[0].Clone()
436     Comparison.Add(BgSum,-1)
437     if not makeDiffPlots:
438     Comparison.Divide(BgSum)
439     Comparison.SetTitle("")
440     Comparison.GetXaxis().SetTitle(xAxisLabel)
441     if makeRatioPlots:
442     Comparison.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
443     elif makeDiffPlots:
444     Comparison.GetYaxis().SetTitle("Data-MC")
445     Comparison.GetYaxis().CenterTitle()
446     Comparison.GetYaxis().SetTitleSize(0.1)
447     Comparison.GetYaxis().SetTitleOffset(0.35)
448     Comparison.GetXaxis().SetTitleSize(0.15)
449     Comparison.GetYaxis().SetLabelSize(0.1)
450     Comparison.GetXaxis().SetLabelSize(0.15)
451     if makeRatioPlots:
452     Comparison.GetYaxis().SetRangeUser(-1.15,1.15)
453     elif makeDiffPlots:
454     YMax = Comparison.GetMaximum()
455     YMin = Comparison.GetMinimum()
456     if YMax <= 0 and YMin <= 0:
457     Comparison.GetYaxis().SetRangeUser(-1.2*YMin,0)
458     elif YMax >= 0 and YMin >= 0:
459     Comparison.GetYaxis().SetRangeUser(0,1.2*YMax)
460     else: #axis crosses y=0
461     if abs(YMax) > abs(YMin):
462     Comparison.GetYaxis().SetRangeUser(-1.2*YMax,1.2*YMax)
463 lantonel 1.32 else:
464 lantonel 1.36 Comparison.GetYaxis().SetRangeUser(-1.2*YMin,1.2*YMin)
465    
466     Comparison.GetYaxis().SetNdivisions(205)
467     Comparison.Draw()
468 lantonel 1.10
469 lantonel 1.36 Canvas.Write()
470     if arguments.savePDFs:
471     pathToDirString = pathToDir.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')
472     Canvas.SaveAs(condor_dir+"/stacked_histograms_pdfs/"+pathToDirString+"/"+histogramName+".pdf")
473 lantonel 1.15
474    
475 lantonel 1.36 ##########################################################################################################################################
476     ##########################################################################################################################################
477     ##########################################################################################################################################
478 lantonel 1.1
479 lantonel 1.36 def MakeTwoDHist(pathToDir,histogramName):
480 lantonel 1.10 numBgMCSamples = 0
481     numDataSamples = 0
482     numSignalSamples = 0
483    
484     LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
485     LumiLabel.SetBorderSize(0)
486     LumiLabel.SetFillColor(0)
487     LumiLabel.SetFillStyle(0)
488    
489 ahart 1.30 BgMCLegend = TLegend(0.76,0.65,0.99,0.9)
490     BgMCLegend.AddEntry (0, "Data & Bkgd. MC", "H").SetTextFont (62)
491 lantonel 1.10 BgMCLegend.SetBorderSize(0)
492     BgMCLegend.SetFillColor(0)
493     BgMCLegend.SetFillStyle(0)
494 ahart 1.30 SignalMCLegend = TLegend(0.76,0.135,0.99,0.377)
495     SignalMCLegend.AddEntry (0, "Signal MC", "H").SetTextFont (62)
496 lantonel 1.10 SignalMCLegend.SetBorderSize(0)
497     SignalMCLegend.SetFillColor(0)
498     SignalMCLegend.SetFillStyle(0)
499    
500 lantonel 1.36 outputFile.cd(pathToDir)
501 lantonel 1.10 Canvas = TCanvas(histogramName)
502     Canvas.SetRightMargin(0.2413793);
503     BgMCHistograms = []
504     SignalMCHistograms = []
505     DataHistograms = []
506    
507     for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
508 ahart 1.28 dataset_file = "%s/%s.root" % (condor_dir,sample)
509 lantonel 1.10 inputFile = TFile(dataset_file)
510 lantonel 1.36 Histogram = inputFile.Get(pathToDir+"/"+histogramName).Clone()
511 lantonel 1.10 Histogram.SetDirectory(0)
512 lantonel 1.25 RebinFactor = int(arguments.rebinFactor)
513     if arguments.rebinFactor and Histogram.GetNbinsX() >= RebinFactor*10 and Histogram.GetNbinsY() >= RebinFactor*10:
514     Histogram.Rebin2D(RebinFactor)
515 lantonel 1.10 inputFile.Close()
516     xAxisLabel = Histogram.GetXaxis().GetTitle()
517     yAxisLabel = Histogram.GetYaxis().GetTitle()
518     histoTitle = Histogram.GetTitle()
519    
520     if( types[sample] == "bgMC"):
521 lantonel 1.4
522 lantonel 1.10 numBgMCSamples += 1
523     Histogram.SetMarkerColor(colors[sample])
524     Histogram.SetFillColor(colors[sample])
525 ahart 1.30 BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
526 lantonel 1.10 BgMCHistograms.append(Histogram)
527    
528     elif( types[sample] == "signalMC"):
529 lantonel 1.3
530 lantonel 1.10 numSignalSamples += 1
531     Histogram.SetMarkerColor(colors[sample])
532     Histogram.SetFillColor(colors[sample])
533 ahart 1.30 SignalMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
534 lantonel 1.10 SignalMCHistograms.append(Histogram)
535    
536     elif( types[sample] == "data"):
537    
538     numDataSamples += 1
539     Histogram.SetMarkerColor(colors[sample])
540 wulsin 1.29 Histogram.SetFillColor(colors[sample])
541 ahart 1.30 BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
542 lantonel 1.10 DataHistograms.append(Histogram)
543    
544    
545 lantonel 1.36 outputFile.cd(pathToDir)
546 lantonel 1.10
547     if(numBgMCSamples is not 0):
548     BgMCHistograms[0].SetTitle(histoTitle)
549     BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
550     BgMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
551     BgMCHistograms[0].Draw()
552     for signalMCHist in SignalMCHistograms:
553     signalMCHist.Draw("SAME")
554     for dataHist in DataHistograms:
555     dataHist.Draw("SAME")
556 lantonel 1.3
557 lantonel 1.10 elif(numSignalSamples is not 0):
558     SignalMCHistograms[0].SetTitle(histoTitle)
559     SignalMCHistograms[0].Draw()
560     SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
561     SignalMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
562     for signalMCHist in SignalMCHistograms:
563     if(signalMCHist is not SignalMCHistograms[0]):
564     signalMCHist.Draw("SAME")
565     for dataHist in DataHistograms:
566     dataHist.Draw("SAME")
567    
568     elif(numDataSamples is not 0):
569     DataHistograms[0].SetTitle(histoTitle)
570     DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
571     DataHistograms[0].GetYaxis().SetTitle(yAxisLabel)
572     DataHistograms[0].Draw()
573     for dataHist in DataHistograms:
574     if(dataHist is not DataHistograms[0]):
575     dataHist.Draw("SAME")
576 lantonel 1.3
577 lantonel 1.8
578 lantonel 1.10 if(numBgMCSamples is not 0 or numDataSamples is not 0):
579     BgMCLegend.Draw()
580     if(numSignalSamples is not 0):
581     SignalMCLegend.Draw()
582 lantonel 1.25 if not arguments.normalizeToUnitArea or numDataSamples > 0:
583     LumiLabel.Draw()
584 lantonel 1.4
585 lantonel 1.10 Canvas.Write()
586 biliu 1.35
587    
588    
589    
590 lantonel 1.36 ##########################################################################################################################################
591     ##########################################################################################################################################
592     ##########################################################################################################################################
593    
594     processed_datasets = []
595    
596     condor_dir = set_condor_output_dir(arguments)
597    
598     #### check which input datasets have valid output files
599     for sample in datasets:
600     fileName = condor_dir + "/" + sample + ".root"
601     if not os.path.exists(fileName):
602     continue
603     testFile = TFile(fileName)
604     if testFile.IsZombie() or not testFile.GetNkeys():
605     continue
606     processed_datasets.append(sample)
607    
608     if len(processed_datasets) is 0:
609     sys.exit("No datasets have been processed")
610    
611    
612     #### make output file
613     outputFileName = "stacked_histograms.root"
614     if arguments.outputFileName:
615     outputFileName = arguments.outputFileName
616    
617     outputFile = TFile(condor_dir + "/" + outputFileName, "RECREATE")
618    
619    
620    
621     #### use the first input file as a template and make stacked versions of all its histograms
622     inputFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
623     inputFile.cd()
624     outputFile.cd()
625    
626     if arguments.savePDFs:
627     os.system("rm -r %s/stacked_histograms_pdfs" % (condor_dir))
628     os.system("mkdir %s/stacked_histograms_pdfs" % (condor_dir))
629    
630    
631     #get root directory in the first layer, generally "OSUAnalysis"
632     for key in inputFile.GetListOfKeys():
633     if (key.GetClassName() != "TDirectoryFile"):
634     continue
635     rootDirectory = key.GetName()
636     outputFile.mkdir(rootDirectory)
637     if arguments.savePDFs:
638     os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,rootDirectory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
639    
640     #cd to root directory and look for histograms
641     inputFile.cd(rootDirectory)
642     for key2 in gDirectory.GetListOfKeys():
643    
644     if re.match ('TH1', key2.GetClassName()): # found a 1-D histogram
645     MakeOneDHist(rootDirectory,key2.GetName())
646     elif re.match ('TH2', key2.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
647     MakeTwoDHist(rootDirectory,key2.GetName())
648    
649     elif (key2.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
650     level2Directory = rootDirectory+"/"+key2.GetName()
651    
652     #make a corresponding directory in the output file
653     outputFile.cd(rootDirectory)
654     gDirectory.mkdir(key2.GetName())
655     if arguments.savePDFs:
656     os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,level2Directory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
657    
658     #####################################################
659     ### This layer is typically the "channels" layer ###
660     #####################################################
661    
662     inputFile.cd(level2Directory)
663     for key3 in gDirectory.GetListOfKeys():
664     if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
665     MakeOneDHist(level2Directory,key3.GetName())
666     elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
667     MakeTwoDHist(level2Directory,key3.GetName())
668    
669     elif (key3.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
670     level3Directory = level2Directory+"/"+key3.GetName()
671    
672     #make a corresponding directory in the output file
673     outputFile.cd(level2Directory)
674     gDirectory.mkdir(key3.GetName())
675     if arguments.savePDFs:
676     os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,level3Directory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
677    
678     #################################################
679     ### This layer is typically the "cuts" layer ###
680     #################################################
681    
682     inputFile.cd(level3Directory)
683     for key3 in gDirectory.GetListOfKeys():
684     if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
685     MakeOneDHist(level3Directory,key3.GetName())
686     elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
687     MakeTwoDHist(level3Directory,key3.GetName())
688    
689 lantonel 1.1
690     outputFile.Close()