1 |
import ROOT
|
2 |
ROOT.gROOT.SetBatch(True)
|
3 |
import sys,os
|
4 |
from BetterConfigParser import BetterConfigParser
|
5 |
import TdrStyles
|
6 |
from Ratio import getRatio
|
7 |
from HistoMaker import HistoMaker
|
8 |
|
9 |
class StackMaker:
|
10 |
def __init__(self, config, var,region,SignalRegion,setup=None):
|
11 |
section='Plot:%s'%region
|
12 |
self.var = var
|
13 |
self.SignalRegion=SignalRegion
|
14 |
self.region = region
|
15 |
self.normalize = eval(config.get(section,'Normalize'))
|
16 |
self.log = eval(config.get(section,'log'))
|
17 |
if config.has_option('plotDef:%s'%var,'log') and not self.log:
|
18 |
self.log = eval(config.get('plotDef:%s'%var,'log'))
|
19 |
self.blind = eval(config.get(section,'blind'))
|
20 |
if setup is None:
|
21 |
self.setup=config.get('Plot_general','setup')
|
22 |
if self.log:
|
23 |
self.setup=config.get('Plot_general','setupLog')
|
24 |
self.setup=self.setup.split(',')
|
25 |
else:
|
26 |
self.setup=setup
|
27 |
if not SignalRegion:
|
28 |
if 'ZH' in self.setup:
|
29 |
self.setup.remove('ZH')
|
30 |
if 'WH' in self.setup:
|
31 |
self.setup.remove('WH')
|
32 |
self.rebin = 1
|
33 |
if config.has_option(section,'rebin'):
|
34 |
self.rebin = eval(config.get(section,'rebin'))
|
35 |
if config.has_option(section,'nBins'):
|
36 |
self.nBins = int(eval(config.get(section,'nBins'))/self.rebin)
|
37 |
else:
|
38 |
self.nBins = int(eval(config.get('plotDef:%s'%var,'nBins'))/self.rebin)
|
39 |
print self.nBins
|
40 |
if config.has_option(section,'min'):
|
41 |
self.xMin = eval(config.get(section,'min'))
|
42 |
else:
|
43 |
self.xMin = eval(config.get('plotDef:%s'%var,'min'))
|
44 |
if config.has_option(section,'max'):
|
45 |
self.xMax = eval(config.get(section,'max'))
|
46 |
else:
|
47 |
self.xMax = eval(config.get('plotDef:%s'%var,'max'))
|
48 |
self.name = config.get('plotDef:%s'%var,'relPath')
|
49 |
self.mass = config.get(section,'Signal')
|
50 |
data = config.get(section,'Datas')
|
51 |
if '<mass>' in self.name:
|
52 |
self.name = self.name.replace('<mass>',self.mass)
|
53 |
print self.name
|
54 |
if config.has_option('Cuts',region):
|
55 |
cut = config.get('Cuts',region)
|
56 |
else:
|
57 |
cut = None
|
58 |
if config.has_option(section, 'Datacut'):
|
59 |
cut=config.get(section, 'Datacut')
|
60 |
if config.has_option(section, 'doFit'):
|
61 |
self.doFit=eval(config.get(section, 'doFit'))
|
62 |
else:
|
63 |
self.doFit = False
|
64 |
|
65 |
self.colorDict=eval(config.get('Plot_general','colorDict'))
|
66 |
self.typLegendDict=eval(config.get('Plot_general','typLegendDict'))
|
67 |
self.anaTag = config.get("Analysis","tag")
|
68 |
self.xAxis = config.get('plotDef:%s'%var,'xAxis')
|
69 |
self.options = {'var': self.name,'name':'','xAxis': self.xAxis, 'nBins': self.nBins, 'xMin': self.xMin, 'xMax': self.xMax,'pdfName': '%s_%s_%s.pdf'%(region,var,self.mass),'cut':cut,'mass': self.mass, 'data': data, 'blind': self.blind}
|
70 |
if config.has_option('Weights','weightF'):
|
71 |
self.options['weight'] = config.get('Weights','weightF')
|
72 |
else:
|
73 |
self.options['weight'] = None
|
74 |
self.plotDir = config.get('Directories','plotpath')
|
75 |
self.maxRatioUncert = 0.5
|
76 |
if self.SignalRegion:
|
77 |
self.maxRatioUncert = 1000.
|
78 |
self.config = config
|
79 |
self.datas = None
|
80 |
self.datatyps = None
|
81 |
self.overlay = None
|
82 |
self.lumi = None
|
83 |
self.histos = None
|
84 |
self.typs = None
|
85 |
self.AddErrors = None
|
86 |
print self.setup
|
87 |
|
88 |
@staticmethod
|
89 |
def myText(txt="CMS Preliminary",ndcX=0,ndcY=0,size=0.8):
|
90 |
ROOT.gPad.Update()
|
91 |
text = ROOT.TLatex()
|
92 |
text.SetNDC()
|
93 |
text.SetTextColor(ROOT.kBlack)
|
94 |
text.SetTextSize(text.GetTextSize()*size)
|
95 |
text.DrawLatex(ndcX,ndcY,txt)
|
96 |
return text
|
97 |
|
98 |
def doCompPlot(self,aStack,l):
|
99 |
c = ROOT.TCanvas(self.var+'Comp','',600,600)
|
100 |
c.SetFillStyle(4000)
|
101 |
c.SetFrameFillStyle(1000)
|
102 |
c.SetFrameFillColor(0)
|
103 |
k=len(self.histos)
|
104 |
l.Clear()
|
105 |
maximum = 0.
|
106 |
for j in range(0,k):
|
107 |
#print histos[j].GetBinContent(1)
|
108 |
i=k-j-1
|
109 |
self.histos[i].SetLineColor(int(self.colorDict[self.typs[i]]))
|
110 |
self.histos[i].SetFillColor(0)
|
111 |
self.histos[i].SetLineWidth(3)
|
112 |
if self.histos[i].Integral() > 0.:
|
113 |
self.histos[i].Scale(1./self.histos[i].Integral())
|
114 |
if self.histos[i].GetMaximum() > maximum:
|
115 |
maximum = self.histos[i].GetMaximum()
|
116 |
l.AddEntry(self.histos[j],self.typLegendDict[self.typs[j]],'l')
|
117 |
aStack.SetMinimum(0.)
|
118 |
aStack.SetMaximum(maximum*1.5)
|
119 |
aStack.GetXaxis().SetTitle(self.xAxis)
|
120 |
aStack.Draw('HISTNOSTACK')
|
121 |
if self.overlay:
|
122 |
if self.overlay.Integral() > 0.:
|
123 |
self.overlay.Scale(1./self.overlay.Integral())
|
124 |
self.overlay.Draw('hist,same')
|
125 |
l.AddEntry(self.overlay,self.typLegendDict['Overlay'],'L')
|
126 |
l.Draw()
|
127 |
name = '%s/comp_%s' %(self.plotDir,self.options['pdfName'])
|
128 |
c.Print(name)
|
129 |
|
130 |
def doPlot(self):
|
131 |
TdrStyles.tdrStyle()
|
132 |
histo_dict = HistoMaker.orderandadd([{self.typs[i]:self.histos[i]} for i in range(len(self.histos))],self.setup)
|
133 |
#sort
|
134 |
print histo_dict
|
135 |
self.histos=[histo_dict[key] for key in self.setup]
|
136 |
self.typs=self.setup
|
137 |
|
138 |
c = ROOT.TCanvas(self.var,'', 600, 600)
|
139 |
c.SetFillStyle(4000)
|
140 |
c.SetFrameFillStyle(1000)
|
141 |
c.SetFrameFillColor(0)
|
142 |
|
143 |
oben = ROOT.TPad('oben','oben',0,0.3 ,1.0,1.0)
|
144 |
oben.SetBottomMargin(0)
|
145 |
oben.SetFillStyle(4000)
|
146 |
oben.SetFrameFillStyle(1000)
|
147 |
oben.SetFrameFillColor(0)
|
148 |
unten = ROOT.TPad('unten','unten',0,0.0,1.0,0.3)
|
149 |
unten.SetTopMargin(0.)
|
150 |
unten.SetBottomMargin(0.35)
|
151 |
unten.SetFillStyle(4000)
|
152 |
unten.SetFrameFillStyle(1000)
|
153 |
unten.SetFrameFillColor(0)
|
154 |
|
155 |
oben.Draw()
|
156 |
unten.Draw()
|
157 |
|
158 |
oben.cd()
|
159 |
allStack = ROOT.THStack(self.var,'')
|
160 |
l = ROOT.TLegend(0.45, 0.6,0.75,0.92)
|
161 |
l.SetLineWidth(2)
|
162 |
l.SetBorderSize(0)
|
163 |
l.SetFillColor(0)
|
164 |
l.SetFillStyle(4000)
|
165 |
l.SetTextFont(62)
|
166 |
l.SetTextSize(0.035)
|
167 |
l_2 = ROOT.TLegend(0.68, 0.6,0.92,0.92)
|
168 |
l_2.SetLineWidth(2)
|
169 |
l_2.SetBorderSize(0)
|
170 |
l_2.SetFillColor(0)
|
171 |
l_2.SetFillStyle(4000)
|
172 |
l_2.SetTextFont(62)
|
173 |
l_2.SetTextSize(0.035)
|
174 |
MC_integral=0
|
175 |
MC_entries=0
|
176 |
|
177 |
for histo in self.histos:
|
178 |
MC_integral+=histo.Integral()
|
179 |
print "\033[1;32m\n\tMC integral = %s\033[1;m"%MC_integral
|
180 |
|
181 |
#ORDER AND ADD TOGETHER
|
182 |
|
183 |
if not 'DYc' in self.typs: self.typLegendDict.update({'DYlight':self.typLegendDict['DYlc']})
|
184 |
print self.typLegendDict
|
185 |
|
186 |
k=len(self.histos)
|
187 |
|
188 |
for j in range(0,k):
|
189 |
#print histos[j].GetBinContent(1)
|
190 |
i=k-j-1
|
191 |
self.histos[i].SetFillColor(int(self.colorDict[self.typs[i]]))
|
192 |
self.histos[i].SetLineColor(1)
|
193 |
allStack.Add(self.histos[i])
|
194 |
|
195 |
d1 = ROOT.TH1F('noData','noData',self.nBins,self.xMin,self.xMax)
|
196 |
datatitle='Data'
|
197 |
addFlag = ''
|
198 |
if 'Zee' in self.datanames and 'Zmm' in self.datanames:
|
199 |
addFlag = 'Z(l^{-}l^{+})H(b#bar{b})'
|
200 |
elif 'Zee' in self.datanames:
|
201 |
addFlag = 'Z(e^{-}e^{+})H(b#bar{b})'
|
202 |
elif 'Zmm' in self.datanames:
|
203 |
addFlag = 'Z(#mu^{-}#mu^{+})H(b#bar{b})'
|
204 |
elif 'Znn' in self.datanames:
|
205 |
addFlag = 'Z(#nu#nu)H(b#bar{b})'
|
206 |
elif 'Wmn' in self.datanames:
|
207 |
addFlag = 'W(#mu#nu)H(b#bar{b})'
|
208 |
elif 'Wen' in self.datanames:
|
209 |
addFlag = 'W(e#nu)H(b#bar{b})'
|
210 |
elif 'Wtn' in self.datanames:
|
211 |
addFlag = 'W(#tau#nu)H(b#bar{b})'
|
212 |
addFlag2 = ''
|
213 |
if 'TTbar' in self.region:
|
214 |
addFlag2 = 't#bar{t} enriched region'
|
215 |
elif 'ZLight' in self.region:
|
216 |
addFlag2 = 'Z+udscg enriched region'
|
217 |
elif 'Zbb' in self.region:
|
218 |
addFlag2 = 'Z+b#bar{b} enriched region'
|
219 |
else:
|
220 |
addFlag2 = 'pp #rightarrow VH; H #rightarrow b#bar{b}'
|
221 |
|
222 |
for i in range(0,len(self.datas)):
|
223 |
print self.datas[i]
|
224 |
d1.Add(self.datas[i],1)
|
225 |
print "\033[1;32m\n\tDATA integral = %s\033[1;m"%d1.Integral()
|
226 |
flow = d1.GetEntries()-d1.Integral()
|
227 |
if flow > 0:
|
228 |
print "\033[1;31m\tU/O flow: %s\033[1;m"%flow
|
229 |
|
230 |
if self.overlay and not isinstance(self.overlay,list):
|
231 |
self.overlay = [self.overlay]
|
232 |
if self.overlay:
|
233 |
for _overlay in self.overlay:
|
234 |
_overlay.SetLineColor(int(self.colorDict[_overlay.GetName()]))
|
235 |
_overlay.SetLineWidth(2)
|
236 |
_overlay.SetFillColor(0)
|
237 |
_overlay.SetFillStyle(4000)
|
238 |
|
239 |
numLegend = 2+k
|
240 |
if self.overlay:
|
241 |
numLegend += len(self.overlay)
|
242 |
l.AddEntry(d1,datatitle,'P')
|
243 |
for j in range(0,k):
|
244 |
if j < numLegend/2.-1:
|
245 |
l.AddEntry(self.histos[j],self.typLegendDict[self.typs[j]],'F')
|
246 |
else:
|
247 |
l_2.AddEntry(self.histos[j],self.typLegendDict[self.typs[j]],'F')
|
248 |
if self.overlay:
|
249 |
for _overlay in self.overlay:
|
250 |
l_2.AddEntry(_overlay,self.typLegendDict[_overlay.GetName()],'L')
|
251 |
|
252 |
if self.normalize:
|
253 |
if MC_integral != 0: stackscale=d1.Integral()/MC_integral
|
254 |
if self.overlay:
|
255 |
for _overlay in self.overlay:
|
256 |
_overlay.Scale(stackscale)
|
257 |
stackhists=allStack.GetHists()
|
258 |
for blabla in stackhists:
|
259 |
if MC_integral != 0: blabla.Scale(stackscale)
|
260 |
|
261 |
allMC=allStack.GetStack().Last().Clone()
|
262 |
|
263 |
allStack.SetTitle()
|
264 |
allStack.Draw("hist")
|
265 |
allStack.GetXaxis().SetTitle('')
|
266 |
#yTitle = 's/(s+b) weighted entries'
|
267 |
if not d1.GetSumOfWeights() % 1 == 0.0:
|
268 |
yTitle = 's/(s+b) weighted entries'
|
269 |
else:
|
270 |
yTitle = 'Entries'
|
271 |
if not '/' in yTitle:
|
272 |
yAppend = '%.0f' %(allStack.GetXaxis().GetBinWidth(1))
|
273 |
yTitle = '%s / %s' %(yTitle, yAppend)
|
274 |
allStack.GetYaxis().SetTitle(yTitle)
|
275 |
allStack.GetXaxis().SetRangeUser(self.xMin,self.xMax)
|
276 |
allStack.GetYaxis().SetRangeUser(0,20000)
|
277 |
theErrorGraph = ROOT.TGraphErrors(allMC)
|
278 |
theErrorGraph.SetFillColor(ROOT.kGray+3)
|
279 |
theErrorGraph.SetFillStyle(3013)
|
280 |
theErrorGraph.Draw('SAME2')
|
281 |
l_2.AddEntry(theErrorGraph,"MC uncert. (stat.)","fl")
|
282 |
Ymax = max(allStack.GetMaximum(),d1.GetMaximum())*1.7
|
283 |
if self.log:
|
284 |
allStack.SetMinimum(0.1)
|
285 |
Ymax = Ymax*ROOT.TMath.Power(10,1.2*(ROOT.TMath.Log(1.2*(Ymax/0.1))/ROOT.TMath.Log(10)))*(0.2*0.1)
|
286 |
#Ymax = Ymax*ROOT.TMath.Power(10,1.3*(ROOT.TMath.Log(1.3*(Ymax/0.1))/ROOT.TMath.Log(10)))*(0.3*0.1)
|
287 |
ROOT.gPad.SetLogy()
|
288 |
allStack.SetMaximum(Ymax)
|
289 |
c.Update()
|
290 |
ROOT.gPad.SetTicks(1,1)
|
291 |
l.SetFillColor(0)
|
292 |
l.SetBorderSize(0)
|
293 |
l_2.SetFillColor(0)
|
294 |
l_2.SetBorderSize(0)
|
295 |
|
296 |
if self.overlay:
|
297 |
for _overlay in self.overlay:
|
298 |
_overlay.Draw('hist,same')
|
299 |
d1.Draw("E,same")
|
300 |
l.Draw()
|
301 |
l_2.Draw()
|
302 |
|
303 |
tPrel = self.myText("CMS",0.17,0.88,1.04)
|
304 |
tLumi = self.myText("#sqrt{s} = %s, L = %.1f fb^{-1}"%('7TeV',(float(5000.)/1000.)),0.17,0.83)
|
305 |
tLumi1 = self.myText("#sqrt{s} = %s, L = %.1f fb^{-1}"%(self.anaTag,(float(self.lumi)/1000.)),0.17,0.78)
|
306 |
tAddFlag = self.myText(addFlag,0.17,0.78)
|
307 |
if addFlag2:
|
308 |
tAddFlag2 = self.myText(addFlag2,0.17,0.73)
|
309 |
|
310 |
unten.cd()
|
311 |
ROOT.gPad.SetTicks(1,1)
|
312 |
|
313 |
l2 = ROOT.TLegend(0.50, 0.82,0.92,0.95)
|
314 |
l2.SetLineWidth(2)
|
315 |
l2.SetBorderSize(0)
|
316 |
l2.SetFillColor(0)
|
317 |
l2.SetFillStyle(4000)
|
318 |
l2.SetTextFont(62)
|
319 |
l2.SetNColumns(2)
|
320 |
|
321 |
|
322 |
ratio, error = getRatio(d1,allMC,self.xMin,self.xMax,"",self.maxRatioUncert)
|
323 |
ksScore = d1.KolmogorovTest( allMC )
|
324 |
chiScore = d1.Chi2Test( allMC , "UWCHI2/NDF")
|
325 |
print ksScore
|
326 |
print chiScore
|
327 |
ratio.SetStats(0)
|
328 |
ratio.GetXaxis().SetTitle(self.xAxis)
|
329 |
ratioError = ROOT.TGraphErrors(error)
|
330 |
ratioError.SetFillColor(ROOT.kGray+3)
|
331 |
ratioError.SetFillStyle(3013)
|
332 |
ratio.Draw("E1")
|
333 |
if self.doFit:
|
334 |
fitData = ROOT.TF1("fData", "gaus",0.7, 1.3)
|
335 |
fitMC = ROOT.TF1("fMC", "gaus",0.7, 1.3)
|
336 |
print 'Fit on data'
|
337 |
d1.Fit(fitData,"R")
|
338 |
print 'Fit on simulation'
|
339 |
allMC.Fit(fitMC,"R")
|
340 |
|
341 |
|
342 |
if not self.AddErrors == None:
|
343 |
self.AddErrors.SetFillColor(5)
|
344 |
self.AddErrors.SetFillStyle(1001)
|
345 |
self.AddErrors.Draw('SAME2')
|
346 |
|
347 |
l2.AddEntry(self.AddErrors,"MC uncert. (stat. + syst.)","f")
|
348 |
|
349 |
|
350 |
l2.AddEntry(ratioError,"MC total uncert.","f")
|
351 |
|
352 |
l2.Draw()
|
353 |
|
354 |
ratioError.Draw('SAME2')
|
355 |
ratio.Draw("E1SAME")
|
356 |
ratio.SetTitle("")
|
357 |
m_one_line = ROOT.TLine(self.xMin,1,self.xMax,1)
|
358 |
m_one_line.SetLineStyle(ROOT.kDashed)
|
359 |
m_one_line.Draw("Same")
|
360 |
|
361 |
if not self.blind:
|
362 |
#tKsChi = self.myText("#chi_{#nu}^{2} = %.3f K_{s} = %.3f"%(chiScore,ksScore),0.17,0.9,1.5)
|
363 |
tKsChi = self.myText("#chi_{#nu}^{2} = %.3f"%(chiScore),0.17,0.9,1.5)
|
364 |
t0 = ROOT.TText()
|
365 |
t0.SetTextSize(ROOT.gStyle.GetLabelSize()*2.4)
|
366 |
t0.SetTextFont(ROOT.gStyle.GetLabelFont())
|
367 |
if not self.log:
|
368 |
t0.DrawTextNDC(0.1059,0.96, "0")
|
369 |
if not os.path.exists(self.plotDir):
|
370 |
os.makedirs(os.path.dirname(self.plotDir))
|
371 |
name = '%s/%s' %(self.plotDir,self.options['pdfName'])
|
372 |
c.Print(name)
|
373 |
fOut = ROOT.TFile.Open(name.replace('.pdf','.root'),'RECREATE')
|
374 |
for theHist in allStack.GetHists():
|
375 |
if not self.AddErrors == None and not theHist.GetName() in ['ZH','WH','VH']:
|
376 |
#print theHist.GetNbinsX()
|
377 |
#print self.AddErrors.GetN()
|
378 |
#print error.GetNbinsX()
|
379 |
for bin in range(0,theHist.GetNbinsX()):
|
380 |
theRelativeTotalError = self.AddErrors.GetErrorY(bin)
|
381 |
if error.GetBinError(bin+1) > 0.:
|
382 |
theRelativeIncrease = theRelativeTotalError/error.GetBinError(bin+1)
|
383 |
else:
|
384 |
theRelativeIncrease = 1.
|
385 |
#print 'TheTotalRelativeIncrease is: %.2f' %theRelativeIncrease
|
386 |
#print 'TheTotalStatError is: %.2f' %error.GetBinError(bin+1)
|
387 |
#print 'TheTotalError is: %.2f' %theRelativeTotalError
|
388 |
theHist.SetBinError(bin,theHist.GetBinError(bin)*theRelativeIncrease)
|
389 |
theHist.SetDirectory(fOut)
|
390 |
if theHist.GetName() == 'ZH' or theHist.GetName() == 'WH':
|
391 |
theHist.SetName('VH')
|
392 |
theHist.Write()
|
393 |
d1.SetName('data_obs')
|
394 |
d1.SetDirectory(fOut)
|
395 |
d1.Write()
|
396 |
fOut.Close()
|
397 |
#self.doCompPlot(allStack,l)
|
398 |
|
399 |
def doSubPlot(self,signal):
|
400 |
|
401 |
TdrStyles.tdrStyle()
|
402 |
histo_dict = HistoMaker.orderandadd([{self.typs[i]:self.histos[i]} for i in range(len(self.histos))],self.setup)
|
403 |
#sort
|
404 |
print histo_dict
|
405 |
sig_histos=[]
|
406 |
sub_histos=[histo_dict[key] for key in self.setup]
|
407 |
self.typs=self.setup
|
408 |
for key in self.setup:
|
409 |
if key in signal:
|
410 |
sig_histos.append(histo_dict[key])
|
411 |
|
412 |
c = ROOT.TCanvas(self.var,'', 600, 600)
|
413 |
c.SetFillStyle(4000)
|
414 |
c.SetFrameFillStyle(1000)
|
415 |
c.SetFrameFillColor(0)
|
416 |
|
417 |
main_pad = ROOT.TPad('main_pad','main_pad',0,0.1 ,1.0,1.0)
|
418 |
# main_pad.SetBottomMargin(0)
|
419 |
main_pad.SetFillStyle(4000)
|
420 |
main_pad.SetFrameFillStyle(1000)
|
421 |
main_pad.SetFrameFillColor(0)
|
422 |
|
423 |
main_pad.Draw()
|
424 |
|
425 |
main_pad.cd()
|
426 |
allStack = ROOT.THStack(self.var,'')
|
427 |
bkgStack = ROOT.THStack(self.var,'')
|
428 |
sigStack = ROOT.THStack(self.var,'')
|
429 |
|
430 |
l = ROOT.TLegend(0.59, 0.55,0.88,0.92)
|
431 |
l.SetLineWidth(2)
|
432 |
l.SetBorderSize(0)
|
433 |
l.SetFillColor(0)
|
434 |
l.SetFillStyle(4000)
|
435 |
l.SetTextFont(62)
|
436 |
l.SetTextSize(0.035)
|
437 |
MC_integral=0
|
438 |
MC_entries=0
|
439 |
|
440 |
for histo in sub_histos:
|
441 |
MC_integral+=histo.Integral()
|
442 |
print "\033[1;32m\n\tMC integral = %s\033[1;m"%MC_integral
|
443 |
|
444 |
|
445 |
|
446 |
if not 'DYc' in self.typs: self.typLegendDict.update({'DYlight':self.typLegendDict['DYlc']})
|
447 |
print self.typLegendDict
|
448 |
|
449 |
k=len(sub_histos)
|
450 |
|
451 |
# debug
|
452 |
print sub_histos
|
453 |
print sig_histos
|
454 |
|
455 |
for j in range(0,k):
|
456 |
#print histos[j].GetBinContent(1)
|
457 |
i=k-j-1
|
458 |
sub_histos[i].SetFillColor(int(self.colorDict[self.typs[i]]))
|
459 |
sub_histos[i].SetLineColor(1)
|
460 |
allStack.Add(sub_histos[i])
|
461 |
print sub_histos[i].GetName()
|
462 |
print sub_histos[i].Integral()
|
463 |
if not sub_histos[i] in sig_histos:
|
464 |
bkgStack.Add(sub_histos[i])
|
465 |
if sub_histos[i] in sig_histos:
|
466 |
sigStack.Add(sub_histos[i])
|
467 |
|
468 |
|
469 |
sub_d1 = ROOT.TH1F('subData','subData',self.nBins,self.xMin,self.xMax)
|
470 |
sub_mc = ROOT.TH1F('subMC','subMC',self.nBins,self.xMin,self.xMax)
|
471 |
|
472 |
d1 = ROOT.TH1F('noData','noData',self.nBins,self.xMin,self.xMax)
|
473 |
datatitle='Data'
|
474 |
addFlag = ''
|
475 |
if 'Zee' in self.datanames and 'Zmm' in self.datanames:
|
476 |
addFlag = 'Z(l^{-}l^{+})H(b#bar{b})'
|
477 |
elif 'Zee' in self.datanames:
|
478 |
addFlag = 'Z(e^{-}e^{+})H(b#bar{b})'
|
479 |
elif 'Zmm' in self.datanames:
|
480 |
addFlag = 'Z(#mu^{-}#mu^{+})H(b#bar{b})'
|
481 |
elif 'Znn' in self.datanames:
|
482 |
addFlag = 'Z(#nu#nu)H(b#bar{b})'
|
483 |
elif 'Wmn' in self.datanames:
|
484 |
addFlag = 'W(#mu#nu)H(b#bar{b})'
|
485 |
elif 'Wen' in self.datanames:
|
486 |
addFlag = 'W(e#nu)H(b#bar{b})'
|
487 |
else:
|
488 |
addFlag = 'pp #rightarrow VH; H #rightarrow b#bar{b}'
|
489 |
for i in range(0,len(self.datas)):
|
490 |
print self.datas[i]
|
491 |
d1.Add(self.datas[i],1)
|
492 |
print "\033[1;32m\n\tDATA integral = %s\033[1;m"%d1.Integral()
|
493 |
flow = d1.GetEntries()-d1.Integral()
|
494 |
if flow > 0:
|
495 |
print "\033[1;31m\tU/O flow: %s\033[1;m"%flow
|
496 |
|
497 |
|
498 |
numLegend = 1+k
|
499 |
if self.overlay:
|
500 |
numLegend += len(self.overlay)
|
501 |
l.AddEntry(d1,datatitle,'P')
|
502 |
# l.AddEntry(sub_d1,datatitle,'P')
|
503 |
for j in range(0,k):
|
504 |
if self.typs[j] in signal:
|
505 |
if j < numLegend/2.:
|
506 |
l.AddEntry(sub_histos[j],self.typLegendDict[self.typs[j]],'F')
|
507 |
else:
|
508 |
l_2.AddEntry(sub_histos[j],self.typLegendDict[self.typs[j]],'F')
|
509 |
if self.overlay:
|
510 |
for _overlay in self.overlay:
|
511 |
l_2.AddEntry(_overlay,self.typLegendDict['Overlay'+_overlay.GetName()],'L')
|
512 |
|
513 |
if self.normalize:
|
514 |
if MC_integral != 0: stackscale=d1.Integral()/MC_integral
|
515 |
if self.overlay:
|
516 |
for _overlay in self.overlay:
|
517 |
_overlay.Scale(stackscale)
|
518 |
stackhists=allStack.GetHists()
|
519 |
for blabla in stackhists:
|
520 |
if MC_integral != 0: blabla.Scale(stackscale)
|
521 |
|
522 |
allMC=allStack.GetStack().Last().Clone()
|
523 |
bkgMC=bkgStack.GetStack().Last().Clone()
|
524 |
|
525 |
bkgMC_noError = bkgMC.Clone()
|
526 |
for bin in range(0,bkgMC_noError.GetNbinsX()):
|
527 |
bkgMC_noError.SetBinError(bin,0.)
|
528 |
sub_d1 = d1.Clone()
|
529 |
sub_d1.Sumw2()
|
530 |
sub_d1.Add(bkgMC_noError,-1)
|
531 |
sub_mc = allMC.Clone()
|
532 |
sub_mc.Sumw2()
|
533 |
sub_mc.Add(bkgMC_noError,-1)
|
534 |
|
535 |
sigStack.SetTitle()
|
536 |
sigStack.Draw("hist")
|
537 |
sigStack.GetXaxis().SetTitle('')
|
538 |
yTitle = 's/(s+b) weighted entries'
|
539 |
if not '/' in yTitle:
|
540 |
yAppend = '%.0f' %(sigStack.GetXaxis().GetBinWidth(1))
|
541 |
yTitle = '%s / %s' %(yTitle, yAppend)
|
542 |
sigStack.GetYaxis().SetTitle(yTitle)
|
543 |
sigStack.GetYaxis().SetTitleOffset(1.3)
|
544 |
sigStack.GetXaxis().SetRangeUser(self.xMin,self.xMax)
|
545 |
sigStack.GetYaxis().SetRangeUser(-2000,20000)
|
546 |
sigStack.GetXaxis().SetTitle(self.xAxis)
|
547 |
|
548 |
theMCOutline = bkgMC.Clone()
|
549 |
for i in range(1,theMCOutline.GetNbinsX()+1):
|
550 |
theMCOutline.SetBinContent(i,theMCOutline.GetBinError(i))
|
551 |
theNegativeOutline = theMCOutline.Clone()
|
552 |
theNegativeOutline.Add(theNegativeOutline,-2.)
|
553 |
|
554 |
theMCOutline.SetLineColor(4)
|
555 |
theNegativeOutline.SetLineColor(4)
|
556 |
theMCOutline.SetLineWidth(2)
|
557 |
theNegativeOutline.SetLineWidth(2)
|
558 |
theMCOutline.SetFillColor(0)
|
559 |
theNegativeOutline.SetFillColor(0)
|
560 |
theMCOutline.Draw("hist same")
|
561 |
theNegativeOutline.Draw("hist same")
|
562 |
l.AddEntry(theMCOutline,"Sub. MC uncert.","fl")
|
563 |
|
564 |
theErrorGraph = ROOT.TGraphErrors(sigStack.GetStack().Last().Clone())
|
565 |
theErrorGraph.SetFillColor(ROOT.kGray+3)
|
566 |
theErrorGraph.SetFillStyle(3013)
|
567 |
theErrorGraph.Draw('SAME2')
|
568 |
l.AddEntry(theErrorGraph,"VH + VV MC uncert.","fl")
|
569 |
|
570 |
Ymax = max(sigStack.GetMaximum(),sub_d1.GetMaximum())*1.7
|
571 |
Ymin = max(-sub_mc.GetMinimum(),-sub_d1.GetMinimum())*2.7
|
572 |
if self.log:
|
573 |
sigStack.SetMinimum(0.1)
|
574 |
Ymax = Ymax*ROOT.TMath.Power(10,1.2*(ROOT.TMath.Log(1.2*(Ymax/0.1))/ROOT.TMath.Log(10)))*(0.2*0.1)
|
575 |
ROOT.gPad.SetLogy()
|
576 |
sigStack.SetMaximum(Ymax)
|
577 |
sigStack.SetMinimum(-Ymin)
|
578 |
c.Update()
|
579 |
ROOT.gPad.SetTicks(1,1)
|
580 |
#sigStack.Draw("hist")
|
581 |
l.SetFillColor(0)
|
582 |
l.SetBorderSize(0)
|
583 |
|
584 |
if self.overlay:
|
585 |
for _overlay in self.overlay:
|
586 |
_overlay.Draw('hist,same')
|
587 |
sub_d1.Draw("E,same")
|
588 |
l.Draw()
|
589 |
|
590 |
tPrel = self.myText("CMS",0.17,0.88,1.04)
|
591 |
tLumi = self.myText("#sqrt{s} = %s, L = %.1f fb^{-1}"%(self.anaTag,(float(self.lumi)/1000.)),0.17,0.83)
|
592 |
tLumi = self.myText("#sqrt{s} = 7TeV, L = 5.0 fb^{-1}",0.17,0.78)
|
593 |
tAddFlag = self.myText(addFlag,0.17,0.73)
|
594 |
|
595 |
ROOT.gPad.SetTicks(1,1)
|
596 |
|
597 |
l2 = ROOT.TLegend(0.5, 0.82,0.92,0.95)
|
598 |
l2.SetLineWidth(2)
|
599 |
l2.SetBorderSize(0)
|
600 |
l2.SetFillColor(0)
|
601 |
l2.SetFillStyle(4000)
|
602 |
l2.SetTextFont(62)
|
603 |
#l2.SetTextSize(0.035)
|
604 |
l2.SetNColumns(2)
|
605 |
|
606 |
|
607 |
|
608 |
if not self.AddErrors == None:
|
609 |
self.AddErrors.SetFillColor(5)
|
610 |
self.AddErrors.SetFillStyle(1001)
|
611 |
self.AddErrors.Draw('SAME2')
|
612 |
|
613 |
l2.AddEntry(self.AddErrors,"MC uncert. (stat. + syst.)","f")
|
614 |
|
615 |
|
616 |
if not os.path.exists(self.plotDir):
|
617 |
os.makedirs(os.path.dirname(self.plotDir))
|
618 |
name = '%s/%s' %(self.plotDir,self.options['pdfName'])
|
619 |
c.Print(name)
|