# TChain, AddFriend and SetBranchAddress don't play nice together?

**URL:** <https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613>\
**Category:** ROOT\
**Created:** [May 26, 2025, 7:12pm UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613 "2025-05-26T19:12:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![FVazquez](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/bc8723/32.png) [@FVazquez](https://root-forum.cern.ch/u/FVazquez)\
**Post date:** [May 26, 2025, 7:12pm UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/1 "2025-05-26T19:12:58Z")

</div>

* * *

_ROOT Version:_ 6.32.02  
_Platform:_ linuxx8664gcc

* * *

I am seeing some odd behaviour when trying to befriend two TChains, and I am not sure if this is a bug or if I misunderstand how SetBranchAddress works with TChain. Basically the variable used to set the branch address to stops updating after certain methods are called, but only if the TChain contains more than one TTree.

The most simple scenario where I observe this behaviour is the following: I have N files (let’s say two for simplicity), each with two TTrees A and B, which I want to concatenate across files. A has a branch called Avar, B has a branch called Bvar. Then I try the following:

```auto
TChain A("A");
A.Add("file1.root");
A.Add("file2.root");

TChain B("B");
B.Add("file1.root");
B.Add("file2.root");

A.AddFriend(&B);
Float_t Avar, Bvar;
A.SetBranchAddress("Avar", &Avar);
A.SetBranchAddress("Bvar", &Bvar);

A.GetEntry(0);
cout << Avar << " " << Bvar << endl; // Here both Avar and Bvar updated properly

A.GetEntries(); //This causes something to happen behind the scenes

A.GetEntry(1);
cout << Avar << " " << Bvar << endl; // Here only Avar updated properly, Bvar still has the previous value

```

“GetEntries()” is not the only method that somehow “unsets” SetBranchAddress for the variable in the friend TChain. I have noticed the same when doing for example `A.Draw(">>EvtEntryList", "somecut", "entrylist")`. And after the branch gets “unset”, it cannot be set again, i.e. redoing ` A.SetBranchAddress("Bvar", &Bvar)` does not fix the issue. Note also that I only see this if the TChain actually has more than one TTree, otherwise everything behaves as expected.

Any idea what is going on?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [May 27, 2025, 5:26am UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/2 "2025-05-27T05:26:33Z")

</div>

I don’t see that in my tests. Do you have a reproducer (trees with data for that code) that shows the issue? Maybe the problem is in your trees; also, note that if you are getting entries 0 and 1, they are most likely still within the same tree in the chain (unless the first tree only has 1 entry); have you made sure that those entries don’t have the values you are seeing printed (i.e. Bvar is the same for entries 0 and 1)?  
Here is an example (“aa.C”) with your code:

```auto
void create1();
void create2();

void aa() {
  create1();
  create2();
  TChain A("A");
  A.Add("file1.root");
  A.Add("file2.root");
  TChain B("B");
  B.Add("file1.root");
  B.Add("file2.root");
  A.AddFriend(&B);
  Float_t Avar, Bvar;
  A.SetBranchAddress("Avar", &Avar);
  A.SetBranchAddress("Bvar", &Bvar);
  A.GetEntry(0);
  cout << Avar << " " << Bvar << endl; // Here both Avar and Bvar updated properly
  A.GetEntries(); //This causes something to happen behind the scenes
  A.GetEntry(1);
  cout << Avar << " " << Bvar << endl; // Here only Avar updated properly, Bvar still has the previous value
}

void create1() {
  Float_t Avar, Bvar;
  TFile f("file1.root","recreate");
  TTree A("A","tree A");
  TTree B("B","tree B");
  A.Branch("Avar",&Avar,"Avar/F");
  B.Branch("Bvar",&Bvar,"Bvar/F");
  for (int i=0; i<5; ++i) {
    Avar = i;
    Bvar = -i;
    A.Fill();
    B.Fill();
  }
  f.cd();
  A.Write("A");
  B.Write("B");
  f.Close();
  return;
}

void create2() {
  Float_t Avar, Bvar;
  TFile f("file2.root","recreate");
  TTree A("A","tree A");
  TTree B("B","tree B");
  A.Branch("Avar",&Avar,"Avar/F");
  B.Branch("Bvar",&Bvar,"Bvar/F");

  for (int i=0; i<5; ++i) {
    Avar = -(10+i);
    Bvar = 10+i;
    A.Fill();
    B.Fill();
  }
  f.cd();
  A.Write("A");
  B.Write("B");
  f.Close();
  return;
}

```

and the output is:

```auto
root [0] .x aa.C
0 0
1 -1
root [1]

```

as expected.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [May 27, 2025, 10:05am UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/3 "2025-05-27T10:05:33Z")

</div>

Maybe related:

> <https://github.com/root-project/root/pull/18871>
>
> In the case of a TTree with a friend TChain, the call to \`GetEntry\` that needs t…o move to a new tree in the friend TChain was not correctly updating the branch addresses. SetBranchAddress now makes a distinction between the case of the branch belonging to the main tree versus the case of the branch belonging to one of the friends. In order for this to work, GetBranch functionality has been split in two: GetBranchFromSelf checks whether the branch name corresponds to a branch of this tree and returns it if so; GetBranchFromFriends checks whether the branch name corresponds to a branch found in one of the trees and returns it if so.
> 
> Fixes #16804

> <https://github.com/root-project/root/issues/16805>
>
> \### Check duplicate issues.
> 
> \- \[X\] Checked for duplicates
> 
> \### Description
> …
> Consider the following tree loading structure:
> \`\`\`
> TChain c2 ---\> TTree ---(friend)---\> TChain c1 ---\> TTree
> \`\`\`
> When iterating over \`c2\`, branches coming from \`c1\` stop updating after the first file boundary is reached in \`c1\`. They start updating again once a file boundary is reached in \`c2\`.
> 
> I think this is distinct from #16804 because in this case the issues lie in \`TChain::LoadTree\`, while for #16804 \`TChain::LoadTree\` is the sole culprit.
> 
> As in #16804, the root cause of the issue is that the following code is not invoked often enough:
> https://github.com/root-project/root/blob/2cc4b70d2e2bd89d9b639c894b4e0558be47c22b/tree/tree/src/TChain.cxx#L1407-L1434
> I will call the operation above an "address refresh".
> 
> TChain has code triggering an address refresh when needed for friends of the current tree:
> https://github.com/root-project/root/blob/2cc4b70d2e2bd89d9b639c894b4e0558be47c22b/tree/tree/src/TChain.cxx#L1384-L1393
> 
> Unfortunately this doesn't work for two reasons:
> 1. The code cannot possibly be reached unless \`c2\` itself has friends because of the following condition:
> https://github.com/root-project/root/blob/2cc4b70d2e2bd89d9b639c894b4e0558be47c22b/tree/tree/src/TChain.cxx#L1370
> In our case \`c2\` doesn't have friends, but the \`fTree\` does. This is easy to fix (or work around).
> 2. Even if \`fFriends\` is made non-null, \`fetree-\>IsUpdated()\` always returns \`false\` because \`TTree::LoadTree\` resets it:
> https://github.com/root-project/root/blob/18b4f317b389fa7931d7eb8e00525f4dca44be0f/tree/tree/src/TTree.cxx#L6512
> Fixing this is much more annoying. For \[my application\](https://gitlab.cern.ch/Proto/Darwin/-/merge\_requests/44), I ended up subscribing to the \`Notify\` signal and calling \`TFriendElement::MarkUpdated\` again. This is only a partial solution because when \`c2\` loads a new file, it doesn't reset friend elements that were marked updated, which would happen somewhere along this line:
> https://github.com/root-project/root/blob/2cc4b70d2e2bd89d9b639c894b4e0558be47c22b/tree/tree/src/TChain.cxx#L1666
> 
> \### Reproducer
> 
> Rename to \`recursiveBugDemo.C\` and run the following macro: \[recursiveBugDemo.txt\](https://github.com/user-attachments/files/17593622/recursiveBugDemo.txt)
> 
> It works similarly to #16804 and should only print \`true\`.
> 
> \### ROOT version
> 
> Seen in:
> 
> \* ROOT 6.30/04 (anaconda)
> \* ROOT 6.28/04 (LCG 104)
> 
> I couldn't find any code change in master in the relevant areas.
> 
> \### Installation method
> 
> anaconda and LCG 104
> 
> \### Operating system
> 
> Linux (CentOS 9, Ubuntu 22.04, Ubuntu 24.04)
> 
> \### Additional context
> 
> \_No response\_

[https://its.cern.ch/jira/browse/ROOT-8735](https://its.cern.ch/jira/browse/ROOT-8735)

Is the problem the same using ROOT 6.36 ?

---

<div class="post-metadata">

**Author:** ![FVazquez](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/bc8723/32.png) [@FVazquez](https://root-forum.cern.ch/u/FVazquez)\
**Post date:** [May 27, 2025, 10:08am UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/4 "2025-05-27T10:08:43Z")

</div>

Thank you for doing this test! Then it does seem like there is something going on with my input files then. I am sharing a link so you can download, they add up to ~500MB (let me know if you have a different preferred file transfer system, this link is valid for 7 days):

[https://www.transfernow.net/dl/2025052729XpOak7](https://www.transfernow.net/dl/2025052729XpOak7)

(There is a third TTree in these files with custom classes, but we don’t touch it for this test.)

Try the following:

```auto
TChain sel("sel");
TChain T("T");
sel.Add("Qlow1_000XXXXX_nu_v9.0.root");
T.Add("Qlow1_000XXXXX_nu_v9.0.root");
sel.Add("bestQ_000XXXXX_nu_v9.0.root");
T.Add("bestQ_000XXXXX_nu_v9.0.root");
sel.AddFriend(&T);

Float_t muonscore; //This var is in sel
Int_t n_gen; //This var is in T
sel.SetBranchAddress("muonscore", &muonscore);
sel.SetBranchAddress("n_gen", &n_gen);
sel.GetEntry(0);
cout << "muonscore(0): " << muonscore << ", n_gen(0): " << n_gen << endl;
sel.GetEntry(100);
cout << "muonscore(100): " << muonscore << ", n_gen(100): " << n_gen << endl;
cout << "Check number of entries: " << sel.GetEntries() << endl;
sel.GetEntry(0);
cout << "muonscore(0): " << muonscore << ", n_gen(0): " << n_gen << endl;

```

The output is:

```auto
muonscore(0): 0.000750723, n_gen(0): 648
muonscore(100): 0.0123782, n_gen(100): 22100
Check number of entries: 395824
muonscore(0): 0.000750723, n_gen(0): 22100

```

And yes, entry 100 is still in the first TTree. And yet if I do the same thing without adding the second file to the TChains, things work as expected!

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [May 27, 2025, 10:36am UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/5 "2025-05-27T10:36:16Z")

</div>

I have not tried with your data, but for now: did my example work fine for you?  
Can you also try (your code and data) with a more recent ROOT (I was testing with the latest, 6.36.00)? maybe it’s a version issue, as @ferhue suggested. I just tried my example on the same version you have, 6.32.02, and it works ok too, but maybe with larger or more complex trees you get the issue.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [May 27, 2025, 11:00am UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/6 "2025-05-27T11:00:47Z")

</div>

Checking your trees, `n_gen` is not a branch, but a leaf inside the branch `sum_mc_evt`.  
If you just open one of your files you should get warnings about it:

```auto
$ root Qlow1_000XXXXX_nu_v9.0.root 
   ------------------------------------------------------------------
  | Welcome to ROOT 6.36.000 https://root.cern |
  | (c) 1995-2025, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on May 25 2025, 16:47:38 |
  | From tags/v6-36-00@v6-36-00 |
  | With c++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0 |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------------

root [0] 
Attaching file Qlow1_000XXXXX_nu_v9.0.root as _file0...
Warning in <TClass::Init>: no dictionary for class Evt is available
Warning in <TClass::Init>: no dictionary for class AAObject is available
Warning in <TClass::Init>: no dictionary for class Hit is available
Warning in <TClass::Init>: no dictionary for class Vec is available
Warning in <TClass::Init>: no dictionary for class Trk is available
Warning in <TClass::Init>: no dictionary for class MC_trks_summary is available
Warning in <TClass::Init>: no dictionary for class hits_summary is available
Warning in <TClass::Init>: no dictionary for class nu_summary is available
Warning in <TClass::Init>: no dictionary for class MC_evts_summary is available
Warning in <TClass::Init>: no dictionary for class Celestial_coordinates is available
Warning in <TClass::Init>: no dictionary for class crkv_hits is available
Warning in <TClass::Init>: no dictionary for class ORCA_Neutrino2020 is available
Warning in <TClass::Init>: no dictionary for class rec_trks_summary is available
Warning in <TClass::Init>: no dictionary for class jppshower_summary is available
(TFile *) 0x5eab1618bde0
root [1]

```

Anyway, this works:

```auto
sel.SetBranchAddress("sum_mc_evt.n_gen", &n_gen);

```

---

<div class="post-metadata">

**Author:** ![FVazquez](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/bc8723/32.png) [@FVazquez](https://root-forum.cern.ch/u/FVazquez)\
**Post date:** [May 27, 2025, 11:29am UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/7 "2025-05-27T11:29:18Z")

</div>

@dastudillo : I confirm that the example you wrote works as expected for me too.

And that I _still_ see the same issue with ROOT 6.36.00 (with `source /cvmfs/sft.cern.ch/lcg/app/releases/ROOT/6.36.00/x86_64-almalinux9.5-gcc115-opt/bin/thisroot.sh`), even with the updated path to the branch, i.e. “sum\_mc\_evt.n\_gen” (thank you for pointing that out though!). Are you saying you cannot reproduce the error with my input files and the correct branch name?

@ferhue: Besides the error I describe here, I do indeed see odd behaviour when the TChain changes TTrees - I assume it’s happening when I call GetEntries(). I don’t know for sure, but the bug report you linked to does indeed sound similar to what I see.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [May 27, 2025, 11:48am UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/8 "2025-05-27T11:48:52Z")

</div>

> [@FVazquez](#):
>
> you cannot reproduce the error with my input files and the correct branch name?

With that change on my laptop, using ROOT 6.36.00 (didn’t test with the older version), I got 648 for `n_gen` in the first and last printed lines (instead of 22100 on the last one).

---

<div class="post-metadata">

**Author:** ![FVazquez](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/bc8723/32.png) [@FVazquez](https://root-forum.cern.ch/u/FVazquez)\
**Post date:** [May 27, 2025, 12:09pm UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/9 "2025-05-27T12:09:15Z")

</div>

Thank you for the test. That is very strange. We have the same inputs, the same ROOT version, and we run the same commands, but we see different outputs.

How are you running the code? I am doing it directly from the ROOT interpreter, are you doing it differently?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [May 27, 2025, 12:12pm UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/10 "2025-05-27T12:12:22Z")

</div>

Update/correction:  
I tried again on another laptop and did not work (already deleted your files from the previous one), but then remembered that I had also added a `GetEntries()` before setting the branch addresses (probably at any point before the first `GetEntry` works) and indeed, it works this way:

```auto
 {
  TChain sel("sel");
  TChain T("T");
  sel.Add("Qlow1_000XXXXX_nu_v9.0.root");
  T.Add("Qlow1_000XXXXX_nu_v9.0.root");
  sel.Add("bestQ_000XXXXX_nu_v9.0.root");
  T.Add("bestQ_000XXXXX_nu_v9.0.root");
  sel.AddFriend(&T);
  cout << "Number of entries: " << sel.GetEntries() << endl;
  Float_t muonscore; //This var is in sel
  Int_t n_gen; //This var is in T
  sel.SetBranchAddress("muonscore", &muonscore);
  //sel.SetBranchAddress("n_gen", &n_gen);
  sel.SetBranchAddress("sum_mc_evt.n_gen", &n_gen);
  sel.GetEntry(0);
  cout << "muonscore(0): " << muonscore << ", n_gen(0): " << n_gen << endl;
  sel.GetEntry(100);
  cout << "muonscore(100): " << muonscore << ", n_gen(100): " << n_gen << endl;
  sel.GetEntry(0);
  cout << "muonscore(0): " << muonscore << ", n_gen(0): " << n_gen << endl;

}

```

even with the original `sel.SetBranchAddress("n_gen", &n_gen);`; in both cases I get:

```auto
root [0] .x a.C
Number of entries: Warning in <TClass::Init>: no dictionary for class Evt is available
Warning in <TClass::Init>: no dictionary for class AAObject is available
Warning in <TClass::Init>: no dictionary for class Hit is available
Warning in <TClass::Init>: no dictionary for class Vec is available
Warning in <TClass::Init>: no dictionary for class Trk is available
Warning in <TClass::Init>: no dictionary for class MC_trks_summary is available
Warning in <TClass::Init>: no dictionary for class hits_summary is available
Warning in <TClass::Init>: no dictionary for class nu_summary is available
Warning in <TClass::Init>: no dictionary for class MC_evts_summary is available
Warning in <TClass::Init>: no dictionary for class Celestial_coordinates is available
Warning in <TClass::Init>: no dictionary for class crkv_hits is available
Warning in <TClass::Init>: no dictionary for class ORCA_Neutrino2020 is available
Warning in <TClass::Init>: no dictionary for class rec_trks_summary is available
Warning in <TClass::Init>: no dictionary for class jppshower_summary is available
395824
muonscore(0): 0.000750723, n_gen(0): 648
muonscore(100): 0.0123782, n_gen(100): 22100
muonscore(0): 0.000750723, n_gen(0): 648
root [1] 

```

Anyway, you should address the warnings too. Maybe [generate a skeleton](https://root-forum.cern.ch/t/analysis-skeleton-for-a-tchain/52777/2) to avoid issues.

---

<div class="post-metadata">

**Author:** ![FVazquez](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/bc8723/32.png) [@FVazquez](https://root-forum.cern.ch/u/FVazquez)\
**Post date:** [May 27, 2025, 12:31pm UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/11 "2025-05-27T12:31:06Z")

</div>

Agreed about the warnings, it’s indeed somewhere on my TODO list. I will look into the skeleton option, thanks!

I noticed that putting GetEntries() early on “fixes” the problem, but it’s not the only method that messes things up. For example, even if we add GetEntries early on, but we call, e.g. `cout << "Run a cut: " << sel.Draw(">>entrylist", "E.run_id>14800", "entrylist") << endl;` where the original GetEntries() was, then the branch stops getting updated too. It is too constraining, especially since I don’t know until after the fact if I broke it or not.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [May 27, 2025, 1:09pm UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/12 "2025-05-27T13:09:24Z")

</div>

Maybe @vpadulan knows some workaround

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [May 28, 2025, 4:29pm UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/13 "2025-05-28T16:29:39Z")

</div>

[[tree] Delegate SetBranchAddress to friends if necessary by vepadulano · Pull Request #18871 · root-project/root · GitHub](https://github.com/root-project/root/pull/18871) is likely to fix this issue but has not been merged in the source code yet.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [June 11, 2025, 4:30pm UTC](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613/14 "2025-06-11T16:30:36Z")

</div>

Closed on June 11, 2025, 4:30pm UTC

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