TTreeReader with TChain: Reading the chain before initializing the TTreeReader produces different output

ROOT Version: 6.18/04
Platform: Ubuntu 18.04 LTS
Compiler: g++ 7.5.0


While trying to read data from TChain using TTreeReader, I observe a difference in output between two almost identical codes. In case number one, given below,

TChain* chain = new TChain("mmtree/tree");
chain->Add(filename);
TTreeReader* tree = new TTreeReader(chain);
cout<<tree->GetEntries()<<endl;
Output
===============
9223372036854775807

I obtain random output, which means that the tree is not reading the chain. However, if I add chain->GetEntries() before initializing the TTreeReader, given below, I get the correct output.

TChain* chain = new TChain("mmtree/tree");
chain->Add(filename);
cout<<chain->GetEntries()<<endl;
TTreeReader* tree = new TTreeReader(chain);
cout<<tree->GetEntries()<<endl;
Output
===============
294703
294703

Is there something missing in my understanding or is this a buggy implementation?

Hi @arsahasransu,

It turns out that the implementation of TTreeReader::GetEntries() uses GetEntriesFast(), which in the case of a TChain will return TTree:kMaxEntries if the header for the specified trees have not yet been parsed (see the comment here).

That is what you are seeing in the first case, whereas in your second example, chain->GetEntries() will cause the header to be read.

Additionally, there’s an overload of TTreeReader::GetEntries() that takes an additional argument, force, which can be used to trigger opening of files in the chain to determine the number of entries.

Cheers,
J.

1 Like

Thank you very much for the explanation. I tried out the force parameter. It works just as you had suggested above.

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