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

Comparing UserCode/VHbb/python/myutils/HistoMaker.py (file contents):
Revision 1.4 by peller, Tue Feb 5 10:02:03 2013 UTC vs.
Revision 1.17 by nmohr, Fri Apr 5 15:59:49 2013 UTC

# Line 9 | Line 9 | from math import sqrt
9   from copy import copy
10  
11   class HistoMaker:
12 <    def __init__(self, samples, path, config, optionsList):
12 >    def __init__(self, samples, path, config, optionsList,GroupDict=None):
13          self.path = path
14          self.config = config
15          self.optionsList = optionsList
# Line 18 | Line 18 | class HistoMaker:
18          self.cuts = []
19          for options in optionsList:
20              self.cuts.append(options['cut'])
21 +        #print self.cuts
22          #self.tc = TreeCache(self.cuts,samples,path)
23          self.tc = TreeCache(self.cuts,samples,path,config)
24          self._rebin = False
25          self.mybinning = None
26 +        self.GroupDict=GroupDict
27 +        self.calc_rebin_flag = False
28 +        VHbbNameSpace=config.get('VHbbNameSpace','library')
29 +        ROOT.gSystem.Load(VHbbNameSpace)
30  
31      def get_histos_from_tree(self,job):
32          if self.lumi == 0:
# Line 38 | Line 43 | class HistoMaker:
43          addOverFlow=eval(self.config.get('Plot_general','addOverFlow'))
44  
45          # get all Histos at once
46 +        CuttedTree = self.tc.get_tree(job,'1')
47          for options in self.optionsList:
48              name=job.name
49 <            group=job.group
49 >            if self.GroupDict is None:
50 >                group=job.group
51 >            else:
52 >                group=self.GroupDict[job.name]
53              treeVar=options['var']
54              name=options['name']
55 <            nBins=self.nBins
56 <            #int(options['nBins'])
55 >            if self._rebin or self.calc_rebin_flag:
56 >                nBins = self.nBins
57 >            else:
58 >                nBins = int(options['nBins'])
59              xMin=float(options['xMin'])
60              xMax=float(options['xMax'])
61              weightF=options['weight']
62              treeCut='%s'%(options['cut'])
52            CuttedTree = self.tc.get_tree(job,treeCut)
63  
64              #options
65  
66              if job.type != 'DATA':
67                  if CuttedTree.GetEntries():
58                    
68                      if 'RTight' in treeVar or 'RMed' in treeVar:
69 <                        drawoption = '(%s)*(%s)'%(weightF,BDT_add_cut)
69 >                        drawoption = '(%s)*(%s & %s)'%(weightF,treeCut,BDT_add_cut)
70                      else:
71 <                        drawoption = '%s'%(weightF)
71 >                        drawoption = '(%s)*(%s)'%(weightF,treeCut)
72                      CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax), drawoption, "goff,e")
73                      full=True
74                  else:
# Line 67 | Line 76 | class HistoMaker:
76              elif job.type == 'DATA':
77                  if options['blind']:
78                      if treeVar == 'H.mass':
79 <                        CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<90. || '+treeVar + '>150.' , "goff,e")
79 >                        CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),' (%(var)s <90. || %(var)s > 150.) & %(cut)s' %options, "goff,e")
80                      else:
81 <                        CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeVar+'<0', "goff,e")
81 >                        CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'%(var)s < 0. & %(cut)s'%options, "goff,e")
82  
83                  else:
84 <                    CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'1', "goff,e")
84 >                    CuttedTree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'%s' %treeCut, "goff,e")
85                  full = True
86              if full:
87                  hTree = ROOT.gDirectory.Get(name)
# Line 83 | Line 92 | class HistoMaker:
92                  if 'RTight' in treeVar or 'RMed' in treeVar:
93                      if TrainFlag:
94                          MC_rescale_factor=2.
95 <                        print 'I RESCALE BY 2.0'
95 >                        #print 'I RESCALE BY 2.0'
96                      else:
97                          MC_rescale_factor = 1.
98                      ScaleFactor = self.tc.get_scale(job,self.config,self.lumi)*MC_rescale_factor
# Line 105 | Line 114 | class HistoMaker:
114              gDict = {}
115              if self._rebin:
116                  gDict[group] = self.mybinning.rebin(hTree)
117 +                del hTree
118              else:
119 +                #print 'not rebinning %s'%job.name
120                  gDict[group] = hTree
121              hTreeList.append(gDict)
122 +        CuttedTree.IsA().Destructor(CuttedTree)
123 +        del CuttedTree
124          return hTreeList
125        
126      @property
# Line 132 | Line 145 | class HistoMaker:
145              return False
146  
147      def calc_rebin(self, bg_list, nBins_start=1000, tolerance=0.35):
148 <        self.norebin_nBins = self.nBins
148 >        self.calc_rebin_flag = True
149 >        self.norebin_nBins = copy(self.nBins)
150          self.rebin_nBins = nBins_start
151          self.nBins = nBins_start
152          i=0
153          #add all together:
154 +        print '\n\t...calculating rebinning...'
155          for job in bg_list:
156 +            htree = self.get_histos_from_tree(job)[0].values()[0]
157              if not i:
158 <                totalBG = self.get_histos_from_tree(job)[0].values()[0]
158 >                totalBG = copy(htree)
159              else:
160 <                totalBG.Add(self.get_histos_from_tree(job)[0].values()[0],1)
160 >                totalBG.Add(htree,1)
161 >            del htree
162              i+=1
163          ErrorR=0
164          ErrorL=0
# Line 177 | Line 194 | class HistoMaker:
194          stepsize=int(inbetween)/(int(self.norebin_nBins)-2)
195          modulo = int(inbetween)%(int(self.norebin_nBins)-2)
196  
197 <        #print'stepsize %s'% stepsize
197 >        #print 'stepsize %s'% stepsize
198          #print 'modulo %s'%modulo
199          binlist=[binL]
200          for i in range(0,int(self.norebin_nBins)-3):
# Line 185 | Line 202 | class HistoMaker:
202          binlist[-1]+=modulo
203          binlist.append(binR)
204          binlist.append(self.rebin_nBins+1)
205 <
205 >        #print 'binning set to %s'%binlist
206          self.mybinning = Rebinner(int(self.norebin_nBins),array('d',[-1.0]+[totalBG.GetBinLowEdge(i) for i in binlist]),True)
207          self._rebin = True
208 <
208 >        print '\t > rebinning is set <\n'
209  
210      @staticmethod
211      def orderandadd(histo_dicts,setup):
195        print histo_dicts
212          ordered_histo_dict = {}
213          for sample in setup:
214              nSample = 0
215              for histo_dict in histo_dicts:
216                  if histo_dict.has_key(sample):
217                      if nSample == 0:
218 <                        ordered_histo_dict[sample] = histo_dict[sample]
218 >                        ordered_histo_dict[sample] = histo_dict[sample].Clone()
219                      else:
220                          printc('magenta','','\t--> added %s to %s'%(sample,sample))
221                          ordered_histo_dict[sample].Add(histo_dict[sample])
222                      nSample += 1
223 +        del histo_dicts
224          return ordered_histo_dict
225  
226   class Rebinner:
# Line 213 | Line 230 | class Rebinner:
230          self.active=active
231      def rebin(self, histo):
232          if not self.active: return histo
216        #print 'rebinning'
233          #print histo.Integral()
234          ROOT.gDirectory.Delete('hnew')
235          histo.Rebin(self.nBins,'hnew',self.lowedgearray)
# Line 227 | Line 243 | class Rebinner:
243          newhisto.SetName(binhisto.GetName())
244          newhisto.SetTitle(binhisto.GetTitle())
245          #print newhisto.Integral()
246 +        del histo
247 +        del binhisto
248          return copy(newhisto)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines