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.5 by peller, Wed May 23 11:44:41 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
12 > from optparse import OptionParser
13 > from BetterConfigParser import BetterConfigParser
14   from samplesclass import sample
15   from mvainfos import mvainfo
16   import pickle
# Line 17 | Line 18 | 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 <
49 < MVAdir=config.get('Directories','MVAdir')
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 > infofile = open(samplesinfo,'r')
70 > info = pickle.load(infofile)
71 > infofile.close()
72 > arglist=opts.discr #RTight_blavla,bsbsb
73  
74 < namelistIN=sys.argv[3]
74 > namelistIN=opts.names
75   namelist=namelistIN.split(',')
76  
77 < doinfo=bool(int(sys.argv[4]))
77 > doinfo=bool(int(opts.update))
78  
79   MVAlist=arglist.split(',')
80 + MVAdir=config.get('Directories','vhbbpath')
81  
82   #CONFIG
83   #factory
# Line 62 | 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 101 | 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:
143 <
144 <    if job.name in namelist:
145 <        #get trees:
146 <        input = TFile.Open(job.getpath(),'read')
147 <        outfile = TFile.Open(job.path+'/'+MVAdir+job.prefix+job.identifier+'.root','recreate')
148 <        input.cd()
149 <        obj = ROOT.TObject
121 <        for key in ROOT.gDirectory.GetListOfKeys():
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 = key.ReadObj()
152 <            #print obj.GetName()
153 <            if obj.GetName() == job.tree:
154 <                continue
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 <            #print key.GetName()
165 <            obj.Write(key.GetName())
166 <        tree = input.Get(job.tree)
167 <        nEntries = tree.GetEntries()
168 <        outfile.cd()
169 <        newtree = tree.CloneTree(0)
170 <
171 <        #MCs:
172 <        if job.type != 'DATA':
173 <            MVA_formulas={}
174 <            MVA_formulas4={}
175 <            for systematic in systematics:
176 <                #print '\t\t - ' + systematic
177 <                MVA_formulas[systematic]=[]
178 <                MVA_formulas4[systematic]=[]
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_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
227 <                    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
228 <            outfile.cd()
229 <            #Setup Branches
230 <            MVAbranches=[]
231 <            MVAbranches4=[]
232 <            for i in range(0,len(readers)):
233 <                MVAbranches.append(array('f',[0]*9))
234 <                MVAbranches4.append(array('f',[0]*9))
235 <                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')
236 <                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/F')
237 <            print '\n--> ' + job.name +':'
238 <            #progbar setup
239 <            if nEntries >= longe:
240 <                step=int(nEntries/longe)
241 <                long=longe
242 <            else:
243 <                long=nEntries
244 <                step = 1
245 <            bar=progbar(long)
246 <            #Fill event by event:
247 <            for entry in range(0,nEntries):
248 <                if entry % step == 0:
168 <                    bar.move()
169 <                #load entry
170 <                tree.GetEntry(entry)
171 <                for systematic in systematics:
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[systematic][j].EvalInstance()
251 <                        
250 >                            MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance()
251 >                            
252                      for j in range(0,len(readers)):
253 <                        MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
254 <                        
255 <                    for j in range(len( MVA_Vars['Nominal'])):
256 <                        MVA_var_buffer[j][0] = MVA_formulas4[systematic][j].EvalInstance()
180 <                        
181 <                    for j in range(0,len(readers)):
182 <                        MVAbranches4[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
183 <                #Fill:
184 <                newtree.Fill()
185 <            newtree.AutoSave()
186 <            outfile.Close()
187 <            
188 <        #DATA:
189 <        if job.type == 'DATA':
190 <            #MVA Formulas
191 <            MVA_formulas_Nominal = []
192 <            #create TTreeFormulas
193 <            for j in range(len( MVA_Vars['Nominal'])):
194 <                MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree))
195 <            outfile.cd()
196 <            MVAbranches=[]
197 <            for i in range(0,len(readers)):
198 <                MVAbranches.append(array('f',[0]))
199 <                newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
200 <                newtree.Branch(MVAinfos[i].MVAname+'_4',MVAbranches[i],'nominal/F')
201 <            #progbar          
202 <            print '\n--> ' + job.name +':'
203 <            if nEntries >= longe:
204 <                step=int(nEntries/longe)
205 <                long=longe
206 <            else:
207 <                long=nEntries
208 <                step = 1
209 <            bar=progbar(long)
210 <            #Fill event by event:
211 <            for entry in range(0,nEntries):
212 <                if entry % step == 0:
213 <                    bar.move()
214 <                #load entry
215 <                tree.GetEntry(entry)
216 <                #nominal:
217 <                for j in range(len( MVA_Vars['Nominal'])):
218 <                        MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance()
219 <                        
220 <                for j in range(0,len(readers)):
221 <                    MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname)
222 <                newtree.Fill()
223 <            newtree.AutoSave()
224 <            outfile.Close()
253 >                        MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname)
254 >                    newtree.Fill()
255 >                newtree.AutoSave()
256 >                outfile.Close()
257  
258   print '\n'
259  
# Line 231 | Line 263 | if doinfo:
263          for MVAinfo in MVAinfos:
264              job.addcomment('Added MVA %s'%MVAinfo.MVAname)
265          job.addpath(MVAdir)
266 <    infofile = open(Apath+MVAdir+'/samples.info','w')
266 >    infofile = open(samplesinfo,'w')
267      pickle.dump(Ainfo,infofile)
268      infofile.close()
269  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines