9 |
|
import sys |
10 |
|
|
11 |
|
class HistoMaker: |
12 |
< |
def __init__(self, path, config, optionsList,rescale=1,which_weightF='weightF'): |
12 |
> |
def __init__(self, path, config, region, optionsList,rescale=1,which_weightF='weightF'): |
13 |
|
self.path = path |
14 |
|
self.config = config |
15 |
|
self.optionsList = optionsList |
16 |
|
self.rescale = rescale |
17 |
|
self.which_weightF=which_weightF |
18 |
< |
|
19 |
< |
|
18 |
> |
self.region = region |
19 |
> |
self.lumi=0. |
20 |
|
|
21 |
|
def getScale(self,job,subsample=-1): |
22 |
|
anaTag=self.config.get('Analysis','tag') |
24 |
|
CountWithPU = input.Get("CountWithPU") |
25 |
|
CountWithPU2011B = input.Get("CountWithPU2011B") |
26 |
|
#print lumi*xsecs[i]/hist.GetBinContent(1) |
27 |
– |
|
27 |
|
if subsample>-1: |
28 |
|
xsec=float(job.xsec[subsample]) |
29 |
|
sf=float(job.sf[subsample]) |
30 |
|
else: |
31 |
|
xsec=float(job.xsec) |
32 |
|
sf=float(job.sf) |
34 |
– |
|
35 |
– |
|
33 |
|
theScale = 1. |
34 |
|
if anaTag == '7TeV': |
35 |
< |
theScale = float(job.lumi)*xsec*sf/(0.46502*CountWithPU.GetBinContent(1)+0.53498*CountWithPU2011B.GetBinContent(1))*self.rescale/float(job.split) |
35 |
> |
theScale = float(self.lumi)*xsec*sf/(0.46502*CountWithPU.GetBinContent(1)+0.53498*CountWithPU2011B.GetBinContent(1))*self.rescale/float(job.split) |
36 |
|
elif anaTag == '8TeV': |
37 |
< |
theScale = float(job.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*self.rescale/float(job.split) |
37 |
> |
theScale = float(self.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*self.rescale/float(job.split) |
38 |
|
return theScale |
39 |
|
|
40 |
|
|
41 |
|
def getHistoFromTree(self,job,subsample=-1): |
42 |
< |
|
42 |
> |
if self.lumi == 0: raise Exception("You're trying to plot with no lumi") |
43 |
> |
|
44 |
|
hTreeList=[] |
45 |
|
groupList=[] |
46 |
|
|
47 |
< |
output=TFile.Open(self.path+'/tmp_%s.root'%job.name,'recreate') |
47 |
> |
|
48 |
> |
plot_path = self.config.get('Directories','plotpath') |
49 |
> |
addOverFlow=eval(self.config.get('Plot_general','addOverFlow')) |
50 |
> |
|
51 |
> |
# define treeCut |
52 |
|
if job.type != 'DATA': |
53 |
< |
|
54 |
< |
if type(self.optionsList[0][7])==str: |
55 |
< |
cutcut=self.config.get('Cuts',self.optionsList[0][7]) |
56 |
< |
elif type(self.optionsList[0][7])==list: |
57 |
< |
cutcut=self.config.get('Cuts',self.optionsList[0][7][0]) |
58 |
< |
cutcut=cutcut.replace(self.optionsList[0][7][1],self.optionsList[0][7][2]) |
59 |
< |
print cutcut |
53 |
> |
if type(self.region)==str: |
54 |
> |
cutcut=self.config.get('Cuts',self.region) |
55 |
> |
elif type(self.region)==list: |
56 |
> |
#replace vars with other vars in the cutstring (used in DC writer) |
57 |
> |
cutcut=self.config.get('Cuts',self.region[0]) |
58 |
> |
cutcut=cutcut.replace(self.region[1],self.region[2]) |
59 |
> |
#print cutcut |
60 |
|
if subsample>-1: |
61 |
|
treeCut='%s & %s & EventForTraining == 0'%(cutcut,job.subcuts[subsample]) |
62 |
|
else: |
63 |
|
treeCut='%s & EventForTraining == 0'%(cutcut) |
62 |
– |
|
64 |
|
elif job.type == 'DATA': |
65 |
< |
cutcut=self.config.get('Cuts',self.optionsList[0][8]) |
65 |
> |
cutcut=self.config.get('Cuts',self.region) |
66 |
|
treeCut='%s'%(cutcut) |
67 |
+ |
|
68 |
+ |
# get and skim the Trees |
69 |
+ |
output=TFile.Open(plot_path+'/tmp_plotCache_%s_%s.root'%(self.region,job.identifier),'recreate') |
70 |
|
input = TFile.Open(self.path+'/'+job.getpath(),'read') |
71 |
|
Tree = input.Get(job.tree) |
72 |
+ |
output.cd() |
73 |
+ |
CuttedTree=Tree.CopyTree(treeCut) |
74 |
+ |
|
75 |
+ |
# get all Histos at once |
76 |
|
weightF=self.config.get('Weights',self.which_weightF) |
69 |
– |
if job.type != 'DATA': |
70 |
– |
#if Tree.GetEntries(): |
71 |
– |
output.cd() |
72 |
– |
CuttedTree=Tree.CopyTree(treeCut) |
73 |
– |
elif job.type == 'DATA': |
74 |
– |
|
75 |
– |
output.cd() |
76 |
– |
CuttedTree=Tree.CopyTree(treeCut) |
77 |
– |
|
77 |
|
for options in self.optionsList: |
79 |
– |
|
78 |
|
if subsample>-1: |
79 |
|
name=job.subnames[subsample] |
80 |
|
group=job.group[subsample] |
81 |
|
else: |
82 |
|
name=job.name |
83 |
|
group=job.group |
86 |
– |
|
87 |
– |
|
84 |
|
treeVar=options[0] |
85 |
|
name=options[1] |
86 |
|
nBins=int(options[3]) |
95 |
|
else: |
96 |
|
full=False |
97 |
|
elif job.type == 'DATA': |
102 |
– |
|
98 |
|
if options[11] == 'blind': |
99 |
|
output.cd() |
100 |
|
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e") |
108 |
|
output.cd() |
109 |
|
hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax) |
110 |
|
hTree.Sumw2() |
116 |
– |
|
111 |
|
if job.type != 'DATA': |
112 |
|
ScaleFactor = self.getScale(job,subsample) |
113 |
|
if ScaleFactor != 0: |
114 |
|
hTree.Scale(ScaleFactor) |
121 |
– |
|
115 |
|
#print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral()) |
116 |
+ |
if addOverFlow: |
117 |
+ |
uFlow = hTree.GetBinContent(0)+hTree.GetBinContent(1) |
118 |
+ |
oFlow = hTree.GetBinContent(hTree.GetNbinsX()+1)+hTree.GetBinContent(hTree.GetNbinsX()) |
119 |
+ |
uFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(0),2)+ROOT.TMath.Power(hTree.GetBinError(1),2)) |
120 |
+ |
oFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()),2)+ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()+1),2)) |
121 |
+ |
hTree.SetBinContent(1,uFlow) |
122 |
+ |
hTree.SetBinContent(hTree.GetNbinsX(),oFlow) |
123 |
+ |
hTree.SetBinError(1,uFlowErr) |
124 |
+ |
hTree.SetBinError(hTree.GetNbinsX(),oFlowErr) |
125 |
|
hTree.SetDirectory(0) |
126 |
|
input.Close() |
127 |
|
hTreeList.append(hTree) |