Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion tree/tree/src/TTree.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8212,19 +8212,29 @@ void TTree::ResetAfterMerge(TFileMergeInfo *info)
}

////////////////////////////////////////////////////////////////////////////////
/// Tell a branch to set its address to zero.
/// Tell a branch to set its address to zero. This is also done for all
/// trees in the list of clones.
///
/// @note If the branch owns any objects, they are deleted.

void TTree::ResetBranchAddress(TBranch *br)
{
if (br && br->GetTree()) {
br->ResetAddress();
if (GetListOfClones()) {
for (TObjLink *lnk = GetListOfClones()->FirstLink(); lnk; lnk = lnk->Next()) {
TTree *clone = (TTree *)lnk->GetObject();
auto clbr = clone ? clone->FindBranch(br->GetName()) : nullptr;
if (clbr)
clone->ResetBranchAddress(clbr);
Comment on lines +8228 to +8229
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.

We need to call here the subset of CopyAddresses related to the branch and it leafs. This will require a bit of refactoring of CopyAddresses :(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think this part is too complex for me to refactor, so I would prefer someone more expert taking over this PR

}
}
}
}

////////////////////////////////////////////////////////////////////////////////
/// Tell all of our branches to drop their current objects and allocate new ones.
/// All trees in lists of Friends and Clones are also told to reset theirs.

void TTree::ResetBranchAddresses()
{
Expand All @@ -8248,6 +8258,13 @@ void TTree::ResetBranchAddresses()
}
}
}
if (GetListOfClones()) {
for (TObjLink *lnk = GetListOfClones()->FirstLink(); lnk; lnk = lnk->Next()) {
TTree *clone = (TTree *)lnk->GetObject();
if (clone)
CopyAddresses(clone);
}
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
47 changes: 47 additions & 0 deletions tree/tree/test/TChainRegressions.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <TFile.h>
#include <TSystem.h>
#include <TTree.h>
#include <TBranch.h>
#include <TEntryList.h>
#include <TDirectory.h>

Expand Down Expand Up @@ -128,6 +129,52 @@ TEST(TChain, UncommonFileExtension)
gSystem->Unlink(dirname);
}

// https://github.com/root-project/root/issues/19402
TEST(TChain, ResetCloneBranchAddress)
{
std::unique_ptr<TFile> file1(TFile::Open("t1_19402.root", "RECREATE"));
TTree t1("t", "");
int value;
t1.Branch("value", &value);
value = 0;
t1.Fill();
value = 1;
t1.Fill();
file1->Write();
file1->Close();

std::unique_ptr<TFile> file2(TFile::Open("t2_19402.root", "RECREATE"));
TTree t2("t", "");
// int value;
t2.Branch("value", &value);
value = 10;
t2.Fill();
value = 11;
t2.Fill();
file2->Write();
file2->Close();

TChain ch("t");
ch.AddFile("t1_19402.root");
ch.AddFile("t2_19402.root");
auto newtree = ch.CloneTree(0);
ch.SetBranchAddress("value", &value);
EXPECT_NE(ch.GetBranch("value")->GetAddress(), nullptr);
EXPECT_NE(newtree->GetBranch("value")->GetAddress(), nullptr);
ch.ResetBranchAddress(ch.GetBranch("value"));
EXPECT_EQ(ch.GetBranch("value")->GetAddress(), nullptr);
EXPECT_EQ(newtree->GetBranch("value")->GetAddress(), nullptr);
ch.SetBranchAddress("value", &value);
EXPECT_NE(ch.GetBranch("value")->GetAddress(), nullptr);
EXPECT_NE(newtree->GetBranch("value")->GetAddress(), nullptr);
ch.ResetBranchAddresses();
EXPECT_EQ(ch.GetBranch("value")->GetAddress(), nullptr);
EXPECT_EQ(newtree->GetBranch("value")->GetAddress(), nullptr);

gSystem->Unlink("t1_19402.root");
gSystem->Unlink("t2_19402.root");
}

// Originally reproducer of https://github.com/root-project/root/issues/7567
// but see also https://github.com/root-project/root/issues/19220 for an update
// The test parameters are
Expand Down
Loading