This macro provides an example of using a trained model with PyTorch and make inference using SOFIE and RDataFrame This macro uses as input the SOFIE header generated from the ONNX model with the TMVA_SOFIE_PyTorch_HiggsModel.py tutorial You need to run that macro before this one.
In this case we are parsing the input file and then run the inference in the same macro making use of the ROOT JITing capability
{
std::string(
"#include \"") +
headerModelFile + std::string(
"\"\n#include <array>\n#include <vector>");
throw std::runtime_error(
"Error compiling : " +
cmd);
std::cout <<
"compiled : " <<
cmd << std::endl;
cmd =
"std::vector<TMVA_SOFIE_" + modelName +
"::Session> sofie_sessions(" + std::to_string(
nslots) +
");";
throw std::runtime_error(
"Error compiling : " +
cmd);
std::string params;
for (
unsigned int i = 0; i <
ninputs; i++) {
if (i > 0) {
params += ", ";
}
params += "float x" + std::to_string(i);
}
cmd =
"double sofie_eval(unsigned int slot, " + params +
") {\n"
" std::array<float, " +
"};\n"
" return sofie_sessions[slot].infer(input.data())[0];\n"
"}";
throw std::runtime_error(
"Error compiling : " +
cmd);
std::cout <<
"compiled : " <<
cmd << std::endl;
std::cout << "Model is ready to be evaluated" << std::endl;
return;
}
Info(
"TMVA_SOFIE_RDataFrame",
"You need to run TMVA_SOFIE_PyTorch_HiggsModel.py to generate the SOFIE header "
"for the PyTorch trained model");
return;
}
Error(
"TMVA_SOFIE_RDataFrame",
"Generated weight file is missing");
return;
}
auto h1 =
df1.Define(
"DNN_Value",
"sofie_eval(rdfslot_,m_jj, m_jjj, m_lv, m_jlv, m_bb, m_wbb, m_wwbb)")
.Histo1D({"h_sig", "", 100, 0, 1}, "DNN_Value");
auto h2 =
df2.Define(
"DNN_Value",
"sofie_eval(rdfslot_,m_jj, m_jjj, m_lv, m_jlv, m_bb, m_wbb, m_wwbb)")
.Histo1D({"h_bkg", "", 100, 0, 1}, "DNN_Value");
h2->DrawClone();
}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
R__EXTERN TStyle * gStyle
R__EXTERN TSystem * gSystem
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
virtual void SetLineColor(Color_t lcolor)
Set the line color.
virtual TObject * DrawClone(Option_t *option="") const
Draw a clone of this object in the current selected pad with: gROOT->SetSelectedPad(c1).
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
modelName
Step 2 : Parse model and generate inference code with SOFIE.
compiled : #include "HiggsModel.hxx"
#include <array>
#include <vector>
compiled : double sofie_eval(unsigned int slot, float x0, float x1, float x2, float x3, float x4, float x5, float x6) {
std::array<float, 7> input{x0, x1, x2, x3, x4, x5, x6};
return sofie_sessions[slot].infer(input.data())[0];
}
Model is ready to be evaluated
- Author
- Lorenzo Moneta
Definition in file TMVA_SOFIE_RDataFrame_JIT.C.