Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tmva103_Application.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ml
3/// \notebook -nodraw
4/// This tutorial illustrates how you can conveniently apply BDTs in C++ using
5/// the fast tree inference engine offered by TMVA. Supported workflows are
6/// event-by-event inference, batch inference and pipelines with RDataFrame.
7///
8/// \macro_code
9/// \macro_output
10///
11/// \date December 2018
12/// \author Stefan Wunsch
13
14using namespace TMVA::Experimental;
15
17{
18 const char* model_filename = "tmva101.json";
19
21 Info("tmva103_Application.C", "%s does not exist", model_filename);
22 return;
23 }
24
25 // Load BDT model from the XGBoost JSON written by tmva101_Training.py
26 RBDT bdt = RBDT::LoadXGBoost(model_filename);
27
28 // Apply model on a single input
29 auto y1 = bdt.Compute({1.0, 2.0, 3.0, 4.0});
30
31 std::cout << "Apply model on a single input vector: " << y1[0] << std::endl;
32
33 // Apply model on a batch of inputs given as a flat, row-major array:
34 // 2 events with 4 variables each
35 float data[8] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
36 auto y2 = bdt.Compute(data, 4);
37
38 std::cout << "Apply model on a batch of inputs:";
39 for (const auto &output : y2)
40 std::cout << " " << output;
41 std::cout << std::endl;
42
43 // Apply model as part of an RDataFrame workflow
44 ROOT::RDataFrame df("Events", "root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoaod/SMHiggsToZZTo4L.root");
45 auto df2 = df.Filter("nMuon >= 2")
46 .Filter("nElectron >= 2")
47 .Define("Muon_pt_1", "Muon_pt[0]")
48 .Define("Muon_pt_2", "Muon_pt[1]")
49 .Define("Electron_pt_1", "Electron_pt[0]")
50 .Define("Electron_pt_2", "Electron_pt[1]")
51 .Define("y",
53 {"Muon_pt_1", "Muon_pt_2", "Electron_pt_1", "Electron_pt_2"});
54
55 std::cout << "Mean response on the signal sample: " << *df2.Mean("y") << std::endl;
56}
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.
Definition TError.cxx:241
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1312