Unable to get value of TLeaf in TTree without first `Scan()`-ing it

ROOT Version: 6.20/07
Platform: linuxx8664gcc
Compiler: Not Provided


Hello,

I have a bunch of ROOT files with TTree named Events, under which I’ve saved a few TLeaves containing information about the number of events remaining after various cuts. I notice that, regardless of whether I’m using C++ or pyROOT, I’m unable to get the value of any TLeaf without first calling TTree::Scan(), either on the entire tree or specifying that individual leaf. Here is an example of what I mean:

Expected Behavior:
Let’s just say the value of NPROC is 57077.000, then I’d expect:

root [1] Events->GetLeaf("NPROC")->GetValue()
(double) 57077.000

Actual Behavior:
Instead, I have to call Scan() first to obtain the actual value.

root [1] Events->GetLeaf("NPROC")->GetValue()
(double) 0.0000000
root [2] Events->Scan("")
**********************************************************************************
*    Row   * Instance *     NPROC *    NFLAGS *    Dijet_par  <continued...>
**********************************************************************************
*        0 *        0 *     57077 *     56781 *    0.0002844 *     ....
<continued...>
root [3] Events->GetLeaf("NPROC")->GetValue()
(double) 57077.000

I’ve just shown an example via the command line, but the same happens for me when using a C++ or pyROOT script. What is the explanation for this behavior, and is there a better way of getting the value of the leaf? Thank you in advance!

Welcome to the ROOT forum, @ammitra !

A TTree is a storage-backed vector, and before you retrieve an entry’s value you need to tell the TTree which entry you want to access using Events->GetEntry(0) or whatever.

But I’d recommend not going there. Instead please use the much more robust and simple RDataFrame.

Cheers, Axel

Thank you, @Axel !
I’ll use an RDataFrame for this task, thank you very much. In fact, the files I am reading were generated using RDataFrame::Snapshot(), so I’ll just stick with using the dataframe to read from them. Thanks again!

1 Like