# Problems with SetBranchAddress

**URL:** <https://root-forum.cern.ch/t/problems-with-setbranchaddress/44026>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [March 17, 2021, 11:33am UTC](https://root-forum.cern.ch/t/problems-with-setbranchaddress/44026 "2021-03-17T11:33:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kristian](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e8c25b/32.png) [@Kristian](https://root-forum.cern.ch/u/Kristian)\
**Post date:** [March 17, 2021, 11:33am UTC](https://root-forum.cern.ch/t/problems-with-setbranchaddress/44026/1 "2021-03-17T11:33:03Z")

</div>

Hi,

I’m currently writing a script which merges two trees. Both trees contains mainly vectors; for `ta` they are strictly of length 3, while for `tm` they are of varying length \> 3.

To actually do this, I’m using `.SetBranchAddress` to get each entry of the two trees. When I write out the entries to the console, most of my entries appears to be garbage values:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/6/4/64a8ff29f1f8113a0342b104f161c2fc752ea491.png)

When I inspect the resulting merged tree, however, everything appears to be as expected, with no trace of these weird values. The changes I make (if the condition `index==-1` triggers, I’ve verified that it does) are apparently not carried through.

Can you guys point out what I am doing wrong? I’ve tried looking around the forum and documentation for similar issues without finding anything.

```auto
    // open the root files
    TFile *fa = TFile::Open(apath);
    TFile *fm = TFile::Open(mpath);
    TTree *ta = (TTree *)fa->Get("a");
    TTree *tm = (TTree *)fm->Get("a101");

    // define variables from analyzed tree
    int mi[3], N;
    ta->SetBranchAddress("N", &N);
    ta->SetBranchAddress("mi", &mi);

    // define variables from matched tree
    // they all have length 100 to ensure there's space for all entries
    double FT[100], BT[100], FI[100], BI[100], FE[100], BE[100], ID[100]; 
    tm->SetBranchAddress("FI", &FI);
    tm->SetBranchAddress("FT", &FT);
    tm->SetBranchAddress("BI", &BI);
    tm->SetBranchAddress("BT", &BT);
    tm->SetBranchAddress("FE", &FE);
    tm->SetBranchAddress("BE", &BE);
    tm->SetBranchAddress("id", &ID);

    // define destination tree and set its branches
    double ft[3], bt[3], fi[3], bi[3], fe[3], be[3], id[3];
    string dest = "merged/" + p.filename().string();
    TFile f(dest.c_str(), "recreate");
    TTree t("tree", "merged tree for TDC calibration");
    t.Branch("FI", &fi, "FI[3]/I");
    t.Branch("BI", &bi, "BI[3]/I");
    t.Branch("FT", &ft, "FT[3]/I");
    t.Branch("BT", &bt, "BT[3]/I");
    t.Branch("FE", &fe, "FE[3]/D");
    t.Branch("BE", &be, "BE[3]/D");
    t.Branch("id", &id, "id[3]/I");

    // loop over every event in the analyzed tree ta
    for (double i = 0; i < ta->GetEntries(); i++) {
        ta->GetEntry(i); 
        tm->GetEntry(N); // N is the corresponding index in tm for each event in ta
            
        // loop over the three alpha particles
        // we use the multiplicity index mi to get the correct values
        int index;
        bool flag = false;
        for (int i = 0; i < 3; i++) {
            index = mi[i];
            if (index == -1) { // if it is a reconstructed event
                c++;
                ft[i] = 0;
                bt[i] = 0;
                fi[i] = -1;
                bi[i] = -1;
                fe[i] = 0;
                be[i] = 0;
                id[i] = -1;
                flag = true;
            } 
            else { // if it is a true event
                ft[i] = FT[index];
                bt[i] = BT[index];
                fi[i] = FI[index];
                bi[i] = BI[index];
                fe[i] = FE[index];
                be[i] = BE[index];
                id[i] = ID[index];
                cout << format("%1%, %2%, %3%, %4%, %5%, %6%, %7%") % FT[i] % BT[i] % FI[i] % BI[i] % FE[i] % BE[i] % ID[i] << endl;
            }
        }
        t.Fill(); // fill the entries into the merged tree t
    }
    t.Write(); // write to disk

```

* * *

