This tutorial illustrates the basic features of the TDataFrame class, a utility which allows to interact with data stored in TTrees following a functional-chain like approach.
5 def fill_tree(treeName, fileName):
6 tdf = ROOT.ROOT.Experimental.TDataFrame(10)
7 tdf.Define(
"b1",
"(double) tdfentry_")\
8 .Define(
"b2",
"(int) tdfentry_ * tdfentry_").Snapshot(treeName, fileName)
11 fileName =
"tdf001_introduction_py.root"
13 fill_tree(treeName, fileName)
18 TDF = ROOT.ROOT.Experimental.TDataFrame
19 d = TDF(treeName, fileName)
31 cutb1b2 =
'b2 % 2 && b1 < 4.'
37 entries1 = d.Filter(cutb1) \
41 print(
"%s entries passed all filters" %entries1.GetValue())
43 entries2 = d.Filter(
"b1 < 5.").Count();
44 print(
"%s entries passed all filters" %entries2.GetValue())
49 b1b2_cut = d.Filter(cutb1b2)
50 minVal = b1b2_cut.Min(
'b1')
51 maxVal = b1b2_cut.Max(
'b1')
52 meanVal = b1b2_cut.Mean(
'b1')
53 nonDefmeanVal = b1b2_cut.Mean(
"b2")
54 print(
"The mean is always included between the min and the max: %s <= %s <= %s" %(minVal.GetValue(), meanVal.GetValue(), maxVal.GetValue()))
61 hist = d.Filter(cutb1).Histo1D(
'b1')
62 print(
"Filled h %s times, mean: %s" %(hist.GetEntries(), hist.GetMean()))
71 cutb1_result = d.Filter(cutb1);
72 cutb1b2_result = d.Filter(cutb1b2);
73 cutb1_cutb1b2_result = cutb1_result.Filter(cutb1b2)
76 evts_cutb1_result = cutb1_result.Count()
77 evts_cutb1b2_result = cutb1b2_result.Count()
78 evts_cutb1_cutb1b2_result = cutb1_cutb1b2_result.Count()
80 print(
"Events passing cutb1: %s" %evts_cutb1_result.GetValue())
81 print(
"Events passing cutb1b2: %s" %evts_cutb1b2_result.GetValue())
82 print(
"Events passing both: %s" %evts_cutb1_cutb1b2_result.GetValue())
97 entries_sum = d.Define(
'sum',
'b2 + b1') \
98 .Filter(
'sum > 4.2') \
100 print(entries_sum.GetValue())
- Date
- May 2017
- Author
- Danilo Piparo
Definition in file tdf001_introduction.py.