# Reading vectors from a tree

**URL:** <https://root-forum.cern.ch/t/reading-vectors-from-a-tree/44801>\
**Category:** General Chat\
**Created:** [May 10, 2021, 6:49pm UTC](https://root-forum.cern.ch/t/reading-vectors-from-a-tree/44801 "2021-05-10T18:49:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marguiitar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marguiitar/32/25850_2.png) [@marguiitar](https://root-forum.cern.ch/u/marguiitar)\
**Post date:** [May 10, 2021, 6:49pm UTC](https://root-forum.cern.ch/t/reading-vectors-from-a-tree/44801/1 "2021-05-10T18:49:12Z")

</div>

Hi! I am a begginer using trees, I have .root file with a lot of variables, I have just declared all of them in my .C document, I declared them like:

vector\< **double** \> \*Station=0;

after that I do:

t-\>SetBranchAddress(“Station”,&Station);

I want to work with this variable as a vector, so I do:  
Double\_t \*sig = new Double\_t [n]; //n is the number of entries in the tree  
sig =Station-\>data();

After that I do a cout to check that everything is correct but it isn’t because it doesn’t show me nothing.

Could you tell me where is the problem?  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 11, 2021, 7:35am UTC](https://root-forum.cern.ch/t/reading-vectors-from-a-tree/44801/2 "2021-05-11T07:35:07Z")

</div>

> [@marguiitar](#):
>
> I want to work with this variable as a vector

Do you mean you want to retrieve a tree variable in a C++ vector ?  
If that’s the case the following tutorial may help you:  
[https://root.cern/doc/master/treegetval\_8C.html](https://root.cern/doc/master/treegetval_8C.html)

---

<div class="post-metadata">

**Author:** ![marguiitar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marguiitar/32/25850_2.png) [@marguiitar](https://root-forum.cern.ch/u/marguiitar)\
**Post date:** [May 11, 2021, 8:27am UTC](https://root-forum.cern.ch/t/reading-vectors-from-a-tree/44801/3 "2021-05-11T08:27:31Z")

</div>

Thank you very much!!  
Variables 0, 5 and 6 are x, y and z? Why they are that name??

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 11, 2021, 8:48am UTC](https://root-forum.cern.ch/t/reading-vectors-from-a-tree/44801/4 "2021-05-11T08:48:50Z")

</div>

GetVal works with Variables Number.

---

<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:** [May 11, 2021, 9:58am UTC](https://root-forum.cern.ch/t/reading-vectors-from-a-tree/44801/5 "2021-05-11T09:58:24Z")

</div>

Please note that [TTree::GetVal](https://root.cern/doc/master/classTTree.html#a3ad3433f279c1b6f54083e40b8fcc717) retrieves the results of the most recent [TTree::Draw](https://root.cern/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45) and nothing else.

Something like this should work:

```cpp
vector<double> *Station = 0;
t->SetBranchAddress("Station", &Station);
Long64_t n = t->GetEntries();
for (Long64_t i = 0; i < n; i++) {
  t->GetEntry(i);
  ULong_t nsig = Station->size(); // MUST be done for every tree entry
  double *sig = Station->data(); // MUST be done for every tree entry
  for (ULong_t j = 0; j < nsig; j++) {
    std::cout << i << " : " << j << " : " << sig[j] << std::endl;
  }
}
t->ResetBranchAddresses(); // disconnect from local variables
delete Station; // cleanup

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 11, 2021, 10:41am UTC](https://root-forum.cern.ch/t/reading-vectors-from-a-tree/44801/6 "2021-05-11T10:41:52Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> esults of the most recent [TTree::Draw](https://root.cern/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45)

Yes, it is interesting if you want to get the values corresponding to combination of variables.
