# Unable to read a TTree with leaves and a branch (TArrayS) with pyROOT

**URL:** <https://root-forum.cern.ch/t/unable-to-read-a-ttree-with-leaves-and-a-branch-tarrays-with-pyroot/45578>\
**Category:** Newbie\
**Tags:** python, root\
**Created:** [June 28, 2021, 12:55pm UTC](https://root-forum.cern.ch/t/unable-to-read-a-ttree-with-leaves-and-a-branch-tarrays-with-pyroot/45578 "2021-06-28T12:55:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Lopez](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/daniel_lopez/32/26634_2.png) [@Daniel\_Lopez](https://root-forum.cern.ch/u/Daniel_Lopez)\
**Post date:** [June 28, 2021, 12:55pm UTC](https://root-forum.cern.ch/t/unable-to-read-a-ttree-with-leaves-and-a-branch-tarrays-with-pyroot/45578/1 "2021-06-28T12:55:18Z")

</div>

Hello everyone, (first post here 😀)

I am new to ROOT, and I am trying to read a .root using pyROOT. The .root comes from a Nuclear detector (CAEN).The root Tree looks like (I can not upload the .root because it exceeds the maximum size, it weights 6.8MB while the max size is 3.0MB):

![tree_structure](https://root-forum.cern.ch/uploads/default/original/3X/a/8/a8255e4d7291f20ff472dae46e4f179a2edf36db.png)

I am interested in the leaf ‘Energy’ and in the leaf “fArray”, which is inside the branch “Samples” and contains the waveform of the event. So, for each event, I have 1 value for Energy, Board, etc, but for the “Samples” I should have an array, creating the waveform of the event.

I am able to read the leaves within the Tree (Data\_F) (Energy, Flags, etc), by doing (I am not using the AsMatrix function because it seems like it will be removed from ROOT soon):

```auto
data_root = TFile("DataF_CH15@V1725S_646_run_waves_acquisition_mode.root")
Tree=data_root.Data_F #getting the Tree
N = Tree.GetEntries() #number of entries of the Tree (numbers)

E = np.array( [] ) #store the Energy
Board = np.array( [] ) #store the Board
Ch_digi = np.array( [] ) #store the Channel
Timestamp = np.array( [] ) #store the Timestamp

for event in Tree: #for each event, store the desired values
    E = np.append(E, Tree.Energy) 
    Board = np.append(Board, Tree.Board)
    Ch_digi = np.append(Ch_digi, Tree.Board) 
    Timestamp = np.append(Timestamp, Tree.Timestamp)

```

But I can not do the same for the leave “fArray” inside the Branch “Samples”, because

> Tree.Samples.fArray

is not a number (which was the case for the previous leaves), but a “\<cppyy.LowLevelView at 0x7fb223e6c730\>”, which is what python’s command line returns.

Another idea I got was to read the TTre using the Get functions:

```auto
Sample = Tree.GetBranch('Samples') #the branch inside the root file
fArray = Sample.GetLeaf('fArray') #the leaf inside the branch
                    #that contains the waveform

```

But I do not know how could I get the data from that variable neither. Any help will be very appretiated. As a final comment, I think that Samples is a TArrayS, since if I type

> Tree.Samples

I get:

> \<cppyy.gbl.TArrayS object at 0x55d0dbdd7040\>

Thanks in advance,  
Dani

* * *

_ROOT Version:_ 6.24/00  
\_Python Version: 3.8.5  
\_Using Spyder (python): 5.0.3

---

<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:** [June 28, 2021, 2:44pm UTC](https://root-forum.cern.ch/t/unable-to-read-a-ttree-with-leaves-and-a-branch-tarrays-with-pyroot/45578/2 "2021-06-28T14:44:31Z")

</div>

Hi,

You can probably iterate over the elements in that `Samples` array, with:

```python
for event in Tree:
   for elem in event.Samples.fArray:
       print(elem) # this should print a number

```

---

<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:** [June 28, 2021, 2:46pm UTC](https://root-forum.cern.ch/t/unable-to-read-a-ttree-with-leaves-and-a-branch-tarrays-with-pyroot/45578/3 "2021-06-28T14:46:06Z")

</div>

Also, it looks like `fN` stores the length of the array, so you could do `event.Samples.fArray[i]` where `0 <= i < event.Samples.fN`

---

<div class="post-metadata">

**Author:** ![Daniel\_Lopez](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/daniel_lopez/32/26634_2.png) [@Daniel\_Lopez](https://root-forum.cern.ch/u/Daniel_Lopez)\
**Post date:** [June 29, 2021, 7:21am UTC](https://root-forum.cern.ch/t/unable-to-read-a-ttree-with-leaves-and-a-branch-tarrays-with-pyroot/45578/4 "2021-06-29T07:21:19Z")

</div>

Hello etejedor,

I deeply appreciate your fast answers, it turns out that both works. Furthermore, fN stores the length of the array.

I have chosen the second option (loop using event.Samples.farray[i]) because it is less demanding (I only need to use 1 event). Thank you so much 😊
