ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.9
Committed: Tue Feb 12 20:59:53 2013 UTC (12 years, 2 months ago) by ahart
Content type: text/x-python
Branch: MAIN
Changes since 1.8: +4 -1 lines
Log Message:
Do not weight data.

File Contents

# User Rev Content
1 lantonel 1.1 #!/usr/bin/env python
2     import sys
3     import os
4 ahart 1.6 import re
5 lantonel 1.1 from array import *
6 lantonel 1.3 from decimal import *
7 lantonel 1.1 from optparse import OptionParser
8 lantonel 1.2 from OSUT3Analysis.Configuration.configurationOptions import *
9 lantonel 1.7 from OSUT3Analysis.Configuration.processingUtilities import *
10 lantonel 1.1
11     parser = OptionParser()
12 lantonel 1.7 parser = set_commandline_arguments(parser)
13 lantonel 1.4
14 lantonel 1.1 (options, args) = parser.parse_args()
15    
16     if options.localConfig:
17     sys.path.append(os.getcwd())
18     exec("from " + options.localConfig.rstrip('.py') + " import *")
19    
20 lantonel 1.7 condor_dir = set_condor_output_dir(options)
21 lantonel 1.1
22    
23     from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TArrow, THStack, TIter, TKey, TPaveLabel
24    
25     gROOT.SetBatch()
26     gStyle.SetOptStat(0)
27     gStyle.SetCanvasBorderMode(0)
28     gStyle.SetPadBorderMode(0)
29     gStyle.SetPadColor(0)
30     gStyle.SetCanvasColor(0)
31     gStyle.SetTextFont(42)
32 lantonel 1.3 gROOT.ForceStyle()
33 lantonel 1.1 outputFile = TFile(condor_dir + "/stacked_histograms.root", "RECREATE")
34    
35     channels = []
36     processed_datasets = []
37    
38     #### check which input datasets have valid output files
39     for sample in datasets:
40     fileName = condor_dir + "/" + sample + ".root"
41     if not os.path.exists(fileName):
42     continue
43     testFile = TFile(fileName)
44     if not (testFile.IsZombie()):
45     processed_datasets.append(sample)
46    
47     if len(processed_datasets) is 0:
48     sys.exit("No datasets have been processed")
49    
50     #### open first input file and re-make its directory structure in the output file
51     testFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
52     testFile.cd()
53     for key in testFile.GetListOfKeys():
54     if (key.GetClassName() != "TDirectoryFile"):
55     continue
56     outputFile.cd()
57     outputFile.mkdir(key.GetName())
58     rootDirectory = key.GetName()
59    
60     testFile.cd(key.GetName())
61     for key2 in gDirectory.GetListOfKeys():
62     if (key2.GetClassName() != "TDirectoryFile"):
63     continue
64     outputFile.cd(key.GetName())
65     gDirectory.mkdir(key2.GetName())
66     channels.append(key2.GetName())
67    
68    
69 ahart 1.6 weight = intLumi / 10000.0
70     for dataset in processed_datasets:
71     dataset_file = "%s/%s.root" % (condor_dir,dataset)
72 ahart 1.9 if types[dataset] != "data":
73     os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", weight))
74     else:
75     os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", 1.0))
76 lantonel 1.1
77     for channel in channels: # loop over final states, which each have their own directory
78    
79     testFile.cd(rootDirectory+"/"+channel)
80     histograms = []
81     for key in gDirectory.GetListOfKeys():
82 ahart 1.6 if re.match ('TH1', key.GetClassName()):
83     histograms.append(key.GetName())
84 lantonel 1.1
85     for histogramName in histograms: # loop over histograms in the current directory
86    
87 lantonel 1.3 numBgMCSamples = 0
88     numDataSamples = 0
89     numSignalSamples = 0
90    
91 lantonel 1.1 Stack = THStack("stack",histogramName)
92    
93     if(intLumi < 1000.):
94     LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
95     else:
96 lantonel 1.3 getcontext().prec = 2
97     LumiInFb = intLumi/1000.
98 lantonel 1.1 LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
99    
100 lantonel 1.3 LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
101     LumiLabel.SetBorderSize(0)
102     LumiLabel.SetFillColor(0)
103     LumiLabel.SetFillStyle(0)
104 lantonel 1.1
105     BgMCLegend = TLegend(0.70,0.65,0.99,0.89, "Data & Bkgd. MC")
106 lantonel 1.3 BgMCLegend.SetBorderSize(0)
107     BgMCLegend.SetFillColor(0)
108     BgMCLegend.SetFillStyle(0)
109 lantonel 1.1 SignalMCLegend = TLegend(0.45,0.65,0.70,0.89,"Signal MC")
110 lantonel 1.3 SignalMCLegend.SetBorderSize(0)
111     SignalMCLegend.SetFillColor(0)
112     SignalMCLegend.SetFillStyle(0)
113 lantonel 1.1
114    
115    
116     outputFile.cd(rootDirectory+"/"+channel)
117     Canvas = TCanvas(histogramName)
118 lantonel 1.4 BgMCHistograms = []
119 lantonel 1.1 SignalMCHistograms = []
120     DataHistograms = []
121 lantonel 1.4
122     backgroundIntegral = 0
123     dataIntegral = 0
124     scaleFactor = 1
125    
126 lantonel 1.1
127     for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
128 ahart 1.6 dataset_file = "%s/%s.root_tmp" % (condor_dir,sample)
129     inputFile = TFile(dataset_file)
130 lantonel 1.1 if(inputFile.IsZombie()):
131     continue
132     Histogram = inputFile.Get(rootDirectory+"/"+channel+"/"+histogramName).Clone()
133     Histogram.SetDirectory(0)
134 ahart 1.6 inputFile.Close()
135 lantonel 1.1 xAxisLabel = Histogram.GetXaxis().GetTitle()
136 lantonel 1.8 histoTitle = Histogram.GetTitle()
137 lantonel 1.1
138     if( types[sample] == "bgMC"):
139 lantonel 1.3
140     numBgMCSamples += 1
141 lantonel 1.1
142     Histogram.SetFillStyle(1001)
143     Histogram.SetFillColor(colors[sample])
144     Histogram.SetLineColor(1)
145     Histogram.SetLineStyle(1)
146     Histogram.SetLineWidth(1)
147 lantonel 1.4
148     backgroundIntegral += Histogram.Integral()
149    
150     BgMCLegend.AddEntry(Histogram,labels[sample],"F")
151     BgMCHistograms.append(Histogram)
152 lantonel 1.1
153     elif( types[sample] == "signalMC"):
154 lantonel 1.3
155     numSignalSamples += 1
156 lantonel 1.1
157     Histogram.SetFillStyle(0)
158     Histogram.SetLineColor(colors[sample])
159     Histogram.SetLineStyle(1)
160     Histogram.SetLineWidth(2)
161    
162     SignalMCLegend.AddEntry(Histogram,labels[sample],"L")
163     SignalMCHistograms.append(Histogram)
164    
165     elif( types[sample] == "data"):
166 lantonel 1.3
167     numDataSamples += 1
168 lantonel 1.1
169     Histogram.SetFillStyle(0)
170     Histogram.SetLineColor(colors[sample])
171     Histogram.SetLineStyle(1)
172     Histogram.SetLineWidth(2)
173 lantonel 1.4
174     dataIntegral += Histogram.Integral()
175 lantonel 1.1
176     BgMCLegend.AddEntry(Histogram,labels[sample],"LEP")
177     DataHistograms.append(Histogram)
178    
179 lantonel 1.5 if dataIntegral > 0 and backgroundIntegral > 0:
180 lantonel 1.4 scaleFactor = dataIntegral/backgroundIntegral
181     for bgMCHist in BgMCHistograms:
182     if options.normalize:
183     bgMCHist.Scale(scaleFactor)
184     Stack.Add(bgMCHist)
185    
186 lantonel 1.1
187     stackMax = Stack.GetMaximum()
188     finalMax = stackMax
189     for signalMCHist in SignalMCHistograms:
190     if(signalMCHist.GetMaximum() > finalMax):
191     finalMax = signalMCHist.GetMaximum()
192     for dataHist in DataHistograms:
193     if(dataHist.GetMaximum() > finalMax):
194     finalMax = dataHist.GetMaximum()
195 lantonel 1.4
196    
197     if len(DataHistograms) is 1:
198     dataIntegral += DataHistograms[0].Integral()
199 lantonel 1.1
200 lantonel 1.3 outputFile.cd(rootDirectory+"/"+channel)
201    
202     if(numBgMCSamples is not 0):
203 lantonel 1.8 Stack.SetTitle(histoTitle)
204 lantonel 1.3 Stack.Draw("HIST")
205 lantonel 1.8 Stack.GetXaxis().SetTitle(xAxisLabel)
206 lantonel 1.3 Stack.SetMaximum(1.1*finalMax)
207     for signalMCHist in SignalMCHistograms:
208     signalMCHist.Draw("HIST SAME")
209     for dataHist in DataHistograms:
210     dataHist.Draw("E SAME")
211    
212     elif(numSignalSamples is not 0):
213 lantonel 1.8 SignalMCHistograms[0].SetTitle(histoTitle)
214 lantonel 1.3 SignalMCHistograms[0].Draw("HIST")
215 lantonel 1.8 SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
216 lantonel 1.3 SignalMCHistograms[0].SetMaximum(1.1*finalMax)
217     for signalMCHist in SignalMCHistograms:
218     if(signalMCHist is not SignalMCHistograms[0]):
219     signalMCHist.Draw("HIST SAME")
220     for dataHist in DataHistograms:
221     dataHist.Draw("E SAME")
222     elif(numDataSamples is not 0):
223 lantonel 1.8 DataHistograms[0].SetTitle(histoTitle)
224 lantonel 1.3 DataHistograms[0].Draw("E")
225 lantonel 1.8 DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
226 lantonel 1.3 DataHistograms[0].SetMaximum(1.1*finalMax)
227     for dataHist in DataHistograms:
228     if(dataHist is not DataHistograms[0]):
229     dataHist.Draw("E SAME")
230    
231 lantonel 1.8
232 lantonel 1.3 if(numBgMCSamples is not 0 or numDataSamples is not 0):
233     BgMCLegend.Draw()
234     if(numSignalSamples is not 0):
235     SignalMCLegend.Draw()
236 lantonel 1.1
237     LumiLabel.Draw()
238 lantonel 1.8 if options.normalize and numBgMCSamples > 0 and numDataSamples > 0:
239 lantonel 1.4 NormLabel = TPaveLabel(0.1,0.75,0.35,0.85,"MC scaled to data","NDC")
240     NormLabel.SetBorderSize(0)
241     NormLabel.SetFillColor(0)
242     NormLabel.SetFillStyle(0)
243     NormLabel.Draw()
244    
245    
246 lantonel 1.1 Canvas.Write()
247    
248 ahart 1.6 for dataset in processed_datasets:
249     dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
250     os.remove(dataset_file)
251 lantonel 1.1
252     outputFile.Close()