ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/workspace_datacard.py
Revision: 1.54
Committed: Thu Oct 18 16:08:13 2012 UTC (12 years, 6 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.53: +14 -37 lines
Log Message:
better forloop and dataadding

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    
115 nmohr 1.24
116 peller 1.53 class Rebinner:
117     def __init__(self,nBins,lowedgearray):
118     self.lowedgearray=lowedgearray
119     self.nBins=nBins
120     def rebin(self, histo):
121     histo.Rebin(self.nBins,'hnew',self.lowedgearray)
122     binhisto=ROOT.gDirectory.Get('hnew')
123     newhisto=ROOT.TH1F('new','new',self.nBins,self.lowedgearray[0],self.lowedgearray[-1])
124     for bin in range(0,self.nBins+1):
125     newhisto.SetBinContent(bin,binhisto.GetBinContent(bin))
126     newhisto.SetBinError(bin,binhisto.GetBinError(bin))
127     newhisto.SetName(binhisto.GetName())
128     newhisto.SetTitle(binhisto.GetTitle())
129     return newhisto
130    
131    
132 peller 1.23
133 peller 1.36 # -------------------- generate the Workspace with all Histograms: ----------------------------------------------------------------------
134 peller 1.11
135     WS = ROOT.RooWorkspace('%s'%Datacradbin,'%s'%Datacradbin) #Zee
136 peller 1.1 print 'WS initialized'
137 bortigno 1.7 disc= ROOT.RooRealVar(name,name,xMin,xMax)
138 peller 1.1 obs = ROOT.RooArgList(disc)
139     ROOT.gROOT.SetStyle("Plain")
140     datas = []
141     datatyps =[]
142     histos = []
143     typs = []
144 nmohr 1.37 hNames = []
145 nmohr 1.39 aNames = []
146 peller 1.1 statUps=[]
147     statDowns=[]
148 nmohr 1.15 if blind:
149 peller 1.29 printc('red','', 'I AM BLINDED!')
150 peller 1.53
151    
152    
153     #---- get the BKG for the rebinning calculation----
154     counterRB=0
155    
156     for job in info:
157     if eval(job.active):
158     if job.subsamples:
159     for subsample in range(0,len(job.subnames)):
160     if job.subnames[subsample] in BKGlist:
161     hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample)
162     if counterRB == 0:
163     hDummyRB = copy(hTemp)
164     else:
165     hDummyRB.Add(hTemp)
166     counterRB += 1
167    
168     else:
169     if job.name in BKGlist:
170     hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor)
171     if counterRB == 0:
172     hDummyRB = copy(hTemp)
173     else:
174     hDummyRB.Add(hTemp)
175     counterRB += 1
176     ErrorR=0
177     ErrorL=0
178     TotR=0
179     TotL=0
180 peller 1.54 binR=nBinsRB
181 peller 1.53 binL=1
182     rel=1.0
183     #---- from right
184     while rel > 0.2:
185     TotR+=hDummyRB.GetBinContent(binR)
186     ErrorR=sqrt(ErrorR**2+hDummyRB.GetBinError(binR)**2)
187     binR-=1
188     if not TotR == 0 and not ErrorR == 0:
189     rel=ErrorR/TotR
190     #print rel
191     print 'upper bin is %s'%binR
192    
193     #---- from left
194     rel=1.0
195     while rel > 0.2:
196     TotL+=hDummyRB.GetBinContent(binL)
197     ErrorL=sqrt(ErrorL**2+hDummyRB.GetBinError(binL)**2)
198     binL+=1
199     if not TotL == 0 and not ErrorL == 0:
200     rel=ErrorL/TotL
201     #print rel
202     print 'lower bin is %s'%binL
203    
204     inbetween=binR-binL
205 peller 1.54 stepsize=int(inbetween)/(int(nBins)-2)
206     modulo = int(inbetween)%(int(nBins)-2)
207 peller 1.53
208     print'stepsize %s'% stepsize
209     print 'modulo %s'%modulo
210    
211     binlist=[0,binL]
212 peller 1.54 for i in range(0,int(nBins)-3):
213 peller 1.53 binlist.append(binlist[-1]+stepsize)
214     binlist[-1]+=modulo
215     binlist.append(binR)
216 peller 1.54 binlist.append(nBinsRB+1)
217 peller 1.53
218     print binlist
219 peller 1.54 myBinning=Rebinner(int(nBins),array('d',[hDummyRB.GetBinLowEdge(i) for i in binlist]))
220 peller 1.53 #--------------------------------------------------
221    
222 peller 1.54 hDummy=myBinning.rebin(hDummyRB)
223 peller 1.53
224    
225 peller 1.33 if weightF_sys:
226 peller 1.48 weightF_sys_UP=config.get('Weights','weightF_sys_UP')
227     weightF_sys_DOWN=config.get('Weights','weightF_sys_DOWN')
228 peller 1.33 weightF_sys_Ups = []
229     weightF_sys_Downs = []
230 nmohr 1.37 if addSample_sys:
231     sTyps = []
232     addSample_sys_histos = []
233     aSample_sys_Ups = []
234     aSample_sys_Downs = []
235 peller 1.33
236 peller 1.1 for job in info:
237 peller 1.28 if eval(job.active):
238 peller 1.29 if job.subsamples:
239     for subsample in range(0,len(job.subnames)):
240     if job.subnames[subsample] in BKGlist:
241 peller 1.45 print 'getting %s'%job.subnames[subsample]
242 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample)
243 peller 1.45 print hTemp.Integral()
244 peller 1.53 histos.append(myBinning.rebin(hTemp))
245 peller 1.29 typs.append(Group[job.subnames[subsample]])
246 nmohr 1.37 hNames.append(job.subnames[subsample])
247 peller 1.33 if weightF_sys:
248 peller 1.48 hTempWU, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample,'weightF_sys_UP')
249 peller 1.53 weightF_sys_Ups.append(myBinning.rebin(hTempWU))
250 peller 1.48 hTempWD, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample,'weightF_sys_DOWN')
251 peller 1.53 weightF_sys_Downs.append(myBinning.rebin(hTempWD))
252 peller 1.48
253 peller 1.29 elif job.subnames[subsample] == SIG:
254 peller 1.45 hNames.append(job.subnames[subsample])
255     print 'getting %s'%job.subnames[subsample]
256 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample)
257 peller 1.45 print hTemp.Integral()
258 peller 1.53 histos.append(myBinning.rebin(hTemp))
259 peller 1.29 typs.append(Group[job.subnames[subsample]])
260 peller 1.33 if weightF_sys:
261 peller 1.48 hTempWU, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample,'weightF_sys_UP')
262 peller 1.53 weightF_sys_Ups.append(myBinning.rebin(hTempWU))
263 peller 1.48 hTempWD, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample,'weightF_sys_DOWN')
264 peller 1.53 weightF_sys_Downs.append(myBinning.rebin(hTempWD))
265 nmohr 1.41 if addSample_sys and job.subnames[subsample] in addSample_sys.values():
266 nmohr 1.40 aNames.append(job.subnames[subsample])
267 nmohr 1.51 hTempS, s_ = getHistoFromTree(job,path,config,options,MC_rescale_factor,subsample)
268 peller 1.53 addSample_sys_histos.append(myBinning.rebin(hTempS))
269 peller 1.29
270     else:
271     if job.name in BKGlist:
272     #print job.getpath()
273 peller 1.45 print 'getting %s'%job.name
274 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor)
275 peller 1.53 histos.append(myBinning.rebin(hTemp))
276 peller 1.45 print hTemp.Integral()
277 peller 1.33 typs.append(Group[job.name])
278 peller 1.42 hNames.append(job.name)
279 peller 1.33 if weightF_sys:
280 peller 1.48 hTempWU, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,-1,'weightF_sys_UP')
281 peller 1.53 weightF_sys_Ups.append(myBinning.rebin(hTempWU))
282 peller 1.48 hTempWD, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,-1,'weightF_sys_DOWN')
283 peller 1.53 weightF_sys_Downs.append((hTempWD))
284 peller 1.33
285 peller 1.29 elif job.name == SIG:
286 peller 1.45 print 'getting %s'%job.name
287 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options,MC_rescale_factor)
288 peller 1.53 histos.append(myBinning.rebin(hTemp))
289 peller 1.45 print hTemp.Integral()
290 peller 1.33 typs.append(Group[job.name])
291 nmohr 1.37 hNames.append(job.name)
292 peller 1.33 if weightF_sys:
293 peller 1.48 hTempWU, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,-1,'weightF_sys_UP')
294 peller 1.53 weightF_sys_Ups.append(myBinning.rebin(hTempWU))
295 peller 1.48 hTempWD, _ = getHistoFromTree(job,path,config,options,MC_rescale_factor,-1,'weightF_sys_DOWN')
296 peller 1.53 weightF_sys_Downs.append(myBinning.rebin(hTempWD))
297 peller 1.33
298 peller 1.29 elif job.name in data:
299     #print 'DATA'
300 peller 1.45 print 'getting %s'%job.name
301 nmohr 1.31 hTemp, typ = getHistoFromTree(job,path,config,options)
302 peller 1.53 datas.append(myBinning.rebin(hTemp))
303 peller 1.45 print hTemp.Integral()
304 peller 1.29 datatyps.append(typ)
305 nmohr 1.40
306 peller 1.43 if addSample_sys and job.name in addSample_sys.values():
307 peller 1.42 aNames.append(job.name)
308     hTempS, s_ = getHistoFromTree(job,path,config,options,MC_rescale_factor)
309 peller 1.53 addSample_sys_histos.append(myBinning.rebin(hTempS))
310 peller 1.1
311     MC_integral=0
312     MC_entries=0
313     for histo in histos:
314     MC_integral+=histo.Integral()
315 peller 1.45 print 'histo integral %s'%histo.Integral()
316 peller 1.36 printc('green','', 'MC integral = %s'%MC_integral)
317    
318 nmohr 1.37 def getAlternativeShapes(histos,altHistos,hNames,aNames,addSample_sys):
319 nmohr 1.51 theHistosUp = []
320     theHistosDown = []
321     for histo in histos:
322     theHistosUp.append(histo.Clone())
323     theHistosDown.append(histo.Clone())
324 nmohr 1.37 for name in addSample_sys.keys():
325     print name
326 nmohr 1.51 hVar = altHistos[aNames.index(addSample_sys[name])].Clone()
327     hNom = histos[hNames.index(name)].Clone()
328     hAlt = hNom.Clone()
329     hNom.Add(hVar,-1.)
330     hAlt.Add(hNom)
331     for bin in range(0,nBins):
332     if hAlt.GetBinContent(bin) < 0.: hAlt.SetBinContent(bin,0.)
333     theHistosUp[hNames.index(name)] = hVar.Clone()
334     theHistosDown[hNames.index(name)] = hAlt.Clone()
335 nmohr 1.37 return theHistosUp, theHistosDown
336    
337 peller 1.13 #order and add together
338 peller 1.33 typs2=copy(typs)
339 nmohr 1.37 typs3=copy(typs)
340     typs4=copy(typs)
341 peller 1.48 typs5=copy(typs)
342 nmohr 1.40 if addSample_sys:
343     aSampleUp, aSampleDown = getAlternativeShapes(histos,addSample_sys_histos,hNames,aNames,addSample_sys)
344 peller 1.13 histos, typs = orderandadd(histos,typs,setup)
345 nmohr 1.37 if weightF_sys:
346 peller 1.48 weightF_sys_Ups,_=orderandadd(weightF_sys_Ups,typs2,setup)
347     weightF_sys_Downs,_=orderandadd(weightF_sys_Downs,typs3,setup)
348    
349 nmohr 1.37 if addSample_sys:
350 peller 1.48 aSampleUp,aNames=orderandadd(aSampleUp,typs4,setup)
351     aSampleDown,aNames=orderandadd(aSampleDown,typs5,setup)
352 peller 1.1
353     for i in range(0,len(histos)):
354 peller 1.29 newname=Dict[typs[i]]
355     histos[i].SetName(newname)
356 peller 1.5 #histos[i].SetDirectory(outfile)
357     outfile.cd()
358 peller 1.1 histos[i].Write()
359 nmohr 1.22 errorsum=0
360     total=0
361     for j in range(histos[i].GetNbinsX()+1):
362     errorsum=errorsum+(histos[i].GetBinError(j))**2
363     errorsum=sqrt(errorsum)
364     total=histos[i].Integral()
365 peller 1.35
366 peller 1.36 if binstat: #treating statistics in single bins
367 peller 1.35 for bin in range(0,nBins):
368     statUps.append(histos[i].Clone())
369     statDowns.append(histos[i].Clone())
370     statUps[i*nBins+bin].SetName('%sCMS_vhbb_stats_%s_%s_%sUp'%(newname,newname,bin,options[10]))
371     statDowns[i*nBins+bin].SetName('%sCMS_vhbb_stats_%s_%s_%sDown'%(newname,newname,bin,options[10]))
372     #shift up and down with statistical error
373     if rescaleSqrtN:
374     statUps[i*nBins+bin].SetBinContent(bin,statUps[i*nBins+bin].GetBinContent(bin)+statUps[i*nBins+bin].GetBinError(bin)/total*errorsum)
375     statDowns[i*nBins+bin].SetBinContent(bin,statDowns[i*nBins+bin].GetBinContent(bin)-statDowns[i*nBins+bin].GetBinError(bin)/total*errorsum)
376     else:
377     statUps[i*nBins+bin].SetBinContent(bin,statUps[i*nBins+bin].GetBinContent(bin)+statUps[i*nBins+bin].GetBinError(bin))
378     statDowns[i*nBins+bin].SetBinContent(bin,statDowns[i*nBins+bin].GetBinContent(bin)-statDowns[i*nBins+bin].GetBinError(bin))
379     statUps[i*nBins+bin].Write()
380     statDowns[i*nBins+bin].Write()
381     histPdf = ROOT.RooDataHist(newname,newname,obs,histos[i])
382     #UP stats of MCs
383     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])
384     #DOWN stats of MCs
385     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])
386     getattr(WS,'import')(histPdf)
387     getattr(WS,'import')(RooStatsUp)
388     getattr(WS,'import')(RooStatsDown)
389 peller 1.13
390 peller 1.35 else:
391     statUps.append(histos[i].Clone())
392     statDowns.append(histos[i].Clone())
393     statUps[i].SetName('%sCMS_vhbb_stats_%s_%sUp'%(newname,newname,options[10]))
394     statDowns[i].SetName('%sCMS_vhbb_stats_%s_%sDown'%(newname,newname,options[10]))
395     #shift up and down with statistical error
396     for j in range(histos[i].GetNbinsX()+1):
397     if rescaleSqrtN:
398     statUps[i].SetBinContent(j,statUps[i].GetBinContent(j)+statUps[i].GetBinError(j)/total*errorsum)
399     statDowns[i].SetBinContent(j,statDowns[i].GetBinContent(j)-statDowns[i].GetBinError(j)/total*errorsum)
400     else:
401     statUps[i].SetBinContent(j,statUps[i].GetBinContent(j)+statUps[i].GetBinError(j))
402     statDowns[i].SetBinContent(j,statDowns[i].GetBinContent(j)-statDowns[i].GetBinError(j))
403 peller 1.47 #if statDowns[i].GetBinError(j)<0.: statDowns[i].SetBinContent(j,0.)
404 peller 1.35 statUps[i].Write()
405     statDowns[i].Write()
406     histPdf = ROOT.RooDataHist(newname,newname,obs,histos[i])
407     #UP stats of MCs
408     RooStatsUp = ROOT.RooDataHist('%sCMS_vhbb_stats_%s_%sUp'%(newname,newname,options[10]),'%sCMS_vhbb_stats_%s_%sUp'%(newname,newname,options[10]),obs, statUps[i])
409     #DOWN stats of MCs
410     RooStatsDown = ROOT.RooDataHist('%sCMS_vhbb_stats_%s_%sDown'%(newname,newname,options[10]),'%sCMS_vhbb_stats_%s_%sDown'%(newname,newname,options[10]),obs, statDowns[i])
411     getattr(WS,'import')(histPdf)
412     getattr(WS,'import')(RooStatsUp)
413     getattr(WS,'import')(RooStatsDown)
414    
415     #And now WeightF sys
416 peller 1.33 if weightF_sys:
417 peller 1.49 weightF_sys_Downs[i].SetName('%sUEPSDown'%(newname))
418     weightF_sys_Ups[i].SetName('%sUEPSUp'%(newname))
419 peller 1.33 weightF_sys_Ups[i].Write()
420 peller 1.35 weightF_sys_Downs[i].Write()
421 peller 1.49 RooWeightFUp = ROOT.RooDataHist('%sUEPSUp'%(newname),'%sUEPSUp'%(newname),obs, weightF_sys_Ups[i])
422     RooWeightFDown = ROOT.RooDataHist('%sUEPSDown'%(newname),'%sUEPSDown'%(newname),obs, weightF_sys_Downs[i])
423 peller 1.33 getattr(WS,'import')(RooWeightFUp)
424     getattr(WS,'import')(RooWeightFDown)
425 nmohr 1.37 #And now Additional sample sys
426     if addSample_sys:
427     aSample_sys_Downs.append(aSampleUp[i].Clone())
428     aSample_sys_Ups.append(aSampleDown[i].Clone())
429     aSample_sys_Downs[i].SetName('%sCMS_vhbb_model_%sDown'%(newname,newname))
430     aSample_sys_Ups[i].SetName('%sCMS_vhbb_model_%sUp'%(newname,newname))
431     aSample_sys_Ups[i].Write()
432     aSample_sys_Downs[i].Write()
433     RooSampleUp = ROOT.RooDataHist('%sCMS_vhbb_model_%sUp'%(newname,newname),'%sCMS_vhbb_model_%sUp'%(newname,newname),obs, aSample_sys_Ups[i])
434     RooSampleDown = ROOT.RooDataHist('%sCMS_vhbb_model_%sDown'%(newname,newname),'%sCMS_vhbb_model_%sDown'%(newname,newname),obs, aSample_sys_Downs[i])
435     getattr(WS,'import')(RooSampleUp)
436     getattr(WS,'import')(RooSampleDown)
437 peller 1.1
438    
439     #HISTOGRAMM of DATA
440 peller 1.54 d1 = datas[0]
441     if len(datas)>1:
442     for i in range(1,len(datas)):
443     d1.Add(datas[i],1)
444 peller 1.13 printc('green','','\nDATA integral = %s\n'%d1.Integral())
445 peller 1.1 flow = d1.GetEntries()-d1.Integral()
446     if flow > 0:
447 peller 1.13 printc('red','','U/O flow: %s'%flow)
448 peller 1.29 d1.SetName(Dict['Data'])
449 peller 1.5 outfile.cd()
450 peller 1.47 #d1.Write()
451    
452 peller 1.36
453 nmohr 1.15 if blind:
454 peller 1.47 print 'toy data integral: %s'%hDummy.Integral()
455 peller 1.29 hDummy.SetName(Dict['Data'])
456     histPdf = ROOT.RooDataHist(Dict['Data'],Dict['Data'],obs,hDummy)
457 nmohr 1.25 #rooDummy = ROOT.RooDataHist('data_obs','data_obs',obs,hDummy)
458     #toy = ROOT.RooHistPdf('data_obs','data_obs',ROOT.RooArgSet(obs),rooDummy)
459     #rooDataSet = toy.generate(ROOT.RooArgSet(obs),int(d1.Integral()))
460     #histPdf = ROOT.RooDataHist('data_obs','data_obs',ROOT.RooArgSet(obs),rooDataSet.reduce(ROOT.RooArgSet(obs)))
461 peller 1.47 hDummy.Write()
462 nmohr 1.15 else:
463 peller 1.29 histPdf = ROOT.RooDataHist(Dict['Data'],Dict['Data'],obs,d1)
464 peller 1.47 d1.Write()
465 peller 1.1 #ROOT.RooAbsData.plotOn(histPdf,frame)
466     getattr(WS,'import')(histPdf)
467    
468     #SYSTEMATICS:
469     UD = ['Up','Down']
470     systhistosarray=[]
471 peller 1.13 Coco=0 #iterates over (all systematics) * (up,down)
472 nmohr 1.22 nominalShape = options[0]
473 bortigno 1.7
474     for sys in systematics:
475 peller 1.36 for Q in UD: # Q = 'Up' and 'Down'
476 peller 1.32 #options[7] ist der CutString name
477     new_cut=sys_cut_suffix[sys]
478     new_options = copy(options)
479     if not new_cut == 'nominal':
480 peller 1.34 old_str,new_str=new_cut.split('>')
481     new_options[7]=[options[7],old_str,new_str.replace('?',Q)]
482 peller 1.1 ff=options[0].split('.')
483 bortigno 1.7 if bdt == True:
484 peller 1.52 #options[0] ist die treeVar
485 peller 1.13 ff[1]='%s_%s'%(sys,Q.lower())
486 peller 1.32 new_options[0]=nominalShape.replace('.nominal','.%s_%s'%(sys,Q.lower()))
487 bortigno 1.7 elif mjj == True:
488 peller 1.52 if sys == 'JER' or sys == 'JES':
489     ff[0]='H_%s'%(sys)
490     ff[1]='mass_%s'%(Q.lower())
491     new_options[0]='.'.join(ff)
492     else: pass
493 peller 1.1
494 peller 1.8 print '\n'
495 peller 1.13 printc('blue','','\t--> doing systematic %s %s'%(sys,Q.lower()))
496 peller 1.1
497     systhistosarray.append([])
498     typsX = []
499    
500     for job in info:
501 peller 1.29 if eval(job.active):
502     if job.subsamples:
503     for subsample in range(0,len(job.subnames)):
504     if job.subnames[subsample] in BKGlist:
505 peller 1.32 hTemp, typ = getHistoFromTree(job,path,config,new_options,MC_rescale_factor,subsample)
506 peller 1.53 systhistosarray[Coco].append(myBinning.rebin(hTemp))
507 peller 1.29 typsX.append(Group[job.subnames[subsample]])
508     elif job.subnames[subsample] == SIG:
509 peller 1.32 hTemp, typ = getHistoFromTree(job,path,config,new_options,MC_rescale_factor,subsample)
510 peller 1.53 systhistosarray[Coco].append(myBinning.rebin(hTemp))
511 peller 1.29 typsX.append(Group[job.subnames[subsample]])
512    
513     else:
514     if job.name in BKGlist:
515 peller 1.32 hTemp, typ = getHistoFromTree(job,path,config,new_options,MC_rescale_factor)
516 peller 1.53 systhistosarray[Coco].append(myBinning.rebin(hTemp))
517 peller 1.29 typsX.append(Group[job.name])
518     elif job.name == SIG:
519 peller 1.32 hTemp, typ = getHistoFromTree(job,path,config,new_options,MC_rescale_factor)
520 peller 1.53 systhistosarray[Coco].append(myBinning.rebin(hTemp))
521 peller 1.29 typsX.append(Group[job.name])
522    
523 peller 1.1 MC_integral=0
524     for histoX in systhistosarray[Coco]:
525     MC_integral+=histoX.Integral()
526 peller 1.13 printc('green','', 'MC integral = %s'%MC_integral)
527     systhistosarray[Coco], typsX = orderandadd(systhistosarray[Coco],typsX,setup)
528 peller 1.29
529 peller 1.36 if scaling: #rescaling after the sys has been propagated through the BDT with a scaling
530 peller 1.13 for i in range(0,len(systhistosarray[Coco])):
531     for bin in range(0,histos[i].GetSize()):
532     A=systhistosarray[Coco][i].GetBinContent(bin)
533     B=histos[i].GetBinContent(bin)
534     systhistosarray[Coco][i].SetBinContent(bin,B+((A-B)/4.))
535 nmohr 1.24 # finaly lpop over histos
536 peller 1.1 for i in range(0,len(systhistosarray[Coco])):
537 peller 1.29 systhistosarray[Coco][i].SetName('%s%s%s'%(Dict[typs[i]],systematicsnaming[sys],Q))
538 peller 1.5 outfile.cd()
539 peller 1.13 systhistosarray[Coco][i].Write()
540 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])
541 peller 1.1 getattr(WS,'import')(histPdf)
542     Coco+=1
543     WS.writeToFile(outpath+'vhbb_WS_'+ROOToutname+'.root')
544    
545 peller 1.36
546    
547     # -------------------- write DATAcard: ----------------------------------------------------------------------
548 peller 1.29 columns=len(setup)
549    
550 nmohr 1.18 if '8TeV' in options[10]:
551 peller 1.42 pier = open(vhbbpath+'/python/pier8TeV.txt','r')
552 nmohr 1.18 else:
553 peller 1.42 pier = open(vhbbpath+'/python/pier.txt','r')
554 peller 1.1 scalefactors=pier.readlines()
555     pier.close()
556     f = open(outpath+'vhbb_DC_'+ROOToutname+'.txt','w')
557     f.write('imax\t1\tnumber of channels\n')
558 peller 1.29 f.write('jmax\t%s\tnumber of backgrounds (\'*\' = automatic)\n'%(columns-1))
559 peller 1.1 f.write('kmax\t*\tnumber of nuisance parameters (sources of systematical uncertainties)\n\n')
560 peller 1.11 if bdt==True:
561     f.write('shapes * * vhbb_WS_%s.root $CHANNEL:$PROCESS $CHANNEL:$PROCESS$SYSTEMATIC\n\n'%ROOToutname)
562     else:
563     f.write('shapes * * vhbb_TH_%s.root $PROCESS $PROCESS$SYSTEMATIC\n\n'%ROOToutname)
564     f.write('bin\t%s\n\n'%Datacradbin)
565 nmohr 1.25 if blind:
566     f.write('observation\t%s\n\n'%(hDummy.Integral()))
567     else:
568     f.write('observation\t%s\n\n'%(int(d1.Integral())))
569 peller 1.14
570 peller 1.29 f.write('bin')
571     for c in range(0,columns): f.write('\t%s'%Datacradbin)
572     f.write('\n')
573    
574     f.write('process')
575     for c in setup: f.write('\t%s'%Dict[c])
576     f.write('\n')
577    
578     f.write('process')
579     for c in range(0,columns): f.write('\t%s'%c)
580     f.write('\n')
581    
582     f.write('rate')
583     for c in range(0,columns): f.write('\t%s'%histos[c].Integral())
584     f.write('\n')
585    
586     InUse=eval(config.get('Datacard','InUse'))
587     #Parse from config
588     for item in InUse:
589     f.write(item)
590     what=eval(config.get('Datacard',item))
591     f.write('\t%s'%what['type'])
592     for c in setup:
593     if c in what:
594     if item == 'CMS_eff_e' and 'Zmm' in options[10]: f.write('\t-')
595     elif item == 'CMS_eff_m' and 'Zee' in options[10]: f.write('\t-')
596     elif item == 'CMS_trigger_e' and 'Zmm' in options[10]: f.write('\t-')
597     elif item == 'CMS_trigger_m' and 'Zee' in options[10]: f.write('\t-')
598     else:
599     f.write('\t%s'%what[c])
600     else:
601     f.write('\t-')
602     f.write('\n')
603    
604     #Write shape stats and sys
605 peller 1.35 if binstat:
606     for c in setup:
607     for bin in range(0,nBins):
608     f.write('CMS_vhbb_stats_%s_%s_%s\tshape'%(Dict[c], bin, options[10]))
609     for it in range(0,columns):
610     if it == setup.index(c):
611     f.write('\t1.0')
612     else:
613     f.write('\t-')
614     f.write('\n')
615    
616     else:
617     for c in setup:
618     f.write('CMS_vhbb_stats_%s_%s\tshape'%(Dict[c], options[10]))
619     for it in range(0,columns):
620     if it == setup.index(c):
621     f.write('\t1.0')
622     else:
623     f.write('\t-')
624     f.write('\n')
625 peller 1.29
626 peller 1.33 if weightF_sys:
627 peller 1.50 f.write('UEPS\tshape')
628 peller 1.33 for it in range(0,columns): f.write('\t1.0')
629     f.write('\n')
630    
631 nmohr 1.37 if addSample_sys:
632 nmohr 1.51 alreadyAdded = []
633 nmohr 1.37 for newSample in addSample_sys.iterkeys():
634 nmohr 1.51 for c in setup:
635     if not c == Group[newSample]: continue
636     if Dict[c] in alreadyAdded: continue
637 nmohr 1.37 f.write('CMS_vhbb_model_%s\tshape'%(Dict[c]))
638     for it in range(0,columns):
639     if it == setup.index(c):
640     f.write('\t1.0')
641     else:
642     f.write('\t-')
643     f.write('\n')
644 nmohr 1.51 alreadyAdded.append(Dict[c])
645 peller 1.33
646 peller 1.45 if scaling: sys_factor=1.0
647 peller 1.29 else: sys_factor=1.0
648     for sys in systematics:
649     f.write('%s\tshape'%systematicsnaming[sys])
650     for c in range(0,columns): f.write('\t%s'%sys_factor)
651     f.write('\n')
652 peller 1.1 f.close()
653 nmohr 1.31 outfile.Close()