{
"cells": [
{
"cell_type": "markdown",
"id": "195e0a28",
"metadata": {},
"source": [
"# TMVA_SOFIE_RDataFrame_JIT\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",
"TMVA_Higgs_Classification.C tutorial\n",
"You need to run that macro before this one.\n",
"In this case we are parsing the input file and then run the inference in the same\n",
"macro making use of the ROOT JITing capability\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": "93723b5d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:23:58.542048Z",
"iopub.status.busy": "2026-05-19T20:23:58.541913Z",
"iopub.status.idle": "2026-05-19T20:23:59.023050Z",
"shell.execute_reply": "2026-05-19T20:23:59.022341Z"
}
},
"outputs": [],
"source": [
"using namespace TMVA::Experimental;"
]
},
{
"cell_type": "markdown",
"id": "649825df",
"metadata": {},
"source": [
" function to compile the generated model and the declaration of the SofieFunctor\n",
"used by RDF.\n",
"Assume that the model name as in the header file\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f72ea418",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:23:59.036211Z",
"iopub.status.busy": "2026-05-19T20:23:59.036080Z",
"iopub.status.idle": "2026-05-19T20:23:59.106374Z",
"shell.execute_reply": "2026-05-19T20:23:59.095642Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void CompileModelForRDF(const std::string & headerModelFile, unsigned int ninputs, unsigned int nslots=0) {\n",
"\n",
" std::string modelName = headerModelFile.substr(0,headerModelFile.find(\".hxx\"));\n",
" std::string cmd = std::string(\"#include \\\"\") + headerModelFile + std::string(\"\\\"\");\n",
" auto ret = gInterpreter->Declare(cmd.c_str());\n",
" if (!ret)\n",
" throw std::runtime_error(\"Error compiling : \" + cmd);\n",
" std::cout << \"compiled : \" << cmd << std::endl;\n",
"\n",
" cmd = \"auto sofie_functor = TMVA::Experimental::SofieFunctor<\" + std::to_string(ninputs) + \",TMVA_SOFIE_\" +\n",
" modelName + \"::Session>(\" + std::to_string(nslots) + \");\";\n",
" ret = gInterpreter->Declare(cmd.c_str());\n",
" if (!ret)\n",
" throw std::runtime_error(\"Error compiling : \" + cmd);\n",
" std::cout << \"compiled : \" << cmd << std::endl;\n",
" std::cout << \"Model is ready to be evaluated\" << std::endl;\n",
" return;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "bff08bf2",
"metadata": {},
"source": [
" Arguments are defined. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dbf3a5ec",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:23:59.108009Z",
"iopub.status.busy": "2026-05-19T20:23:59.107883Z",
"iopub.status.idle": "2026-05-19T20:23:59.316646Z",
"shell.execute_reply": "2026-05-19T20:23:59.316189Z"
}
},
"outputs": [],
"source": [
"std::string modelName = \"HiggsModel\";"
]
},
{
"cell_type": "markdown",
"id": "e97c9b59",
"metadata": {},
"source": [
"check if the input file exists"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "78c137c6",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:23:59.327571Z",
"iopub.status.busy": "2026-05-19T20:23:59.327447Z",
"iopub.status.idle": "2026-05-19T20:23:59.540593Z",
"shell.execute_reply": "2026-05-19T20:23:59.540165Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : You need to run TMVA_SOFIE_Keras_Higgs_Model.py to generate the SOFIE header for the Keras trained model\n"
]
}
],
"source": [
"std::string modelHeaderFile = modelName + \".hxx\";\n",
"if (gSystem->AccessPathName(modelHeaderFile.c_str())) {\n",
" Info(\"TMVA_SOFIE_RDataFrame\",\"You need to run TMVA_SOFIE_Keras_Higgs_Model.py to generate the SOFIE header for the Keras trained model\");\n",
" return;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "b5ba0784",
"metadata": {},
"source": [
"check that also weigh file exists"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b5bd3f28",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:23:59.586788Z",
"iopub.status.busy": "2026-05-19T20:23:59.586590Z",
"iopub.status.idle": "2026-05-19T20:23:59.792873Z",
"shell.execute_reply": "2026-05-19T20:23:59.791361Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Error in : Generated weight file is missing\n"
]
}
],
"source": [
"std::string modelWeightFile = modelName + std::string(\".dat\");\n",
"if (gSystem->AccessPathName(modelWeightFile.c_str())) {\n",
" Error(\"TMVA_SOFIE_RDataFrame\",\"Generated weight file is missing\");\n",
" return;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "9f4aed94",
"metadata": {},
"source": [
"now compile using ROOT JIT trained model (see function above)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "618df4f9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:23:59.794277Z",
"iopub.status.busy": "2026-05-19T20:23:59.794152Z",
"iopub.status.idle": "2026-05-19T20:24:00.314938Z",
"shell.execute_reply": "2026-05-19T20:24:00.314225Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_58:1:10: fatal error: 'HiggsModel.hxx' file not found\n",
"#include \"HiggsModel.hxx\"\n",
" ^~~~~~~~~~~~~~~~\n"
]
}
],
"source": [
"CompileModelForRDF(modelHeaderFile,7);\n",
"\n",
"std::string inputFileName = \"Higgs_data.root\";\n",
"std::string inputFile = std::string{gROOT->GetTutorialDir()} + \"/machine_learning/data/\" + inputFileName;\n",
"\n",
"ROOT::RDataFrame df1(\"sig_tree\", inputFile);\n",
"auto h1 = df1.Define(\"DNN_Value\", \"sofie_functor(rdfslot_,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",
"auto h2 = df2.Define(\"DNN_Value\", \"sofie_functor(rdfslot_,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
}