{ "cells": [ { "cell_type": "markdown", "id": "1966fa1c", "metadata": {}, "source": [ "# TMVA_SOFIE_RDataFrame\n", "This macro provides an example of using a trained model with Keras\n", "and make inference using SOFIE and RDataFrame\n", "This macro uses as input a Keras model generated with the\n", "Python tutorial TMVA_SOFIE_Keras_HiggsModel.py\n", "You need to run that macro before to generate the trained Keras model\n", "and also the corresponding header file with SOFIE which can then be used for inference\n", "\n", "Execute in this order:\n", "```\n", "python3 TMVA_SOFIE_Keras_HiggsModel.py\n", "root TMVA_SOFIE_RDataFrame.C\n", "```\n", "\n", "\n", "\n", "**Author:** Lorenzo Moneta \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": "71590d9f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:23:50.694370Z", "iopub.status.busy": "2026-05-19T20:23:50.694232Z", "iopub.status.idle": "2026-05-19T20:23:51.168975Z", "shell.execute_reply": "2026-05-19T20:23:51.164715Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_44:5:10: fatal error: 'HiggsModel.hxx' file not found\n", "#include \"HiggsModel.hxx\"\n", " ^~~~~~~~~~~~~~~~\n" ] } ], "source": [ "using namespace TMVA::Experimental;\n", "\n", "R__ADD_INCLUDE_PATH($PWD)\n", "#include \"HiggsModel.hxx\"\n", "#include \"TMVA/SOFIEHelpers.hxx\"\n", "\n", "using namespace TMVA::Experimental;" ] }, { "cell_type": "markdown", "id": "1b135cc8", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 2, "id": "845485a0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:23:51.190097Z", "iopub.status.busy": "2026-05-19T20:23:51.189942Z", "iopub.status.idle": "2026-05-19T20:23:51.426968Z", "shell.execute_reply": "2026-05-19T20:23:51.417145Z" } }, "outputs": [], "source": [ "int nthreads = 2;" ] }, { "cell_type": "code", "execution_count": 3, "id": "3461392f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:23:51.434981Z", "iopub.status.busy": "2026-05-19T20:23:51.434848Z", "iopub.status.idle": "2026-05-19T20:23:51.658565Z", "shell.execute_reply": "2026-05-19T20:23:51.657881Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:10:55: error: 'TMVA_SOFIE_HiggsModel' is not a class, namespace, or enumeration\n", "auto h1 = df1.DefineSlot(\"DNN_Value\", SofieFunctor<7, TMVA_SOFIE_HiggsModel::Session>(nslots),\n", " ^\n", "input_line_54:10:55: note: 'TMVA_SOFIE_HiggsModel' declared here\n", "input_line_54:16:55: error: use of undeclared identifier 'TMVA_SOFIE_HiggsModel'\n", "auto h2 = df2.DefineSlot(\"DNN_Value\", SofieFunctor<7, TMVA_SOFIE_HiggsModel::Session>(nslots),\n", " ^\n" ] } ], "source": [ "std::string inputFileName = \"Higgs_data.root\";\n", "std::string inputFile = std::string{gROOT->GetTutorialDir()} + \"/machine_learning/data/\" + inputFileName;\n", "\n", "ROOT::EnableImplicitMT(nthreads);\n", "\n", "ROOT::RDataFrame df1(\"sig_tree\", inputFile);\n", "int nslots = df1.GetNSlots();\n", "std::cout << \"Running using \" << nslots << \" threads\" << std::endl;\n", "auto h1 = df1.DefineSlot(\"DNN_Value\", SofieFunctor<7, TMVA_SOFIE_HiggsModel::Session>(nslots),\n", " {\"m_jj\", \"m_jjj\", \"m_lv\", \"m_jlv\", \"m_bb\", \"m_wbb\", \"m_wwbb\"})\n", " .Histo1D({\"h_sig\", \"\", 100, 0, 1}, \"DNN_Value\");\n", "\n", "ROOT::RDataFrame df2(\"bkg_tree\", inputFile);\n", "nslots = df2.GetNSlots();\n", "auto h2 = df2.DefineSlot(\"DNN_Value\", SofieFunctor<7, TMVA_SOFIE_HiggsModel::Session>(nslots),\n", " {\"m_jj\", \"m_jjj\", \"m_lv\", \"m_jlv\", \"m_bb\", \"m_wbb\", \"m_wwbb\"})\n", " .Histo1D({\"h_bkg\", \"\", 100, 0, 1}, \"DNN_Value\");\n", "\n", "h1->SetLineColor(kRed);\n", "h2->SetLineColor(kBlue);\n", "\n", "auto c1 = new TCanvas();\n", "gStyle->SetOptStat(0);\n", "\n", "h2->DrawClone();\n", "h1->DrawClone(\"SAME\");\n", "c1->BuildLegend();" ] } ], "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 }