1 |
< |
from samplesclass import sample |
2 |
< |
from printcolor import printc |
1 |
> |
import sys,os |
2 |
|
import pickle |
3 |
|
import ROOT |
5 |
– |
from ROOT import TFile, TTree |
6 |
– |
import ROOT |
4 |
|
from array import array |
5 |
+ |
from printcolor import printc |
6 |
|
from BetterConfigParser import BetterConfigParser |
7 |
< |
import sys,os |
7 |
> |
from TreeCache import TreeCache |
8 |
|
|
9 |
|
class HistoMaker: |
10 |
< |
def __init__(self, path, config, region, optionsList,rescale=1,which_weightF='weightF'): |
10 |
> |
def __init__(self, samples, path, config, optionsList): |
11 |
|
self.path = path |
12 |
|
self.config = config |
13 |
|
self.optionsList = optionsList |
16 |
– |
self.rescale = rescale |
17 |
– |
self.which_weightF=which_weightF |
18 |
– |
self.region = region |
14 |
|
self.lumi=0. |
15 |
+ |
self.cuts = [] |
16 |
+ |
for options in optionsList: |
17 |
+ |
self.cuts.append(options['cut']) |
18 |
+ |
self.tc = TreeCache(self.cuts,samples,path) |
19 |
|
|
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") |
25 |
– |
CountWithPU2011B = input.Get("CountWithPU2011B") |
26 |
– |
#print lumi*xsecs[i]/hist.GetBinContent(1) |
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) |
33 |
– |
theScale = 1. |
34 |
– |
if anaTag == '7TeV': |
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))*MC_rescale_factor/float(job.split) |
38 |
– |
input.Close() |
39 |
– |
return theScale |
20 |
|
|
21 |
< |
|
22 |
< |
def getHistoFromTree(self,job,subsample=-1): |
23 |
< |
if self.lumi == 0: raise Exception("You're trying to plot with no lumi") |
21 |
> |
def get_histos_from_tree(self,job): |
22 |
> |
if self.lumi == 0: |
23 |
> |
raise Exception("You're trying to plot with no lumi") |
24 |
|
|
25 |
|
hTreeList=[] |
26 |
|
groupList=[] |
33 |
|
plot_path = self.config.get('Directories','plotpath') |
34 |
|
addOverFlow=eval(self.config.get('Plot_general','addOverFlow')) |
35 |
|
|
56 |
– |
scratchDir = os.environ["TMPDIR"] |
57 |
– |
#scratchDir = '/shome/peller/' |
58 |
– |
# define treeCut |
59 |
– |
if job.type != 'DATA': |
60 |
– |
if type(self.region)==str: |
61 |
– |
cutcut=self.config.get('Cuts',self.region) |
62 |
– |
elif type(self.region)==list: |
63 |
– |
#replace vars with other vars in the cutstring (used in DC writer) |
64 |
– |
cutcut=self.config.get('Cuts',self.region[0]) |
65 |
– |
cutcut=cutcut.replace(self.region[1],self.region[2]) |
66 |
– |
#print cutcut |
67 |
– |
if subsample>-1: |
68 |
– |
treeCut='%s & %s'%(cutcut,job.subcuts[subsample]) |
69 |
– |
else: |
70 |
– |
treeCut='%s'%(cutcut) |
71 |
– |
elif job.type == 'DATA': |
72 |
– |
cutcut=self.config.get('Cuts',self.region) |
73 |
– |
treeCut='%s'%(cutcut) |
74 |
– |
|
75 |
– |
# get and skim the Trees |
76 |
– |
output=TFile.Open(scratchDir+'/tmp_plotCache_%s_%s.root'%(self.region,job.identifier),'recreate') |
77 |
– |
input = TFile.Open(self.path+'/'+job.getpath(),'read') |
78 |
– |
Tree = input.Get(job.tree) |
79 |
– |
output.cd() |
80 |
– |
CuttedTree=Tree.CopyTree(treeCut) |
81 |
– |
input.Close() |
82 |
– |
del input |
36 |
|
# get all Histos at once |
84 |
– |
weightF=self.config.get('Weights',self.which_weightF) |
37 |
|
for options in self.optionsList: |
38 |
< |
if subsample>-1: |
39 |
< |
name=job.subnames[subsample] |
40 |
< |
group=job.group[subsample] |
41 |
< |
else: |
42 |
< |
name=job.name |
43 |
< |
group=job.group |
44 |
< |
treeVar=options[0] |
45 |
< |
name=options[1] |
46 |
< |
nBins=int(options[3]) |
47 |
< |
xMin=float(options[4]) |
96 |
< |
xMax=float(options[5]) |
38 |
> |
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 |
|
|
49 |
|
#options |
50 |
|
|
51 |
|
if job.type != 'DATA': |
52 |
|
if CuttedTree.GetEntries(): |
53 |
|
|
54 |
< |
if 'RTight' in treeVar or 'RMed' in treeVar: drawoption = '(%s)*(%s)'%(weightF,BDT_add_cut) |
55 |
< |
else: drawoption = '%s'%(weightF) |
56 |
< |
output.cd() |
54 |
> |
if 'RTight' in treeVar or 'RMed' in treeVar: |
55 |
> |
drawoption = '(%s)*(%s)'%(weightF,BDT_add_cut) |
56 |
> |
else: |
57 |
> |
drawoption = '%s'%(weightF) |
58 |
|
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 |
< |
if options[11] == 'blind': |
63 |
> |
if options['blind'] == 'blind': |
64 |
|
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") |
68 |
|
CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e") |
69 |
|
|
70 |
|
else: |
119 |
– |
output.cd() |
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: |
125 |
– |
output.cd() |
76 |
|
hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax) |
77 |
|
hTree.Sumw2() |
78 |
|
if job.type != 'DATA': |
80 |
|
if TrainFlag: |
81 |
|
MC_rescale_factor=2. |
82 |
|
print 'I RESCALE BY 2.0' |
83 |
< |
else: MC_rescale_factor = 1. |
84 |
< |
ScaleFactor = self.getScale(job,subsample,MC_rescale_factor) |
85 |
< |
else: ScaleFactor = self.getScale(job,subsample) |
83 |
> |
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 |
|
if ScaleFactor != 0: |
89 |
|
hTree.Scale(ScaleFactor) |
90 |
|
#print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral()) |
101 |
|
hTreeList.append(hTree) |
102 |
|
groupList.append(group) |
103 |
|
|
152 |
– |
output.Close() |
153 |
– |
del output |
104 |
|
return hTreeList, groupList |
105 |
|
|
106 |
|
|