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.2 by peller, Wed May 9 09:48:23 2012 UTC vs.
Revision 1.17 by peller, Tue Nov 27 09:00:27 2012 UTC

# Line 9 | Line 9 | from copy import copy
9   #suppres the EvalInstace conversion warning bug
10   import warnings
11   warnings.filterwarnings( action='ignore', category=RuntimeWarning, message='creating converter.*' )
12 < from ConfigParser import SafeConfigParser
13 < from samplesinfo import sample
12 > from optparse import OptionParser
13 > from BetterConfigParser import BetterConfigParser
14 > from samplesclass import sample
15   from mvainfos import mvainfo
16   import pickle
17   from progbar import progbar
18   from printcolor import printc
19  
20   #CONFIGURE
21 <
21 > print 'hello'
22   #load config
23 < config = SafeConfigParser()
24 < config.read('./config')
23 > #os.mkdir(path+'/sys')
24 > argv = sys.argv
25 > parser = OptionParser()
26 > parser.add_option("-U", "--update", dest="update", default=0,
27 >                      help="update infofile")
28 > parser.add_option("-D", "--discr", dest="discr", default="",
29 >                      help="discriminators to be added")
30 > #parser.add_option("-I", "--inpath", dest="inpath", default="",
31 > #                      help="path to samples")
32 > #parser.add_option("-O", "--outpath", dest="outpath", default="",
33 > #                      help="path where to store output samples")
34 > parser.add_option("-S", "--samples", dest="names", default="",
35 >                      help="samples you want to run on")
36 > parser.add_option("-C", "--config", dest="config", default=[], action="append",
37 >                      help="configuration file")
38 > (opts, args) = parser.parse_args(argv)
39 > if opts.config =="":
40 >        opts.config = "config"
41 > config = BetterConfigParser()
42 > #config.read('./config7TeV_ZZ')
43 > config.read(opts.config)
44 > anaTag = config.get("Analysis","tag")
45  
46   #get locations:
47   Wdir=config.get('Directories','Wdir')
48 <
48 > MVASubdir=config.get('Directories','MVAdir')
49 > samplesinfo=config.get('Directories','samplesinfo')
50  
51   #systematics
52   systematics=config.get('systematics','systematics')
53   systematics=systematics.split(' ')
54  
55   #TreeVar Array
56 < MVA_Vars={}
57 < for systematic in systematics:
58 <    MVA_Vars[systematic]=config.get('treeVars',systematic)
59 <    MVA_Vars[systematic]=MVA_Vars[systematic].split(' ')
56 > #MVA_Vars={}
57 > #for systematic in systematics:
58 > #    MVA_Vars[systematic]=config.get('treeVars',systematic)
59 > #    MVA_Vars[systematic]=MVA_Vars[systematic].split(' ')
60  
61   ######################
62   #Evaluate multi: Must Have same treeVars!!!
63  
64 < Apath=sys.argv[1]
65 < arglist=sys.argv[2] #RTight_blavla,bsbsb
64 > #OUTpath=opts.outpath
65 > #INpath=opts.inpath
66 > INpath = config.get('Directories','MVAin')
67 > OUTpath = config.get('Directories','MVAout')
68  
69 < #for axample
70 < #0 5 0
71 < #and
72 < #5 -1 1
73 <
74 < start=int(sys.argv[3])
75 < stop=int(sys.argv[4])
76 < doinfo=bool(int(sys.argv[5]))
69 > infofile = open(samplesinfo,'r')
70 > info = pickle.load(infofile)
71 > infofile.close()
72 > arglist=opts.discr #RTight_blavla,bsbsb
73 >
74 > namelistIN=opts.names
75 > namelist=namelistIN.split(',')
76 >
77 > doinfo=bool(int(opts.update))
78  
79   MVAlist=arglist.split(',')
80 + MVAdir=config.get('Directories','vhbbpath')
81  
82   #CONFIG
83   #factory
# Line 65 | Line 91 | factoryname=config.get('factory','factor
91   #MVAinfofiles=[]
92   MVAinfos=[]
93   for MVAname in MVAlist:
94 <    MVAinfofile = open(Wdir+'/weights/'+factoryname+'_'+MVAname+'.info','r')
94 >    MVAinfofile = open(MVAdir+'/data/'+factoryname+'_'+MVAname+'.info','r')
95      MVAinfos.append(pickle.load(MVAinfofile))
96      MVAinfofile.close()
97      
# Line 92 | Line 118 | for MVA in MVAlist:
118  
119   #define variables and specatators
120   MVA_var_buffer = []
121 + MVA_var_buffer4 = []
122   for i in range(len( MVA_Vars['Nominal'])):
123      MVA_var_buffer.append(array( 'f', [ 0 ] ))
124      for reader in readers:
# Line 103 | Line 130 | for i in range(len( MVA_Vars['Nominal'])
130   #        reader.AddSpectator(spectators[i],MVA_spectator_buffer[i])
131   #Load raeder
132   for i in range(0,len(readers)):
133 <    readers[i].BookMVA(MVAinfos[i].MVAname,MVAinfos[i].getweightfile())
133 >    readers[i].BookMVA(MVAinfos[i].MVAname,MVAdir+'/data/'+MVAinfos[i].getweightfile())
134   #--> Now the MVA is booked
135  
136   #Apply samples
137 < infofile = open(Apath+'/samples.info','r')
137 > infofile = open(samplesinfo,'r')
138   Ainfo = pickle.load(infofile)
139   infofile.close()
140  
141   #eval
142 < for job in Ainfo[start:stop]:
143 <    #get trees:
144 <    input = TFile.Open(job.getpath(),'read')
145 <    outfile = TFile.Open(job.path+'/MVAout2/'+job.prefix+job.identifier+'.root','recreate')
146 <    input.cd()
147 <    obj = ROOT.TObject
148 <    for key in ROOT.gDirectory.GetListOfKeys():
149 <        input.cd()
150 <        obj = key.ReadObj()
151 <        print obj.GetName()
152 <        if obj.GetName() == job.tree:
153 <            continue
154 <        outfile.cd()
155 <        print key.GetName()
156 <        obj.Write(key.GetName())
157 <    tree = input.Get(job.tree)
158 <    nEntries = tree.GetEntries()
159 <    outfile.cd()
160 <    newtree = tree.CloneTree(0)
161 <
162 <    #MCs:
163 <    if job.type != 'DATA':
164 <        MVA_formulas={}
165 <        for systematic in systematics:
166 <            #print '\t\t - ' + systematic
167 <            MVA_formulas[systematic]=[]
168 <            #create TTreeFormulas
169 <            for j in range(len( MVA_Vars['Nominal'])):
170 <                MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
171 <        outfile.cd()
172 <        #Setup Branches
173 <        MVAbranches=[]
174 <        for i in range(0,len(readers)):
175 <            MVAbranches.append(array('f',[0]*9))
176 <            newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down/F')
177 <        print '\n--> ' + job.name +':'
178 <        #progbar setup
179 <        if nEntries >= longe:
180 <            step=int(nEntries/longe)
181 <            long=longe
182 <        else:
183 <            long=nEntries
184 <            step = 1
185 <        bar=progbar(long)
186 <        #Fill event by event:
187 <        for entry in range(0,nEntries):
188 <            if entry % step == 0:
189 <                bar.move()
190 <            #load entry
191 <            tree.GetEntry(entry)
192 <            for systematic in systematics:
142 > for job in Ainfo:
143 >    if eval(job.active):
144 >        if job.name in namelist:
145 >            #get trees:
146 >            print INpath+'/'+job.prefix+job.identifier+'.root'
147 >            input = TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read')
148 >            print OUTpath+'/'+job.prefix+job.identifier+'.root'
149 >            outfile = TFile.Open(OUTpath+'/'+job.prefix+job.identifier+'.root','recreate')
150 >            input.cd()
151 >            obj = ROOT.TObject
152 >            for key in ROOT.gDirectory.GetListOfKeys():
153 >                input.cd()
154 >                obj = key.ReadObj()
155 >                #print obj.GetName()
156 >                if obj.GetName() == job.tree:
157 >                    continue
158 >                outfile.cd()
159 >                #print key.GetName()
160 >                obj.Write(key.GetName())
161 >            tree = input.Get(job.tree)
162 >            nEntries = tree.GetEntries()
163 >            outfile.cd()
164 >            newtree = tree.CloneTree(0)
165 >            #input.Close()
166 >
167 >            #MCs:
168 >            if job.type != 'DATA':
169 >                MVA_formulas={}
170 >                MVA_formulas4={}
171 >                for systematic in systematics:
172 >                    #print '\t\t - ' + systematic
173 >                    MVA_formulas[systematic]=[]
174 >                    MVA_formulas4[systematic]=[]
175 >                    #create TTreeFormulas
176 >                    for j in range(len( MVA_Vars['Nominal'])):
177 >                        MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
178 >                        MVA_formulas4[systematic].append(ROOT.TTreeFormula("MVA_formula4%s_%s"%(j,systematic),MVA_Vars['Nominal'][j]+'+('+MVA_Vars[systematic][j]+'-'+MVA_Vars['Nominal'][j]+')*4',tree))#HERE change
179 >                outfile.cd()
180 >                #Setup Branches
181 >                MVAbranches=[]
182 >                MVAbranches4=[]
183 >                for i in range(0,len(readers)):
184 >                    MVAbranches.append(array('f',[0]*11))
185 >                    MVAbranches4.append(array('f',[0]*11))
186 >                    newtree.Branch(MVAinfos[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')
187 >                    newtree.Branch(MVAinfos[i].MVAname+'_4',MVAbranches4[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down:beff1_up:beff1_down/F')
188 >                print '\n--> ' + job.name +':'
189 >                #progbar setup
190 >                if nEntries >= longe:
191 >                    step=int(nEntries/longe)
192 >                    long=longe
193 >                else:
194 >                    long=nEntries
195 >                    step = 1
196 >                bar=progbar(long)
197 >                #Fill event by event:
198 >                for entry in range(0,nEntries):
199 >                    if entry % step == 0:
200 >                        bar.move()
201 >                    #load entry
202 >                    tree.GetEntry(entry)
203 >                    for systematic in systematics:
204 >                        for j in range(len( MVA_Vars['Nominal'])):
205 >                            MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance()
206 >                            
207 >                        for j in range(0,len(readers)):
208 >                            MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
209 >                            
210 >                        for j in range(len( MVA_Vars['Nominal'])):
211 >                            MVA_var_buffer[j][0] = MVA_formulas4[systematic][j].EvalInstance()
212 >                            
213 >                        for j in range(0,len(readers)):
214 >                            MVAbranches4[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
215 >                    #Fill:
216 >                    newtree.Fill()
217 >                newtree.AutoSave()
218 >                outfile.Close()
219 >                
220 >            #DATA:
221 >            if job.type == 'DATA':
222 >                #MVA Formulas
223 >                MVA_formulas_Nominal = []
224 >                #create TTreeFormulas
225                  for j in range(len( MVA_Vars['Nominal'])):
226 <                    MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance()
227 <                    
228 <                for j in range(0,len(readers)):
229 <                    MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
230 <            #Fill:
231 <            newtree.Fill()
232 <        newtree.AutoSave()
233 <        outfile.Close()
234 <        
235 <    #DATA:
236 <    if job.type == 'DATA':
237 <        #MVA Formulas
238 <        MVA_formulas_Nominal = []
239 <        #create TTreeFormulas
240 <        for j in range(len( MVA_Vars['Nominal'])):
241 <            MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree))
242 <        outfile.cd()
243 <        MVAbranches=[]
244 <        for i in range(0,len(readers)):
245 <            MVAbranches.append(array('f',[0]))
246 <            newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
247 <        #progbar          
248 <        print '\n--> ' + job.name +':'
249 <        if nEntries >= longe:
250 <            step=int(nEntries/longe)
251 <            long=longe
252 <        else:
253 <            long=nEntries
254 <            step = 1
255 <        bar=progbar(long)
256 <        #Fill event by event:
198 <        for entry in range(0,nEntries):
199 <            if entry % step == 0:
200 <                bar.move()
201 <            #load entry
202 <            tree.GetEntry(entry)
203 <            #nominal:
204 <            for j in range(len( MVA_Vars['Nominal'])):
205 <                    MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance()
206 <                    
207 <            for j in range(0,len(readers)):
208 <                MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname)
209 <            newtree.Fill()
210 <        newtree.AutoSave()
211 <        outfile.Close()
212 <
213 <
214 <
226 >                    MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree))
227 >                outfile.cd()
228 >                MVAbranches=[]
229 >                for i in range(0,len(readers)):
230 >                    MVAbranches.append(array('f',[0]))
231 >                    newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
232 >                    newtree.Branch(MVAinfos[i].MVAname+'_4',MVAbranches[i],'nominal/F')
233 >                #progbar          
234 >                print '\n--> ' + job.name +':'
235 >                if nEntries >= longe:
236 >                    step=int(nEntries/longe)
237 >                    long=longe
238 >                else:
239 >                    long=nEntries
240 >                    step = 1
241 >                bar=progbar(long)
242 >                #Fill event by event:
243 >                for entry in range(0,nEntries):
244 >                    if entry % step == 0:
245 >                        bar.move()
246 >                    #load entry
247 >                    tree.GetEntry(entry)
248 >                    #nominal:
249 >                    for j in range(len( MVA_Vars['Nominal'])):
250 >                            MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance()
251 >                            
252 >                    for j in range(0,len(readers)):
253 >                        MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname)
254 >                    newtree.Fill()
255 >                newtree.AutoSave()
256 >                outfile.Close()
257  
258 + print '\n'
259  
260   #Update Info:
261   if doinfo:
262      for job in Ainfo:        
263          for MVAinfo in MVAinfos:
264              job.addcomment('Added MVA %s'%MVAinfo.MVAname)
265 <        job.addpath('/MVAout')
266 <    infofile = open(Apath+'/MVAout/samples.info','w')
265 >        job.addpath(MVAdir)
266 >    infofile = open(samplesinfo,'w')
267      pickle.dump(Ainfo,infofile)
268      infofile.close()
269  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines