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.23 by nmohr, Fri Feb 1 10:47:38 2013 UTC

# Line 12 | Line 12 | warnings.filterwarnings( action='ignore'
12   from optparse import OptionParser
13   import pickle
14  
15
15   #CONFIGURE
16   ROOT.gROOT.SetBatch(True)
17   print('hello')
18   #load config
20 #os.mkdir(path+'/sys')
19   argv = sys.argv
20   parser = OptionParser()
21   parser.add_option("-U", "--update", dest="update", default=0,
22                        help="update infofile")
23   parser.add_option("-D", "--discr", dest="discr", default="",
24                        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")
25   parser.add_option("-S", "--samples", dest="names", default="",
26                        help="samples you want to run on")
27   parser.add_option("-C", "--config", dest="config", default=[], action="append",
# Line 38 | Line 32 | if opts.config =="":
32          opts.config = "config"
33  
34   #Import after configure to get help message
35 < from myutils import BetterConfigParser, progbar, printc, mvainfo, ParseInfo
35 > from myutils import BetterConfigParser, progbar, printc, ParseInfo, MvaEvaluator
36  
37   config = BetterConfigParser()
44 #config.read('./config7TeV_ZZ')
38   config.read(opts.config)
39   anaTag = config.get("Analysis","tag")
40  
# Line 84 | Line 77 | longe=40
77   #Workdir
78   workdir=ROOT.gDirectory.GetPath()
79  
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)
80  
81  
82   theMVAs = []
83   for mva in MVAinfos:
84 <    theMVAs.append(MvaEvaluater(config,mva))
84 >    theMVAs.append(MvaEvaluator(config,mva))
85  
86  
87   #eval
# Line 154 | Line 111 | for job in samples:
111              
112      #Set branch adress for all vars
113      for i in range(0,len(theMVAs)):
114 <        theMVAs[i].setBranches(tree,job)
114 >        theMVAs[i].setVariables(tree,job)
115      outfile.cd()
116      #Setup Branches
117 <    MVAbranches=[]
117 >    mvaVals=[]
118      for i in range(0,len(theMVAs)):
119          if job.type == 'Data':
120 <            MVAbranches.append(array('f',[0]))
121 <            newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
120 >            mvaVals.append(array('f',[0]))
121 >            newtree.Branch(MVAinfos[i].MVAname,mvaVals[i],'nominal/F')
122          else:
123 <            MVAbranches.append(array('f',[0]*11))
124 <            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')
123 >            mvaVals.append(array('f',[0]*11))
124 >            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')
125          MVA_formulas_Nominal = []
126          print('\n--> ' + job.name +':')
127      #progbar setup
# Line 183 | Line 140 | for job in samples:
140          tree.GetEntry(entry)
141                              
142          for i in range(0,len(theMVAs)):
143 <            theMVAs[i].evaluate(MVAbranches[i],job)
143 >            theMVAs[i].evaluate(mvaVals[i],job)
144          #Fill:
145          newtree.Fill()
146      newtree.AutoSave()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines