ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.35
Committed: Mon May 27 20:34:12 2013 UTC (11 years, 11 months ago) by biliu
Content type: text/x-python
Branch: MAIN
CVS Tags: V02-01-01
Changes since 1.34: +84 -34 lines
Log Message:
Modified to be compatible with the new feature that we could have plots after applying each cut.

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.17 parser = OptionParser()
14 lantonel 1.7 parser = set_commandline_arguments(parser)
15 lantonel 1.17 (arguments, args) = parser.parse_args()
16 lantonel 1.1
17 lantonel 1.16 if arguments.localConfig:
18 lantonel 1.1 sys.path.append(os.getcwd())
19 lantonel 1.16 exec("from " + arguments.localConfig.rstrip('.py') + " import *")
20 lantonel 1.1
21 lantonel 1.15
22     outputFileName = "stacked_histograms.root"
23 lantonel 1.16 if arguments.outputFileName:
24     outputFileName = arguments.outputFileName
25 lantonel 1.15
26 lantonel 1.16 condor_dir = set_condor_output_dir(arguments)
27 lantonel 1.1
28    
29 lantonel 1.15
30 lantonel 1.25 #### deal with conflicting arguments
31 lantonel 1.16 if arguments.normalizeToData and arguments.normalizeToUnitArea:
32 lantonel 1.13 print "Conflicting normalizations requsted, will normalize to unit area"
33 lantonel 1.16 arguments.normalizeToData = False
34     if arguments.normalizeToData and arguments.noStack:
35 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"
36 lantonel 1.16 arguments.normalizeToData = False
37     arguments.normalizeToUnitArea = True
38 lantonel 1.25 if arguments.makeRatioPlots and arguments.makeDiffPlots:
39     print "You have requested both ratio and difference plots. Will make just ratio plots instead"
40     arguments.makeRatioPlots = False
41 lantonel 1.13
42 lantonel 1.32 from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TLegendEntry, TArrow, THStack, TIter, TKey, TPaveLabel, gPad
43 lantonel 1.1
44     gROOT.SetBatch()
45     gStyle.SetOptStat(0)
46     gStyle.SetCanvasBorderMode(0)
47     gStyle.SetPadBorderMode(0)
48     gStyle.SetPadColor(0)
49     gStyle.SetCanvasColor(0)
50     gStyle.SetTextFont(42)
51 lantonel 1.3 gROOT.ForceStyle()
52 lantonel 1.15 outputFile = TFile(condor_dir + "/" + outputFileName, "RECREATE")
53 lantonel 1.1
54 biliu 1.35
55     channels = {}
56 lantonel 1.1 processed_datasets = []
57     #### check which input datasets have valid output files
58     for sample in datasets:
59     fileName = condor_dir + "/" + sample + ".root"
60     if not os.path.exists(fileName):
61     continue
62     testFile = TFile(fileName)
63 lantonel 1.14 if testFile.IsZombie() or not testFile.GetNkeys():
64     continue
65     processed_datasets.append(sample)
66 lantonel 1.1
67     if len(processed_datasets) is 0:
68     sys.exit("No datasets have been processed")
69    
70     #### open first input file and re-make its directory structure in the output file
71     testFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
72     testFile.cd()
73 biliu 1.35
74     #To find whether there are a third layer in directories.
75     GetPlotsAfterEachCut = True
76    
77 lantonel 1.1 for key in testFile.GetListOfKeys():
78 biliu 1.35 if (key.GetClassName() != "TDirectoryFile"):
79     continue
80     testFile.cd(key.GetName())
81     for key2 in gDirectory.GetListOfKeys():
82     if (key2.GetClassName() != "TDirectoryFile"):
83     continue
84     testFile.cd(key.GetName()+"/"+key2.GetName())
85     for key3 in gDirectory.GetListOfKeys():
86     if (key3.GetClassName() != "TDirectoryFile"):
87     GetPlotsAfterEachCut = False
88    
89     if GetPlotsAfterEachCut:
90     for key in testFile.GetListOfKeys():
91     if (key.GetClassName() != "TDirectoryFile"):
92     continue
93     outputFile.cd()
94     outputFile.mkdir(key.GetName())
95     rootDirectory = key.GetName()
96 lantonel 1.1
97 biliu 1.35 testFile.cd(key.GetName())
98     for key2 in gDirectory.GetListOfKeys():
99     if (key2.GetClassName() != "TDirectoryFile"):
100     continue
101     outputFile.cd(key.GetName())
102     gDirectory.mkdir(key2.GetName())
103     channels[key2.GetName()] = []
104     testFile.cd(key.GetName()+"/"+key2.GetName())
105     for key3 in gDirectory.GetListOfKeys():
106     if (key3.GetClassName() != "TDirectoryFile"):
107     continue
108     outputFile.cd(key.GetName()+"/"+key2.GetName())
109     gDirectory.mkdir(key3.GetName())
110     channels[key2.GetName()].append("/"+key3.GetName())
111     else:
112     for key in testFile.GetListOfKeys():
113     if (key.GetClassName() != "TDirectoryFile"):
114 lantonel 1.1 continue
115 biliu 1.35 outputFile.cd()
116     outputFile.mkdir(key.GetName())
117     rootDirectory = key.GetName()
118    
119     testFile.cd(key.GetName())
120     for key2 in gDirectory.GetListOfKeys():
121     if (key2.GetClassName() != "TDirectoryFile"):
122     continue
123     outputFile.cd(key.GetName())
124     gDirectory.mkdir(key2.GetName())
125     channels[key2.GetName()] = []
126    
127 lantonel 1.31 ## weight = intLumi / 10000.0
128     ## for dataset in processed_datasets:
129     ## dataset_file = "%s/%s.root" % (condor_dir,dataset)
130     ## fin = TFile (dataset_file)
131     ## flags = fin.Get ("flags")
132     ## noWeights = flags and flags.GetBinContent (1)
133     ## fin.Close ()
134    
135     ## if types[dataset] != "data" and not noWeights:
136     ## os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", weight))
137     ## else:
138     ## os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", 1.0))
139 biliu 1.35 def MakeOneDHist(histogramname,channel,cut):
140 lantonel 1.10 numBgMCSamples = 0
141     numDataSamples = 0
142     numSignalSamples = 0
143 lantonel 1.1
144 lantonel 1.10 Stack = THStack("stack",histogramName)
145 lantonel 1.1
146 lantonel 1.10 if(intLumi < 1000.):
147     LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
148     else:
149     getcontext().prec = 2
150     LumiInFb = intLumi/1000.
151     LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
152    
153     LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
154     LumiLabel.SetBorderSize(0)
155     LumiLabel.SetFillColor(0)
156     LumiLabel.SetFillStyle(0)
157 lantonel 1.32
158     BgMCLegend = TLegend()
159     BgTitle = BgMCLegend.AddEntry(0, "Data & Bkgd. MC", "H")
160     BgTitle.SetTextAlign(21)
161     BgTitle.SetTextFont(62)
162 lantonel 1.10 BgMCLegend.SetBorderSize(0)
163     BgMCLegend.SetFillColor(0)
164     BgMCLegend.SetFillStyle(0)
165 lantonel 1.32 SignalMCLegend = TLegend()
166     SignalTitle = SignalMCLegend.AddEntry(0, "Signal MC", "H")
167     SignalTitle.SetTextAlign(21)
168     SignalTitle.SetTextFont(62)
169 lantonel 1.10 SignalMCLegend.SetBorderSize(0)
170     SignalMCLegend.SetFillColor(0)
171     SignalMCLegend.SetFillStyle(0)
172    
173 biliu 1.35 outputFile.cd(rootDirectory+"/"+channel+cut)
174 lantonel 1.10 Canvas = TCanvas(histogramName)
175     BgMCHistograms = []
176 lantonel 1.32 BgMCLegendEntries = []
177 lantonel 1.10 SignalMCHistograms = []
178 lantonel 1.32 SignalMCLegendEntries = []
179 lantonel 1.10 DataHistograms = []
180 lantonel 1.32 DataLegendEntries = []
181    
182 lantonel 1.10
183     backgroundIntegral = 0
184     dataIntegral = 0
185     scaleFactor = 1
186    
187     for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
188 ahart 1.28 dataset_file = "%s/%s.root" % (condor_dir,sample)
189 lantonel 1.10 inputFile = TFile(dataset_file)
190 biliu 1.35 Histogram = inputFile.Get(rootDirectory+"/"+channel+cut+"/"+histogramName).Clone()
191 lantonel 1.10 Histogram.SetDirectory(0)
192 lantonel 1.27 if arguments.rebinFactor:
193     RebinFactor = int(arguments.rebinFactor)
194     if Histogram.GetNbinsX() >= RebinFactor*10:
195     Histogram.Rebin(RebinFactor)
196 lantonel 1.10 inputFile.Close()
197     xAxisLabel = Histogram.GetXaxis().GetTitle()
198     histoTitle = Histogram.GetTitle()
199 wulsin 1.29
200 jbrinson 1.34 legLabel = labels[sample]
201 wulsin 1.29 if (arguments.printYields):
202     yieldHist = Histogram.Integral()
203     legLabel = legLabel + " (%.1f)" % yieldHist
204    
205 lantonel 1.10 if( types[sample] == "bgMC"):
206    
207     numBgMCSamples += 1
208 lantonel 1.32 backgroundIntegral += Histogram.Integral()
209 wulsin 1.29
210 lantonel 1.32 Histogram.SetLineStyle(1)
211 lantonel 1.16 if(arguments.noStack):
212 lantonel 1.14 Histogram.SetFillStyle(0)
213     Histogram.SetLineColor(colors[sample])
214     Histogram.SetLineWidth(2)
215     else:
216     Histogram.SetFillStyle(1001)
217     Histogram.SetFillColor(colors[sample])
218     Histogram.SetLineColor(1)
219     Histogram.SetLineWidth(1)
220 lantonel 1.31
221 jbrinson 1.34 BgMCLegendEntries.append(legLabel)
222 lantonel 1.10 BgMCHistograms.append(Histogram)
223 lantonel 1.31
224 lantonel 1.10
225     elif( types[sample] == "signalMC"):
226    
227     numSignalSamples += 1
228 lantonel 1.11
229 lantonel 1.10 Histogram.SetFillStyle(0)
230     Histogram.SetLineColor(colors[sample])
231     Histogram.SetLineStyle(1)
232     Histogram.SetLineWidth(2)
233 lantonel 1.16 if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
234 lantonel 1.11 Histogram.Scale(1./Histogram.Integral())
235 lantonel 1.32
236 jbrinson 1.34 SignalMCLegendEntries.append(legLabel)
237 lantonel 1.10 SignalMCHistograms.append(Histogram)
238    
239     elif( types[sample] == "data"):
240    
241     numDataSamples += 1
242 lantonel 1.32 dataIntegral += Histogram.Integral()
243    
244 lantonel 1.10 Histogram.SetFillStyle(0)
245     Histogram.SetLineColor(colors[sample])
246     Histogram.SetLineStyle(1)
247     Histogram.SetLineWidth(2)
248 lantonel 1.16 if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
249 lantonel 1.11 Histogram.Scale(1./Histogram.Integral())
250 lantonel 1.32
251 jbrinson 1.34 DataLegendEntries.append(legLabel)
252 lantonel 1.10 DataHistograms.append(Histogram)
253    
254     if dataIntegral > 0 and backgroundIntegral > 0:
255     scaleFactor = dataIntegral/backgroundIntegral
256     for bgMCHist in BgMCHistograms:
257 lantonel 1.16 if arguments.normalizeToData:
258 lantonel 1.10 bgMCHist.Scale(scaleFactor)
259 lantonel 1.16 if arguments.normalizeToUnitArea and not arguments.noStack and backgroundIntegral > 0:
260 lantonel 1.11 bgMCHist.Scale(1./backgroundIntegral)
261 lantonel 1.16 elif arguments.normalizeToUnitArea and arguments.noStack and bgMCHist.Integral() > 0:
262 lantonel 1.14 bgMCHist.Scale(1./bgMCHist.Integral())
263 lantonel 1.16 if not arguments.noStack:
264 lantonel 1.14 Stack.Add(bgMCHist)
265 lantonel 1.3
266 lantonel 1.14
267    
268 lantonel 1.15
269    
270 lantonel 1.32 ### formatting data histograms and adding to legend
271     counter = 0
272     for Histogram in DataHistograms:
273     BgMCLegend.AddEntry(Histogram,DataLegendEntries[counter],"LEP").SetTextFont (42)
274     counter = counter+1
275    
276 lantonel 1.31
277 lantonel 1.32 ### creating the histogram to represent the statistical errors on the stack
278 lantonel 1.33 if numBgMCSamples is not 0 and not arguments.noStack:
279 lantonel 1.31 ErrorHisto = BgMCHistograms[0].Clone("errors")
280     ErrorHisto.SetFillStyle(3001)
281     ErrorHisto.SetFillColor(13)
282     ErrorHisto.SetLineWidth(0)
283 lantonel 1.32 BgMCLegend.AddEntry(ErrorHisto,"Stat. Errors","F").SetTextFont (42)
284 lantonel 1.31 for Histogram in BgMCHistograms:
285     if Histogram is not BgMCHistograms[0]:
286     ErrorHisto.Add(Histogram)
287    
288 lantonel 1.32
289     ### formatting bgMC histograms and adding to legend
290     counter = numBgMCSamples-1
291     for Histogram in reversed(BgMCHistograms):
292     if(arguments.noStack):
293     BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[counter],"L").SetTextFont (42)
294     else:
295     BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[counter],"F").SetTextFont (42)
296     counter = counter-1
297    
298     ### formatting signalMC histograms and adding to legend
299     counter = 0
300     for Histogram in SignalMCHistograms:
301     SignalMCLegend.AddEntry(Histogram,SignalMCLegendEntries[counter],"L").SetTextFont (42)
302     counter = counter+1
303    
304    
305    
306 lantonel 1.33 ### finding the maximum value of anything going on the canvas, and know how to set the y-axis
307     finalMax = 0
308     if numBgMCSamples is not 0 and not arguments.noStack:
309     finalMax = ErrorHisto.GetMaximum() + ErrorHisto.GetBinError(ErrorHisto.GetMaximumBin())
310     else:
311     for bgMCHist in BgMCHistograms:
312     if(bgMCHist.GetMaximum() > finalMax):
313     finalMax = bgMCHist.GetMaximum()
314     for signalMCHist in SignalMCHistograms:
315     if(signalMCHist.GetMaximum() > finalMax):
316     finalMax = signalMCHist.GetMaximum()
317     for dataHist in DataHistograms:
318     if(dataHist.GetMaximum() > finalMax):
319     finalMax = dataHist.GetMaximum() + dataHist.GetBinError(dataHist.GetMaximumBin())
320     finalMax = 1.1*finalMax
321    
322    
323 lantonel 1.32 ### Drawing histograms to canvas
324    
325    
326 biliu 1.35 outputFile.cd(rootDirectory+"/"+channel+cut)
327 lantonel 1.3
328 lantonel 1.16 makeRatioPlots = arguments.makeRatioPlots
329 lantonel 1.25 makeDiffPlots = arguments.makeDiffPlots
330    
331 lantonel 1.15 if numBgMCSamples is 0 or numDataSamples is not 1:
332     makeRatioPlots = False
333 lantonel 1.25 makeDiffPlots = False
334     if makeRatioPlots or makeDiffPlots:
335 lantonel 1.15 Canvas.SetFillStyle(0)
336     Canvas.Divide(1,2)
337     Canvas.cd(1)
338     gPad.SetPad(0.01,0.25,0.99,0.99)
339     gPad.SetMargin(0.1,0.05,0.02,0.07)
340     gPad.SetFillStyle(0)
341     gPad.Update()
342     gPad.Draw()
343     Canvas.cd(2)
344     gPad.SetPad(0.01,0.01,0.99,0.25)
345 lantonel 1.25 #format: gPad.SetMargin(l,r,b,t)
346 lantonel 1.15 gPad.SetMargin(0.1,0.05,0.4,0.02)
347     gPad.SetFillStyle(0)
348     gPad.SetGridy(1)
349     gPad.Update()
350     gPad.Draw()
351    
352     Canvas.cd(1)
353    
354 lantonel 1.31 if numBgMCSamples is not 0:
355 ahart 1.28
356 lantonel 1.16 if not arguments.noStack:
357 lantonel 1.14 Stack.SetTitle(histoTitle)
358     Stack.Draw("HIST")
359     Stack.GetXaxis().SetTitle(xAxisLabel)
360 lantonel 1.33 Stack.SetMaximum(finalMax)
361 lantonel 1.20 Stack.SetMinimum(0.0001)
362 lantonel 1.25 if makeRatioPlots or makeDiffPlots:
363 lantonel 1.15 Stack.GetHistogram().GetXaxis().SetLabelSize(0)
364 lantonel 1.31 #draw shaded error bands
365 lantonel 1.32 ErrorHisto.Draw("A E2 SAME")
366 lantonel 1.31
367    
368 lantonel 1.14 else:
369     BgMCHistograms[0].SetTitle(histoTitle)
370     BgMCHistograms[0].Draw("HIST")
371     BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
372 lantonel 1.33 BgMCHistograms[0].SetMaximum(finalMax)
373 lantonel 1.20 BgMCHistograms[0].SetMinimum(0.0001)
374 lantonel 1.14 for bgMCHist in BgMCHistograms:
375 lantonel 1.32 bgMCHist.Draw("A HIST SAME")
376 lantonel 1.10 for signalMCHist in SignalMCHistograms:
377 lantonel 1.32 signalMCHist.Draw("A HIST SAME")
378 lantonel 1.10 for dataHist in DataHistograms:
379 lantonel 1.32 dataHist.Draw("A E SAME")
380 lantonel 1.10
381 lantonel 1.31 elif numSignalSamples is not 0:
382 lantonel 1.10 SignalMCHistograms[0].SetTitle(histoTitle)
383     SignalMCHistograms[0].Draw("HIST")
384     SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
385 lantonel 1.33 SignalMCHistograms[0].SetMaximum(finalMax)
386 lantonel 1.20 SignalMCHistograms[0].SetMinimum(0.0001)
387 lantonel 1.10 for signalMCHist in SignalMCHistograms:
388     if(signalMCHist is not SignalMCHistograms[0]):
389 lantonel 1.32 signalMCHist.Draw("A HIST SAME")
390 lantonel 1.10 for dataHist in DataHistograms:
391 lantonel 1.32 dataHist.Draw("A E SAME")
392 lantonel 1.1
393 lantonel 1.10 elif(numDataSamples is not 0):
394     DataHistograms[0].SetTitle(histoTitle)
395     DataHistograms[0].Draw("E")
396     DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
397 lantonel 1.33 DataHistograms[0].SetMaximum(finalMax)
398 lantonel 1.20 DataHistograms[0].SetMinimum(0.0001)
399 lantonel 1.10 for dataHist in DataHistograms:
400     if(dataHist is not DataHistograms[0]):
401 lantonel 1.32 dataHist.Draw("A E SAME")
402 lantonel 1.3
403 lantonel 1.10
404     if(numBgMCSamples is not 0 or numDataSamples is not 0):
405 lantonel 1.32 BgMCLegend.SetX1NDC(0.75)
406     if numBgMCSamples is 0:
407     BgMCLegend.SetY1NDC(0.9-0.05*(1+numDataSamples))
408     else:
409     BgMCLegend.SetY1NDC(0.9-0.05*(2+numBgMCSamples+numDataSamples))
410     BgMCLegend.SetX2NDC(0.9)
411     BgMCLegend.SetY2NDC(0.9)
412 lantonel 1.10 BgMCLegend.Draw()
413 lantonel 1.32 if(numSignalSamples is not 0):
414     SignalMCLegend.SetX1NDC(0.6)
415     SignalMCLegend.SetY1NDC(0.9-0.05*(1+numSignalSamples))
416     SignalMCLegend.SetX2NDC(0.75)
417     SignalMCLegend.SetY2NDC(0.9)
418     SignalMCLegend.Draw()
419     elif numSignalSamples is not 0:
420     SignalMCLegend.SetX1NDC(0.75)
421     SignalMCLegend.SetY1NDC(0.9-0.05*(1+numSignalSamples))
422     SignalMCLegend.SetX2NDC(0.9)
423     SignalMCLegend.SetY2NDC(0.9)
424 lantonel 1.10 SignalMCLegend.Draw()
425    
426 lantonel 1.25 if not arguments.normalizeToUnitArea or numDataSamples > 0:
427     LumiLabel.Draw()
428 lantonel 1.16 if arguments.normalizeToData and numBgMCSamples > 0 and numDataSamples > 0:
429 lantonel 1.32 if numSignalSamples is 0:
430     NormLabel = TPaveLabel(0.6,0.85,0.75,0.9,"MC scaled to data","NDC")
431     else:
432     NormLabel = TPaveLabel(0.45,0.85,0.6,0.9,"MC scaled to data","NDC")
433 lantonel 1.10 NormLabel.SetBorderSize(0)
434     NormLabel.SetFillColor(0)
435     NormLabel.SetFillStyle(0)
436     NormLabel.Draw()
437 lantonel 1.16 elif arguments.normalizeToUnitArea:
438 lantonel 1.32 if (numBgMCSamples is not 0 or numDataSamples is not 0) and numSignalSamples is 0:
439     NormLabel = TPaveLabel(0.6,0.85,0.75,0.9,"Scaled to unit area","NDC")
440     else:
441     NormLabel = TPaveLabel(0.45,0.85,0.6,0.9,"Scaled to unit area","NDC")
442 lantonel 1.13 NormLabel.SetBorderSize(0)
443     NormLabel.SetFillColor(0)
444     NormLabel.SetFillStyle(0)
445     NormLabel.Draw()
446 lantonel 1.15
447    
448 lantonel 1.25 if makeRatioPlots or makeDiffPlots:
449 lantonel 1.15 Canvas.cd(2)
450     BgSum = Stack.GetStack().Last()
451 lantonel 1.25 Comparison = DataHistograms[0].Clone()
452     Comparison.Add(BgSum,-1)
453     if not makeDiffPlots:
454     Comparison.Divide(BgSum)
455     Comparison.SetTitle("")
456     Comparison.GetXaxis().SetTitle(xAxisLabel)
457     if makeRatioPlots:
458     Comparison.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
459     elif makeDiffPlots:
460     Comparison.GetYaxis().SetTitle("Data-MC")
461     Comparison.GetYaxis().CenterTitle()
462     Comparison.GetYaxis().SetTitleSize(0.1)
463     Comparison.GetYaxis().SetTitleOffset(0.35)
464     Comparison.GetXaxis().SetTitleSize(0.15)
465     Comparison.GetYaxis().SetLabelSize(0.1)
466     Comparison.GetXaxis().SetLabelSize(0.15)
467     if makeRatioPlots:
468 lantonel 1.33 Comparison.GetYaxis().SetRangeUser(-1.15,1.15)
469 lantonel 1.25 elif makeDiffPlots:
470     YMax = Comparison.GetMaximum()
471     YMin = Comparison.GetMinimum()
472     if YMax <= 0 and YMin <= 0:
473     Comparison.GetYaxis().SetRangeUser(-1.2*YMin,0)
474     elif YMax >= 0 and YMin >= 0:
475     Comparison.GetYaxis().SetRangeUser(0,1.2*YMax)
476     else: #axis crosses y=0
477     if abs(YMax) > abs(YMin):
478     Comparison.GetYaxis().SetRangeUser(-1.2*YMax,1.2*YMax)
479     else:
480     Comparison.GetYaxis().SetRangeUser(-1.2*YMin,1.2*YMin)
481    
482     Comparison.GetYaxis().SetNdivisions(205)
483     Comparison.Draw()
484 lantonel 1.10 Canvas.Write()
485 lantonel 1.1
486 biliu 1.35 def MakeTwoDHist(histogramname,channel,cut):
487 lantonel 1.10 numBgMCSamples = 0
488     numDataSamples = 0
489     numSignalSamples = 0
490    
491     if(intLumi < 1000.):
492     LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
493     else:
494     getcontext().prec = 2
495     LumiInFb = intLumi/1000.
496     LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
497    
498     LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
499     LumiLabel.SetBorderSize(0)
500     LumiLabel.SetFillColor(0)
501     LumiLabel.SetFillStyle(0)
502    
503 ahart 1.30 BgMCLegend = TLegend(0.76,0.65,0.99,0.9)
504     BgMCLegend.AddEntry (0, "Data & Bkgd. MC", "H").SetTextFont (62)
505 lantonel 1.10 BgMCLegend.SetBorderSize(0)
506     BgMCLegend.SetFillColor(0)
507     BgMCLegend.SetFillStyle(0)
508 ahart 1.30 SignalMCLegend = TLegend(0.76,0.135,0.99,0.377)
509     SignalMCLegend.AddEntry (0, "Signal MC", "H").SetTextFont (62)
510 lantonel 1.10 SignalMCLegend.SetBorderSize(0)
511     SignalMCLegend.SetFillColor(0)
512     SignalMCLegend.SetFillStyle(0)
513    
514 biliu 1.35 outputFile.cd(rootDirectory+"/"+channel+cut)
515 lantonel 1.10 Canvas = TCanvas(histogramName)
516     Canvas.SetRightMargin(0.2413793);
517     BgMCHistograms = []
518     SignalMCHistograms = []
519     DataHistograms = []
520    
521     for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
522 ahart 1.28 dataset_file = "%s/%s.root" % (condor_dir,sample)
523 lantonel 1.10 inputFile = TFile(dataset_file)
524 biliu 1.35 Histogram = inputFile.Get(rootDirectory+"/"+channel+"/"+cut+"/"+histogramName).Clone()
525 lantonel 1.10 Histogram.SetDirectory(0)
526 lantonel 1.25 RebinFactor = int(arguments.rebinFactor)
527     if arguments.rebinFactor and Histogram.GetNbinsX() >= RebinFactor*10 and Histogram.GetNbinsY() >= RebinFactor*10:
528     Histogram.Rebin2D(RebinFactor)
529 lantonel 1.10 inputFile.Close()
530     xAxisLabel = Histogram.GetXaxis().GetTitle()
531     yAxisLabel = Histogram.GetYaxis().GetTitle()
532     histoTitle = Histogram.GetTitle()
533    
534     if( types[sample] == "bgMC"):
535 lantonel 1.4
536 lantonel 1.10 numBgMCSamples += 1
537     Histogram.SetMarkerColor(colors[sample])
538     Histogram.SetFillColor(colors[sample])
539 ahart 1.30 BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
540 lantonel 1.10 BgMCHistograms.append(Histogram)
541    
542     elif( types[sample] == "signalMC"):
543 lantonel 1.3
544 lantonel 1.10 numSignalSamples += 1
545     Histogram.SetMarkerColor(colors[sample])
546     Histogram.SetFillColor(colors[sample])
547 ahart 1.30 SignalMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
548 lantonel 1.10 SignalMCHistograms.append(Histogram)
549    
550     elif( types[sample] == "data"):
551    
552     numDataSamples += 1
553     Histogram.SetMarkerColor(colors[sample])
554 wulsin 1.29 Histogram.SetFillColor(colors[sample])
555 ahart 1.30 BgMCLegend.AddEntry(Histogram,labels[sample],"F").SetTextFont (42)
556 lantonel 1.10 DataHistograms.append(Histogram)
557    
558    
559 biliu 1.35 outputFile.cd(rootDirectory+"/"+channel+cut)
560 lantonel 1.10
561     if(numBgMCSamples is not 0):
562     BgMCHistograms[0].SetTitle(histoTitle)
563     BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
564     BgMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
565     BgMCHistograms[0].Draw()
566     for signalMCHist in SignalMCHistograms:
567     signalMCHist.Draw("SAME")
568     for dataHist in DataHistograms:
569     dataHist.Draw("SAME")
570 lantonel 1.3
571 lantonel 1.10 elif(numSignalSamples is not 0):
572     SignalMCHistograms[0].SetTitle(histoTitle)
573     SignalMCHistograms[0].Draw()
574     SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
575     SignalMCHistograms[0].GetYaxis().SetTitle(yAxisLabel)
576     for signalMCHist in SignalMCHistograms:
577     if(signalMCHist is not SignalMCHistograms[0]):
578     signalMCHist.Draw("SAME")
579     for dataHist in DataHistograms:
580     dataHist.Draw("SAME")
581    
582     elif(numDataSamples is not 0):
583     DataHistograms[0].SetTitle(histoTitle)
584     DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
585     DataHistograms[0].GetYaxis().SetTitle(yAxisLabel)
586     DataHistograms[0].Draw()
587     for dataHist in DataHistograms:
588     if(dataHist is not DataHistograms[0]):
589     dataHist.Draw("SAME")
590 lantonel 1.3
591 lantonel 1.8
592 lantonel 1.10 if(numBgMCSamples is not 0 or numDataSamples is not 0):
593     BgMCLegend.Draw()
594     if(numSignalSamples is not 0):
595     SignalMCLegend.Draw()
596 lantonel 1.25 if not arguments.normalizeToUnitArea or numDataSamples > 0:
597     LumiLabel.Draw()
598 lantonel 1.4
599 lantonel 1.10 Canvas.Write()
600 biliu 1.35
601     for channel in channels: # loop over final states, which each have their own directory
602     if GetPlotsAfterEachCut:
603     for cut in channels[channel]:
604     testFile.cd(rootDirectory+"/"+channel+cut)
605    
606     for key in gDirectory.GetListOfKeys(): # loop over histograms in the current directory
607     histogramName = key.GetName()
608 lantonel 1.10
609 biliu 1.35 if re.match ('TH1', key.GetClassName()): # plot a 1-D histogram
610     MakeOneDHist(histogramName,channel,cut)
611     if re.match ('TH2', key.GetClassName()) and arguments.draw2DPlots: # plot a 2-D histogram
612     MakeTwoDHist(histogramName,channel,cut)
613     else:
614     testFile.cd(rootDirectory+"/"+channel)
615    
616     for key in gDirectory.GetListOfKeys(): # loop over histograms in the current directory
617     histogramName = key.GetName()
618    
619     if re.match ('TH1', key.GetClassName()): # plot a 1-D histogram
620     MakeOneDHist(histogramName,channel,"")
621     if re.match ('TH2', key.GetClassName()) and arguments.draw2DPlots: # plot a 2-D histogram
622     MakeTwoDHist(histogramName,channel,"")
623 lantonel 1.4
624 ahart 1.28 #for dataset in processed_datasets:
625     # dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
626     # os.remove(dataset_file)
627 lantonel 1.1
628     outputFile.Close()