Home | News | Documentation | Download

Check one branch in a TChain before processing all branches

I’m processing a set of fairly complicated trees and need to check the value of one branch before reading in the rest of the branches (in order to make sure maximum array sizes aren’t exceeded). The following code works fine if inTree is a TTree, but I’m not sure what to do if inTree is a TChain instead.

  Long64_t nEntries = inTree->GetEntries();
  for (Long64_t iEntry = 0; iEntry < nEntries; iEntry++){

      // first make sure NumCombos isn't too big...

    inTree->SetBranchStatus("*",0);
    inTree->SetBranchStatus("NumCombos",1);
    inTree->GetEntry(iEntry);
    if (inNumCombos > MAXCOMBOS) continue;
    inTree->SetBranchStatus("*",1);

      // if inNumCombos is okay, process the full tree...

    inTree->GetEntry(iEntry);

      // other processing...

  }

I see from the documentation that this won’t work for a TChain, but is there a good alternative solution for this? Thanks a lot for any guidance!

Use

type_of_value inNumCombos;
TBranch *br = nullptr;

intree->SetBranchAddress("NumCombos", &inNumCombos, &br);

  for (Long64_t iEntry = 0; iEntry < inTree->GetEntriesFast(); iEntry++){

      // first make sure NumCombos isn't too big...
    auto localEntry = inTree->LoadTree(iEntry)
    br->GetEntry(localEntry);

    if (inNumCombos > MAXCOMBOS) continue;

    // if inNumCombos is okay, process the full tree...

    inTree->GetEntry(iEntry);

      // other processing...

  }

With br being passed to SetBranchAddres, it will be update whenever the TChain needs it to.
LoadTree will return a “local entry number” that is suitable to be passed to TBranch::GetEntry.
GetEntriesFast avoid having to read/open all the files in the TChain just to know the total number of entries.

Perfect, that’s what I was looking for, thanks!

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