ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/gethistofromtree.py
Revision: 1.5
Committed: Mon Jun 25 11:43:56 2012 UTC (12 years, 10 months ago) by peller
Content type: text/x-python
Branch: MAIN
CVS Tags: ICHEP8TeV
Changes since 1.4: +4 -2 lines
Log Message:
bugfix and removed conflicts

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 #load config
13 config = BetterConfigParser()
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)/(CountWithPU.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 cutcut=config.get('Cuts',options[8])
44 treeCut='%s & EventForTraining == 0'%(cutcut)
45
46
47 input = TFile.Open(job.getpath(),'read')
48
49 Tree = input.Get(job.tree)
50 #Tree=tmpTree.CloneTree()
51 #Tree.SetDirectory(0)
52
53 #Tree=tmpTree.Clone()
54 weightF=config.get('Weights','weightF')
55 #hTree = ROOT.TH1F('%s'%name,'%s'%title,nBins,xMin,xMax)
56 #hTree.SetDirectory(0)
57 #hTree.Sumw2()
58 #print 'drawing...'
59 if job.type != 'DATA':
60 #print treeCut
61 #print job.name
62 if Tree.GetEntries():
63 Tree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'(%s)*(%s)' %(treeCut,weightF), "goff,e")
64 full=True
65 else:
66 full=False
67 elif job.type == 'DATA':
68
69 if len(options)>10:
70 if options[11] == 'blind':
71 treeCut = treeCut + '&'+treeVar+'<0'
72
73
74 Tree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeCut, "goff,e")
75 full = True
76 if full:
77 hTree = ROOT.gDirectory.Get(name)
78 else:
79 hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
80 hTree.Sumw2()
81 #print job.name + ' Sumw2', hTree.GetEntries()
82
83 if job.type != 'DATA':
84 ScaleFactor = getScale(job,rescale)
85 if ScaleFactor != 0:
86 hTree.Scale(ScaleFactor)
87
88 print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
89
90 hTree.SetDirectory(0)
91 input.Close()
92 return hTree, job.group
93
94
95 ######################
96
97
98
99 def orderandadd(histos,typs,setup):
100 #ORDER AND ADD TOGETHER
101
102 ordnung=[]
103 ordnungtyp=[]
104 num=[0]*len(setup)
105 for i in range(0,len(setup)):
106 for j in range(0,len(histos)):
107 if typs[j] in setup[i]:
108 num[i]+=1
109 ordnung.append(histos[j])
110 ordnungtyp.append(typs[j])
111
112 del histos
113 del typs
114
115 histos=ordnung
116 typs=ordnungtyp
117
118 for k in range(0,len(num)):
119 for m in range(0,num[k]):
120 if m > 0:
121
122 #add
123 histos[k].Add(histos[k+1],1)
124 printc('magenta','','\t--> added %s to %s'%(typs[k],typs[k+1]))
125 del histos[k+1]
126 del typs[k+1]
127
128 del histos[len(setup):]
129 del typs[len(setup):]
130
131 return histos, typs
132
133