ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/gethistofromtree.py
Revision: 1.1
Committed: Fri May 25 13:07:06 2012 UTC (12 years, 11 months ago) by peller
Content type: text/x-python
Branch: MAIN
Log Message:
added gethistofromtree

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