15namespace Experimental {
44 auto xmldoc = xml.
ParseFile(filename.c_str());
47 ss <<
"Failed to open TMVA XML file "
49 throw std::runtime_error(ss.str());
52 for (
auto node = xml.
GetChild(mainNode); node; node = xml.
GetNext(node)) {
53 const auto nodeName = std::string(xml.
GetNodeName(node));
55 if (nodeName.compare(
"Variables") == 0) {
56 c.numVariables = std::atoi(xml.
GetAttr(node,
"NVar"));
57 c.variables = std::vector<std::string>(
c.numVariables);
58 c.expressions = std::vector<std::string>(
c.numVariables);
59 for (
auto thisNode = xml.
GetChild(node); thisNode; thisNode = xml.
GetNext(thisNode)) {
60 const auto iVariable = std::atoi(xml.
GetAttr(thisNode,
"VarIndex"));
61 c.variables[iVariable] = xml.
GetAttr(thisNode,
"Title");
62 c.expressions[iVariable] = xml.
GetAttr(thisNode,
"Expression");
66 else if (nodeName.compare(
"Classes") == 0) {
67 c.numClasses = std::atoi(xml.
GetAttr(node,
"NClass"));
68 for (
auto thisNode = xml.
GetChild(node); thisNode; thisNode = xml.
GetNext(thisNode)) {
69 c.classes.push_back(xml.
GetAttr(thisNode,
"Name"));
73 else if (nodeName.compare(
"GeneralInfo") == 0) {
74 std::string analysisType =
"";
75 for (
auto thisNode = xml.
GetChild(node); thisNode; thisNode = xml.
GetNext(thisNode)) {
76 if (std::string(
"AnalysisType").compare(xml.
GetAttr(thisNode,
"name")) == 0) {
77 analysisType = xml.
GetAttr(thisNode,
"value");
80 if (analysisType.compare(
"Classification") == 0) {
82 }
else if (analysisType.compare(
"Regression") == 0) {
84 }
else if (analysisType.compare(
"Multiclass") == 0) {
92 if (
c.numVariables !=
c.variables.size() ||
c.numVariables == 0) {
94 ss <<
"Failed to parse input variables from TMVA config " << filename <<
".";
95 throw std::runtime_error(ss.str());
97 if (
c.numClasses !=
c.classes.size() ||
c.numClasses == 0) {
99 ss <<
"Failed to parse output classes from TMVA config " << filename <<
".";
100 throw std::runtime_error(ss.str());
103 std::stringstream ss;
104 ss <<
"Failed to parse analysis type from TMVA config " << filename <<
".";
105 throw std::runtime_error(ss.str());
136 fReader = std::make_unique<Reader>(
"Silent");
138 fValues = std::vector<float>(numVars);
139 for (std::size_t i = 0; i < numVars; i++) {
146 std::vector<float>
Compute(
const std::vector<float> &
x)
149 throw std::runtime_error(
"Size of input vector is not equal to number of variables.");
152 for (std::size_t i = 0; i <
x.size(); i++) {
162 return std::vector<float>({
static_cast<float>(
fReader->EvaluateMVA(
name))});
174 throw std::runtime_error(
"RReader has undefined analysis type.");
175 return std::vector<float>();
183 const auto shape =
x.GetShape();
184 if (shape.size() != 2)
185 throw std::runtime_error(
"Can only compute model outputs for input tensor of rank 2.");
187 const auto numEntries = shape[0];
188 const auto numVars = shape[1];
190 throw std::runtime_error(
"Second dimension of input tensor is not equal to number of variables.");
193 unsigned int numClasses = 1;
198 y =
y.Reshape({numEntries, numClasses});
201 for (std::size_t i = 0; i < numEntries; i++) {
202 for (std::size_t j = 0; j < numVars; j++) {
217 for (std::size_t k = 0; k < numClasses; k++)
#define R__WRITE_LOCKGUARD(mutex)
TMVA::Reader legacy interface.
RTensor< float > Compute(RTensor< float > &x)
Compute model prediction on input RTensor.
std::vector< float > fValues
std::vector< float > Compute(const std::vector< float > &x)
Compute model prediction on vector.
Internal::AnalysisType fAnalysisType
std::vector< std::string > fExpressions
std::vector< std::string > GetVariableNames()
std::vector< std::string > fVariables
RReader(const std::string &path)
Create TMVA model from XML file.
std::unique_ptr< Reader > fReader
RTensor is a container with contiguous memory and shape information.
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
returns first child of xmlnode
void FreeDoc(XMLDocPointer_t xmldoc)
frees allocated document data and deletes document itself
XMLNodePointer_t DocGetRootElement(XMLDocPointer_t xmldoc)
returns root node of document
const char * GetNodeName(XMLNodePointer_t xmlnode)
returns name of xmlnode
const char * GetAttr(XMLNodePointer_t xmlnode, const char *name)
returns value of attribute for xmlnode
XMLDocPointer_t ParseFile(const char *filename, Int_t maxbuf=100000)
Parses content of file and tries to produce xml structures.
XMLNodePointer_t GetNext(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
return next to xmlnode node if realnode==kTRUE, any special nodes in between will be skipped
R__EXTERN TVirtualRWMutex * gCoreMutex
XMLConfig ParseXMLConfig(const std::string &filename)
Parse TMVA XML config.
AnalysisType
Internal definition of analysis types.
create variable transformations
Container for information extracted from TMVA XML config.
std::vector< std::string > classes
unsigned int numVariables
std::vector< std::string > expressions
AnalysisType analysisType
std::vector< std::string > variables