Accessing contents of *.root files

I wish to access the contents of the file tmva_reg_example.root.

Can someone please help me with this? I have no idea of the internal structure of the data so am unable to proceed in any meaningful way.

  TMVA::DataLoader *dataloader=new TMVA::DataLoader("dataset");
  // If you wish to modify default settings
  // (please check "src/Config.h" to see all available global options)
  //     (TMVA::gConfig().GetVariablePlotting()).fTimesRMS = 8.0;
  //     (TMVA::gConfig().GetIONames()).fWeightFileDir = "myWeightDirectory";
  // Define the input variables that shall be used for the MVA training
  // note that you may also use variable expressions, such as: "3*var1/var2*abs(var3)"
  // [all types of expressions that can also be parsed by TTree::Draw( "expression" )]
  dataloader->AddVariable( "var1", "Variable 1", "units", 'F' );
  dataloader->AddVariable( "var2", "Variable 2", "units", 'F' );
  // You can add so-called "Spectator variables", which are not used in the MVA training,
  // but will appear in the final "TestTree" produced by TMVA. This TestTree will contain the
  // input variables, the response values of all trained MVAs, and the spectator variables
  dataloader->AddSpectator( "spec1:=var1*2",  "Spectator 1", "units", 'F' );
  dataloader->AddSpectator( "spec2:=var1*3",  "Spectator 2", "units", 'F' );
  // Add the variable carrying the regression target
  dataloader->AddTarget( "fvalue" );
  // dataloader->AddTarget( "fvalue2" );
  // It is also possible to declare additional targets for multi-dimensional regression, ie:
  //     factory->AddTarget( "fvalue2" );
  // BUT: this is currently ONLY implemented for MLP
  // Read training and test data (see TMVAClassification for reading ASCII files)
  // load the signal and background event samples from ROOT trees
  TFile *input(0);
  TString fname = "./tmva_reg_example.root";
  if (!gSystem->AccessPathName( fname )) {
    input = TFile::Open( fname ); // check if file in local directory exists
  else {
    input = TFile::Open("", "CACHEREAD"); // if not: download from ROOT server
  if (!input) {
    std::cout << "ERROR: could not open data file" << std::endl;
  std::cout << "--- TMVARegression           : Using input file: " << input->GetName() << std::endl;
  // Register the regression tree
  TTree *regTree = (TTree*)input->Get("TreeR");
  // global event weights per tree (see below for setting event-wise weights)
  Double_t regWeight  = 1.0;
  // You can add an arbitrary number of regression trees
  dataloader->AddRegressionTree( regTree, regWeight );
  // This would set individual event weights (the variables defined in the
  // expression need to exist in the original TTree)
  dataloader->SetWeightExpression( "var1", "Regression" );



What are you trying to do with that data?

You pasted code - does that code not work? If so, could you paste the error message you see?

Your question needs to be more precise, otherwise we don’t know how to help you.


I apologize for being unclear.

Yes the code above works.
The purpose of putting the code here was to show, how the tmva_reg_example.root file is being used in a working sample code.

The above code is only a section of a larger code where later in the code, the dataloader is fed as a parameter to Machine Learning algorithms for training purposes.

I looked into the member functions of DataLoader, TFile, and TTree to look for something to help access the contents of the tmva_reg_example.root file. But failed to do so.
This is where I needed some help.

The exact reason why I wish to access the contents of the .root file, is to read the number of target variables in the dataset.


Do you have someone nearby who could give you an intro to ROOT? If not, have a look at the I/O section in the Users Guide at


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