Skip to content

Cannot access methods of branches with TChain::Scan and friends #20266

@vepadulano

Description

@vepadulano

Check duplicate issues.

  • Checked for duplicates

Description

TChain::Scan is not able to access the method of a branch from a friend, this is a pseudo-code gist, see the full reproducer below.

write_data_friend();
auto chain0 = MakeFriendChain();

write_data_main();
auto chain1 = MakeMainChain();
chain1->LoadTree(0);
chain1->GetTree()->AddFriend(chain0);

chain1->Scan("nameOfFriendChain.nameOfBranch.Method()");

This is the latest bug found while working for the CMS DAS framework. If I run the full reproducer with current master, I get:

Error in <TTreeFormula::Compile>:  Bad numerical expression : "stepzerochain.genEvents.Weight()"

If I run the reproducer after the current tip of the branch from PR #20222, I get the following

****************************************************************************************
*    Row   *     steponechain.genEvents.Weight() *    stepzerochain.genEvents.Weight() *
****************************************************************************************
*        0 *                                11.1 *                                11.1 *
*        1 *                                12.2 *                                12.2 *
*        2 *                                13.3 *                                13.3 *
*        3 *                                14.4 *                                14.4 *
*        4 *                                15.5 *                                15.5 *
*        5 *                                16.6 *                                16.6 *
*        6 *                                17.7 *                                17.7 *
*        7 *                                18.8 *                                18.8 *
*        8 *                                19.9 *                                19.9 *
*        9 *                                  21 *                                  21 *
****************************************************************************************

Note that in this case, although some values are visibile, they are all the same. Values coming from steponechain should be different than values coming from stepzerochain.

The reason for this is because of a faulty interaction with TChain::FindLeaf happening in TTreeFormula::FindLeafForExpression. In particular in these two lines

if (!branch) branch = fTree->FindBranch(first);
if (!leaf) leaf = fTree->FindLeaf(first);

The call to FindBranch now is able to find the branch thanks to the extension of the logic coming from TTree also to TChain. But the same logic is not applied (yet) to TChain::FindLeaf. So we end up finding the branch from stepzerochain, the correct one, but the leaf is never found. Because of this, the logic in FindLeafForExpression continues and ends up in this section

while (!leaf && (leafcur = (TLeaf*) next())) {
TBranch* br = leafcur->GetBranch();
bool yes = BranchHasMethod(leafcur, br, work, params, readentry);
if (yes) {
leaf = leafcur;

Where simply all the leaves of the current tree/chain are traversed. Since the chain steponechain has a leaf with the same name, at some point that will be chosen over the correct one coming from stepzerochain.

The solution is to extend the logic of TChain::FindLeaf as it was done for TChain::FindBranch. After doing that (going to upload the patch to #20222), I see the example working

****************************************************************************************
*    Row   *     steponechain.genEvents.Weight() *    stepzerochain.genEvents.Weight() *
****************************************************************************************
*        0 *                                11.1 *                                 1.1 *
*        1 *                                12.2 *                                 2.2 *
*        2 *                                13.3 *                                 3.3 *
*        3 *                                14.4 *                                 4.4 *
*        4 *                                15.5 *                                 5.5 *
*        5 *                                16.6 *                                 6.1 *
*        6 *                                17.7 *                                 7.2 *
*        7 *                                18.8 *                                 8.3 *
*        8 *                                19.9 *                                 9.4 *
*        9 *                                  21 *                                10.5 *
****************************************************************************************

Reproducer

Full reproducer at https://github.com/vepadulano/cms-das-genevent-reproducer

ROOT version

Any

Installation method

Any

Operating system

Any

Additional context

No response

Metadata

Metadata

Labels

bugexperimentAffects an experiment / reported by its software & computimng expertsin:TTree

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions