# 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:** 1\
**Showing post:** 10

<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.

---

_[View the full topic](https://root-forum.cern.ch/t/tchain-addfriend-and-setbranchaddress-dont-play-nice-together/63613)._
