ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/myutils/HistoMaker.py
Revision: 1.7
Committed: Wed Feb 6 12:55:04 2013 UTC (12 years, 3 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.6: +10 -4 lines
Log Message:
bugfix

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