ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/HistoMaker.py
Revision: 1.2
Committed: Tue Oct 2 13:00:32 2012 UTC (12 years, 7 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.1: +21 -31 lines
Log Message:
plotting

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 BetterConfigParser import BetterConfigParser
9     import sys
10    
11     class HistoMaker:
12 peller 1.2 def __init__(self, path, config, region, optionsList,rescale=1,which_weightF='weightF'):
13 peller 1.1 self.path = path
14     self.config = config
15     self.optionsList = optionsList
16     self.rescale = rescale
17     self.which_weightF=which_weightF
18 peller 1.2 self.region = region
19 peller 1.1
20     def getScale(self,job,subsample=-1):
21     anaTag=self.config.get('Analysis','tag')
22     input = TFile.Open(self.path+'/'+job.getpath())
23     CountWithPU = input.Get("CountWithPU")
24     CountWithPU2011B = input.Get("CountWithPU2011B")
25     #print lumi*xsecs[i]/hist.GetBinContent(1)
26     if subsample>-1:
27     xsec=float(job.xsec[subsample])
28     sf=float(job.sf[subsample])
29     else:
30     xsec=float(job.xsec)
31     sf=float(job.sf)
32     theScale = 1.
33     if anaTag == '7TeV':
34     theScale = float(job.lumi)*xsec*sf/(0.46502*CountWithPU.GetBinContent(1)+0.53498*CountWithPU2011B.GetBinContent(1))*self.rescale/float(job.split)
35     elif anaTag == '8TeV':
36     theScale = float(job.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*self.rescale/float(job.split)
37     return theScale
38    
39    
40     def getHistoFromTree(self,job,subsample=-1):
41    
42     hTreeList=[]
43     groupList=[]
44    
45 peller 1.2
46     plot_path = self.config.get('Directories','plotpath')
47    
48     # define treeCut
49 peller 1.1 if job.type != 'DATA':
50 peller 1.2 if type(self.region)==str:
51     cutcut=self.config.get('Cuts',self.region)
52     elif type(self.region)==list:
53     #replace vars with other vars in the cutstring (used in DC writer)
54     cutcut=self.config.get('Cuts',self.region[0])
55     cutcut=cutcut.replace(self.region[1],self.region[2])
56     #print cutcut
57 peller 1.1 if subsample>-1:
58     treeCut='%s & %s & EventForTraining == 0'%(cutcut,job.subcuts[subsample])
59     else:
60     treeCut='%s & EventForTraining == 0'%(cutcut)
61     elif job.type == 'DATA':
62 peller 1.2 cutcut=self.config.get('Cuts',self.region)
63 peller 1.1 treeCut='%s'%(cutcut)
64 peller 1.2
65     # get and skim the Trees
66     output=TFile.Open(plot_path+'/tmp_plotCache_%s_%s.root'%(self.region,job.identifier),'recreate')
67 peller 1.1 input = TFile.Open(self.path+'/'+job.getpath(),'read')
68     Tree = input.Get(job.tree)
69 peller 1.2 output.cd()
70     CuttedTree=Tree.CopyTree(treeCut)
71    
72     # get all Histos at once
73 peller 1.1 weightF=self.config.get('Weights',self.which_weightF)
74     for options in self.optionsList:
75     if subsample>-1:
76     name=job.subnames[subsample]
77     group=job.group[subsample]
78     else:
79     name=job.name
80     group=job.group
81     treeVar=options[0]
82     name=options[1]
83     nBins=int(options[3])
84     xMin=float(options[4])
85     xMax=float(options[5])
86    
87     if job.type != 'DATA':
88     if CuttedTree.GetEntries():
89     output.cd()
90     CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), weightF, "goff,e")
91     full=True
92     else:
93     full=False
94     elif job.type == 'DATA':
95     if options[11] == 'blind':
96     output.cd()
97     CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e")
98     else:
99     output.cd()
100     CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'1', "goff,e")
101     full = True
102     if full:
103     hTree = ROOT.gDirectory.Get(name)
104     else:
105     output.cd()
106     hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
107     hTree.Sumw2()
108     if job.type != 'DATA':
109     ScaleFactor = self.getScale(job,subsample)
110     if ScaleFactor != 0:
111     hTree.Scale(ScaleFactor)
112     #print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
113     hTree.SetDirectory(0)
114     input.Close()
115     hTreeList.append(hTree)
116     groupList.append(group)
117    
118     return hTreeList, groupList
119    
120    
121     ######################
122     def orderandadd(histos,typs,setup):
123     #ORDER AND ADD TOGETHER
124     ordnung=[]
125     ordnungtyp=[]
126     num=[0]*len(setup)
127     for i in range(0,len(setup)):
128     for j in range(0,len(histos)):
129     if typs[j] in setup[i]:
130     num[i]+=1
131     ordnung.append(histos[j])
132     ordnungtyp.append(typs[j])
133     del histos
134     del typs
135     histos=ordnung
136     typs=ordnungtyp
137     print typs
138     for k in range(0,len(num)):
139     for m in range(0,num[k]):
140     if m > 0:
141     #add
142     histos[k].Add(histos[k+1],1)
143     printc('magenta','','\t--> added %s to %s'%(typs[k],typs[k+1]))
144     del histos[k+1]
145     del typs[k+1]
146     del histos[len(setup):]
147     del typs[len(setup):]
148     return histos, typs