ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/JohnStupak/python/doPostProc.py
Revision: 1.11
Committed: Tue Apr 16 22:42:05 2013 UTC (12 years ago) by jstupak
Content type: text/x-python
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +43 -37 lines
Log Message:
minor bug fix

File Contents

# Content
1 from ROOT import *
2 gROOT.SetBatch(1)
3 from LJMet.Com.Sample import samplesForPlotting as samples
4 from LJMet.Com.Sample import signalForPlotting as signalsForPlotting
5 from LJMet.Com.Sample import signal as signals
6 from tdrStyle import *
7 from sys import argv
8 from array import array
9 import os
10 setTDRStyle()
11 gStyle.SetOptStat(False)
12
13 DEBUG=False
14
15 if len(argv)>1:
16 inputDir=argv[1]
17 else:
18 inputDir='/uscms_data/d1/jstupak/chargedHiggs/2013_2_18'
19
20 doCutTable=False
21 getWhfCorrections=False
22 doLimitSetting=False
23
24 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
25
26 doCuts=['0','1','2','3','4p']
27 #doCuts=['preselection','0','1','2','1p','2p','3p']
28 #doCuts=['preselection','0','1','2','1p','2p','3p','final']
29
30 doChannels=['el','mu']
31 #doChannels=['el']
32 #doChannels=['mu']
33
34 doShapeComparison=True
35
36 #3x3 matrix (W+light,W+heavy,ttbar)
37 residualSF_Wj=0.937195062637
38 residualSF_Wc=1.11306285858
39 residualSF_Wb=residualSF_Wc
40 residualSF_ttbar=1.07874298096
41
42 #These currently do nothing, just thinking ahead
43 doJES=False
44 doJER=False
45 doBTS=False
46
47 outputDir=inputDir+'/plots'
48
49 #H+- Cuts
50 sharedSelectionCuts='jet_0_pt_ChargedHiggsCalc > 50 && jet_1_pt_ChargedHiggsCalc > 30'
51 elSelectionCuts='elec_1_pt_ChargedHiggsCalc > 30 && abs(elec_1_eta_ChargedHiggsCalc) < 2.5 && elec_1_RelIso_ChargedHiggsCalc < 0.1 && corr_met_ChargedHiggsCalc > 20 && Muon_DeltaR_LjetsTopoCalcNew > 0.3'
52 muSelectionCuts='muon_1_pt_ChargedHiggsCalc > 26 && abs(muon_1_eta_ChargedHiggsCalc) < 2.1 && muon_1_RelIso_ChargedHiggsCalc < 0.12 && corr_met_ChargedHiggsCalc > 20 && Muon_DeltaR_LjetsTopoCalcNew > 0.3'
53
54 finalCuts='BestTop_Pt_LjetsTopoCalcNew > 85 && Jet1Jet2_Pt_LjetsTopoCalcNew > 140 && 130 < BestTop_LjetsTopoCalcNew && BestTop_LjetsTopoCalcNew < 210'
55
56 """
57 #Wprime Cuts
58 sharedSelectionCuts='jet_0_pt_ChargedHiggsCalc > 120 && jet_1_pt_ChargedHiggsCalc > 40'
59 elSelectionCuts='elec_1_pt_ChargedHiggsCalc > 50 && abs(elec_1_eta_ChargedHiggsCalc) < 2.5 && elec_1_RelIso_ChargedHiggsCalc < 0.1 && corr_met_ChargedHiggsCalc > 20 && Muon_DeltaR_LjetsTopoCalcNew > 0.3'
60 muSelectionCuts='muon_1_pt_ChargedHiggsCalc > 50 && abs(muon_1_eta_ChargedHiggsCalc) < 2.1 && muon_1_RelIso_ChargedHiggsCalc < 0.12 && corr_met_ChargedHiggsCalc > 20 && Muon_DeltaR_LjetsTopoCalcNew > 0.3'
61
62 finalCuts='BestTop_Pt_LjetsTopoCalcNew > 85 && Jet1Jet2_Pt_LjetsTopoCalcNew > 140 && 130 < BestTop_LjetsTopoCalcNew && BestTop_LjetsTopoCalcNew < 210'
63 """
64 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65
66 elLumi=19624
67 muLumi=19624
68
69 treeName='ljmet'
70
71 SHyFT_Wj=1
72 SHyFT_Wc=1.66
73 SHyFT_Wb=1.21
74
75 lumiFracUnc = 0.022
76 ttbarSigmaFracUnc = 0.15
77 wJetsSigmaFracUnc = 0.20
78 otherSigmaFracUnc = 0.20
79
80 #---------------------------------------------------------------------------------------------------------------------------------------------
81
82 if doLimitSetting:
83 doChannels=['el','mu']
84 getWhfCorrections=False
85 doCutTable=False
86 for signal in signals:
87 inSamples=False
88 for sample in samples:
89 if signal.name==sample.name: inSamples=True
90 if not inSamples: samples.append(signal)
91 samples=sorted(samples,key=lambda sample: sample.name)
92
93 if getWhfCorrections:
94 doCuts=['preselection','0','1']
95 doChannels=['mu']
96 doCutTable=False
97 doLimitSetting=False
98 residualSF_Wj = 1
99 residualSF_Wc = 1
100 residualSF_Wb = 1
101 residualSF_ttbar = 1
102
103 if doCutTable:
104 getWhfCorrections=False
105 doLimitSetting=False
106
107 #---------------------------------------------------------------------------------------------------------------------------------------------
108
109 SFWj=SHyFT_Wj*residualSF_Wj
110 SFWc=SHyFT_Wc*residualSF_Wc
111 SFWb=SHyFT_Wb*residualSF_Wb
112
113 if not os.path.isdir(outputDir): os.system("mkdir -p "+outputDir)
114 output=TFile(outputDir+'/plots.root',"RECREATE")
115
116 class ChargedHiggsPlot:
117
118 def __init__(self,name,distribution,bins=None,nBins=100,xMin=0,xMax=100,xTitle='',yLog=True,cuts="0",extraCuts=None,channel='el'):
119 self.name=name; self.distribution=distribution; self.bins=bins; self.nBins=nBins; self.xMin=xMin; self.xMax=xMax; self.xTitle=xTitle; self.yLog=yLog; self.cuts=cuts; self.extraCuts=extraCuts; self.channel=channel;
120
121 if self.bins:
122 self.nBins=len(self.bins)-1
123 self.xMin=self.bins[0]
124 self.xMax=self.bins[-1]
125 else:
126 self.bins=[]
127 for n in range(self.nBins+1):
128 self.bins.append(self.xMin+n*(float(self.xMax-self.xMin)/self.nBins))
129 self.bins=array('f',self.bins)
130
131 #---------------------------------------------------------------------------------------------------------------------------------------------------------
132
133 def Prepare(self):
134 result={}
135
136 cuts=sharedSelectionCuts
137 if self.channel=='el': cuts+='&&'+elSelectionCuts
138 elif self.channel=='mu': cuts+='&&'+muSelectionCuts
139
140 if self.cuts=="preselection" or self.cuts=='0p':
141 bTagCut='1'
142 elif self.cuts=="0":
143 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)==0)'
144 elif self.cuts=="1":
145 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)==1)'
146 elif self.cuts=="1p" or self.cuts=="final":
147 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)>=1)'
148 elif self.cuts=="2":
149 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)==2)'
150 elif self.cuts=="2p":
151 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)>=2)'
152 elif self.cuts=="3":
153 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)==3)'
154 elif self.cuts=="3p":
155 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)>=3)'
156 elif self.cuts=="4":
157 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)==4)'
158 elif self.cuts=="4p":
159 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)>=4)'
160 elif self.cuts=="5":
161 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)==5)'
162 elif self.cuts=="5p":
163 bTagCut='((jet_0_tag_ChargedHiggsCalc + jet_1_tag_ChargedHiggsCalc + jet_2_tag_ChargedHiggsCalc + jet_3_tag_ChargedHiggsCalc + jet_4_tag_ChargedHiggsCalc + jet_5_tag_ChargedHiggsCalc)>=5)'
164
165 if self.cuts=='final':
166 cuts+='&& '+finalCuts
167
168 if self.extraCuts: cuts+='&& '+self.extraCuts
169
170 #make name unique
171 self.name+='_nB'+self.cuts+'_'+self.channel
172
173 if (self.channel == 'el'): self.lumi=elLumi
174 elif (self.channel == 'mu'): self.lumi=muLumi
175
176 #get histograms with proper normalization
177 for sample in samples:
178 #skip electron (muon) data for muon (electron) channel
179 if (self.channel=='el' and 'SingleMu' in sample.name) or (self.channel=='mu' and 'SingleEl' in sample.name): continue
180
181 sample.file=TFile(inputDir+'/'+sample.name+'.root')
182
183 sample.tree=sample.file.Get(treeName)
184 hName=self.name+'__'+sample.name
185 sample.h=TH1F(hName,";"+self.xTitle,self.nBins,self.bins)
186
187 weight='1'
188 if sample.isMC:
189 weight+='* weight_PU_ABCD_PileUpCalc'
190 if (self.channel == 'el'):
191 weight+='* weight_ElectronEff_53x_ChargedHiggsCalc'
192 weight+='* (0.973*(abs(elec_1_eta_ChargedHiggsCalc)<1.5) + 1.02*(abs(elec_1_eta_ChargedHiggsCalc)>1.5&&abs(elec_1_eta_ChargedHiggsCalc)<2.5))'
193 elif (self.channel == 'mu'): weight+='* weight_MuonEff_ChargedHiggsCalc'
194
195 if sample.name=='WJets':
196 self.Wlight=sample.h.Clone(self.name+'_light')
197 self.Wc=sample.h.Clone(self.name+'_c')
198 self.Wb=sample.h.Clone(self.name+'_b')
199
200 sample.tree.Draw(self.distribution+">>"+self.name+'_light',weight+'*'+str(SFWj)+'*('+cuts+'&&'+bTagCut+'&& n_Bjets_ChargedHiggsCalc==0 && n_Cjets_ChargedHiggsCalc==0)','E GOFF')
201 sample.tree.Draw(self.distribution+">>"+self.name+'_c',weight+'*'+str(SFWc)+'*('+cuts+'&&'+bTagCut+'&& n_Bjets_ChargedHiggsCalc==0 && n_Cjets_ChargedHiggsCalc>0)','E GOFF')
202 sample.tree.Draw(self.distribution+">>"+self.name+'_b',weight+'*'+str(SFWb)+'*('+cuts+'&&'+bTagCut+'&& n_Bjets_ChargedHiggsCalc>0)','E GOFF')
203
204 sample.h.Add(self.Wlight)
205 sample.h.Add(self.Wc)
206 sample.h.Add(self.Wb)
207 #Wnorm=sample.h.Integral(0,self.nBins+1)
208
209 #get shape from untagged sample
210 #sample.tree.Draw(self.distribution+">>"+self.name,weight+'*'+str(SFWj)+'*('+cuts+'&& n_Bjets_ChargedHiggsCalc==0 && n_Cjets_ChargedHiggsCalc==0)','E GOFF')
211 #sample.tree.Draw(self.distribution+">>+"+self.name,weight+'*'+str(SFWc)+'*('+cuts+'&& n_Bjets_ChargedHiggsCalc==0 && n_Cjets_ChargedHiggsCalc>0)','E GOFF')
212 #sample.tree.Draw(self.distribution+">>+"+self.name,weight+'*'+str(SFWb)+'*('+cuts+'&& n_Bjets_ChargedHiggsCalc>0)','E GOFF')
213 #sample.h.Scale(Wnorm/sample.h.Integral(0,self.nBins+1)) #normalize to tagged integral
214
215 self.Wlight.Scale(self.lumi*sample.crossSection/sample.nEvents)
216 self.Wc.Scale(self.lumi*sample.crossSection/sample.nEvents)
217 self.Wb.Scale(self.lumi*sample.crossSection/sample.nEvents)
218 result['W+light']=self.Wlight.Integral(0,self.nBins+1)
219 result['W+c']=self.Wc.Integral(0,self.nBins+1)
220 result['W+b']=self.Wb.Integral(0,self.nBins+1)
221
222 else:
223 nSelectedRaw=sample.tree.Draw(self.distribution+">>"+hName,weight+'*('+cuts+'&&'+bTagCut+')','E GOFF')
224 if 'TTbar' in sample.name:
225 sample.h.Scale(residualSF_ttbar)
226 self.ttbar=sample.h.Clone(self.name+'_ttbar')
227 self.ttbar.Scale(self.lumi*sample.crossSection/sample.nEvents)
228
229 if sample.isMC and sample.h.Integral(0,self.nBins+1)!=0: sample.h.Scale(self.lumi*sample.crossSection/sample.nEvents)
230
231 if DEBUG: #for event comparison
232 if 'TTbar_Powheg' in sample.name and self.channel=='el':
233 sample.tree.SetScanField(0)
234 print '\n',sample.name,' ',self.channel
235 sample.tree.Scan('run_CommonCalc:lumi_CommonCalc:event_CommonCalc:corr_met_ChargedHiggsCalc:jet_0_pt_ChargedHiggsCalc:jet_1_pt_ChargedHiggsCalc:elec_1_pt_ChargedHiggsCalc:elec_1_eta_ChargedHiggsCalc:elec_1_RelIso_ChargedHiggsCalc:muon_1_pt_ChargedHiggsCalc:muon_1_eta_ChargedHiggsCalc:muon_1_RelIso_ChargedHiggsCalc','('+cuts+'&&'+bTagCut+')')
236
237 result[sample.name]=sample.h.Integral(0,self.nBins+1) #for cutflow table
238
239 #format histograms and draw plots
240 output.cd()
241 self.signals=[]
242 self.backgroundStack=THStack()
243
244 self.ewk=TH1F(self.name+'__ewk',';'+self.xTitle,self.nBins,self.bins)
245 self.top=TH1F(self.name+'__top',';'+self.xTitle,self.nBins,self.bins)
246 self.data=TH1F(self.name+'__DATA',';'+self.xTitle,self.nBins,self.bins)
247 self.ewk.Sumw2()
248 self.top.Sumw2()
249 self.data.SetMarkerStyle(20)
250 for sample in samples:
251 if (self.channel=='el' and 'SingleMu' in sample.name) or (self.channel=='mu' and 'SingleEl' in sample.name): continue
252 elif sample.isSignal:
253 sample.h.Scale(20)
254 sample.h.SetLineColor(1)
255 sample.h.SetLineStyle(2+len(self.signals))
256 self.signals+=[sample.h]
257 elif sample.isBackground:
258 if sample.name[0]=='T': self.top.Add(sample.h)
259 else: self.ewk.Add(sample.h)
260 elif sample.isData:
261 self.data.Add(sample.h)
262 sample.h.SetLineWidth(2)
263
264 self.ewk.SetFillColor(ROOT.kGreen-3)
265 self.ewk.SetLineColor(ROOT.kGreen-2)
266 self.top.SetFillColor(ROOT.kRed-7)
267 self.top.SetLineColor(ROOT.kRed-4)
268
269 self.backgroundStack.Add(self.ewk)
270 self.backgroundStack.Add(self.top)
271
272 self.background=self.backgroundStack.GetStack().Last().Clone(self.name+'_background')
273 result['Total Background']=self.background.Integral(0,self.nBins+1)
274 result['Data']=self.data.Integral(0,self.nBins+1)
275
276 return result
277
278 #---------------------------------------------------------------------------------------------------------------------------------------------------------
279
280 def Draw(self):
281 gStyle.SetErrorX(0.5)
282
283 self.canvas=TCanvas(self.name,"",1000,800)
284
285 yDiv=0.35
286 self.uPad=TPad("uPad","",0,yDiv,1,1) #for actual plots
287 self.uPad.SetTopMargin(0.07)
288 self.uPad.SetBottomMargin(0)
289 self.uPad.SetRightMargin(.05)
290 self.uPad.SetLeftMargin(.18)
291 self.uPad.Draw()
292
293 self.lPad=TPad("lPad","",0,0,1,yDiv) #for sigma runner
294 self.lPad.SetTopMargin(0)
295 self.lPad.SetBottomMargin(.4)
296 self.lPad.SetRightMargin(.05)
297 self.lPad.SetLeftMargin(.18)
298 self.lPad.SetGridy()
299 self.lPad.Draw()
300
301 self.data.SetMaximum(2*self.data.GetMaximum())
302 self.data.SetMinimum(0.025)
303
304 binWidth=round(self.data.GetBinWidth(1),5)
305 if binWidth-round(binWidth)<.001*binWidth: binWidth=int(round(binWidth))
306 yTitle="Events"
307 if '[' in self.xTitle and ']' in self.xTitle: #get units from x axis title
308 begin=self.xTitle.find('[')+1
309 end=self.xTitle.find(']')
310 yTitle+=' / '+str(binWidth)+' '+self.xTitle[begin:end]
311 self.data.GetYaxis().SetTitle(yTitle)
312
313 self.formatUpperHist(self.data)
314
315 self.uPad.cd()
316 self.data.Draw("E1 X0") #draw data first because its easier to format a TH1 than a THStack
317 self.backgroundStack.Draw("SAME HIST")
318 for signal in self.signals:
319 for s in signalsForPlotting:
320 if s.name in signal.GetName(): signal.Draw("SAME HIST")
321 self.data.Draw("SAME E1 X0") #redraw data so its not hidden
322 self.uPad.RedrawAxis()
323
324 #calculate stat+sys uncertainty
325 self.uncBand=self.background.Clone("unc")
326 for binNo in range(0,self.nBins+1):
327 lumiUnc=0
328 sigmaUnc=0
329 statUnc=0
330 for sample in samples:
331 if sample.isBackground:
332 lumiUnc+=(sample.h.GetBinContent(binNo)*lumiFracUnc)**2
333 if sample.name=='WJets': sigmaFracUnc=wJetsSigmaFracUnc
334 elif sample.name[0]=='T': sigmaFracUnc=ttbarSigmaFracUnc
335 else: sigmaFracUnc=otherSigmaFracUnc #WW, Z+Jets
336 sigmaUnc+=(sample.h.GetBinContent(binNo)*sigmaFracUnc)**2
337 statUnc+=sample.h.GetBinError(binNo)**2
338 totalUnc=sqrt(lumiUnc+sigmaUnc+statUnc)
339 self.uncBand.SetBinError(binNo,totalUnc)
340 self.background.SetBinError(binNo,totalUnc)
341 self.uncBand.SetFillStyle(3344)
342 self.uncBand.SetFillColor(1)
343 self.uncBand.SetLineColor(1)
344 self.uncBand.SetMarkerSize(0)
345 gStyle.SetHatchesLineWidth(1)
346 self.uncBand.Draw("SAME E2")
347
348 legend=TLegend(0.55,0.55,0.90,0.90)
349 legend.SetShadowColor(0);
350 legend.SetFillColor(0);
351 legend.SetLineColor(0);
352 legend.AddEntry(self.data,"Data")
353 legend.AddEntry(self.top,"t#bar{t} + Single-Top", "f")
354 legend.AddEntry(self.ewk,"W#rightarrowl#nu + Z/#gamma*#rightarrowl^{+}l^{-} + VV" , "f")
355 for signal in self.signals:
356 mass=signal.GetName()[-3:]
357 for s in signalsForPlotting:
358 if s.name in signal.GetName(): legend.AddEntry(signal, "H^{#pm} x 20 (m="+mass+" GeV)", "l")
359 legend.AddEntry(self.uncBand , "Uncertainty" , "f")
360 legend.Draw("SAME")
361
362 prelimTex=TLatex()
363 prelimTex.SetNDC()
364 prelimTex.SetTextSize(0.04)
365 prelimTex.SetTextAlign(31) # align right
366 lumi=self.lumi/1000.
367 lumi=round(lumi,2)
368 prelimTex.DrawLatex(0.9, 0.95, "CMS Preliminary, "+str(lumi)+" fb^{-1} at #sqrt{s} = 8 TeV");
369
370 if self.cuts=="preselection" or self.cuts=='0p':
371 bTagLabel="N_{b tags} #geq 0"
372 elif self.cuts=="0":
373 bTagLabel="N_{b tags} = 0"
374 elif self.cuts=="1":
375 bTagLabel="N_{b tags} = 1"
376 elif self.cuts=="1p":
377 bTagLabel="N_{b tags} #geq 1"
378 elif self.cuts=="2":
379 bTagLabel="N_{b tags} = 2"
380 elif self.cuts=="2p":
381 bTagLabel="N_{b tags} #geq 2"
382 elif self.cuts=="3":
383 bTagLabel="N_{b tags} = 3"
384 elif self.cuts=="3p":
385 bTagLabel="N_{b tags} #geq 3"
386 elif self.cuts=="4":
387 bTagLabel="N_{b tags} = 4"
388 elif self.cuts=="4p":
389 bTagLabel="N_{b tags} #geq 4"
390 elif self.cuts=="5":
391 bTagLabel="N_{b tags} = 5"
392 elif self.cuts=="5p":
393 bTagLabel="N_{b tags} #geq 5"
394 elif self.cuts=="final":
395 bTagLabel="N_{b tags} #geq 1"
396
397
398 channelTex = TLatex()
399 channelTex.SetNDC()
400 channelTex.SetTextSize(0.08)
401 channelTex.SetTextAlign(31)
402 if self.channel=='el': text='e'
403 elif self.channel=='mu': text='#mu'
404 text+="+jets "+bTagLabel
405 channelTex.DrawLatex(0.5, 0.83, text);
406
407 self.lPad.cd()
408 self.pull=self.data.Clone("pull")
409 for binNo in range(0,self.nBins+1):
410 if self.background.GetBinError(binNo)!=0:
411 self.pull.SetBinContent(binNo,(self.data.GetBinContent(binNo)-self.background.GetBinContent(binNo))/self.background.GetBinError(binNo))
412 self.pull.SetMaximum(3)
413 self.pull.SetMinimum(-3)
414 self.pull.SetFillColor(2)
415 self.pull.SetLineColor(2)
416 self.formatLowerHist(self.pull)
417 self.pull.GetYaxis().SetTitle('#sigma(Data-MC)')
418 self.pull.Draw("HIST")
419
420 output.cd()
421 self.canvas.Write()
422 self.canvas.SaveAs(outputDir+'/'+self.name+'.pdf')
423 self.canvas.SaveAs(outputDir+'/'+self.name+'.eps')
424
425 for sample in samples:
426 if sample.isSignal:
427 sample.h.Scale(1./20)
428 if doLimitSetting:
429 sample.h.Scale(1./sample.crossSection)
430
431 if not doLimitSetting:
432 self.Wlight.Write()
433 self.Wc.Write()
434 self.Wb.Write()
435 self.ttbar.Write()
436
437 self.data.Write()
438 self.ewk.Write()
439 self.top.Write()
440 for sample in samples:
441 if sample.isSignal or not doLimitSetting:
442 try: sample.h.Write()
443 except: pass
444
445 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
446
447 if doShapeComparison:
448 if self.ewk.Integral(0,self.nBins+1)>0: self.ewk.Scale(1./self.ewk.Integral(0,self.nBins+1))
449 self.ewk.SetFillColor(kWhite)
450 if self.top.Integral(0,self.nBins+1)>0: self.top.Scale(1./self.top.Integral(0,self.nBins+1))
451 self.top.SetFillColor(kWhite)
452 for signal in self.signals:
453 if signal.Integral(0,self.nBins+1)>0: signal.Scale(1./signal.Integral(0,self.nBins+1))
454 signal.SetFillColor(kWhite)
455
456 max=0
457 for hist in [self.ewk,self.top]+self.signals:
458 if hist.GetMaximum()>max: max=hist.GetMaximum()
459 self.ewk.SetMaximum(1.1*max)
460
461 #self.formatUpperHist(self.ewk);
462 self.ewk.GetYaxis().SetTitle('Shape')
463
464 ewkEff=self.ewk.Clone('ewkEff')
465 #self.formatLowerHist(ewkEff)
466 ewkEff.GetYaxis().SetTitle('1-Integral')
467 ewkEff.SetMinimum(.8)
468 ewkEff.SetMaximum(1)
469
470 topEff=self.top.Clone('topEff')
471 signalEffs=[]
472 for signal in self.signals: signalEffs.append(signal.Clone('signalEff'+str(len(signalEffs))));
473
474 for binNo in range(1,self.nBins+1):
475 ewkEff.SetBinContent(binNo,1-self.ewk.Integral(0,binNo))
476 topEff.SetBinContent(binNo,1-self.top.Integral(0,binNo))
477 for signal,signalEff in zip(self.signals,signalEffs): signalEff.SetBinContent(binNo,1-signal.Integral(0,binNo))
478
479 self.canvas=TCanvas('shape_'+self.name,"",1000,800)
480 #self.uPad.cd()
481 self.ewk.Draw()
482 self.top.Draw("SAME")
483 for signal in self.signals: signal.Draw("SAME")
484
485 legend.Clear()
486 legend.AddEntry(self.top,"t#bar{t} + Single-Top", "l")
487 legend.AddEntry(self.ewk,"W#rightarrowl#nu + Z/#gamma*#rightarrowl^{+}l^{-} + VV" , "l")
488 for signal in self.signals:
489 mass=signal.GetName()[-3:]
490 for s in signalsForPlotting:
491 if s.name in signal.GetName(): legend.AddEntry(signal, "H^{#pm} (m="+mass+" GeV)", "l")
492 legend.Draw("SAME")
493
494 #self.lPad.cd()
495 #ewkEff.Draw("C")
496 #topEff.Draw("SAME C")
497 #for signalEff in signalEffs: signalEff.Draw("SAME C")
498
499 self.canvas.SaveAs(outputDir+'/shape_'+self.name+'.pdf')
500 self.canvas.SaveAs(outputDir+'/shape_'+self.name+'.eps')
501
502 #---------------------------------------------------------------------------------------------------------------------------------------------------------
503
504 def formatUpperHist(self,histogram):
505 histogram.GetXaxis().SetLabelSize(0)
506
507 histogram.GetYaxis().CenterTitle()
508 histogram.GetYaxis().SetLabelSize(0.08)
509 histogram.GetYaxis().SetTitleSize(0.12)
510 histogram.GetYaxis().SetTitleOffset(.75)
511
512 if self.yLog:
513 self.uPad.SetLogy()
514 histogram.SetMaximum(500*histogram.GetMaximum())
515
516 #---------------------------------------------------------------------------------------------------------------------------------------------------------
517
518 def formatLowerHist(self,histogram):
519 histogram.GetXaxis().SetLabelSize(.15)
520 histogram.GetXaxis().SetTitleSize(0.18)
521 histogram.GetXaxis().SetTitleOffset(0.95)
522 histogram.GetXaxis().SetTitle(self.xTitle)
523
524 histogram.GetYaxis().SetLabelSize(0.125)
525 histogram.GetYaxis().SetTitleSize(0.1)
526 histogram.GetYaxis().SetTitleOffset(.55)
527 histogram.GetYaxis().SetNdivisions(5);
528
529 ##################################################################################################################################################################
530
531 if __name__=='__main__':
532
533 yields={}
534 plots=[]
535 for cuts in doCuts:
536 yields[cuts]={}
537 if doCutTable:
538 plots+=[ChargedHiggsPlot(name='electron0_pt',distribution='elec_1_pt_ChargedHiggsCalc',nBins=100,xMin=0,xMax=7000,xTitle='electron p_{T} [GeV]',yLog=True,cuts=cuts,channel='el'),
539 ChargedHiggsPlot(name='muon0_pt',distribution='muon_1_pt_ChargedHiggsCalc',nBins=100,xMin=0,xMax=7000,xTitle='muon p_{T} [GeV]',yLog=True,cuts=cuts,channel='mu')
540 ]
541
542 elif getWhfCorrections:
543 plots+=[ChargedHiggsPlot(name='muon0_pt',distribution='muon_1_pt_ChargedHiggsCalc',nBins=100,xMin=0,xMax=7000,xTitle='muon p_{T} [GeV]',yLog=True,cuts=cuts,channel='mu')]
544
545 else:
546 plots+=[#ChargedHiggsPlot(name='electron0_pt',distribution='elec_1_pt_ChargedHiggsCalc',nBins=70,xMin=0,xMax=700,xTitle='electron p_{T} [GeV]',yLog=True,cuts=cuts,channel='el'),
547 #ChargedHiggsPlot(name='electron0_eta',distribution='elec_1_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='electron #eta',yLog=False,cuts=cuts,channel='el'),
548 #ChargedHiggsPlot(name='electron0_RelIso',distribution='elec_1_RelIso_ChargedHiggsCalc',nBins=30,xMin=0,xMax=0.15,xTitle='electron Rel. Isolation',yLog=True,cuts=cuts,channel='el'),
549
550 #ChargedHiggsPlot(name='muon0_pt',distribution='muon_1_pt_ChargedHiggsCalc',nBins=70,xMin=6,xMax=706,xTitle='muon p_{T} [GeV]',yLog=True,cuts=cuts,channel='mu'),
551 #ChargedHiggsPlot(name='muon0_eta',distribution='muon_1_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='muon #eta',yLog=False,cuts=cuts,channel='mu'),
552 #ChargedHiggsPlot(name='muon0_RelIso',distribution='muon_1_RelIso_ChargedHiggsCalc',nBins=30,xMin=0,xMax=0.15,xTitle='muon Rel. Isolation',yLog=True,cuts=cuts,channel='mu')
553 ]
554
555
556 for channel in doChannels:
557 plots+=[###ChargedHiggsPlot(name='BestJetJet2W_M',distribution='BestJetJet2W_M_LjetsTopoCalcNew',nBins=68,xMin=100,xMax=3500,xTitle='M(tb) [GeV]',yLog=True,cuts=cuts,channel=channel),
558 ###ChargedHiggsPlot(name='Jet1Jet2W_M',distribution='Jet1Jet2W_M_LjetsTopoCalcNew',nBins=68,xMin=100,xMax=3500,xTitle='M(tb) [GeV]',yLog=True,cuts=cuts,channel=channel),
559 #ChargedHiggsPlot(name='corr_met',distribution='corr_met_ChargedHiggsCalc',nBins=70,xMin=0,xMax=700,xTitle='E_{T}^{miss} [GeV]',yLog=True,cuts=cuts,channel=channel),
560 #ChargedHiggsPlot(name='PFjet0_pt',distribution='jet_0_pt_ChargedHiggsCalc',nBins=100,xMin=0,xMax=1000,xTitle='p_{T} (jet1) [GeV]',yLog=True,cuts=cuts,channel=channel),
561 #ChargedHiggsPlot(name='PFjet0_eta',distribution='jet_0_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='#eta (jet1)',yLog=False,cuts=cuts,channel=channel),
562 #ChargedHiggsPlot(name='PFjet1_pt',distribution='jet_1_pt_ChargedHiggsCalc',nBins=80,xMin=0,xMax=800,xTitle='p_{T} (jet2) [GeV]',yLog=True,cuts=cuts,channel=channel),
563 #ChargedHiggsPlot(name='PFjet1_eta',distribution='jet_1_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='#eta (jet2)',yLog=False,cuts=cuts,channel=channel),
564 #ChargedHiggsPlot(name='PFjet2_pt',distribution='jet_2_pt_ChargedHiggsCalc',nBins=80,xMin=0,xMax=800,xTitle='p_{T} (jet3) [GeV]',yLog=True,cuts=cuts,channel=channel),
565 #ChargedHiggsPlot(name='PFjet2_eta',distribution='jet_2_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='#eta (jet3)',yLog=False,cuts=cuts,channel=channel),
566 #ChargedHiggsPlot(name='PFjet3_pt',distribution='jet_3_pt_ChargedHiggsCalc',nBins=80,xMin=0,xMax=800,xTitle='p_{T} (jet4) [GeV]',yLog=True,cuts=cuts,channel=channel),
567 #ChargedHiggsPlot(name='PFjet3_eta',distribution='jet_3_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='#eta (jet4)',yLog=False,cuts=cuts,channel=channel),
568 #ChargedHiggsPlot(name='PFjet4_pt',distribution='jet_4_pt_ChargedHiggsCalc',nBins=80,xMin=0,xMax=800,xTitle='p_{T} (jet5) [GeV]',yLog=True,cuts=cuts,channel=channel),
569 #ChargedHiggsPlot(name='PFjet4_eta',distribution='jet_4_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='#eta (jet5)',yLog=False,cuts=cuts,channel=channel),
570 #ChargedHiggsPlot(name='PFjet5_pt',distribution='jet_5_pt_ChargedHiggsCalc',nBins=80,xMin=0,xMax=800,xTitle='p_{T} (jet6) [GeV]',yLog=True,cuts=cuts,channel=channel),
571 #ChargedHiggsPlot(name='PFjet5_eta',distribution='jet_5_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='#eta (jet6)',yLog=False,cuts=cuts,channel=channel),
572 #ChargedHiggsPlot(name='PFjet6_pt',distribution='jet_6_pt_ChargedHiggsCalc',nBins=80,xMin=0,xMax=800,xTitle='p_{T} (jet7) [GeV]',yLog=True,cuts=cuts,channel=channel),
573 #ChargedHiggsPlot(name='PFjet6_eta',distribution='jet_6_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='#eta (jet7)',yLog=False,cuts=cuts,channel=channel),
574 #ChargedHiggsPlot(name='PFjet7_pt',distribution='jet_7_pt_ChargedHiggsCalc',nBins=80,xMin=0,xMax=800,xTitle='p_{T} (jet8) [GeV]',yLog=True,cuts=cuts,channel=channel),
575 #ChargedHiggsPlot(name='PFjet7_eta',distribution='jet_7_eta_ChargedHiggsCalc',nBins=50,xMin=-2.5,xMax=2.5,xTitle='#eta (jet8)',yLog=False,cuts=cuts,channel=channel),
576
577 ###ChargedHiggsPlot(name='TopMass_Best',distribution='BestTop_LjetsTopoCalcNew',nBins=100,xMin=0,xMax=1000,xTitle='M(best jet,W) [GeV]',yLog=True,cuts=cuts,channel=channel),
578 ###ChargedHiggsPlot(name='TopPt_Best',distribution='BestTop_Pt_LjetsTopoCalcNew',nBins=150,xMin=0,xMax=1500,xTitle='p_{T}(best jet,W) [GeV]',yLog=True,cuts=cuts,channel=channel),
579 #ChargedHiggsPlot(name='Pt_Jet1Jet2',distribution='Jet1Jet2_Pt_LjetsTopoCalcNew',nBins=150,xMin=0,xMax=1500,xTitle='p_{T}(jet1,jet2) [GeV]',yLog=True,cuts=cuts,channel=channel),
580 #ChargedHiggsPlot(name='HT',distribution='Ht_LjetsTopoCalcNew',nBins=125,xMin=0,xMax=2500,xTitle='H_{T} [GeV]',yLog=True,cuts=cuts,channel=channel),
581 #ChargedHiggsPlot(name='ST',distribution='jet_0_pt_ChargedHiggsCalc+jet_1_pt_ChargedHiggsCalc+((elec_1_pt_ChargedHiggsCalc>0)*elec_1_pt_ChargedHiggsCalc)+((muon_1_pt_ChargedHiggsCalc>0)*muon_1_pt_ChargedHiggsCalc)+corr_met_ChargedHiggsCalc',nBins=150,xMin=0,xMax=3000,xTitle='S_{T} [GeV]',yLog=True,cuts=cuts,channel=channel),
582 #ChargedHiggsPlot(name='nPV',distribution='nPV_ChargedHiggsCalc',nBins=50,xMin=0,xMax=50,xTitle='# Vertices',yLog=True,cuts=cuts,channel=channel),
583 #ChargedHiggsPlot(name='Nj',distribution='nSelJets_CommonCalc',nBins=7,xMin=1.5,xMax=8.5,xTitle='N_{jets}',yLog=True,cuts=cuts,channel=channel)
584 ]
585
586 if not yields.has_key('preselection'): yields['preselection']={}
587 if (not doCutTable) and (not getWhfCorrections) and (not doLimitSetting):
588 for channel in doChannels:
589 plots+=[ChargedHiggsPlot(name='Nb',distribution='(jet_0_tag_ChargedHiggsCalc==1)+(jet_1_tag_ChargedHiggsCalc==1)+(jet_2_tag_ChargedHiggsCalc==1)+(jet_3_tag_ChargedHiggsCalc==1)+(jet_4_tag_ChargedHiggsCalc==1)+(jet_5_tag_ChargedHiggsCalc==1)+(jet_6_tag_ChargedHiggsCalc==1)+(jet_7_tag_ChargedHiggsCalc==1)+(jet_8_tag_ChargedHiggsCalc==1)+(jet_9_tag_ChargedHiggsCalc==1)',nBins=7,xMin=-0.5,xMax=6.5,xTitle='N_{b-jets}',yLog=True,cuts='preselection',channel=channel),
590
591 #for top pT reweighting
592 ChargedHiggsPlot(name='TopPt_Best',distribution='BestTop_Pt_LjetsTopoCalcNew',nBins=150,xMin=0,xMax=1500,xTitle='p_{T}(best jet,W) [GeV]',yLog=True,cuts='2p',channel=channel,extraCuts='jet_2_pt_ChargedHiggsCalc>30 && jet_3_pt_ChargedHiggsCalc>30 && 400<BestJetJet2W_M_LjetsTopoCalcNew && BestJetJet2W_M_LjetsTopoCalcNew<750')
593 ]
594
595
596 for plot in plots:
597 y=plot.Prepare()
598 try: yields[plot.cuts][plot.channel]=y
599 except: pass
600 plot.Draw()
601
602 cWidth=15; nameWidth=30
603 for channel in doChannels:
604 print ("CHANNEL:"+channel).ljust(nameWidth+(2*cWidth+1)),"N_b"
605 print "".ljust(nameWidth),
606 for cuts in doCuts: print cuts.ljust(cWidth),
607 print
608 for sample in ['Data','W+light','W+c','W+b']:
609 print sample.ljust(nameWidth),
610 for cuts in doCuts:
611 print str(int(round(yields[cuts][channel][sample]))).ljust(cWidth),
612 #print str(yields[cuts][channel][sample]).ljust(cWidth),
613 print
614 for sample in samples:
615 if (channel=='el' and 'SingleMu' in sample.name) or (channel=='mu' and 'SingleEl' in sample.name): continue
616 #if sample.isData: continue
617 print sample.name.ljust(nameWidth),
618 for cuts in doCuts:
619 print str(int(round(yields[cuts][channel][sample.name]))).ljust(cWidth),
620 #print str(yields[cuts][channel][sample.name]).ljust(cWidth),
621 print
622 for sample in ['Total Background']:
623 print sample.ljust(nameWidth),
624 for cuts in doCuts:
625 print str(int(round(yields[cuts][channel][sample]))).ljust(cWidth),
626 #print str(yields[cuts][channel][sample]).ljust(cWidth),
627 print
628 print 'Background/Data'.ljust(nameWidth),
629 for cuts in doCuts:
630 print str(round(yields[cuts][channel]['Total Background']/yields[cuts][channel]['Data'],3)).ljust(cWidth),
631 print 3*'\n'
632
633 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
634
635 if getWhfCorrections:
636
637 """
638 Ndata=TMatrix(2,1)
639 Nw=TMatrix(2,2)
640 Nother=TMatrix(2,1)
641 SF=TMatrix(2,1)
642
643 Ndata[0][0]=yields['0']['mu']['Data']
644 Nother[0][0]=yields['0']['mu']['Total Background']-yields['0']['mu']['W+light']-yields['0']['mu']['W+c']-yields['0']['mu']['W+b']
645 Nw[0][0]=yields['0']['mu']['W+light']
646 Nw[0][1]=yields['0']['mu']['W+c']+yields['0']['mu']['W+b']
647
648 Ndata[1][0]=yields['1']['mu']['Data']
649 Nother[1][0]=yields['1']['mu']['Total Background']-yields['1']['mu']['W+light']-yields['1']['mu']['W+c']-yields['1']['mu']['W+b']
650 Nw[1][0]=yields['1']['mu']['W+light']
651 Nw[1][1]=yields['1']['mu']['W+c']+yields['1']['mu']['W+b']
652
653 SF=Nw.Invert()*(Ndata-Nother)
654 print "residualSF_Wj="+str(SF[0][0])
655 print "residualSF_Wc="+str(SF[1][0])
656 print "residualSF_Wb="+str(SF[1][0])
657 """
658 """
659 Ndata=TMatrix(3,1)
660 Nw=TMatrix(3,3)
661 Nother=TMatrix(3,1)
662 SF=TMatrix(3,1)
663
664 channel='mu'
665
666 Ndata[0][0]=yields['preselection'][channel]['Data']
667 Nother[0][0]=yields['preselection'][channel]['Total Background']-yields['preselection'][channel]['W+light']-yields['preselection'][channel]['W+c']-yields['preselection'][channel]['W+b']
668 Nw[0][0]=yields['preselection'][channel]['W+light']
669 Nw[0][1]=yields['preselection'][channel]['W+c']
670 Nw[0][2]=yields['preselection'][channel]['W+b']
671
672 Ndata[1][0]=yields['0'][channel]['Data']
673 Nother[1][0]=yields['0'][channel]['Total Background']-yields['0'][channel]['W+light']-yields['0'][channel]['W+c']-yields['0'][channel]['W+b']
674 Nw[1][0]=yields['0'][channel]['W+light']
675 Nw[1][1]=yields['0'][channel]['W+c']
676 Nw[1][2]=yields['0'][channel]['W+b']
677
678 Ndata[2][0]=yields['1'][channel]['Data']
679 Nother[2][0]=yields['1'][channel]['Total Background']-yields['1'][channel]['W+light']-yields['1'][channel]['W+c']-yields['1'][channel]['W+b']
680 Nw[2][0]=yields['1'][channel]['W+light']
681 Nw[2][1]=yields['1'][channel]['W+c']
682 Nw[2][2]=yields['1'][channel]['W+b']
683
684 SF=Nw.Invert()*(Ndata-Nother)
685 print 'SF W+light:',SF[0][0]
686 print 'SF W+c:',SF[1][0]
687 print 'SF W+b:',SF[2][0]
688 """
689
690 Ndata=TMatrix(3,1)
691 Nw=TMatrix(3,3)
692 Nother=TMatrix(3,1)
693 SF=TMatrix(3,1)
694
695 channel='mu'
696
697 Ndata[0][0]=yields['preselection'][channel]['Data']
698 Nother[0][0]=yields['preselection'][channel]['Total Background']-yields['preselection'][channel]['W+light']-yields['preselection'][channel]['W+c']-yields['preselection'][channel]['W+b']-yields['preselection'][channel]['TTbar_Madgraph']
699 Nw[0][0]=yields['preselection'][channel]['W+light']
700 Nw[0][1]=yields['preselection'][channel]['W+c']+yields['preselection'][channel]['W+b']
701 Nw[0][2]=yields['preselection'][channel]['TTbar_Madgraph']
702
703 Ndata[1][0]=yields['0'][channel]['Data']
704 Nother[1][0]=yields['0'][channel]['Total Background']-yields['0'][channel]['W+light']-yields['0'][channel]['W+c']-yields['0'][channel]['W+b']-yields['0'][channel]['TTbar_Madgraph']
705 Nw[1][0]=yields['0'][channel]['W+light']
706 Nw[1][1]=yields['0'][channel]['W+c']+yields['0'][channel]['W+b']
707 Nw[1][2]=yields['0'][channel]['TTbar_Madgraph']
708
709 Ndata[2][0]=yields['1'][channel]['Data']
710 Nother[2][0]=yields['1'][channel]['Total Background']-yields['1'][channel]['W+light']-yields['1'][channel]['W+c']-yields['1'][channel]['W+b']-yields['1'][channel]['TTbar_Madgraph']
711 Nw[2][0]=yields['1'][channel]['W+light']
712 Nw[2][1]=yields['1'][channel]['W+c']+yields['1'][channel]['W+b']
713 Nw[2][2]=yields['1'][channel]['TTbar_Madgraph']
714
715 SF=Nw.Invert()*(Ndata-Nother)
716 print "residualSF_Wj="+str(SF[0][0])
717 print "residualSF_Wc="+str(SF[1][0])
718 print "residualSF_Wb=residualSF_Wc"
719 print "residualSF_ttbar="+str(SF[2][0])
720