ROOT
master
Reference Guide
Loading...
Searching...
No Matches
TMVA_SOFIE_RDataFrame.py
Go to the documentation of this file.
1
### \file
2
### \ingroup tutorial_ml
3
### \notebook -nodraw
4
### Example of inference with SOFIE and RDataFrame, of a model trained with Keras.
5
### First, generate the input model by running `TMVA_Higgs_Classification.C`.
6
###
7
### This tutorial parses the input model and runs the inference using ROOT's JITing capability.
8
###
9
### \macro_code
10
### \macro_output
11
### \author Lorenzo Moneta
12
13
from
os.path
import
exists
14
15
import
ROOT
16
17
# check if the input file exists
18
modelFile =
"Higgs_trained_model.h5"
19
modelName =
"Higgs_trained_model"
20
21
if
not
exists(modelFile):
22
raise
FileNotFoundError
(
"You need to run TMVA_Higgs_Classification.C to generate the Keras trained model"
)
23
24
# parse the input Keras model into RModel object
25
model =
ROOT.TMVA.Experimental.SOFIE.PyKeras.Parse
(modelFile)
26
27
# generating inference code
28
model.Generate
()
29
model.OutputGenerated
(
"Higgs_trained_model_generated.hxx"
)
30
model.PrintGenerated
()
31
32
# compile using ROOT JIT trained model
33
print(
"compiling SOFIE model and functor...."
)
34
ROOT.gInterpreter.Declare
(
'#include "Higgs_trained_model_generated.hxx"'
)
35
ROOT.gInterpreter.Declare
(
'auto sofie_functor = TMVA::Experimental::SofieFunctor<7,TMVA_SOFIE_'
+modelName+
'::Session>(0,"Higgs_trained_model_generated.dat");'
)
36
37
# run inference over input data
38
inputFile = str(
ROOT.gROOT.GetTutorialDir
()) +
"/machine_learning/data/Higgs_data.root"
39
df1 =
ROOT.RDataFrame
(
"sig_tree"
, inputFile)
40
h1 =
df1.Define
(
"DNN_Value"
,
"sofie_functor(rdfslot_,m_jj, m_jjj, m_lv, m_jlv, m_bb, m_wbb, m_wwbb)"
).Histo1D((
"h_sig"
,
""
, 100, 0, 1),
"DNN_Value"
)
41
42
df2 =
ROOT.RDataFrame
(
"bkg_tree"
, inputFile)
43
h2 =
df2.Define
(
"DNN_Value"
,
"sofie_functor(rdfslot_,m_jj, m_jjj, m_lv, m_jlv, m_bb, m_wbb, m_wwbb)"
).Histo1D((
"h_bkg"
,
""
, 100, 0, 1),
"DNN_Value"
)
44
45
# run over the input data once, combining both RDataFrame graphs.
46
ROOT.RDF.RunGraphs
([h1, h2])
47
48
print(
"Number of signal entries"
,
h1.GetEntries
())
49
print(
"Number of background entries"
,
h2.GetEntries
())
50
51
h1.SetLineColor
(
"kRed"
)
52
h2.SetLineColor
(
"kBlue"
)
53
54
c1 =
ROOT.TCanvas
()
55
ROOT.gStyle.SetOptStat
(0)
56
57
h2.DrawClone
()
58
h1.DrawClone
(
"SAME"
)
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:360
ROOT::Detail::TRangeCast
Definition
TCollection.h:313
ROOT::RDataFrame
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
Definition
RDataFrame.hxx:44
tutorials
machine_learning
TMVA_SOFIE_RDataFrame.py
ROOT master - Reference Guide Generated on Wed Dec 10 2025 13:59:13 (GVA Time) using Doxygen 1.10.0