ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.50
Committed: Thu Jun 27 08:34:04 2013 UTC (11 years, 10 months ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.49: +1 -1 lines
Log Message:
changed y-axis label slightly

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.46
19     parser.add_option("-f", "--fancy", action="store_true", dest="makeFancy", default=False,
20     help="removes the title and replaces it with the official CMS plot heading")
21 wulsin 1.44 parser.add_option("--ylog", action="store_true", dest="setLogY", default=False,
22     help="Set logarithmic scale on vertical axis on all plots")
23     parser.add_option("--ymin", dest="setYMin",
24     help="Minimum of y axis")
25     parser.add_option("--ymax", dest="setYMax",
26     help="Maximum of y axis")
27    
28 lantonel 1.17 (arguments, args) = parser.parse_args()
29 lantonel 1.1
30 wulsin 1.44
31 lantonel 1.16 if arguments.localConfig:
32 lantonel 1.1 sys.path.append(os.getcwd())
33 lantonel 1.16 exec("from " + arguments.localConfig.rstrip('.py') + " import *")
34 lantonel 1.1
35 lantonel 1.25 #### deal with conflicting arguments
36 lantonel 1.16 if arguments.normalizeToData and arguments.normalizeToUnitArea:
37 lantonel 1.13 print "Conflicting normalizations requsted, will normalize to unit area"
38 lantonel 1.16 arguments.normalizeToData = False
39     if arguments.normalizeToData and arguments.noStack:
40 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"
41 lantonel 1.16 arguments.normalizeToData = False
42     arguments.normalizeToUnitArea = True
43 lantonel 1.25 if arguments.makeRatioPlots and arguments.makeDiffPlots:
44     print "You have requested both ratio and difference plots. Will make just ratio plots instead"
45     arguments.makeRatioPlots = False
46 lantonel 1.13
47 lantonel 1.36
48     from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TLegendEntry, THStack, TIter, TKey, TPaveLabel, gPad
49    
50    
51     ### setting ROOT options so our plots will look awesome and everyone will love us
52 lantonel 1.1
53     gROOT.SetBatch()
54     gStyle.SetOptStat(0)
55     gStyle.SetCanvasBorderMode(0)
56     gStyle.SetPadBorderMode(0)
57     gStyle.SetPadColor(0)
58     gStyle.SetCanvasColor(0)
59     gStyle.SetTextFont(42)
60 lantonel 1.36 gStyle.SetCanvasDefH(600)
61     gStyle.SetCanvasDefW(600)
62     gStyle.SetCanvasDefX(0)
63     gStyle.SetCanvasDefY(0)
64 lantonel 1.47 gStyle.SetPadTopMargin(0.07)
65 lantonel 1.36 gStyle.SetPadBottomMargin(0.13)
66     gStyle.SetPadLeftMargin(0.15)
67     gStyle.SetPadRightMargin(0.05)
68     gStyle.SetTitleColor(1, "XYZ")
69     gStyle.SetTitleFont(42, "XYZ")
70 lantonel 1.47 gStyle.SetTitleSize(0.04, "XYZ")
71     gStyle.SetTitleXOffset(1.1)
72     gStyle.SetTitleYOffset(2)
73 lantonel 1.40 gStyle.SetTextAlign(12)
74 lantonel 1.36 gStyle.SetLabelColor(1, "XYZ")
75     gStyle.SetLabelFont(42, "XYZ")
76     gStyle.SetLabelOffset(0.007, "XYZ")
77 lantonel 1.47 gStyle.SetLabelSize(0.04, "XYZ")
78 lantonel 1.36 gStyle.SetAxisColor(1, "XYZ")
79     gStyle.SetStripDecimals(True)
80     gStyle.SetTickLength(0.03, "XYZ")
81     gStyle.SetNdivisions(510, "XYZ")
82     gStyle.SetPadTickX(1)
83     gStyle.SetPadTickY(1)
84 lantonel 1.3 gROOT.ForceStyle()
85 lantonel 1.1
86 biliu 1.35
87 lantonel 1.36 #set the text for the luminosity label
88     if(intLumi < 1000.):
89     LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
90     LumiText = "L_{int} = " + str.format('{0:.1f}', LumiInPb) + " pb^{-1}"
91     else:
92     LumiInFb = intLumi/1000.
93     LumiText = "L_{int} = " + str.format('{0:.1f}', LumiInFb) + " fb^{-1}"
94 lantonel 1.1
95 lantonel 1.36 #bestest place for lumi. label, in top left corner
96     topLeft_x_left = 0.1375839
97     topLeft_x_right = 0.4580537
98     topLeft_y_bottom = 0.8479021
99     topLeft_y_top = 0.9475524
100     topLeft_y_offset = 0.035
101 lantonel 1.46
102     #set the text for the fancy heading
103     HeaderText = "CMS Preliminary: " + LumiText + " at #sqrt{s} = 8 TeV"
104    
105     #position for header
106     header_x_left = 0.2181208
107     header_x_right = 0.9562937
108     header_y_bottom = 0.9479866
109     header_y_top = 0.9947552
110    
111    
112    
113     ##########################################################################################################################################
114     ##########################################################################################################################################
115     ##########################################################################################################################################
116    
117     def plainTextString(inputString):
118 lantonel 1.49
119 lantonel 1.46 inputString.replace('<','lt')
120     inputString.replace('>','gt')
121     inputString.replace('(','')
122     inputString.replace(')','')
123     inputString.replace('=','eq')
124    
125 lantonel 1.49 # we have to handle whitespace separately
126     # there can be special characters that don't get recognized by just replacing " "
127     stringList = list(inputString)
128     inputString = ""
129     for char in stringList:
130     if char.isspace():
131     inputString = inputString + "_"
132     else:
133     inputString = inputString + char
134    
135 lantonel 1.46 return inputString
136 lantonel 1.36
137     ##########################################################################################################################################
138     ##########################################################################################################################################
139     ##########################################################################################################################################
140 lantonel 1.1
141 lantonel 1.37 # some fancy-ass code from Andrzej Zuranski to merge bins in the ratio plot until the error goes below some threshold
142     def ratioHistogram( dataHist, mcHist, relErrMax=0.10):
143    
144     def groupR(group):
145     Data,MC = [float(sum(hist.GetBinContent(i) for i in group)) for hist in [dataHist,mcHist]]
146     return (Data-MC)/MC if MC else 0
147    
148     def groupErr(group):
149     Data,MC = [float(sum(hist.GetBinContent(i) for i in group)) for hist in [dataHist,mcHist]]
150     dataErr2,mcErr2 = [sum(hist.GetBinError(i)**2 for i in group) for hist in [dataHist,mcHist]]
151     return abs(math.sqrt( (dataErr2+mcErr2)/(Data-MC)**2 + mcErr2/MC**2 ) * (Data-MC)/MC) if Data and MC else 0
152    
153     def regroup(groups):
154     err,iG = max( (groupErr(g),groups.index(g)) for g in groups )
155     if err < relErrMax or len(groups)<3 : return groups
156     iH = max( [iG-1,iG+1], key = lambda i: groupErr(groups[i]) if 0<=i<len(groups) else -1 )
157     iLo,iHi = sorted([iG,iH])
158     return regroup(groups[:iLo] + [groups[iLo]+groups[iHi]] + groups[iHi+1:])
159    
160 lantonel 1.41 #don't rebin the histograms of the number of a given object (except for the pileup ones)
161 wulsin 1.43 if ((dataHist.GetName().find("num") is not -1 and dataHist.GetName().find("Primaryvertexs") is -1) or
162     dataHist.GetName().find("CutFlow") is not -1 or
163     dataHist.GetName().find("GenMatch") is not -1):
164 lantonel 1.41 ratio = dataHist.Clone()
165     ratio.Add(mcHist,-1)
166     ratio.Divide(mcHist)
167     ratio.SetTitle("")
168     else:
169     groups = regroup( [(i,) for i in range(1,1+dataHist.GetNbinsX())] )
170     ratio = TH1F("ratio","",len(groups), array('d', [dataHist.GetBinLowEdge(min(g)) for g in groups ] + [dataHist.GetXaxis().GetBinUpEdge(dataHist.GetNbinsX())]) )
171     for i,g in enumerate(groups) :
172     ratio.SetBinContent(i+1,groupR(g))
173     ratio.SetBinError(i+1,groupErr(g))
174    
175     ratio.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
176 lantonel 1.37 ratio.SetLineColor(1)
177     ratio.SetLineWidth(2)
178     return ratio
179    
180     ##########################################################################################################################################
181     ##########################################################################################################################################
182     ##########################################################################################################################################
183    
184    
185 lantonel 1.36 def MakeOneDHist(pathToDir,histogramName):
186 biliu 1.35
187 lantonel 1.36
188     numBgMCSamples = 0
189     numDataSamples = 0
190     numSignalSamples = 0
191    
192     Stack = THStack("stack",histogramName)
193 lantonel 1.1
194 lantonel 1.46 HeaderLabel = TPaveLabel(header_x_left,header_y_bottom,header_x_right,header_y_top,HeaderText,"NDC")
195     HeaderLabel.SetTextAlign(32)
196     HeaderLabel.SetBorderSize(0)
197     HeaderLabel.SetFillColor(0)
198     HeaderLabel.SetFillStyle(0)
199    
200 lantonel 1.36 LumiLabel = TPaveLabel(topLeft_x_left,topLeft_y_bottom,topLeft_x_right,topLeft_y_top,LumiText,"NDC")
201     LumiLabel.SetBorderSize(0)
202     LumiLabel.SetFillColor(0)
203     LumiLabel.SetFillStyle(0)
204    
205     NormLabel = TPaveLabel()
206     NormLabel.SetDrawOption("NDC")
207     NormLabel.SetX1NDC(topLeft_x_left)
208     NormLabel.SetX2NDC(topLeft_x_right)
209    
210     NormLabel.SetBorderSize(0)
211     NormLabel.SetFillColor(0)
212     NormLabel.SetFillStyle(0)
213    
214     NormText = ""
215     if arguments.normalizeToUnitArea:
216     NormText = "Scaled to unit area"
217     elif arguments.normalizeToData:
218     NormText = "MC scaled to data"
219     NormLabel.SetLabel(NormText)
220 lantonel 1.1
221 lantonel 1.36
222     BgMCLegend = TLegend()
223     BgTitle = BgMCLegend.AddEntry(0, "Data & Bkgd. MC", "H")
224     BgTitle.SetTextAlign(22)
225     BgTitle.SetTextFont(62)
226     BgMCLegend.SetBorderSize(0)
227     BgMCLegend.SetFillColor(0)
228     BgMCLegend.SetFillStyle(0)
229     SignalMCLegend = TLegend()
230     SignalTitle = SignalMCLegend.AddEntry(0, "Signal MC", "H")
231     SignalTitle.SetTextAlign(22)
232     SignalTitle.SetTextFont(62)
233     SignalMCLegend.SetBorderSize(0)
234     SignalMCLegend.SetFillColor(0)
235     SignalMCLegend.SetFillStyle(0)
236    
237     outputFile.cd(pathToDir)
238     Canvas = TCanvas(histogramName)
239     BgMCHistograms = []
240     BgMCLegendEntries = []
241     SignalMCHistograms = []
242     SignalMCLegendEntries = []
243     DataHistograms = []
244     DataLegendEntries = []
245    
246    
247     backgroundIntegral = 0
248     dataIntegral = 0
249     scaleFactor = 1
250    
251     for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
252     dataset_file = "%s/%s.root" % (condor_dir,sample)
253     inputFile = TFile(dataset_file)
254 wulsin 1.48 HistogramObj = inputFile.Get(pathToDir+"/"+histogramName)
255     if not HistogramObj:
256     print "WARNING: Could not find histogram " + pathToDir + "/" + histogramName + " in file " + dataset_file + ". Will skip it and continue."
257     return
258     Histogram = HistogramObj.Clone()
259 lantonel 1.36 Histogram.SetDirectory(0)
260     inputFile.Close()
261     if arguments.rebinFactor:
262     RebinFactor = int(arguments.rebinFactor)
263     #don't rebin histograms which will have less than 5 bins or any gen-matching histograms
264 wulsin 1.38 if Histogram.GetNbinsX() >= RebinFactor*5 and Histogram.GetTitle().find("GenMatch") is -1:
265 lantonel 1.36 Histogram.Rebin(RebinFactor)
266    
267     xAxisLabel = Histogram.GetXaxis().GetTitle()
268 lantonel 1.47 unitIndex = xAxisLabel.find("[")
269     if unitIndex is not -1: #x axis has a unit
270     yAxisLabel = "Entries / " + str(Histogram.GetXaxis().GetBinWidth(1)) + " " + xAxisLabel[unitIndex+1:-1]
271     else:
272 lantonel 1.50 yAxisLabel = "Entries per bin (" + str(Histogram.GetXaxis().GetBinWidth(1)) + " width)"
273 lantonel 1.47
274 lantonel 1.46 if not arguments.makeFancy:
275     histoTitle = Histogram.GetTitle()
276     else:
277     histoTitle = ""
278    
279 lantonel 1.36
280     legLabel = labels[sample]
281     if (arguments.printYields):
282     yieldHist = Histogram.Integral()
283     legLabel = legLabel + " (%.1f)" % yieldHist
284 lantonel 1.10
285 lantonel 1.36 if( types[sample] == "bgMC"):
286 lantonel 1.10
287 lantonel 1.36 numBgMCSamples += 1
288     backgroundIntegral += Histogram.Integral()
289 lantonel 1.32
290 lantonel 1.36 Histogram.SetLineStyle(1)
291     if(arguments.noStack):
292     Histogram.SetFillStyle(0)
293     Histogram.SetLineColor(colors[sample])
294     Histogram.SetLineWidth(2)
295     else:
296     Histogram.SetFillStyle(1001)
297     Histogram.SetFillColor(colors[sample])
298     Histogram.SetLineColor(1)
299     Histogram.SetLineWidth(1)
300 wulsin 1.29
301 lantonel 1.36 BgMCLegendEntries.append(legLabel)
302     BgMCHistograms.append(Histogram)
303 wulsin 1.29
304 lantonel 1.10
305 lantonel 1.36 elif( types[sample] == "signalMC"):
306    
307     numSignalSamples += 1
308    
309     Histogram.SetFillStyle(0)
310     Histogram.SetLineColor(colors[sample])
311     Histogram.SetLineStyle(1)
312     Histogram.SetLineWidth(2)
313     if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
314     Histogram.Scale(1./Histogram.Integral())
315    
316     SignalMCLegendEntries.append(legLabel)
317     SignalMCHistograms.append(Histogram)
318 lantonel 1.10
319 lantonel 1.36 elif( types[sample] == "data"):
320 lantonel 1.11
321 lantonel 1.36 numDataSamples += 1
322     dataIntegral += Histogram.Integral()
323    
324 lantonel 1.47 Histogram.SetMarkerStyle(20)
325     Histogram.SetMarkerSize(0.8)
326 lantonel 1.36 Histogram.SetFillStyle(0)
327     Histogram.SetLineColor(colors[sample])
328     Histogram.SetLineStyle(1)
329     Histogram.SetLineWidth(2)
330     if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
331     Histogram.Scale(1./Histogram.Integral())
332 lantonel 1.32
333 lantonel 1.36 DataLegendEntries.append(legLabel)
334     DataHistograms.append(Histogram)
335 lantonel 1.10
336 lantonel 1.36 #scaling histograms as per user's specifications
337     if dataIntegral > 0 and backgroundIntegral > 0:
338     scaleFactor = dataIntegral/backgroundIntegral
339     for bgMCHist in BgMCHistograms:
340     if arguments.normalizeToData:
341     bgMCHist.Scale(scaleFactor)
342    
343     if arguments.normalizeToUnitArea and not arguments.noStack and backgroundIntegral > 0:
344     bgMCHist.Scale(1./backgroundIntegral)
345     elif arguments.normalizeToUnitArea and arguments.noStack and bgMCHist.Integral() > 0:
346     bgMCHist.Scale(1./bgMCHist.Integral())
347 lantonel 1.10
348 lantonel 1.36 if not arguments.noStack:
349     Stack.Add(bgMCHist)
350 lantonel 1.3
351 lantonel 1.14
352    
353 lantonel 1.36 ### formatting data histograms and adding to legend
354     legendIndex = 0
355     for Histogram in DataHistograms:
356 lantonel 1.41 BgMCLegend.AddEntry(Histogram,DataLegendEntries[legendIndex],"LEP")
357 lantonel 1.36 legendIndex = legendIndex+1
358    
359    
360     ### creating the histogram to represent the statistical errors on the stack
361     if numBgMCSamples is not 0 and not arguments.noStack:
362     ErrorHisto = BgMCHistograms[0].Clone("errors")
363     ErrorHisto.SetFillStyle(3001)
364     ErrorHisto.SetFillColor(13)
365     ErrorHisto.SetLineWidth(0)
366 lantonel 1.41 BgMCLegend.AddEntry(ErrorHisto,"Stat. Errors","F")
367 lantonel 1.36 for Histogram in BgMCHistograms:
368     if Histogram is not BgMCHistograms[0]:
369     ErrorHisto.Add(Histogram)
370    
371    
372     ### formatting bgMC histograms and adding to legend
373     legendIndex = numBgMCSamples-1
374     for Histogram in reversed(BgMCHistograms):
375     if(arguments.noStack):
376 lantonel 1.41 BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"L")
377 lantonel 1.36 else:
378 lantonel 1.41 BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"F")
379 lantonel 1.36 legendIndex = legendIndex-1
380    
381    
382     ### formatting signalMC histograms and adding to legend
383     legendIndex = 0
384     for Histogram in SignalMCHistograms:
385 lantonel 1.41 SignalMCLegend.AddEntry(Histogram,SignalMCLegendEntries[legendIndex],"L")
386 lantonel 1.36 legendIndex = legendIndex+1
387    
388    
389     ### finding the maximum value of anything going on the canvas, so we know how to set the y-axis
390     finalMax = 0
391     if numBgMCSamples is not 0 and not arguments.noStack:
392     finalMax = ErrorHisto.GetMaximum() + ErrorHisto.GetBinError(ErrorHisto.GetMaximumBin())
393     else:
394     for bgMCHist in BgMCHistograms:
395     if(bgMCHist.GetMaximum() > finalMax):
396     finalMax = bgMCHist.GetMaximum()
397     for signalMCHist in SignalMCHistograms:
398     if(signalMCHist.GetMaximum() > finalMax):
399     finalMax = signalMCHist.GetMaximum()
400     for dataHist in DataHistograms:
401     if(dataHist.GetMaximum() > finalMax):
402     finalMax = dataHist.GetMaximum() + dataHist.GetBinError(dataHist.GetMaximumBin())
403     finalMax = 1.15*finalMax
404 wulsin 1.44 if arguments.setYMax:
405     finalMax = float(arguments.setYMax)
406 lantonel 1.15
407    
408 lantonel 1.36 ### Drawing histograms to canvas
409 lantonel 1.32
410 lantonel 1.36 outputFile.cd(pathToDir)
411    
412     makeRatioPlots = arguments.makeRatioPlots
413     makeDiffPlots = arguments.makeDiffPlots
414 lantonel 1.32
415 wulsin 1.45 yAxisMin = 0.0001
416     if arguments.setYMin:
417     yAxisMin = float(arguments.setYMin)
418    
419 lantonel 1.36 if numBgMCSamples is 0 or numDataSamples is not 1:
420     makeRatioPlots = False
421     makeDiffPlots = False
422     if makeRatioPlots or makeDiffPlots:
423     Canvas.SetFillStyle(0)
424     Canvas.Divide(1,2)
425     Canvas.cd(1)
426 lantonel 1.47 gPad.SetPad(0,0.25,1,1)
427     gPad.SetMargin(0.15,0.05,0.01,0.07)
428 lantonel 1.36 gPad.SetFillStyle(0)
429     gPad.Update()
430     gPad.Draw()
431 wulsin 1.44 if arguments.setLogY:
432     gPad.SetLogy()
433 lantonel 1.36 Canvas.cd(2)
434 lantonel 1.47 gPad.SetPad(0,0,1,0.25)
435 lantonel 1.36 #format: gPad.SetMargin(l,r,b,t)
436 lantonel 1.47 gPad.SetMargin(0.15,0.05,0.4,0.01)
437 lantonel 1.36 gPad.SetFillStyle(0)
438     gPad.SetGridy(1)
439     gPad.Update()
440     gPad.Draw()
441    
442     Canvas.cd(1)
443 lantonel 1.32
444 lantonel 1.36 if numBgMCSamples is not 0: # the first thing to draw to the canvas is a bgMC sample
445    
446     if not arguments.noStack: # draw unstacked background samples
447     Stack.SetTitle(histoTitle)
448     Stack.Draw("HIST")
449     Stack.GetXaxis().SetTitle(xAxisLabel)
450 lantonel 1.47 Stack.GetYaxis().SetTitle(yAxisLabel)
451 lantonel 1.36 Stack.SetMaximum(finalMax)
452 wulsin 1.44 Stack.SetMinimum(yAxisMin)
453 lantonel 1.36 if makeRatioPlots or makeDiffPlots:
454     Stack.GetHistogram().GetXaxis().SetLabelSize(0)
455     #draw shaded error bands
456     ErrorHisto.Draw("A E2 SAME")
457    
458     else: #draw the stacked backgrounds
459     BgMCHistograms[0].SetTitle(histoTitle)
460     BgMCHistograms[0].Draw("HIST")
461     BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
462 lantonel 1.47 BgMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
463 lantonel 1.36 BgMCHistograms[0].SetMaximum(finalMax)
464 wulsin 1.44 BgMCHistograms[0].SetMinimum(yAxisMin)
465 lantonel 1.36 for bgMCHist in BgMCHistograms:
466     bgMCHist.Draw("A HIST SAME")
467 lantonel 1.33
468 lantonel 1.36 for signalMCHist in SignalMCHistograms:
469     signalMCHist.Draw("A HIST SAME")
470     for dataHist in DataHistograms:
471 lantonel 1.47 dataHist.Draw("A E X0 SAME")
472 lantonel 1.32
473 lantonel 1.36
474     elif numSignalSamples is not 0: # the first thing to draw to the canvas is a signalMC sample
475     SignalMCHistograms[0].SetTitle(histoTitle)
476     SignalMCHistograms[0].Draw("HIST")
477     SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
478 lantonel 1.47 SignalMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
479 lantonel 1.36 SignalMCHistograms[0].SetMaximum(finalMax)
480 wulsin 1.44 SignalMCHistograms[0].SetMinimum(yAxisMin)
481 lantonel 1.36
482     for signalMCHist in SignalMCHistograms:
483     if(signalMCHist is not SignalMCHistograms[0]):
484     signalMCHist.Draw("A HIST SAME")
485     for dataHist in DataHistograms:
486 lantonel 1.47 dataHist.Draw("A E X0 SAME")
487 lantonel 1.36
488    
489     elif(numDataSamples is not 0): # the first thing to draw to the canvas is a data sample
490     DataHistograms[0].SetTitle(histoTitle)
491     DataHistograms[0].Draw("E")
492     DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
493 lantonel 1.47 DataHistograms[0].GetYaxis().SetTitle(yAxisLabel)
494 lantonel 1.36 DataHistograms[0].SetMaximum(finalMax)
495 wulsin 1.44 DataHistograms[0].SetMinimum(yAxisMin)
496 lantonel 1.36 for dataHist in DataHistograms:
497     if(dataHist is not DataHistograms[0]):
498 lantonel 1.47 dataHist.Draw("A E X0 SAME")
499 lantonel 1.32
500 lantonel 1.1
501 lantonel 1.3
502 lantonel 1.36 #legend coordinates, empirically determined :-)
503     x_left = 0.6761745
504     x_right = 0.9328859
505     x_width = x_right - x_left
506 lantonel 1.47 y_max = 0.9
507 lantonel 1.36 entry_height = 0.05
508    
509     if(numBgMCSamples is not 0 or numDataSamples is not 0): #then draw the data & bgMC legend
510    
511     numExtraEntries = 1 # count the legend title
512     BgMCLegend.SetX1NDC(x_left)
513     if numBgMCSamples > 0:
514     numExtraEntries = numExtraEntries + 1 # count the stat. errors entry
515 lantonel 1.10
516 lantonel 1.36 BgMCLegend.SetY1NDC(y_max-entry_height*(numExtraEntries+numBgMCSamples+numDataSamples))
517     BgMCLegend.SetX2NDC(x_right)
518     BgMCLegend.SetY2NDC(y_max)
519     BgMCLegend.Draw()
520    
521     if(numSignalSamples is not 0): #then draw the signalMC legend to the left of the other one
522     SignalMCLegend.SetX1NDC(x_left-x_width)
523     SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
524     SignalMCLegend.SetX2NDC(x_left)
525     SignalMCLegend.SetY2NDC(y_max)
526     SignalMCLegend.Draw()
527    
528     elif numSignalSamples is not 0: #draw the signalMC legend in the upper right corner
529     SignalMCLegend.SetX1NDC(x_left)
530     SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
531     SignalMCLegend.SetX2NDC(x_right)
532     SignalMCLegend.SetY2NDC(y_max)
533     SignalMCLegend.Draw()
534    
535    
536 lantonel 1.46 # Deciding which text labels to draw and drawing them
537     drawLumiLabel = False
538     drawNormLabel = False
539     offsetNormLabel = False
540     drawHeaderLabel = False
541    
542 lantonel 1.36 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
543 lantonel 1.46 drawLumiLabel = True
544     #move the normalization label down before drawing if we drew the lumi. label
545     offsetNormLabel = True
546     if arguments.normalizeToUnitArea or arguments.normalizeToData:
547     drawNormLabel = True
548     if arguments.makeFancy:
549     drawHeaderLabel = True
550     drawLumiLabel = False
551    
552     #now that flags are set, draw the appropriate labels
553    
554     if drawLumiLabel:
555 lantonel 1.36 LumiLabel.Draw()
556 lantonel 1.46
557     if drawNormLabel:
558     if offsetNormLabel:
559 lantonel 1.36 NormLabel.SetY1NDC(topLeft_y_bottom-topLeft_y_offset)
560     NormLabel.SetY2NDC(topLeft_y_top-topLeft_y_offset)
561 lantonel 1.46 else:
562     NormLabel.SetY1NDC(topLeft_y_bottom)
563     NormLabel.SetY2NDC(topLeft_y_top)
564     NormLabel.Draw()
565    
566     if drawHeaderLabel:
567     HeaderLabel.Draw()
568    
569    
570 lantonel 1.36
571    
572 lantonel 1.46 #drawing the ratio or difference plot if requested
573 lantonel 1.36
574     if makeRatioPlots or makeDiffPlots:
575     Canvas.cd(2)
576     BgSum = Stack.GetStack().Last()
577     if makeRatioPlots:
578 lantonel 1.41 Comparison = ratioHistogram(DataHistograms[0],BgSum)
579 lantonel 1.36 elif makeDiffPlots:
580 wulsin 1.42 Comparison = DataHistograms[0].Clone("diff")
581 lantonel 1.41 Comparison.Add(BgSum,-1)
582     Comparison.SetTitle("")
583 lantonel 1.36 Comparison.GetYaxis().SetTitle("Data-MC")
584 lantonel 1.41 Comparison.GetXaxis().SetTitle(xAxisLabel)
585 lantonel 1.36 Comparison.GetYaxis().CenterTitle()
586     Comparison.GetYaxis().SetTitleSize(0.1)
587 lantonel 1.47 Comparison.GetYaxis().SetTitleOffset(0.5)
588 lantonel 1.36 Comparison.GetXaxis().SetTitleSize(0.15)
589     Comparison.GetYaxis().SetLabelSize(0.1)
590     Comparison.GetXaxis().SetLabelSize(0.15)
591 lantonel 1.41 if makeRatioPlots:
592     RatioYRange = 1.15
593     if arguments.ratioYRange:
594     RatioYRange = float(arguments.ratioYRange)
595 wulsin 1.39 Comparison.GetYaxis().SetRangeUser(-1*RatioYRange, RatioYRange)
596 lantonel 1.36 elif makeDiffPlots:
597     YMax = Comparison.GetMaximum()
598     YMin = Comparison.GetMinimum()
599     if YMax <= 0 and YMin <= 0:
600     Comparison.GetYaxis().SetRangeUser(-1.2*YMin,0)
601     elif YMax >= 0 and YMin >= 0:
602     Comparison.GetYaxis().SetRangeUser(0,1.2*YMax)
603     else: #axis crosses y=0
604     if abs(YMax) > abs(YMin):
605     Comparison.GetYaxis().SetRangeUser(-1.2*YMax,1.2*YMax)
606 lantonel 1.32 else:
607 lantonel 1.36 Comparison.GetYaxis().SetRangeUser(-1.2*YMin,1.2*YMin)
608    
609     Comparison.GetYaxis().SetNdivisions(205)
610     Comparison.Draw()
611 lantonel 1.10
612 lantonel 1.36 Canvas.Write()
613     if arguments.savePDFs:
614 lantonel 1.46 pathToDirString = plainTextString(pathToDir)
615 lantonel 1.36 Canvas.SaveAs(condor_dir+"/stacked_histograms_pdfs/"+pathToDirString+"/"+histogramName+".pdf")
616 lantonel 1.15
617    
618 lantonel 1.36 ##########################################################################################################################################
619     ##########################################################################################################################################
620     ##########################################################################################################################################
621 lantonel 1.1
622 lantonel 1.36 def MakeTwoDHist(pathToDir,histogramName):
623 lantonel 1.10 numBgMCSamples = 0
624     numDataSamples = 0
625     numSignalSamples = 0
626    
627     LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
628     LumiLabel.SetBorderSize(0)
629     LumiLabel.SetFillColor(0)
630     LumiLabel.SetFillStyle(0)
631    
632 ahart 1.30 BgMCLegend = TLegend(0.76,0.65,0.99,0.9)
633     BgMCLegend.AddEntry (0, "Data & Bkgd. MC", "H").SetTextFont (62)
634 lantonel 1.10 BgMCLegend.SetBorderSize(0)
635     BgMCLegend.SetFillColor(0)
636     BgMCLegend.SetFillStyle(0)
637 ahart 1.30 SignalMCLegend = TLegend(0.76,0.135,0.99,0.377)
638     SignalMCLegend.AddEntry (0, "Signal MC", "H").SetTextFont (62)
639 lantonel 1.10 SignalMCLegend.SetBorderSize(0)
640     SignalMCLegend.SetFillColor(0)
641     SignalMCLegend.SetFillStyle(0)
642    
643 lantonel 1.36 outputFile.cd(pathToDir)
644 lantonel 1.10 Canvas = TCanvas(histogramName)
645     Canvas.SetRightMargin(0.2413793);
646     BgMCHistograms = []
647     SignalMCHistograms = []
648     DataHistograms = []
649    
650     for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
651 ahart 1.28 dataset_file = "%s/%s.root" % (condor_dir,sample)
652 lantonel 1.10 inputFile = TFile(dataset_file)
653 lantonel 1.36 Histogram = inputFile.Get(pathToDir+"/"+histogramName).Clone()
654 lantonel 1.10 Histogram.SetDirectory(0)
655 lantonel 1.25 RebinFactor = int(arguments.rebinFactor)
656     if arguments.rebinFactor and Histogram.GetNbinsX() >= RebinFactor*10 and Histogram.GetNbinsY() >= RebinFactor*10:
657     Histogram.Rebin2D(RebinFactor)
658 lantonel 1.10 inputFile.Close()
659     xAxisLabel = Histogram.GetXaxis().GetTitle()
660     yAxisLabel = Histogram.GetYaxis().GetTitle()
661 lantonel 1.46 if not arguments.makeFancy:
662     histoTitle = Histogram.GetTitle()
663     else:
664     histoTitle = ""
665 lantonel 1.10
666     if( types[sample] == "bgMC"):
667 lantonel 1.4
668 lantonel 1.10 numBgMCSamples += 1
669     Histogram.SetMarkerColor(colors[sample])
670     Histogram.SetFillColor(colors[sample])
671 ahart 1.30 BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
672 lantonel 1.10 BgMCHistograms.append(Histogram)
673    
674     elif( types[sample] == "signalMC"):
675 lantonel 1.3
676 lantonel 1.10 numSignalSamples += 1
677     Histogram.SetMarkerColor(colors[sample])
678     Histogram.SetFillColor(colors[sample])
679 ahart 1.30 SignalMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
680 lantonel 1.10 SignalMCHistograms.append(Histogram)
681    
682     elif( types[sample] == "data"):
683    
684     numDataSamples += 1
685     Histogram.SetMarkerColor(colors[sample])
686 wulsin 1.29 Histogram.SetFillColor(colors[sample])
687 ahart 1.30 BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
688 lantonel 1.10 DataHistograms.append(Histogram)
689    
690    
691 lantonel 1.36 outputFile.cd(pathToDir)
692 lantonel 1.10
693     if(numBgMCSamples is not 0):
694     BgMCHistograms[0].SetTitle(histoTitle)
695     BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
696     BgMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
697     BgMCHistograms[0].Draw()
698     for signalMCHist in SignalMCHistograms:
699     signalMCHist.Draw("SAME")
700     for dataHist in DataHistograms:
701     dataHist.Draw("SAME")
702 lantonel 1.3
703 lantonel 1.10 elif(numSignalSamples is not 0):
704     SignalMCHistograms[0].SetTitle(histoTitle)
705     SignalMCHistograms[0].Draw()
706     SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
707     SignalMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
708     for signalMCHist in SignalMCHistograms:
709     if(signalMCHist is not SignalMCHistograms[0]):
710     signalMCHist.Draw("SAME")
711     for dataHist in DataHistograms:
712     dataHist.Draw("SAME")
713    
714     elif(numDataSamples is not 0):
715     DataHistograms[0].SetTitle(histoTitle)
716     DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
717     DataHistograms[0].GetYaxis().SetTitle(yAxisLabel)
718     DataHistograms[0].Draw()
719     for dataHist in DataHistograms:
720     if(dataHist is not DataHistograms[0]):
721     dataHist.Draw("SAME")
722 lantonel 1.3
723 lantonel 1.8
724 lantonel 1.10 if(numBgMCSamples is not 0 or numDataSamples is not 0):
725     BgMCLegend.Draw()
726     if(numSignalSamples is not 0):
727     SignalMCLegend.Draw()
728 lantonel 1.25 if not arguments.normalizeToUnitArea or numDataSamples > 0:
729     LumiLabel.Draw()
730 lantonel 1.4
731 lantonel 1.10 Canvas.Write()
732 biliu 1.35
733    
734    
735    
736 lantonel 1.36 ##########################################################################################################################################
737     ##########################################################################################################################################
738     ##########################################################################################################################################
739    
740     processed_datasets = []
741    
742     condor_dir = set_condor_output_dir(arguments)
743    
744     #### check which input datasets have valid output files
745     for sample in datasets:
746     fileName = condor_dir + "/" + sample + ".root"
747     if not os.path.exists(fileName):
748     continue
749     testFile = TFile(fileName)
750     if testFile.IsZombie() or not testFile.GetNkeys():
751     continue
752     processed_datasets.append(sample)
753    
754     if len(processed_datasets) is 0:
755     sys.exit("No datasets have been processed")
756    
757    
758     #### make output file
759     outputFileName = "stacked_histograms.root"
760     if arguments.outputFileName:
761     outputFileName = arguments.outputFileName
762    
763     outputFile = TFile(condor_dir + "/" + outputFileName, "RECREATE")
764    
765    
766    
767     #### use the first input file as a template and make stacked versions of all its histograms
768     inputFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
769     inputFile.cd()
770     outputFile.cd()
771    
772     if arguments.savePDFs:
773 lantonel 1.46 os.system("rm -rf %s/stacked_histograms_pdfs" % (condor_dir))
774 lantonel 1.36 os.system("mkdir %s/stacked_histograms_pdfs" % (condor_dir))
775    
776    
777     #get root directory in the first layer, generally "OSUAnalysis"
778     for key in inputFile.GetListOfKeys():
779     if (key.GetClassName() != "TDirectoryFile"):
780     continue
781     rootDirectory = key.GetName()
782     outputFile.mkdir(rootDirectory)
783     if arguments.savePDFs:
784 lantonel 1.46 os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,plainTextString(rootDirectory)))
785 lantonel 1.36
786     #cd to root directory and look for histograms
787     inputFile.cd(rootDirectory)
788     for key2 in gDirectory.GetListOfKeys():
789    
790     if re.match ('TH1', key2.GetClassName()): # found a 1-D histogram
791     MakeOneDHist(rootDirectory,key2.GetName())
792     elif re.match ('TH2', key2.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
793     MakeTwoDHist(rootDirectory,key2.GetName())
794    
795     elif (key2.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
796     level2Directory = rootDirectory+"/"+key2.GetName()
797    
798     #make a corresponding directory in the output file
799     outputFile.cd(rootDirectory)
800     gDirectory.mkdir(key2.GetName())
801     if arguments.savePDFs:
802 lantonel 1.46 os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,plainTextString(level2Directory)))
803 lantonel 1.36
804     #####################################################
805     ### This layer is typically the "channels" layer ###
806     #####################################################
807    
808     inputFile.cd(level2Directory)
809     for key3 in gDirectory.GetListOfKeys():
810     if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
811     MakeOneDHist(level2Directory,key3.GetName())
812     elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
813     MakeTwoDHist(level2Directory,key3.GetName())
814    
815     elif (key3.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
816     level3Directory = level2Directory+"/"+key3.GetName()
817    
818     #make a corresponding directory in the output file
819     outputFile.cd(level2Directory)
820     gDirectory.mkdir(key3.GetName())
821     if arguments.savePDFs:
822 lantonel 1.46 os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,plainTextString(level3Directory)))
823 lantonel 1.36
824     #################################################
825     ### This layer is typically the "cuts" layer ###
826     #################################################
827    
828     inputFile.cd(level3Directory)
829     for key3 in gDirectory.GetListOfKeys():
830     if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
831     MakeOneDHist(level3Directory,key3.GetName())
832     elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram
833     MakeTwoDHist(level3Directory,key3.GetName())
834    
835 lantonel 1.1
836     outputFile.Close()