Issue Creating Multiple TMVA Readers

Hi all,

I ran into a problem when creating multiple TMVA readers in a single interactive ROOT session. When multiple readers are created, they seem to get their DataSet objects confused. I have looked through the TMVA code a bit, and I suspect that this issue arises when DataSetInfo objects are accessed by name to create the DataSet. Whenever a reader is created it assigns the same default name to its DataSetInfo, and thus multiple readers have trouble accessing their own DataSetInfo. The following script demonstrates this problem (the script depends on a weights file which I have attached). This was created on ROOT v5.26.00 using TMVA v4.0.4.

#include <vector>
#include <iostream>

void MultiReader()
{
  Int_t nVars = 9;
  std::vector<TString> varNames (nVars);

  varNames[0]="TMVACoreHC";
  varNames[1]="TMVAFirstChiSqRatio";
  varNames[2]="TMVANormTkr1Hits";
  varNames[3]="TMVANormVtxAngle";
  varNames[4]="TMVANumVertices";
  varNames[5]="TMVAVtxMaxDistCenter";
  varNames[6]="Tkr1ToTFirst";
  varNames[7]="Tkr1ToTTrAve";
  varNames[8]="VtxZDir";

  // Initialize input values of both readers to 0
  std::vector<Float_t> varVals1 (nVars,0);
  std::vector<Float_t> varVals2 (nVars,0);

  // Create first reader, add variables, and book method
  TMVA::Reader *reader1 = new TMVA::Reader("Silent");
  for (int i(0); i < nVars; i++) {
    reader1->AddVariable( varNames[i], &varVals1[i]);
  }
  reader1->BookMVA( "BDT", "TMVA_node_28_BDT.weights.xml");

  // Evaluate first reader with input values of 0
  std::cout<<"Evaluating reader1 with input values = "<<varVals1[0]<<std::endl
	   <<"reader1: "<<reader1->EvaluateMVA("BDT")<<std::endl<<std::endl;

  // Create second reader, add variables, and book method
  // This reader shouldn't be Silent, but somehow it is...
  TMVA::Reader *reader2 = new TMVA::Reader("");
  for (int i(0); i < nVars; i++) {
    reader2->AddVariable( varNames[i], &varVals2[i]);
  }
  reader2->BookMVA( "BDT", "TMVA_node_28_BDT.weights.xml");

  // Evaluate second reader with input values of 0
  std::cout<<"Evaluating reader2 with input values = "<<varVals2[0]<<std::endl
	   <<"reader2: "<<reader2->EvaluateMVA("BDT")<<std::endl<<std::endl;

  // Set second reader input values to 1
  for (int i(0); i < nVars; i++) { varVals2[i] = 1; }
  std::cout<<"Resetting input values of reader2 = "<<varVals2[0]<<std::endl
	   <<"Keeping input values of reader1 = "<<varVals1[0]<<std::endl<<std::endl;

  // This shouldn't effect the output of reader 1, but it does
  std::cout<<"Evaluating reader1 with input values = "<<varVals1[0]<<std::endl
	   <<"reader1: "<<reader1->EvaluateMVA("BDT")<<std::endl<<std::endl;

  delete reader1;
  delete reader2;
}

Since I need to create multiple readers to read different weights files, this is quite a problem for me. If anyone can think of a work around, I would be grateful to hear it.

Thanks,
Alex
MultiReader.tar.gz (8.84 KB)

1 Like