1 |
#!/usr/bin/env python
|
2 |
from optparse import OptionParser
|
3 |
import sys
|
4 |
import pickle
|
5 |
import ROOT
|
6 |
ROOT.gROOT.SetBatch(True)
|
7 |
from array import array
|
8 |
#warnings.filterwarnings( action='ignore', category=RuntimeWarning, message='creating converter.*' )
|
9 |
#usage: ./train run gui
|
10 |
|
11 |
#CONFIGURE
|
12 |
argv = sys.argv
|
13 |
parser = OptionParser()
|
14 |
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
|
15 |
help="Verbose mode.")
|
16 |
parser.add_option("-T", "--training", dest="training", default="",
|
17 |
help="Training")
|
18 |
parser.add_option("-C", "--config", dest="config", default=[], action="append",
|
19 |
help="configuration file")
|
20 |
(opts, args) = parser.parse_args(argv)
|
21 |
if opts.config =="":
|
22 |
opts.config = "config"
|
23 |
|
24 |
#Import after configure to get help message
|
25 |
from myutils import BetterConfigParser, mvainfo, ParseInfo, TreeCache
|
26 |
|
27 |
#load config
|
28 |
config = BetterConfigParser()
|
29 |
config.read(opts.config)
|
30 |
anaTag = config.get("Analysis","tag")
|
31 |
run=opts.training
|
32 |
gui=opts.verbose
|
33 |
|
34 |
#GLOABAL rescale from Train/Test Spliiting:
|
35 |
global_rescale=2.
|
36 |
|
37 |
#get locations:
|
38 |
MVAdir=config.get('Directories','vhbbpath')+'/data/'
|
39 |
samplesinfo=config.get('Directories','samplesinfo')
|
40 |
|
41 |
#systematics
|
42 |
systematics=config.get('systematics','systematics')
|
43 |
systematics=systematics.split(' ')
|
44 |
|
45 |
weightF=config.get('Weights','weightF')
|
46 |
|
47 |
VHbbNameSpace=config.get('VHbbNameSpace','library')
|
48 |
ROOT.gSystem.Load(VHbbNameSpace)
|
49 |
|
50 |
#CONFIG
|
51 |
#factory
|
52 |
factoryname=config.get('factory','factoryname')
|
53 |
factorysettings=config.get('factory','factorysettings')
|
54 |
#MVA
|
55 |
MVAtype=config.get(run,'MVAtype')
|
56 |
MVAname=run
|
57 |
MVAsettings=config.get(run,'MVAsettings')
|
58 |
fnameOutput = MVAdir+factoryname+'_'+MVAname+'.root'
|
59 |
#locations
|
60 |
path=config.get('Directories','SYSout')
|
61 |
|
62 |
TCutname=config.get(run, 'treeCut')
|
63 |
TCut=config.get('Cuts',TCutname)
|
64 |
#print TCut
|
65 |
|
66 |
#signals
|
67 |
signals=config.get(run,'signals')
|
68 |
signals=signals.split(' ')
|
69 |
#backgrounds
|
70 |
backgrounds=config.get(run,'backgrounds')
|
71 |
backgrounds=backgrounds.split(' ')
|
72 |
|
73 |
treeVarSet=config.get(run,'treeVarSet')
|
74 |
|
75 |
#variables
|
76 |
#TreeVar Array
|
77 |
MVA_Vars={}
|
78 |
MVA_Vars['Nominal']=config.get(treeVarSet,'Nominal')
|
79 |
MVA_Vars['Nominal']=MVA_Vars['Nominal'].split(' ')
|
80 |
|
81 |
#Infofile
|
82 |
info = ParseInfo(samplesinfo,path)
|
83 |
|
84 |
#Workdir
|
85 |
workdir=ROOT.gDirectory.GetPath()
|
86 |
|
87 |
|
88 |
TrainCut='%s & EventForTraining==1'%TCut
|
89 |
EvalCut='%s & EventForTraining==0'%TCut
|
90 |
cuts = [TrainCut,EvalCut]
|
91 |
|
92 |
|
93 |
samples = []
|
94 |
samples = info.get_samples(signals+backgrounds)
|
95 |
|
96 |
tc = TreeCache(cuts,samples,path)
|
97 |
|
98 |
output = ROOT.TFile.Open(fnameOutput, "RECREATE")
|
99 |
|
100 |
print '\n\t>>> READING EVENTS <<<\n'
|
101 |
|
102 |
signal_samples = info.get_samples(signals)
|
103 |
background_samples = info.get_samples(backgrounds)
|
104 |
|
105 |
#TRAIN trees
|
106 |
Tbackgrounds = []
|
107 |
TbScales = []
|
108 |
Tsignals = []
|
109 |
TsScales = []
|
110 |
#EVAL trees
|
111 |
Ebackgrounds = []
|
112 |
EbScales = []
|
113 |
Esignals = []
|
114 |
EsScales = []
|
115 |
|
116 |
#load trees
|
117 |
for job in signal_samples:
|
118 |
print '\tREADING IN %s AS SIG'%job.name
|
119 |
Tsignal = tc.get_tree(job,TrainCut)
|
120 |
ROOT.gDirectory.Cd(workdir)
|
121 |
TsScale = tc.get_scale(job,config)*global_rescale
|
122 |
Tsignals.append(Tsignal)
|
123 |
TsScales.append(TsScale)
|
124 |
Esignal = tc.get_tree(job,EvalCut)
|
125 |
Esignals.append(Esignal)
|
126 |
EsScales.append(TsScale)
|
127 |
print '\t\t\tTraining %s events'%Tsignal.GetEntries()
|
128 |
print '\t\t\tEval %s events'%Esignal.GetEntries()
|
129 |
for job in background_samples:
|
130 |
print '\tREADING IN %s AS BKG'%job.name
|
131 |
Tbackground = tc.get_tree(job,TrainCut)
|
132 |
ROOT.gDirectory.Cd(workdir)
|
133 |
TbScale = tc.get_scale(job,config)*global_rescale
|
134 |
Tbackgrounds.append(Tbackground)
|
135 |
TbScales.append(TbScale)
|
136 |
Ebackground = tc.get_tree(job,EvalCut)
|
137 |
ROOT.gDirectory.Cd(workdir)
|
138 |
Ebackgrounds.append(Ebackground)
|
139 |
EbScales.append(TbScale)
|
140 |
print '\t\t\tTraining %s events'%Tbackground.GetEntries()
|
141 |
print '\t\t\tEval %s events'%Ebackground.GetEntries()
|
142 |
|
143 |
|
144 |
factory = ROOT.TMVA.Factory(factoryname, output, factorysettings)
|
145 |
|
146 |
#set input trees
|
147 |
for i in range(len(Tsignals)):
|
148 |
factory.AddSignalTree(Tsignals[i], TsScales[i], ROOT.TMVA.Types.kTraining)
|
149 |
factory.AddSignalTree(Esignals[i], EsScales[i], ROOT.TMVA.Types.kTesting)
|
150 |
|
151 |
for i in range(len(Tbackgrounds)):
|
152 |
if (Tbackgrounds[i].GetEntries()>0):
|
153 |
factory.AddBackgroundTree(Tbackgrounds[i], TbScales[i], ROOT.TMVA.Types.kTraining)
|
154 |
|
155 |
if (Ebackgrounds[i].GetEntries()>0):
|
156 |
factory.AddBackgroundTree(Ebackgrounds[i], EbScales[i], ROOT.TMVA.Types.kTesting)
|
157 |
|
158 |
for var in MVA_Vars['Nominal']:
|
159 |
factory.AddVariable(var,'D') # add the variables
|
160 |
|
161 |
#Execute TMVA
|
162 |
factory.SetSignalWeightExpression(weightF)
|
163 |
factory.SetBackgroundWeightExpression(weightF)
|
164 |
factory.Verbose()
|
165 |
factory.BookMethod(MVAtype,MVAname,MVAsettings)
|
166 |
factory.TrainAllMethods()
|
167 |
factory.TestAllMethods()
|
168 |
factory.EvaluateAllMethods()
|
169 |
output.Write()
|
170 |
|
171 |
#WRITE INFOFILE
|
172 |
infofile = open(MVAdir+factoryname+'_'+MVAname+'.info','w')
|
173 |
info=mvainfo(MVAname)
|
174 |
info.factoryname=factoryname
|
175 |
info.factorysettings=factorysettings
|
176 |
info.MVAtype=MVAtype
|
177 |
info.MVAsettings=MVAsettings
|
178 |
info.weightfilepath=MVAdir
|
179 |
info.path=path
|
180 |
info.varset=treeVarSet
|
181 |
info.vars=MVA_Vars['Nominal']
|
182 |
pickle.dump(info,infofile)
|
183 |
infofile.close()
|
184 |
|
185 |
# open the TMVA Gui
|
186 |
if gui == True:
|
187 |
ROOT.gROOT.ProcessLine( ".L myutils/TMVAGui.C")
|
188 |
ROOT.gROOT.ProcessLine( "TMVAGui(\"%s\")" % fnameOutput )
|
189 |
ROOT.gApplication.Run()
|
190 |
|
191 |
|