Safe to cast TTree* to TChain* in MakeClass Loop method?

Hi, I’m using a class that inherits from the auto-generated code from MakeClass. The class takes as an argument a TTree, but if you pass a TChain, it correctly switches trees. However because it’s coded as a TTree, you don’t have access to the actual TChain inside the Loop() method (even though it’s called fChain, it’s of type TTree *).

My problem is that I’d like to retrieve the list of files used using fChain->GetListOfFiles() and following the instructions at http://root.cern.ch/root/html/TChain.html#TChain:AddFile, but the compiler tells me:

MydaqT.C:133:24: error: no member named 'GetListOfFiles' in 'TTree' auto toa = fChain->GetListOfFiles();
because the fChain member pointer is of type TTree* instead of TChain*.

Can I just cast fChain to a TChain * to get around this? Is it safe to do? It compiles and seems to run, but I am wondering if I am introducing a subtle bug.

Jean-François

Hi,

If you ever run the selector in a different context, fChain might end up being a TTree. So I’d recommend to at least check whether it is a TChain, e.g. using dynamic_cast<TChain*> which returns 0 if it’s not, else a pointer to a TChain.

Cheers, Axel