ATLAS Offline Software
Loading...
Searching...
No Matches
EventSelectionConfigExpr_unitTest.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3"""Unit tests for the generic EXPR object-kinematic cuts.
4
5The config-time validation (compatibility matrix + the two error classes) runs
6before any algorithm is created, so the error tests pass without the C++
7component. The happy-path tests require CP::ObjectKinematicSelectorAlg to be
8built (they instantiate it through the real accumulator)."""
9
10import unittest
11from EventSelectionAlgorithms.EventSelectionConfigTestSupport import run, named, prop
12from EventSelectionAlgorithms.EventSelectionConfig import (
13 UnavailableFeatureError, InconsistentSettingsError,
14)
15
16ALL = dict(electrons="AnaElectrons", muons="AnaMuons", taus="AnaTaus", jets="AnaJets",
17 largeRjets="AnaLRJets", photons="AnaPhotons", met="AnaMET", btagDecoration="btag")
18
19
20class TestExprHappyPath(unittest.TestCase):
21 def test_dR(self):
22 alg = named(run("EXPR dR(jet[0], jet[1]) < 1.0", containers=ALL), "EXPR")[0]
23 self.assertEqual(alg.getType(), "CP::ObjectKinematicSelectorAlg")
24 self.assertEqual(prop(alg, "variable"), "dR")
25 self.assertEqual(prop(alg, "sign"), "LT")
26 self.assertEqual(prop(alg, "refValue"), 1.0)
27 self.assertEqual(list(prop(alg, "operandKinds")), ["PARTICLE", "PARTICLE"])
28 self.assertEqual(list(prop(alg, "indices")), [0, 1])
29
31 alg = named(run("EXPR pt(bjet[0]) > 30000", containers=ALL), "EXPR")[0]
32 self.assertIn("btag", list(prop(alg, "selections"))[0])
33
35 alg = named(run("EXPR m(el[0]+mu[0]) > 80000", containers=ALL), "EXPR")[0]
36 self.assertEqual(prop(alg, "variable"), "m")
37 bare = [c.replace("_%SYS%", "") for c in prop(alg, "collections")]
38 self.assertEqual(bare, ["AnaElectrons", "AnaMuons"])
39
40 def test_dphi_met(self):
41 alg = named(run("EXPR dPhi(met, jet[0]) > 2.0", containers=ALL), "EXPR")[0]
42 self.assertEqual(list(prop(alg, "operandKinds")), ["MET", "PARTICLE"])
43 self.assertEqual(prop(alg, "metTerm"), "Final")
44 bare = [c.replace("_%SYS%", "") for c in prop(alg, "collections")]
45 self.assertEqual(bare, ["AnaJets"])
46
48 a = named(run("EXPR dR(jet[0], jet[1]) < 1.0", containers=ALL), "EXPR")[0]
49 b = named(run("EXPR dR(jet[0],jet[1])<1.0", containers=ALL), "EXPR")[0]
50 self.assertEqual(prop(a, "variable"), prop(b, "variable"))
51 self.assertEqual(list(prop(a, "indices")), list(prop(b, "indices")))
52
53
54class TestExprInconsistent(unittest.TestCase):
55 CASES = [
56 "EXPR dR(jet[0]) < 1.0",
57 "EXPR dR(jet[0], jet[1], jet[2]) < 1.0",
58 "EXPR eta(met) < 1.0",
59 "EXPR dR(met, jet[0]) < 1.0",
60 "EXPR pt(met[0]) > 30000",
61 "EXPR pt(met+jet[0]) > 30000",
62 "EXPR dR(jet[0]+jet[1]) < 1.0",
63 "EXPR pt(jet) > 30000",
64 ]
66 for line in self.CASES:
67 with self.subTest(line=line):
68 self.assertRaises(InconsistentSettingsError, run, line, containers=ALL)
69
70
71class TestExprUnavailable(unittest.TestCase):
73 self.assertRaises(UnavailableFeatureError, run,
74 "EXPR mT2(jet[0], jet[1]) < 100000", containers=ALL)
75
77 self.assertRaises(UnavailableFeatureError, run,
78 "EXPR pt(fjet[0]) > 30000", containers=ALL)
79
81 # charge() is recognised but not yet implemented -> UnavailableFeatureError
82 self.assertRaises(UnavailableFeatureError, run,
83 "EXPR charge(el[0]) == 1", containers=ALL)
84 self.assertRaises(UnavailableFeatureError, run,
85 "EXPR charge(jet[0]) == 1", containers=ALL)
86
87
88class TestExprMissingInput(unittest.TestCase):
90 self.assertRaises(ValueError, run, "EXPR pt(jet[0]) > 30000")
91
93 self.assertRaises(ValueError, run, "EXPR pt(bjet[0]) > 30000",
94 containers={"jets": "AnaJets"})
95
96
97if __name__ == "__main__":
98 unittest.main()
int run(int argc, char *argv[])