Logo ROOT   6.18/05
Reference Guide
pyroot002_TTreeAsMatrix.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_pyroot
3## \notebook -nodraw
4## This tutorial shows how a TTree can be quickly converted to a numpy array or
5## a pandas.DataFrame.
6##
7## \macro_code
8##
9## \date April 2018
10## \author Stefan Wunsch
11
12import ROOT
13from sys import exit
14
15try:
16 import numpy as np
17except:
18 print("Failed to import numpy.")
19 exit()
20
21
22# Helper function to create an example tree
23def make_example():
24 root_file = ROOT.TFile("pyroot002_example.root", "RECREATE")
25 tree = ROOT.TTree("tree", "tutorial")
26 x = np.empty((1), dtype="float32")
27 y = np.empty((1), dtype="float32")
28 tree.Branch("x", x, "x/F")
29 tree.Branch("y", y, "y/F")
30
31 for i in range(4):
32 x[0] = i
33 y[0] = -i
34 tree.Fill()
35 root_file.Write()
36
37 return (root_file, x, y), tree
38
39
40# The conversion of the TTree to a numpy array is implemented with multi-
41# thread support.
42ROOT.ROOT.EnableImplicitMT()
43
44# Create a ROOT file with a tree and the branches "x" and "y"
45_, tree = make_example()
46
47# Print content of the tree by looping explicitly
48print("Tree content:\n{}\n".format(
49 np.asarray([[tree.x, tree.y] for event in tree])))
50
51# Read-out full tree as numpy array
52array = tree.AsMatrix()
53print("Tree converted to a numpy array:\n{}\n".format(array))
54
55# Get numpy array and according labels of the columns
56array, labels = tree.AsMatrix(return_labels=True)
57print("Return numpy array and labels:\n{}\n{}\n".format(labels, array))
58
59# Apply numpy methods on the data
60print("Mean of the columns retrieved with a numpy method: {}\n".format(
61 np.mean(array, axis=0)))
62
63# Read only specific branches
64array = tree.AsMatrix(columns=["x"])
65print("Only the content of the branch 'x':\n{}\n".format(np.squeeze(array)))
66
67array = tree.AsMatrix(exclude=["x"])
68print("Read all branches except 'x':\n{}\n".format(np.squeeze(array)))
69
70# Get an array with a specific data-type
71array = tree.AsMatrix(dtype="int")
72print("Return numpy array with data-type 'int':\n{}\n".format(array))
73
74## Convert the tree to a pandas.DataFrame
75try:
76 import pandas
77except:
78 print("Failed to import pandas.")
79 exit()
80
81data, columns = tree.AsMatrix(return_labels=True)
82df = pandas.DataFrame(data=data, columns=columns)
83print("Tree converted to a pandas.DataFrame:\n{}".format(df))