Read multiple branches with TTreeReaderArray

I have questions about the ability to read multiple separate branches of a TTree more efficiently.

Writing a standalone C++ script. I have existing .root files generated by someone else. I create a TTreeReader for it:

       TFile myFile("myFile.root");
        TTree* myTree = (TTree*)myFile.Get("cell");
        TTreeReader myReader(myTree);
       myTree->Print();

> 
> ******************************************************************************
> *Tree    :cell      : Event                                                  *
> *Entries :   625000 : Total =       283063547 bytes  File  Size =  125481168         :          : *
> *Tree compression factor =   2.26                       *
> ******************************************************************************
> *Br   22 :nFiber1   : Int_t cell                                             *
> *Entries :   625000 : Total  Size=    5008744 bytes  File Size  =    1285896 *
> *Baskets :       78 : Basket Size=      32000 bytes  Compression=   3.89     *
> *............................................................................*
> *Br   23 :nFiber2   : Int_t cell                                             *
> *Entries :   625000 : Total  Size=    5008744 bytes  File Size  =    1284406 *
> *Baskets :       78 : Basket Size=      32000 bytes  Compression=   3.89     *
> *............................................................................*
> *Br   24 :nFiber3   : Int_t cell                                             *
> *Entries :   625000 : Total  Size=    5008744 bytes  File Size  =    1282746 *
> *Baskets :       78 : Basket Size=      32000 bytes  Compression=   3.90     *
> *............................................................................*

The TTree contains many separate branches: “nFiber1”, “nFiber2”, “nFiber3”,… all the way to 24. It wasn’t written in the best way. I wish it was written as an array but that’s what I have. I could read it with lots of lines:

TTreeReaderValue<Int_t> nFiber1(myReader, "nFiber1");
TTreeReaderValue<Int_t> nFiber2(myReader, "nFiber2");
....

And then combine that into a single array later in the code. But that’s a lot of lines, and I’m actually reading 3 files in at the same time. So this is very clumsy.

My Question is: is there a way to read multiple branches with a TTreeReaderArray? Something like:

TTreeReaderArray<Int_t> nFiberChannels(myReader, "nFiber1","nFiber2","nFiber3");
....

I don’t think the above is possible though.

Even something like the below would be helpful:

TTreeReaderValue<Int_t> nFiberChannels[0](myReader, "nFiber1");
TTreeReaderValue<Int_t> nFiberChannels[1](myReader, "nFiber1");
...

Hi,
you are right, TTreeReaderArray is not intended to be used like that.
You can definitely define a vector of TTreeReaderValues though. For example, this compiles:

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

int main()
{
   TFile f("f.root");
   TTreeReader r("tree", &f);
   std::vector<TTreeReaderValue<int>> values;
   for (auto i = 0; i < 10; ++i) {
      const auto branchName = ("nFiber" + std::to_string(i)).c_str(); 
      values.emplace_back(r, branchName);
   }
   return 0;
}

You might also want to check out RDataFrame, which is a more user-friendly interface for (parallel) processing of HEP data, available since ROOT v6.14.
With RDataFrame you don’t have to take care of details like creating readers for the columns that you need, and you can focus on writing your analysis/data processing.

Cheers,
Enrico

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