Skip to content

Commit e57f7e2

Browse files
committed
[RDF] Extend the tests for #20320
1 parent cd2db41 commit e57f7e2

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

tree/dataframe/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ if(pyroot)
161161
endif()
162162
if(NOT MSVC OR win_broken_tests)
163163
ROOT_ADD_PYUNITTEST(dataframe_merge_results dataframe_merge_results.py)
164+
ROOT_ADD_PYUNITTEST(dataframe_snapshot_py dataframe_snapshot.py)
164165
endif()
165166
endif()
166167

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import unittest
2+
3+
import ROOT
4+
5+
6+
class SnapshotTests(unittest.TestCase):
7+
# Regression described in https://github.com/root-project/root/issues/20320#issuecomment-3553697692
8+
# This was caused by an iterator invalidation when snapshots with JIT-ted filters is used
9+
def test_snapshot(self):
10+
df = ROOT.RDataFrame(10)
11+
for var in ["pt", "eta", "phi", "pdgId", "mass", "tightId", "pfIsoId", "deltaEtaSC", "cutBased"]:
12+
df = df.Define("Muon_%s" % var, "ROOT::RVec<float>(2, 1.)")
13+
df = df.Define("Electron_%s" % var, "ROOT::RVec<float>(2, 1.)")
14+
for var in ["pt", "eta", "phi", "pdgId", "mass"]:
15+
for var2 in []:
16+
df = df.Define("Muon_good_%s" % var2, "ROOT::RVec<float>(2, 1.)")
17+
df = df.Define(
18+
"Muon_good_%s" % var,
19+
"Muon_%s[abs(Muon_eta) < 2.4 && Muon_pt > 0 && Muon_tightId && Muon_pfIsoId >= 0]" % var,
20+
)
21+
for var in ["pt", "eta", "phi", "pdgId", "mass"]:
22+
df = df.Define(
23+
"Electron_good_%s" % var,
24+
"Electron_%s[!(abs(Electron_eta+Electron_deltaEtaSC)>0 && abs(Electron_eta+Electron_deltaEtaSC)< 0) && abs(Electron_eta)<2.4 && Electron_pt > 0 && Electron_cutBased > 0]"
25+
% var,
26+
)
27+
28+
df = df.Define("Muon_IDSF", "1+0.01*(Muon_pt-40)")
29+
df = df.Vary(
30+
"Muon_IDSF",
31+
"ROOT::VecOps::RVec<ROOT::VecOps::RVec<float>>({1+0.005*(Muon_pt-40), 1+0.02*(Muon_pt-40)})",
32+
["down", "up"],
33+
"muon_unc",
34+
)
35+
df = df.Define("Electron_IDSF", "1+0.01*(Electron_pt-40)")
36+
df = df.Vary(
37+
"Electron_IDSF",
38+
"ROOT::VecOps::RVec<ROOT::VecOps::RVec<float>>({1+0.005*(Electron_pt-40), 1+0.02*(Electron_pt-40)})",
39+
["down", "up"],
40+
"electron_unc",
41+
)
42+
43+
df = df.Filter("(Muon_good_pt.size() + Electron_good_pt.size()) > 0")
44+
45+
comprAlgo = getattr(ROOT.RCompressionSetting.EAlgorithm, "kZLIB")
46+
opts = ROOT.RDF.RSnapshotOptions("RECREATE", comprAlgo, 0, 0, 99, False)
47+
opts.fIncludeVariations = True
48+
49+
snapshot = df.Snapshot("Events", "output.root", ["Electron_IDSF", "Muon_IDSF"], opts)
50+
self.assertIsNotNone(snapshot)
51+
52+
with ROOT.TFile.Open("output.root") as f:
53+
tree = f.Get("Events")
54+
self.assertIsNotNone(tree)
55+
self.assertEqual(tree.GetEntries(), 10)
56+
57+
58+
if __name__ == "__main__":
59+
unittest.main()

0 commit comments

Comments
 (0)