ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/gethistofromtree.py
Revision: 1.13
Committed: Thu Sep 20 14:14:53 2012 UTC (12 years, 7 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.12: +1 -1 lines
Log Message:
bin-by-bin statistical treatment in DC

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