9 |
|
from copy import copy |
10 |
|
|
11 |
|
class HistoMaker: |
12 |
< |
def __init__(self, samples, path, config, optionsList): |
12 |
> |
def __init__(self, samples, path, config, optionsList,GroupDict=None): |
13 |
|
self.path = path |
14 |
|
self.config = config |
15 |
|
self.optionsList = optionsList |
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 |
|
|
29 |
|
def get_histos_from_tree(self,job): |
30 |
|
if self.lumi == 0: |
43 |
|
# get all Histos at once |
44 |
|
for options in self.optionsList: |
45 |
|
name=job.name |
46 |
< |
group=job.group |
46 |
> |
if self.GroupDict is None: |
47 |
> |
group=job.group |
48 |
> |
else: |
49 |
> |
group=self.GroupDict[job.name] |
50 |
|
treeVar=options['var'] |
51 |
|
name=options['name'] |
52 |
< |
nBins=self.nBins |
53 |
< |
#int(options['nBins']) |
52 |
> |
if self._rebin or self.calc_rebin_flag: |
53 |
> |
nBins = self.nBins |
54 |
> |
else: |
55 |
> |
nBins = int(options['nBins']) |
56 |
|
xMin=float(options['xMin']) |
57 |
|
xMax=float(options['xMax']) |
58 |
|
weightF=options['weight'] |
63 |
|
|
64 |
|
if job.type != 'DATA': |
65 |
|
if CuttedTree.GetEntries(): |
58 |
– |
|
66 |
|
if 'RTight' in treeVar or 'RMed' in treeVar: |
67 |
|
drawoption = '(%s)*(%s)'%(weightF,BDT_add_cut) |
68 |
|
else: |
72 |
|
else: |
73 |
|
full=False |
74 |
|
elif job.type == 'DATA': |
75 |
< |
if options['blind']: |
75 |
> |
if eval(options['blind']): |
76 |
|
if treeVar == 'H.mass': |
77 |
|
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<90. || '+treeVar + '>150.' , "goff,e") |
78 |
|
else: |
90 |
|
if 'RTight' in treeVar or 'RMed' in treeVar: |
91 |
|
if TrainFlag: |
92 |
|
MC_rescale_factor=2. |
93 |
< |
print 'I RESCALE BY 2.0' |
93 |
> |
#print 'I RESCALE BY 2.0' |
94 |
|
else: |
95 |
|
MC_rescale_factor = 1. |
96 |
|
ScaleFactor = self.tc.get_scale(job,self.config,self.lumi)*MC_rescale_factor |
112 |
|
gDict = {} |
113 |
|
if self._rebin: |
114 |
|
gDict[group] = self.mybinning.rebin(hTree) |
115 |
+ |
del hTree |
116 |
|
else: |
117 |
+ |
#print 'not rebinning %s'%job.name |
118 |
|
gDict[group] = hTree |
119 |
|
hTreeList.append(gDict) |
120 |
+ |
CuttedTree.IsA().Destructor(CuttedTree) |
121 |
+ |
del CuttedTree |
122 |
|
return hTreeList |
123 |
|
|
124 |
|
@property |
143 |
|
return False |
144 |
|
|
145 |
|
def calc_rebin(self, bg_list, nBins_start=1000, tolerance=0.35): |
146 |
< |
self.norebin_nBins = self.nBins |
146 |
> |
self.calc_rebin_flag = True |
147 |
> |
self.norebin_nBins = copy(self.nBins) |
148 |
|
self.rebin_nBins = nBins_start |
149 |
|
self.nBins = nBins_start |
150 |
|
i=0 |
151 |
|
#add all together: |
152 |
+ |
print '\n\t...calculating rebinning...' |
153 |
|
for job in bg_list: |
154 |
+ |
htree = self.get_histos_from_tree(job)[0].values()[0] |
155 |
|
if not i: |
156 |
< |
totalBG = self.get_histos_from_tree(job)[0].values()[0] |
156 |
> |
totalBG = copy(htree) |
157 |
|
else: |
158 |
< |
totalBG.Add(self.get_histos_from_tree(job)[0].values()[0],1) |
158 |
> |
totalBG.Add(htree,1) |
159 |
> |
del htree |
160 |
|
i+=1 |
161 |
|
ErrorR=0 |
162 |
|
ErrorL=0 |
192 |
|
stepsize=int(inbetween)/(int(self.norebin_nBins)-2) |
193 |
|
modulo = int(inbetween)%(int(self.norebin_nBins)-2) |
194 |
|
|
195 |
< |
#print'stepsize %s'% stepsize |
195 |
> |
#print 'stepsize %s'% stepsize |
196 |
|
#print 'modulo %s'%modulo |
197 |
|
binlist=[binL] |
198 |
|
for i in range(0,int(self.norebin_nBins)-3): |
200 |
|
binlist[-1]+=modulo |
201 |
|
binlist.append(binR) |
202 |
|
binlist.append(self.rebin_nBins+1) |
203 |
< |
|
203 |
> |
#print 'binning set to %s'%binlist |
204 |
|
self.mybinning = Rebinner(int(self.norebin_nBins),array('d',[-1.0]+[totalBG.GetBinLowEdge(i) for i in binlist]),True) |
205 |
|
self._rebin = True |
206 |
< |
|
206 |
> |
print '\t > rebinning is set <\n' |
207 |
|
|
208 |
|
@staticmethod |
209 |
|
def orderandadd(histo_dicts,setup): |
213 |
|
for histo_dict in histo_dicts: |
214 |
|
if histo_dict.has_key(sample): |
215 |
|
if nSample == 0: |
216 |
< |
ordered_histo_dict[sample] = histo_dict[sample] |
216 |
> |
ordered_histo_dict[sample] = histo_dict[sample].Clone() |
217 |
|
else: |
218 |
|
printc('magenta','','\t--> added %s to %s'%(sample,sample)) |
219 |
|
ordered_histo_dict[sample].Add(histo_dict[sample]) |
220 |
|
nSample += 1 |
221 |
+ |
del histo_dicts |
222 |
|
return ordered_histo_dict |
223 |
|
|
224 |
|
class Rebinner: |
228 |
|
self.active=active |
229 |
|
def rebin(self, histo): |
230 |
|
if not self.active: return histo |
215 |
– |
#print 'rebinning' |
231 |
|
#print histo.Integral() |
232 |
|
ROOT.gDirectory.Delete('hnew') |
233 |
|
histo.Rebin(self.nBins,'hnew',self.lowedgearray) |
241 |
|
newhisto.SetName(binhisto.GetName()) |
242 |
|
newhisto.SetTitle(binhisto.GetTitle()) |
243 |
|
#print newhisto.Integral() |
244 |
+ |
del histo |
245 |
+ |
del binhisto |
246 |
|
return copy(newhisto) |