18 print(
"Failed to import numpy.")
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")
37 return (root_file, x, y), tree
42ROOT.ROOT.EnableImplicitMT()
45_, tree = make_example()
48print(
"Tree content:\n{}\n".format(
49 np.asarray([[tree.x, tree.y]
for event
in tree])))
52array = tree.AsMatrix()
53print(
"Tree converted to a numpy array:\n{}\n".format(array))
56array, labels = tree.AsMatrix(return_labels=
True)
57print(
"Return numpy array and labels:\n{}\n{}\n".format(labels, array))
60print(
"Mean of the columns retrieved with a numpy method: {}\n".format(
61 np.mean(array, axis=0)))
64array = tree.AsMatrix(columns=[
"x"])
65print(
"Only the content of the branch 'x':\n{}\n".format(np.squeeze(array)))
67array = tree.AsMatrix(exclude=[
"x"])
68print(
"Read all branches except 'x':\n{}\n".format(np.squeeze(array)))
71array = tree.AsMatrix(dtype=
"int")
72print(
"Return numpy array with data-type 'int':\n{}\n".format(array))
78 print(
"Failed to import pandas.")
81data, columns = tree.AsMatrix(return_labels=
True)
82df = pandas.DataFrame(data=data, columns=columns)
83print(
"Tree converted to a pandas.DataFrame:\n{}".format(df))