ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/myutils/HistoMaker.py
Revision: 1.3
Committed: Mon Feb 4 14:22:21 2013 UTC (12 years, 3 months ago) by nmohr
Content type: text/x-python
Branch: MAIN
Changes since 1.2: +22 -36 lines
Log Message:
New version of workspace writer

File Contents

# User Rev Content
1 nmohr 1.2 import sys,os
2 peller 1.1 import pickle
3     import ROOT
4     from array import array
5 nmohr 1.2 from printcolor import printc
6 peller 1.1 from BetterConfigParser import BetterConfigParser
7 nmohr 1.2 from TreeCache import TreeCache
8 peller 1.1
9     class HistoMaker:
10 nmohr 1.2 def __init__(self, samples, path, config, optionsList):
11 peller 1.1 self.path = path
12     self.config = config
13     self.optionsList = optionsList
14     self.lumi=0.
15 nmohr 1.2 self.cuts = []
16     for options in optionsList:
17     self.cuts.append(options['cut'])
18 nmohr 1.3 #self.tc = TreeCache(self.cuts,samples,path)
19     self.tc = TreeCache(self.cuts,samples,path,config)
20 peller 1.1
21 nmohr 1.2 def get_histos_from_tree(self,job):
22     if self.lumi == 0:
23     raise Exception("You're trying to plot with no lumi")
24 peller 1.1
25     hTreeList=[]
26    
27     #get the conversion rate in case of BDT plots
28     TrainFlag = eval(self.config.get('Analysis','TrainFlag'))
29     BDT_add_cut='EventForTraining == 0'
30    
31    
32     plot_path = self.config.get('Directories','plotpath')
33     addOverFlow=eval(self.config.get('Plot_general','addOverFlow'))
34    
35     # get all Histos at once
36     for options in self.optionsList:
37 nmohr 1.2 name=job.name
38     group=job.group
39     treeVar=options['var']
40     name=options['name']
41     nBins=int(options['nBins'])
42     xMin=float(options['xMin'])
43     xMax=float(options['xMax'])
44     weightF=options['weight']
45     treeCut='%s'%(options['cut'])
46     CuttedTree = self.tc.get_tree(job,treeCut)
47 peller 1.1
48     #options
49    
50     if job.type != 'DATA':
51     if CuttedTree.GetEntries():
52    
53 nmohr 1.2 if 'RTight' in treeVar or 'RMed' in treeVar:
54     drawoption = '(%s)*(%s)'%(weightF,BDT_add_cut)
55     else:
56     drawoption = '%s'%(weightF)
57 peller 1.1 CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), drawoption, "goff,e")
58     full=True
59     else:
60     full=False
61     elif job.type == 'DATA':
62 nmohr 1.3 if options['blind']:
63 peller 1.1 if treeVar == 'H.mass':
64     CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<90. || '+treeVar + '>150.' , "goff,e")
65     else:
66     CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e")
67    
68     else:
69     CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'1', "goff,e")
70     full = True
71     if full:
72     hTree = ROOT.gDirectory.Get(name)
73     else:
74     hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
75     hTree.Sumw2()
76     if job.type != 'DATA':
77     if 'RTight' in treeVar or 'RMed' in treeVar:
78     if TrainFlag:
79     MC_rescale_factor=2.
80     print 'I RESCALE BY 2.0'
81 nmohr 1.2 else:
82     MC_rescale_factor = 1.
83     ScaleFactor = self.tc.get_scale(job,self.config,self.lumi)*MC_rescale_factor
84     else:
85     ScaleFactor = self.tc.get_scale(job,self.config,self.lumi)
86 peller 1.1 if ScaleFactor != 0:
87     hTree.Scale(ScaleFactor)
88     #print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
89     if addOverFlow:
90     uFlow = hTree.GetBinContent(0)+hTree.GetBinContent(1)
91     oFlow = hTree.GetBinContent(hTree.GetNbinsX()+1)+hTree.GetBinContent(hTree.GetNbinsX())
92     uFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(0),2)+ROOT.TMath.Power(hTree.GetBinError(1),2))
93     oFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()),2)+ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()+1),2))
94     hTree.SetBinContent(1,uFlow)
95     hTree.SetBinContent(hTree.GetNbinsX(),oFlow)
96     hTree.SetBinError(1,uFlowErr)
97     hTree.SetBinError(hTree.GetNbinsX(),oFlowErr)
98     hTree.SetDirectory(0)
99 nmohr 1.3 gDict = {}
100     gDict[group] = hTree
101     hTreeList.append(gDict)
102 peller 1.1
103 nmohr 1.3 return hTreeList
104 peller 1.1
105    
106 nmohr 1.3 @staticmethod
107     def orderandadd(histo_dicts,setup):
108     print histo_dicts
109     ordered_histo_dict = {}
110     for sample in setup:
111     nSample = 0
112     for histo_dict in histo_dicts:
113     if histo_dict.has_key(sample):
114     if nSample == 0:
115     ordered_histo_dict[sample] = histo_dict[sample]
116     else:
117     printc('magenta','','\t--> added %s to %s'%(sample,sample))
118     ordered_histo_dict[sample].Add(histo_dict[sample])
119     nSample += 1
120     return ordered_histo_dict