Logo ROOT   6.14/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 
12 import ROOT
13 from sys import exit
14 
15 try:
16  import numpy as np
17 except:
18  print("Failed to import numpy.")
19  exit()
20 
21 
22 # Helper function to create an example tree
23 def 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.
42 ROOT.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
48 print("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
52 array = tree.AsMatrix()
53 print("Tree converted to a numpy array:\n{}\n".format(array))
54 
55 # Get numpy array and according labels of the columns
56 array, labels = tree.AsMatrix(return_labels=True)
57 print("Return numpy array and labels:\n{}\n{}\n".format(labels, array))
58 
59 # Apply numpy methods on the data
60 print("Mean of the columns retrieved with a numpy method: {}\n".format(
61  np.mean(array, axis=0)))
62 
63 # Read only specific branches
64 array = tree.AsMatrix(columns=["x"])
65 print("Only the content of the branch 'x':\n{}\n".format(np.squeeze(array)))
66 
67 array = tree.AsMatrix(exclude=["x"])
68 print("Read all branches except 'x':\n{}\n".format(np.squeeze(array)))
69 
70 # Get an array with a specific data-type
71 array = tree.AsMatrix(dtype="int")
72 print("Return numpy array with data-type 'int':\n{}\n".format(array))
73 
74 ## Convert the tree to a pandas.DataFrame
75 try:
76  import pandas
77 except:
78  print("Failed to import pandas.")
79  exit()
80 
81 data, columns = tree.AsMatrix(return_labels=True)
82 df = pandas.DataFrame(data=data, columns=columns)
83 print("Tree converted to a pandas.DataFrame:\n{}".format(df))