Accessing Multiple Trees from a Single ROOT file

Dear rooters,

Is there anywhere an example code for accessing multiple Trees from a single ROOT file? I have been looking for this for a while and I am still unable to find a minimal working example. I am sure there are many people out there with a similar situation. So how is this handled ?
Using Makeclass and Friendship is not at all straight forward it seems. I’d be happy to be proven wrong by someone.

Hi attikis,
I don’t know if we have a tutorial that shows what you are asking, but this should work – given you have a file “f.root” which contains tree “t” with branch “x” and tree “t2” with branch “y”, both of type int:

TFile f("f.root");
TTree *t = nullptr;
f.GetObject("t", t);
TTree *t2 = nullptr;
f.GetObject("t2",t2);
t->AddFriend(t2);
TTreeReader r(t);
TTreeReaderValue<int> x(r, "x");
TTreeReaderValue<int> y(r, "t2.y");
while (r.Next())
  std::cout << *x << " " << *y << std::endl;

and similarly for chains:

TChain c1("t");
c1.Add("f.root");
TChain c2("t2");
c2.Add("f.root");
c1.AddFriend(&c2);
TTreeReader r(&c1);
TTreeReaderValue<int> y(r, "t2.y");
while (r.Next())
  std::cout << *y << std::endl;

(edit: forget what I wrote before about MakeClass :sweat_smile: )
Also take a look at TTree::MakeSelector if you want your skeleton analysis generated automatically, I think MakeClass is a bit outdated.

Cheers,
Enrico

1 Like

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