ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/HistoMaker.py
Revision: 1.3
Committed: Tue Oct 2 13:19:45 2012 UTC (12 years, 7 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.2: +5 -3 lines
Log Message:
bugfix

File Contents

# User Rev Content
1 peller 1.1 from samplesclass import sample
2     from printcolor import printc
3     import pickle
4     import ROOT
5     from ROOT import TFile, TTree
6     import ROOT
7     from array import array
8     from BetterConfigParser import BetterConfigParser
9     import sys
10    
11     class HistoMaker:
12 peller 1.2 def __init__(self, path, config, region, optionsList,rescale=1,which_weightF='weightF'):
13 peller 1.1 self.path = path
14     self.config = config
15     self.optionsList = optionsList
16     self.rescale = rescale
17     self.which_weightF=which_weightF
18 peller 1.2 self.region = region
19 peller 1.3 self.lumi=0.
20 peller 1.1
21     def getScale(self,job,subsample=-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 peller 1.3 theScale = float(self.lumi)*xsec*sf/(0.46502*CountWithPU.GetBinContent(1)+0.53498*CountWithPU2011B.GetBinContent(1))*self.rescale/float(job.split)
36 peller 1.1 elif anaTag == '8TeV':
37 peller 1.3 theScale = float(self.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*self.rescale/float(job.split)
38 peller 1.1 return theScale
39    
40    
41     def getHistoFromTree(self,job,subsample=-1):
42 peller 1.3 if self.lumi == 0: raise Exception("You're trying to plot with no lumi")
43    
44 peller 1.1 hTreeList=[]
45     groupList=[]
46    
47 peller 1.2
48     plot_path = self.config.get('Directories','plotpath')
49    
50     # define treeCut
51 peller 1.1 if job.type != 'DATA':
52 peller 1.2 if type(self.region)==str:
53     cutcut=self.config.get('Cuts',self.region)
54     elif type(self.region)==list:
55     #replace vars with other vars in the cutstring (used in DC writer)
56     cutcut=self.config.get('Cuts',self.region[0])
57     cutcut=cutcut.replace(self.region[1],self.region[2])
58     #print cutcut
59 peller 1.1 if subsample>-1:
60     treeCut='%s & %s & EventForTraining == 0'%(cutcut,job.subcuts[subsample])
61     else:
62     treeCut='%s & EventForTraining == 0'%(cutcut)
63     elif job.type == 'DATA':
64 peller 1.2 cutcut=self.config.get('Cuts',self.region)
65 peller 1.1 treeCut='%s'%(cutcut)
66 peller 1.2
67     # get and skim the Trees
68     output=TFile.Open(plot_path+'/tmp_plotCache_%s_%s.root'%(self.region,job.identifier),'recreate')
69 peller 1.1 input = TFile.Open(self.path+'/'+job.getpath(),'read')
70     Tree = input.Get(job.tree)
71 peller 1.2 output.cd()
72     CuttedTree=Tree.CopyTree(treeCut)
73    
74     # get all Histos at once
75 peller 1.1 weightF=self.config.get('Weights',self.which_weightF)
76     for options in self.optionsList:
77     if subsample>-1:
78     name=job.subnames[subsample]
79     group=job.group[subsample]
80     else:
81     name=job.name
82     group=job.group
83     treeVar=options[0]
84     name=options[1]
85     nBins=int(options[3])
86     xMin=float(options[4])
87     xMax=float(options[5])
88    
89     if job.type != 'DATA':
90     if CuttedTree.GetEntries():
91     output.cd()
92     CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), weightF, "goff,e")
93     full=True
94     else:
95     full=False
96     elif job.type == 'DATA':
97     if options[11] == 'blind':
98     output.cd()
99     CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e")
100     else:
101     output.cd()
102     CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'1', "goff,e")
103     full = True
104     if full:
105     hTree = ROOT.gDirectory.Get(name)
106     else:
107     output.cd()
108     hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
109     hTree.Sumw2()
110     if job.type != 'DATA':
111     ScaleFactor = self.getScale(job,subsample)
112     if ScaleFactor != 0:
113     hTree.Scale(ScaleFactor)
114     #print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
115     hTree.SetDirectory(0)
116     input.Close()
117     hTreeList.append(hTree)
118     groupList.append(group)
119    
120     return hTreeList, groupList
121    
122    
123     ######################
124     def orderandadd(histos,typs,setup):
125     #ORDER AND ADD TOGETHER
126     ordnung=[]
127     ordnungtyp=[]
128     num=[0]*len(setup)
129     for i in range(0,len(setup)):
130     for j in range(0,len(histos)):
131     if typs[j] in setup[i]:
132     num[i]+=1
133     ordnung.append(histos[j])
134     ordnungtyp.append(typs[j])
135     del histos
136     del typs
137     histos=ordnung
138     typs=ordnungtyp
139     print typs
140     for k in range(0,len(num)):
141     for m in range(0,num[k]):
142     if m > 0:
143     #add
144     histos[k].Add(histos[k+1],1)
145     printc('magenta','','\t--> added %s to %s'%(typs[k],typs[k+1]))
146     del histos[k+1]
147     del typs[k+1]
148     del histos[len(setup):]
149     del typs[len(setup):]
150     return histos, typs