ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/gethistofromtree.py
Revision: 1.15
Committed: Thu Oct 4 13:02:23 2012 UTC (12 years, 7 months ago) by nmohr
Content type: text/x-python
Branch: MAIN
Changes since 1.14: +13 -2 lines
Log Message:
Plotting style

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
12 def getScale(job,path,config,rescale,subsample=-1):
13 anaTag=config.get('Analysis','tag')
14 input = TFile.Open(path+'/'+job.getpath())
15 CountWithPU = input.Get("CountWithPU")
16 CountWithPU2011B = input.Get("CountWithPU2011B")
17 #print lumi*xsecs[i]/hist.GetBinContent(1)
18
19 if subsample>-1:
20 xsec=float(job.xsec[subsample])
21 sf=float(job.sf[subsample])
22 else:
23 xsec=float(job.xsec)
24 sf=float(job.sf)
25
26
27 theScale = 1.
28 if anaTag == '7TeV':
29 theScale = float(job.lumi)*xsec*sf/(0.46502*CountWithPU.GetBinContent(1)+0.53498*CountWithPU2011B.GetBinContent(1))*rescale/float(job.split)
30 elif anaTag == '8TeV':
31 theScale = float(job.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*rescale/float(job.split)
32 return theScale
33
34 def getHistoFromTree(job,path,config,options,rescale=1,subsample=-1,which_weightF='weightF'):
35
36 #print job.getpath()
37 #print options
38 treeVar=options[0]
39 if subsample>-1:
40 name=job.subnames[subsample]
41 group=job.group[subsample]
42 else:
43 name=job.name
44 group=job.group
45
46 #title=job.plotname()
47 nBins=int(options[3])
48 xMin=float(options[4])
49 xMax=float(options[5])
50 addOverFlow=eval(config.get('Plot_general','addOverFlow'))
51
52 if job.type != 'DATA':
53
54 if type(options[7])==str:
55 cutcut=config.get('Cuts',options[7])
56 elif type(options[7])==list:
57 cutcut=config.get('Cuts',options[7][0])
58 cutcut=cutcut.replace(options[7][1],options[7][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)
64
65 elif job.type == 'DATA':
66 cutcut=config.get('Cuts',options[8])
67 treeCut='%s'%(cutcut)
68
69
70 input = TFile.Open(path+'/'+job.getpath(),'read')
71
72 Tree = input.Get(job.tree)
73 #Tree=tmpTree.CloneTree()
74 #Tree.SetDirectory(0)
75
76 #Tree=tmpTree.Clone()
77 weightF=config.get('Weights',which_weightF)
78 #hTree = ROOT.TH1F('%s'%name,'%s'%title,nBins,xMin,xMax)
79 #hTree.SetDirectory(0)
80 #hTree.Sumw2()
81 #print 'drawing...'
82 if job.type != 'DATA':
83 #print treeCut
84 #print job.name
85 if Tree.GetEntries():
86 Tree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'(%s)*(%s)' %(treeCut,weightF), "goff,e")
87 full=True
88 else:
89 full=False
90 elif job.type == 'DATA':
91
92 if len(options)>10:
93 if options[11] == 'blind':
94 treeCut = treeCut + '&'+treeVar+'<0'
95
96
97 Tree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeCut, "goff,e")
98 full = True
99 if full:
100 hTree = ROOT.gDirectory.Get(name)
101 else:
102 hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
103 hTree.Sumw2()
104 #print job.name + ' Sumw2', hTree.GetEntries()
105
106 if job.type != 'DATA':
107 ScaleFactor = getScale(job,path,config,rescale,subsample)
108 if ScaleFactor != 0:
109 hTree.Scale(ScaleFactor)
110
111 if addOverFlow:
112 print 'Adding overflow'
113 uFlow = hTree.GetBinContent(0)+hTree.GetBinContent(1)
114 oFlow = hTree.GetBinContent(hTree.GetNbinsX()+1)+hTree.GetBinContent(hTree.GetNbinsX())
115 uFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(0),2)+ROOT.TMath.Power(hTree.GetBinError(1),2))
116 oFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()),2)+ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()+1),2))
117 hTree.SetBinContent(1,uFlow)
118 hTree.SetBinContent(hTree.GetNbinsX(),oFlow)
119 hTree.SetBinError(1,uFlowErr)
120 hTree.SetBinError(hTree.GetNbinsX(),oFlowErr)
121
122
123 print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
124
125 hTree.SetDirectory(0)
126 input.Close()
127
128 return hTree, group
129
130
131 ######################
132
133
134
135 def orderandadd(histos,typs,setup):
136 #ORDER AND ADD TOGETHER
137
138 ordnung=[]
139 ordnungtyp=[]
140 num=[0]*len(setup)
141 for i in range(0,len(setup)):
142 for j in range(0,len(histos)):
143 if typs[j] in setup[i]:
144 num[i]+=1
145 ordnung.append(histos[j])
146 ordnungtyp.append(typs[j])
147
148 del histos
149 del typs
150
151 histos=ordnung
152 typs=ordnungtyp
153
154 print typs
155
156 for k in range(0,len(num)):
157 for m in range(0,num[k]):
158 if m > 0:
159
160 #add
161 histos[k].Add(histos[k+1],1)
162 printc('magenta','','\t--> added %s to %s'%(typs[k],typs[k+1]))
163 del histos[k+1]
164 del typs[k+1]
165
166 del histos[len(setup):]
167 del typs[len(setup):]
168
169 return histos, typs
170
171