# Accessing the same leaf for different events of TTree

**URL:** <https://root-forum.cern.ch/t/accessing-the-same-leaf-for-different-events-of-ttree/24891>\
**Category:** ROOT\
**Tags:** tree\
**Created:** [May 24, 2017, 5:58pm UTC](https://root-forum.cern.ch/t/accessing-the-same-leaf-for-different-events-of-ttree/24891 "2017-05-24T17:58:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdule](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/e36b37/32.png) [@gdule](https://root-forum.cern.ch/u/gdule)\
**Post date:** [May 24, 2017, 5:58pm UTC](https://root-forum.cern.ch/t/accessing-the-same-leaf-for-different-events-of-ttree/24891/1 "2017-05-24T17:58:11Z")

</div>

Hello,

I have a TTree object with, lets say, a 100 events on it  
(tree-\>Show(0) will show all the leaves for the first event, tree-\>Show(1) for the second event, and so on).

I would like to access this information through  
TLeaf\* leaf = tree-\>GetLeaf(“some\_information”)  
however, this only get me the first event, so I was wondering how I can do that for any event.

Thanks.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [May 24, 2017, 6:17pm UTC](https://root-forum.cern.ch/t/accessing-the-same-leaf-for-different-events-of-ttree/24891/2 "2017-05-24T18:17:49Z")

</div>

I believe you just need to use [`TTree::GetEntry`](https://root.cern.ch/doc/master/classTTree.html#a9fc48df5560fce1a2d63ecd1ac5b40cb).

---

<div class="post-metadata">

**Author:** ![gdule](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/e36b37/32.png) [@gdule](https://root-forum.cern.ch/u/gdule)\
**Post date:** [May 24, 2017, 6:29pm UTC](https://root-forum.cern.ch/t/accessing-the-same-leaf-for-different-events-of-ttree/24891/3 "2017-05-24T18:29:09Z")

</div>

When using for example:  
TLeaf\* leaf = tree-\>GetLeaf(“some\_information”)  
leaf -\> GetEntry(1)

I receive an error  
"  
Error: Can’t call TLeaf::GetEntry(1) in current scope (tmpfile)(1)  
Possible candidates are…  
(in TLeaf)  
\*\*\* Interpreter error recovered \*\*\*  
"  
I’m sorry for the trouble, i’m new to root

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [May 24, 2017, 6:37pm UTC](https://root-forum.cern.ch/t/accessing-the-same-leaf-for-different-events-of-ttree/24891/4 "2017-05-24T18:37:23Z")

</div>

`GetEntry` is a member of `TTree`

```auto
TLeaf* leaf = tree->GetLeaf("some_information");
tree -> GetEntry(1);

```

Also, see this about posting code fragments:

> [@Getting the same histograms after cuts](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/2):
>
> Remark on the way to post code snippets: When you post pieces of code on the forum enclose it with ``` . That makes your post much more readable (I systematically edit your posts that way) . So do: ``` // this is a C++ piece of code. ``` And it will display as: // this is a C++ piece of code.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [May 24, 2017, 6:41pm UTC](https://root-forum.cern.ch/t/accessing-the-same-leaf-for-different-events-of-ttree/24891/5 "2017-05-24T18:41:26Z")

</div>

A working example:

```auto
{
   //Create a tree and fill it with some data
   TTree *tree = new TTree("tree","Tree");
   double value;
   tree->Branch("value", &value);
   for (int i=0;i<5000;i++) {
      value = i;
      tree->Fill();
   }
   tree->ResetBranchAddresses();

   //Get the pointer to the leaf.
   TLeaf * leaf = tree->GetLeaf("value");

   //Get each entry in the tree (first 10) and print it to the screen.
   for (int i=0;i<10;i++) {
      tree->GetEntry(i);
      std::cout << leaf->GetValue() << "\n";
   }
}

```

Outputs:

```auto
$ root tleaf.C
root [0]
Processing tleaf.C...
0
1
2
3
4
5
6
7
8
9

```

---

<div class="post-metadata">

**Author:** ![gdule](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/e36b37/32.png) [@gdule](https://root-forum.cern.ch/u/gdule)\
**Post date:** [May 24, 2017, 6:54pm UTC](https://root-forum.cern.ch/t/accessing-the-same-leaf-for-different-events-of-ttree/24891/6 "2017-05-24T18:54:40Z")

</div>

It works!

Thank you!

---

<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:** [June 7, 2017, 6:54pm UTC](https://root-forum.cern.ch/t/accessing-the-same-leaf-for-different-events-of-ttree/24891/7 "2017-06-07T18:54:45Z")

</div>

Closed on June 7, 2017, 6:54pm UTC

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