# Same TSelector for different Trees

**URL:** <https://root-forum.cern.ch/t/same-tselector-for-different-trees/62285>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [November 20, 2024, 5:49pm UTC](https://root-forum.cern.ch/t/same-tselector-for-different-trees/62285 "2024-11-20T17:49:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerbz](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/ecc23a/32.png) [@gerbz](https://root-forum.cern.ch/u/gerbz)\
**Post date:** [November 20, 2024, 5:49pm UTC](https://root-forum.cern.ch/t/same-tselector-for-different-trees/62285/1 "2024-11-20T17:49:22Z")

</div>

* * *

_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.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 20, 2024, 7:00pm UTC](https://root-forum.cern.ch/t/same-tselector-for-different-trees/62285/2 "2024-11-20T19:00:30Z")

</div>

Hello,

Welcome to the ROOT Community!  
Currently, the recommended way to approach analysis is via [ROOT: ROOT::RDataFrame Class Reference](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) .

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
