Skip to content

[tree] Use the counter of the same object for variable-size arrays - #24183

Merged
pcanal merged 2 commits into
root-project:masterfrom
kr1xh:fix/ttree-branchcount-sibling
Nov 19, 2026
Merged

pcanal merged 2 commits into
root-project:masterfrom
kr1xh:fix/ttree-branchcount-sibling

Conversation

@kr1xh

@kr1xh kr1xh commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

AI DESCRIPTION BELOW

This Pull request:

Fixes counted arrays (Double_t *fX; //[fN]) in split objects that read the wrong number of elements when two objects of the same class are stored in top-level branches without a trailing dot. In that layout both objects have a sub-branch called fN, and b.fX ends up using a.fN as its counter.

Two bugs share one cause, looking the counter up by its short name. Each commit fixes one of them:

  1. [tree], write side: when the sub-branches are created, TBranchElement::Init finds the counter with fTree->GetBranch("fN"), which returns the first fN in the tree. The wrong link (fBranchCount and the leaf's fLeafCount) is then stored in the file.
  2. [treeplayer], TTreeReaderArray: even when the stored link is correct, TTreeReaderArray takes only the counter's short name ("fN") and looks it up again, so it still finds a.fN. This bug stays hidden until commit 1 is applied. With commit 1 alone, Draw("b.fX") is correct but TTreeReaderArray still reads 0 elements.

Changes or fixes:

  • TBranchElement.cxx: a new helper, FindBranchCount(), looks for the counter among the sub-branches of the array's parent and falls back to the tree-wide search only if it isn't there. Basic-type arrays (TStreamerBasicPointer), arrays of objects (TStreamerLoop) and the MakeClass recovery path in ReadLeavesMakeClass all use it.

  • TTreeReaderArray.cxx: TArrayParameterSizeReader gets a constructor that takes the counter branch itself and creates the size reader from the counter's full name (b.fN, b.fV.fN). The size-reader setup in TDynamicArrayReader moves into SetSizeReader() so both constructors share it.

  • New gtest testTBranchElementBranchCount (tree/tree/test/TBranchElementBranchCount.cxx, dictionary BranchCountStruct.h):

    • unprefixed basic-type array, array of objects, and an array inside an embedded split member (b.fV.fX): checks the stored counter (branch and leaf), TTreeReaderArray size and values, and Draw;
    • controls that pass with and without the fix: prefixed names, a single object; and counted arrays inside TClonesArray / std::vector, whose counters must stay the collection branch.

    The Unprefixed* tests fail on master. With only the first commit, they fail on the TTreeReaderArray checks.

Not in this PR: files written before this change still store the wrong counter, so they are still read incorrectly. The data on disk is complete pointing b.fX's counter back to b.fN after opening an old file reads 0 1 2, so the file could be repaired when it is read. The options are discussed in #24146, and I'd do the repair as a follow-up once we agree on an approach.

I can split the two commits into two PRs if you prefer.

Used Claude Opus 5.5, at High Effort

Checklist:

  • tested changes locally
  • updated the docs (if necessary)

This PR fixes #24146

@pcanal

pcanal commented Nov 16, 2026

Copy link
Copy Markdown
Member

@pcanal pcanal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the commit log to indicate the AI agent/model used in producing the commit.

@kr1xh
kr1xh force-pushed the fix/ttree-branchcount-sibling branch from d3e4811 to fba00bd Compare November 18, 2026 19:53
@kr1xh
kr1xh requested a review from pcanal November 18, 2026 19:59
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Test Results

    22 files      22 suites   3d 10h 3m 42s ⏱️
 3 894 tests  3 894 ✅ 0 💤 0 ❌
76 234 runs  76 234 ✅ 0 💤 0 ❌

Results for commit 6d3c27c.

♻️ This comment has been updated with latest results.

When two objects of the same class are stored in top-level branches
whose names have no trailing dot, their sub-branches get identical
names, e.g. "fN" and "fX" under both "a" and "b". The counter of a
variable-size array member (`Double_t *fX; //[fN]`) was found with
TTree::GetBranch("fN"), which returns the first match in the tree. So
b.fX used a.fN as its counter, and that link was stored in the file.

Look up the counter among the sub-branches of the array's parent
first, and only fall back to the tree-wide search if it is not there.
This covers basic-type arrays, arrays of objects and the MakeClass
recovery path in ReadLeavesMakeClass.

Files written before this change store the wrong counter and are still
read incorrectly; this is not addressed here.

Assisted-by: claude-code:claude-opus-5-5

@pcanal pcanal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

For a variable-size array member of a split object, TTreeReaderArray
took only the short name of the counter branch ("fN") and looked it up
again in the tree. With two objects of the same class in top-level
branches without a trailing dot, that lookup returns the first "fN" in
the tree, so b.fX was read with a.fN's size even when the file stores
the right counter.

Pass the counter branch itself to the size reader and read the size
through the counter's full name ("b.fN").

Fixes root-project#24146

Assisted-by: claude-code:claude-opus-5-5
@kr1xh
kr1xh force-pushed the fix/ttree-branchcount-sibling branch from fba00bd to 6d3c27c Compare November 19, 2026 14:23
@kr1xh
kr1xh requested a review from pcanal November 19, 2026 15:30
@pcanal
pcanal merged commit 1fa357d into root-project:master Nov 19, 2026
34 checks passed
@pcanal

pcanal commented Nov 19, 2026

Copy link
Copy Markdown
Member

/backport to 6.40, 6.42

@root-project-bot

Copy link
Copy Markdown

Preparing to backport PR #24183 to branches 6.40 and 6.42 requested by pcanal

@root-project-bot

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Counted array in a split object uses another object's counter when sub-branches are unprefixed (via TTreeReader)

4 participants