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):
- using
TTreeandSetBranchAddress(see e.g. this example) - using
TTreeReaderandTTreeReaderValue(see e.g. this example) - using
TTree::Drawto directly create histograms (I could not find tutorials, documentation is here, some example usage is here) - using
TDataFrame(tutorials here, documentation here, requires ROOT v6.10 or superior). EDIT: since ROOT v6.14TDataFramehas been renamed toRDataFrameand 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;
}