23 """A helper class to create the dataset for the tutorial below."""
26 "df036_missingBranches_py_file_1.root",
27 "df036_missingBranches_py_file_2.root",
28 "df036_missingBranches_py_file_3.root",
30 treenames = [
"tree_1",
"tree_2",
"tree_3"]
34 with ROOT.TFile(self.filenames[0],
"RECREATE"):
35 t = ROOT.TTree(self.treenames[0], self.treenames[0])
36 x = array.array(
"i", [0])
37 y = array.array(
"i", [0])
38 t.Branch(
"x", x,
"x/I")
39 t.Branch(
"y", y,
"y/I")
41 for i
in range(1, self.nentries + 1):
48 with ROOT.TFile(self.filenames[1],
"RECREATE"):
49 t = ROOT.TTree(self.treenames[1], self.treenames[1])
50 y = array.array(
"i", [0])
51 t.Branch(
"y", y,
"y/I")
53 for i
in range(1, self.nentries + 1):
59 with ROOT.TFile(self.filenames[2],
"RECREATE"):
60 t = ROOT.TTree(self.treenames[2], self.treenames[2])
61 x = array.array(
"i", [0])
62 t.Branch(
"x", x,
"x/I")
64 for i
in range(1, self.nentries + 1):
71 """Enable using the class as a context manager."""
74 def __exit__(self, *_):
76 Enable using the class as a context manager. At the end of the context,
77 remove the files created.
79 for filename
in self.filenames:
83def df036_missingBranches(dataset: DatasetContext):
89 for fname, tname
in zip(dataset.filenames, dataset.treenames):
90 chain.Add(fname +
"?#" + tname)
94 default_value = ROOT.std.numeric_limits[int].min()
98 df.DefaultValueFor(
"x", default_value)
99 .DefaultValueFor(
"y", default_value)
100 .Display(columnList=(
"x",
"y"), nRows=15)
105 display_2 = df.DefaultValueFor(
"y", default_value).FilterAvailable(
"x").Display(columnList=(
"x",
"y"), nRows=15)
108 display_3 = df.FilterMissing(
"y").Display(columnList=(
"x",), nRows=15)
110 print(
"Example 1: provide a default value for all missing branches")
112 print(
"Example 2: provide a default value for branch y, but skip events where branch x is missing")
114 print(
"Example 3: only keep events where branch y is missing and display values for branch x")
118if __name__ ==
"__main__":
119 with DatasetContext()
as dataset:
120 df036_missingBranches(dataset)
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...