Same TSelector for different Trees


ROOT Version: 6.28/06
Platform: Ubuntu 20.04
Compiler: gcc 9.4.0


I have a TSelector class that I created using MakeSelector. I would like to use this class to analyze two different trees, one for data and one for simulations. The problem is that each of these trees contains a branch that isn’t present in the other tree. I understand that TSelectors are designed to work on a particular set of branches, but is it possible to conditionally bind the branches based on whether or not they are present in the input tree? If not, is there a recommended way to handle this scenario? I have tried to initialize the TTreeReaderValue objects in the Init() or Begin() methods, but it appears that the actual binding takes place before then. I’m currently commenting out/in the relevant pieces of code, but it makes the program difficult to maintain. Creating separate classes for data and simulations is also not ideal for the same reason.

Hello,

Welcome to the ROOT Community!
Currently, the recommended way to approach analysis is via ROOT: ROOT::RDataFrame Class Reference .

In the case of a selector, as you rightfully point out, initialisation happens in the constructor. A way in which you can at runtime decide to initialise the TTreeReaderValue data member relative to the MC-only branch is to make it a std::unique_ptr<TTreeReaderValue<T>> that you initialise to nullptr in absence of the branch and like the others for Monte Carlo. Then of course you will have to remember this during the analysis to avoid to access a potentially invalid address, but that should be simple and negligible in terms of performance given the nature of the analysis task.

I hope that helps.

Cheers,
Danilo