6 |
|
import ROOT |
7 |
|
from array import array |
8 |
|
from BetterConfigParser import BetterConfigParser |
9 |
< |
import sys |
9 |
> |
import sys,os |
10 |
|
|
11 |
|
class HistoMaker: |
12 |
|
def __init__(self, path, config, region, optionsList,rescale=1,which_weightF='weightF'): |
18 |
|
self.region = region |
19 |
|
self.lumi=0. |
20 |
|
|
21 |
< |
def getScale(self,job,subsample=-1): |
21 |
> |
def getScale(self,job,subsample=-1,MC_rescale_factor=1): |
22 |
|
anaTag=self.config.get('Analysis','tag') |
23 |
|
input = TFile.Open(self.path+'/'+job.getpath()) |
24 |
|
CountWithPU = input.Get("CountWithPU") |
32 |
|
sf=float(job.sf) |
33 |
|
theScale = 1. |
34 |
|
if anaTag == '7TeV': |
35 |
< |
theScale = float(self.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))*MC_rescale_factor/float(job.split) |
36 |
|
elif anaTag == '8TeV': |
37 |
< |
theScale = float(self.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*self.rescale/float(job.split) |
37 |
> |
theScale = float(self.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*MC_rescale_factor/float(job.split) |
38 |
|
return theScale |
39 |
|
|
40 |
|
|
44 |
|
hTreeList=[] |
45 |
|
groupList=[] |
46 |
|
|
47 |
+ |
#get the conversion rate in case of BDT plots |
48 |
+ |
TrainFlag = eval(self.config.get('Analysis','TrainFlag')) |
49 |
+ |
BDT_add_cut='EventForTraining == 0' |
50 |
+ |
|
51 |
|
|
52 |
|
plot_path = self.config.get('Directories','plotpath') |
53 |
+ |
addOverFlow=eval(self.config.get('Plot_general','addOverFlow')) |
54 |
|
|
55 |
+ |
scratchDir = os.environ["TMPDIR"] |
56 |
+ |
#scratchDir = '/shome/peller/' |
57 |
|
# define treeCut |
58 |
|
if job.type != 'DATA': |
59 |
|
if type(self.region)==str: |
64 |
|
cutcut=cutcut.replace(self.region[1],self.region[2]) |
65 |
|
#print cutcut |
66 |
|
if subsample>-1: |
67 |
< |
treeCut='%s & %s & EventForTraining == 0'%(cutcut,job.subcuts[subsample]) |
67 |
> |
treeCut='%s & %s'%(cutcut,job.subcuts[subsample]) |
68 |
|
else: |
69 |
< |
treeCut='%s & EventForTraining == 0'%(cutcut) |
69 |
> |
treeCut='%s'%(cutcut) |
70 |
|
elif job.type == 'DATA': |
71 |
|
cutcut=self.config.get('Cuts',self.region) |
72 |
|
treeCut='%s'%(cutcut) |
73 |
|
|
74 |
|
# get and skim the Trees |
75 |
< |
output=TFile.Open(plot_path+'/tmp_plotCache_%s_%s.root'%(self.region,job.identifier),'recreate') |
75 |
> |
output=TFile.Open(scratchDir+'/tmp_plotCache_%s_%s.root'%(self.region,job.identifier),'recreate') |
76 |
|
input = TFile.Open(self.path+'/'+job.getpath(),'read') |
77 |
|
Tree = input.Get(job.tree) |
78 |
|
output.cd() |
93 |
|
xMin=float(options[4]) |
94 |
|
xMax=float(options[5]) |
95 |
|
|
96 |
+ |
#options |
97 |
+ |
|
98 |
|
if job.type != 'DATA': |
99 |
|
if CuttedTree.GetEntries(): |
100 |
+ |
|
101 |
+ |
if 'RTight' in treeVar or 'RMed' in treeVar: drawoption = '(%s)*(%s)'%(weightF,BDT_add_cut) |
102 |
+ |
else: drawoption = '%s'%(weightF) |
103 |
|
output.cd() |
104 |
< |
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), weightF, "goff,e") |
104 |
> |
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), drawoption, "goff,e") |
105 |
|
full=True |
106 |
|
else: |
107 |
|
full=False |
108 |
|
elif job.type == 'DATA': |
109 |
|
if options[11] == 'blind': |
110 |
|
output.cd() |
111 |
< |
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e") |
111 |
> |
if treeVar == 'H.mass': |
112 |
> |
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<80. || '+treeVar + '>150.' , "goff,e") |
113 |
> |
else: |
114 |
> |
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e") |
115 |
> |
|
116 |
|
else: |
117 |
|
output.cd() |
118 |
|
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'1', "goff,e") |
124 |
|
hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax) |
125 |
|
hTree.Sumw2() |
126 |
|
if job.type != 'DATA': |
127 |
< |
ScaleFactor = self.getScale(job,subsample) |
127 |
> |
if 'RTight' in treeVar or 'RMed' in treeVar: |
128 |
> |
if TrainFlag: |
129 |
> |
MC_rescale_factor=2. |
130 |
> |
print 'I RESCALE BY 2.0' |
131 |
> |
else: MC_rescale_factor = 1. |
132 |
> |
ScaleFactor = self.getScale(job,subsample,MC_rescale_factor) |
133 |
> |
else: ScaleFactor = self.getScale(job,subsample) |
134 |
|
if ScaleFactor != 0: |
135 |
|
hTree.Scale(ScaleFactor) |
136 |
|
#print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral()) |
137 |
+ |
if addOverFlow: |
138 |
+ |
uFlow = hTree.GetBinContent(0)+hTree.GetBinContent(1) |
139 |
+ |
oFlow = hTree.GetBinContent(hTree.GetNbinsX()+1)+hTree.GetBinContent(hTree.GetNbinsX()) |
140 |
+ |
uFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(0),2)+ROOT.TMath.Power(hTree.GetBinError(1),2)) |
141 |
+ |
oFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()),2)+ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()+1),2)) |
142 |
+ |
hTree.SetBinContent(1,uFlow) |
143 |
+ |
hTree.SetBinContent(hTree.GetNbinsX(),oFlow) |
144 |
+ |
hTree.SetBinError(1,uFlowErr) |
145 |
+ |
hTree.SetBinError(hTree.GetNbinsX(),oFlowErr) |
146 |
|
hTree.SetDirectory(0) |
147 |
|
input.Close() |
148 |
|
hTreeList.append(hTree) |