ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/StackMaker.py
Revision: 1.1
Committed: Thu Oct 18 13:44:02 2012 UTC (12 years, 6 months ago) by nmohr
Content type: text/x-python
Branch: MAIN
Log Message:
Stack as class

File Contents

# Content
1 import ROOT
2 import sys,os
3 from HistoMaker import orderandadd
4 from BetterConfigParser import BetterConfigParser
5 import TdrStyles
6 from Ratio import getRatio
7
8 class StackMaker:
9 def __init__(self, config, var,region,SignalRegion):
10 plotConfig = BetterConfigParser()
11 plotConfig.read('vhbbPlotDef.ini')
12 section='Plot:%s'%region
13 self.var = var
14 self.normalize = eval(config.get(section,'Normalize'))
15 self.log = eval(config.get(section,'log'))
16 if plotConfig.has_option('plotDef:%s'%var,'log') and not self.log:
17 self.log = eval(plotConfig.get('plotDef:%s'%var,'log'))
18 self.blind = eval(config.get(section,'blind'))
19 if self.blind: blindopt='blind'
20 else: blindopt = 'noblind'
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 if not SignalRegion: self.setup.remove('ZH')
26 self.rebin = 1
27 if config.has_option(section,'rebin'):
28 self.rebin = eval(config.get(section,'rebin'))
29 if config.has_option(section,'nBins'):
30 self.nBins = int(eval(config.get(section,'nBins'))/self.rebin)
31 else:
32 self.nBins = int(eval(plotConfig.get('plotDef:%s'%var,'nBins'))/self.rebin)
33 print self.nBins
34 if config.has_option(section,'min'):
35 self.xMin = eval(config.get(section,'min'))
36 else:
37 self.xMin = eval(plotConfig.get('plotDef:%s'%var,'min'))
38 if config.has_option(section,'max'):
39 self.xMax = eval(config.get(section,'max'))
40 else:
41 self.xMax = eval(plotConfig.get('plotDef:%s'%var,'max'))
42 self.name = plotConfig.get('plotDef:%s'%var,'relPath')
43 self.mass = config.get(section,'Signal')
44 data = config.get(section,'Datas')
45 if '<mass>' in self.name:
46 self.name = self.name.replace('<mass>',self.mass)
47 print self.name
48 if config.has_option(section, 'Datacut'):
49 datacut=config.get(section, 'Datacut')
50 else:
51 datacut = region
52 self.colorDict=eval(config.get('Plot_general','colorDict'))
53 self.typLegendDict=eval(config.get('Plot_general','typLegendDict'))
54 self.anaTag = config.get("Analysis","tag")
55 self.xAxis = plotConfig.get('plotDef:%s'%var,'xAxis')
56 self.options = [self.name,'',self.xAxis,self.nBins,self.xMin,self.xMax,'%s_%s.pdf'%(region,var),region,datacut,self.mass,data,blindopt]
57 self.config = config
58 self.datas = None
59 self.datatyps = None
60 self.overlay = None
61 self.lumi = None
62 self.histos = None
63 self.typs = None
64 print self.setup
65
66 def myText(self,txt="CMS Preliminary",ndcX=0,ndcY=0,size=0.8):
67 ROOT.gPad.Update()
68 text = ROOT.TLatex()
69 text.SetNDC()
70 text.SetTextColor(ROOT.kBlack)
71 text.SetTextSize(text.GetTextSize()*size)
72 text.DrawLatex(ndcX,ndcY,txt)
73 return text
74
75
76 def doPlot(self):
77 TdrStyles.tdrStyle()
78 self.histos, self.typs = orderandadd(self.histos,self.typs,self.setup)
79
80 c = ROOT.TCanvas(self.var,'', 600, 600)
81 c.SetFillStyle(4000)
82 c.SetFrameFillStyle(1000)
83 c.SetFrameFillColor(0)
84
85 oben = ROOT.TPad('oben','oben',0,0.3 ,1.0,1.0)
86 oben.SetBottomMargin(0)
87 oben.SetFillStyle(4000)
88 oben.SetFrameFillStyle(1000)
89 oben.SetFrameFillColor(0)
90 unten = ROOT.TPad('unten','unten',0,0.0,1.0,0.3)
91 unten.SetTopMargin(0.)
92 unten.SetBottomMargin(0.35)
93 unten.SetFillStyle(4000)
94 unten.SetFrameFillStyle(1000)
95 unten.SetFrameFillColor(0)
96
97 oben.Draw()
98 unten.Draw()
99
100 oben.cd()
101 allStack = ROOT.THStack(self.var,'')
102 l = ROOT.TLegend(0.63, 0.60,0.92,0.92)
103 l.SetLineWidth(2)
104 l.SetBorderSize(0)
105 l.SetFillColor(0)
106 l.SetFillStyle(4000)
107 l.SetTextFont(62)
108 l.SetTextSize(0.035)
109 MC_integral=0
110 MC_entries=0
111
112 for histo in self.histos:
113 MC_integral+=histo.Integral()
114 print "\033[1;32m\n\tMC integral = %s\033[1;m"%MC_integral
115
116 #ORDER AND ADD TOGETHER
117 #print typs
118 #print setup
119
120
121 k=len(self.histos)
122
123 for j in range(0,k):
124 #print histos[j].GetBinContent(1)
125 i=k-j-1
126 self.histos[i].SetFillColor(int(self.colorDict[self.setup[i]]))
127 self.histos[i].SetLineColor(1)
128 allStack.Add(self.histos[i])
129
130 d1 = ROOT.TH1F('noData','noData',self.nBins,self.xMin,self.xMax)
131 datatitle='Data'
132 addFlag = ''
133 if 'Zee' in self.datanames and 'Zmm' in self.datanames:
134 addFlag = 'Z(l^{-}l^{+})H(b#bar{b})'
135 elif 'Zee' in self.datanames:
136 addFlag = 'Z(e^{-}e^{+})H(b#bar{b})'
137 elif 'Zmm' in self.datanames:
138 addFlag = 'Z(#mu^{-}#mu^{+})H(b#bar{b})'
139 for i in range(0,len(self.datas)):
140 d1.Add(self.datas[i],1)
141 print "\033[1;32m\n\tDATA integral = %s\033[1;m"%d1.Integral()
142 flow = d1.GetEntries()-d1.Integral()
143 if flow > 0:
144 print "\033[1;31m\tU/O flow: %s\033[1;m"%flow
145
146 self.overlay.SetLineColor(2)
147 self.overlay.SetLineWidth(2)
148 self.overlay.SetFillColor(0)
149 self.overlay.SetFillStyle(4000)
150 self.overlay.SetNameTitle('Overlay','Overlay')
151
152 l.AddEntry(d1,datatitle,'P')
153 for j in range(0,k):
154 l.AddEntry(self.histos[j],self.typLegendDict[self.typs[j]],'F')
155 l.AddEntry(self.overlay,self.typLegendDict['Overlay'],'L')
156
157 if self.normalize:
158 if MC_integral != 0: stackscale=d1.Integral()/MC_integral
159 self.overlay.Scale(stackscale)
160 stackhists=allStack.GetHists()
161 for blabla in stackhists:
162 if MC_integral != 0: blabla.Scale(stackscale)
163
164 allMC=allStack.GetStack().Last().Clone()
165
166 allStack.SetTitle()
167 allStack.Draw("hist")
168 allStack.GetXaxis().SetTitle('')
169 yTitle = 'Entries'
170 if not '/' in yTitle:
171 yAppend = '%s' %(allStack.GetXaxis().GetBinWidth(1))
172 yTitle = '%s / %s' %(yTitle, yAppend)
173 allStack.GetYaxis().SetTitle(yTitle)
174 allStack.GetXaxis().SetRangeUser(self.xMin,self.xMax)
175 allStack.GetYaxis().SetRangeUser(0,20000)
176 theErrorGraph = ROOT.TGraphErrors(allMC)
177 theErrorGraph.SetFillColor(ROOT.kGray+3)
178 theErrorGraph.SetFillStyle(3013)
179 theErrorGraph.Draw('SAME2')
180 l.AddEntry(theErrorGraph,"MC uncert. (stat.)","fl")
181 Ymax = max(allStack.GetMaximum(),d1.GetMaximum())*1.7
182 if self.log:
183 allStack.SetMinimum(0.05)
184 Ymax = Ymax*ROOT.TMath.Power(10,1.6*(ROOT.TMath.Log(1.6*(Ymax/0.1))/ROOT.TMath.Log(10)))*(0.6*0.1)
185 ROOT.gPad.SetLogy()
186 allStack.SetMaximum(Ymax)
187 c.Update()
188 ROOT.gPad.SetTicks(1,1)
189 #allStack.Draw("hist")
190 l.SetFillColor(0)
191 l.SetBorderSize(0)
192
193 self.overlay.Draw('hist,same')
194 d1.Draw("E,same")
195 l.Draw()
196
197 tPrel = self.myText("CMS Preliminary",0.17,0.88,1.04)
198 tLumi = self.myText("#sqrt{s} = %s, L = %s fb^{-1}"%(self.anaTag,(float(self.lumi)/1000.)),0.17,0.83)
199 tAddFlag = self.myText(addFlag,0.17,0.78)
200
201 unten.cd()
202 ROOT.gPad.SetTicks(1,1)
203
204 ratio, error = getRatio(d1,allMC,self.xMin,self.xMax)
205 ksScore = d1.KolmogorovTest( allMC )
206 chiScore = d1.Chi2Test( allMC , "UWCHI2/NDF")
207 print ksScore
208 print chiScore
209 ratio.SetStats(0)
210 ratio.GetXaxis().SetTitle(self.xAxis)
211 ratioError = ROOT.TGraphErrors(error)
212 ratioError.SetFillColor(ROOT.kGray+3)
213 ratioError.SetFillStyle(3013)
214 ratio.Draw("E1")
215 ratioError.Draw('SAME2')
216 ratio.Draw("E1SAME")
217 ratio.SetTitle("")
218 m_one_line = ROOT.TLine(self.xMin,1,self.xMax,1)
219 m_one_line.SetLineStyle(ROOT.kDashed)
220 m_one_line.Draw("Same")
221
222 if not self.blind:
223 tKsChi = self.myText("#chi_{#nu}^{2} = %.3f K_{s} = %.3f"%(chiScore,ksScore),0.17,0.9,1.5)
224 t0 = ROOT.TText()
225 t0.SetTextSize(ROOT.gStyle.GetLabelSize()*2.4)
226 t0.SetTextFont(ROOT.gStyle.GetLabelFont())
227 if not self.log:
228 t0.DrawTextNDC(0.1059,0.96, "0")
229
230 name = '%s/%s' %(self.config.get('Directories','plotpath'),self.options[6])
231 c.Print(name)