ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/fitMCToData.py
Revision: 1.2
Committed: Tue Apr 9 21:42:18 2013 UTC (12 years ago) by ahart
Content type: text/x-python
Branch: MAIN
Changes since 1.1: +34 -31 lines
Log Message:
Perform the full weighting at merging instead of splitting it between the merging and plotting steps.

File Contents

# User Rev Content
1 ahart 1.1 #!/usr/bin/env python
2     import sys
3     import os
4     import re
5     from optparse import OptionParser
6     from array import *
7     from decimal import *
8    
9     from OSUT3Analysis.Configuration.configurationOptions import *
10     from OSUT3Analysis.Configuration.processingUtilities import *
11    
12     ##set default plotting options
13     line_width = 2
14     plotting_options = ""
15    
16     parser = OptionParser()
17     parser = set_commandline_arguments(parser)
18    
19     parser.add_option("--hist", action="store_true", dest="plot_hist", default=False,
20     help="plot as hollow histograms instead of error bar crosses")
21     parser.add_option("--line-width", dest="line_width",
22     help="set line width (default is 2)")
23     parser.add_option("--pdf", action="store_true", dest="plot_savePdf", default=False,
24     help="save plot as pdf in addition")
25    
26     (arguments, args) = parser.parse_args()
27    
28     if arguments.localConfig:
29     sys.path.insert(0,os.getcwd())
30     exec("from " + arguments.localConfig.rstrip('.py') + " import *")
31    
32     outputFileName = "mc_fit_to_data.root"
33     if arguments.outputFileName:
34     outputFileName = arguments.outputFileName
35     pdfFileName = outputFileName[:-5] + ".pdf"
36    
37     condor_dir = set_condor_output_dir(arguments)
38    
39     if arguments.makeRatioPlots and arguments.makeDiffPlots:
40     print "You have requested both ratio and difference plots. Will make just ratio plots instead"
41     arguments.makeRatioPlots = False
42    
43     from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TArrow, THStack, TIter, TKey, TPaveLabel, TPaveText, TF1, gPad
44     sys.argv = []
45     gROOT.SetBatch()
46     gStyle.SetOptStat(0)
47     gStyle.SetCanvasBorderMode(0)
48     gStyle.SetPadBorderMode(0)
49     gStyle.SetPadColor(0)
50     gStyle.SetCanvasColor(0)
51     gStyle.SetTextFont(42)
52     gROOT.ForceStyle()
53    
54     outputFile = TFile(outputFileName, "RECREATE")
55    
56     datasets_needed = []
57     for histogram in input_histograms:
58     for dataset in histogram['datasets']:
59     if dataset not in datasets_needed:
60     datasets_needed.append(dataset)
61     if histogram['target_dataset'] not in datasets_needed:
62     datasets_needed.append(histogram['target_dataset'])
63    
64 ahart 1.2 #weight = intLumi / 10000.0
65     #for dataset in datasets_needed:
66     # dataset_file = "%s/%s.root" % (condor_dir,dataset)
67     # fin = TFile (dataset_file)
68     # flags = fin.Get ("flags")
69     # noWeights = flags and flags.GetBinContent (1)
70     # fin.Close ()
71     #
72     # if types[dataset] != "data" and not noWeights:
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 ahart 1.1
77     for histogram in input_histograms:
78     HistogramsToFit = []
79     HistogramDatasets = []
80     TargetDataset = histogram['target_dataset']
81    
82     Stack = []
83     Stack.append (THStack("stack_before",histogram['name']))
84     Stack.append (THStack("stack_after",histogram['name']))
85    
86     if(intLumi < 1000.):
87     LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
88     else:
89     getcontext().prec = 2
90     LumiInFb = intLumi/1000.
91     LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
92    
93     LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
94     LumiLabel.SetBorderSize(0)
95     LumiLabel.SetFillColor(0)
96     LumiLabel.SetFillStyle(0)
97    
98 ahart 1.2 Label = TPaveText(0.39, 0.7, 0.59, 0.9,"NDC")
99 ahart 1.1 Label.SetBorderSize(0)
100     Label.SetFillColor(0)
101     Label.SetFillStyle(0)
102     Label.SetTextAlign(12)
103    
104     BgMCLegend = TLegend(0.70,0.65,0.94,0.89, "Data & Bkgd. MC")
105     BgMCLegend.SetBorderSize(0)
106     BgMCLegend.SetFillColor(0)
107     BgMCLegend.SetFillStyle(0)
108    
109     scaleFactor = 1
110    
111     numBgMCSamples = 0
112     numDataSamples = 0
113     numSignalSamples = 0
114    
115 ahart 1.2 fileName = condor_dir + "/" + histogram['target_dataset'] + ".root"
116 ahart 1.1 if not os.path.exists(fileName):
117     continue
118     inputFile = TFile(fileName)
119     if inputFile.IsZombie() or not inputFile.GetNkeys():
120     continue
121    
122     Target = inputFile.Get("OSUAnalysis/"+histogram['channel']+"/"+histogram['name']).Clone()
123     Target.SetDirectory(0)
124     inputFile.Close()
125    
126     numDataSamples += 1
127    
128     Target.SetFillStyle(0)
129     Target.SetLineColor(colors[TargetDataset])
130     Target.SetLineStyle(1)
131     Target.SetLineWidth(2)
132    
133     BgMCLegend.AddEntry(Target,labels[TargetDataset],"LEP")
134    
135     xAxisLabel = Target.GetXaxis().GetTitle()
136     histoTitle = Target.GetTitle()
137    
138     if not outputFile.Get ("OSUAnalysis"):
139     outputFile.mkdir ("OSUAnalysis")
140     if not outputFile.Get ("OSUAnalysis/" + histogram['channel']):
141     outputFile.Get ("OSUAnalysis").mkdir (histogram['channel'])
142    
143     for dataset in histogram['datasets']:
144 ahart 1.2 fileName = condor_dir + "/" + dataset + ".root"
145 ahart 1.1 if not os.path.exists(fileName):
146     continue
147     inputFile = TFile(fileName)
148     if inputFile.IsZombie() or not inputFile.GetNkeys():
149     continue
150    
151     Histogram = inputFile.Get("OSUAnalysis/"+histogram['channel']+"/"+histogram['name']).Clone()
152     Histogram.SetDirectory(0)
153     inputFile.Close()
154    
155     if arguments.rebinFactor:
156     RebinFactor = int(arguments.rebinFactor)
157     if Histogram.GetNbinsX() >= RebinFactor*10:
158     Histogram.Rebin(RebinFactor)
159    
160     numBgMCSamples += 1
161    
162     if(arguments.noStack):
163     Histogram.SetFillStyle(0)
164     Histogram.SetLineColor(colors[dataset])
165     Histogram.SetLineWidth(2)
166     BgMCLegend.AddEntry(Histogram,labels[dataset],"L")
167     else:
168     Histogram.SetFillStyle(1001)
169     Histogram.SetFillColor(colors[dataset])
170     Histogram.SetLineColor(1)
171     Histogram.SetLineWidth(1)
172     BgMCLegend.AddEntry(Histogram,labels[dataset],"F")
173     Histogram.SetLineStyle(1)
174    
175     HistogramsToFit.append(Histogram)
176     HistogramDatasets.append(dataset)
177    
178     def fitf (x, par):
179     xBin = HistogramsToFit[0].FindBin (x[0])
180     value = 0.0
181    
182     for i in range (0, len (HistogramsToFit)):
183     value += par[i] * HistogramsToFit[i].GetBinContent (xBin)
184    
185     return value
186    
187     lowerLimit = Target.GetBinLowEdge (1)
188     upperLimit = Target.GetBinLowEdge (Target.GetNbinsX ()) + Target.GetBinWidth (Target.GetNbinsX ())
189     if 'lowerLimit' in histogram:
190     lowerLimit = histogram['lowerLimit']
191     if 'upperLimit' in histogram:
192     upperLimit = histogram['upperLimit']
193     func = TF1 ("fit", fitf, lowerLimit, upperLimit, len (HistogramsToFit))
194    
195     for i in range (0, len (HistogramsToFit)):
196     func.SetParameter (i, 1.0)
197     func.SetParName (i, labels[HistogramDatasets[i]])
198    
199     for i in range (0, histogram['iterations'] - 1):
200     print "Iteration " + str (i + 1) + "..."
201     Target.Fit ("fit", "QEMR0")
202     Target.Fit ("fit", "VEMR0")
203    
204     func.SetLineWidth (line_width)
205     func.SetLineColor (632)
206    
207     Target.SetMarkerStyle (20)
208     Target.SetMarkerColor (1)
209     Target.SetLineColor (1)
210    
211 ahart 1.2 finalMax = 0
212     if not arguments.noStack:
213     for bgMCHist in HistogramsToFit:
214     finalMax += bgMCHist.GetMaximum()
215     else:
216     for bgMCHist in HistogramsToFit:
217     if(bgMCHist.GetMaximum() > finalMax):
218     finalMax = bgMCHist.GetMaximum()
219     if(Target.GetMaximum() > finalMax):
220     finalMax = Target.GetMaximum()
221    
222     Target.SetMaximum(1.1*finalMax)
223     Target.SetMinimum(0.0001)
224    
225 ahart 1.1 Canvas = TCanvas(histogram['name'])
226     Canvas.cd (1)
227     Target.Draw ()
228     func.Draw (plotting_options + "same")
229    
230     outputFile.cd ("OSUAnalysis/" + histogram['channel'])
231     Canvas.Write ()
232     if arguments.plot_savePdf:
233     if histogram == input_histograms[0]:
234     Canvas.Print (pdfFileName + "(", "pdf")
235     else:
236     Canvas.Print (pdfFileName, "pdf")
237     Target.SetStats (0)
238    
239     for i in range (0, 2):
240    
241     if i == 1:
242     for j in range (0, len (HistogramsToFit)):
243     HistogramsToFit[j].Scale (func.GetParameter (j))
244    
245     for bgMCHist in HistogramsToFit:
246     if not arguments.noStack:
247     Stack[i].Add(bgMCHist)
248    
249     makeRatioPlots = arguments.makeRatioPlots
250     makeDiffPlots = arguments.makeDiffPlots
251     if i == 0:
252     Canvas = TCanvas(histogram['name'] + "_Before")
253     if i == 1:
254     Canvas = TCanvas(histogram['name'] + "_After")
255     if makeRatioPlots or makeDiffPlots:
256     Canvas.SetFillStyle(0)
257     Canvas.Divide(1,2)
258     Canvas.cd(1)
259     gPad.SetPad(0.01,0.25,0.99,0.99)
260     gPad.SetMargin(0.1,0.05,0.02,0.07)
261     gPad.SetFillStyle(0)
262     gPad.Update()
263     gPad.Draw()
264     Canvas.cd(2)
265     gPad.SetPad(0.01,0.01,0.99,0.25)
266     #format: gPad.SetMargin(l,r,b,t)
267     gPad.SetMargin(0.1,0.05,0.4,0.02)
268     gPad.SetFillStyle(0)
269     gPad.SetGridy(1)
270     gPad.Update()
271     gPad.Draw()
272    
273     Canvas.cd(1)
274    
275     if not arguments.noStack:
276     Stack[i].SetTitle(histoTitle)
277     Stack[i].Draw("HIST")
278     Stack[i].GetXaxis().SetTitle(xAxisLabel)
279     Stack[i].SetMaximum(1.1*finalMax)
280     Stack[i].SetMinimum(0.0001)
281     if makeRatioPlots or makeDiffPlots:
282     Stack[i].GetHistogram().GetXaxis().SetLabelSize(0)
283     else:
284     HistogramsToFit[0].SetTitle(histoTitle)
285     HistogramsToFit[0].Draw("HIST")
286     HistogramsToFit[0].GetXaxis().SetTitle(xAxisLabel)
287     HistogramsToFit[0].SetMaximum(1.1*finalMax)
288     HistogramsToFit[0].SetMinimum(0.0001)
289     for bgMCHist in HistogramsToFit:
290     bgMCHist.Draw("HIST SAME")
291    
292     dataYield = Target.Integral (1, Target.GetNbinsX ())
293     mcYield = 0.0
294     for bgMCHist in HistogramsToFit:
295     mcYield += bgMCHist.Integral (1, bgMCHist.GetNbinsX ())
296     Label.Clear ()
297     if i == 0:
298     Label.AddText ("Before Fit to Data")
299     if i == 1:
300     Label.AddText ("After Fit to Data")
301 ahart 1.2 Label.AddText ("data yield: " + '%.1f' % dataYield)
302     Label.AddText ("MC yield: " + '%.1f' % mcYield)
303 ahart 1.1
304     Target.Draw("E SAME")
305     BgMCLegend.Draw()
306     LumiLabel.Draw()
307     Label.Draw()
308    
309     if makeRatioPlots or makeDiffPlots:
310     Canvas.cd(2)
311     BgSum = Stack[i].GetStack().Last()
312     Comparison = Target.Clone()
313     Comparison.Add(BgSum,-1)
314     if not makeDiffPlots:
315     Comparison.Divide(BgSum)
316     Comparison.SetTitle("")
317     Comparison.GetXaxis().SetTitle(xAxisLabel)
318     if makeRatioPlots:
319     Comparison.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
320     elif makeDiffPlots:
321     Comparison.GetYaxis().SetTitle("Data-MC")
322     Comparison.GetYaxis().CenterTitle()
323     Comparison.GetYaxis().SetTitleSize(0.1)
324     Comparison.GetYaxis().SetTitleOffset(0.35)
325     Comparison.GetXaxis().SetTitleSize(0.15)
326     Comparison.GetYaxis().SetLabelSize(0.1)
327     Comparison.GetXaxis().SetLabelSize(0.15)
328     if makeRatioPlots:
329     Comparison.GetYaxis().SetRangeUser(-1,1)
330     elif makeDiffPlots:
331     YMax = Comparison.GetMaximum()
332     YMin = Comparison.GetMinimum()
333     if YMax <= 0 and YMin <= 0:
334     Comparison.GetYaxis().SetRangeUser(-1.2*YMin,0)
335     elif YMax >= 0 and YMin >= 0:
336     Comparison.GetYaxis().SetRangeUser(0,1.2*YMax)
337     else: #axis crosses y=0
338     if abs(YMax) > abs(YMin):
339     Comparison.GetYaxis().SetRangeUser(-1.2*YMax,1.2*YMax)
340     else:
341     Comparison.GetYaxis().SetRangeUser(-1.2*YMin,1.2*YMin)
342    
343     Comparison.GetYaxis().SetNdivisions(205)
344     Comparison.Draw()
345    
346     outputFile.cd ("OSUAnalysis/" + histogram['channel'])
347     if i == 0:
348     Canvas.Write (histogram['name'] + "_Before")
349     if arguments.plot_savePdf:
350     Canvas.Print (pdfFileName, "pdf")
351     if i == 1:
352     Canvas.Write (histogram['name'] + "_After")
353     if arguments.plot_savePdf:
354     if histogram == input_histograms[-1]:
355     Canvas.Print (pdfFileName + ")", "pdf")
356     else:
357     Canvas.Print (pdfFileName, "pdf")
358    
359     outputFile.Close ()
360    
361 ahart 1.2 #for dataset in datasets_needed:
362     # dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
363     # os.remove(dataset_file)