33void 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;
104 Use[
"SVM_Gauss"] = 0;
108 std::cout << std::endl;
109 std::cout <<
"==> Start TMVAClassificationApplication" << std::endl;
112 if (myMethodList !=
"") {
113 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
116 for (
UInt_t i=0; i<mlist.size(); i++) {
117 std::string regMethod(mlist[i]);
119 if (Use.find(regMethod) == Use.end()) {
120 std::cout <<
"Method \"" << regMethod
121 <<
"\" not known in TMVA under this name. Choose among the following:" << std::endl;
122 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
123 std::cout << it->first <<
" ";
125 std::cout << std::endl;
142 reader->
AddVariable(
"myvar1 := var1+var2", &var1 );
143 reader->
AddVariable(
"myvar2 := var1-var2", &var2 );
152 Float_t Category_cat1, Category_cat2, Category_cat3;
153 if (Use[
"Category"]){
155 reader->
AddSpectator(
"Category_cat1 := var3<=0", &Category_cat1 );
156 reader->
AddSpectator(
"Category_cat2 := (var3>0)&&(var4<0)", &Category_cat2 );
157 reader->
AddSpectator(
"Category_cat3 := (var3>0)&&(var4>=0)", &Category_cat3 );
162 TString dir =
"dataset/weights/";
163 TString prefix =
"TMVAClassification";
166 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
170 reader->
BookMVA( methodName, weightfile );
184 TH1F *histPDEFoam(0);
185 TH1F *histPDEFoamErr(0);
186 TH1F *histPDEFoamSig(0);
214 if (Use[
"Likelihood"]) histLk =
new TH1F(
"MVA_Likelihood",
"MVA_Likelihood", nbin, -1, 1 );
215 if (Use[
"LikelihoodD"]) histLkD =
new TH1F(
"MVA_LikelihoodD",
"MVA_LikelihoodD", nbin, -1, 0.9999 );
216 if (Use[
"LikelihoodPCA"]) histLkPCA =
new TH1F(
"MVA_LikelihoodPCA",
"MVA_LikelihoodPCA", nbin, -1, 1 );
217 if (Use[
"LikelihoodKDE"]) histLkKDE =
new TH1F(
"MVA_LikelihoodKDE",
"MVA_LikelihoodKDE", nbin, -0.00001, 0.99999 );
218 if (Use[
"LikelihoodMIX"]) histLkMIX =
new TH1F(
"MVA_LikelihoodMIX",
"MVA_LikelihoodMIX", nbin, 0, 1 );
219 if (Use[
"PDERS"]) histPD =
new TH1F(
"MVA_PDERS",
"MVA_PDERS", nbin, 0, 1 );
220 if (Use[
"PDERSD"]) histPDD =
new TH1F(
"MVA_PDERSD",
"MVA_PDERSD", nbin, 0, 1 );
221 if (Use[
"PDERSPCA"]) histPDPCA =
new TH1F(
"MVA_PDERSPCA",
"MVA_PDERSPCA", nbin, 0, 1 );
222 if (Use[
"KNN"]) histKNN =
new TH1F(
"MVA_KNN",
"MVA_KNN", nbin, 0, 1 );
223 if (Use[
"HMatrix"]) histHm =
new TH1F(
"MVA_HMatrix",
"MVA_HMatrix", nbin, -0.95, 1.55 );
224 if (Use[
"Fisher"]) histFi =
new TH1F(
"MVA_Fisher",
"MVA_Fisher", nbin, -4, 4 );
225 if (Use[
"FisherG"]) histFiG =
new TH1F(
"MVA_FisherG",
"MVA_FisherG", nbin, -1, 1 );
226 if (Use[
"BoostedFisher"]) histFiB =
new TH1F(
"MVA_BoostedFisher",
"MVA_BoostedFisher", nbin, -2, 2 );
227 if (Use[
"LD"]) histLD =
new TH1F(
"MVA_LD",
"MVA_LD", nbin, -2, 2 );
228 if (Use[
"MLP"]) histNn =
new TH1F(
"MVA_MLP",
"MVA_MLP", nbin, -1.25, 1.5 );
229 if (Use[
"MLPBFGS"]) histNnbfgs =
new TH1F(
"MVA_MLPBFGS",
"MVA_MLPBFGS", nbin, -1.25, 1.5 );
230 if (Use[
"MLPBNN"]) histNnbnn =
new TH1F(
"MVA_MLPBNN",
"MVA_MLPBNN", nbin, -1.25, 1.5 );
231 if (Use[
"CFMlpANN"]) histNnC =
new TH1F(
"MVA_CFMlpANN",
"MVA_CFMlpANN", nbin, 0, 1 );
232 if (Use[
"TMlpANN"]) histNnT =
new TH1F(
"MVA_TMlpANN",
"MVA_TMlpANN", nbin, -1.3, 1.3 );
233 if (Use[
"DNN_GPU"]) histDnnGpu =
new TH1F(
"MVA_DNN_GPU",
"MVA_DNN_GPU", nbin, -0.1, 1.1);
234 if (Use[
"DNN_CPU"]) histDnnCpu =
new TH1F(
"MVA_DNN_CPU",
"MVA_DNN_CPU", nbin, -0.1, 1.1);
235 if (Use[
"BDT"]) histBdt =
new TH1F(
"MVA_BDT",
"MVA_BDT", nbin, -0.8, 0.8 );
236 if (Use[
"BDTG"]) histBdtG =
new TH1F(
"MVA_BDTG",
"MVA_BDTG", nbin, -1.0, 1.0 );
237 if (Use[
"BDTB"]) histBdtB =
new TH1F(
"MVA_BDTB",
"MVA_BDTB", nbin, -1.0, 1.0 );
238 if (Use[
"BDTD"]) histBdtD =
new TH1F(
"MVA_BDTD",
"MVA_BDTD", nbin, -0.8, 0.8 );
239 if (Use[
"BDTF"]) histBdtF =
new TH1F(
"MVA_BDTF",
"MVA_BDTF", nbin, -1.0, 1.0 );
240 if (Use[
"RuleFit"]) histRf =
new TH1F(
"MVA_RuleFit",
"MVA_RuleFit", nbin, -2.0, 2.0 );
241 if (Use[
"SVM_Gauss"]) histSVMG =
new TH1F(
"MVA_SVM_Gauss",
"MVA_SVM_Gauss", nbin, 0.0, 1.0 );
242 if (Use[
"SVM_Poly"]) histSVMP =
new TH1F(
"MVA_SVM_Poly",
"MVA_SVM_Poly", nbin, 0.0, 1.0 );
243 if (Use[
"SVM_Lin"]) histSVML =
new TH1F(
"MVA_SVM_Lin",
"MVA_SVM_Lin", nbin, 0.0, 1.0 );
244 if (Use[
"FDA_MT"]) histFDAMT =
new TH1F(
"MVA_FDA_MT",
"MVA_FDA_MT", nbin, -2.0, 3.0 );
245 if (Use[
"FDA_GA"]) histFDAGA =
new TH1F(
"MVA_FDA_GA",
"MVA_FDA_GA", nbin, -2.0, 3.0 );
246 if (Use[
"Category"]) histCat =
new TH1F(
"MVA_Category",
"MVA_Category", nbin, -2., 2. );
247 if (Use[
"Plugin"]) histPBdt =
new TH1F(
"MVA_PBDT",
"MVA_BDT", nbin, -0.8, 0.8 );
250 if (Use[
"PDEFoam"]) {
251 histPDEFoam =
new TH1F(
"MVA_PDEFoam",
"MVA_PDEFoam", nbin, 0, 1 );
252 histPDEFoamErr =
new TH1F(
"MVA_PDEFoamErr",
"MVA_PDEFoam error", nbin, 0, 1 );
253 histPDEFoamSig =
new TH1F(
"MVA_PDEFoamSig",
"MVA_PDEFoam significance", nbin, 0, 10 );
257 TH1F *probHistFi(0), *rarityHistFi(0);
259 probHistFi =
new TH1F(
"MVA_Fisher_Proba",
"MVA_Fisher_Proba", nbin, 0, 1 );
260 rarityHistFi =
new TH1F(
"MVA_Fisher_Rarity",
"MVA_Fisher_Rarity", nbin, 0, 1 );
268 TString fname =
"./tmva_class_example.root";
274 input =
TFile::Open(
"http://root.cern/files/tmva_class_example.root",
"CACHEREAD");
277 std::cout <<
"ERROR: could not open data file" << std::endl;
280 std::cout <<
"--- TMVAClassificationApp : Using input file: " <<
input->GetName() << std::endl;
289 std::cout <<
"--- Select signal sample" << std::endl;
298 Int_t nSelCutsGA = 0;
301 std::vector<Float_t> vecVar(4);
303 std::cout <<
"--- Processing: " << theTree->
GetEntries() <<
" events" << std::endl;
308 if (ievt%1000 == 0) std::cout <<
"--- ... Processing event: " << ievt << std::endl;
312 var1 = userVar1 + userVar2;
313 var2 = userVar1 - userVar2;
320 if (passed) nSelCutsGA++;
323 if (Use[
"Likelihood" ]) histLk ->Fill( reader->
EvaluateMVA(
"Likelihood method" ) );
324 if (Use[
"LikelihoodD" ]) histLkD ->Fill( reader->
EvaluateMVA(
"LikelihoodD method" ) );
325 if (Use[
"LikelihoodPCA"]) histLkPCA ->Fill( reader->
EvaluateMVA(
"LikelihoodPCA method" ) );
326 if (Use[
"LikelihoodKDE"]) histLkKDE ->Fill( reader->
EvaluateMVA(
"LikelihoodKDE method" ) );
327 if (Use[
"LikelihoodMIX"]) histLkMIX ->Fill( reader->
EvaluateMVA(
"LikelihoodMIX method" ) );
328 if (Use[
"PDERS" ]) histPD ->Fill( reader->
EvaluateMVA(
"PDERS method" ) );
329 if (Use[
"PDERSD" ]) histPDD ->Fill( reader->
EvaluateMVA(
"PDERSD method" ) );
330 if (Use[
"PDERSPCA" ]) histPDPCA ->Fill( reader->
EvaluateMVA(
"PDERSPCA method" ) );
331 if (Use[
"KNN" ]) histKNN ->Fill( reader->
EvaluateMVA(
"KNN method" ) );
332 if (Use[
"HMatrix" ]) histHm ->Fill( reader->
EvaluateMVA(
"HMatrix method" ) );
333 if (Use[
"Fisher" ]) histFi ->Fill( reader->
EvaluateMVA(
"Fisher method" ) );
334 if (Use[
"FisherG" ]) histFiG ->Fill( reader->
EvaluateMVA(
"FisherG method" ) );
335 if (Use[
"BoostedFisher"]) histFiB ->Fill( reader->
EvaluateMVA(
"BoostedFisher method" ) );
336 if (Use[
"LD" ]) histLD ->Fill( reader->
EvaluateMVA(
"LD method" ) );
337 if (Use[
"MLP" ]) histNn ->Fill( reader->
EvaluateMVA(
"MLP method" ) );
338 if (Use[
"MLPBFGS" ]) histNnbfgs ->Fill( reader->
EvaluateMVA(
"MLPBFGS method" ) );
339 if (Use[
"MLPBNN" ]) histNnbnn ->Fill( reader->
EvaluateMVA(
"MLPBNN method" ) );
340 if (Use[
"CFMlpANN" ]) histNnC ->Fill( reader->
EvaluateMVA(
"CFMlpANN method" ) );
341 if (Use[
"TMlpANN" ]) histNnT ->Fill( reader->
EvaluateMVA(
"TMlpANN method" ) );
342 if (Use[
"DNN_GPU"]) histDnnGpu->Fill(reader->
EvaluateMVA(
"DNN_GPU method"));
343 if (Use[
"DNN_CPU"]) histDnnCpu->Fill(reader->
EvaluateMVA(
"DNN_CPU method"));
344 if (Use[
"BDT" ]) histBdt ->Fill( reader->
EvaluateMVA(
"BDT method" ) );
345 if (Use[
"BDTG" ]) histBdtG ->Fill( reader->
EvaluateMVA(
"BDTG method" ) );
346 if (Use[
"BDTB" ]) histBdtB ->Fill( reader->
EvaluateMVA(
"BDTB method" ) );
347 if (Use[
"BDTD" ]) histBdtD ->Fill( reader->
EvaluateMVA(
"BDTD method" ) );
348 if (Use[
"BDTF" ]) histBdtF ->Fill( reader->
EvaluateMVA(
"BDTF method" ) );
349 if (Use[
"RuleFit" ]) histRf ->Fill( reader->
EvaluateMVA(
"RuleFit method" ) );
350 if (Use[
"SVM_Gauss" ]) histSVMG ->Fill( reader->
EvaluateMVA(
"SVM_Gauss method" ) );
351 if (Use[
"SVM_Poly" ]) histSVMP ->Fill( reader->
EvaluateMVA(
"SVM_Poly method" ) );
352 if (Use[
"SVM_Lin" ]) histSVML ->Fill( reader->
EvaluateMVA(
"SVM_Lin method" ) );
353 if (Use[
"FDA_MT" ]) histFDAMT ->Fill( reader->
EvaluateMVA(
"FDA_MT method" ) );
354 if (Use[
"FDA_GA" ]) histFDAGA ->Fill( reader->
EvaluateMVA(
"FDA_GA method" ) );
355 if (Use[
"Category" ]) histCat ->Fill( reader->
EvaluateMVA(
"Category method" ) );
356 if (Use[
"Plugin" ]) histPBdt ->Fill( reader->
EvaluateMVA(
"P_BDT method" ) );
359 if (Use[
"PDEFoam"]) {
362 histPDEFoam ->Fill( val );
363 histPDEFoamErr->Fill( err );
364 if (err>1.e-50) histPDEFoamSig->Fill( val/err );
369 probHistFi ->Fill( reader->
GetProba (
"Fisher method" ) );
370 rarityHistFi->Fill( reader->
GetRarity(
"Fisher method" ) );
376 std::cout <<
"--- End of event loop: "; sw.
Print();
379 if (Use[
"CutsGA"]) std::cout <<
"--- Efficiency for CutsGA method: " <<
double(nSelCutsGA)/theTree->
GetEntries()
380 <<
" (for a required signal efficiency of " << effS <<
")" << std::endl;
388 std::vector<Double_t> cutsMin;
389 std::vector<Double_t> cutsMax;
390 mcuts->
GetCuts( 0.7, cutsMin, cutsMax );
391 std::cout <<
"--- -------------------------------------------------------------" << std::endl;
392 std::cout <<
"--- Retrieve cut values for signal efficiency of 0.7 from Reader" << std::endl;
393 for (
UInt_t ivar=0; ivar<cutsMin.size(); ivar++) {
394 std::cout <<
"... Cut: "
399 << cutsMax[ivar] << std::endl;
401 std::cout <<
"--- -------------------------------------------------------------" << std::endl;
408 if (Use[
"Likelihood" ]) histLk ->Write();
409 if (Use[
"LikelihoodD" ]) histLkD ->Write();
410 if (Use[
"LikelihoodPCA"]) histLkPCA ->Write();
411 if (Use[
"LikelihoodKDE"]) histLkKDE ->Write();
412 if (Use[
"LikelihoodMIX"]) histLkMIX ->Write();
413 if (Use[
"PDERS" ]) histPD ->Write();
414 if (Use[
"PDERSD" ]) histPDD ->Write();
415 if (Use[
"PDERSPCA" ]) histPDPCA ->Write();
416 if (Use[
"KNN" ]) histKNN ->Write();
417 if (Use[
"HMatrix" ]) histHm ->Write();
418 if (Use[
"Fisher" ]) histFi ->Write();
419 if (Use[
"FisherG" ]) histFiG ->Write();
420 if (Use[
"BoostedFisher"]) histFiB ->Write();
421 if (Use[
"LD" ]) histLD ->Write();
422 if (Use[
"MLP" ]) histNn ->Write();
423 if (Use[
"MLPBFGS" ]) histNnbfgs ->Write();
424 if (Use[
"MLPBNN" ]) histNnbnn ->Write();
425 if (Use[
"CFMlpANN" ]) histNnC ->Write();
426 if (Use[
"TMlpANN" ]) histNnT ->Write();
427 if (Use[
"DNN_GPU"]) histDnnGpu->Write();
428 if (Use[
"DNN_CPU"]) histDnnCpu->Write();
429 if (Use[
"BDT" ]) histBdt ->Write();
430 if (Use[
"BDTG" ]) histBdtG ->Write();
431 if (Use[
"BDTB" ]) histBdtB ->Write();
432 if (Use[
"BDTD" ]) histBdtD ->Write();
433 if (Use[
"BDTF" ]) histBdtF ->Write();
434 if (Use[
"RuleFit" ]) histRf ->Write();
435 if (Use[
"SVM_Gauss" ]) histSVMG ->Write();
436 if (Use[
"SVM_Poly" ]) histSVMP ->Write();
437 if (Use[
"SVM_Lin" ]) histSVML ->Write();
438 if (Use[
"FDA_MT" ]) histFDAMT ->Write();
439 if (Use[
"FDA_GA" ]) histFDAGA ->Write();
440 if (Use[
"Category" ]) histCat ->Write();
441 if (Use[
"Plugin" ]) histPBdt ->Write();
444 if (Use[
"PDEFoam"]) { histPDEFoam->Write(); histPDEFoamErr->Write(); histPDEFoamSig->Write(); }
447 if (Use[
"Fisher"]) {
if (probHistFi != 0) probHistFi->Write();
if (rarityHistFi != 0) rarityHistFi->Write(); }
450 std::cout <<
"--- Created root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
454 std::cout <<
"==> TMVAClassificationApplication is done!" << std::endl << std::endl;
457int main(
int argc,
char** argv )
460 for (
int i=1; i<argc; i++) {
462 if(regMethod==
"-b" || regMethod==
"--batch")
continue;
464 methodList += regMethod;
466 TMVAClassificationApplication(methodList);
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
R__EXTERN TSystem * gSystem
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
static Bool_t SetCacheFileDir(std::string_view cacheDir, Bool_t operateDisconnected=kTRUE, Bool_t forceCacheread=kFALSE)
Sets the directory where to locally stage/cache remote files.
1-D histogram with a float per channel (see TH1 documentation)
const TString & GetInputVar(Int_t i) const
Multivariate optimisation of signal efficiency for given background efficiency, applying rectangular ...
Double_t GetCuts(Double_t effS, std::vector< Double_t > &cutMin, std::vector< Double_t > &cutMax) const
retrieve cut values for given signal efficiency
The Reader class serves to use the MVAs in a specific analysis context.
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...
Double_t GetRarity(const TString &methodTag, Double_t mvaVal=-9999999)
evaluates the MVA's rarity
IMethod * FindMVA(const TString &methodTag)
return pointer to method with tag "methodTag"
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
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
void AddSpectator(const TString &expression, Float_t *)
Add a float spectator or expression to the reader.
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
Double_t GetMVAError() const
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void Stop()
Stop the stopwatch.
void Print(Option_t *option="") const override
Print the real and cpu time passed between the start and stop events.
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
A TTree represents a columnar dataset.
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
virtual Long64_t GetEntries() const
create variable transformations