Problem using BuildIndex and GetEntryWithIndex with a TChain

Hi all,

does BuildIndex and GetEntryWithIndex work properly with TChains?

I have found that the BuildIndex command seems to return the number of files added to the chain (rather than the number of indices) and GetEntryWithIndex does not return -1 if the entry could not be found. Instead, GetEntryWithIndex will leave you pointing to the last element in the chain, and GetEntryNumberWithIndex will return the last index in the TChain.

I have verified this with 5.16/00 on a linux box and 5.18/00 on a mac.

I attach a very simple macro which reproduces the problem by writing a very simple TTree out to a file, loading it back into a TChain, and trying to use the functions above on a value that ought to fail.

kind regards
Peter Cogan

[code]void chainProblem(){

// Make a tree and a file and write them to disk //

TFile file(“newTestFile.root”, “recreate”);
TTree *tree = new TTree(“testTree”, “my test tree”);
Int_t variable=0;
tree->Branch(“variable”, &variable, “variable/I”);
for (int i=0; i<100; i++){

// Try loading back in as a TChain //
TChain *chain = new TChain(“testTree”);
cout<<"Entries in chain: "<GetEntries()<<endl;
cout<<"BuildIndex returns "<BuildIndex(“variable”)<<endl;
cout<<“Try to get value that is not in the chain, this should return a -1:”<<endl;


I am really amazed that the root developers have not responded to this post. Is peterc just making a stupid mistake / misreading the documentation or is this a real bug? It would be good to know as I think it is quite important. You could easily imagine getting very spurious results from a TChain analysis if you access it via a built index


I can reproduce the issue. I will look into it. My apologies for the delay.


Thanks Philippe - was worried this one had slipped under the radar altogether - look forward to seeing the result.



I can reproduce the issue. I will look into it. My apologies for the delay.



This problem has now been fixed in the head of the SVN repository.

Thanks for reporting this issue :slight_smile:


I believe this issue is persistent once again. I am able to build the index from a root file by retrieving the TTree from the TFile but adding the file to a TChain and building the index does not appear to generate the index.

Hi @bpnuke ,

if this is a bug report, could you provide a short macro that demonstrates the problem? On go to Support -> Submit a bug to add it to the bug tracker.

Can do, I’ll work something up shortly and post appropriately.