ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/gethistofromtree.py
Revision: 1.6
Committed: Fri Jul 13 10:36:52 2012 UTC (12 years, 10 months ago) by nmohr
Content type: text/x-python
Branch: MAIN
Changes since 1.5: +7 -3 lines
Log Message:
7 and 8 TeV

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