Number TTree in a TChain

What is the best way of getting the number of active TTree in a TChain?

I am adding a list root files to a TChain with the Add() function. Some of these ROOT files do not contain a required TTree, some others do. In the end, the number of TTree is smaller than the number of files. How can I get the number of active TTree in a TChain so I can loop over them?

I tried to use GetTreeOffsetLen() but it does not work. If there is no TTree in the TChain, it returns 100 (???)

Thank you for your help!

This is not an ‘expected’ use case. TChain assumes there is a TTree each of the input file and even-though it supports that case, it does not keep track of any statistic on whether the file contained the tree or not.

However you can write a smaller script that discover the information.

TChain ch(....);
ch.Add(....);
auto previous = 0;
auto missing = 0;
for(Long64_t e = 0; e < ch.GetEntriesFast(); ++e) {
   if (ch.LoadTree(e) < 0)
     break;
   if (previous != ch.GetTreeNumber()) {
     if ( (ch.GetTreeNumber() - previous) != 1 ) {
        missing +=  (ch.GetTreeNumber() - previous) - 1;
     }
    previous = ch.GetTreeNumber();
  }
}

where the number you are looking for is stored in missing at the end.

Thank you for the quick help. I think that can work!

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