ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/HistoMaker.py
Revision: 1.1
Committed: Tue Oct 2 11:23:09 2012 UTC (12 years, 7 months ago) by peller
Content type: text/x-python
Branch: MAIN
Log Message:
new histo maker, beta version :)

File Contents

# Content
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 def __init__(self, path, config, 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
20
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
28 if subsample>-1:
29 xsec=float(job.xsec[subsample])
30 sf=float(job.sf[subsample])
31 else:
32 xsec=float(job.xsec)
33 sf=float(job.sf)
34
35
36 theScale = 1.
37 if anaTag == '7TeV':
38 theScale = float(job.lumi)*xsec*sf/(0.46502*CountWithPU.GetBinContent(1)+0.53498*CountWithPU2011B.GetBinContent(1))*self.rescale/float(job.split)
39 elif anaTag == '8TeV':
40 theScale = float(job.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*self.rescale/float(job.split)
41 return theScale
42
43
44 def getHistoFromTree(self,job,subsample=-1):
45
46 hTreeList=[]
47 groupList=[]
48
49 output=TFile.Open(self.path+'/tmp_%s.root'%job.name,'recreate')
50 if job.type != 'DATA':
51
52 if type(self.optionsList[0][7])==str:
53 cutcut=self.config.get('Cuts',self.optionsList[0][7])
54 elif type(self.optionsList[0][7])==list:
55 cutcut=self.config.get('Cuts',self.optionsList[0][7][0])
56 cutcut=cutcut.replace(self.optionsList[0][7][1],self.optionsList[0][7][2])
57 print cutcut
58 if subsample>-1:
59 treeCut='%s & %s & EventForTraining == 0'%(cutcut,job.subcuts[subsample])
60 else:
61 treeCut='%s & EventForTraining == 0'%(cutcut)
62
63 elif job.type == 'DATA':
64 cutcut=self.config.get('Cuts',self.optionsList[0][8])
65 treeCut='%s'%(cutcut)
66 input = TFile.Open(self.path+'/'+job.getpath(),'read')
67 Tree = input.Get(job.tree)
68 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
78 for options in self.optionsList:
79
80 if subsample>-1:
81 name=job.subnames[subsample]
82 group=job.group[subsample]
83 else:
84 name=job.name
85 group=job.group
86
87
88 treeVar=options[0]
89 name=options[1]
90 nBins=int(options[3])
91 xMin=float(options[4])
92 xMax=float(options[5])
93
94 if job.type != 'DATA':
95 if CuttedTree.GetEntries():
96 output.cd()
97 CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), weightF, "goff,e")
98 full=True
99 else:
100 full=False
101 elif job.type == 'DATA':
102
103 if options[11] == 'blind':
104 output.cd()
105 CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e")
106 else:
107 output.cd()
108 CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'1', "goff,e")
109 full = True
110 if full:
111 hTree = ROOT.gDirectory.Get(name)
112 else:
113 output.cd()
114 hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
115 hTree.Sumw2()
116
117 if job.type != 'DATA':
118 ScaleFactor = self.getScale(job,subsample)
119 if ScaleFactor != 0:
120 hTree.Scale(ScaleFactor)
121
122 #print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
123 hTree.SetDirectory(0)
124 input.Close()
125 hTreeList.append(hTree)
126 groupList.append(group)
127
128 return hTreeList, groupList
129
130
131 ######################
132 def orderandadd(histos,typs,setup):
133 #ORDER AND ADD TOGETHER
134 ordnung=[]
135 ordnungtyp=[]
136 num=[0]*len(setup)
137 for i in range(0,len(setup)):
138 for j in range(0,len(histos)):
139 if typs[j] in setup[i]:
140 num[i]+=1
141 ordnung.append(histos[j])
142 ordnungtyp.append(typs[j])
143 del histos
144 del typs
145 histos=ordnung
146 typs=ordnungtyp
147 print typs
148 for k in range(0,len(num)):
149 for m in range(0,num[k]):
150 if m > 0:
151 #add
152 histos[k].Add(histos[k+1],1)
153 printc('magenta','','\t--> added %s to %s'%(typs[k],typs[k+1]))
154 del histos[k+1]
155 del typs[k+1]
156 del histos[len(setup):]
157 del typs[len(setup):]
158 return histos, typs