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.1 by peller, Tue May 8 10:37:17 2012 UTC vs.
Revision 1.8 by peller, Thu Aug 2 16:03:52 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 BetterConfigParser import BetterConfigParser
13 > from samplesclass import sample
14   from mvainfos import mvainfo
15   import pickle
16   from progbar import progbar
# Line 19 | Line 19 | from printcolor import printc
19   #CONFIGURE
20  
21   #load config
22 < config = SafeConfigParser()
22 > config = BetterConfigParser()
23   config.read('./config')
24  
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')
32   systematics=systematics.split(' ')
33  
34   #TreeVar Array
35 < MVA_Vars={}
36 < for systematic in systematics:
37 <    MVA_Vars[systematic]=config.get('treeVars',systematic)
38 <    MVA_Vars[systematic]=MVA_Vars[systematic].split(' ')
35 > #MVA_Vars={}
36 > #for systematic in systematics:
37 > #    MVA_Vars[systematic]=config.get('treeVars',systematic)
38 > #    MVA_Vars[systematic]=MVA_Vars[systematic].split(' ')
39  
40   ######################
41   #Evaluate multi: Must Have same treeVars!!!
# 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 92 | Line 89 | for MVA in MVAlist:
89  
90   #define variables and specatators
91   MVA_var_buffer = []
92 + MVA_var_buffer4 = []
93   for i in range(len( MVA_Vars['Nominal'])):
94      MVA_var_buffer.append(array( 'f', [ 0 ] ))
95      for reader in readers:
# Line 112 | Line 110 | Ainfo = pickle.load(infofile)
110   infofile.close()
111  
112   #eval
113 < for job in Ainfo[start:stop]:
114 <    #get trees:
115 <    input = TFile.Open(job.getpath(),'read')
116 <    outfile = TFile.Open(job.path+'/MVAout/'+job.prefix+job.identifier+'.root','recreate')
117 <    input.cd()
118 <    obj = ROOT.TObject
119 <    for key in ROOT.gDirectory.GetListOfKeys():
120 <        input.cd()
121 <        obj = key.ReadObj()
122 <        print obj.GetName()
123 <        if obj.GetName() == job.tree:
124 <            continue
125 <        outfile.cd()
126 <        print key.GetName()
127 <        obj.Write(key.GetName())
128 <    tree = input.Get(job.tree)
129 <    nEntries = tree.GetEntries()
130 <    outfile.cd()
131 <    newtree = tree.CloneTree(0)
132 <
133 <    #MCs:
134 <    if job.type != 'DATA':
135 <        MVA_formulas={}
136 <        for systematic in systematics:
137 <            #print '\t\t - ' + systematic
138 <            MVA_formulas[systematic]=[]
139 <            #create TTreeFormulas
140 <            for j in range(len( MVA_Vars['Nominal'])):
141 <                MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
142 <        outfile.cd()
143 <        #Setup Branches
144 <        MVAbranches=[]
145 <        for i in range(0,len(readers)):
146 <            MVAbranches.append(array('f',[0]*9))
147 <            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')
148 <        print '\n--> ' + job.name +':'
149 <        #progbar setup
150 <        if nEntries >= longe:
151 <            step=int(nEntries/longe)
152 <            long=longe
153 <        else:
154 <            long=nEntries
155 <            step = 1
156 <        bar=progbar(long)
157 <        #Fill event by event:
158 <        for entry in range(0,nEntries):
159 <            if entry % step == 0:
160 <                bar.move()
161 <            #load entry
162 <            tree.GetEntry(entry)
163 <            for systematic in systematics:
113 > for job in Ainfo:
114 >    if eval(job.active):
115 >        if job.name in namelist:
116 >            #get trees:
117 >            input = TFile.Open(job.getpath(),'read')
118 >            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():
122 >                input.cd()
123 >                obj = key.ReadObj()
124 >                #print obj.GetName()
125 >                if obj.GetName() == job.tree:
126 >                    continue
127 >                outfile.cd()
128 >                #print key.GetName()
129 >                obj.Write(key.GetName())
130 >            tree = input.Get(job.tree)
131 >            nEntries = tree.GetEntries()
132 >            outfile.cd()
133 >            newtree = tree.CloneTree(0)
134 >
135 >            #MCs:
136 >            if job.type != 'DATA':
137 >                MVA_formulas={}
138 >                MVA_formulas4={}
139 >                for systematic in systematics:
140 >                    #print '\t\t - ' + systematic
141 >                    MVA_formulas[systematic]=[]
142 >                    MVA_formulas4[systematic]=[]
143 >                    #create TTreeFormulas
144 >                    for j in range(len( MVA_Vars['Nominal'])):
145 >                        MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
146 >                        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
147 >                outfile.cd()
148 >                #Setup Branches
149 >                MVAbranches=[]
150 >                MVAbranches4=[]
151 >                for i in range(0,len(readers)):
152 >                    MVAbranches.append(array('f',[0]*9))
153 >                    MVAbranches4.append(array('f',[0]*9))
154 >                    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')
155 >                    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')
156 >                print '\n--> ' + job.name +':'
157 >                #progbar setup
158 >                if nEntries >= longe:
159 >                    step=int(nEntries/longe)
160 >                    long=longe
161 >                else:
162 >                    long=nEntries
163 >                    step = 1
164 >                bar=progbar(long)
165 >                #Fill event by event:
166 >                for entry in range(0,nEntries):
167 >                    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 >                            
178 >                        for j in range(len( MVA_Vars['Nominal'])):
179 >                            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_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 <            #Fill:
199 <            newtree.Fill()
200 <        newtree.AutoSave()
201 <        outfile.Close()
202 <        
203 <    #DATA:
204 <    if job.type == 'DATA':
205 <        #MVA Formulas
206 <        MVA_formulas_Nominal = []
207 <        #create TTreeFormulas
208 <        for j in range(len( MVA_Vars['Nominal'])):
209 <            MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree))
210 <        outfile.cd()
211 <        MVAbranches=[]
212 <        for i in range(0,len(readers)):
213 <            MVAbranches.append(array('f',[0]))
214 <            newtree.Branch(MVAinfos[i].MVAname,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:
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 <
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()
225  
226 + print '\n'
227  
228   #Update Info:
229   if doinfo:
230      for job in Ainfo:        
231          for MVAinfo in MVAinfos:
232              job.addcomment('Added MVA %s'%MVAinfo.MVAname)
233 <        job.addpath('/MVAout')
234 <    infofile = open(Apath+'/MVAout/samples.info','w')
233 >        job.addpath(MVAdir)
234 >    infofile = open(Apath+MVAdir+'/samples.info','w')
235      pickle.dump(Ainfo,infofile)
236      infofile.close()
237  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines