Skip to content

Commit 001ff2e

Browse files
committed
[tree] Rework TChain::GetEntries to avoid side-effects
Instead of relying on TChain::LoadTree to compute the total number of entries, which has guaranteed side-effects, switch to a simpler accumulation algorithm by opening just the files of the chain. Use the READ_WITHOUT_GLOBAL_REGISTRATION option to avoid possible side-effects in this case as well.
1 parent 4b90a87 commit 001ff2e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

tree/tree/src/TChain.cxx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -905,14 +905,20 @@ Long64_t TChain::GetEntries() const
905905
// and `LoadTree` will be no-op.
906906
if (kLoadTree & fFriendLockStatus)
907907
return fEntries;
908-
const auto readEntry = fReadEntry;
909-
auto *thisChain = const_cast<TChain *>(this);
910-
thisChain->LoadTree(TTree::kMaxEntries - 1);
911-
thisChain->InvalidateCurrentTree();
912-
if (readEntry >= 0)
913-
thisChain->LoadTree(readEntry);
914-
else
915-
thisChain->fReadEntry = readEntry;
908+
Long64_t totalEntries{};
909+
for (auto chainEl : ROOT::Detail::TRangeStaticCast<TChainElement>(fFiles)) {
910+
TDirectory::TContext ctxt;
911+
std::unique_ptr<TFile> curFile{TFile::Open(chainEl->GetTitle(), "READ_WITHOUT_GLOBALREGISTRATION")};
912+
if (!curFile || curFile->IsZombie()) {
913+
continue;
914+
}
915+
std::unique_ptr<TTree> curTree{curFile->Get<TTree>(chainEl->GetName())};
916+
if (!curTree) {
917+
continue;
918+
}
919+
totalEntries += curTree->GetEntries();
920+
}
921+
const_cast<TChain *>(this)->fEntries = totalEntries;
916922
}
917923
return fEntries;
918924
}

0 commit comments

Comments
 (0)