Early error checking with TTreeReader

ROOT Version: 6.08

How do I do error detecting with TTreeReader, i.e. make sure the Trees/Branches exist?

Going off the example at https://root.cern.ch/doc/master/classTTreeReader.html, I notice it only checks the TTreeReaderValues, not the TTreeReader itself. If “MyTree” is not a tree in the file, the while loop will be skipped entirely and analyze will erroneously return true (assuming there’s a missing return true; at the end of the analyze function body).

How do I check that TTreeReader is properly setup before calling next()? It seems TTreeReader::GetEntryStatus() might be what I want but it always returns kEntryNotLoaded after initialization.

Also, why does the example check TTreeReaderValue.GetSetupStatus() every single iteration of the loop? I’d assume the setup status should only need to be checked once, unless the name is misleading and the ‘setup’ changes between iterations? I would have assumed GetReadStatus() should be called every loop and GetSetupStatus() after initializing the reader value.

Notably, GetSetupStatus() always returns kSetupNotSetup until you call Next(), which seems to defeat the purpose of checking a setup status. (Related: is the example in https://root.cern.ch/doc/v608/classTTreeReader.html incorrect? The CheckValues will always return false).

Moreover, this again causes analyze to short-circuit and return true erroneously. If the TTreeReaderValue is supplied an invalid branch name, ROOT does output the error message Error in <TTreeReaderValueBase::CreateProxy()>: The tree does not have a branch called asdf. However, the while loop will never be entered and the CheckValues will never be called. So analyze returns true, and my program will continue on oblivious of the error.

The workflow I would expect is

   TTreeReader reader("MyTree", file);
   if (!reader.GetSetupStatus()) return false;

   TTreeReaderValue<float> value(reader, "value");
   if (!value.GetSetupStatus()) return false;

   while (reader.Next()) {
      if (!value.GetReadStatus()) return false;
      auto val = *value;

Hi Riley,
sorry for the late reply, this slipped through the cracks!

I think there is a number of issues that can only be checked for after at least one entry has been loaded, e.g. branch types vs reader value types – before an entry is loaded, TTreeReader doesn’t touch the file at all, so it can’t tell whether there is a type mismatch or not.

Other issues might only arise when switching from one file to another when processing a TChain.

So in general I think you should check at every entry. The example you link to has been corrected since v608 (the version in your second link). The new version, in your first link, checks at every entry.

@Axel can probably comment with more authority.


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