# Reading two dimensional array from TTree

I need to extract individual entries from a branch containing a two-dimensional array of integers. Here’s a printout of the tree structure:

``` from ROOT import* myfile = TFile( filepath ) tree = myfile.Get("R1DC") tree.Print()```

*Tree :R1DC : Region 1 DC *
*Entries : 5266 : Total = 272221366 bytes File Size = 1903027 *

• ``````   :          : Tree compression factor = 145.43                       *
``````

*Br 0 :evt : EventNumber/I:TDC1190/I *
*Entries : 5266 : Total Size= 272221022 bytes File Size = 1871079 *

Suppose I needed to access the ith element of only a one-dimensional array. For the 10th entry I would do the following:

``` tree.GetEntry(10) tree.GetLeaf("TDC1190").GetValue(i)```
This only returns zeros, probably because TDC1190 is a two dimensional array. How can I access these elements directly?

Thanks for the help
r4480.root (1.82 MB)

Hi,

I propose to use something like:

``````from ROOT import *
f=TFile("r4480.root")
for e in f.R1DC:
for i in  e.TDC1190:
if i!=0: print "evt %s a value is non zero %s." %(e.EventNumber,i)``````

The tree is on the other hand rather full of zeroes.

Cheers,
D

Hi,

yes, a 2D array is simply handed back as a 1D array. The values should be correct, however. You can either calculate the indices, or reshape it using numpy.

(And yes, with a compression factor of 145, I readily suspect it really is full of zeroes. Use tree.Scan() to have the C++ side of things print the values, if you still doubt it.)

Cheers,
Wim

Hi,

Thanks for the reply. I still have one more problem. “evt.TDC1190” and “evt.TDC1190” are the first and second hits from a detector. With only ‘i’ as my index, how would I know what entry I am reading specifically?

Thanks

Hi wlav,

I will give that a try. I’m not quite sure how I could calculate the indices. Is the array flattened as in the following example:

{{1,2,3},{4,5,6}}->{1,2,3,4,5,6}

Thanks.

Hi,

you need to know the dimensions of the 2D array. If this is NxM, then [i][j] is iM+j as indexed in the 1D array. So in your example, NxM = 2x3, and  (i.e. value ‘5’) is found at 1D location 13+1 = 4, as you’ve shown in the flattened layout.

Cheers,
Wim