# Obtain every nth value from a TBranch array in PyRoot

**URL:** <https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946>\
**Category:** ROOT\
**Tags:** python\
**Created:** [September 9, 2019, 5:31pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946 "2019-09-09T17:31:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hilary](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hilary/32/19751_2.png) [@Hilary](https://root-forum.cern.ch/u/Hilary)\
**Post date:** [September 9, 2019, 5:31pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/1 "2019-09-09T17:31:09Z")

</div>

* * *

_ROOT Version:_ Not Provided  
_Platform:_ Linux  
_Compiler:_ Not Provided

* * *

Hello, I am attempting to iterate over the values of one TBranch in a TTree that is located in a TDirectory under a TFile. I am fairly new to ROOT, though I have history with Python, so I thought it best to use PyROOT to complete the iteration. However, if anyone can help me get this to work in either PyROOT or ROOT, it would be very helpful.

so far I have this:  
"  
myfile = ROOT.TFile("/home/hilary/root/compile/Research/GENIE\_atm.root")  
mytree = ROOT.TDirectory(myfile.GENIE/Event) # this is the name of the tree in the file  
for e in mytree:  
print (e.mc\_Momentum) # mc\_Momentum is the name of branch of the tree  
)  
"  
I know this does not work and the issue is mainly to do with my lack of understanding of how to tell PyROOT that I want to values of the TBranch named mc\_Momentum. The location of the TBranch (mc\_Momentum) I am interested in is: GENIE\_atm.root/GENIE/Event/mc\_Momentum

I am not sure how I need to write the code such that PyROOT knows that I am interested in the values of mc\_Momentum. Any ideas?

For clarification, the TFile is is GENIE\_atm.root, the TDirectory is GENIE, the TTree is Event, and the TBranch is mc\_Momentum.

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [September 12, 2019, 9:28am UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/2 "2019-09-12T09:28:00Z")

</div>

Dear Hilary,

Can you try:

```python
myfile = ROOT.TFile("/home/hilary/root/compile/Research/GENIE_atm.root")
mytree = myfile.GENIE.Event
for e in mytree:
    print (e.mc_Momentum) # mc_Momentum is the name of branch of the tree
)

```

---

<div class="post-metadata">

**Author:** ![Hilary](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hilary/32/19751_2.png) [@Hilary](https://root-forum.cern.ch/u/Hilary)\
**Post date:** [September 12, 2019, 2:51pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/3 "2019-09-12T14:51:19Z")

</div>

Hello,

When I try this, I get the following error code:

 ![cern%201](https://root-forum.cern.ch/uploads/default/original/3X/3/d/3d01e668ed80476cc0b1fbbb6f9846427a488df3.png)

However, after tinkering around with my code, I managed to get PyROOT to understand the complexity of my TFile. I ended up with this code:

file = str("/home/hilary/root/compile/Research/GENIE\_atm.root")  
Tdir = ROOT.TChain(“GENIE/Event”)  
Tdir.Add(file)  
num = Tdir.GetEntries()

for i in range (num):  
Entry = Tdir.GetEntry(i)  
EntryFromBranch = Tdir.GetEntries()  
for j in range(EntryFromBranch):  
PT = Tdir.GetLeaf(“mc\_Momentum”).GetValue(j)

So I think PyROOT now understands the TBranch I am trying to access, however, I still do not know how to iterate over the values contained in the TBranch such that I can extract every 4th number. I now get this error when I run my newly edited code:

 ![cern%202](https://root-forum.cern.ch/uploads/default/original/3X/8/7/877c460416492f5566230653aa502ee9f0be8c3c.png)

I think this might have to do with the fact that mc\_Momentum might be an array? But I am not completely sure. Do you have any ideas?

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [September 12, 2019, 3:14pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/4 "2019-09-12T15:14:25Z")

</div>

Hi Hilary,  
Would it be possible for you to share your input so I can inspect it?

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [September 12, 2019, 3:18pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/5 "2019-09-12T15:18:17Z")

</div>

And I would also try:

```auto
myfile = ROOT.TFile("/home/hilary/root/compile/Research/GENIE_atm.root")
mydir = myfile.GENIE
mytree = mydir.Get("Event")
for e in mytree:
    print (e.mc_Momentum) # mc_Momentum is the name of branch of the tree
)

```

---

<div class="post-metadata">

**Author:** ![Hilary](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hilary/32/19751_2.png) [@Hilary](https://root-forum.cern.ch/u/Hilary)\
**Post date:** [September 12, 2019, 3:42pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/6 "2019-09-12T15:42:20Z")

</div>

Hello again,

What do you mean by input?

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [September 13, 2019, 7:00am UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/7 "2019-09-13T07:00:53Z")

</div>

I meant your input file,

```auto
GENIE_atm.root

```

Did my other suggestion work?

---

<div class="post-metadata">

**Author:** ![Hilary](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hilary/32/19751_2.png) [@Hilary](https://root-forum.cern.ch/u/Hilary)\
**Post date:** [September 13, 2019, 2:05pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/8 "2019-09-13T14:05:36Z")

</div>

Here’s the GENIE\_atm.root file:

> **[GENIE\_atm.root](https://drive.google.com/file/d/1yYN08urfX6DFPBnMsG3rPKPuMtLriAQ6/view?usp=drive_open)**
>
> Google Drive file.

I could not get it to upload on here, I think the file is too big for the fourm’s servers. However, do not worry, the file is only 250,000 KB.

I tried the last suggestion you gave and it resulted in the following:

 ![cern%203](https://root-forum.cern.ch/uploads/default/original/3X/7/0/70714e609c856a61753892fd06bee272678ab7ac.png)

Any ideas as to why it is printing out the data type and its size instead of the numbers (values) contained in each Double\_t?

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [September 13, 2019, 2:54pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/9 "2019-09-13T14:54:38Z")

</div>

Hi Hilary,  
Ok, then it seems my last suggestion is working.  
From the prints, it seems that `ec_Momentum` is not of type double, it is an array of doubles of variable size. That is why the print is telling you the type of the array values (`Double_t`) and the size of the array. To get the values in the arrays, you can do:

```python
myfile = ROOT.TFile("/home/hilary/root/compile/Research/GENIE_atm.root")
mydir = myfile.GENIE
mytree = mydir.Get("Event")
for e in mytree:
    for v in e.mc_Momentum:
        print(v) # This will print the values of each array
)

```

---

<div class="post-metadata">

**Author:** ![Hilary](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hilary/32/19751_2.png) [@Hilary](https://root-forum.cern.ch/u/Hilary)\
**Post date:** [September 13, 2019, 10:13pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/10 "2019-09-13T22:13:04Z")

</div>

This one worked! Thank you so much!

---

<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:** [September 27, 2019, 10:13pm UTC](https://root-forum.cern.ch/t/obtain-every-nth-value-from-a-tbranch-array-in-pyroot/35946/11 "2019-09-27T22:13:07Z")

</div>

Closed on September 27, 2019, 10:13pm UTC

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