Scan through variables in TMVA xml file

Hi all,

I’m trying to use TMVA::Reader to BookMVA methods and use them in a program built in C++.

The only issue is, I have many different models I am testing with many different variables. I don’t want to have to rebuild each time I use a different model for something. I would like to scan through the xml and see which variables exist and add only those to the reader before booking my model. Is this possible?

I can’t find a good XML reader in root. But I also see the TMVA::MethodBase has a “ReadVariablesFromXML” but I can’t initialise a BookMethod class because is it abstract.

Any advice appreciated

Cheers


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.23
Platform: lxplus
Compiler: Not Provided


Hi @lgolino,

Welcome back to the ROOT forum! I’m sure @moneta can shed some light on this.

Cheers,
J.

Thanks. I wondering whether to use a standard XML reader or of there is something built into TMVA libraries that will work better. Cheers

Hi,

You can use the functions in TMVA to read the XML file. First of all in the Reader when you book a method a pointer to the class is returned. You might need to downcast to MethodBase or to the specific method, if you want to call some of its member functions.

You can also look at reading the XML file directly using the TMVA::Tools::ReadAttr function, and you can look for example at the code used here in MethodBase on how it is used to read the different field of the XML file.

Lorenzo

Hi, thanks for your response

But if I’ve already booked the method then I must have already added the variables.

Can you find a list of classes derived from this class? I couldn’t find one earlier. I am using BDT.

Basically the pseudocode of what I’m looking for would be like this:

xmlreader = new TMVA::XMLReader(); //this class doesnt exist but its the class i would need to use.
reader = new TMVA::Reader();

for(variable: xmlreader.GetVarFromXML("TMVAClassification_BDT.weights.xml")
{
    reader->AddVariable("variable",&variable);
}

reader->BookMVA("BDT", "TMVAClassification_BDT.weights.xml");

Actually I’ve just found MethodBDT. Seems like I might be able to do something like:

method = new TMVA::MethodBDT(data, "TMVAClassification_BDT.weights.xml") ;
method->ReadStateFromXML( "var"  );

or something. But I cant initialise a MethodBDT, what is DataSetInfo?

Hi both, thanks for the advice you gave, they led me to a solution:

      TMVA::Experimental::RReader* rreader = new TMVA::Experimental::RReader(foldername);
      for(std::string variable: rreader->GetVariableNames())
      {
         std::cout << "Adding variable " << variable << " to the TMVA::Reader\n";
         if(variable == "yourVarHere") reader->AddVariable( "yourVarHere",&yourVarHere);
         .....
         //repeat for all variables possible...
         .....
      }

Pretty simple in the end, but works perfectly.

Cheers.

Hi,
I understand now your problem to get the variable names to add to the Reader before booking the method.
Yes, this is a good solution, using an internal parser function, (see here) of the XML file. Maybe we should make this function public, so it is possible to use without the RReader class.

Thank you for posting this problem!

Cheers

Lorenzo

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.