# Vector in Branch

**URL:** <https://root-forum.cern.ch/t/vector-in-branch/12796>\
**Category:** ROOT\
**Created:** [July 20, 2011, 4:09pm UTC](https://root-forum.cern.ch/t/vector-in-branch/12796 "2011-07-20T16:09:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aregjan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@aregjan](https://root-forum.cern.ch/u/aregjan)\
**Post date:** [July 20, 2011, 4:09pm UTC](https://root-forum.cern.ch/t/vector-in-branch/12796/1 "2011-07-20T16:09:17Z")

</div>

The TTree documentation describes various ways to create a Branch, in particular:

> [@](#):
>
> ==\> Case E  
> TBranch \*branch = tree-\>Branch( branchname, STLcollection, buffsize, splitlevel);  
> STLcollection is the address of a pointer to std::vector, std::list,  
> std::deque, std::set or std::multiset containing pointers to objects.

So, I am trying to create a branch:

```auto
    std::vector<float> wave_model(64);
        ptree->Branch("wave_model",&wave_model);
```

The code compiles fine, however when I try to run I get this:

> [@](#):
>
> Error in TTree::Branch: The pointer specified for wave\_model is not of a class or type known to ROOT

What am I doing wrong?

p.s. I am running Version 5.28/00.

---

<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:** [July 20, 2011, 4:22pm UTC](https://root-forum.cern.ch/t/vector-in-branch/12796/2 "2011-07-20T16:22:30Z")

</div>

Hi,

You need to load the dictionary for vector. Add (literally):`gROOT->ProcessLine("#include <vector>");` to your code _before_ attempting to create the branches.

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![aregjan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@aregjan](https://root-forum.cern.ch/u/aregjan)\
**Post date:** [July 20, 2011, 5:55pm UTC](https://root-forum.cern.ch/t/vector-in-branch/12796/3 "2011-07-20T17:55:30Z")

</div>

Aha, that worked, thanks Philippe! My problems, however, are continuing. Now I am loading the resulting .root file and am trying to set the addresses for the branches, and here’s the error that I get (I also include the tree description):

```auto
root [3] ptree->Print()
******************************************************************************
*Tree :ptree : event data *
*Entries : 10000 : Total = 24619740 bytes File Size = 3623243 *
* : : Tree compression factor = 6.80 *
******************************************************************************
*Br 0 :pulse : adcoffset[1024]/s *
*Entries : 10000 : Total Size= 20542345 bytes File Size = 993299 *
*Baskets : 667 : Basket Size= 32000 bytes Compression= 20.67 *
*............................................................................*
*Br 1 :wave_model : vector<float> *
*Entries : 10000 : Total Size= 2709540 bytes File Size = 2024254 *
*Baskets : 87 : Basket Size= 32000 bytes Compression= 1.34 *
*............................................................................*
*Br 2 :event : samples/i:rate/f:time/l:time_abs/D:cn/i:pattern/i: *
* | counter/i:min_i/i:min/i:E/F:Ec/F:Ecp/f:Ped/F:PSD/F:PSDsm/F: *
* | Chisquare_v0/F:Chisquare_v1/F:Chisquare_v2/F:Chisquare_v3/F: *
* | Chisquare_v5/F:Chisquare_v6/F:Chisquare_v7/F:Chisquare_v8/F:id/i *
*Entries : 10000 : Total Size= 1045412 bytes File Size = 592936 *
*Baskets : 33 : Basket Size= 32000 bytes Compression= 1.76 *
*............................................................................*
*Br 3 :tlv_header : beamEnergy/D:beamCurrent/D:gate/i:inputCounts/i: *
* | outputCounts/i:prescaleCounts/i *
*Entries : 10000 : Total Size= 321906 bytes File Size = 4711 *
*Baskets : 11 : Basket Size= 32000 bytes Compression= 68.11 *
*............................................................................*
root [4] std::vector<float> wave_model;               
root [5] ptree->SetBranchAddress("wave_model",&wave_model);
Error in <TTree::SetBranchAddress>: The address for "wave_model" should be the address of a pointer!
```

Any ideas on this one?

---

<div class="post-metadata">

**Author:** ![aregjan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@aregjan](https://root-forum.cern.ch/u/aregjan)\
**Post date:** [July 20, 2011, 6:04pm UTC](https://root-forum.cern.ch/t/vector-in-branch/12796/4 "2011-07-20T18:04:34Z")

</div>

Ok, I got it, it’s expecting a pointer to a pointer, i.e.

```auto
ptree->SetBranchAddress("wave_model",&(&wave_model));
```

seems to work…

---

<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:** [July 20, 2011, 11:03pm UTC](https://root-forum.cern.ch/t/vector-in-branch/12796/5 "2011-07-20T23:03:48Z")

</div>

[quote]ptree-\>SetBranchAddress(“wave\_model”,&(&wave\_model));  
seems to work…[/quote]Unfortunately it only appears to work. This call pass the address of a transient pointer (i.e. it last only as long as the statement)) but the function keep a copy of this value … resulting in the TTree essentially using random memory (because this address _will_ be reused).  
You need:`std::vector<float> * wave_model = 0;               
ptree->SetBranchAddress("wave_model",&wave_model);`

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![aregjan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@aregjan](https://root-forum.cern.ch/u/aregjan)\
**Post date:** [July 21, 2011, 12:17am UTC](https://root-forum.cern.ch/t/vector-in-branch/12796/6 "2011-07-21T00:17:12Z")

</div>

Yep, in fact that’s what I ended up using. 🙂

Thanks again!

---

<div class="post-metadata">

**Author:** ![Kiran](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@Kiran](https://root-forum.cern.ch/u/Kiran)\
**Post date:** [August 23, 2019, 5:09am UTC](https://root-forum.cern.ch/t/vector-in-branch/12796/7 "2019-08-23T05:09:58Z")

</div>

Hi  
I am having oroblem while reading vector objects from branches in tree . My branch contains data of type TObjArray. I am able to read only the events. I wish to read other branches and calculate the invariant mass. How can i do that.

Cheers  
Kiran

---

<div class="post-metadata">

**Author:** ![RongkunWang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rongkunwang/32/36036_2.png) [@RongkunWang](https://root-forum.cern.ch/u/RongkunWang)\
**Post date:** [August 23, 2019, 2:59pm UTC](https://root-forum.cern.ch/t/vector-in-branch/12796/8 "2019-08-23T14:59:05Z")

</div>

You might need to load the library where the filled objects are defined? Or compile together with that piece of code

---

<div class="post-metadata">

**Author:** ![Kiran](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@Kiran](https://root-forum.cern.ch/u/Kiran)\
**Post date:** [August 26, 2019, 5:40am UTC](https://root-forum.cern.ch/t/vector-in-branch/12796/9 "2019-08-26T05:40:11Z")

</div>

Hi

I am having Error : TTree::SetBranchAddress: The pointer type given (TLorentzVector) does not correspond to the class needed (TObjArray) by the branch: reco\_6gvect while running the code . How to sort it out?

Thanks.
