TChain works incorrectly if reads another TChain via AddFile

It happened that I have a TChain saved in file, which points to a set of TTrees in another files. I guess it’s not appropriate use of TChain, but it is not documented that there are any problems with it.

Usually all files passed via the command line are added to the TChain via AddFile method. While I was using a manual loop over the TChain there were no problems and I could read file containing TChains or TTrees. But when I’ve switched to the TSelector I’ve got a problem: only first tree of the TChain was analyzed.

It turned out that the problem is that TChain doesn’t check the type of a tree it reads from a file. If a tree happens to be TChain it is nevertheless added as ordinary TTree and iterates it as ordinary TTree with global indexing. As soon the index exceeds the number of events in a first chain’s tree, the event becomes stuck on the last event of the first tree.

This happens on ROOT 5.34.03