# Error with TTree

**URL:** <https://root-forum.cern.ch/t/error-with-ttree/40938>\
**Category:** Newbie\
**Created:** [August 10, 2020, 2:47pm UTC](https://root-forum.cern.ch/t/error-with-ttree/40938 "2020-08-10T14:47:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmiguez2020](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@dmiguez2020](https://root-forum.cern.ch/u/dmiguez2020)\
**Post date:** [August 10, 2020, 2:47pm UTC](https://root-forum.cern.ch/t/error-with-ttree/40938/1 "2020-08-10T14:47:21Z")

</div>

Hello all,

i am using a TTree called output. I also have some TH1D declared earlier in the code:

```auto
TH1D *DirTheta_resto = new TH1D("DirTheta_resto","Theta Distribution;Theta [deg]; # events", 180, 0,180.);

```

Later I used SetBranchAddress as follows:

```auto
    TString* reactores;
    double px_neutrao_r;
    double py_neutrao_r;
    double pz_neutrao_r;
    
    output->SetBranchAddress("parentMeta1",&reactores);
    output->SetBranchAddress("mcmom2x",&px_neutrao_r);
    output->SetBranchAddress("mcmom2y",&py_neutrao_r);
    output->SetBranchAddress("mcmom2z",&pz_neutrao_r);

```

and finally i iterated over the ttree:

```auto
for(int i = 0; i<nentries; i++) // iterar sobre a TTree
{
    cout << SEPARATOR << "\naddress " << 4+i << " before = " << DirTheta_resto << endl;

    output->GetEntry(i);

    cout << "\naddress " << 4+i << " after = " << DirTheta_resto << endl;
  
   (...)

}

```

The program crashes when i try to fill the TH1D, and the output for those prints is:

address 4 before = 0x7fa397ad9990

address 4 after = 0x0

ENTRY 0: reactor = BRUCE 4; px = 3.54902; py = 5.53718; pz = 4.0301

Can anyone help me? Why is the function GetEntry wiping the pointer to my histogram?

Thank you

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [August 13, 2020, 7:25am UTC](https://root-forum.cern.ch/t/error-with-ttree/40938/2 "2020-08-13T07:25:32Z")

</div>

Hi,

The TTree reads into memory that you don’t expect it to write into. That’s very likely because the `SetBranchAddress` isn’t doing what you expect it to do, which in turn is likely caused by the `TTree` having different branch types than you expect. Can you post the output of `output->Print()`?

Cheers, Axel.

---

<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:** [August 27, 2020, 7:28am UTC](https://root-forum.cern.ch/t/error-with-ttree/40938/3 "2020-08-27T07:28:50Z")

</div>

Closed on August 27, 2020, 7:28am UTC

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