ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/StackMaker.py
Revision: 1.5
Committed: Thu Oct 25 09:49:58 2012 UTC (12 years, 6 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
15 bins, ctrl regions and blinding

File Contents

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