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.23 by nmohr, Fri Feb 1 10:47:38 2013 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2 + from __future__ import print_function
3   import sys
4   import os
5   import ROOT
5 from ROOT import TFile
6   from array import array
7   from math import sqrt
8   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
14 < from mvainfos import mvainfo
12 > from optparse import OptionParser
13   import pickle
16 from progbar import progbar
17 from printcolor import printc
14  
15   #CONFIGURE
16 <
16 > ROOT.gROOT.SetBatch(True)
17 > print('hello')
18   #load config
19 < config = SafeConfigParser()
20 < config.read('./config')
19 > argv = sys.argv
20 > parser = OptionParser()
21 > parser.add_option("-U", "--update", dest="update", default=0,
22 >                      help="update infofile")
23 > parser.add_option("-D", "--discr", dest="discr", default="",
24 >                      help="discriminators to be added")
25 > parser.add_option("-S", "--samples", dest="names", default="",
26 >                      help="samples you want to run on")
27 > parser.add_option("-C", "--config", dest="config", default=[], action="append",
28 >                      help="configuration file")
29 > (opts, args) = parser.parse_args(argv)
30 >
31 > if opts.config =="":
32 >        opts.config = "config"
33 >
34 > #Import after configure to get help message
35 > from myutils import BetterConfigParser, progbar, printc, ParseInfo, MvaEvaluator
36 >
37 > config = BetterConfigParser()
38 > config.read(opts.config)
39 > anaTag = config.get("Analysis","tag")
40  
41   #get locations:
42   Wdir=config.get('Directories','Wdir')
43 <
43 > samplesinfo=config.get('Directories','samplesinfo')
44  
45   #systematics
46 < systematics=config.get('systematics','systematics')
47 < systematics=systematics.split(' ')
46 > INpath = config.get('Directories','MVAin')
47 > OUTpath = config.get('Directories','MVAout')
48 >
49 > info = ParseInfo(samplesinfo,INpath)
50  
51 < #TreeVar Array
52 < MVA_Vars={}
53 < for systematic in systematics:
54 <    MVA_Vars[systematic]=config.get('treeVars',systematic)
55 <    MVA_Vars[systematic]=MVA_Vars[systematic].split(' ')
56 <
39 < ######################
40 < #Evaluate multi: Must Have same treeVars!!!
41 <
42 < Apath=sys.argv[1]
43 < arglist=sys.argv[2] #RTight_blavla,bsbsb
44 <
45 < #for axample
46 < #0 5 0
47 < #and
48 < #5 -1 1
49 <
50 < start=int(sys.argv[3])
51 < stop=int(sys.argv[4])
52 < doinfo=bool(int(sys.argv[5]))
51 > arglist=opts.discr #RTight_blavla,bsbsb
52 >
53 > namelistIN=opts.names
54 > namelist=namelistIN.split(',')
55 >
56 > #doinfo=bool(int(opts.update))
57  
58   MVAlist=arglist.split(',')
59  
60   #CONFIG
61   #factory
62   factoryname=config.get('factory','factoryname')
63 +
64 + #load the namespace
65 + VHbbNameSpace=config.get('VHbbNameSpace','library')
66 + ROOT.gSystem.Load(VHbbNameSpace)
67 +
68   #MVA
60 #MVAnames=[]
61 #for MVA in MVAlist:
62 #    print MVA
63 #    MVAnames.append(config.get(MVA,'MVAname'))
64 #print Wdir+'/weights/'+factoryname+'_'+MVAname+'.info'
65 #MVAinfofiles=[]
69   MVAinfos=[]
70 + MVAdir=config.get('Directories','vhbbpath')
71   for MVAname in MVAlist:
72 <    MVAinfofile = open(Wdir+'/weights/'+factoryname+'_'+MVAname+'.info','r')
72 >    MVAinfofile = open(MVAdir+'/data/'+factoryname+'_'+MVAname+'.info','r')
73      MVAinfos.append(pickle.load(MVAinfofile))
74      MVAinfofile.close()
75      
72 treeVarSet=MVAinfos[0].varset
73 #variables
74 #TreeVar Array
75 MVA_Vars={}
76 for systematic in systematics:
77    MVA_Vars[systematic]=config.get(treeVarSet,systematic)
78    MVA_Vars[systematic]=MVA_Vars[systematic].split(' ')
79 #Spectators:
80 #spectators=config.get(treeVarSet,'spectators')
81 #spectators=spectators.split(' ')
82 #progbar quatsch
76   longe=40
77   #Workdir
78   workdir=ROOT.gDirectory.GetPath()
86 #os.mkdir(Apath+'/MVAout')
79  
80 < #Book TMVA readers: MVAlist=["MMCC_bla","CC5050_bla"]
81 < readers=[]
82 < for MVA in MVAlist:
83 <    readers.append(ROOT.TMVA.Reader("!Color:!Silent"))
84 <
85 < #define variables and specatators
94 < MVA_var_buffer = []
95 < for i in range(len( MVA_Vars['Nominal'])):
96 <    MVA_var_buffer.append(array( 'f', [ 0 ] ))
97 <    for reader in readers:
98 <        reader.AddVariable( MVA_Vars['Nominal'][i],MVA_var_buffer[i])
99 < #MVA_spectator_buffer = []
100 < #for i in range(len(spectators)):
101 < #    MVA_spectator_buffer.append(array( 'f', [ 0 ] ))
102 < #    for reader in readers:
103 < #        reader.AddSpectator(spectators[i],MVA_spectator_buffer[i])
104 < #Load raeder
105 < for i in range(0,len(readers)):
106 <    readers[i].BookMVA(MVAinfos[i].MVAname,MVAinfos[i].getweightfile())
107 < #--> Now the MVA is booked
108 <
109 < #Apply samples
110 < infofile = open(Apath+'/samples.info','r')
111 < Ainfo = pickle.load(infofile)
112 < infofile.close()
80 >
81 >
82 > theMVAs = []
83 > for mva in MVAinfos:
84 >    theMVAs.append(MvaEvaluator(config,mva))
85 >
86  
87   #eval
88 < for job in Ainfo[start:stop]:
88 >
89 > samples = info.get_samples(namelist)
90 > for job in samples:
91      #get trees:
92 <    input = TFile.Open(job.getpath(),'read')
93 <    outfile = TFile.Open(job.path+'/MVAout/'+job.prefix+job.identifier+'.root','recreate')
92 >    print(INpath+'/'+job.prefix+job.identifier+'.root')
93 >    input = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read')
94 >    print(OUTpath+'/'+job.prefix+job.identifier+'.root')
95 >    outfile = ROOT.TFile.Open(OUTpath+'/'+job.prefix+job.identifier+'.root','recreate')
96      input.cd()
97      obj = ROOT.TObject
98      for key in ROOT.gDirectory.GetListOfKeys():
99          input.cd()
100          obj = key.ReadObj()
101 <        print obj.GetName()
101 >        #print obj.GetName()
102          if obj.GetName() == job.tree:
103              continue
104          outfile.cd()
105 <        print key.GetName()
105 >        #print key.GetName()
106          obj.Write(key.GetName())
107      tree = input.Get(job.tree)
108      nEntries = tree.GetEntries()
109      outfile.cd()
110      newtree = tree.CloneTree(0)
111 <
112 <    #MCs:
113 <    if job.type != 'DATA':
114 <        MVA_formulas={}
115 <        for systematic in systematics:
116 <            #print '\t\t - ' + systematic
117 <            MVA_formulas[systematic]=[]
118 <            #create TTreeFormulas
119 <            for j in range(len( MVA_Vars['Nominal'])):
120 <                MVA_formulas[systematic].append(ROOT.TTreeFormula("MVA_formula%s_%s"%(j,systematic),MVA_Vars[systematic][j],tree))
121 <        outfile.cd()
145 <        #Setup Branches
146 <        MVAbranches=[]
147 <        for i in range(0,len(readers)):
148 <            MVAbranches.append(array('f',[0]*9))
149 <            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')
150 <        print '\n--> ' + job.name +':'
151 <        #progbar setup
152 <        if nEntries >= longe:
153 <            step=int(nEntries/longe)
154 <            long=longe
111 >            
112 >    #Set branch adress for all vars
113 >    for i in range(0,len(theMVAs)):
114 >        theMVAs[i].setVariables(tree,job)
115 >    outfile.cd()
116 >    #Setup Branches
117 >    mvaVals=[]
118 >    for i in range(0,len(theMVAs)):
119 >        if job.type == 'Data':
120 >            mvaVals.append(array('f',[0]))
121 >            newtree.Branch(MVAinfos[i].MVAname,mvaVals[i],'nominal/F')
122          else:
123 <            long=nEntries
124 <            step = 1
158 <        bar=progbar(long)
159 <        #Fill event by event:
160 <        for entry in range(0,nEntries):
161 <            if entry % step == 0:
162 <                bar.move()
163 <            #load entry
164 <            tree.GetEntry(entry)
165 <            for systematic in systematics:
166 <                for j in range(len( MVA_Vars['Nominal'])):
167 <                    MVA_var_buffer[j][0] = MVA_formulas[systematic][j].EvalInstance()
168 <                    
169 <                for j in range(0,len(readers)):
170 <                    MVAbranches[j][systematics.index(systematic)] = readers[j].EvaluateMVA(MVAinfos[j].MVAname)
171 <            #Fill:
172 <            newtree.Fill()
173 <        newtree.AutoSave()
174 <        outfile.Close()
175 <        
176 <    #DATA:
177 <    if job.type == 'DATA':
178 <        #MVA Formulas
123 >            mvaVals.append(array('f',[0]*11))
124 >            newtree.Branch(theMVAs[i].MVAname,mvaVals[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down:beff1_up:beff1_down/F')
125          MVA_formulas_Nominal = []
126 <        #create TTreeFormulas
127 <        for j in range(len( MVA_Vars['Nominal'])):
128 <            MVA_formulas_Nominal.append(ROOT.TTreeFormula("MVA_formula%s_Nominal"%j, MVA_Vars['Nominal'][j],tree))
129 <        outfile.cd()
130 <        MVAbranches=[]
131 <        for i in range(0,len(readers)):
132 <            MVAbranches.append(array('f',[0]))
133 <            newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
134 <        #progbar          
135 <        print '\n--> ' + job.name +':'
136 <        if nEntries >= longe:
137 <            step=int(nEntries/longe)
138 <            long=longe
139 <        else:
140 <            long=nEntries
141 <            step = 1
142 <        bar=progbar(long)
143 <        #Fill event by event:
144 <        for entry in range(0,nEntries):
145 <            if entry % step == 0:
146 <                bar.move()
147 <            #load entry
148 <            tree.GetEntry(entry)
149 <            #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 <
215 <
216 <
217 < #Update Info:
218 < if doinfo:
219 <    for job in Ainfo:        
220 <        for MVAinfo in MVAinfos:
221 <            job.addcomment('Added MVA %s'%MVAinfo.MVAname)
222 <        job.addpath('/MVAout')
223 <    infofile = open(Apath+'/MVAout/samples.info','w')
224 <    pickle.dump(Ainfo,infofile)
225 <    infofile.close()
226 <
227 <
126 >        print('\n--> ' + job.name +':')
127 >    #progbar setup
128 >    if nEntries >= longe:
129 >        step=long(nEntries/longe)
130 >        long=longe
131 >    else:
132 >        long=nEntries
133 >        step = 1
134 >    bar=progbar(long)
135 >    #Fill event by event:
136 >    for entry in range(0,nEntries):
137 >        if entry % step == 0:
138 >            bar.move()
139 >        #load entry
140 >        tree.GetEntry(entry)
141 >                            
142 >        for i in range(0,len(theMVAs)):
143 >            theMVAs[i].evaluate(mvaVals[i],job)
144 >        #Fill:
145 >        newtree.Fill()
146 >    newtree.AutoSave()
147 >    outfile.Close()
148 >                
149 > print('\n')

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines