ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/stack_from_dc.py
Revision: 1.4
Committed: Tue Nov 6 14:15:07 2012 UTC (12 years, 6 months ago) by nmohr
Content type: text/x-python
Branch: MAIN
Changes since 1.3: +44 -33 lines
Log Message:
Add pre fit plot

File Contents

# User Rev Content
1 nmohr 1.1 #!/usr/bin/env python
2     import pickle
3     import ROOT
4     from BetterConfigParser import BetterConfigParser
5     import sys, os
6     from optparse import OptionParser
7     from copy import copy,deepcopy
8     from StackMaker import StackMaker
9     from math import sqrt
10     import math
11     from HiggsAnalysis.CombinedLimit.DatacardParser import *
12     from HiggsAnalysis.CombinedLimit.ShapeTools import *
13    
14     ROOT.gROOT.SetBatch(True)
15     ROOT.gSystem.Load("libHiggsAnalysisCombinedLimit.so")
16    
17     #CONFIGURE
18     argv = sys.argv
19     parser = OptionParser()
20     parser.add_option("-D", "--datacard", dest="dc", default="",
21     help="Datacard to be plotted")
22     parser.add_option("-B", "--bin", dest="bin", default="",
23     help="DC bin to plot")
24     parser.add_option("-M", "--mlfit", dest="mlfit", default="",
25     help="mlfit file for nuisances")
26     parser.add_option("-F", "--fitresult", dest="fit", default="s",
27     help="Fit result to be used, 's' (signal+background) or 'b' (background only), default is 's'")
28     parser.add_option("-C", "--config", dest="config", default=[], action="append",
29     help="configuration file")
30     (opts, args) = parser.parse_args(argv)
31    
32    
33     def readBestFit(theFile):
34     file = ROOT.TFile(theFile)
35 nmohr 1.4 if file == None: raise RuntimeError, "Cannot open file %s" % theFile
36 nmohr 1.1 fit_s = file.Get("fit_s")
37     fit_b = file.Get("fit_b")
38     prefit = file.Get("nuisances_prefit")
39     if fit_s == None or fit_s.ClassName() != "RooFitResult": raise RuntimeError, "File %s does not contain the output of the signal fit 'fit_s'" % args[0]
40     if fit_b == None or fit_b.ClassName() != "RooFitResult": raise RuntimeError, "File %s does not contain the output of the background fit 'fit_b'" % args[0]
41     if prefit == None or prefit.ClassName() != "RooArgSet": raise RuntimeError, "File %s does not contain the prefit nuisances 'nuisances_prefit'" % args[0]
42    
43     isFlagged = {}
44     table = {}
45     fpf_b = fit_b.floatParsFinal()
46     fpf_s = fit_s.floatParsFinal()
47     nuiVariation = {}
48     for i in range(fpf_s.getSize()):
49     nuis_s = fpf_s.at(i)
50     name = nuis_s.GetName();
51     nuis_b = fpf_b.find(name)
52     nuis_p = prefit.find(name)
53     if nuis_p != None:
54     mean_p, sigma_p = (nuis_p.getVal(), nuis_p.getError())
55     for fit_name, nuis_x in [('b', nuis_b), ('s',nuis_s)]:
56     if nuis_p != None:
57     valShift = (nuis_x.getVal() - mean_p)/sigma_p
58     #sigShift = nuis_x.getError()/sigma_p
59     print fit_name, name
60     print valShift
61     nuiVariation['%s_%s'%(fit_name,name)] = valShift
62     #print valShift
63     return nuiVariation
64    
65 nmohr 1.4 def getBestFitShapes(procs,theShapes,shapeNui,DC,setup,opts,Dict):
66     b = opts.bin
67     for p in procs:
68     counter = 0
69     nom = theShapes[p].Clone()
70     for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:
71     if errline[b][p] == 0: continue
72     if ("shape" in pdf) and not 'CMS_vhbb_stats_' in lsyst:
73     if shapeNui > 0.:
74     theVari = 'Up'
75     else:
76     theVari = 'Down'
77     bestNuiVar = theShapes[p+lsyst+theVari].Clone()
78     bestNuiVar.Add(nom,-1.)
79     bestNuiVar.Scale(abs(shapeNui[p]))
80     if counter == 0:
81     bestNui = bestNuiVar.Clone()
82     else:
83     bestNui.Add(bestNuiVar)
84     counter +=1
85     nom.Add(bestNui)
86     nom.Scale(theShapes[p].Integral()/nom.Integral())
87     nBins = nom.GetNbinsX()
88     for bin in range(1,nBins+1):
89     nom.SetBinError(bin,theShapes[p].GetBinError(bin))
90     theShapes['%s_%s'%(opts.fit,p)] = nom.Clone()
91     histos = []
92     typs = []
93     for s in setup:
94     if 'ZH' == s:
95     Overlay=copy(theShapes[Dict[s]])
96     else:
97     histos.append(theShapes['%s_%s'%(opts.fit,Dict[s])])
98     typs.append(s)
99     return histos,typs
100    
101 nmohr 1.1
102     def drawFromDC():
103     config = BetterConfigParser()
104     config.read(opts.config)
105     print config.sections()
106     region = 'BDT'
107     var = 'BDT'
108     ws_var = config.get('plotDef:%s'%var,'relPath')
109     blind = eval(config.get('Plot:%s'%region,'blind'))
110     Stack=StackMaker(config,var,region,True)
111    
112 nmohr 1.4 preFit = False
113     if not opts.mlfit:
114     preFit = True
115    
116 nmohr 1.1 dataname = ''
117     if 'Zmm' in opts.bin: dataname = 'Zmm'
118     elif 'Zee' in opts.bin: dataname = 'Zee'
119     elif 'Wmn' in opts.bin: dataname = 'Wmn'
120     elif 'Wen' in opts.bin: dataname = 'Wen'
121     elif 'Znn' in opts.bin: dataname = 'Znn'
122    
123     log = eval(config.get('Plot:%s'%region,'log'))
124    
125     setup = config.get('Plot_general','setup').split(',')
126     Dict = eval(config.get('LimitGeneral','Dict'))
127     lumi = eval(config.get('Plot_general','lumi'))
128    
129     options = copy(opts)
130     options.dataname = "data_obs"
131     options.mass = 0
132     options.format = "%8.3f +/- %6.3f"
133 nmohr 1.2 options.channel = opts.bin
134 nmohr 1.1 options.excludeSyst = []
135     options.norm = False
136     options.stat = False
137     options.bin = True # fake that is a binary output, so that we parse shape lines
138     options.out = "tmp.root"
139     options.fileName = args[0]
140     options.cexpr = False
141     options.fixpars = False
142     options.libs = []
143     options.verbose = 0
144     options.poisson = 0
145     options.nuisancesToExclude = []
146     options.noJMax = None
147    
148     file = open(opts.dc, "r")
149     os.chdir(os.path.dirname(opts.dc))
150     DC = parseCard(file, options)
151     if not DC.hasShapes: DC.hasShapes = True
152     MB = ShapeBuilder(DC, options)
153     theShapes = {}
154     theSyst = {}
155 nmohr 1.4 nuiVar = {}
156 nmohr 1.1 if opts.mlfit:
157     nuiVar = readBestFit(opts.mlfit)
158     for b in DC.bins:
159     if options.channel != None and (options.channel != b): continue
160     exps = {}
161     expNui = {}
162     shapeNui = {}
163     for (p,e) in DC.exp[b].items(): # so that we get only self.DC.processes contributing to this bin
164     exps[p] = [ e, [] ]
165     expNui[p] = [ e, [] ]
166     for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:
167     if pdf in ('param', 'flatParam'): continue
168     # begin skip systematics
169     skipme = False
170     for xs in options.excludeSyst:
171     if re.search(xs, lsyst):
172     skipme = True
173     if skipme: continue
174     # end skip systematics
175     counter = 0
176     for p in DC.exp[b].keys(): # so that we get only self.DC.processes contributing to this bin
177     if errline[b][p] == 0: continue
178     if pdf == 'gmN':
179     exps[p][1].append(1/sqrt(pdfargs[0]+1));
180     elif pdf == 'gmM':
181     exps[p][1].append(errline[b][p]);
182     elif type(errline[b][p]) == list:
183     kmax = max(errline[b][p][0], errline[b][p][1], 1.0/errline[b][p][0], 1.0/errline[b][p][1]);
184     exps[p][1].append(kmax-1.);
185     elif pdf == 'lnN':
186     exps[p][1].append(max(errline[b][p], 1.0/errline[b][p])-1.);
187     if not nuiVar.has_key('%s_%s'%(opts.fit,lsyst)):
188     nui = 0.
189     else:
190     nui= nuiVar['%s_%s'%(opts.fit,lsyst)]
191     expNui[p][1].append(abs(1-errline[b][p])*nui);
192     elif ("shape" in pdf) and not 'CMS_vhbb_stats_' in lsyst:
193     s0 = MB.getShape(b,p)
194     sUp = MB.getShape(b,p,lsyst+"Up")
195     sDown = MB.getShape(b,p,lsyst+"Down")
196     if (s0.InheritsFrom("RooDataHist")):
197     s0 = ROOT.RooAbsData.createHistogram(s0,ws_var)
198     s0.SetName(p)
199     sUp = ROOT.RooAbsData.createHistogram(sUp,ws_var)
200     sUp.SetName(p+lsyst+'Up')
201     sDown = ROOT.RooAbsData.createHistogram(sDown,ws_var)
202     sDown.SetName(p+lsyst+'Down')
203     theShapes[p] = s0.Clone()
204     theShapes[p+lsyst+'Up'] = sUp.Clone()
205     theShapes[p+lsyst+'Down'] = sDown.Clone()
206     if not nuiVar.has_key('%s_%s'%(opts.fit,lsyst)):
207     nui = 0.
208     else:
209     nui= nuiVar['%s_%s'%(opts.fit,lsyst)]
210     shapeNui[p] = nui
211     if counter == 0:
212     theSyst[lsyst] = s0.Clone()
213     theSyst[lsyst+'Up'] = sUp.Clone()
214     theSyst[lsyst+'Down'] = sDown.Clone()
215     else:
216     theSyst[lsyst].Add(s0)
217     theSyst[lsyst+'Up'].Add(sUp.Clone())
218     theSyst[lsyst+'Down'].Add(sDown.Clone())
219     counter += 1
220     procs = DC.exp[b].keys(); procs.sort()
221     fmt = ("%%-%ds " % max([len(p) for p in procs]))+" "+options.format;
222     #Compute norm uncertainty and best fit
223     theNormUncert = {}
224     theBestFit = {}
225     for p in procs:
226     relunc = sqrt(sum([x*x for x in exps[p][1]]))
227     print fmt % (p, exps[p][0], exps[p][0]*relunc)
228     theNormUncert[p] = relunc
229     absBestFit = sum([x for x in expNui[p][1]])
230     theBestFit[p] = 1.+absBestFit
231    
232     histos = []
233     typs = []
234    
235     setup2=copy(setup)
236    
237     shapesUp = [[] for _ in range(0,len(setup2))]
238     shapesDown = [[] for _ in range(0,len(setup2))]
239    
240     for p in procs:
241 nmohr 1.2 b = opts.bin
242 nmohr 1.1 for s in setup:
243     if not Dict[s] == p: continue
244     if 'ZH' == s:
245     Overlay=copy(theShapes[Dict[s]])
246     else:
247     histos.append(theShapes[Dict[s]])
248     typs.append(s)
249     print s
250     for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:
251     if errline[b][p] == 0: continue
252     if ("shape" in pdf) and not 'CMS_vhbb_stats_' in lsyst:
253     print 'syst %s'%lsyst
254     shapesUp[setup2.index(s)].append(theShapes[Dict[s]+lsyst+'Up'])
255     shapesDown[setup2.index(s)].append(theShapes[Dict[s]+lsyst+'Down'])
256    
257     #-------------
258     #Compute absolute uncertainty from shapes
259     counter = 0
260     for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:
261     if ("shape" in pdf) and not 'CMS_vhbb_stats_' in lsyst:
262     theSystUp = theSyst[lsyst+'Up'].Clone()
263     theSystUp.Add(theSyst[lsyst].Clone(),-1.)
264     theSystUp.Multiply(theSystUp)
265     theSystDown = theSyst[lsyst+'Down'].Clone()
266     theSystDown.Add(theSyst[lsyst].Clone(),-1.)
267     theSystDown.Multiply(theSystDown)
268     if counter == 0:
269     theAbsSystUp = theSystUp.Clone()
270     theAbsSystDown = theSystDown.Clone()
271     else:
272     theAbsSystUp.Add(theSystUp.Clone())
273     theAbsSystDown.Add(theSystDown.Clone())
274     counter +=1
275    
276     #-------------
277     #Best fit for shapes
278 nmohr 1.4 if not preFit:
279     histos, typs = getBestFitShapes(procs,theShapes,shapeNui,DC,setup,opts,Dict)
280 nmohr 1.1
281     counter = 0
282     errUp=[]
283     total=[]
284     errDown=[]
285     nBins = histos[0].GetNbinsX()
286     print 'total bins %s'%nBins
287     Error = ROOT.TGraphAsymmErrors(histos[0])
288     theTotalMC = histos[0].Clone()
289     for h in range(1,len(histos)):
290     theTotalMC.Add(histos[h])
291    
292     total = [[]]*nBins
293     errUp = [[]]*nBins
294     errDown = [[]]*nBins
295     for bin in range(1,nBins+1):
296     binError = theTotalMC.GetBinError(bin)
297     if math.isnan(binError):
298     binError = 0.
299     total[bin-1]=theTotalMC.GetBinContent(bin)
300     #Stat uncertainty of the MC outline
301     errUp[bin-1] = [binError]
302     errDown[bin-1] = [binError]
303     #Relative norm uncertainty of the individual MC
304     for h in range(0,len(histos)):
305     errUp[bin-1].append(histos[h].GetBinContent(bin)*theNormUncert[histos[h].GetName()])
306     errDown[bin-1].append(histos[h].GetBinContent(bin)*theNormUncert[histos[h].GetName()])
307     #Shape uncertainty of the MC
308     for bin in range(1,nBins+1):
309     #print sqrt(theSystUp.GetBinContent(bin))
310     errUp[bin-1].append(sqrt(theAbsSystUp.GetBinContent(bin)))
311     errDown[bin-1].append(sqrt(theAbsSystDown.GetBinContent(bin)))
312    
313    
314     #Add all in quadrature
315     totErrUp=[sqrt(sum([x**2 for x in bin])) for bin in errUp]
316     totErrDown=[sqrt(sum([x**2 for x in bin])) for bin in errDown]
317    
318     #Make TGraph with errors
319     for bin in range(1,nBins+1):
320     if not total[bin-1] == 0:
321     point=histos[0].GetXaxis().GetBinCenter(bin)
322     Error.SetPoint(bin-1,point,1)
323     Error.SetPointEYlow(bin-1,totErrDown[bin-1]/total[bin-1])
324     print 'down %s'%(totErrDown[bin-1]/total[bin-1])
325     Error.SetPointEYhigh(bin-1,totErrUp[bin-1]/total[bin-1])
326     print 'up %s'%(totErrUp[bin-1]/total[bin-1])
327    
328     #-----------------------
329     #Read data
330 nmohr 1.2 data0 = MB.getShape(opts.bin,'data_obs')
331 nmohr 1.1 if (data0.InheritsFrom("RooDataHist")):
332     data0 = ROOT.RooAbsData.createHistogram(data0,ws_var)
333     data0.SetName('data_obs')
334     datas=[data0]
335     datatyps = [None]
336     datanames=[dataname]
337    
338    
339     if blind:
340     for bin in range(10,datas[0].GetNbinsX()+1):
341     datas[0].SetBinContent(bin,0)
342    
343     histos.append(copy(Overlay))
344     typs.append('ZH')
345    
346     Stack.histos = histos
347     Stack.typs = typs
348     Stack.datas = datas
349     Stack.datatyps = datatyps
350     Stack.datanames= datanames
351     Stack.overlay = Overlay
352     Stack.AddErrors=Error
353     Stack.lumi = lumi
354     Stack.doPlot()
355    
356     print 'i am done!\n'
357     #-------------------------------------------------
358    
359    
360     if __name__ == "__main__":
361     drawFromDC()
362     sys.exit(0)