ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/StackMaker.py
Revision: 1.3
Committed: Fri Oct 19 15:47:19 2012 UTC (12 years, 6 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.2: +1 -1 lines
Log Message:
conflict removal

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