_ROOT Version:_ 6.22/07

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 17, 2021, 12:21pm UTC](https://root-forum.cern.ch/t/problems-with-setbranchaddress/44026/3 "2021-03-17T12:21:12Z")

</div>

Attach the output of: `ta->Print(); tm->Print();`

---

<div class="post-metadata">

**Author:** ![Kristian](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e8c25b/32.png) [@Kristian](https://root-forum.cern.ch/u/Kristian)\
**Post date:** [March 17, 2021, 12:27pm UTC](https://root-forum.cern.ch/t/problems-with-setbranchaddress/44026/4 "2021-03-17T12:27:56Z")

</div>

Since there are a decent number of branches in both of them, I’ve removed those that aren’t relevant for the code above.

`ta->Print()`:

```auto
******************************************************************************
*Tree :a : a *
*Entries : 282534 : Total = 78122887 bytes File Size = 51224866 *
* : : Tree compression factor = 1.52 *
******************************************************************************
*Br 0 :mul : mul/I *
*Entries : 282534 : Total Size= 1132494 bytes File Size = 53260 *
*Baskets : 21 : Basket Size= 666624 bytes Compression= 12.51 *
*............................................................................*
*Br 1 :mi : mi[3]/I *
*Entries : 282534 : Total Size= 3396525 bytes File Size = 78987 *
*Baskets : 63 : Basket Size= 1999360 bytes Compression= 25.31 *
*............................................................................*
*Br 10 :N : N/I *
*Entries : 282534 : Total Size= 1132440 bytes File Size = 302824 *
*Baskets : 21 : Basket Size= 666624 bytes Compression= 2.20 *
*............................................................................*

```

`tm->Print()`:

```auto
******************************************************************************
*Tree :a101 : A calibrated/matched Howard style tree. *
*Entries : 1189650 : Total = 136293919 bytes File Size = 71252209 *
* : : Tree compression factor = 1.91 *
******************************************************************************
*Br 0 :mul : mul/i *
*Entries : 1189650 : Total Size= 4765069 bytes File Size = 486189 *
*Baskets : 64 : Basket Size= 1454592 bytes Compression= 8.05 *
*............................................................................*
*Br 1 : ___N___ : ___N___ /i *
*Entries : 1189650 : Total Size= 4765349 bytes File Size = 1366173 *
*Baskets : 64 : Basket Size= 1455104 bytes Compression= 2.86 *
*............................................................................*
*Br 2 :FI : FI[mul]/i *
*Entries : 1189650 : Total Size= 10486020 bytes File Size = 2703655 *
*Baskets : 197 : Basket Size= 7109632 bytes Compression= 3.19 *
*............................................................................*
*Br 3 :FT : FT[mul]/i *
*Entries : 1189650 : Total Size= 10486020 bytes File Size = 3345432 *
*Baskets : 197 : Basket Size= 7109632 bytes Compression= 2.57 *
*............................................................................*
*Br 4 :FE : FE[mul]/D *
*Entries : 1189650 : Total Size= 16200404 bytes File Size = 10089312 *
*Baskets : 270 : Basket Size= 8851968 bytes Compression= 1.32 *
*............................................................................*
*Br 5 :BI : BI[mul]/i *
*Entries : 1189650 : Total Size= 10486020 bytes File Size = 2663664 *
*Baskets : 197 : Basket Size= 7109632 bytes Compression= 3.23 *
*............................................................................*
*Br 6 :BT : BT[mul]/i *
*Entries : 1189650 : Total Size= 10486020 bytes File Size = 4210311 *
*Baskets : 197 : Basket Size= 7109632 bytes Compression= 2.05 *
*............................................................................*
*Br 7 :BE : BE[mul]/D *
*Entries : 1189650 : Total Size= 16200404 bytes File Size = 10089315 *
*Baskets : 270 : Basket Size= 8851968 bytes Compression= 1.32 *
*............................................................................*
*Br 10 :id : id[mul]/i *
*Entries : 1189650 : Total Size= 10486020 bytes File Size = 2097932 *
*Baskets : 197 : Basket Size= 7109632 bytes Compression= 4.10 *
*............................................................................*

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 17, 2021, 12:40pm UTC](https://root-forum.cern.ch/t/problems-with-setbranchaddress/44026/5 "2021-03-17T12:40:40Z")

</div>

At least:

```cpp
    // define variables from matched tree
    UInt_t mul; tm->SetBranchAddress("mul", &mul); // "mul" SHOULD BE Int_t !!!
    // they all have length 100 to ensure there's space for all entries
    UInt_t FT[100], BT[100], FI[100], BI[100], ID[100]; 
    Double_t FE[100], BE[100];
    tm->SetBranchAddress("FI", &FI);
    // ...

```

---

<div class="post-metadata">

**Author:** ![Kristian](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e8c25b/32.png) [@Kristian](https://root-forum.cern.ch/u/Kristian)\
**Post date:** [March 17, 2021, 12:58pm UTC](https://root-forum.cern.ch/t/problems-with-setbranchaddress/44026/6 "2021-03-17T12:58:18Z")

</div>

Yeah that solved it. It’s even explicitly mentioned in the docs that one should verify the types, so I don’t know why I didn’t think of it myself.

Thank you for the help.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 17, 2021, 1:03pm UTC](https://root-forum.cern.ch/t/problems-with-setbranchaddress/44026/7 "2021-03-17T13:03:41Z")

</div>

Note: In principle “`mul`” should be an “`Int_t`” i.e. “`mul/I`” (not an “`UInt_t`” i.e. “`mul/i`”). If you can, recreate the “`a101`” tree. I guess @pcanal could say if this requirement has been “lifted” now and, if yes, starting with which ROOT version.
