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

Comparing UserCode/VHbb/python/evaluateMVA.py (file contents):
Revision 1.22 by nmohr, Fri Jan 25 16:18:00 2013 UTC vs.
Revision 1.27 by bortigno, Sun Feb 24 01:51:10 2013 UTC

# Line 17 | Line 17 | import pickle
17   ROOT.gROOT.SetBatch(True)
18   print('hello')
19   #load config
20 #os.mkdir(path+'/sys')
20   argv = sys.argv
21   parser = OptionParser()
22   parser.add_option("-U", "--update", dest="update", default=0,
23                        help="update infofile")
24   parser.add_option("-D", "--discr", dest="discr", default="",
25                        help="discriminators to be added")
27 #parser.add_option("-I", "--inpath", dest="inpath", default="",
28 #                      help="path to samples")
29 #parser.add_option("-O", "--outpath", dest="outpath", default="",
30 #                      help="path where to store output samples")
26   parser.add_option("-S", "--samples", dest="names", default="",
27                        help="samples you want to run on")
28   parser.add_option("-C", "--config", dest="config", default=[], action="append",
# Line 38 | Line 33 | if opts.config =="":
33          opts.config = "config"
34  
35   #Import after configure to get help message
36 < from myutils import BetterConfigParser, progbar, printc, mvainfo, ParseInfo
36 > from myutils import BetterConfigParser, progbar, printc, ParseInfo, MvaEvaluator
37  
38   config = BetterConfigParser()
44 #config.read('./config7TeV_ZZ')
39   config.read(opts.config)
40   anaTag = config.get("Analysis","tag")
41  
# Line 84 | Line 78 | longe=40
78   #Workdir
79   workdir=ROOT.gDirectory.GetPath()
80  
87 class MvaEvaluater:
88    def __init__(self, config, MVAinfo):
89        self.varset = MVAinfo.varset
90        #Define reader
91        self.reader = ROOT.TMVA.Reader("!Color:!Silent")
92        MVAdir=config.get('Directories','vhbbpath')
93        self.systematics=config.get('systematics','systematics').split(' ')
94        self.MVA_Vars={}
95        self.MVAname = MVAinfo.MVAname
96        for systematic in self.systematics:
97            self.MVA_Vars[systematic]=config.get(self.varset,systematic)
98            self.MVA_Vars[systematic]=self.MVA_Vars[systematic].split(' ')
99        #define variables and specatators
100        self.MVA_var_buffer = []
101        for i in range(len( self.MVA_Vars['Nominal'])):
102            self.MVA_var_buffer.append(array( 'f', [ 0 ] ))
103            self.reader.AddVariable( self.MVA_Vars['Nominal'][i],self.MVA_var_buffer[i])
104        self.reader.BookMVA(MVAinfo.MVAname,MVAdir+'/data/'+MVAinfo.getweightfile())
105        #--> Now the MVA is booked
106
107    def setBranches(self,tree,job):
108        #Set formulas for all vars
109        self.MVA_formulas={}
110        for systematic in self.systematics:
111            if job.type == 'DATA' and not systematic == 'Nominal': continue
112            self.MVA_formulas[systematic]=[]
113            for j in range(len( self.MVA_Vars['Nominal'])):
114                self.MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),self.MVA_Vars[systematic][j],tree))
115
116    def evaluate(self,MVAbranches,job):
117        #Evaluate all vars and fill the branches
118        for systematic in self.systematics:
119            for j in range(len( self.MVA_Vars['Nominal'])):
120                if job.type == 'DATA' and not systematic == 'Nominal': continue
121                self.MVA_var_buffer[j][0] = self.MVA_formulas[systematic][j].EvalInstance()                
122            MVAbranches[self.systematics.index(systematic)] = self.reader.EvaluateMVA(self.MVAname)
81  
82  
83   theMVAs = []
84   for mva in MVAinfos:
85 <    theMVAs.append(MvaEvaluater(config,mva))
85 >    theMVAs.append(MvaEvaluator(config,mva))
86  
87  
88   #eval
89  
90   samples = info.get_samples(namelist)
91 + print(samples)
92   for job in samples:
93      #get trees:
94      print(INpath+'/'+job.prefix+job.identifier+'.root')
# Line 148 | Line 107 | for job in samples:
107          #print key.GetName()
108          obj.Write(key.GetName())
109      tree = input.Get(job.tree)
151    nEntries = tree.GetEntries()
110      outfile.cd()
111 <    newtree = tree.CloneTree(0)
111 >    newtree = tree.CopyTree('V.pt > 100') #hard skim to get faster
112 >    nEntries = newtree.GetEntries()
113 >    input.Close()
114              
115      #Set branch adress for all vars
116      for i in range(0,len(theMVAs)):
117 <        theMVAs[i].setBranches(tree,job)
117 >        theMVAs[i].setVariables(newtree,job)
118      outfile.cd()
119      #Setup Branches
120 <    MVAbranches=[]
120 >    mvaVals=[]
121      for i in range(0,len(theMVAs)):
122          if job.type == 'Data':
123 <            MVAbranches.append(array('f',[0]))
124 <            newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
123 >            mvaVals.append(array('f',[0]))
124 >            newtree.Branch(MVAinfos[i].MVAname,mvaVals[i],'nominal/F')
125          else:
126 <            MVAbranches.append(array('f',[0]*11))
127 <            newtree.Branch(theMVAs[i].MVAname,MVAbranches[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down:beff1_up:beff1_down/F')
126 >            mvaVals.append(array('f',[0]*11))
127 >            newtree.Branch(theMVAs[i].MVAname,mvaVals[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down:beff1_up:beff1_down/F')
128          MVA_formulas_Nominal = []
129          print('\n--> ' + job.name +':')
170    #progbar setup
171    if nEntries >= longe:
172        step=long(nEntries/longe)
173        long=longe
174    else:
175        long=nEntries
176        step = 1
177    bar=progbar(long)
130      #Fill event by event:
131      for entry in range(0,nEntries):
132 <        if entry % step == 0:
181 <            bar.move()
182 <        #load entry
183 <        tree.GetEntry(entry)
132 >        newtree.GetEntry(entry)
133                              
134          for i in range(0,len(theMVAs)):
135 <            theMVAs[i].evaluate(MVAbranches[i],job)
135 >            theMVAs[i].evaluate(mvaVals[i],job)
136          #Fill:
137          newtree.Fill()
138      newtree.AutoSave()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines