ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/fitMCToData.py
Revision: 1.1
Committed: Sun Apr 7 00:57:26 2013 UTC (12 years ago) by ahart
Content type: text/x-python
Branch: MAIN
Log Message:
First commit of script for fitting MC normalizations to data.

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     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    
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     Label = TPaveText(0.34, 0.7, 0.64, 0.9,"NDC")
99     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     fileName = condor_dir + "/" + histogram['target_dataset'] + ".root_tmp"
116     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     fileName = condor_dir + "/" + dataset + ".root_tmp"
145     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.SetParLimits (i, 0.0, 10.0)
198     func.SetParName (i, labels[HistogramDatasets[i]])
199    
200     for i in range (0, histogram['iterations'] - 1):
201     print "Iteration " + str (i + 1) + "..."
202     Target.Fit ("fit", "QEMR0")
203     Target.Fit ("fit", "VEMR0")
204    
205     func.SetLineWidth (line_width)
206     func.SetLineColor (632)
207    
208     Target.SetMarkerStyle (20)
209     Target.SetMarkerColor (1)
210     Target.SetLineColor (1)
211    
212     Canvas = TCanvas(histogram['name'])
213     Canvas.cd (1)
214     Target.Draw ()
215     func.Draw (plotting_options + "same")
216    
217     outputFile.cd ("OSUAnalysis/" + histogram['channel'])
218     Canvas.Write ()
219     if arguments.plot_savePdf:
220     if histogram == input_histograms[0]:
221     Canvas.Print (pdfFileName + "(", "pdf")
222     else:
223     Canvas.Print (pdfFileName, "pdf")
224     Target.SetStats (0)
225    
226     for i in range (0, 2):
227    
228     if i == 1:
229     for j in range (0, len (HistogramsToFit)):
230     HistogramsToFit[j].Scale (func.GetParameter (j))
231    
232     for bgMCHist in HistogramsToFit:
233     if not arguments.noStack:
234     Stack[i].Add(bgMCHist)
235    
236     finalMax = 0
237     if not arguments.noStack:
238     finalMax = Stack[i].GetMaximum()
239     else:
240     for bgMCHist in HistogramsToFit:
241     if(bgMCHist.GetMaximum() > finalMax):
242     finalMax = bgMCHist.GetMaximum()
243     if(Target.GetMaximum() > finalMax):
244     finalMax = Target.GetMaximum()
245    
246     makeRatioPlots = arguments.makeRatioPlots
247     makeDiffPlots = arguments.makeDiffPlots
248     if i == 0:
249     Canvas = TCanvas(histogram['name'] + "_Before")
250     if i == 1:
251     Canvas = TCanvas(histogram['name'] + "_After")
252     if makeRatioPlots or makeDiffPlots:
253     Canvas.SetFillStyle(0)
254     Canvas.Divide(1,2)
255     Canvas.cd(1)
256     gPad.SetPad(0.01,0.25,0.99,0.99)
257     gPad.SetMargin(0.1,0.05,0.02,0.07)
258     gPad.SetFillStyle(0)
259     gPad.Update()
260     gPad.Draw()
261     Canvas.cd(2)
262     gPad.SetPad(0.01,0.01,0.99,0.25)
263     #format: gPad.SetMargin(l,r,b,t)
264     gPad.SetMargin(0.1,0.05,0.4,0.02)
265     gPad.SetFillStyle(0)
266     gPad.SetGridy(1)
267     gPad.Update()
268     gPad.Draw()
269    
270     Canvas.cd(1)
271    
272     if not arguments.noStack:
273     Stack[i].SetTitle(histoTitle)
274     Stack[i].Draw("HIST")
275     Stack[i].GetXaxis().SetTitle(xAxisLabel)
276     Stack[i].SetMaximum(1.1*finalMax)
277     Stack[i].SetMinimum(0.0001)
278     if makeRatioPlots or makeDiffPlots:
279     Stack[i].GetHistogram().GetXaxis().SetLabelSize(0)
280     else:
281     HistogramsToFit[0].SetTitle(histoTitle)
282     HistogramsToFit[0].Draw("HIST")
283     HistogramsToFit[0].GetXaxis().SetTitle(xAxisLabel)
284     HistogramsToFit[0].SetMaximum(1.1*finalMax)
285     HistogramsToFit[0].SetMinimum(0.0001)
286     for bgMCHist in HistogramsToFit:
287     bgMCHist.Draw("HIST SAME")
288    
289     dataYield = Target.Integral (1, Target.GetNbinsX ())
290     mcYield = 0.0
291     for bgMCHist in HistogramsToFit:
292     mcYield += bgMCHist.Integral (1, bgMCHist.GetNbinsX ())
293     Label.Clear ()
294     if i == 0:
295     Label.AddText ("Before Fit to Data")
296     if i == 1:
297     Label.AddText ("After Fit to Data")
298     Label.AddText ("data yield: " + str (dataYield))
299     Label.AddText ("MC yield: " + str (mcYield))
300    
301     Target.Draw("E SAME")
302     BgMCLegend.Draw()
303     LumiLabel.Draw()
304     Label.Draw()
305    
306     if makeRatioPlots or makeDiffPlots:
307     Canvas.cd(2)
308     BgSum = Stack[i].GetStack().Last()
309     Comparison = Target.Clone()
310     Comparison.Add(BgSum,-1)
311     if not makeDiffPlots:
312     Comparison.Divide(BgSum)
313     Comparison.SetTitle("")
314     Comparison.GetXaxis().SetTitle(xAxisLabel)
315     if makeRatioPlots:
316     Comparison.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
317     elif makeDiffPlots:
318     Comparison.GetYaxis().SetTitle("Data-MC")
319     Comparison.GetYaxis().CenterTitle()
320     Comparison.GetYaxis().SetTitleSize(0.1)
321     Comparison.GetYaxis().SetTitleOffset(0.35)
322     Comparison.GetXaxis().SetTitleSize(0.15)
323     Comparison.GetYaxis().SetLabelSize(0.1)
324     Comparison.GetXaxis().SetLabelSize(0.15)
325     if makeRatioPlots:
326     Comparison.GetYaxis().SetRangeUser(-1,1)
327     elif makeDiffPlots:
328     YMax = Comparison.GetMaximum()
329     YMin = Comparison.GetMinimum()
330     if YMax <= 0 and YMin <= 0:
331     Comparison.GetYaxis().SetRangeUser(-1.2*YMin,0)
332     elif YMax >= 0 and YMin >= 0:
333     Comparison.GetYaxis().SetRangeUser(0,1.2*YMax)
334     else: #axis crosses y=0
335     if abs(YMax) > abs(YMin):
336     Comparison.GetYaxis().SetRangeUser(-1.2*YMax,1.2*YMax)
337     else:
338     Comparison.GetYaxis().SetRangeUser(-1.2*YMin,1.2*YMin)
339    
340     Comparison.GetYaxis().SetNdivisions(205)
341     Comparison.Draw()
342    
343     outputFile.cd ("OSUAnalysis/" + histogram['channel'])
344     if i == 0:
345     Canvas.Write (histogram['name'] + "_Before")
346     if arguments.plot_savePdf:
347     Canvas.Print (pdfFileName, "pdf")
348     if i == 1:
349     Canvas.Write (histogram['name'] + "_After")
350     if arguments.plot_savePdf:
351     if histogram == input_histograms[-1]:
352     Canvas.Print (pdfFileName + ")", "pdf")
353     else:
354     Canvas.Print (pdfFileName, "pdf")
355    
356     outputFile.Close ()
357    
358     for dataset in datasets_needed:
359     dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
360     os.remove(dataset_file)