ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/StackMaker.py
Revision: 1.6
Committed: Fri Oct 26 08:45:48 2012 UTC (12 years, 6 months ago) by nmohr
Content type: text/x-python
Branch: MAIN
CVS Tags: hcpApproval, HCP_unblinding
Changes since 1.5: +5 -1 lines
Log Message:
Different maxUncert in signal region

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