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.21 by peller, Wed Jan 16 16:22:46 2013 UTC vs.
Revision 1.22 by nmohr, Fri Jan 25 16:18:00 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
# Line 10 | Line 11 | import warnings
11   warnings.filterwarnings( action='ignore', category=RuntimeWarning, message='creating converter.*' )
12   from optparse import OptionParser
13   import pickle
13 from myutils import BetterConfigParser, progbar, mvainfo, printc, parse_info
14  
15  
16   #CONFIGURE
17   ROOT.gROOT.SetBatch(True)
18 < print 'hello'
18 > print('hello')
19   #load config
20   #os.mkdir(path+'/sys')
21   argv = sys.argv
# Line 34 | Line 34 | parser.add_option("-C", "--config", dest
34                        help="configuration file")
35   (opts, args) = parser.parse_args(argv)
36  
37 #from samplesclass import sample
38 #from mvainfos import mvainfo
39 #from progbar import progbar
40 #from printcolor import printc
41
42
37   if opts.config =="":
38          opts.config = "config"
39 +
40 + #Import after configure to get help message
41 + from myutils import BetterConfigParser, progbar, printc, mvainfo, ParseInfo
42 +
43   config = BetterConfigParser()
44   #config.read('./config7TeV_ZZ')
45   config.read(opts.config)
# Line 52 | Line 50 | Wdir=config.get('Directories','Wdir')
50   samplesinfo=config.get('Directories','samplesinfo')
51  
52   #systematics
55
56
53   INpath = config.get('Directories','MVAin')
54   OUTpath = config.get('Directories','MVAout')
55  
56 < info = parse_info(samplesinfo,INpath)
56 > info = ParseInfo(samplesinfo,INpath)
57  
62 #infofile = open(samplesinfo,'r')
63 #info = pickle.load(infofile)
64 #infofile.close()
58   arglist=opts.discr #RTight_blavla,bsbsb
59  
60   namelistIN=opts.names
# Line 71 | Line 64 | namelist=namelistIN.split(',')
64  
65   MVAlist=arglist.split(',')
66  
74
67   #CONFIG
68   #factory
69   factoryname=config.get('factory','factoryname')
# Line 92 | Line 84 | longe=40
84   #Workdir
85   workdir=ROOT.gDirectory.GetPath()
86  
95
96 #Apply samples
97 #infofile = open(samplesinfo,'r')
98 #Ainfo = pickle.load(infofile)
99 #infofile.close()
100
101
87   class MvaEvaluater:
88      def __init__(self, config, MVAinfo):
89          self.varset = MVAinfo.varset
# Line 143 | Line 128 | for mva in MVAinfos:
128  
129  
130   #eval
146 for job in info:
147    if eval(job.active):
148        if job.name in namelist:
149            #get trees:
150            print INpath+'/'+job.prefix+job.identifier+'.root'
151            input = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read')
152            print OUTpath+'/'+job.prefix+job.identifier+'.root'
153            outfile = ROOT.TFile.Open(OUTpath+'/'+job.prefix+job.identifier+'.root','recreate')
154            input.cd()
155            obj = ROOT.TObject
156            for key in ROOT.gDirectory.GetListOfKeys():
157                input.cd()
158                obj = key.ReadObj()
159                #print obj.GetName()
160                if obj.GetName() == job.tree:
161                    continue
162                outfile.cd()
163                #print key.GetName()
164                obj.Write(key.GetName())
165            tree = input.Get(job.tree)
166            nEntries = tree.GetEntries()
167            outfile.cd()
168            newtree = tree.CloneTree(0)
169            
131  
132 <            #Set branch adress for all vars
133 <            for i in range(0,len(theMVAs)):
134 <                theMVAs[i].setBranches(tree,job)
135 <            outfile.cd()
136 <            #Setup Branches
137 <            MVAbranches=[]
138 <            for i in range(0,len(theMVAs)):
139 <                if job.type == 'Data':
140 <                    MVAbranches.append(array('f',[0]))
141 <                    newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
142 <                else:
143 <                    MVAbranches.append(array('f',[0]*11))
144 <                    newtree.Branch(theMVAs[i].MVAname,MVAbranches[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down:beff1_up:beff1_down/F')
145 <                MVA_formulas_Nominal = []
146 <            print '\n--> ' + job.name +':'
147 <            #progbar setup
148 <            if nEntries >= longe:
149 <                step=int(nEntries/longe)
150 <                long=longe
151 <            else:
152 <                long=nEntries
153 <                step = 1
154 <            bar=progbar(long)
155 <            #Fill event by event:
156 <            for entry in range(0,nEntries):
157 <                if entry % step == 0:
158 <                    bar.move()
159 <                #load entry
160 <                tree.GetEntry(entry)
132 > samples = info.get_samples(namelist)
133 > for job in samples:
134 >    #get trees:
135 >    print(INpath+'/'+job.prefix+job.identifier+'.root')
136 >    input = ROOT.TFile.Open(INpath+'/'+job.prefix+job.identifier+'.root','read')
137 >    print(OUTpath+'/'+job.prefix+job.identifier+'.root')
138 >    outfile = ROOT.TFile.Open(OUTpath+'/'+job.prefix+job.identifier+'.root','recreate')
139 >    input.cd()
140 >    obj = ROOT.TObject
141 >    for key in ROOT.gDirectory.GetListOfKeys():
142 >        input.cd()
143 >        obj = key.ReadObj()
144 >        #print obj.GetName()
145 >        if obj.GetName() == job.tree:
146 >            continue
147 >        outfile.cd()
148 >        #print key.GetName()
149 >        obj.Write(key.GetName())
150 >    tree = input.Get(job.tree)
151 >    nEntries = tree.GetEntries()
152 >    outfile.cd()
153 >    newtree = tree.CloneTree(0)
154 >            
155 >    #Set branch adress for all vars
156 >    for i in range(0,len(theMVAs)):
157 >        theMVAs[i].setBranches(tree,job)
158 >    outfile.cd()
159 >    #Setup Branches
160 >    MVAbranches=[]
161 >    for i in range(0,len(theMVAs)):
162 >        if job.type == 'Data':
163 >            MVAbranches.append(array('f',[0]))
164 >            newtree.Branch(MVAinfos[i].MVAname,MVAbranches[i],'nominal/F')
165 >        else:
166 >            MVAbranches.append(array('f',[0]*11))
167 >            newtree.Branch(theMVAs[i].MVAname,MVAbranches[i],'nominal:JER_up:JER_down:JES_up:JES_down:beff_up:beff_down:bmis_up:bmis_down:beff1_up:beff1_down/F')
168 >        MVA_formulas_Nominal = []
169 >        print('\n--> ' + job.name +':')
170 >    #progbar setup
171 >    if nEntries >= longe:
172 >        step=long(nEntries/longe)
173 >        long=longe
174 >    else:
175 >        long=nEntries
176 >        step = 1
177 >    bar=progbar(long)
178 >    #Fill event by event:
179 >    for entry in range(0,nEntries):
180 >        if entry % step == 0:
181 >            bar.move()
182 >        #load entry
183 >        tree.GetEntry(entry)
184                              
185 <                for i in range(0,len(theMVAs)):
186 <                    theMVAs[i].evaluate(MVAbranches[i],job)
187 <                #Fill:
188 <                newtree.Fill()
189 <            newtree.AutoSave()
190 <            outfile.Close()
185 >        for i in range(0,len(theMVAs)):
186 >            theMVAs[i].evaluate(MVAbranches[i],job)
187 >        #Fill:
188 >        newtree.Fill()
189 >    newtree.AutoSave()
190 >    outfile.Close()
191                  
192 < print '\n'
209 <
210 < #Update Info:
211 < #if doinfo:
212 < #    for job in Ainfo:        
213 < #        for MVAinfo in MVAinfos:
214 < #            job.addcomment('Added MVA %s'%MVAinfo.MVAname)
215 < #        job.addpath(MVAdir)
216 < #    infofile = open(samplesinfo,'w')
217 < #    pickle.dump(Ainfo,infofile)
218 < #    infofile.close()
192 > print('\n')

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines