1 |
nmohr |
1.2 |
import sys,os
|
2 |
peller |
1.1 |
import pickle
|
3 |
|
|
import ROOT
|
4 |
|
|
from array import array
|
5 |
nmohr |
1.2 |
from printcolor import printc
|
6 |
peller |
1.1 |
from BetterConfigParser import BetterConfigParser
|
7 |
nmohr |
1.2 |
from TreeCache import TreeCache
|
8 |
peller |
1.1 |
|
9 |
|
|
class HistoMaker:
|
10 |
nmohr |
1.2 |
def __init__(self, samples, path, config, optionsList):
|
11 |
peller |
1.1 |
self.path = path
|
12 |
|
|
self.config = config
|
13 |
|
|
self.optionsList = optionsList
|
14 |
|
|
self.lumi=0.
|
15 |
nmohr |
1.2 |
self.cuts = []
|
16 |
|
|
for options in optionsList:
|
17 |
|
|
self.cuts.append(options['cut'])
|
18 |
|
|
self.tc = TreeCache(self.cuts,samples,path)
|
19 |
peller |
1.1 |
|
20 |
|
|
|
21 |
nmohr |
1.2 |
def get_histos_from_tree(self,job):
|
22 |
|
|
if self.lumi == 0:
|
23 |
|
|
raise Exception("You're trying to plot with no lumi")
|
24 |
peller |
1.1 |
|
25 |
|
|
hTreeList=[]
|
26 |
|
|
groupList=[]
|
27 |
|
|
|
28 |
|
|
#get the conversion rate in case of BDT plots
|
29 |
|
|
TrainFlag = eval(self.config.get('Analysis','TrainFlag'))
|
30 |
|
|
BDT_add_cut='EventForTraining == 0'
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
plot_path = self.config.get('Directories','plotpath')
|
34 |
|
|
addOverFlow=eval(self.config.get('Plot_general','addOverFlow'))
|
35 |
|
|
|
36 |
|
|
# get all Histos at once
|
37 |
|
|
for options in self.optionsList:
|
38 |
nmohr |
1.2 |
name=job.name
|
39 |
|
|
group=job.group
|
40 |
|
|
treeVar=options['var']
|
41 |
|
|
name=options['name']
|
42 |
|
|
nBins=int(options['nBins'])
|
43 |
|
|
xMin=float(options['xMin'])
|
44 |
|
|
xMax=float(options['xMax'])
|
45 |
|
|
weightF=options['weight']
|
46 |
|
|
treeCut='%s'%(options['cut'])
|
47 |
|
|
CuttedTree = self.tc.get_tree(job,treeCut)
|
48 |
peller |
1.1 |
|
49 |
|
|
#options
|
50 |
|
|
|
51 |
|
|
if job.type != 'DATA':
|
52 |
|
|
if CuttedTree.GetEntries():
|
53 |
|
|
|
54 |
nmohr |
1.2 |
if 'RTight' in treeVar or 'RMed' in treeVar:
|
55 |
|
|
drawoption = '(%s)*(%s)'%(weightF,BDT_add_cut)
|
56 |
|
|
else:
|
57 |
|
|
drawoption = '%s'%(weightF)
|
58 |
peller |
1.1 |
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), drawoption, "goff,e")
|
59 |
|
|
full=True
|
60 |
|
|
else:
|
61 |
|
|
full=False
|
62 |
|
|
elif job.type == 'DATA':
|
63 |
nmohr |
1.2 |
if options['blind'] == 'blind':
|
64 |
peller |
1.1 |
output.cd()
|
65 |
|
|
if treeVar == 'H.mass':
|
66 |
|
|
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<90. || '+treeVar + '>150.' , "goff,e")
|
67 |
|
|
else:
|
68 |
|
|
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e")
|
69 |
|
|
|
70 |
|
|
else:
|
71 |
|
|
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'1', "goff,e")
|
72 |
|
|
full = True
|
73 |
|
|
if full:
|
74 |
|
|
hTree = ROOT.gDirectory.Get(name)
|
75 |
|
|
else:
|
76 |
|
|
hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
|
77 |
|
|
hTree.Sumw2()
|
78 |
|
|
if job.type != 'DATA':
|
79 |
|
|
if 'RTight' in treeVar or 'RMed' in treeVar:
|
80 |
|
|
if TrainFlag:
|
81 |
|
|
MC_rescale_factor=2.
|
82 |
|
|
print 'I RESCALE BY 2.0'
|
83 |
nmohr |
1.2 |
else:
|
84 |
|
|
MC_rescale_factor = 1.
|
85 |
|
|
ScaleFactor = self.tc.get_scale(job,self.config,self.lumi)*MC_rescale_factor
|
86 |
|
|
else:
|
87 |
|
|
ScaleFactor = self.tc.get_scale(job,self.config,self.lumi)
|
88 |
peller |
1.1 |
if ScaleFactor != 0:
|
89 |
|
|
hTree.Scale(ScaleFactor)
|
90 |
|
|
#print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
|
91 |
|
|
if addOverFlow:
|
92 |
|
|
uFlow = hTree.GetBinContent(0)+hTree.GetBinContent(1)
|
93 |
|
|
oFlow = hTree.GetBinContent(hTree.GetNbinsX()+1)+hTree.GetBinContent(hTree.GetNbinsX())
|
94 |
|
|
uFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(0),2)+ROOT.TMath.Power(hTree.GetBinError(1),2))
|
95 |
|
|
oFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()),2)+ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()+1),2))
|
96 |
|
|
hTree.SetBinContent(1,uFlow)
|
97 |
|
|
hTree.SetBinContent(hTree.GetNbinsX(),oFlow)
|
98 |
|
|
hTree.SetBinError(1,uFlowErr)
|
99 |
|
|
hTree.SetBinError(hTree.GetNbinsX(),oFlowErr)
|
100 |
|
|
hTree.SetDirectory(0)
|
101 |
|
|
hTreeList.append(hTree)
|
102 |
|
|
groupList.append(group)
|
103 |
|
|
|
104 |
|
|
return hTreeList, groupList
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
######################
|
108 |
|
|
def orderandadd(histos,typs,setup):
|
109 |
|
|
#ORDER AND ADD TOGETHER
|
110 |
|
|
ordnung=[]
|
111 |
|
|
ordnungtyp=[]
|
112 |
|
|
num=[0]*len(setup)
|
113 |
|
|
for i in range(0,len(setup)):
|
114 |
|
|
for j in range(0,len(histos)):
|
115 |
|
|
if typs[j] in setup[i]:
|
116 |
|
|
num[i]+=1
|
117 |
|
|
ordnung.append(histos[j])
|
118 |
|
|
ordnungtyp.append(typs[j])
|
119 |
|
|
del histos
|
120 |
|
|
del typs
|
121 |
|
|
histos=ordnung
|
122 |
|
|
typs=ordnungtyp
|
123 |
|
|
print typs
|
124 |
|
|
for k in range(0,len(num)):
|
125 |
|
|
for m in range(0,num[k]):
|
126 |
|
|
if m > 0:
|
127 |
|
|
#add
|
128 |
|
|
histos[k].Add(histos[k+1],1)
|
129 |
|
|
printc('magenta','','\t--> added %s to %s'%(typs[k],typs[k+1]))
|
130 |
|
|
del histos[k+1]
|
131 |
|
|
del typs[k+1]
|
132 |
|
|
del histos[len(setup):]
|
133 |
|
|
del typs[len(setup):]
|
134 |
|
|
return histos, typs
|