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.6 by nmohr, Wed Jun 6 21:22:17 2012 UTC vs.
Revision 1.9 by nmohr, Thu Aug 9 13:03:29 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[5:]
25 + parser = OptionParser()
26 + parser.add_option("-C", "--config", dest="config", default=[], action="append",
27 +                      help="configuration defining the plots to make")
28 + (opts, args) = parser.parse_args(argv)
29 + if opts.config ==[]:
30 +        opts.config = "config"
31 + print opts.config
32   config = BetterConfigParser()
33 < config.read('./config')
33 > config.read(opts.config)
34 > anaTag = config.get("Analysis","tag")
35  
36   #get locations:
37   Wdir=config.get('Directories','Wdir')
# Line 32 | Line 43 | systematics=config.get('systematics','sy
43   systematics=systematics.split(' ')
44  
45   #TreeVar Array
46 < MVA_Vars={}
47 < for systematic in systematics:
48 <    MVA_Vars[systematic]=config.get('treeVars',systematic)
49 <    MVA_Vars[systematic]=MVA_Vars[systematic].split(' ')
46 > #MVA_Vars={}
47 > #for systematic in systematics:
48 > #    MVA_Vars[systematic]=config.get('treeVars',systematic)
49 > #    MVA_Vars[systematic]=MVA_Vars[systematic].split(' ')
50  
51   ######################
52   #Evaluate multi: Must Have same treeVars!!!
53  
54   Apath=sys.argv[1]
55 + infofile = open(Apath+'/samples.info','r')
56 + info = pickle.load(infofile)
57 + infofile.close()
58   arglist=sys.argv[2] #RTight_blavla,bsbsb
59  
60   namelistIN=sys.argv[3]
# Line 111 | Line 125 | infofile.close()
125  
126   #eval
127   for job in Ainfo:
128 <
129 <    if job.name in namelist:
130 <        #get trees:
131 <        input = TFile.Open(job.getpath(),'read')
132 <        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():
128 >    if eval(job.active):
129 >        if job.name in namelist:
130 >            #get trees:
131 >            input = TFile.Open(job.getpath(),'read')
132 >            outfile = TFile.Open(job.path+'/'+MVAdir+job.prefix+job.identifier+'.root','recreate')
133              input.cd()
134 <            obj = key.ReadObj()
135 <            #print obj.GetName()
136 <            if obj.GetName() == job.tree:
137 <                continue
134 >            obj = ROOT.TObject
135 >            for key in ROOT.gDirectory.GetListOfKeys():
136 >                input.cd()
137 >                obj = key.ReadObj()
138 >                #print obj.GetName()
139 >                if obj.GetName() == job.tree:
140 >                    continue
141 >                outfile.cd()
142 >                #print key.GetName()
143 >                obj.Write(key.GetName())
144 >            tree = input.Get(job.tree)
145 >            nEntries = tree.GetEntries()
146              outfile.cd()
147 <            #print key.GetName()
148 <            obj.Write(key.GetName())
149 <        tree = input.Get(job.tree)
150 <        nEntries = tree.GetEntries()
151 <        outfile.cd()
152 <        newtree = tree.CloneTree(0)
153 <
154 <        #MCs:
155 <        if job.type != 'DATA':
156 <            MVA_formulas={}
157 <            MVA_formulas4={}
158 <            for systematic in systematics:
159 <                #print '\t\t - ' + systematic
160 <                MVA_formulas[systematic]=[]
161 <                MVA_formulas4[systematic]=[]
147 >            newtree = tree.CloneTree(0)
148 >
149 >            #MCs:
150 >            if job.type != 'DATA':
151 >                MVA_formulas={}
152 >                MVA_formulas4={}
153 >                for systematic in systematics:
154 >                    #print '\t\t - ' + systematic
155 >                    MVA_formulas[systematic]=[]
156 >                    MVA_formulas4[systematic]=[]
157 >                    #create TTreeFormulas
158 >                    for j in range(len( MVA_Vars['Nominal'])):
159 >                        MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
160 >                        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
161 >                outfile.cd()
162 >                #Setup Branches
163 >                MVAbranches=[]
164 >                MVAbranches4=[]
165 >                for i in range(0,len(readers)):
166 >                    MVAbranches.append(array('f',[0]*9))
167 >                    MVAbranches4.append(array('f',[0]*9))
168 >                    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')
169 >                    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')
170 >                print '\n--> ' + job.name +':'
171 >                #progbar setup
172 >                if nEntries >= longe:
173 >                    step=int(nEntries/longe)
174 >                    long=longe
175 >                else:
176 >                    long=nEntries
177 >                    step = 1
178 >                bar=progbar(long)
179 >                #Fill event by event:
180 >                for entry in range(0,nEntries):
181 >                    if entry % step == 0:
182 >                        bar.move()
183 >                    #load entry
184 >                    tree.GetEntry(entry)
185 >                    for systematic in systematics:
186 >                        for j in range(len( MVA_Vars['Nominal'])):
187 >                            MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance()
188 >                            
189 >                        for j in range(0,len(readers)):
190 >                            MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
191 >                            
192 >                        for j in range(len( MVA_Vars['Nominal'])):
193 >                            MVA_var_buffer[j][0] = MVA_formulas4[systematic][j].EvalInstance()
194 >                            
195 >                        for j in range(0,len(readers)):
196 >                            MVAbranches4[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
197 >                    #Fill:
198 >                    newtree.Fill()
199 >                newtree.AutoSave()
200 >                outfile.Close()
201 >                
202 >            #DATA:
203 >            if job.type == 'DATA':
204 >                #MVA Formulas
205 >                MVA_formulas_Nominal = []
206                  #create TTreeFormulas
207                  for j in range(len( MVA_Vars['Nominal'])):
208 <                    MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
209 <                    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
210 <            outfile.cd()
211 <            #Setup Branches
212 <            MVAbranches=[]
213 <            MVAbranches4=[]
214 <            for i in range(0,len(readers)):
215 <                MVAbranches.append(array('f',[0]*9))
216 <                MVAbranches4.append(array('f',[0]*9))
217 <                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')
218 <                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')
219 <            print '\n--> ' + job.name +':'
220 <            #progbar setup
221 <            if nEntries >= longe:
222 <                step=int(nEntries/longe)
223 <                long=longe
224 <            else:
225 <                long=nEntries
226 <                step = 1
227 <            bar=progbar(long)
228 <            #Fill event by event:
229 <            for entry in range(0,nEntries):
230 <                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 <                        
208 >                    MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree))
209 >                outfile.cd()
210 >                MVAbranches=[]
211 >                for i in range(0,len(readers)):
212 >                    MVAbranches.append(array('f',[0]))
213 >                    newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
214 >                    newtree.Branch(MVAinfos[i].MVAname+'_4',MVAbranches[i],'nominal/F')
215 >                #progbar          
216 >                print '\n--> ' + job.name +':'
217 >                if nEntries >= longe:
218 >                    step=int(nEntries/longe)
219 >                    long=longe
220 >                else:
221 >                    long=nEntries
222 >                    step = 1
223 >                bar=progbar(long)
224 >                #Fill event by event:
225 >                for entry in range(0,nEntries):
226 >                    if entry % step == 0:
227 >                        bar.move()
228 >                    #load entry
229 >                    tree.GetEntry(entry)
230 >                    #nominal:
231                      for j in range(len( MVA_Vars['Nominal'])):
232 <                        MVA_var_buffer[j][0] = MVA_formulas4[systematic][j].EvalInstance()
233 <                        
232 >                            MVA_var_buffer[j][0] = MVA_formulas_Nominal[j].EvalInstance()
233 >                            
234                      for j in range(0,len(readers)):
235 <                        MVAbranches4[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
236 <                #Fill:
237 <                newtree.Fill()
238 <            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()
235 >                        MVAbranches[j][0]= readers[j].EvaluateMVA(MVAinfos[j].MVAname)
236 >                    newtree.Fill()
237 >                newtree.AutoSave()
238 >                outfile.Close()
239  
240   print '\n'
241  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines