ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/workspace_datacard.py
Revision: 1.57
Committed: Mon Oct 22 15:45:27 2012 UTC (12 years, 6 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.56: +11 -0 lines
Log Message:
signal injection

File Contents

# User Rev Content
1 peller 1.8 #!/usr/bin/env python
2 peller 1.53 import os, sys, ROOT, warnings, pickle
3 peller 1.1 from ROOT import TFile
4     from array import array
5     from math import sqrt
6     from copy import copy
7     #suppres the EvalInstace conversion warning bug
8     warnings.filterwarnings( action='ignore', category=RuntimeWarning, message='creating converter.*' )
9 nmohr 1.31 from optparse import OptionParser
10 nmohr 1.20 from BetterConfigParser import BetterConfigParser
11 peller 1.1 from samplesclass import sample
12     from mvainfos import mvainfo
13     from progbar import progbar
14     from printcolor import printc
15 peller 1.13 from gethistofromtree import getHistoFromTree, orderandadd
16 peller 1.1
17     #CONFIGURE
18 nmohr 1.31 argv = sys.argv
19     parser = OptionParser()
20     parser.add_option("-P", "--path", dest="path", default="",
21     help="path to samples")
22     parser.add_option("-V", "--var", dest="variable", default="",
23     help="variable for shape analysis")
24     parser.add_option("-C", "--config", dest="config", default=[], action="append",
25     help="configuration file")
26     (opts, args) = parser.parse_args(argv)
27     if opts.config =="":
28     opts.config = "config"
29     print opts.config
30 nmohr 1.20 config = BetterConfigParser()
31 nmohr 1.31 config.read(opts.config)
32     anaTag = config.get("Analysis","tag")
33 peller 1.30
34 peller 1.36
35     # -------------------- parsing configuration and options: (an ugly spaghetti code section) ----------------------------------------------------------------------
36 peller 1.1 #get locations:
37     Wdir=config.get('Directories','Wdir')
38 peller 1.42 vhbbpath=config.get('Directories','vhbbpath')
39 nmohr 1.46 samplesinfo=config.get('Directories','samplesinfo')
40 peller 1.1 #systematics
41     systematics=config.get('systematics','systematics')
42     systematics=systematics.split(' ')
43     weightF=config.get('Weights','weightF')
44 nmohr 1.31 path=opts.path
45     var=opts.variable
46 peller 1.1 plot=config.get('Limit',var)
47 nmohr 1.46 infofile = open(samplesinfo,'r')
48 peller 1.1 info = pickle.load(infofile)
49     infofile.close()
50     options = plot.split(',')
51 bortigno 1.7 if len(options) < 12:
52     print "You have to choose option[11]: either Mjj or BDT"
53     sys.exit("You have to choose option[11]: either Mjj or BDT")
54 peller 1.1 name=options[1]
55     title = options[2]
56 peller 1.54 nBinsRB=int(options[3])
57     nBins= int(config.get('LimitGeneral','BDTbinning'))
58 peller 1.1 xMin=float(options[4])
59     xMax=float(options[5])
60 peller 1.29 SIG=options[9]
61 peller 1.1 data=options[10]
62 bortigno 1.7 anType=options[11]
63 peller 1.13 RCut=options[7]
64 peller 1.29 setup=eval(config.get('LimitGeneral','setup'))
65 peller 1.13 ROOToutname = options[6]
66     outpath=config.get('Directories','limits')
67     outfile = ROOT.TFile(outpath+'vhbb_TH_'+ROOToutname+'.root', 'RECREATE')
68 peller 1.29 systematicsnaming=eval(config.get('LimitGeneral','systematicsnaming7TeV'))
69 peller 1.44
70     TrainFlag = eval(config.get('Analysis','TrainFlag'))
71    
72    
73 peller 1.54
74 peller 1.30 if anaTag =='8TeV':
75 peller 1.29 systematicsnaming=eval(config.get('LimitGeneral','systematicsnaming8TeV'))
76 peller 1.44 elif not anaTag =='7TeV':
77 peller 1.30 print "What is your Analysis Tag in config? (anaTag)"
78     sys.exit("What is your Analysis Tag in config? (anaTag)")
79 peller 1.29 scaling=eval(config.get('LimitGeneral','scaling'))
80 peller 1.44
81     if TrainFlag:
82     MC_rescale_factor=2.
83     print 'I RESCALE BY 2.0'
84     else: MC_rescale_factor = 1.
85    
86 peller 1.29 rescaleSqrtN=eval(config.get('LimitGeneral','rescaleSqrtN'))
87 peller 1.11 if 'RTight' in RCut:
88 nmohr 1.17 Datacradbin=options[10]
89 peller 1.11 elif 'RMed' in RCut:
90 nmohr 1.17 Datacradbin=options[10]
91 peller 1.11 else:
92     Datacradbin=options[10]
93 peller 1.36 blind=eval(config.get('LimitGeneral','blind'))
94     BKGlist = eval(config.get('LimitGeneral','BKG'))
95     #Groups for adding samples together
96     Group = eval(config.get('LimitGeneral','Group'))
97     #naming for DC
98     Dict= eval(config.get('LimitGeneral','Dict'))
99     weightF_sys = eval(config.get('LimitGeneral','weightF_sys'))
100     binstat = eval(config.get('LimitGeneral','binstat'))
101 nmohr 1.37 addSample_sys = None if not config.has_option('LimitGeneral','addSample_sys') else eval(config.get('LimitGeneral','addSample_sys'))
102 peller 1.36 bdt = False
103     mjj = False
104     #print str(anType)
105     #print len(options)
106     if str(anType) == 'BDT':
107     bdt = True
108     systematics = eval(config.get('LimitGeneral','sys_BDT'))
109     elif str(anType) == 'Mjj':
110     mjj = True
111     systematics = eval(config.get('LimitGeneral','sys_Mjj'))
112     sys_cut_suffix=eval(config.get('LimitGeneral','sys_cut_suffix'))
113    
114 peller 1.56 rebin_active=eval(config.get('LimitGeneral','rebin_active'))
115 peller 1.36
116 peller 1.57 signal_inject=config.get('LimitGeneral','signal_inject')
117    
118 nmohr 1.24
119 peller 1.53 class Rebinner:
120 peller 1.56 def __init__(self,nBins,lowedgearray,active=True):
121 peller 1.53 self.lowedgearray=lowedgearray
122     self.nBins=nBins
123 peller 1.56 self.active=active
124 peller 1.53 def rebin(self, histo):
125 peller 1.56 if not self.active: return histo
126 peller 1.53 histo.Rebin(self.nBins,'hnew',self.lowedgearray)
127     binhisto=ROOT.gDirectory.Get('hnew')
128     newhisto=ROOT.TH1F('new','new',self.nBins,self.lowedgearray[0],self.lowedgearray[-1])
129 peller 1.56 newhisto.Sumw2()
130 peller 1.53 for bin in range(0,self.nBins+1):
131     newhisto.SetBinContent(bin,binhisto.GetBinContent(bin))
132     newhisto.SetBinError(bin,binhisto.GetBinError(bin))
133     newhisto.SetName(binhisto.GetName())
134     newhisto.SetTitle(binhisto.GetTitle())
135     return newhisto
136    
137    
138 peller 1.23
139 peller 1.36 # -------------------- generate the Workspace with all Histograms: ----------------------------------------------------------------------
140 peller 1.11
141     WS = ROOT.RooWorkspace('%s'%Datacradbin,'%s'%Datacradbin) #Zee
142 peller 1.1 print 'WS initialized'
143 bortigno 1.7 disc= ROOT.RooRealVar(name,name,xMin,xMax)
144 peller 1.1 obs = ROOT.RooArgList(disc)
145     ROOT.gROOT.SetStyle("Plain")
146     datas = []
147     datatyps =[]
148     histos = []
149     typs = []
150 nmohr 1.37 hNames = []
151 nmohr 1.39 aNames = []
152 peller 1.1 statUps=[]
153     statDowns=[]
154 nmohr 1.15 if blind:
155 peller 1.29 printc('red','', 'I AM BLINDED!')
156 peller 1.53
157    
158    
159     #---- get the BKG for the rebinning calculation----
160     counterRB=0
161 peller 1.57 injection = False
162 peller 1.53
163     for job in info:
164     if eval(job.active):
165     if job.subsamples:
166     for subsample in range(0,len(job.subnames)):
167     if job.subnames[subsample] in BKGlist:
168     hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample)
169     if counterRB == 0:
170     hDummyRB = copy(hTemp)
171     else:
172     hDummyRB.Add(hTemp)
173     counterRB += 1
174    
175     else:
176     if job.name in BKGlist:
177     hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor)
178     if counterRB == 0:
179     hDummyRB = copy(hTemp)
180     else:
181     hDummyRB.Add(hTemp)
182     counterRB += 1
183 peller 1.57
184    
185     elif job.name == signal_inject:
186     inject_SIG, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor)
187     injection = True
188    
189 peller 1.53 ErrorR=0
190     ErrorL=0
191     TotR=0
192     TotL=0
193 peller 1.54 binR=nBinsRB
194 peller 1.53 binL=1
195     rel=1.0
196     #---- from right
197 peller 1.56 while rel > 0.35:
198 peller 1.53 TotR+=hDummyRB.GetBinContent(binR)
199     ErrorR=sqrt(ErrorR**2+hDummyRB.GetBinError(binR)**2)
200     binR-=1
201     if not TotR == 0 and not ErrorR == 0:
202     rel=ErrorR/TotR
203     #print rel
204     print 'upper bin is %s'%binR
205    
206     #---- from left
207     rel=1.0
208 peller 1.56 while rel > 0.35:
209 peller 1.53 TotL+=hDummyRB.GetBinContent(binL)
210     ErrorL=sqrt(ErrorL**2+hDummyRB.GetBinError(binL)**2)
211     binL+=1
212     if not TotL == 0 and not ErrorL == 0:
213     rel=ErrorL/TotL
214     #print rel
215     print 'lower bin is %s'%binL
216    
217     inbetween=binR-binL
218 peller 1.54 stepsize=int(inbetween)/(int(nBins)-2)
219     modulo = int(inbetween)%(int(nBins)-2)
220 peller 1.53
221     print'stepsize %s'% stepsize
222     print 'modulo %s'%modulo
223    
224 peller 1.55 binlist=[binL]
225 peller 1.54 for i in range(0,int(nBins)-3):
226 peller 1.53 binlist.append(binlist[-1]+stepsize)
227     binlist[-1]+=modulo
228     binlist.append(binR)
229 peller 1.54 binlist.append(nBinsRB+1)
230 peller 1.53
231     print binlist
232 peller 1.56 myBinning=Rebinner(int(nBins),array('d',[-1.0]+[hDummyRB.GetBinLowEdge(i) for i in binlist]),rebin_active)
233 peller 1.53 #--------------------------------------------------
234    
235 peller 1.57
236     if injection: hDummyRB.Add(inject_SIG)
237 peller 1.54 hDummy=myBinning.rebin(hDummyRB)
238 peller 1.53
239    
240 peller 1.33 if weightF_sys:
241 peller 1.48 weightF_sys_UP=config.get('Weights','weightF_sys_UP')
242     weightF_sys_DOWN=config.get('Weights','weightF_sys_DOWN')
243 peller 1.33 weightF_sys_Ups = []
244     weightF_sys_Downs = []
245 nmohr 1.37 if addSample_sys:
246     sTyps = []
247     addSample_sys_histos = []
248     aSample_sys_Ups = []
249     aSample_sys_Downs = []
250 peller 1.33
251 peller 1.1 for job in info:
252 peller 1.28 if eval(job.active):
253 peller 1.29 if job.subsamples:
254     for subsample in range(0,len(job.subnames)):
255     if job.subnames[subsample] in BKGlist:
256 peller 1.45 print 'getting %s'%job.subnames[subsample]
257 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample)
258 peller 1.45 print hTemp.Integral()
259 peller 1.53 histos.append(myBinning.rebin(hTemp))
260 peller 1.29 typs.append(Group[job.subnames[subsample]])
261 nmohr 1.37 hNames.append(job.subnames[subsample])
262 peller 1.33 if weightF_sys:
263 peller 1.48 hTempWU, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample,'weightF_sys_UP')
264 peller 1.53 weightF_sys_Ups.append(myBinning.rebin(hTempWU))
265 peller 1.48 hTempWD, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample,'weightF_sys_DOWN')
266 peller 1.53 weightF_sys_Downs.append(myBinning.rebin(hTempWD))
267 peller 1.48
268 peller 1.29 elif job.subnames[subsample] == SIG:
269 peller 1.45 hNames.append(job.subnames[subsample])
270     print 'getting %s'%job.subnames[subsample]
271 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample)
272 peller 1.45 print hTemp.Integral()
273 peller 1.53 histos.append(myBinning.rebin(hTemp))
274 peller 1.29 typs.append(Group[job.subnames[subsample]])
275 peller 1.33 if weightF_sys:
276 peller 1.48 hTempWU, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample,'weightF_sys_UP')
277 peller 1.53 weightF_sys_Ups.append(myBinning.rebin(hTempWU))
278 peller 1.48 hTempWD, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample,'weightF_sys_DOWN')
279 peller 1.53 weightF_sys_Downs.append(myBinning.rebin(hTempWD))
280 nmohr 1.41 if addSample_sys and job.subnames[subsample] in addSample_sys.values():
281 nmohr 1.40 aNames.append(job.subnames[subsample])
282 nmohr 1.51 hTempS, s_ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample)
283 peller 1.53 addSample_sys_histos.append(myBinning.rebin(hTempS))
284 peller 1.29
285     else:
286     if job.name in BKGlist:
287     #print job.getpath()
288 peller 1.45 print 'getting %s'%job.name
289 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor)
290 peller 1.53 histos.append(myBinning.rebin(hTemp))
291 peller 1.45 print hTemp.Integral()
292 peller 1.33 typs.append(Group[job.name])
293 peller 1.42 hNames.append(job.name)
294 peller 1.33 if weightF_sys:
295 peller 1.48 hTempWU, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,-1,'weightF_sys_UP')
296 peller 1.53 weightF_sys_Ups.append(myBinning.rebin(hTempWU))
297 peller 1.48 hTempWD, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,-1,'weightF_sys_DOWN')
298 peller 1.55 weightF_sys_Downs.append(myBinning.rebin(hTempWD))
299 peller 1.33
300 peller 1.29 elif job.name == SIG:
301 peller 1.45 print 'getting %s'%job.name
302 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor)
303 peller 1.53 histos.append(myBinning.rebin(hTemp))
304 peller 1.45 print hTemp.Integral()
305 peller 1.33 typs.append(Group[job.name])
306 nmohr 1.37 hNames.append(job.name)
307 peller 1.33 if weightF_sys:
308 peller 1.48 hTempWU, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,-1,'weightF_sys_UP')
309 peller 1.53 weightF_sys_Ups.append(myBinning.rebin(hTempWU))
310 peller 1.48 hTempWD, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,-1,'weightF_sys_DOWN')
311 peller 1.53 weightF_sys_Downs.append(myBinning.rebin(hTempWD))
312 peller 1.33
313 peller 1.29 elif job.name in data:
314     #print 'DATA'
315 peller 1.45 print 'getting %s'%job.name
316 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options)
317 peller 1.53 datas.append(myBinning.rebin(hTemp))
318 peller 1.45 print hTemp.Integral()
319 peller 1.29 datatyps.append(typ)
320 nmohr 1.40
321 peller 1.43 if addSample_sys and job.name in addSample_sys.values():
322 peller 1.42 aNames.append(job.name)
323     hTempS, s_ = getHistoFromTree(job,path,config,options,MC_rescale_factor)
324 peller 1.53 addSample_sys_histos.append(myBinning.rebin(hTempS))
325 peller 1.1
326     MC_integral=0
327     MC_entries=0
328     for histo in histos:
329     MC_integral+=histo.Integral()
330 peller 1.45 print 'histo integral %s'%histo.Integral()
331 peller 1.36 printc('green','', 'MC integral = %s'%MC_integral)
332    
333 nmohr 1.37 def getAlternativeShapes(histos,altHistos,hNames,aNames,addSample_sys):
334 nmohr 1.51 theHistosUp = []
335     theHistosDown = []
336     for histo in histos:
337     theHistosUp.append(histo.Clone())
338     theHistosDown.append(histo.Clone())
339 nmohr 1.37 for name in addSample_sys.keys():
340     print name
341 nmohr 1.51 hVar = altHistos[aNames.index(addSample_sys[name])].Clone()
342     hNom = histos[hNames.index(name)].Clone()
343     hAlt = hNom.Clone()
344     hNom.Add(hVar,-1.)
345     hAlt.Add(hNom)
346     for bin in range(0,nBins):
347     if hAlt.GetBinContent(bin) < 0.: hAlt.SetBinContent(bin,0.)
348     theHistosUp[hNames.index(name)] = hVar.Clone()
349     theHistosDown[hNames.index(name)] = hAlt.Clone()
350 nmohr 1.37 return theHistosUp, theHistosDown
351    
352 peller 1.13 #order and add together
353 peller 1.33 typs2=copy(typs)
354 nmohr 1.37 typs3=copy(typs)
355     typs4=copy(typs)
356 peller 1.48 typs5=copy(typs)
357 nmohr 1.40 if addSample_sys:
358     aSampleUp, aSampleDown = getAlternativeShapes(histos,addSample_sys_histos,hNames,aNames,addSample_sys)
359 peller 1.13 histos, typs = orderandadd(histos,typs,setup)
360 nmohr 1.37 if weightF_sys:
361 peller 1.48 weightF_sys_Ups,_=orderandadd(weightF_sys_Ups,typs2,setup)
362     weightF_sys_Downs,_=orderandadd(weightF_sys_Downs,typs3,setup)
363    
364 nmohr 1.37 if addSample_sys:
365 peller 1.48 aSampleUp,aNames=orderandadd(aSampleUp,typs4,setup)
366     aSampleDown,aNames=orderandadd(aSampleDown,typs5,setup)
367 peller 1.1
368     for i in range(0,len(histos)):
369 peller 1.29 newname=Dict[typs[i]]
370     histos[i].SetName(newname)
371 peller 1.5 #histos[i].SetDirectory(outfile)
372     outfile.cd()
373 peller 1.1 histos[i].Write()
374 nmohr 1.22 errorsum=0
375     total=0
376     for j in range(histos[i].GetNbinsX()+1):
377     errorsum=errorsum+(histos[i].GetBinError(j))**2
378     errorsum=sqrt(errorsum)
379     total=histos[i].Integral()
380 peller 1.35
381 peller 1.36 if binstat: #treating statistics in single bins
382 peller 1.35 for bin in range(0,nBins):
383     statUps.append(histos[i].Clone())
384     statDowns.append(histos[i].Clone())
385     statUps[i*nBins+bin].SetName('%sCMS_vhbb_stats_%s_%s_%sUp'%(newname,newname,bin,options[10]))
386     statDowns[i*nBins+bin].SetName('%sCMS_vhbb_stats_%s_%s_%sDown'%(newname,newname,bin,options[10]))
387     #shift up and down with statistical error
388     if rescaleSqrtN:
389     statUps[i*nBins+bin].SetBinContent(bin,statUps[i*nBins+bin].GetBinContent(bin)+statUps[i*nBins+bin].GetBinError(bin)/total*errorsum)
390     statDowns[i*nBins+bin].SetBinContent(bin,statDowns[i*nBins+bin].GetBinContent(bin)-statDowns[i*nBins+bin].GetBinError(bin)/total*errorsum)
391     else:
392     statUps[i*nBins+bin].SetBinContent(bin,statUps[i*nBins+bin].GetBinContent(bin)+statUps[i*nBins+bin].GetBinError(bin))
393     statDowns[i*nBins+bin].SetBinContent(bin,statDowns[i*nBins+bin].GetBinContent(bin)-statDowns[i*nBins+bin].GetBinError(bin))
394     statUps[i*nBins+bin].Write()
395     statDowns[i*nBins+bin].Write()
396     histPdf = ROOT.RooDataHist(newname,newname,obs,histos[i])
397     #UP stats of MCs
398     RooStatsUp = ROOT.RooDataHist('%sCMS_vhbb_stats_%s_%s_%sUp'%(newname,newname,bin,options[10]),'%sCMS_vhbb_stats_%s_%s_%sUp'%(newname,newname,bin,options[10]),obs, statUps[i*nBins+bin])
399     #DOWN stats of MCs
400     RooStatsDown = ROOT.RooDataHist('%sCMS_vhbb_stats_%s_%s_%sDown'%(newname,newname,bin,options[10]),'%sCMS_vhbb_stats_%s_%s_%sDown'%(newname,newname,bin,options[10]),obs, statDowns[i*nBins+bin])
401     getattr(WS,'import')(histPdf)
402     getattr(WS,'import')(RooStatsUp)
403     getattr(WS,'import')(RooStatsDown)
404 peller 1.13
405 peller 1.35 else:
406     statUps.append(histos[i].Clone())
407     statDowns.append(histos[i].Clone())
408     statUps[i].SetName('%sCMS_vhbb_stats_%s_%sUp'%(newname,newname,options[10]))
409     statDowns[i].SetName('%sCMS_vhbb_stats_%s_%sDown'%(newname,newname,options[10]))
410     #shift up and down with statistical error
411     for j in range(histos[i].GetNbinsX()+1):
412     if rescaleSqrtN:
413     statUps[i].SetBinContent(j,statUps[i].GetBinContent(j)+statUps[i].GetBinError(j)/total*errorsum)
414     statDowns[i].SetBinContent(j,statDowns[i].GetBinContent(j)-statDowns[i].GetBinError(j)/total*errorsum)
415     else:
416     statUps[i].SetBinContent(j,statUps[i].GetBinContent(j)+statUps[i].GetBinError(j))
417     statDowns[i].SetBinContent(j,statDowns[i].GetBinContent(j)-statDowns[i].GetBinError(j))
418 peller 1.47 #if statDowns[i].GetBinError(j)<0.: statDowns[i].SetBinContent(j,0.)
419 peller 1.35 statUps[i].Write()
420     statDowns[i].Write()
421     histPdf = ROOT.RooDataHist(newname,newname,obs,histos[i])
422     #UP stats of MCs
423     RooStatsUp = ROOT.RooDataHist('%sCMS_vhbb_stats_%s_%sUp'%(newname,newname,options[10]),'%sCMS_vhbb_stats_%s_%sUp'%(newname,newname,options[10]),obs, statUps[i])
424     #DOWN stats of MCs
425     RooStatsDown = ROOT.RooDataHist('%sCMS_vhbb_stats_%s_%sDown'%(newname,newname,options[10]),'%sCMS_vhbb_stats_%s_%sDown'%(newname,newname,options[10]),obs, statDowns[i])
426     getattr(WS,'import')(histPdf)
427     getattr(WS,'import')(RooStatsUp)
428     getattr(WS,'import')(RooStatsDown)
429    
430     #And now WeightF sys
431 peller 1.33 if weightF_sys:
432 peller 1.49 weightF_sys_Downs[i].SetName('%sUEPSDown'%(newname))
433     weightF_sys_Ups[i].SetName('%sUEPSUp'%(newname))
434 peller 1.33 weightF_sys_Ups[i].Write()
435 peller 1.35 weightF_sys_Downs[i].Write()
436 peller 1.49 RooWeightFUp = ROOT.RooDataHist('%sUEPSUp'%(newname),'%sUEPSUp'%(newname),obs, weightF_sys_Ups[i])
437     RooWeightFDown = ROOT.RooDataHist('%sUEPSDown'%(newname),'%sUEPSDown'%(newname),obs, weightF_sys_Downs[i])
438 peller 1.33 getattr(WS,'import')(RooWeightFUp)
439     getattr(WS,'import')(RooWeightFDown)
440 nmohr 1.37 #And now Additional sample sys
441     if addSample_sys:
442     aSample_sys_Downs.append(aSampleUp[i].Clone())
443     aSample_sys_Ups.append(aSampleDown[i].Clone())
444     aSample_sys_Downs[i].SetName('%sCMS_vhbb_model_%sDown'%(newname,newname))
445     aSample_sys_Ups[i].SetName('%sCMS_vhbb_model_%sUp'%(newname,newname))
446     aSample_sys_Ups[i].Write()
447     aSample_sys_Downs[i].Write()
448     RooSampleUp = ROOT.RooDataHist('%sCMS_vhbb_model_%sUp'%(newname,newname),'%sCMS_vhbb_model_%sUp'%(newname,newname),obs, aSample_sys_Ups[i])
449     RooSampleDown = ROOT.RooDataHist('%sCMS_vhbb_model_%sDown'%(newname,newname),'%sCMS_vhbb_model_%sDown'%(newname,newname),obs, aSample_sys_Downs[i])
450     getattr(WS,'import')(RooSampleUp)
451     getattr(WS,'import')(RooSampleDown)
452 peller 1.1
453    
454     #HISTOGRAMM of DATA
455 peller 1.54 d1 = datas[0]
456     if len(datas)>1:
457     for i in range(1,len(datas)):
458     d1.Add(datas[i],1)
459 peller 1.13 printc('green','','\nDATA integral = %s\n'%d1.Integral())
460 peller 1.1 flow = d1.GetEntries()-d1.Integral()
461     if flow > 0:
462 peller 1.13 printc('red','','U/O flow: %s'%flow)
463 peller 1.29 d1.SetName(Dict['Data'])
464 peller 1.5 outfile.cd()
465 peller 1.47 #d1.Write()
466    
467 peller 1.36
468 nmohr 1.15 if blind:
469 peller 1.47 print 'toy data integral: %s'%hDummy.Integral()
470 peller 1.29 hDummy.SetName(Dict['Data'])
471     histPdf = ROOT.RooDataHist(Dict['Data'],Dict['Data'],obs,hDummy)
472 nmohr 1.25 #rooDummy = ROOT.RooDataHist('data_obs','data_obs',obs,hDummy)
473     #toy = ROOT.RooHistPdf('data_obs','data_obs',ROOT.RooArgSet(obs),rooDummy)
474     #rooDataSet = toy.generate(ROOT.RooArgSet(obs),int(d1.Integral()))
475     #histPdf = ROOT.RooDataHist('data_obs','data_obs',ROOT.RooArgSet(obs),rooDataSet.reduce(ROOT.RooArgSet(obs)))
476 peller 1.47 hDummy.Write()
477 nmohr 1.15 else:
478 peller 1.29 histPdf = ROOT.RooDataHist(Dict['Data'],Dict['Data'],obs,d1)
479 peller 1.47 d1.Write()
480 peller 1.1 #ROOT.RooAbsData.plotOn(histPdf,frame)
481     getattr(WS,'import')(histPdf)
482    
483     #SYSTEMATICS:
484     UD = ['Up','Down']
485     systhistosarray=[]
486 peller 1.13 Coco=0 #iterates over (all systematics) * (up,down)
487 nmohr 1.22 nominalShape = options[0]
488 bortigno 1.7
489     for sys in systematics:
490 peller 1.36 for Q in UD: # Q = 'Up' and 'Down'
491 peller 1.32 #options[7] ist der CutString name
492     new_cut=sys_cut_suffix[sys]
493     new_options = copy(options)
494     if not new_cut == 'nominal':
495 peller 1.34 old_str,new_str=new_cut.split('>')
496     new_options[7]=[options[7],old_str,new_str.replace('?',Q)]
497 peller 1.1 ff=options[0].split('.')
498 bortigno 1.7 if bdt == True:
499 peller 1.52 #options[0] ist die treeVar
500 peller 1.13 ff[1]='%s_%s'%(sys,Q.lower())
501 peller 1.32 new_options[0]=nominalShape.replace('.nominal','.%s_%s'%(sys,Q.lower()))
502 bortigno 1.7 elif mjj == True:
503 peller 1.52 if sys == 'JER' or sys == 'JES':
504     ff[0]='H_%s'%(sys)
505     ff[1]='mass_%s'%(Q.lower())
506     new_options[0]='.'.join(ff)
507     else: pass
508 peller 1.1
509 peller 1.8 print '\n'
510 peller 1.13 printc('blue','','\t--> doing systematic %s %s'%(sys,Q.lower()))
511 peller 1.1
512     systhistosarray.append([])
513     typsX = []
514    
515     for job in info:
516 peller 1.29 if eval(job.active):
517     if job.subsamples:
518     for subsample in range(0,len(job.subnames)):
519     if job.subnames[subsample] in BKGlist:
520 peller 1.32 hTemp, typ = getHistoFromTree(job,path,config,new_options,MC_rescale_factor,subsample)
521 peller 1.53 systhistosarray[Coco].append(myBinning.rebin(hTemp))
522 peller 1.29 typsX.append(Group[job.subnames[subsample]])
523     elif job.subnames[subsample] == SIG:
524 peller 1.32 hTemp, typ = getHistoFromTree(job,path,config,new_options,MC_rescale_factor,subsample)
525 peller 1.53 systhistosarray[Coco].append(myBinning.rebin(hTemp))
526 peller 1.29 typsX.append(Group[job.subnames[subsample]])
527    
528     else:
529     if job.name in BKGlist:
530 peller 1.32 hTemp, typ = getHistoFromTree(job,path,config,new_options,MC_rescale_factor)
531 peller 1.53 systhistosarray[Coco].append(myBinning.rebin(hTemp))
532 peller 1.29 typsX.append(Group[job.name])
533     elif job.name == SIG:
534 peller 1.32 hTemp, typ = getHistoFromTree(job,path,config,new_options,MC_rescale_factor)
535 peller 1.53 systhistosarray[Coco].append(myBinning.rebin(hTemp))
536 peller 1.29 typsX.append(Group[job.name])
537    
538 peller 1.1 MC_integral=0
539     for histoX in systhistosarray[Coco]:
540     MC_integral+=histoX.Integral()
541 peller 1.13 printc('green','', 'MC integral = %s'%MC_integral)
542     systhistosarray[Coco], typsX = orderandadd(systhistosarray[Coco],typsX,setup)
543 peller 1.29
544 peller 1.36 if scaling: #rescaling after the sys has been propagated through the BDT with a scaling
545 peller 1.13 for i in range(0,len(systhistosarray[Coco])):
546     for bin in range(0,histos[i].GetSize()):
547     A=systhistosarray[Coco][i].GetBinContent(bin)
548     B=histos[i].GetBinContent(bin)
549     systhistosarray[Coco][i].SetBinContent(bin,B+((A-B)/4.))
550 nmohr 1.24 # finaly lpop over histos
551 peller 1.1 for i in range(0,len(systhistosarray[Coco])):
552 peller 1.29 systhistosarray[Coco][i].SetName('%s%s%s'%(Dict[typs[i]],systematicsnaming[sys],Q))
553 peller 1.5 outfile.cd()
554 peller 1.13 systhistosarray[Coco][i].Write()
555 peller 1.29 histPdf = ROOT.RooDataHist('%s%s%s'%(Dict[typs[i]],systematicsnaming[sys],Q),'%s%s%s'%(Dict[typs[i]],systematicsnaming[sys],Q),obs,systhistosarray[Coco][i])
556 peller 1.1 getattr(WS,'import')(histPdf)
557     Coco+=1
558     WS.writeToFile(outpath+'vhbb_WS_'+ROOToutname+'.root')
559    
560 peller 1.36
561    
562     # -------------------- write DATAcard: ----------------------------------------------------------------------
563 peller 1.29 columns=len(setup)
564    
565 nmohr 1.18 if '8TeV' in options[10]:
566 peller 1.42 pier = open(vhbbpath+'/python/pier8TeV.txt','r')
567 nmohr 1.18 else:
568 peller 1.42 pier = open(vhbbpath+'/python/pier.txt','r')
569 peller 1.1 scalefactors=pier.readlines()
570     pier.close()
571     f = open(outpath+'vhbb_DC_'+ROOToutname+'.txt','w')
572     f.write('imax\t1\tnumber of channels\n')
573 peller 1.29 f.write('jmax\t%s\tnumber of backgrounds (\'*\' = automatic)\n'%(columns-1))
574 peller 1.1 f.write('kmax\t*\tnumber of nuisance parameters (sources of systematical uncertainties)\n\n')
575 peller 1.11 if bdt==True:
576     f.write('shapes * * vhbb_WS_%s.root $CHANNEL:$PROCESS $CHANNEL:$PROCESS$SYSTEMATIC\n\n'%ROOToutname)
577     else:
578     f.write('shapes * * vhbb_TH_%s.root $PROCESS $PROCESS$SYSTEMATIC\n\n'%ROOToutname)
579     f.write('bin\t%s\n\n'%Datacradbin)
580 nmohr 1.25 if blind:
581     f.write('observation\t%s\n\n'%(hDummy.Integral()))
582     else:
583     f.write('observation\t%s\n\n'%(int(d1.Integral())))
584 peller 1.14
585 peller 1.29 f.write('bin')
586     for c in range(0,columns): f.write('\t%s'%Datacradbin)
587     f.write('\n')
588    
589     f.write('process')
590     for c in setup: f.write('\t%s'%Dict[c])
591     f.write('\n')
592    
593     f.write('process')
594     for c in range(0,columns): f.write('\t%s'%c)
595     f.write('\n')
596    
597     f.write('rate')
598     for c in range(0,columns): f.write('\t%s'%histos[c].Integral())
599     f.write('\n')
600    
601     InUse=eval(config.get('Datacard','InUse'))
602     #Parse from config
603     for item in InUse:
604     f.write(item)
605     what=eval(config.get('Datacard',item))
606     f.write('\t%s'%what['type'])
607     for c in setup:
608     if c in what:
609     if item == 'CMS_eff_e' and 'Zmm' in options[10]: f.write('\t-')
610     elif item == 'CMS_eff_m' and 'Zee' in options[10]: f.write('\t-')
611     elif item == 'CMS_trigger_e' and 'Zmm' in options[10]: f.write('\t-')
612     elif item == 'CMS_trigger_m' and 'Zee' in options[10]: f.write('\t-')
613     else:
614     f.write('\t%s'%what[c])
615     else:
616     f.write('\t-')
617     f.write('\n')
618    
619     #Write shape stats and sys
620 peller 1.35 if binstat:
621     for c in setup:
622     for bin in range(0,nBins):
623     f.write('CMS_vhbb_stats_%s_%s_%s\tshape'%(Dict[c], bin, options[10]))
624     for it in range(0,columns):
625     if it == setup.index(c):
626     f.write('\t1.0')
627     else:
628     f.write('\t-')
629     f.write('\n')
630    
631     else:
632     for c in setup:
633     f.write('CMS_vhbb_stats_%s_%s\tshape'%(Dict[c], options[10]))
634     for it in range(0,columns):
635     if it == setup.index(c):
636     f.write('\t1.0')
637     else:
638     f.write('\t-')
639     f.write('\n')
640 peller 1.29
641 peller 1.33 if weightF_sys:
642 peller 1.50 f.write('UEPS\tshape')
643 peller 1.33 for it in range(0,columns): f.write('\t1.0')
644     f.write('\n')
645    
646 nmohr 1.37 if addSample_sys:
647 nmohr 1.51 alreadyAdded = []
648 nmohr 1.37 for newSample in addSample_sys.iterkeys():
649 nmohr 1.51 for c in setup:
650     if not c == Group[newSample]: continue
651     if Dict[c] in alreadyAdded: continue
652 nmohr 1.37 f.write('CMS_vhbb_model_%s\tshape'%(Dict[c]))
653     for it in range(0,columns):
654     if it == setup.index(c):
655     f.write('\t1.0')
656     else:
657     f.write('\t-')
658     f.write('\n')
659 nmohr 1.51 alreadyAdded.append(Dict[c])
660 peller 1.33
661 peller 1.45 if scaling: sys_factor=1.0
662 peller 1.29 else: sys_factor=1.0
663     for sys in systematics:
664     f.write('%s\tshape'%systematicsnaming[sys])
665     for c in range(0,columns): f.write('\t%s'%sys_factor)
666     f.write('\n')
667 peller 1.1 f.close()
668 nmohr 1.31 outfile.Close()