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.7 by nmohr, Tue Jun 19 22:55:47 2012 UTC vs.
Revision 1.13 by nmohr, Thu Sep 20 15:57:22 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 optparse import OptionParser
13   from BetterConfigParser import BetterConfigParser
14   from samplesclass import sample
15   from mvainfos import mvainfo
# Line 19 | Line 20 | from printcolor import printc
20   #CONFIGURE
21  
22   #load 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("-P", "--path", dest="path", default="",
31 +                      help="path to samples")
32 + parser.add_option("-S", "--samples", dest="names", default="",
33 +                      help="samples you want to run on")
34 + parser.add_option("-C", "--config", dest="config", default=[], action="append",
35 +                      help="configuration file")
36 + (opts, args) = parser.parse_args(argv)
37 + if opts.config =="":
38 +        opts.config = "config"
39   config = BetterConfigParser()
40 < config.read('./config')
40 > config.read(opts.config)
41 > anaTag = config.get("Analysis","tag")
42  
43   #get locations:
44   Wdir=config.get('Directories','Wdir')
45 <
28 < MVAdir=config.get('Directories','MVAdir')
45 > MVASubdir=config.get('Directories','MVAdir')
46  
47   #systematics
48   systematics=config.get('systematics','systematics')
# Line 40 | Line 57 | systematics=systematics.split(' ')
57   ######################
58   #Evaluate multi: Must Have same treeVars!!!
59  
60 < Apath=sys.argv[1]
61 < arglist=sys.argv[2] #RTight_blavla,bsbsb
60 > Apath=opts.path
61 > infofile = open(Apath+'/samples.info','r')
62 > info = pickle.load(infofile)
63 > infofile.close()
64 > arglist=opts.discr #RTight_blavla,bsbsb
65  
66 < namelistIN=sys.argv[3]
66 > namelistIN=opts.names
67   namelist=namelistIN.split(',')
68  
69 < doinfo=bool(int(sys.argv[4]))
69 > doinfo=bool(int(opts.update))
70  
71   MVAlist=arglist.split(',')
72 + MVAdir=config.get('Directories','vhbbpath')
73  
74   #CONFIG
75   #factory
# Line 62 | Line 83 | factoryname=config.get('factory','factor
83   #MVAinfofiles=[]
84   MVAinfos=[]
85   for MVAname in MVAlist:
86 <    MVAinfofile = open(Wdir+'/weights/'+factoryname+'_'+MVAname+'.info','r')
86 >    MVAinfofile = open(MVAdir+'/data/'+factoryname+'_'+MVAname+'.info','r')
87      MVAinfos.append(pickle.load(MVAinfofile))
88      MVAinfofile.close()
89      
# Line 101 | Line 122 | for i in range(len( MVA_Vars['Nominal'])
122   #        reader.AddSpectator(spectators[i],MVA_spectator_buffer[i])
123   #Load raeder
124   for i in range(0,len(readers)):
125 <    readers[i].BookMVA(MVAinfos[i].MVAname,MVAinfos[i].getweightfile())
125 >    readers[i].BookMVA(MVAinfos[i].MVAname,MVAdir+'/data/'+MVAinfos[i].getweightfile())
126   #--> Now the MVA is booked
127  
128   #Apply samples
# Line 111 | Line 132 | infofile.close()
132  
133   #eval
134   for job in Ainfo:
135 <
136 <    if job.name in namelist:
137 <        #get trees:
138 <        input = TFile.Open(job.getpath(),'read')
139 <        outfile = TFile.Open(job.path+'/'+MVAdir+job.prefix+job.identifier+'.root','recreate')
119 <        input.cd()
120 <        obj = ROOT.TObject
121 <        for key in ROOT.gDirectory.GetListOfKeys():
135 >    if eval(job.active):
136 >        if job.name in namelist:
137 >            #get trees:
138 >            input = TFile.Open(Apath+'/'+job.getpath(),'read')
139 >            outfile = TFile.Open(Apath+'/'+MVASubdir+job.prefix+job.identifier+'.root','recreate')
140              input.cd()
141 <            obj = key.ReadObj()
142 <            #print obj.GetName()
143 <            if obj.GetName() == job.tree:
144 <                continue
141 >            obj = ROOT.TObject
142 >            for key in ROOT.gDirectory.GetListOfKeys():
143 >                input.cd()
144 >                obj = key.ReadObj()
145 >                #print obj.GetName()
146 >                if obj.GetName() == job.tree:
147 >                    continue
148 >                outfile.cd()
149 >                #print key.GetName()
150 >                obj.Write(key.GetName())
151 >            tree = input.Get(job.tree)
152 >            nEntries = tree.GetEntries()
153              outfile.cd()
154 <            #print key.GetName()
155 <            obj.Write(key.GetName())
156 <        tree = input.Get(job.tree)
157 <        nEntries = tree.GetEntries()
158 <        outfile.cd()
159 <        newtree = tree.CloneTree(0)
160 <
161 <        #MCs:
162 <        if job.type != 'DATA':
163 <            MVA_formulas={}
164 <            MVA_formulas4={}
165 <            for systematic in systematics:
166 <                #print '\t\t - ' + systematic
167 <                MVA_formulas[systematic]=[]
168 <                MVA_formulas4[systematic]=[]
154 >            newtree = tree.CloneTree(0)
155 >
156 >            #MCs:
157 >            if job.type != 'DATA':
158 >                MVA_formulas={}
159 >                MVA_formulas4={}
160 >                for systematic in systematics:
161 >                    #print '\t\t - ' + systematic
162 >                    MVA_formulas[systematic]=[]
163 >                    MVA_formulas4[systematic]=[]
164 >                    #create TTreeFormulas
165 >                    for j in range(len( MVA_Vars['Nominal'])):
166 >                        MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
167 >                        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
168 >                outfile.cd()
169 >                #Setup Branches
170 >                MVAbranches=[]
171 >                MVAbranches4=[]
172 >                for i in range(0,len(readers)):
173 >                    MVAbranches.append(array('f',[0]*9))
174 >                    MVAbranches4.append(array('f',[0]*9))
175 >                    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')
176 >                    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')
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:
193 >                        for j in range(len( MVA_Vars['Nominal'])):
194 >                            MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance()
195 >                            
196 >                        for j in range(0,len(readers)):
197 >                            MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
198 >                            
199 >                        for j in range(len( MVA_Vars['Nominal'])):
200 >                            MVA_var_buffer[j][0] = MVA_formulas4[systematic][j].EvalInstance()
201 >                            
202 >                        for j in range(0,len(readers)):
203 >                            MVAbranches4[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
204 >                    #Fill:
205 >                    newtree.Fill()
206 >                newtree.AutoSave()
207 >                outfile.Close()
208 >                
209 >            #DATA:
210 >            if job.type == 'DATA':
211 >                #MVA Formulas
212 >                MVA_formulas_Nominal = []
213                  #create TTreeFormulas
214                  for j in range(len( MVA_Vars['Nominal'])):
215 <                    MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
216 <                    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
217 <            outfile.cd()
218 <            #Setup Branches
219 <            MVAbranches=[]
220 <            MVAbranches4=[]
221 <            for i in range(0,len(readers)):
222 <                MVAbranches.append(array('f',[0]*9))
223 <                MVAbranches4.append(array('f',[0]*9))
224 <                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')
225 <                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')
226 <            print '\n--> ' + job.name +':'
227 <            #progbar setup
228 <            if nEntries >= longe:
229 <                step=int(nEntries/longe)
230 <                long=longe
231 <            else:
232 <                long=nEntries
233 <                step = 1
234 <            bar=progbar(long)
235 <            #Fill event by event:
236 <            for entry in range(0,nEntries):
237 <                if entry % step == 0:
168 <                    bar.move()
169 <                #load entry
170 <                tree.GetEntry(entry)
171 <                for systematic in systematics:
172 <                    for j in range(len( MVA_Vars['Nominal'])):
173 <                        MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance()
174 <                        
175 <                    for j in range(0,len(readers)):
176 <                        MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
177 <                        
215 >                    MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree))
216 >                outfile.cd()
217 >                MVAbranches=[]
218 >                for i in range(0,len(readers)):
219 >                    MVAbranches.append(array('f',[0]))
220 >                    newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
221 >                    newtree.Branch(MVAinfos[i].MVAname+'_4',MVAbranches[i],'nominal/F')
222 >                #progbar          
223 >                print '\n--> ' + job.name +':'
224 >                if nEntries >= longe:
225 >                    step=int(nEntries/longe)
226 >                    long=longe
227 >                else:
228 >                    long=nEntries
229 >                    step = 1
230 >                bar=progbar(long)
231 >                #Fill event by event:
232 >                for entry in range(0,nEntries):
233 >                    if entry % step == 0:
234 >                        bar.move()
235 >                    #load entry
236 >                    tree.GetEntry(entry)
237 >                    #nominal:
238                      for j in range(len( MVA_Vars['Nominal'])):
239 <                        MVA_var_buffer[j][0] = MVA_formulas4[systematic][j].EvalInstance()
240 <                        
239 >                            MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance()
240 >                            
241                      for j in range(0,len(readers)):
242 <                        MVAbranches4[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
243 <                #Fill:
244 <                newtree.Fill()
245 <            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()
242 >                        MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname)
243 >                    newtree.Fill()
244 >                newtree.AutoSave()
245 >                outfile.Close()
246  
247   print '\n'
248  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines