TTree LoadTree

Hi,

I’m confused about the TTree LoadTree function. What exactly does this do and when do I need to use it? It seems like it is misnamed since it doesn’t actually load anything into memory, but just sets fReadEntry equal to the argument.

Thanks.

Andrew

Hi Andrew,

This is correct but only if you have a TTree, if instead you have a TChain, LoadTree will not only move the cursor but also potentially move to a new file (closing the current one, opening the next one and load the TTree object out of the new file).

Typically you use LoadTree within your main loop:for(Long64_t entry = 0; entry < fChain->GetEntriesFast(); ++entry) { // GetEntriesFast in case of chain will not return the real number of entries until the last file in the chain // is accessed at least once, use LoadTree to move the cursor and also give us an early indication that we // reached the last entry and to give use the current entry number within the current TTree (i.e. the // entry number we should pass to TBranch::GetEntry since it does not know about the TChain. Long64_t local_entry = LoadTree(entry); if (local_entry < 0) break; ....

Cheers,
Philippe.

So, is there ever a reason to call LoadTree for a TTree?

Thanks.

Andrew

Hi Andrew,

It can be useful to make the code ‘TChain’ ready and in case where you want to decentralize the calls to TBranch::GetEntry but do not want to pass around the entry number (after LoadTree, TTree::GetReadEntry returns the ‘current’ entry number).

Cheers,
Philippe.

Hi,

I don’t understand what you mean “make the code ‘TChain’ ready”.

Andrew

Hi,

Even though in for(Long64_t entry = 0; entry < fChain->GetEntriesFast(); ++entry) { Long64_t local_entry = LoadTree(entry); if (local_entry < 0) break; when fChain is a TTree, the last two lines do not do too much, they have the advantage of also being the most efficient when fChain points to a TChain. In other words, even if your code might not be using / being passed a TChain for now, it does not hurt to write in such a way that it eventually could be re-used with a TChain.

Cheers,
Philippe.