# Pyroot 0 0 2_ T Tree As Matrix¶

This tutorial shows how a TTree can be quickly converted to a numpy array or a pandas.DataFrame.

Author: Stefan Wunsch
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Sunday, August 09, 2020 at 08:28 AM.

In :
import ROOT
from sys import exit

try:
import numpy as np
except:
print("Failed to import numpy.")
exit()

Welcome to JupyROOT 6.23/01


Helper function to create an example tree

In :
def make_example():
root_file = ROOT.TFile("pyroot002_example.root", "RECREATE")
tree = ROOT.TTree("tree", "tutorial")
x = np.empty((1), dtype="float32")
y = np.empty((1), dtype="float32")
tree.Branch("x", x, "x/F")
tree.Branch("y", y, "y/F")

for i in range(4):
x = i
y = -i
tree.Fill()
root_file.Write()

return (root_file, x, y), tree


The conversion of the TTree to a numpy array is implemented with multi- thread support.

In :
ROOT.ROOT.EnableImplicitMT()


Create a ROOT file with a tree and the branches "x" and "y"

In :
_, tree = make_example()


Print content of the tree by looping explicitly

In :
print("Tree content:\n{}\n".format(
np.asarray([[tree.x, tree.y] for event in tree])))

Tree content:
[[ 0.  0.]
[ 1. -1.]
[ 2. -2.]
[ 3. -3.]]



Read-out full tree as numpy array

In :
array = tree.AsMatrix()
print("Tree converted to a numpy array:\n{}\n".format(array))

Tree converted to a numpy array:
[[ 0.  0.]
[ 1. -1.]
[ 2. -2.]
[ 3. -3.]]



Get numpy array and according labels of the columns

In :
array, labels = tree.AsMatrix(return_labels=True)
print("Return numpy array and labels:\n{}\n{}\n".format(labels, array))

Return numpy array and labels:
['x', 'y']
[[ 0.  0.]
[ 1. -1.]
[ 2. -2.]
[ 3. -3.]]



Apply numpy methods on the data

In :
print("Mean of the columns retrieved with a numpy method: {}\n".format(
np.mean(array, axis=0)))

Mean of the columns retrieved with a numpy method: [ 1.5 -1.5]



In :
array = tree.AsMatrix(columns=["x"])
print("Only the content of the branch 'x':\n{}\n".format(np.squeeze(array)))

array = tree.AsMatrix(exclude=["x"])

Only the content of the branch 'x':
[0. 1. 2. 3.]

[ 0. -1. -2. -3.]



Get an array with a specific data-type

In :
array = tree.AsMatrix(dtype="int")
print("Return numpy array with data-type 'int':\n{}\n".format(array))

Return numpy array with data-type 'int':
[[ 0  0]
[ 1 -1]
[ 2 -2]
[ 3 -3]]



Convert the tree to a pandas.DataFrame

In :
try:
import pandas
except:
print("Failed to import pandas.")
exit()

data, columns = tree.AsMatrix(return_labels=True)
df = pandas.DataFrame(data=data, columns=columns)
print("Tree converted to a pandas.DataFrame:\n{}".format(df))

Tree converted to a pandas.DataFrame:
x    y
0  0.0  0.0
1  1.0 -1.0
2  2.0 -2.0
3  3.0 -3.0