Read values from root file

Hi,
please take a look at the tutorials, in particular the ones under “Tree tutorials”. You can also search this forum for more specific issues.

As an overview, there are four main ways to loop over ROOT data (lower to higher level):

  1. using TTree and SetBranchAddress (see e.g. this example)
  2. using TTreeReader and TTreeReaderValue (see e.g. this example)
  3. using TTree::Draw to directly create histograms (I could not find tutorials, documentation is here, some example usage is here)
  4. using TDataFrame (tutorials here, documentation here, requires ROOT v6.10 or superior). EDIT: since ROOT v6.14 TDataFrame has been renamed to RDataFrame and left the experimental stage.

To read multiple files containing trees with the same name and structure you have to create a TChain instead of a TTree object:

// creating a TTree from one file
TFile f("file.root");
TTree *t = nullptr;
f.GetObject("treename", t);
// creating a TChain from two files
TChain c("treename");
c.Add("file1.root");
c.Add("file2.root");

Simple example of creating a histogram of variable x from a tree contained in two files using TDataFrame:

#include "ROOT/TDataFrame.hxx"
using namespace ROOT::Experimental;

int main() {
  TDataFrame df("treename", {"file1.root","file2.root"});
  auto hist = df.Histo1D("x");
  hist->DrawClone();
  return 0;
}
1 Like