ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/HistoMaker.py
(Generate patch)

Comparing UserCode/VHbb/python/HistoMaker.py (file contents):
Revision 1.1 by peller, Tue Oct 2 11:23:09 2012 UTC vs.
Revision 1.6 by peller, Thu Oct 11 08:39:53 2012 UTC

# Line 9 | Line 9 | from BetterConfigParser import BetterCon
9   import sys
10  
11   class HistoMaker:
12 <    def __init__(self, path, config, optionsList,rescale=1,which_weightF='weightF'):
12 >    def __init__(self, path, config, region, optionsList,rescale=1,which_weightF='weightF'):
13          self.path = path
14          self.config = config
15          self.optionsList = optionsList
16          self.rescale = rescale
17          self.which_weightF=which_weightF
18 <
19 <
18 >        self.region = region
19 >        self.lumi=0.
20  
21      def getScale(self,job,subsample=-1):
22          anaTag=self.config.get('Analysis','tag')
# Line 24 | Line 24 | class HistoMaker:
24          CountWithPU = input.Get("CountWithPU")
25          CountWithPU2011B = input.Get("CountWithPU2011B")
26          #print lumi*xsecs[i]/hist.GetBinContent(1)
27        
27          if subsample>-1:
28              xsec=float(job.xsec[subsample])
29              sf=float(job.sf[subsample])
30          else:
31              xsec=float(job.xsec)
32              sf=float(job.sf)
34        
35        
33          theScale = 1.
34          if anaTag == '7TeV':
35 <            theScale = float(job.lumi)*xsec*sf/(0.46502*CountWithPU.GetBinContent(1)+0.53498*CountWithPU2011B.GetBinContent(1))*self.rescale/float(job.split)
35 >            theScale = float(self.lumi)*xsec*sf/(0.46502*CountWithPU.GetBinContent(1)+0.53498*CountWithPU2011B.GetBinContent(1))*self.rescale/float(job.split)
36          elif anaTag == '8TeV':
37 <            theScale = float(job.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*self.rescale/float(job.split)
37 >            theScale = float(self.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*self.rescale/float(job.split)
38          return theScale
39  
40  
41      def getHistoFromTree(self,job,subsample=-1):
42 <        
42 >        if self.lumi == 0: raise Exception("You're trying to plot with no lumi")
43 >        
44          hTreeList=[]
45          groupList=[]
46  
47 <        output=TFile.Open(self.path+'/tmp_%s.root'%job.name,'recreate')
47 >        #get the conversion rate in case of BDT plots
48 >        TrainFlag = eval(self.config.get('Analysis','TrainFlag'))
49 >        if TrainFlag:
50 >            MC_rescale_factor=2.
51 >            print 'I RESCALE BY 2.0'
52 >        else: MC_rescale_factor = 1.
53 >
54 >        BDT_add_cut='EventForTraining == 0'
55 >
56 >
57 >        plot_path = self.config.get('Directories','plotpath')
58 >        addOverFlow=eval(self.config.get('Plot_general','addOverFlow'))
59 >
60 >        # define treeCut
61          if job.type != 'DATA':
62 <        
63 <            if type(self.optionsList[0][7])==str:
64 <                cutcut=self.config.get('Cuts',self.optionsList[0][7])
65 <            elif type(self.optionsList[0][7])==list:
66 <                cutcut=self.config.get('Cuts',self.optionsList[0][7][0])
67 <                cutcut=cutcut.replace(self.optionsList[0][7][1],self.optionsList[0][7][2])
68 <                print cutcut
62 >            if type(self.region)==str:
63 >                cutcut=self.config.get('Cuts',self.region)
64 >            elif type(self.region)==list:
65 >                #replace vars with other vars in the cutstring (used in DC writer)
66 >                cutcut=self.config.get('Cuts',self.region[0])
67 >                cutcut=cutcut.replace(self.region[1],self.region[2])
68 >                #print cutcut
69              if subsample>-1:
70 <                treeCut='%s & %s & EventForTraining == 0'%(cutcut,job.subcuts[subsample])        
70 >                treeCut='%s & %s'%(cutcut,job.subcuts[subsample])        
71              else:
72 <                treeCut='%s & EventForTraining == 0'%(cutcut)
62 <
72 >                treeCut='%s'%(cutcut)
73          elif job.type == 'DATA':
74 <            cutcut=self.config.get('Cuts',self.optionsList[0][8])
74 >            cutcut=self.config.get('Cuts',self.region)
75              treeCut='%s'%(cutcut)
76 +
77 +        # get and skim the Trees
78 +        output=TFile.Open(plot_path+'/tmp_plotCache_%s_%s.root'%(self.region,job.identifier),'recreate')
79          input = TFile.Open(self.path+'/'+job.getpath(),'read')
80          Tree = input.Get(job.tree)
81 +        output.cd()
82 +        CuttedTree=Tree.CopyTree(treeCut)
83 +    
84 +        # get all Histos at once
85          weightF=self.config.get('Weights',self.which_weightF)
69        if job.type != 'DATA':
70            #if Tree.GetEntries():
71            output.cd()
72            CuttedTree=Tree.CopyTree(treeCut)
73        elif job.type == 'DATA':
74        
75            output.cd()
76            CuttedTree=Tree.CopyTree(treeCut)
77
86          for options in self.optionsList:
79
87              if subsample>-1:
88                  name=job.subnames[subsample]
89                  group=job.group[subsample]
90              else:
91                  name=job.name
92                  group=job.group
86
87
93              treeVar=options[0]
94              name=options[1]
95              nBins=int(options[3])
96              xMin=float(options[4])
97              xMax=float(options[5])
98  
99 +            #options
100 +
101              if job.type != 'DATA':
102                  if CuttedTree.GetEntries():
103 +                    
104 +                    if 'BDT' in treeVar: drawoption = '(%s)*(%s)'%(weightF,BDT_add_cut)
105 +                    else: drawoption = '%s'%(weightF)
106                      output.cd()
107 <                    CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), weightF, "goff,e")
107 >                    CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), drawoption, "goff,e")
108                      full=True
109                  else:
110                      full=False
111              elif job.type == 'DATA':
102            
112                  if options[11] == 'blind':
113                      output.cd()
114 <                    CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e")
114 >                    if treeVar == 'H.mass':
115 >                        CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<80. || '+treeVar + '>150.' , "goff,e")
116 >                    else:
117 >                        CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e")
118 >
119                  else:
120                      output.cd()
121                      CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'1', "goff,e")
# Line 113 | Line 126 | class HistoMaker:
126                  output.cd()
127                  hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
128                  hTree.Sumw2()
116
129              if job.type != 'DATA':
130 <                ScaleFactor = self.getScale(job,subsample)
130 >                if 'BDT' in treeVar: ScaleFactor = self.getScale(job,subsample,MC_rescale_factor)
131 >                else: ScaleFactor = self.getScale(job,subsample)
132                  if ScaleFactor != 0:
133                      hTree.Scale(ScaleFactor)
121                    
134              #print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
135 +            if addOverFlow:
136 +                uFlow = hTree.GetBinContent(0)+hTree.GetBinContent(1)
137 +                oFlow = hTree.GetBinContent(hTree.GetNbinsX()+1)+hTree.GetBinContent(hTree.GetNbinsX())
138 +                uFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(0),2)+ROOT.TMath.Power(hTree.GetBinError(1),2))
139 +                oFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()),2)+ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()+1),2))
140 +                hTree.SetBinContent(1,uFlow)
141 +                hTree.SetBinContent(hTree.GetNbinsX(),oFlow)
142 +                hTree.SetBinError(1,uFlowErr)
143 +                hTree.SetBinError(hTree.GetNbinsX(),oFlowErr)
144              hTree.SetDirectory(0)
145              input.Close()
146              hTreeList.append(hTree)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines