ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/myutils/HistoMaker.py
Revision: 1.15
Committed: Tue Mar 26 14:23:12 2013 UTC (12 years, 1 month ago) by nmohr
Content type: text/x-python
Branch: MAIN
CVS Tags: LHCP_PreAppFreeze
Changes since 1.14: +8 -8 lines
Log Message:
More efficient HistoMaker

File Contents

# Content
1 import sys,os
2 import pickle
3 import ROOT
4 from array import array
5 from printcolor import printc
6 from BetterConfigParser import BetterConfigParser
7 from TreeCache import TreeCache
8 from math import sqrt
9 from copy import copy
10
11 class HistoMaker:
12 def __init__(self, samples, path, config, optionsList,GroupDict=None):
13 self.path = path
14 self.config = config
15 self.optionsList = optionsList
16 self.nBins = optionsList[0]['nBins']
17 self.lumi=0.
18 self.cuts = []
19 for options in optionsList:
20 self.cuts.append(options['cut'])
21 #print self.cuts
22 #self.tc = TreeCache(self.cuts,samples,path)
23 self.tc = TreeCache(self.cuts,samples,path,config)
24 self._rebin = False
25 self.mybinning = None
26 self.GroupDict=GroupDict
27 self.calc_rebin_flag = False
28 VHbbNameSpace=config.get('VHbbNameSpace','library')
29 ROOT.gSystem.Load(VHbbNameSpace)
30
31 def get_histos_from_tree(self,job):
32 if self.lumi == 0:
33 raise Exception("You're trying to plot with no lumi")
34
35 hTreeList=[]
36
37 #get the conversion rate in case of BDT plots
38 TrainFlag = eval(self.config.get('Analysis','TrainFlag'))
39 BDT_add_cut='EventForTraining == 0'
40
41
42 plot_path = self.config.get('Directories','plotpath')
43 addOverFlow=eval(self.config.get('Plot_general','addOverFlow'))
44
45 # get all Histos at once
46 CuttedTree = self.tc.get_tree(job,'1')
47 for options in self.optionsList:
48 name=job.name
49 if self.GroupDict is None:
50 group=job.group
51 else:
52 group=self.GroupDict[job.name]
53 treeVar=options['var']
54 name=options['name']
55 if self._rebin or self.calc_rebin_flag:
56 nBins = self.nBins
57 else:
58 nBins = int(options['nBins'])
59 xMin=float(options['xMin'])
60 xMax=float(options['xMax'])
61 weightF=options['weight']
62 treeCut='%s'%(options['cut'])
63
64 #options
65
66 if job.type != 'DATA':
67 if CuttedTree.GetEntries():
68 if 'RTight' in treeVar or 'RMed' in treeVar:
69 drawoption = '(%s)*(%s & %s)'%(weightF,treeCut,BDT_add_cut)
70 else:
71 drawoption = '(%s)*(%s)'%(weightF,treeCut)
72 CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), drawoption, "goff,e")
73 full=True
74 else:
75 full=False
76 elif job.type == 'DATA':
77 if options['blind']:
78 if treeVar == 'H.mass':
79 CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<90. || '+treeVar + '>150. & %s' %treeCut, "goff,e")
80 else:
81 CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0 & %s'%treeCut, "goff,e")
82
83 else:
84 CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'%s' %treeCut, "goff,e")
85 full = True
86 if full:
87 hTree = ROOT.gDirectory.Get(name)
88 else:
89 hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
90 hTree.Sumw2()
91 if job.type != 'DATA':
92 if 'RTight' in treeVar or 'RMed' in treeVar:
93 if TrainFlag:
94 MC_rescale_factor=2.
95 #print 'I RESCALE BY 2.0'
96 else:
97 MC_rescale_factor = 1.
98 ScaleFactor = self.tc.get_scale(job,self.config,self.lumi)*MC_rescale_factor
99 else:
100 ScaleFactor = self.tc.get_scale(job,self.config,self.lumi)
101 if ScaleFactor != 0:
102 hTree.Scale(ScaleFactor)
103 #print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
104 if addOverFlow:
105 uFlow = hTree.GetBinContent(0)+hTree.GetBinContent(1)
106 oFlow = hTree.GetBinContent(hTree.GetNbinsX()+1)+hTree.GetBinContent(hTree.GetNbinsX())
107 uFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(0),2)+ROOT.TMath.Power(hTree.GetBinError(1),2))
108 oFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()),2)+ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()+1),2))
109 hTree.SetBinContent(1,uFlow)
110 hTree.SetBinContent(hTree.GetNbinsX(),oFlow)
111 hTree.SetBinError(1,uFlowErr)
112 hTree.SetBinError(hTree.GetNbinsX(),oFlowErr)
113 hTree.SetDirectory(0)
114 gDict = {}
115 if self._rebin:
116 gDict[group] = self.mybinning.rebin(hTree)
117 del hTree
118 else:
119 #print 'not rebinning %s'%job.name
120 gDict[group] = hTree
121 hTreeList.append(gDict)
122 CuttedTree.IsA().Destructor(CuttedTree)
123 del CuttedTree
124 return hTreeList
125
126 @property
127 def rebin(self):
128 return self._rebin
129
130 @property
131 def rebin(self, value):
132 if self._rebin and value:
133 return True
134 elif self._rebin and not value:
135 self.nBins = self.norebin_nBins
136 self._rebin = False
137 elif not self._rebin and value:
138 if self.mybinning is None:
139 raise Exception('define rebinning first')
140 else:
141 self.nBins = self.rebin_nBins
142 self._rebin = True
143 return True
144 elif not self._rebin and not self.value:
145 return False
146
147 def calc_rebin(self, bg_list, nBins_start=1000, tolerance=0.35):
148 self.calc_rebin_flag = True
149 self.norebin_nBins = copy(self.nBins)
150 self.rebin_nBins = nBins_start
151 self.nBins = nBins_start
152 i=0
153 #add all together:
154 print '\n\t...calculating rebinning...'
155 for job in bg_list:
156 htree = self.get_histos_from_tree(job)[0].values()[0]
157 if not i:
158 totalBG = copy(htree)
159 else:
160 totalBG.Add(htree,1)
161 del htree
162 i+=1
163 ErrorR=0
164 ErrorL=0
165 TotR=0
166 TotL=0
167 binR=self.rebin_nBins
168 binL=1
169 rel=1.0
170 #---- from right
171 while rel > tolerance:
172 TotR+=totalBG.GetBinContent(binR)
173 ErrorR=sqrt(ErrorR**2+totalBG.GetBinError(binR)**2)
174 binR-=1
175 if not TotR == 0 and not ErrorR == 0:
176 rel=ErrorR/TotR
177 #print rel
178 #print 'upper bin is %s'%binR
179
180 #---- from left
181 rel=1.0
182 while rel > tolerance:
183 TotL+=totalBG.GetBinContent(binL)
184 ErrorL=sqrt(ErrorL**2+totalBG.GetBinError(binL)**2)
185 binL+=1
186 if not TotL == 0 and not ErrorL == 0:
187 rel=ErrorL/TotL
188 #print rel
189 #it's the lower edge
190 binL+=1
191 #print 'lower bin is %s'%binL
192
193 inbetween=binR-binL
194 stepsize=int(inbetween)/(int(self.norebin_nBins)-2)
195 modulo = int(inbetween)%(int(self.norebin_nBins)-2)
196
197 #print 'stepsize %s'% stepsize
198 #print 'modulo %s'%modulo
199 binlist=[binL]
200 for i in range(0,int(self.norebin_nBins)-3):
201 binlist.append(binlist[-1]+stepsize)
202 binlist[-1]+=modulo
203 binlist.append(binR)
204 binlist.append(self.rebin_nBins+1)
205 #print 'binning set to %s'%binlist
206 self.mybinning = Rebinner(int(self.norebin_nBins),array('d',[-1.0]+[totalBG.GetBinLowEdge(i) for i in binlist]),True)
207 self._rebin = True
208 print '\t > rebinning is set <\n'
209
210 @staticmethod
211 def orderandadd(histo_dicts,setup):
212 ordered_histo_dict = {}
213 for sample in setup:
214 nSample = 0
215 for histo_dict in histo_dicts:
216 if histo_dict.has_key(sample):
217 if nSample == 0:
218 ordered_histo_dict[sample] = histo_dict[sample].Clone()
219 else:
220 printc('magenta','','\t--> added %s to %s'%(sample,sample))
221 ordered_histo_dict[sample].Add(histo_dict[sample])
222 nSample += 1
223 del histo_dicts
224 return ordered_histo_dict
225
226 class Rebinner:
227 def __init__(self,nBins,lowedgearray,active=True):
228 self.lowedgearray=lowedgearray
229 self.nBins=nBins
230 self.active=active
231 def rebin(self, histo):
232 if not self.active: return histo
233 #print histo.Integral()
234 ROOT.gDirectory.Delete('hnew')
235 histo.Rebin(self.nBins,'hnew',self.lowedgearray)
236 binhisto=ROOT.gDirectory.Get('hnew')
237 #print binhisto.Integral()
238 newhisto=ROOT.TH1F('new','new',self.nBins,self.lowedgearray[0],self.lowedgearray[-1])
239 newhisto.Sumw2()
240 for bin in range(1,self.nBins+1):
241 newhisto.SetBinContent(bin,binhisto.GetBinContent(bin))
242 newhisto.SetBinError(bin,binhisto.GetBinError(bin))
243 newhisto.SetName(binhisto.GetName())
244 newhisto.SetTitle(binhisto.GetTitle())
245 #print newhisto.Integral()
246 del histo
247 del binhisto
248 return copy(newhisto)