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.19 by bortigno, Fri Jan 11 13:47:24 2013 UTC vs.
Revision 1.28 by bortigno, Tue Feb 26 13:10:41 2013 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2 + from __future__ import print_function
3   import sys
4   import os
5   import ROOT
# Line 9 | Line 10 | from copy import copy
10   import warnings
11   warnings.filterwarnings( action='ignore', category=RuntimeWarning, message='creating converter.*' )
12   from optparse import OptionParser
12 from BetterConfigParser import BetterConfigParser
13   import pickle
14  
15 +
16   #CONFIGURE
17   ROOT.gROOT.SetBatch(True)
18 < print 'hello'
18 > print('hello')
19   #load config
19 #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")
26 #parser.add_option("-I", "--inpath", dest="inpath", default="",
27 #                      help="path to samples")
28 #parser.add_option("-O", "--outpath", dest="outpath", default="",
29 #                      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",
29                        help="configuration file")
30   (opts, args) = parser.parse_args(argv)
31  
36 from samplesclass import sample
37 from mvainfos import mvainfo
38 from progbar import progbar
39 from printcolor import printc
40
32   if opts.config =="":
33          opts.config = "config"
34 +
35 + #Import after configure to get help message
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  
42   #get locations:
43   Wdir=config.get('Directories','Wdir')
50 MVASubdir=config.get('Directories','MVAdir')
44   samplesinfo=config.get('Directories','samplesinfo')
45  
46   #systematics
54
55
47   INpath = config.get('Directories','MVAin')
48   OUTpath = config.get('Directories','MVAout')
49  
50 < infofile = open(samplesinfo,'r')
51 < info = pickle.load(infofile)
61 < infofile.close()
50 > info = ParseInfo(samplesinfo,INpath)
51 >
52   arglist=opts.discr #RTight_blavla,bsbsb
53  
54   namelistIN=opts.names
55   namelist=namelistIN.split(',')
56  
57 < doinfo=bool(int(opts.update))
57 > #doinfo=bool(int(opts.update))
58  
59   MVAlist=arglist.split(',')
60  
71
61   #CONFIG
62   #factory
63   factoryname=config.get('factory','factoryname')
# Line 90 | Line 79 | longe=40
79   workdir=ROOT.gDirectory.GetPath()
80  
81  
93 #Apply samples
94 infofile = open(samplesinfo,'r')
95 Ainfo = pickle.load(infofile)
96 infofile.close()
97
98
99 class MvaEvaluater:
100    def __init__(self, config, MVAinfo):
101        self.varset = MVAinfo.varset
102        #Define reader
103        self.reader = ROOT.TMVA.Reader("!Color:!Silent")
104        MVAdir=config.get('Directories','vhbbpath')
105        self.systematics=config.get('systematics','systematics').split(' ')
106        self.MVA_Vars={}
107        self.MVAname = MVAinfo.MVAname
108        for systematic in self.systematics:
109            self.MVA_Vars[systematic]=config.get(self.varset,systematic)
110            self.MVA_Vars[systematic]=self.MVA_Vars[systematic].split(' ')
111        #define variables and specatators
112        self.MVA_var_buffer = []
113        for i in range(len( self.MVA_Vars['Nominal'])):
114            self.MVA_var_buffer.append(array( 'f', [ 0 ] ))
115            self.reader.AddVariable( self.MVA_Vars['Nominal'][i],self.MVA_var_buffer[i])
116        self.reader.BookMVA(MVAinfo.MVAname,MVAdir+'/data/'+MVAinfo.getweightfile())
117        #--> Now the MVA is booked
118
119    def setBranches(self,tree,job):
120        #Set formulas for all vars
121        self.MVA_formulas={}
122        for systematic in self.systematics:
123            if job.type == 'DATA' and not systematic == 'Nominal': continue
124            self.MVA_formulas[systematic]=[]
125            for j in range(len( self.MVA_Vars['Nominal'])):
126                self.MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),self.MVA_Vars[systematic][j],tree))
127
128    def evaluate(self,MVAbranches,job):
129        #Evaluate all vars and fill the branches
130        for systematic in self.systematics:
131            for j in range(len( self.MVA_Vars['Nominal'])):
132                if job.type == 'DATA' and not systematic == 'Nominal': continue
133                self.MVA_var_buffer[j][0] = self.MVA_formulas[systematic][j].EvalInstance()                
134            MVAbranches[self.systematics.index(systematic)] = self.reader.EvaluateMVA(self.MVAname)
135
82  
83   theMVAs = []
84   for mva in MVAinfos:
85 <    theMVAs.append(MvaEvaluater(config,mva))
85 >    theMVAs.append(MvaEvaluator(config,mva))
86  
87  
88   #eval
143 for job in Ainfo:
144    if eval(job.active):
145        if job.name in namelist:
146            #get trees:
147            print INpath+'/'+job.prefix+job.identifier+'.root'
148            input = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read')
149            print OUTpath+'/'+job.prefix+job.identifier+'.root'
150            outfile = ROOT.TFile.Open(OUTpath+'/'+job.prefix+job.identifier+'.root','recreate')
151            input.cd()
152            obj = ROOT.TObject
153            for key in ROOT.gDirectory.GetListOfKeys():
154                input.cd()
155                obj = key.ReadObj()
156                #print obj.GetName()
157                if obj.GetName() == job.tree:
158                    continue
159                outfile.cd()
160                #print key.GetName()
161                obj.Write(key.GetName())
162            tree = input.Get(job.tree)
163            nEntries = tree.GetEntries()
164            outfile.cd()
165            newtree = tree.CloneTree(0)
166            
89  
90 <            #Set branch adress for all vars
91 <            for i in range(0,len(theMVAs)):
92 <                theMVAs[i].setBranches(tree,job)
93 <            outfile.cd()
94 <            #Setup Branches
95 <            MVAbranches=[]
96 <            for i in range(0,len(theMVAs)):
97 <                if job.type == 'Data':
98 <                    MVAbranches.append(array('f',[0]))
99 <                    newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
100 <                else:
101 <                    MVAbranches.append(array('f',[0]*11))
102 <                    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')
103 <                MVA_formulas_Nominal = []
104 <            print '\n--> ' + job.name +':'
105 <            #progbar setup
106 <            if nEntries >= longe:
107 <                step=int(nEntries/longe)
108 <                long=longe
109 <            else:
110 <                long=nEntries
111 <                step = 1
112 <            bar=progbar(long)
113 <            #Fill event by event:
114 <            for entry in range(0,nEntries):
115 <                if entry % step == 0:
116 <                    bar.move()
117 <                #load entry
118 <                tree.GetEntry(entry)
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')
95 >    input = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read')
96 >    print(OUTpath+'/'+job.prefix+job.identifier+'.root')
97 >    outfile = ROOT.TFile.Open(OUTpath+'/'+job.prefix+job.identifier+'.root','recreate')
98 >    input.cd()
99 >    obj = ROOT.TObject
100 >    for key in ROOT.gDirectory.GetListOfKeys():
101 >        input.cd()
102 >        obj = key.ReadObj()
103 >        #print obj.GetName()
104 >        if obj.GetName() == job.tree:
105 >            continue
106 >        outfile.cd()
107 >        #print key.GetName()
108 >        obj.Write(key.GetName())
109 >    tree = input.Get(job.tree)
110 >    nEntries = tree.GetEntries()
111 >    outfile.cd()
112 >    newtree = tree.CloneTree(0)
113 >            
114 >    #Set branch adress for all vars
115 >    for i in range(0,len(theMVAs)):
116 >        theMVAs[i].setVariables(tree,job)
117 >    outfile.cd()
118 >    #Setup Branches
119 >    mvaVals=[]
120 >    for i in range(0,len(theMVAs)):
121 >        if job.type == 'Data':
122 >            mvaVals.append(array('f',[0]))
123 >            newtree.Branch(MVAinfos[i].MVAname,mvaVals[i],'nominal/F')
124 >        else:
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 +':')
129 >    #Fill event by event:
130 >    for entry in range(0,nEntries):
131 >        tree.GetEntry(entry)
132                              
133 <                for i in range(0,len(theMVAs)):
134 <                    theMVAs[i].evaluate(MVAbranches[i],job)
135 <                #Fill:
136 <                newtree.Fill()
137 <            newtree.AutoSave()
138 <            outfile.Close()
133 >        for i in range(0,len(theMVAs)):
134 >            theMVAs[i].evaluate(mvaVals[i],job)
135 >        #Fill:
136 >        newtree.Fill()
137 >    newtree.AutoSave()
138 >    outfile.Close()
139                  
140 < print '\n'
206 <
207 < #Update Info:
208 < if doinfo:
209 <    for job in Ainfo:        
210 <        for MVAinfo in MVAinfos:
211 <            job.addcomment('Added MVA %s'%MVAinfo.MVAname)
212 <        job.addpath(MVAdir)
213 <    infofile = open(samplesinfo,'w')
214 <    pickle.dump(Ainfo,infofile)
215 <    infofile.close()
216 <
217 <
140 > print('\n')

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines