forked from rgerosa/boostedWWAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 5
/
trainingClass.py
409 lines (295 loc) · 17 KB
/
trainingClass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# A class which takes histograms and plots them in a versatile way
# inputs are file names which can be "data" or "MC"
import ROOT
#ROOT.gROOT.ProcessLine(".L ~/tdrstyle.C")
#from ROOT import setTDRStyle
#from ROOT import TTree
#ROOT.setTDRStyle()
#ROOT.gStyle.SetPalette(1)
#ROOT.gStyle.SetOptFit(0)
#
#ROOT.TH1.SetDefaultSumw2()
#ROOT.TH2.SetDefaultSumw2()
#
#ROOT.gStyle.SetPadTopMargin(0.09);
#ROOT.gStyle.SetPadLeftMargin(0.16);
# TMVA::Tools::Instance();
ROOT.TMVA.Tools.Instance();
import os
from array import array
import math
from optparse import OptionParser
def getfractionbelowcut( cut, list ):
ctr = 0;
for i in range(len(list)):
if list[i] < cut: ctr = ctr + 1;
return (float(ctr)/float(len(list)));
def ComputeROCFromList(ls,lb,LtoR):
lsmax = max(ls);
lbmax = max(lb);
lsmin = min(ls);
lbmin = min(lb);
allmax = max(lsmax,lbmax);
allmin = max(lsmin,lbmin);
xval = array('d', [])
yval = array('d', [])
nsteps = 1000;
stepsize = (allmax - allmin)/nsteps;
for i in range(nsteps+1):
curCutVal = stepsize*float(i) + allmin;
effsig = getfractionbelowcut( curCutVal, ls );
effbkg = getfractionbelowcut( curCutVal, lb );
if LtoR:
xval.append( 1-effsig );
yval.append( effbkg );
else:
xval.append( effsig );
yval.append( 1-effbkg );
tg = ROOT.TGraph( nsteps+1, xval, yval );
return tg;
def ComputeROC(hsig,hbkg,LtoR):
nbins = hsig.GetNbinsX();
binsize = hsig.GetBinWidth(1);
lowedge = hsig.GetBinLowEdge(1);
print "lowedge: ",lowedge
hsigIntegral = hsig.Integral();
hbkgIntegral = hbkg.Integral();
xval = array('d', [])
yval = array('d', [])
ctr = 0;
for i in range(1,nbins+1):
effBkg = 0;
effSig = 0;
if LtoR: effBkg = hbkg.Integral( i, nbins )/hbkgIntegral;
else: effBkg = hbkg.Integral( 1, i )/hbkgIntegral;
if LtoR: effSig = hsig.Integral( i, nbins )/hsigIntegral;
else: effSig = hsig.Integral( 1, i )/hsigIntegral;
print "cut: ",(lowedge+(i-1)*binsize),"effBkg: ", effBkg, ", effSig: ", effSig;
xval.append( effSig );
yval.append( 1-effBkg );
ctr = ctr + 1;
print nbins, "and ", ctr
tg = ROOT.TGraph( nbins, xval, yval );
return tg;
class trainingClass:
### ------------------------------------------------
def __init__(self, signalFile, backgroundFile, listOfTrainingVariables, label):
print "Welcome to the training..."
self.SigFile_ = ROOT.TFile(signalFile);
self.BkgFile_ = ROOT.TFile(backgroundFile);
self.SigTree_ = self.SigFile_.Get("otree");
self.BkgTree_ = self.BkgFile_.Get("otree");
self.ListOfTrainingVariables = listOfTrainingVariables;
self.Label_ = label;
############################
############################
############################
def doTraining( self, pTlo, pThi ):
print "pT range: ", pTlo, " - ", pThi;
self.OutputFileName_ = "classifier/Wtagger_"+str(pTlo)+"to"+str(pThi)+"_"+self.Label_+".root";
outputFile = ROOT.TFile( self.OutputFileName_, 'RECREATE' )
factory = ROOT.TMVA.Factory( "Wtagger_"+str(pTlo)+"to"+str(pThi)+"_"+self.Label_, outputFile, "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" )
#training variables
for i in range(len(self.ListOfTrainingVariables)):
varString = self.ListOfTrainingVariables[i]+" := "+self.ListOfTrainingVariables[i];
print varString
factory.AddVariable( varString, 'F' );
#spectators
factory.AddSpectator( "jet_pt_pr", 'F' )
factory.AddSpectator( "jet_mass_pr", 'F' )
# Global event weights (see below for setting event-wise weights)
signalWeight = 1.0
backgroundWeight = 1.0
factory.AddSignalTree ( self.SigTree_, signalWeight )
factory.AddBackgroundTree( self.BkgTree_, backgroundWeight )
# what's this for?
#factory.SetBackgroundWeightExpression( "weight" )
# cuts definition
mycutSig = ROOT.TCut( "jet_pt_pr < "+str(pThi)+" && jet_pt_pr > "+str(pTlo)+" && jet_mass_pr > 60 && jet_mass_pr < 100" );
mycutBkg = ROOT.TCut( "jet_pt_pr < "+str(pThi)+" && jet_pt_pr > "+str(pTlo)+" && jet_mass_pr > 60 && jet_mass_pr < 100" );
# Here, the relevant variables are copied over in new, slim trees that are
# used for TMVA training and testing
# "SplitMode=Random" means that the input events are randomly shuffled before
# splitting them into training and test samples
print "PrepareTrainingAndTestTree ... "
factory.PrepareTrainingAndTestTree( mycutSig, mycutBkg,"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" )
print "BookMethod ... "
# factory.BookMethod( ROOT.TMVA.Types.kBDT, "BDT","!H:!V:NTrees=850:nEventsMin=150:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" )
factory.BookMethod( ROOT.TMVA.Types.kBDT, "BDT","!H:!V:NTrees=1000:nEventsMin=40:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20:PruneMethod=CostComplexity:PruneStrength=4" )
# Train MVAs
print "TrainAllMethods ... "
factory.TrainAllMethods()
# Test MVAs
print "TestAllMethods ... "
factory.TestAllMethods()
# Evaluate MVAs
print "EvaluateAllMethods ... "
factory.EvaluateAllMethods()
# Save the output.
outputFile.Close()
############################
############################
############################
def plotTrainingResults( self, pTlo, pThi ):
rootname = "Wtagger_"+str(pTlo)+"to"+str(pThi)+"_"+self.Label_;
if not os.path.isdir("plots"): os.system("mkdir plots");
if not os.path.isdir("plots/classifier"): os.system("mkdir plots/classifier");
os.system("bash TMVAscripts/runAllScripts.sh classifier/"+rootname+".root");
if not os.path.isdir("classifier/trainingPlots"): os.system("mkdir classifier/trainingPlots");
if not os.path.isdir("classifier/trainingPlots/"+rootname): os.system("mkdir classifier/trainingPlots/"+rootname);
os.system("mv plots/classifier/"+rootname+".root* classifier/trainingPlots/"+rootname+"/.");
def getWeightFile( self, pTlo, pThi ):
self.WeightFile_ = "weights/"+"Wtagger_"+str(pTlo)+"to"+str(pThi)+"_"+self.Label_+"_BDT.weights.xml";
return self.WeightFile_
############################
############################
############################
def makeFinalPlots( self, pTlo, pThi, bdtcut ):
print self.ListOfTrainingVariables;
print self.SigTree_.GetEntries();
print self.BkgTree_.GetEntries();
# h_sig = [];
# h_bkg = [];
nameOfWeightFile = self.getWeightFile( pTlo, pThi );
# here we have a flag to either use TMVA file or original files
# make a list of arrays
reader = ROOT.TMVA.Reader("!Color:!Silent")
listOfVarArray = [];
for i in range(len(self.ListOfTrainingVariables)):
# curVar = array('f',[0.]);
listOfVarArray.append( array('f',[0.]) );
varString = self.ListOfTrainingVariables[i]+" := "+self.ListOfTrainingVariables[i];
reader.AddVariable( varString, listOfVarArray[i] );
#spectators
spec1 = array('f',[0.]);
spec2 = array('f',[0.]);
reader.AddSpectator( "jet_pt_pr", spec1 )
reader.AddSpectator( "jet_mass_pr", spec2 )
reader.BookMVA("BDT",nameOfWeightFile);
mdVal_sig = []; mdVal_bkg = [];
discrVal_sig = []; discrVal_bkg = [];
h_mass_pr_sig = ROOT.TH1F("h_mass_pr_sig","; pruned mass;", 50, 0, 150 );
h_mass_pr_bkg = ROOT.TH1F("h_mass_pr_bkg","; pruned mass;", 50, 0, 150 );
## ---------------
for i in range(self.SigTree_.GetEntries()):
self.SigTree_.GetEntry(i);
for j in range(len(self.ListOfTrainingVariables)):
listOfVarArray[j][0] = getattr( self.SigTree_,self.ListOfTrainingVariables[j] );
bdtv = reader.EvaluateMVA("BDT");
if bdtv > bdtcut: h_mass_pr_sig.Fill( getattr( self.SigTree_, "jet_mass_pr" ) );
# print "mass = ", getattr( self.SigTree_, "jet_mass_pr" ), " and discriminant = ", bdtv
if getattr(self.SigTree_,"jet_pt_pr") > pTlo and getattr(self.SigTree_,"jet_pt_pr") < pThi and getattr(self.SigTree_,"jet_mass_pr") > 60. and getattr(self.SigTree_,"jet_mass_pr") < 100:
mdVal_sig.append( getattr( self.SigTree_, "jet_massdrop_pr") );
discrVal_sig.append( bdtv );
## ---------------
for i in range(self.BkgTree_.GetEntries()):
self.BkgTree_.GetEntry(i);
for j in range(len(self.ListOfTrainingVariables)):
listOfVarArray[j][0] = getattr( self.BkgTree_,self.ListOfTrainingVariables[j] );
bdtv = reader.EvaluateMVA("BDT");
if bdtv > bdtcut: h_mass_pr_bkg.Fill( getattr( self.BkgTree_, "jet_mass_pr" ) );
if getattr(self.BkgTree_,"jet_pt_pr") > pTlo and getattr(self.BkgTree_,"jet_pt_pr") < pThi and getattr(self.BkgTree_,"jet_mass_pr") > 60. and getattr(self.BkgTree_,"jet_mass_pr") < 100:
mdVal_bkg.append( getattr( self.BkgTree_, "jet_massdrop_pr") );
discrVal_bkg.append( reader.EvaluateMVA("BDT") );
## ---------------
hmd_sig = ROOT.TH1F("hmd_sig","hmd_sig",100,min(mdVal_sig),max(mdVal_sig));
for i in range(len(mdVal_sig)): hmd_sig.Fill( mdVal_sig[i] );
hmd_bkg = ROOT.TH1F("hmd_bkg","hmd_bkg",100,min(mdVal_bkg),max(mdVal_bkg));
for i in range(len(mdVal_bkg)): hmd_bkg.Fill( mdVal_bkg[i] );
hdiscr_sig = ROOT.TH1F("hdiscr_sig","hdiscr_sig",100,min(discrVal_sig),max(discrVal_sig));
for i in range(len(discrVal_sig)): hdiscr_sig.Fill( discrVal_sig[i] );
hdiscr_bkg = ROOT.TH1F("hdiscr_bkg","hdiscr_bkg",100,min(discrVal_bkg),max(discrVal_bkg));
for i in range(len(discrVal_bkg)): hdiscr_bkg.Fill( discrVal_bkg[i] );
can1ex = ROOT.TCanvas("can1ex","can1ex",800,800);
can1ex.cd();
hmd_sig.Draw("hist");
hmd_bkg.SetLineColor(2);
hmd_bkg.Draw("histsames");
can1ex.SaveAs("finalPlot/extestmd_"+self.Label_+".eps");
can1ex.SaveAs("finalPlot/extestmd_"+self.Label_+".png");
can2ex = ROOT.TCanvas("can2ex","can2ex",800,800);
can2ex.cd();
hdiscr_sig.Draw("hist");
hdiscr_bkg.SetLineColor(2);
hdiscr_bkg.Draw("histsames");
can2ex.SaveAs("finalPlot/extestdiscr_"+self.Label_+".eps");
can2ex.SaveAs("finalPlot/extestdiscr_"+self.Label_+".png");
tgs = [];
tgs.append( ComputeROCFromList(discrVal_sig,discrVal_bkg, True) );
tgs.append( ComputeROCFromList(mdVal_sig,mdVal_bkg, False) );
tgs.append( h_mass_pr_sig );
tgs.append( h_mass_pr_bkg );
return tgs;
############################
############################
############################
def makeFinalPlotsInternal(self, pTlo, pThi):
self.OutputFileName_ = "classifier/Wtagger_"+str(pTlo)+"to"+str(pThi)+"_"+self.Label_+".root";
internalTreeName = ROOT.TFile(self.OutputFileName_);
internalTree = internalTreeName.Get("TestTree");
internalTrainTree = internalTreeName.Get("TrainTree");
mdVal_sig = []; mdVal_bkg = [];
discrVal_sig = []; discrVal_bkg = [];
discrVal_sig_train = []; discrVal_bkg_train = [];
## ---------------
for i in range(internalTree.GetEntries()):
internalTree.GetEntry(i);
if getattr(internalTree,"jet_pt_pr") > pTlo and getattr(internalTree,"jet_pt_pr") < pThi and getattr(internalTree,"jet_mass_pr") > 60. and getattr(internalTree,"jet_mass_pr") < 100 and internalTree.classID == 0:
mdVal_sig.append( getattr( internalTree, "jet_massdrop_pr") );
discrVal_sig.append( getattr( internalTree, "BDT") );
## ---------------
for i in range(internalTree.GetEntries()):
internalTree.GetEntry(i);
if getattr(internalTree,"jet_pt_pr") > pTlo and getattr(internalTree,"jet_pt_pr") < pThi and getattr(internalTree,"jet_mass_pr") > 60. and getattr(internalTree,"jet_mass_pr") < 100 and internalTree.classID == 1:
mdVal_bkg.append( getattr( internalTree, "jet_massdrop_pr") );
discrVal_bkg.append( getattr( internalTree, "BDT") );
## ---------------
for i in range(internalTrainTree.GetEntries()):
internalTrainTree.GetEntry(i);
if getattr(internalTrainTree,"jet_pt_pr") > pTlo and getattr(internalTrainTree,"jet_pt_pr") < pThi and getattr(internalTrainTree,"jet_mass_pr") > 60. and getattr(internalTrainTree,"jet_mass_pr") < 100 and internalTrainTree.classID == 0:
mdVal_sig.append( getattr( internalTrainTree, "jet_massdrop_pr") );
discrVal_sig_train.append( getattr( internalTrainTree, "BDT") );
## ---------------
for i in range(internalTrainTree.GetEntries()):
internalTrainTree.GetEntry(i);
if getattr(internalTrainTree,"jet_pt_pr") > pTlo and getattr(internalTrainTree,"jet_pt_pr") < pThi and getattr(internalTrainTree,"jet_mass_pr") > 60. and getattr(internalTrainTree,"jet_mass_pr") < 100 and internalTrainTree.classID == 1:
mdVal_bkg.append( getattr( internalTrainTree, "jet_massdrop_pr") );
discrVal_bkg_train.append( getattr( internalTrainTree, "BDT") );
hmd_sig = ROOT.TH1F("hmd_sig",";mass drop ("+self.Label_+");",30,min(mdVal_sig),max(mdVal_sig));
for i in range(len(mdVal_sig)): hmd_sig.Fill( mdVal_sig[i] );
hmd_bkg = ROOT.TH1F("hmd_bkg",";mass drop ("+self.Label_+");",30,min(mdVal_bkg),max(mdVal_bkg));
for i in range(len(mdVal_bkg)): hmd_bkg.Fill( mdVal_bkg[i] );
hdiscr_sig = ROOT.TH1F("hdiscr_sig",";BDT discr ("+self.Label_+");",30,min(discrVal_sig),max(discrVal_sig));
for i in range(len(discrVal_sig)): hdiscr_sig.Fill( discrVal_sig[i] );
hdiscr_bkg = ROOT.TH1F("hdiscr_bkg",";BDT discr ("+self.Label_+");",30,min(discrVal_bkg),max(discrVal_bkg));
for i in range(len(discrVal_bkg)): hdiscr_bkg.Fill( discrVal_bkg[i] );
hdiscr_sig_train = ROOT.TH1F("hdiscr_sig_train",";BDT discr ("+self.Label_+");",30,min(discrVal_sig_train),max(discrVal_sig_train));
for i in range(len(discrVal_sig_train)): hdiscr_sig_train.Fill( discrVal_sig_train[i] );
hdiscr_bkg_train = ROOT.TH1F("hdiscr_bkg_train",";BDT discr ("+self.Label_+");",30,min(discrVal_bkg_train),max(discrVal_bkg_train));
for i in range(len(discrVal_bkg_train)): hdiscr_bkg_train.Fill( discrVal_bkg_train[i] );
can1 = ROOT.TCanvas("can1"+self.Label_,"can1"+self.Label_,800,800);
can1.cd();
hmd_sig.Draw("hist");
hmd_bkg.SetLineColor(2);
hmd_bkg.Draw("histsames");
can1.SaveAs("finalPlot/testmd_"+self.Label_+".eps");
can1.SaveAs("finalPlot/testmd_"+self.Label_+".png");
can2 = ROOT.TCanvas("can2"+self.Label_,"can2"+self.Label_,800,800);
can2.cd();
hdiscr_sig.Draw("hist");
hdiscr_bkg.SetLineColor(2);
hdiscr_bkg.Draw("histsames");
hdiscr_sig_train.SetLineStyle(2);
hdiscr_sig_train.Draw("histsames");
hdiscr_bkg_train.SetLineColor(2);
hdiscr_bkg_train.SetLineStyle(2);
hdiscr_bkg_train.Draw("histsames");
can2.SaveAs("finalPlot/testdiscr_"+self.Label_+".eps");
can2.SaveAs("finalPlot/testdiscr_"+self.Label_+".png");
tgs = [];
# tgs.append( ComputeROC(hdiscr_sig,hdiscr_bkg, True) );
# tgs.append( ComputeROC(hmd_sig,hmd_bkg, False) );
tgs.append( ComputeROCFromList(discrVal_sig,discrVal_bkg, True) );
tgs.append( ComputeROCFromList(mdVal_sig,mdVal_bkg, False) );
tgs.append( ComputeROCFromList(discrVal_sig_train,discrVal_bkg_train, True) );
return tgs;