{ "cells": [ { "cell_type": "markdown", "id": "2ef482ec", "metadata": {}, "source": [ "# TMVAMulticlassApplication\n", "This macro provides a simple example on how to use the trained multiclass\n", "classifiers within an analysis module\n", "- Project : TMVA - a Root-integrated toolkit for multivariate data analysis\n", "- Package : TMVA\n", "- Root Macro: TMVAMulticlassApplication\n", "\n", "\n", "\n", "**Author:** Andreas Hoecker \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:24 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "7b49da83", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:17.123400Z", "iopub.status.busy": "2026-05-19T20:24:17.123288Z", "iopub.status.idle": "2026-05-19T20:24:17.134427Z", "shell.execute_reply": "2026-05-19T20:24:17.133936Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "#include \"TFile.h\"\n", "#include \"TTree.h\"\n", "#include \"TString.h\"\n", "#include \"TSystem.h\"\n", "#include \"TROOT.h\"\n", "#include \"TStopwatch.h\"\n", "#include \"TH1F.h\"\n", "\n", "#include \"TMVA/Tools.h\"\n", "#include \"TMVA/Reader.h\"\n", "\n", "using namespace TMVA;" ] }, { "cell_type": "markdown", "id": "f45f2bc9", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 2, "id": "1f397ad3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:17.135708Z", "iopub.status.busy": "2026-05-19T20:24:17.135578Z", "iopub.status.idle": "2026-05-19T20:24:17.454305Z", "shell.execute_reply": "2026-05-19T20:24:17.453920Z" } }, "outputs": [], "source": [ "TString myMethodList = \"\";" ] }, { "cell_type": "code", "execution_count": 3, "id": "3ad61dc4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:17.466240Z", "iopub.status.busy": "2026-05-19T20:24:17.466100Z", "iopub.status.idle": "2026-05-19T20:24:17.690979Z", "shell.execute_reply": "2026-05-19T20:24:17.680516Z" } }, "outputs": [], "source": [ "TMVA::Tools::Instance();" ] }, { "cell_type": "markdown", "id": "5296b54d", "metadata": {}, "source": [ "---------------------------------------------------------------\n", "Default MVA methods to be trained + tested" ] }, { "cell_type": "code", "execution_count": 4, "id": "59d9a772", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:17.707260Z", "iopub.status.busy": "2026-05-19T20:24:17.707106Z", "iopub.status.idle": "2026-05-19T20:24:18.042266Z", "shell.execute_reply": "2026-05-19T20:24:18.041596Z" } }, "outputs": [], "source": [ "std::map Use;\n", "Use[\"MLP\"] = 1;\n", "Use[\"BDTG\"] = 1;\n", "Use[\"DL_CPU\"] = 1;\n", "Use[\"DL_GPU\"] = 1;\n", "Use[\"FDA_GA\"] = 1;\n", "Use[\"PDEFoam\"] = 1;" ] }, { "cell_type": "markdown", "id": "b75038af", "metadata": {}, "source": [ "---------------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": 5, "id": "4df37e8d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:18.057278Z", "iopub.status.busy": "2026-05-19T20:24:18.057127Z", "iopub.status.idle": "2026-05-19T20:24:18.259486Z", "shell.execute_reply": "2026-05-19T20:24:18.258957Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "==> Start TMVAMulticlassApp\n" ] } ], "source": [ "std::cout << std::endl;\n", "std::cout << \"==> Start TMVAMulticlassApp\" << std::endl;\n", "if (myMethodList != \"\") {\n", " for (std::map::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;\n", "\n", " std::vector mlist = gTools().SplitString( myMethodList, ',' );\n", " for (UInt_t i=0; i::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << \" \" << std::endl;\n", " std::cout << std::endl;\n", " return;\n", " }\n", " Use[regMethod] = 1;\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "06ab49c5", "metadata": {}, "source": [ "create the Reader object" ] }, { "cell_type": "code", "execution_count": 6, "id": "368808d9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:18.277448Z", "iopub.status.busy": "2026-05-19T20:24:18.277282Z", "iopub.status.idle": "2026-05-19T20:24:18.479548Z", "shell.execute_reply": "2026-05-19T20:24:18.478936Z" } }, "outputs": [], "source": [ "TMVA::Reader *reader = new TMVA::Reader( \"!Color:!Silent\" );" ] }, { "cell_type": "markdown", "id": "1e06204b", "metadata": {}, "source": [ "create a set of variables and declare them to the reader\n", "- the variable names must corresponds in name and type to\n", "those given in the weight file(s) that you use" ] }, { "cell_type": "code", "execution_count": 7, "id": "2a732e61", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:18.481447Z", "iopub.status.busy": "2026-05-19T20:24:18.481331Z", "iopub.status.idle": "2026-05-19T20:24:18.683577Z", "shell.execute_reply": "2026-05-19T20:24:18.682976Z" } }, "outputs": [], "source": [ "Float_t var1, var2, var3, var4;\n", "reader->AddVariable( \"var1\", &var1 );\n", "reader->AddVariable( \"var2\", &var2 );\n", "reader->AddVariable( \"var3\", &var3 );\n", "reader->AddVariable( \"var4\", &var4 );" ] }, { "cell_type": "markdown", "id": "9585e851", "metadata": {}, "source": [ "book the MVA methods" ] }, { "cell_type": "code", "execution_count": 8, "id": "5542f17e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:18.685599Z", "iopub.status.busy": "2026-05-19T20:24:18.685472Z", "iopub.status.idle": "2026-05-19T20:24:19.060901Z", "shell.execute_reply": "2026-05-19T20:24:19.048370Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " : Booking \"BDTG method\" of type \"BDT\" from dataset/weights/TMVAMulticlass_BDTG.weights.xml.\n", " : Reading weight file: dataset/weights/TMVAMulticlass_BDTG.weights.xml\n", "
DataSetInfo : [Default] : Added class \"Signal\"\n", "
DataSetInfo : [Default] : Added class \"bg0\"\n", "
DataSetInfo : [Default] : Added class \"bg1\"\n", "
DataSetInfo : [Default] : Added class \"bg2\"\n", " : Booked classifier \"BDTG\" of type: \"BDT\"\n", " : Booking \"DL_CPU method\" of type \"DL\" from dataset/weights/TMVAMulticlass_DL_CPU.weights.xml.\n", " : Reading weight file: dataset/weights/TMVAMulticlass_DL_CPU.weights.xml\n", " : Booked classifier \"DL_CPU\" of type: \"DL\"\n", "TMVAMultiClassApplication: Skip DL_GPU method since it has not been trained !\n", "TMVAMultiClassApplication: Skip FDA_GA method since it has not been trained !\n", " : Booking \"MLP method\" of type \"MLP\" from dataset/weights/TMVAMulticlass_MLP.weights.xml.\n", " : Reading weight file: dataset/weights/TMVAMulticlass_MLP.weights.xml\n", "
MLP : Building Network. \n", " : Initializing weights\n", " : Booked classifier \"MLP\" of type: \"MLP\"\n", " : Booking \"PDEFoam method\" of type \"PDEFoam\" from dataset/weights/TMVAMulticlass_PDEFoam.weights.xml.\n", " : Reading weight file: dataset/weights/TMVAMulticlass_PDEFoam.weights.xml\n", " : Read foams from file: dataset/weights/TMVAMulticlass_PDEFoam.weights_foams.root\n", " : Booked classifier \"PDEFoam\" of type: \"PDEFoam\"\n" ] } ], "source": [ "TString dir = \"dataset/weights/\";\n", "TString prefix = \"TMVAMulticlass\";\n", "\n", "for (std::map::iterator it = Use.begin(); it != Use.end(); it++) {\n", " if (it->second) {\n", " TString methodName = TString(it->first) + TString(\" method\");\n", " TString weightfile = dir + prefix + TString(\"_\") + TString(it->first) + TString(\".weights.xml\");\n", " // check if file existing (i.e. method has been trained)\n", " if (!gSystem->AccessPathName( weightfile ))\n", " // file exists\n", " reader->BookMVA( methodName, weightfile );\n", " else {\n", " std::cout << \"TMVAMultiClassApplication: Skip \" << methodName << \" since it has not been trained !\" << std::endl;\n", " it->second = 0;\n", " }\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "ff1ab0ac", "metadata": {}, "source": [ "book output histograms" ] }, { "cell_type": "code", "execution_count": 9, "id": "74a4b3dc", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:19.062491Z", "iopub.status.busy": "2026-05-19T20:24:19.062367Z", "iopub.status.idle": "2026-05-19T20:24:19.272460Z", "shell.execute_reply": "2026-05-19T20:24:19.271703Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- TMVAMulticlassApp : Using input file: /github/home/ROOT-CI/build/tutorials/machine_learning/data/tmva_multiclass_example.root\n" ] } ], "source": [ "UInt_t nbin = 100;\n", "TH1F *histMLP_signal(0), *histBDTG_signal(0), *histFDAGA_signal(0), *histPDEFoam_signal(0);\n", "TH1F *histDLCPU_signal(0), *histDLGPU_signal(0);\n", "if (Use[\"MLP\"])\n", " histMLP_signal = new TH1F( \"MVA_MLP_signal\", \"MVA_MLP_signal\", nbin, 0., 1.1 );\n", "if (Use[\"BDTG\"])\n", " histBDTG_signal = new TH1F( \"MVA_BDTG_signal\", \"MVA_BDTG_signal\", nbin, 0., 1.1 );\n", "if (Use[\"DL_CPU\"])\n", " histDLCPU_signal = new TH1F(\"MVA_DLCPU_signal\", \"MVA_DLCPU_signal\", nbin, 0., 1.1);\n", "if (Use[\"DL_GPU\"])\n", " histDLGPU_signal = new TH1F(\"MVA_DLGPU_signal\", \"MVA_DLGPU_signal\", nbin, 0., 1.1);\n", "if (Use[\"FDA_GA\"])\n", " histFDAGA_signal = new TH1F( \"MVA_FDA_GA_signal\", \"MVA_FDA_GA_signal\", nbin, 0., 1.1 );\n", "if (Use[\"PDEFoam\"])\n", " histPDEFoam_signal = new TH1F( \"MVA_PDEFoam_signal\", \"MVA_PDEFoam_signal\", nbin, 0., 1.1 );\n", "\n", "\n", "TFile *input(nullptr);\n", "TString fname = gROOT->GetTutorialDir() + \"/machine_learning/data/tmva_multiclass_example.root\";\n", "if (!gSystem->AccessPathName( fname )) {\n", " input = TFile::Open( fname ); // check if file in local directory exists\n", "}\n", "if (!input) {\n", " std::cout << \"ERROR: could not open data file\" << std::endl;\n", " exit(1);\n", "}\n", "std::cout << \"--- TMVAMulticlassApp : Using input file: \" << input->GetName() << std::endl;" ] }, { "cell_type": "markdown", "id": "8f104290", "metadata": {}, "source": [ "prepare the tree\n", "- here the variable names have to corresponds to your tree\n", "- you can use the same variables as above which is slightly faster,\n", "but of course you can use different ones and copy the values inside the event loop" ] }, { "cell_type": "code", "execution_count": 10, "id": "71d526b4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:19.274410Z", "iopub.status.busy": "2026-05-19T20:24:19.274276Z", "iopub.status.idle": "2026-05-19T20:24:19.831767Z", "shell.execute_reply": "2026-05-19T20:24:19.831379Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Select signal sample\n", "--- Processing: 2000 events\n", "--- ... Processing event: 0\n", " : Rebuilding Dataset Default\n", "--- ... Processing event: 1000\n" ] } ], "source": [ "TTree* theTree = (TTree*)input->Get(\"TreeS\");\n", "std::cout << \"--- Select signal sample\" << std::endl;\n", "theTree->SetBranchAddress( \"var1\", &var1 );\n", "theTree->SetBranchAddress( \"var2\", &var2 );\n", "theTree->SetBranchAddress( \"var3\", &var3 );\n", "theTree->SetBranchAddress( \"var4\", &var4 );\n", "\n", "std::cout << \"--- Processing: \" << theTree->GetEntries() << \" events\" << std::endl;\n", "TStopwatch sw;\n", "sw.Start();\n", "\n", "for (Long64_t ievt=0; ievtGetEntries();ievt++) {\n", " if (ievt%1000 == 0){\n", " std::cout << \"--- ... Processing event: \" << ievt << std::endl;\n", " }\n", " theTree->GetEntry(ievt);\n", "\n", " if (Use[\"MLP\"])\n", " histMLP_signal->Fill((reader->EvaluateMulticlass( \"MLP method\" ))[0]);\n", " if (Use[\"BDTG\"])\n", " histBDTG_signal->Fill((reader->EvaluateMulticlass( \"BDTG method\" ))[0]);\n", " if (Use[\"DL_CPU\"])\n", " histDLCPU_signal->Fill((reader->EvaluateMulticlass(\"DL_CPU method\"))[0]);\n", " if (Use[\"DL_GPU\"])\n", " histDLGPU_signal->Fill((reader->EvaluateMulticlass(\"DL_GPU method\"))[0]);\n", " if (Use[\"FDA_GA\"])\n", " histFDAGA_signal->Fill((reader->EvaluateMulticlass( \"FDA_GA method\" ))[0]);\n", " if (Use[\"PDEFoam\"])\n", " histPDEFoam_signal->Fill((reader->EvaluateMulticlass( \"PDEFoam method\" ))[0]);\n", "\n", "}" ] }, { "cell_type": "markdown", "id": "b945b940", "metadata": {}, "source": [ "get elapsed time" ] }, { "cell_type": "code", "execution_count": 11, "id": "9a77ff6a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:19.849159Z", "iopub.status.busy": "2026-05-19T20:24:19.849023Z", "iopub.status.idle": "2026-05-19T20:24:20.055904Z", "shell.execute_reply": "2026-05-19T20:24:20.055583Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- End of event loop: Real time 0:00:00, CP time 0.330\n", "--- Created root file: \"TMVMulticlassApp.root\" containing the MVA output histograms\n", "==> TMVAMulticlassApp is done!\n", "\n" ] } ], "source": [ "sw.Stop();\n", "std::cout << \"--- End of event loop: \"; sw.Print();\n", "\n", "TFile *target = new TFile( \"TMVAMulticlassApp.root\",\"RECREATE\" );\n", "if (Use[\"MLP\"])\n", " histMLP_signal->Write();\n", "if (Use[\"BDTG\"])\n", " histBDTG_signal->Write();\n", "if (Use[\"DL_CPU\"])\n", " histDLCPU_signal->Write();\n", "if (Use[\"DL_GPU\"])\n", " histDLGPU_signal->Write();\n", "if (Use[\"FDA_GA\"])\n", " histFDAGA_signal->Write();\n", "if (Use[\"PDEFoam\"])\n", " histPDEFoam_signal->Write();\n", "\n", "target->Close();\n", "std::cout << \"--- Created root file: \\\"TMVMulticlassApp.root\\\" containing the MVA output histograms\" << std::endl;\n", "\n", "delete reader;\n", "\n", "std::cout << \"==> TMVAMulticlassApp is done!\" << std::endl << std::endl;" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }