33 void TMVAClassificationApplication( TString myMethodList =
"" )
41 std::map<std::string,int> Use;
51 Use[
"Likelihood"] = 1;
52 Use[
"LikelihoodD"] = 0;
53 Use[
"LikelihoodPCA"] = 1;
54 Use[
"LikelihoodKDE"] = 0;
55 Use[
"LikelihoodMIX"] = 0;
62 Use[
"PDEFoamBoost"] = 0;
69 Use[
"BoostedFisher"] = 0;
103 Use[
"SVM_Gauss"] = 0;
107 std::cout << std::endl;
108 std::cout <<
"==> Start TMVAClassificationApplication" << std::endl;
111 if (myMethodList !=
"") {
112 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
115 for (
UInt_t i=0; i<mlist.size(); i++) {
116 std::string regMethod(mlist[i]);
118 if (Use.find(regMethod) == Use.end()) {
119 std::cout <<
"Method \"" << regMethod
120 <<
"\" not known in TMVA under this name. Choose among the following:" << std::endl;
121 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
122 std::cout << it->first <<
" ";
124 std::cout << std::endl;
141 reader->
AddVariable(
"myvar1 := var1+var2", &var1 );
142 reader->
AddVariable(
"myvar2 := var1-var2", &var2 );
151 Float_t Category_cat1, Category_cat2, Category_cat3;
152 if (Use[
"Category"]){
154 reader->
AddSpectator(
"Category_cat1 := var3<=0", &Category_cat1 );
155 reader->
AddSpectator(
"Category_cat2 := (var3>0)&&(var4<0)", &Category_cat2 );
156 reader->
AddSpectator(
"Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
161 TString dir =
"dataset/weights/";
162 TString prefix =
"TMVAClassification";
165 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
167 TString methodName = TString(it->first) + TString(
" method");
168 TString weightfile = dir + prefix + TString(
"_") + TString(it->first) + TString(
".weights.xml");
169 reader->
BookMVA( methodName, weightfile );
175 TH1F *histLk(0), *histLkD(0), *histLkPCA(0), *histLkKDE(0), *histLkMIX(0), *histPD(0), *histPDD(0);
176 TH1F *histPDPCA(0), *histPDEFoam(0), *histPDEFoamErr(0), *histPDEFoamSig(0), *histKNN(0), *histHm(0);
177 TH1F *histFi(0), *histFiG(0), *histFiB(0), *histLD(0), *histNn(0),*histNnbfgs(0),*histNnbnn(0);
178 TH1F *histNnC(0), *histNnT(0), *histNdn(0), *histBdt(0), *histBdtG(0), *histBdtB(0), *histBdtD(0);
179 TH1F *histBdtF(0), *histRf(0), *histSVMG(0), *histSVMP(0), *histSVML(0), *histFDAMT(0), *histFDAGA(0);
180 TH1F *histCat(0), *histPBdt(0);
182 if (Use[
"Likelihood"]) histLk =
new TH1F(
"MVA_Likelihood",
"MVA_Likelihood", nbin, -1, 1 );
183 if (Use[
"LikelihoodD"]) histLkD =
new TH1F(
"MVA_LikelihoodD",
"MVA_LikelihoodD", nbin, -1, 0.9999 );
184 if (Use[
"LikelihoodPCA"]) histLkPCA =
new TH1F(
"MVA_LikelihoodPCA",
"MVA_LikelihoodPCA", nbin, -1, 1 );
185 if (Use[
"LikelihoodKDE"]) histLkKDE =
new TH1F(
"MVA_LikelihoodKDE",
"MVA_LikelihoodKDE", nbin, -0.00001, 0.99999 );
186 if (Use[
"LikelihoodMIX"]) histLkMIX =
new TH1F(
"MVA_LikelihoodMIX",
"MVA_LikelihoodMIX", nbin, 0, 1 );
187 if (Use[
"PDERS"]) histPD =
new TH1F(
"MVA_PDERS",
"MVA_PDERS", nbin, 0, 1 );
188 if (Use[
"PDERSD"]) histPDD =
new TH1F(
"MVA_PDERSD",
"MVA_PDERSD", nbin, 0, 1 );
189 if (Use[
"PDERSPCA"]) histPDPCA =
new TH1F(
"MVA_PDERSPCA",
"MVA_PDERSPCA", nbin, 0, 1 );
190 if (Use[
"KNN"]) histKNN =
new TH1F(
"MVA_KNN",
"MVA_KNN", nbin, 0, 1 );
191 if (Use[
"HMatrix"]) histHm =
new TH1F(
"MVA_HMatrix",
"MVA_HMatrix", nbin, -0.95, 1.55 );
192 if (Use[
"Fisher"]) histFi =
new TH1F(
"MVA_Fisher",
"MVA_Fisher", nbin, -4, 4 );
193 if (Use[
"FisherG"]) histFiG =
new TH1F(
"MVA_FisherG",
"MVA_FisherG", nbin, -1, 1 );
194 if (Use[
"BoostedFisher"]) histFiB =
new TH1F(
"MVA_BoostedFisher",
"MVA_BoostedFisher", nbin, -2, 2 );
195 if (Use[
"LD"]) histLD =
new TH1F(
"MVA_LD",
"MVA_LD", nbin, -2, 2 );
196 if (Use[
"MLP"]) histNn =
new TH1F(
"MVA_MLP",
"MVA_MLP", nbin, -1.25, 1.5 );
197 if (Use[
"MLPBFGS"]) histNnbfgs =
new TH1F(
"MVA_MLPBFGS",
"MVA_MLPBFGS", nbin, -1.25, 1.5 );
198 if (Use[
"MLPBNN"]) histNnbnn =
new TH1F(
"MVA_MLPBNN",
"MVA_MLPBNN", nbin, -1.25, 1.5 );
199 if (Use[
"CFMlpANN"]) histNnC =
new TH1F(
"MVA_CFMlpANN",
"MVA_CFMlpANN", nbin, 0, 1 );
200 if (Use[
"TMlpANN"]) histNnT =
new TH1F(
"MVA_TMlpANN",
"MVA_TMlpANN", nbin, -1.3, 1.3 );
201 if (Use[
"DNN"]) histNdn =
new TH1F(
"MVA_DNN",
"MVA_DNN", nbin, -0.1, 1.1 );
202 if (Use[
"BDT"]) histBdt =
new TH1F(
"MVA_BDT",
"MVA_BDT", nbin, -0.8, 0.8 );
203 if (Use[
"BDTG"]) histBdtG =
new TH1F(
"MVA_BDTG",
"MVA_BDTG", nbin, -1.0, 1.0 );
204 if (Use[
"BDTB"]) histBdtB =
new TH1F(
"MVA_BDTB",
"MVA_BDTB", nbin, -1.0, 1.0 );
205 if (Use[
"BDTD"]) histBdtD =
new TH1F(
"MVA_BDTD",
"MVA_BDTD", nbin, -0.8, 0.8 );
206 if (Use[
"BDTF"]) histBdtF =
new TH1F(
"MVA_BDTF",
"MVA_BDTF", nbin, -1.0, 1.0 );
207 if (Use[
"RuleFit"]) histRf =
new TH1F(
"MVA_RuleFit",
"MVA_RuleFit", nbin, -2.0, 2.0 );
208 if (Use[
"SVM_Gauss"]) histSVMG =
new TH1F(
"MVA_SVM_Gauss",
"MVA_SVM_Gauss", nbin, 0.0, 1.0 );
209 if (Use[
"SVM_Poly"]) histSVMP =
new TH1F(
"MVA_SVM_Poly",
"MVA_SVM_Poly", nbin, 0.0, 1.0 );
210 if (Use[
"SVM_Lin"]) histSVML =
new TH1F(
"MVA_SVM_Lin",
"MVA_SVM_Lin", nbin, 0.0, 1.0 );
211 if (Use[
"FDA_MT"]) histFDAMT =
new TH1F(
"MVA_FDA_MT",
"MVA_FDA_MT", nbin, -2.0, 3.0 );
212 if (Use[
"FDA_GA"]) histFDAGA =
new TH1F(
"MVA_FDA_GA",
"MVA_FDA_GA", nbin, -2.0, 3.0 );
213 if (Use[
"Category"]) histCat =
new TH1F(
"MVA_Category",
"MVA_Category", nbin, -2., 2. );
214 if (Use[
"Plugin"]) histPBdt =
new TH1F(
"MVA_PBDT",
"MVA_BDT", nbin, -0.8, 0.8 );
217 if (Use[
"PDEFoam"]) {
218 histPDEFoam =
new TH1F(
"MVA_PDEFoam",
"MVA_PDEFoam", nbin, 0, 1 );
219 histPDEFoamErr =
new TH1F(
"MVA_PDEFoamErr",
"MVA_PDEFoam error", nbin, 0, 1 );
220 histPDEFoamSig =
new TH1F(
"MVA_PDEFoamSig",
"MVA_PDEFoam significance", nbin, 0, 10 );
224 TH1F *probHistFi(0), *rarityHistFi(0);
226 probHistFi =
new TH1F(
"MVA_Fisher_Proba",
"MVA_Fisher_Proba", nbin, 0, 1 );
227 rarityHistFi =
new TH1F(
"MVA_Fisher_Rarity",
"MVA_Fisher_Rarity", nbin, 0, 1 );
235 TString fname =
"./tmva_example.root";
239 input =
TFile::Open(
"http://root.cern.ch/files/tmva_class_example.root" );
242 std::cout <<
"ERROR: could not open data file" << std::endl;
245 std::cout <<
"--- TMVAClassificationApp : Using input file: " << input->GetName() << std::endl;
254 std::cout <<
"--- Select signal sample" << std::endl;
255 TTree* theTree = (TTree*)input->Get(
"TreeS");
257 theTree->SetBranchAddress(
"var1", &userVar1 );
258 theTree->SetBranchAddress(
"var2", &userVar2 );
259 theTree->SetBranchAddress(
"var3", &var3 );
260 theTree->SetBranchAddress(
"var4", &var4 );
263 Int_t nSelCutsGA = 0;
266 std::vector<Float_t> vecVar(4);
268 std::cout <<
"--- Processing: " << theTree->GetEntries() <<
" events" << std::endl;
271 for (
Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
273 if (ievt%1000 == 0) std::cout <<
"--- ... Processing event: " << ievt << std::endl;
275 theTree->GetEntry(ievt);
277 var1 = userVar1 + userVar2;
278 var2 = userVar1 - userVar2;
285 if (passed) nSelCutsGA++;
288 if (Use[
"Likelihood" ]) histLk ->Fill( reader->
EvaluateMVA(
"Likelihood method" ) );
289 if (Use[
"LikelihoodD" ]) histLkD ->Fill( reader->
EvaluateMVA(
"LikelihoodD method" ) );
290 if (Use[
"LikelihoodPCA"]) histLkPCA ->Fill( reader->
EvaluateMVA(
"LikelihoodPCA method" ) );
291 if (Use[
"LikelihoodKDE"]) histLkKDE ->Fill( reader->
EvaluateMVA(
"LikelihoodKDE method" ) );
292 if (Use[
"LikelihoodMIX"]) histLkMIX ->Fill( reader->
EvaluateMVA(
"LikelihoodMIX method" ) );
293 if (Use[
"PDERS" ]) histPD ->Fill( reader->
EvaluateMVA(
"PDERS method" ) );
294 if (Use[
"PDERSD" ]) histPDD ->Fill( reader->
EvaluateMVA(
"PDERSD method" ) );
295 if (Use[
"PDERSPCA" ]) histPDPCA ->Fill( reader->
EvaluateMVA(
"PDERSPCA method" ) );
296 if (Use[
"KNN" ]) histKNN ->Fill( reader->
EvaluateMVA(
"KNN method" ) );
297 if (Use[
"HMatrix" ]) histHm ->Fill( reader->
EvaluateMVA(
"HMatrix method" ) );
298 if (Use[
"Fisher" ]) histFi ->Fill( reader->
EvaluateMVA(
"Fisher method" ) );
299 if (Use[
"FisherG" ]) histFiG ->Fill( reader->
EvaluateMVA(
"FisherG method" ) );
300 if (Use[
"BoostedFisher"]) histFiB ->Fill( reader->
EvaluateMVA(
"BoostedFisher method" ) );
301 if (Use[
"LD" ]) histLD ->Fill( reader->
EvaluateMVA(
"LD method" ) );
302 if (Use[
"MLP" ]) histNn ->Fill( reader->
EvaluateMVA(
"MLP method" ) );
303 if (Use[
"MLPBFGS" ]) histNnbfgs ->Fill( reader->
EvaluateMVA(
"MLPBFGS method" ) );
304 if (Use[
"MLPBNN" ]) histNnbnn ->Fill( reader->
EvaluateMVA(
"MLPBNN method" ) );
305 if (Use[
"CFMlpANN" ]) histNnC ->Fill( reader->
EvaluateMVA(
"CFMlpANN method" ) );
306 if (Use[
"TMlpANN" ]) histNnT ->Fill( reader->
EvaluateMVA(
"TMlpANN method" ) );
307 if (Use[
"DNN" ]) histNdn ->Fill( reader->
EvaluateMVA(
"DNN method" ) );
308 if (Use[
"BDT" ]) histBdt ->Fill( reader->
EvaluateMVA(
"BDT method" ) );
309 if (Use[
"BDTG" ]) histBdtG ->Fill( reader->
EvaluateMVA(
"BDTG method" ) );
310 if (Use[
"BDTB" ]) histBdtB ->Fill( reader->
EvaluateMVA(
"BDTB method" ) );
311 if (Use[
"BDTD" ]) histBdtD ->Fill( reader->
EvaluateMVA(
"BDTD method" ) );
312 if (Use[
"BDTF" ]) histBdtF ->Fill( reader->
EvaluateMVA(
"BDTF method" ) );
313 if (Use[
"RuleFit" ]) histRf ->Fill( reader->
EvaluateMVA(
"RuleFit method" ) );
314 if (Use[
"SVM_Gauss" ]) histSVMG ->Fill( reader->
EvaluateMVA(
"SVM_Gauss method" ) );
315 if (Use[
"SVM_Poly" ]) histSVMP ->Fill( reader->
EvaluateMVA(
"SVM_Poly method" ) );
316 if (Use[
"SVM_Lin" ]) histSVML ->Fill( reader->
EvaluateMVA(
"SVM_Lin method" ) );
317 if (Use[
"FDA_MT" ]) histFDAMT ->Fill( reader->
EvaluateMVA(
"FDA_MT method" ) );
318 if (Use[
"FDA_GA" ]) histFDAGA ->Fill( reader->
EvaluateMVA(
"FDA_GA method" ) );
319 if (Use[
"Category" ]) histCat ->Fill( reader->
EvaluateMVA(
"Category method" ) );
320 if (Use[
"Plugin" ]) histPBdt ->Fill( reader->
EvaluateMVA(
"P_BDT method" ) );
323 if (Use[
"PDEFoam"]) {
326 histPDEFoam ->Fill( val );
327 histPDEFoamErr->Fill( err );
328 if (err>1.
e-50) histPDEFoamSig->Fill( val/err );
333 probHistFi ->Fill( reader->
GetProba (
"Fisher method" ) );
334 rarityHistFi->Fill( reader->
GetRarity(
"Fisher method" ) );
340 std::cout <<
"--- End of event loop: "; sw.
Print();
343 if (Use[
"CutsGA"]) std::cout <<
"--- Efficiency for CutsGA method: " << double(nSelCutsGA)/theTree->GetEntries()
344 <<
" (for a required signal efficiency of " << effS <<
")" << std::endl;
353 std::vector<Double_t> cutsMin;
354 std::vector<Double_t> cutsMax;
355 mcuts->
GetCuts( 0.7, cutsMin, cutsMax );
356 std::cout <<
"--- -------------------------------------------------------------" << std::endl;
357 std::cout <<
"--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
358 for (
UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
359 std::cout <<
"... Cut: " 364 << cutsMax[ivar] << std::endl;
366 std::cout <<
"--- -------------------------------------------------------------" << std::endl;
372 TFile *target =
new TFile(
"TMVApp.root",
"RECREATE" );
373 if (Use[
"Likelihood" ]) histLk ->Write();
374 if (Use[
"LikelihoodD" ]) histLkD ->Write();
375 if (Use[
"LikelihoodPCA"]) histLkPCA ->Write();
376 if (Use[
"LikelihoodKDE"]) histLkKDE ->Write();
377 if (Use[
"LikelihoodMIX"]) histLkMIX ->Write();
378 if (Use[
"PDERS" ]) histPD ->Write();
379 if (Use[
"PDERSD" ]) histPDD ->Write();
380 if (Use[
"PDERSPCA" ]) histPDPCA ->Write();
381 if (Use[
"KNN" ]) histKNN ->Write();
382 if (Use[
"HMatrix" ]) histHm ->Write();
383 if (Use[
"Fisher" ]) histFi ->Write();
384 if (Use[
"FisherG" ]) histFiG ->Write();
385 if (Use[
"BoostedFisher"]) histFiB ->Write();
386 if (Use[
"LD" ]) histLD ->Write();
387 if (Use[
"MLP" ]) histNn ->Write();
388 if (Use[
"MLPBFGS" ]) histNnbfgs ->Write();
389 if (Use[
"MLPBNN" ]) histNnbnn ->Write();
390 if (Use[
"CFMlpANN" ]) histNnC ->Write();
391 if (Use[
"TMlpANN" ]) histNnT ->Write();
392 if (Use[
"DNN" ]) histNdn ->Write();
393 if (Use[
"BDT" ]) histBdt ->Write();
394 if (Use[
"BDTG" ]) histBdtG ->Write();
395 if (Use[
"BDTB" ]) histBdtB ->Write();
396 if (Use[
"BDTD" ]) histBdtD ->Write();
397 if (Use[
"BDTF" ]) histBdtF ->Write();
398 if (Use[
"RuleFit" ]) histRf ->Write();
399 if (Use[
"SVM_Gauss" ]) histSVMG ->Write();
400 if (Use[
"SVM_Poly" ]) histSVMP ->Write();
401 if (Use[
"SVM_Lin" ]) histSVML ->Write();
402 if (Use[
"FDA_MT" ]) histFDAMT ->Write();
403 if (Use[
"FDA_GA" ]) histFDAGA ->Write();
404 if (Use[
"Category" ]) histCat ->Write();
405 if (Use[
"Plugin" ]) histPBdt ->Write();
408 if (Use[
"PDEFoam"]) { histPDEFoam->Write(); histPDEFoamErr->Write(); histPDEFoamSig->Write(); }
411 if (Use[
"Fisher"]) {
if (probHistFi != 0) probHistFi->Write();
if (rarityHistFi != 0) rarityHistFi->Write(); }
414 std::cout <<
"--- Created root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
418 std::cout <<
"==> TMVAClassificationApplication is done!" << std::endl << std::endl;
421 int main(
int argc,
char** argv )
424 for (
int i=1; i<argc; i++) {
425 TString regMethod(argv[i]);
426 if(regMethod==
"-b" || regMethod==
"--batch")
continue;
427 if (!methodList.IsNull()) methodList += TString(
",");
428 methodList += regMethod;
430 TMVAClassificationApplication(methodList);
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Double_t GetRarity(const TString &methodTag, Double_t mvaVal=-9999999)
evaluates the MVA's rarity
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Double_t GetCuts(Double_t effS, std::vector< Double_t > &cutMin, std::vector< Double_t > &cutMax) const
retrieve cut values for given signal efficiency
Double_t GetMVAError() const
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
const TString & GetInputVar(Int_t i) const
void Stop()
Stop the stopwatch.
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
R__EXTERN TSystem * gSystem
void AddSpectator(const TString &expression, Float_t *)
Add a float spectator or expression to the reader.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Double_t EvaluateMVA(const std::vector< Float_t > &, const TString &methodTag, Double_t aux=0)
Evaluate a std::vector<float> of input data for a given method The parameter aux is obligatory for th...
Abstract ClassifierFactory template that handles arbitrary types.
int main(int argc, char **argv)
Double_t GetProba(const TString &methodTag, Double_t ap_sig=0.5, Double_t mvaVal=-9999999)
evaluates probability of MVA for given set of input variables
MethodCuts * FindCutsMVA(const TString &methodTag)
special function for Cuts to avoid dynamic_casts in ROOT macros, which are not properly handled by CI...