{ "cells": [ { "cell_type": "markdown", "id": "39d8b9c2", "metadata": {}, "source": [ "# tmva103_Application\n", "This tutorial illustrates how you can conveniently apply BDTs in C++ using\n", "the fast tree inference engine offered by TMVA. Supported workflows are\n", "event-by-event inference, batch inference and pipelines with RDataFrame.\n", "\n", "\n", "\n", "\n", "**Author:** Stefan Wunsch \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:22 PM." ] }, { "cell_type": "code", "execution_count": null, "id": "d4661a77", "metadata": { "collapsed": false }, "outputs": [], "source": [ "using namespace TMVA::Experimental;" ] }, { "cell_type": "code", "execution_count": null, "id": "536a7d5a", "metadata": { "collapsed": false }, "outputs": [], "source": [ "const char* model_filename = \"tmva101.root\";\n", "\n", "if (gSystem->AccessPathName(model_filename)) {\n", " Info(\"tmva103_Application.C\", \"%s does not exist\", model_filename);\n", " return;\n", "}" ] }, { "cell_type": "markdown", "id": "045347fa", "metadata": {}, "source": [ "Load BDT model" ] }, { "cell_type": "code", "execution_count": null, "id": "042e47c7", "metadata": { "collapsed": false }, "outputs": [], "source": [ "RBDT bdt(\"myBDT\", model_filename);" ] }, { "cell_type": "markdown", "id": "2713893f", "metadata": {}, "source": [ "Apply model on a single input" ] }, { "cell_type": "code", "execution_count": null, "id": "84fe481f", "metadata": { "collapsed": false }, "outputs": [], "source": [ "auto y1 = bdt.Compute({1.0, 2.0, 3.0, 4.0});\n", "\n", "std::cout << \"Apply model on a single input vector: \" << y1[0] << std::endl;" ] }, { "cell_type": "markdown", "id": "727b430f", "metadata": {}, "source": [ "Apply model on a batch of inputs" ] }, { "cell_type": "code", "execution_count": null, "id": "936e4bca", "metadata": { "collapsed": false }, "outputs": [], "source": [ "float data[8] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};\n", "RTensor x(data, {2, 4});\n", "auto y2 = bdt.Compute(x);\n", "\n", "std::cout << \"Apply model on an input tensor: \" << y2 << std::endl;" ] }, { "cell_type": "markdown", "id": "e9957c36", "metadata": {}, "source": [ "Apply model as part of an RDataFrame workflow" ] }, { "cell_type": "code", "execution_count": null, "id": "c641b006", "metadata": { "collapsed": false }, "outputs": [], "source": [ "ROOT::RDataFrame df(\"Events\", \"root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoaod/SMHiggsToZZTo4L.root\");\n", "auto df2 = df.Filter(\"nMuon >= 2\")\n", " .Filter(\"nElectron >= 2\")\n", " .Define(\"Muon_pt_1\", \"Muon_pt[0]\")\n", " .Define(\"Muon_pt_2\", \"Muon_pt[1]\")\n", " .Define(\"Electron_pt_1\", \"Electron_pt[0]\")\n", " .Define(\"Electron_pt_2\", \"Electron_pt[1]\")\n", " .Define(\"y\",\n", " Compute<4, float>(bdt),\n", " {\"Muon_pt_1\", \"Muon_pt_2\", \"Electron_pt_1\", \"Electron_pt_2\"});\n", "\n", "std::cout << \"Mean response on the signal sample: \" << *df2.Mean(\"y\") << 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 }