|
| 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