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.3 by peller, Wed May 9 15:25:27 2012 UTC vs.
Revision 1.4 by peller, Thu May 10 16:16:05 2012 UTC

# Line 25 | Line 25 | config.read('./config')
25   #get locations:
26   Wdir=config.get('Directories','Wdir')
27  
28 + MVAdir=config.get('Directories','MVAdir')
29  
30   #systematics
31   systematics=config.get('systematics','systematics')
# Line 42 | Line 43 | for systematic in systematics:
43   Apath=sys.argv[1]
44   arglist=sys.argv[2] #RTight_blavla,bsbsb
45  
46 < #for axample
47 < #0 5 0
48 < #and
49 < #5 -1 1
49 <
50 < start=int(sys.argv[3])
51 < stop=int(sys.argv[4])
52 < doinfo=bool(int(sys.argv[5]))
46 > namelistIN=sys.argv[3]
47 > namelist=namelistIN.split(',')
48 >
49 > doinfo=bool(int(sys.argv[4]))
50  
51   MVAlist=arglist.split(',')
52  
# Line 112 | Line 109 | Ainfo = pickle.load(infofile)
109   infofile.close()
110  
111   #eval
112 < for job in Ainfo[start:stop]:
113 <    #get trees:
114 <    input = TFile.Open(job.getpath(),'read')
115 <    outfile = TFile.Open(job.path+'/MVAout/'+job.prefix+job.identifier+'.root','recreate')
116 <    input.cd()
117 <    obj = ROOT.TObject
121 <    for key in ROOT.gDirectory.GetListOfKeys():
112 > for job in Ainfo:
113 >
114 >    if job.name in namelist:
115 >        #get trees:
116 >        input = TFile.Open(job.getpath(),'read')
117 >        outfile = TFile.Open(job.path+'/'+MVAdir+job.prefix+job.identifier+'.root','recreate')
118          input.cd()
119 <        obj = key.ReadObj()
120 <        #print obj.GetName()
121 <        if obj.GetName() == job.tree:
122 <            continue
119 >        obj = ROOT.TObject
120 >        for key in ROOT.gDirectory.GetListOfKeys():
121 >            input.cd()
122 >            obj = key.ReadObj()
123 >            #print obj.GetName()
124 >            if obj.GetName() == job.tree:
125 >                continue
126 >            outfile.cd()
127 >            #print key.GetName()
128 >            obj.Write(key.GetName())
129 >        tree = input.Get(job.tree)
130 >        nEntries = tree.GetEntries()
131          outfile.cd()
132 <        #print key.GetName()
133 <        obj.Write(key.GetName())
134 <    tree = input.Get(job.tree)
135 <    nEntries = tree.GetEntries()
136 <    outfile.cd()
137 <    newtree = tree.CloneTree(0)
138 <
139 <    #MCs:
140 <    if job.type != 'DATA':
141 <        MVA_formulas={}
142 <        for systematic in systematics:
143 <            #print '\t\t - ' + systematic
144 <            MVA_formulas[systematic]=[]
132 >        newtree = tree.CloneTree(0)
133 >
134 >        #MCs:
135 >        if job.type != 'DATA':
136 >            MVA_formulas={}
137 >            for systematic in systematics:
138 >                #print '\t\t - ' + systematic
139 >                MVA_formulas[systematic]=[]
140 >                #create TTreeFormulas
141 >                for j in range(len( MVA_Vars['Nominal'])):
142 >                    MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
143 >            outfile.cd()
144 >            #Setup Branches
145 >            MVAbranches=[]
146 >            for i in range(0,len(readers)):
147 >                MVAbranches.append(array('f',[0]*9))
148 >                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')
149 >            print '\n--> ' + job.name +':'
150 >            #progbar setup
151 >            if nEntries >= longe:
152 >                step=int(nEntries/longe)
153 >                long=longe
154 >            else:
155 >                long=nEntries
156 >                step = 1
157 >            bar=progbar(long)
158 >            #Fill event by event:
159 >            for entry in range(0,nEntries):
160 >                if entry % step == 0:
161 >                    bar.move()
162 >                #load entry
163 >                tree.GetEntry(entry)
164 >                for systematic in systematics:
165 >                    for j in range(len( MVA_Vars['Nominal'])):
166 >                        MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance()
167 >                        
168 >                    for j in range(0,len(readers)):
169 >                        MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
170 >                #Fill:
171 >                newtree.Fill()
172 >            newtree.AutoSave()
173 >            outfile.Close()
174 >            
175 >        #DATA:
176 >        if job.type == 'DATA':
177 >            #MVA Formulas
178 >            MVA_formulas_Nominal = []
179              #create TTreeFormulas
180              for j in range(len( MVA_Vars['Nominal'])):
181 <                MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
182 <        outfile.cd()
183 <        #Setup Branches
184 <        MVAbranches=[]
185 <        for i in range(0,len(readers)):
186 <            MVAbranches.append(array('f',[0]*9))
187 <            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')
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)
165 <            for systematic in systematics:
181 >                MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree))
182 >            outfile.cd()
183 >            MVAbranches=[]
184 >            for i in range(0,len(readers)):
185 >                MVAbranches.append(array('f',[0]))
186 >                newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
187 >            #progbar          
188 >            print '\n--> ' + job.name +':'
189 >            if nEntries >= longe:
190 >                step=int(nEntries/longe)
191 >                long=longe
192 >            else:
193 >                long=nEntries
194 >                step = 1
195 >            bar=progbar(long)
196 >            #Fill event by event:
197 >            for entry in range(0,nEntries):
198 >                if entry % step == 0:
199 >                    bar.move()
200 >                #load entry
201 >                tree.GetEntry(entry)
202 >                #nominal:
203                  for j in range(len( MVA_Vars['Nominal'])):
204 <                    MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance()
205 <                    
204 >                        MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance()
205 >                        
206                  for j in range(0,len(readers)):
207 <                    MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
208 <            #Fill:
209 <            newtree.Fill()
210 <        newtree.AutoSave()
174 <        outfile.Close()
175 <        
176 <    #DATA:
177 <    if job.type == 'DATA':
178 <        #MVA Formulas
179 <        MVA_formulas_Nominal = []
180 <        #create TTreeFormulas
181 <        for j in range(len( MVA_Vars['Nominal'])):
182 <            MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree))
183 <        outfile.cd()
184 <        MVAbranches=[]
185 <        for i in range(0,len(readers)):
186 <            MVAbranches.append(array('f',[0]))
187 <            newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
188 <        #progbar          
189 <        print '\n--> ' + job.name +':'
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 <            #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 <
207 >                    MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname)
208 >                newtree.Fill()
209 >            newtree.AutoSave()
210 >            outfile.Close()
211  
212 + print '\n'
213  
214   #Update Info:
215   if doinfo:
216      for job in Ainfo:        
217          for MVAinfo in MVAinfos:
218              job.addcomment('Added MVA %s'%MVAinfo.MVAname)
219 <        job.addpath('/MVAout')
220 <    infofile = open(Apath+'/MVAout/samples.info','w')
219 >        job.addpath(MVAdir)
220 >    infofile = open(Apath+MVAdir+'/samples.info','w')
221      pickle.dump(Ainfo,infofile)
222      infofile.close()
223  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines