ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/tree_stack.py
Revision: 1.14
Committed: Wed Oct 3 10:04:29 2012 UTC (12 years, 7 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.13: +1 -1 lines
Log Message:
config for plots with light,c,b

File Contents

# Content
1 #!/usr/bin/env python
2 from samplesclass import sample
3 from printcolor import printc
4 import pickle
5 import ROOT
6 from ROOT import TFile, TTree
7 import ROOT
8 from array import array
9 from BetterConfigParser import BetterConfigParser
10 import sys, os
11 from mvainfos import mvainfo
12 #from gethistofromtree import getHistoFromTree, orderandadd
13 from Ratio import getRatio
14 from optparse import OptionParser
15 from HistoMaker import HistoMaker, orderandadd
16
17 #CONFIGURE
18 argv = sys.argv
19 parser = OptionParser()
20 parser.add_option("-P", "--path", dest="path", default="",
21 help="path to samples")
22 parser.add_option("-R", "--reg", dest="region", default="",
23 help="region to plot")
24 parser.add_option("-C", "--config", dest="config", default=[], action="append",
25 help="configuration file")
26 (opts, args) = parser.parse_args(argv)
27 if opts.config =="":
28 opts.config = "config"
29 print opts.config
30 config = BetterConfigParser()
31 config.read(opts.config)
32 anaTag = config.get("Analysis","tag")
33
34 path = opts.path
35 region = opts.region
36
37 plotConfig = BetterConfigParser()
38 plotConfig.read('vhbbPlotDef.ini')
39
40 #get locations:
41 Wdir=config.get('Directories','Wdir')
42
43 section='Plot:%s'%region
44
45 Normalize = eval(config.get(section,'Normalize'))
46 log = eval(config.get(section,'log'))
47 blind = eval(config.get(section,'blind'))
48
49 infofile = open(path+'/samples.info','r')
50 info = pickle.load(infofile)
51 infofile.close()
52
53 #options = plot.split(',')
54
55 mass = config.get(section,'Signal')
56
57 vars = (config.get(section, 'vars')).split(',')
58
59 names = [plotConfig.get('plotDef:%s'%x,'relPath') for x in vars]
60 nBins = [eval(plotConfig.get('plotDef:%s'%x,'nBins')) for x in vars]
61 xMin = [eval(plotConfig.get('plotDef:%s'%x,'min')) for x in vars]
62 xMax = [eval(plotConfig.get('plotDef:%s'%x,'max')) for x in vars]
63 xAxis = [plotConfig.get('plotDef:%s'%x,'xAxis') for x in vars]
64
65 for p in range(0,len(names)):
66 if '<mass>' in names[p]:
67 newp= names[p].replace('<mass>',mass)
68 names[p]=newp
69 print names[p]
70
71 data = config.get(section,'Datas')
72 if config.has_option(section, 'Datacut'):
73 datacut=config.get(section, 'Datacut')
74 else:
75 datacut = region
76
77 options=[]
78
79 if blind: blindopt='blind'
80 else: blindopt = 'noblind'
81
82 for i in range(0,len(vars)):
83 options.append([names[i],'',xAxis[i],nBins[i],xMin[i],xMax[i],'%s_%s.pdf'%(region,vars[i]),region,datacut,mass,data,blindopt])
84
85
86 setup=config.get('Plot_general','setup')
87 setup=setup.split(',')
88
89 samples=config.get('Plot_general','samples')
90 samples=samples.split(',')
91
92 colorDict=eval(config.get('Plot_general','colorDict'))
93 #color=color.split(',')
94
95
96 weightF=config.get('Weights','weightF')
97 Group = eval(config.get('Plot_general','Group'))
98
99
100 #GETALL AT ONCE
101
102 Plotter=HistoMaker(path,config,region,options)
103
104 #print '\nProducing Plot of %s\n'%vars[v]
105 Lhistos = [[] for _ in range(0,len(vars))]
106 Ltyps = [[] for _ in range(0,len(vars))]
107 Ldatas = [[] for _ in range(0,len(vars))]
108 Ldatatyps = [[] for _ in range(0,len(vars))]
109 Ldatanames = [[] for _ in range(0,len(vars))]
110
111
112 #Find out Lumi:
113 for job in info:
114 if job.name in data: lumi_data=float(job.lumi)
115
116 Plotter.lumi=lumi_data
117
118 for job in info:
119 if eval(job.active):
120 if job.subsamples:
121 for subsample in range(0,len(job.subnames)):
122
123 if job.subnames[subsample] in samples:
124 hTempList, typList = Plotter.getHistoFromTree(job,subsample)
125 for v in range(0,len(vars)):
126 Lhistos[v].append(hTempList[v])
127 Ltyps[v].append(Group[job.subnames[subsample]])
128
129 else:
130 if job.name in samples:
131 #print job.getpath()
132 hTempList, typList = Plotter.getHistoFromTree(job)
133 for v in range(0,len(vars)):
134 Lhistos[v].append(hTempList[v])
135 Ltyps[v].append(Group[job.name])
136
137 elif job.name in data:
138 #print 'DATA'
139 hTemp, typ = Plotter.getHistoFromTree(job)
140 for v in range(0,len(vars)):
141 Ldatas[v].append(hTemp[v])
142 Ldatatyps[v].append(typ[v])
143 Ldatanames[v].append(job.name)
144
145
146 for v in range(0,len(vars)):
147
148 histos = Lhistos[v]
149 typs = Ltyps[v]
150 datas = Ldatas[v]
151 datatyps = Ldatatyps[v]
152 datanames= Ldatanames[v]
153
154 ROOT.gROOT.SetStyle("Plain")
155
156 c = ROOT.TCanvas(vars[v],'', 700, 600)
157 c.SetFillStyle(4000)
158 c.SetFrameFillStyle(1000)
159 c.SetFrameFillColor(0)
160
161 oben = ROOT.TPad('oben','oben',0,0.3 ,1.0,1.0)
162 oben.SetBottomMargin(0)
163 oben.SetFillStyle(4000)
164 oben.SetFrameFillStyle(1000)
165 oben.SetFrameFillColor(0)
166 unten = ROOT.TPad('unten','unten',0,0.0,1.0,0.3)
167 unten.SetTopMargin(0.)
168 unten.SetBottomMargin(0.35)
169 unten.SetFillStyle(4000)
170 unten.SetFrameFillStyle(1000)
171 unten.SetFrameFillColor(0)
172
173 oben.Draw()
174 unten.Draw()
175
176 oben.cd()
177
178 allStack = ROOT.THStack(vars[v],'')
179 l = ROOT.TLegend(0.75, 0.63, 0.88, 0.88)
180 MC_integral=0
181 MC_entries=0
182
183 for histo in histos:
184 MC_integral+=histo.Integral()
185 #MC_entries+=histo.GetEntries()
186 print "\033[1;32m\n\tMC integral = %s\033[1;m"%MC_integral
187
188 #ORDER AND ADD TOGETHER
189 #print typs
190 #print setup
191 histos, typs = orderandadd(histos,typs,setup)
192
193
194 k=len(histos)
195 for j in range(0,k):
196 #print histos[j].GetBinContent(1)
197 i=k-j-1
198 histos[i].SetFillColor(int(colorDict[setup[i]]))
199 histos[i].SetLineColor(1)
200 allStack.Add(histos[i])
201 l.AddEntry(histos[j],typs[j],'F')
202
203 d1 = ROOT.TH1F('noData','noData',nBins[v],xMin[v],xMax[v])
204 datatitle=''
205 for i in range(0,len(datas)):
206 d1.Add(datas[i],1)
207 if i ==0:
208 datatitle=datanames[i]
209 else:
210 datatitle=datatitle+ ' + '+datanames[i]
211 print "\033[1;32m\n\tDATA integral = %s\033[1;m"%d1.Integral()
212 flow = d1.GetEntries()-d1.Integral()
213 if flow > 0:
214 print "\033[1;31m\tU/O flow: %s\033[1;m"%flow
215 l.AddEntry(d1,datatitle,'PL')
216
217 if Normalize:
218 stackscale=d1.Integral()/MC_integral
219 stackhists=allStack.GetHists()
220 for blabla in stackhists:
221 blabla.Scale(stackscale)
222
223 allStack.SetTitle()
224 allStack.Draw("hist")
225 allStack.GetXaxis().SetTitle('')
226 allStack.GetYaxis().SetTitle('Counts')
227 allStack.GetXaxis().SetRangeUser(xMin[v],xMax[v])
228 allStack.GetYaxis().SetRangeUser(0,20000)
229 Ymax = max(allStack.GetMaximum(),d1.GetMaximum())*1.3
230 allStack.SetMaximum(Ymax)
231 allStack.SetMinimum(0.1)
232 c.Update()
233 if log:
234 ROOT.gPad.SetLogy()
235 ROOT.gPad.SetTicks(1,1)
236 allStack.Draw("hist")
237 d1.SetMarkerStyle(21)
238 d1.Draw("P,E1,X0,same")
239 l.SetFillColor(0)
240 l.SetBorderSize(0)
241 l.Draw()
242
243
244 allMC=ROOT.TH1F('allMC','allMC',nBins[v],xMin[v],xMax[v])
245 allMC.Sumw2()
246 for bin in range(0,nBins[v]):
247 allMC.SetBinContent(bin,allStack.GetStack().Last().GetBinContent(bin))
248
249 t = ROOT.TLatex()
250 t.SetNDC()
251 t.SetTextAlign(12)
252 t.SetTextSize(0.04)
253 t.DrawLatex(0.13,0.85,"CMS Preliminary")
254 t.SetTextSize(0.03)
255 t.DrawLatex(0.13,0.79,"#sqrt{s} = %s, L = %s fb^{-1}"%(anaTag,(float(lumi_data)/1000.)))
256
257 unten.cd()
258 ROOT.gPad.SetTicks(1,1)
259
260 ratio, error, ksScore, chiScore = getRatio(d1,allMC,xMin[v],xMax[v])
261 ksScore = allMC.KolmogorovTest( d1 )
262 chiScore = allMC.Chi2Test( d1 , "UWCHI2/NDF")
263 print ksScore
264 print chiScore
265 ratio.SetStats(0)
266 ratio.GetYaxis().SetRangeUser(0,2)
267 ratio.GetYaxis().SetNdivisions(502,0)
268 ratio.GetXaxis().SetTitle(xAxis[v])
269 ratio.Draw("E1")
270 ratio.SetTitle("")
271 m_one_line = ROOT.TLine(xMin[v],1,xMax[v],1)
272 m_one_line.SetLineStyle(7)
273 m_one_line.SetLineColor(4)
274 m_one_line.Draw("Same")
275
276 t = ROOT.TLatex()
277 t.SetNDC()
278 t.SetTextAlign(12)
279 t.SetTextSize(0.07)
280 t.DrawLatex(0.12,0.9,"K_{s}: %.2f"%(ksScore))
281 t.DrawLatex(0.12,0.4,"#chi_{#nu}^{2}: %.2f"%(chiScore))
282
283 name = '%s/%s' %(config.get('Directories','plotpath'),options[v][6])
284 c.Print(name)
285
286 os.system('rm %s/tmp_plotCache_%s*'%(config.get('Directories','plotpath'),region))
287 print 'i am done!\n'
288
289 sys.exit(0)