{ "cells": [ { "cell_type": "markdown", "id": "171247db", "metadata": {}, "source": [ "# TMVAClassificationCategoryApplication\n", "This macro provides a simple example on how to use the trained classifiers\n", "(with categories) within an analysis module\n", "- Project : TMVA - a Root-integrated toolkit for multivariate data analysis\n", "- Package : TMVA\n", "- Executable: TMVAClassificationCategoryApplication\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:23 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "2bda4f85", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:02.194346Z", "iopub.status.busy": "2026-05-19T20:24:02.194206Z", "iopub.status.idle": "2026-05-19T20:24:02.243686Z", "shell.execute_reply": "2026-05-19T20:24:02.230653Z" } }, "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 \"TH1F.h\"\n", "#include \"TStopwatch.h\"\n", "\n", "#include \"TMVA/Tools.h\"\n", "#include \"TMVA/Reader.h\"\n", "#include \"TMVA/MethodCuts.h\"\n", "\n", "Bool_t UseOffsetMethod = kTRUE;" ] }, { "cell_type": "markdown", "id": "52f103c2", "metadata": {}, "source": [ "---------------------------------------------------------------\n", "default MVA methods to be trained + tested" ] }, { "cell_type": "code", "execution_count": 2, "id": "7122e9cb", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:02.266167Z", "iopub.status.busy": "2026-05-19T20:24:02.265996Z", "iopub.status.idle": "2026-05-19T20:24:02.855456Z", "shell.execute_reply": "2026-05-19T20:24:02.854842Z" } }, "outputs": [], "source": [ "std::map Use;" ] }, { "cell_type": "code", "execution_count": 3, "id": "58a66e18", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:02.871239Z", "iopub.status.busy": "2026-05-19T20:24:02.871097Z", "iopub.status.idle": "2026-05-19T20:24:03.135229Z", "shell.execute_reply": "2026-05-19T20:24:03.119942Z" } }, "outputs": [], "source": [ "Use[\"LikelihoodCat\"] = 1;\n", "Use[\"FisherCat\"] = 1;" ] }, { "cell_type": "markdown", "id": "5db7dd87", "metadata": {}, "source": [ "---------------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": 4, "id": "bfb7130d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:03.163179Z", "iopub.status.busy": "2026-05-19T20:24:03.163008Z", "iopub.status.idle": "2026-05-19T20:24:03.409053Z", "shell.execute_reply": "2026-05-19T20:24:03.408428Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "==> Start TMVAClassificationCategoryApplication\n" ] } ], "source": [ "std::cout << std::endl\n", " << \"==> Start TMVAClassificationCategoryApplication\" << std::endl;" ] }, { "cell_type": "markdown", "id": "546edeeb", "metadata": {}, "source": [ "Create the Reader object" ] }, { "cell_type": "code", "execution_count": 5, "id": "034b025d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:03.427062Z", "iopub.status.busy": "2026-05-19T20:24:03.426910Z", "iopub.status.idle": "2026-05-19T20:24:03.633361Z", "shell.execute_reply": "2026-05-19T20:24:03.632933Z" } }, "outputs": [], "source": [ "TMVA::Reader *reader = new TMVA::Reader( \"!Color:!Silent\" );" ] }, { "cell_type": "markdown", "id": "56770c47", "metadata": {}, "source": [ "Create a set of variables and spectators and declare them to the reader\n", "- the variable names MUST corresponds in name and type to those given in the weight file(s) used" ] }, { "cell_type": "code", "execution_count": 6, "id": "562a61af", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:03.659422Z", "iopub.status.busy": "2026-05-19T20:24:03.659242Z", "iopub.status.idle": "2026-05-19T20:24:03.871038Z", "shell.execute_reply": "2026-05-19T20:24:03.870622Z" } }, "outputs": [], "source": [ "Float_t var1, var2, var3, var4, eta;\n", "reader->AddVariable( \"var1\", &var1 );\n", "reader->AddVariable( \"var2\", &var2 );\n", "reader->AddVariable( \"var3\", &var3 );\n", "reader->AddVariable( \"var4\", &var4 );\n", "\n", "reader->AddSpectator( \"eta\", &eta );" ] }, { "cell_type": "markdown", "id": "96ec9ae7", "metadata": {}, "source": [ "Book the MVA methods" ] }, { "cell_type": "code", "execution_count": 7, "id": "421bcec5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:03.905384Z", "iopub.status.busy": "2026-05-19T20:24:03.905232Z", "iopub.status.idle": "2026-05-19T20:24:04.108934Z", "shell.execute_reply": "2026-05-19T20:24:04.108575Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " : Booking \"FisherCat method\" of type \"Category\" from dataset/weights/TMVAClassificationCategory_FisherCat.weights.xml.\n", " : Reading weight file: dataset/weights/TMVAClassificationCategory_FisherCat.weights.xml\n", "
DataSetInfo : [Default] : Added class \"Signal\"\n", "
DataSetInfo : [Default] : Added class \"Background\"\n", " : Recreating sub-classifiers from XML-file \n", "
DataSetInfo : [Category_Fisher_1_dsi] : Added class \"Signal\"\n", "
DataSetInfo : [Category_Fisher_1_dsi] : Added class \"Background\"\n", "
DataSetInfo : [Category_Fisher_2_dsi] : Added class \"Signal\"\n", "
DataSetInfo : [Category_Fisher_2_dsi] : Added class \"Background\"\n", " : Booked classifier \"FisherCat\" of type: \"Category\"\n", " : Booking \"LikelihoodCat method\" of type \"Category\" from dataset/weights/TMVAClassificationCategory_LikelihoodCat.weights.xml.\n", " : Reading weight file: dataset/weights/TMVAClassificationCategory_LikelihoodCat.weights.xml\n", " : Recreating sub-classifiers from XML-file \n", "
DataSetInfo : [Category_Likelihood_1_dsi] : Added class \"Signal\"\n", "
DataSetInfo : [Category_Likelihood_1_dsi] : Added class \"Background\"\n", "
DataSetInfo : [Category_Likelihood_2_dsi] : Added class \"Signal\"\n", "
DataSetInfo : [Category_Likelihood_2_dsi] : Added class \"Background\"\n", " : Booked classifier \"LikelihoodCat\" of type: \"Category\"\n" ] } ], "source": [ "for (std::map::iterator it = Use.begin(); it != Use.end(); it++) {\n", " if (it->second) {\n", " TString methodName = it->first + \" method\";\n", " TString weightfile = \"dataset/weights/TMVAClassificationCategory_\" + TString(it->first) + \".weights.xml\";\n", " reader->BookMVA( methodName, weightfile );\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "4776e3b0", "metadata": {}, "source": [ "Book output histograms" ] }, { "cell_type": "code", "execution_count": 8, "id": "036b1b78", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:04.123434Z", "iopub.status.busy": "2026-05-19T20:24:04.123292Z", "iopub.status.idle": "2026-05-19T20:24:04.333570Z", "shell.execute_reply": "2026-05-19T20:24:04.332988Z" } }, "outputs": [], "source": [ "UInt_t nbin = 100;\n", "std::map hist;\n", "hist[\"LikelihoodCat\"] = new TH1F( \"MVA_LikelihoodCat\", \"MVA_LikelihoodCat\", nbin, -1, 0.9999 );\n", "hist[\"FisherCat\"] = new TH1F( \"MVA_FisherCat\", \"MVA_FisherCat\", nbin, -4, 4 );" ] }, { "cell_type": "markdown", "id": "8a417608", "metadata": {}, "source": [ "Prepare input tree (this must be replaced by your data source)\n", "in this example, there is a toy tree with signal and one with background events\n", "we'll later on use only the \"signal\" events for the test in this example." ] }, { "cell_type": "code", "execution_count": 9, "id": "f67238ff", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:04.335714Z", "iopub.status.busy": "2026-05-19T20:24:04.335547Z", "iopub.status.idle": "2026-05-19T20:24:04.537847Z", "shell.execute_reply": "2026-05-19T20:24:04.537256Z" } }, "outputs": [], "source": [ "TString fname = gSystem->GetDirName(__FILE__) + \"/data/\";" ] }, { "cell_type": "markdown", "id": "44ffc3c9", "metadata": {}, "source": [ "if directory data not found try using tutorials dir" ] }, { "cell_type": "code", "execution_count": 10, "id": "7378cbcd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:04.540008Z", "iopub.status.busy": "2026-05-19T20:24:04.539866Z", "iopub.status.idle": "2026-05-19T20:24:04.750191Z", "shell.execute_reply": "2026-05-19T20:24:04.749732Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- TMVAClassificationApp : Accessing /github/home/ROOT-CI/build/tutorials/machine_learning/data/toy_sigbkg_categ_offset.root!\n" ] } ], "source": [ "if (gSystem->AccessPathName( fname + \"toy_sigbkg_categ_offset.root\" )) {\n", " fname = gROOT->GetTutorialDir() + \"/machine_learning/data/\";\n", "}\n", "if (UseOffsetMethod) fname += \"toy_sigbkg_categ_offset.root\";\n", "else fname += \"toy_sigbkg_categ_varoff.root\";\n", "std::cout << \"--- TMVAClassificationApp : Accessing \" << fname << \"!\" << std::endl;\n", "TFile *input = TFile::Open(fname);\n", "if (!input) {\n", " std::cout << \"ERROR: could not open data file: \" << fname << std::endl;\n", " exit(1);\n", "}" ] }, { "cell_type": "markdown", "id": "fa6c1831", "metadata": {}, "source": [ "Event loop" ] }, { "cell_type": "markdown", "id": "536d96b7", "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": 11, "id": "b4a0940c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:04.752104Z", "iopub.status.busy": "2026-05-19T20:24:04.751962Z", "iopub.status.idle": "2026-05-19T20:24:04.978052Z", "shell.execute_reply": "2026-05-19T20:24:04.970633Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Use signal sample for evaluation\n", "--- Processing: 10000 events\n", "--- ... Processing event: 0\n", " : Rebuilding Dataset Default\n", "--- ... Processing event: 1000\n", "--- ... Processing event: 2000\n", "--- ... Processing event: 3000\n", "--- ... Processing event: 4000\n", "--- ... Processing event: 5000\n", "--- ... Processing event: 6000\n", "--- ... Processing event: 7000\n", "--- ... Processing event: 8000\n", "--- ... Processing event: 9000\n", "--- End of event loop: Real time 0:00:00, CP time 0.040\n" ] } ], "source": [ "TTree* theTree = (TTree*)input->Get(\"TreeS\");\n", "std::cout << \"--- Use signal sample for evaluation\" << std::endl;\n", "theTree->SetBranchAddress( \"var1\", &var1 );\n", "theTree->SetBranchAddress( \"var2\", &var2 );\n", "theTree->SetBranchAddress( \"var3\", &var3 );\n", "theTree->SetBranchAddress( \"var4\", &var4 );\n", "\n", "theTree->SetBranchAddress( \"eta\", &eta ); // spectator\n", "\n", "std::cout << \"--- Processing: \" << theTree->GetEntries() << \" events\" << std::endl;\n", "TStopwatch sw;\n", "sw.Start();\n", "for (Long64_t ievt=0; ievtGetEntries();ievt++) {\n", "\n", " if (ievt%1000 == 0) std::cout << \"--- ... Processing event: \" << ievt << std::endl;\n", "\n", " theTree->GetEntry(ievt);\n", "\n", " // Return the MVA outputs and fill into histograms\n", "\n", " for (std::map::iterator it = Use.begin(); it != Use.end(); it++) {\n", " if (!it->second) continue;\n", " TString methodName = it->first + \" method\";\n", " hist[it->first]->Fill( reader->EvaluateMVA( methodName ) );\n", " }\n", "\n", "}\n", "sw.Stop();\n", "std::cout << \"--- End of event loop: \"; sw.Print();" ] }, { "cell_type": "markdown", "id": "28582eab", "metadata": {}, "source": [ "Write histograms" ] }, { "cell_type": "code", "execution_count": 12, "id": "53daaf18", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:04.980290Z", "iopub.status.busy": "2026-05-19T20:24:04.980153Z", "iopub.status.idle": "2026-05-19T20:24:05.193965Z", "shell.execute_reply": "2026-05-19T20:24:05.193463Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Created root file: \"TMVApp.root\" containing the MVA output histograms\n", "==> TMVAClassificationApplication is done!\n", "\n" ] } ], "source": [ "TFile *target = new TFile( \"TMVApp.root\",\"RECREATE\" );\n", "for (std::map::iterator it = Use.begin(); it != Use.end(); it++)\n", " if (it->second) hist[it->first]->Write();\n", "\n", "target->Close();\n", "std::cout << \"--- Created root file: \\\"TMVApp.root\\\" containing the MVA output histograms\" << std::endl;\n", "\n", "delete reader;\n", "std::cout << \"==> TMVAClassificationApplication 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 }