Objects associated to multiple TTree entries


I have a TTree that contains one branch, which holds an instance of type Event. I also have another TTree that has one branch in it that holds instances of type Run. Multiple Events are associated to the same Run, there is a datamember of Event, called runNumber, which will correspond to a unique entry in the Run TTree (the Run object also has a datamember called runNumber). So no two entries in the Run TTree have the same runNumber.

What I would like to know is if there is a method to easily associate my events to the run objects?

I was trying to be efficient with my disk usage by avoiding having two branches in my Event TTree, one storing the Event object and the other storing the associated Run object. I think this is a bit of a long shot, but can the TRef class somehow enable me to put a reference to the appropriate Run object inside each of my Event objects, so I effectively only store a reference to the entry in the second table?

I think my question boils down to this: Does ROOT provide a useful mechanism to associate one object with multiple entries in a TTree, without having to duplicate the same object over and over by storing it in a branch (I am assuming that this is what would happen if I put my Run object in a branch in the Event TTree).

Cheers. Will.


Yes. You can make the 2 TTrees friend of each other (TTree::AddFriend) and tell them the ‘link’ between them by creating an index on the Run TTree (TTree::BulldIndex).

See root.cern.ch/root/html/tutorials … end.C.html


Hi Phillipe,

Thanks for pointing me in the right direction. I tried using BuildIndex on my TChain and I got a load of this… (lb is the the TChain)

root [4] lb.BuildIndex(“m_runNumber”,“m_lumiBlockNumber”)
Error in TChainIndex::TChainIndex: The indices in files of this chain aren’t sorted.

Error in TTreePlayer::BuildIndex: Creating a TChainIndex unsuccessfull - switching to TTreeIndex

Playing around a little, it looks like things are working ok anyway, but I am concerned by these errors. Are they indicating that I will run in to trouble at some point? Are these errors indicating somehow that I cannot save the built index within the files that the TChain came from?


[quote]Are these errors indicating somehow that I cannot save the built index within the files that the TChain came from?[/quote]Yes, you can not. Since there is no way to optimize the lookup since the entries accross the TTrees are not sorted. I.e. Given a value of the index, there is no fast way to know which TTree it is in and thus only a full index (across all TTrees) will work.