ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
Revision: 1.5
Committed: Fri Feb 1 09:05:04 2013 UTC (12 years, 3 months ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
bug fix in setting scale factor

File Contents

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