19 print(
"Failed to import numpy.")
25 root_file = ROOT.TFile(
"pyroot002_example.root",
"RECREATE")
26 tree = ROOT.TTree(
"tree",
"tutorial")
27 x = np.empty((1), dtype=
"float32")
28 y = np.empty((1), dtype=
"float32")
29 tree.Branch(
"x", x,
"x/F")
30 tree.Branch(
"y", y,
"y/F")
38 return (root_file, x, y), tree
43ROOT.ROOT.EnableImplicitMT()
46_, tree = make_example()
49print(
"Tree content:\n{}\n".format(
50 np.asarray([[tree.x, tree.y]
for event
in tree])))
53array = tree.AsMatrix()
54print(
"Tree converted to a numpy array:\n{}\n".format(array))
57array, labels = tree.AsMatrix(return_labels=
True)
58print(
"Return numpy array and labels:\n{}\n{}\n".format(labels, array))
61print(
"Mean of the columns retrieved with a numpy method: {}\n".format(
62 np.mean(array, axis=0)))
65array = tree.AsMatrix(columns=[
"x"])
66print(
"Only the content of the branch 'x':\n{}\n".format(np.squeeze(array)))
68array = tree.AsMatrix(exclude=[
"x"])
69print(
"Read all branches except 'x':\n{}\n".format(np.squeeze(array)))
72array = tree.AsMatrix(dtype=
"int")
73print(
"Return numpy array with data-type 'int':\n{}\n".format(array))
79 print(
"Failed to import pandas.")
82data, columns = tree.AsMatrix(return_labels=
True)
83df = pandas.DataFrame(data=data, columns=columns)
84print(
"Tree converted to a pandas.DataFrame:\n{}".format(df))