Warning in TTreeReader with TChain

Hello,

I’m observing a misleading warning when using a TTreeReader with a TChain. This is similar to the issue reported in this thread. Here is a minimal example reproducing the problem:

// test.C

#include <iostream>
#include <string>

#include <TChain.h>
#include <TFile.h>
#include <TTreeReader.h>
#include <TTreeReaderValue.h>

void WriteTestFiles() {
  for (int i = 0; i < 2; ++i) {
    TFile file(("test" + std::to_string(i + 1) + ".root").c_str(), "recreate");
    TTree tree("tree", "");

    int n = 1;
    tree.Branch("n", &n);
    tree.Fill();

    tree.Write();
    file.Close();
  }
}

void test() {
  WriteTestFiles();

  TChain chain("tree");
  chain.Add("test*.root");

  TTreeReader reader(&chain);

  reader.GetEntries(true);
  reader.Next();  // Prints warning
}

The last line produces the following warning:

Warning in <TTreeReader::SetEntryBase()>: The current tree in the TChain tree has changed (e.g. by TTree::Process) even though TTreeReader::SetEntry() was called, which switched the tree again. Did you mean to call TTreeReader::SetLocalEntry()?

I’m getting this with ROOT 6.14/04 and 6.16/00.

I believe that issuing such a warning in this context is a bug. I also haven’t found any suitable workaround to suppress it. Namely, there seem to be no way to get the total number of entries from a TTreeReader without triggering the warning. Is this really the case, or am I missing something?

Yes, this is a bug …

You can work-around problem with:

  TTreeReader readerGetEntries(&chain);
  TTreeReader reader(&chain);

  readerGetEntries.GetEntries(true);
  reader.Next(); 

Cheers,
Philippe.

I wouldn’t consider this a suitable workaround (meaning that the warning is a lesser evil, at least in my real-life code). But thank you for confirming the problem.

See https://github.com/root-project/root/pull/3936

1 Like

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