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.28 by bortigno, Tue Feb 26 13:10:41 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 154 | Line 113 | for job in samples:
113              
114      #Set branch adress for all vars
115      for i in range(0,len(theMVAs)):
116 <        theMVAs[i].setBranches(tree,job)
116 >        theMVAs[i].setVariables(tree,job)
117      outfile.cd()
118      #Setup Branches
119 <    MVAbranches=[]
119 >    mvaVals=[]
120      for i in range(0,len(theMVAs)):
121          if job.type == 'Data':
122 <            MVAbranches.append(array('f',[0]))
123 <            newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
122 >            mvaVals.append(array('f',[0]))
123 >            newtree.Branch(MVAinfos[i].MVAname,mvaVals[i],'nominal/F')
124          else:
125 <            MVAbranches.append(array('f',[0]*11))
126 <            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')
125 >            mvaVals.append(array('f',[0]*11))
126 >            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')
127          MVA_formulas_Nominal = []
128          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)
129      #Fill event by event:
130      for entry in range(0,nEntries):
180        if entry % step == 0:
181            bar.move()
182        #load entry
131          tree.GetEntry(entry)
132                              
133          for i in range(0,len(theMVAs)):
134 <            theMVAs[i].evaluate(MVAbranches[i],job)
134 >            theMVAs[i].evaluate(mvaVals[i],job)
135          #Fill:
136          newtree.Fill()
137      newtree.AutoSave()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines