ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/gethistofromtree.py
Revision: 1.8
Committed: Fri Aug 10 09:36:16 2012 UTC (12 years, 9 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.7: +6 -3 lines
Log Message:
update

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