21 """A helper class to create the dataset for the tutorial below."""
24 "df036_missingBranches_py_file_1.root",
25 "df036_missingBranches_py_file_2.root",
26 "df036_missingBranches_py_file_3.root"
28 treenames = [
"tree_1",
"tree_2",
"tree_3"]
32 with ROOT.TFile(self.filenames[0],
"RECREATE")
as f:
33 t = ROOT.TTree(self.treenames[0], self.treenames[0])
34 x = numpy.array([0], dtype=int)
35 y = numpy.array([0], dtype=int)
36 t.Branch(
"x", x,
"x/I")
37 t.Branch(
"y", y,
"y/I")
39 for i
in range(1, self.nentries + 1):
46 with ROOT.TFile(self.filenames[1],
"RECREATE")
as f:
47 t = ROOT.TTree(self.treenames[1], self.treenames[1])
48 y = numpy.array([0], dtype=int)
49 t.Branch(
"y", y,
"y/I")
51 for i
in range(1, self.nentries + 1):
57 with ROOT.TFile(self.filenames[2],
"RECREATE")
as f:
58 t = ROOT.TTree(self.treenames[2], self.treenames[2])
59 x = numpy.array([0], dtype=int)
60 t.Branch(
"x", x,
"x/I")
62 for i
in range(1, self.nentries + 1):
69 """Enable using the class as a context manager."""
72 def __exit__(self, *_):
74 Enable using the class as a context manager. At the end of the context,
75 remove the files created.
77 for filename
in self.filenames:
87 for fname, tname
in zip(dataset.filenames, dataset.treenames):
88 chain.Add(fname +
"?#" + tname)
92 default_value = ROOT.std.numeric_limits[int].min()
96 df.DefaultValueFor(
"x", default_value)
97 .DefaultValueFor(
"y", default_value)
98 .Display(columnList=(
"x",
"y"), nRows=15)
104 df.DefaultValueFor(
"y", default_value)
105 .FilterAvailable(
"x")
106 .Display(columnList=(
"x",
"y"), nRows=15)
110 display_3 = df.FilterMissing(
"y").Display(columnList=(
"x",), nRows=15)
112 print(
"Example 1: provide a default value for all missing branches")
114 print(
"Example 2: provide a default value for branch y, but skip events where branch x is missing")
116 print(
"Example 3: only keep events where branch y is missing and display values for branch x")
120if __name__ ==
"__main__":
121 with DatasetContext()
as dataset:
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...