ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/myutils/StackMaker.py
Revision: 1.4
Committed: Thu Feb 7 12:36:20 2013 UTC (12 years, 3 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.3: +5 -2 lines
Log Message:
made treestack work after changes made in DC writter

File Contents

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