4"""Tests for reading the CaloScoringPlane PID file (python/caloScoringPlanePID.py).
6The numerical checks (rows summing to 1, energies increasing, ...) are done in
7C++ by CaloScoringPlanePID::Set() and tested in tests/test_calo_scoring_plane_pid.cxx;
8the tests at the end check that their errors reach Python with the file name.
15from caloScoringPlanePID
import SPECIES, PIDConfigError, load, parse
17SHIPPED = os.path.join(os.path.dirname(__file__),
"..",
"geometry",
"caloScoringPlane_pid_provided.yaml")
18IDENTITY = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
22 return {
"points": [{
"energy": 1,
"matrix": IDENTITY}], **extra}
26 assert SPECIES == (
"electron",
"hadron",
"muon")
31 assert model.energies == [1.0]
32 assert model.matrices == [1, 0, 0, 0, 1, 0, 0, 0, 1]
33 assert model.overrides == {}
34 assert not model.log_interpolation
41 "species": [
"muon",
"electron",
"hadron"],
42 "points": [{
"energy": 1,
"matrix": [[0.7, 0.0, 0.3], [0, 1, 0], [0, 0, 1]]}],
47 assert m[3 * mu + mu] == 0.7
48 assert m[3 * mu + h] == 0.3
49 assert m[3 * e + e] == 1.0
50 assert m[3 * h + h] == 1.0
56 "interpolation":
"log",
57 "points": [{
"energy": 3,
"matrix": IDENTITY}, {
"energy": 100.0,
"matrix": IDENTITY}],
60 assert model.log_interpolation
61 assert model.energies == [3.0, 100.0]
62 assert len(model.matrices) == 18
67 model = parse(
config(pdg_overrides={22:
"electron", -211:
"none", 130:
"muon"}))
68 assert model.overrides == {22: 0, -211: -1, 130: 2}
73 assert parse(yaml.safe_load(
"pdg_overrides:\n" + yaml.safe_dump(
config()))).overrides == {}
76@pytest.mark.parametrize(
79 ([1, 2],
"top level must be a mapping"),
80 (
config(energies=[1]),
"unknown key(s) energies"),
81 (
config(species=[
"electron",
"electron",
"muon"]),
"'species' must be a permutation"),
82 (
config(species=[
"electron",
"pion",
"muon"]),
"'species' must be a permutation"),
83 (
config(interpolation=
"spline"),
"'interpolation' must be linear or log"),
84 ({
"points": []},
"'points' must be a non-empty list"),
85 ({
"species": list(SPECIES)},
"'points' must be a non-empty list"),
86 ({
"points": [{
"energy": 1}]},
"points[0] must have exactly the keys"),
87 ({
"points": [{
"energy": 1,
"matrix": IDENTITY,
"x": 2}]},
"points[0] must have exactly the keys"),
88 ({
"points": [{
"energy": 1,
"matrix": [[1, 0], [0, 1], [0, 0]]}]},
"points[0].matrix must be 3 rows"),
89 ({
"points": [{
"energy": 1,
"matrix": [[1, 0, 0], [0, 1, 0]]}]},
"points[0].matrix must be 3 rows"),
90 ({
"points": [{
"energy":
"high",
"matrix": IDENTITY}]},
"points[0].energy must be a number"),
91 ({
"points": [{
"energy": 1,
"matrix": [[1, 0, 0], [0,
"a", 0], [0, 0, 1]]}]},
"points[0].matrix[1][1]"),
92 ({
"points": [{
"energy": 1,
"matrix": [[
True, 0, 0], [0, 1, 0], [0, 0, 1]]}]},
"must be a number"),
93 (
config(pdg_overrides=[22]),
"'pdg_overrides' must map PDG codes"),
94 (
config(pdg_overrides={
"gamma":
"electron"}),
"keys must be PDG codes"),
95 (
config(pdg_overrides={22:
"photon"}),
"pdg_overrides[22] must be one of"),
96 (
config(pdg_overrides={22: [
"electron"]}),
"pdg_overrides[22] must be one of"),
100 with pytest.raises(PIDConfigError)
as error:
101 parse(cfg,
"pid.yaml")
102 assert str(error.value).startswith(
"pid.yaml: ")
103 assert message
in str(error.value)
107 with pytest.raises(PIDConfigError, match=
"cannot read the PID file"):
108 load(str(tmp_path /
"missing.yaml"))
112 path = tmp_path /
"broken.yaml"
113 path.write_text(
"points: [\n")
114 with pytest.raises(PIDConfigError, match=
"not valid YAML"):
119 (tmp_path /
"pid.yaml").write_text(yaml.safe_dump(
config()))
120 monkeypatch.setenv(
"PID_TEST_DIR", str(tmp_path))
121 assert load(
"$PID_TEST_DIR/pid.yaml").energies == [1.0]
125 model = load(SHIPPED)
126 assert model.log_interpolation
127 assert model.energies == sorted(model.energies)
128 assert len(model.matrices) == 9 * len(model.energies)
135def ROOT(root_with_calo_scoring_plane):
136 return root_with_calo_scoring_plane
140 from caloScoringPlanePID
import load_pid
142 pid = load_pid(SHIPPED)
143 assert list(pid.GetEnergies()) == load(SHIPPED).energies
144 assert pid.IsLogInterpolation()
148 from caloScoringPlanePID
import load_pid
151 assert pid.Identify(13, 5.0, 0.99) == ROOT.CaloScoringPlanePID.kMuon
155 from caloScoringPlanePID
import load_pid
157 path = tmp_path /
"pid.yaml"
158 path.write_text(yaml.safe_dump({
"points": [{
"energy": 1,
"matrix": [[0.5, 0.4, 0], [0, 1, 0], [0, 0, 1]]}]}))
159 with pytest.raises(PIDConfigError)
as error:
161 assert str(error.value).startswith(f
"{path}: energy point 0")
162 assert "true electron row sums to 0.9" in str(error.value)
166 from caloScoringPlanePID
import load_pid
168 path = tmp_path /
"pid.yaml"
169 path.write_text(
"points:\n - energy: .nan\n matrix: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]\n")
170 with pytest.raises(PIDConfigError, match=
"not finite"):
175 from caloScoringPlanePID
import make_pid
177 pid = make_pid(parse(
config(pdg_overrides={-211:
"muon"})))
178 assert pid.TrueSpecies(211) == ROOT.CaloScoringPlanePID.kMuon
179 assert pid.TrueSpecies(-211) == ROOT.CaloScoringPlanePID.kMuon
183 from caloScoringPlanePID
import load_pid
185 path = tmp_path /
"pid.yaml"
186 path.write_text(yaml.safe_dump(
config(pdg_overrides={211:
"hadron", -211:
"muon"})))
187 with pytest.raises(PIDConfigError)
as error:
189 assert str(path)
in str(error.value)
190 assert "conflicting overrides" in str(error.value)
194 from caloScoringPlanePID
import PIDModel, make_pid
196 with pytest.raises(TypeError):
197 make_pid(PIDModel(energies=[
"1"], matrices=[0.0] * 9))
test_shipped_file_parses()
test_species_order_is_that_of_the_cpp_enum()
test_conflicting_overrides_are_rejected(ROOT, tmp_path)
test_numerical_errors_name_the_file(ROOT, tmp_path)
test_overrides_keep_the_sign()
test_shipped_file_loads_into_the_cpp_class(ROOT)
test_binding_errors_are_not_reported_as_file_errors(ROOT)
test_rows_and_columns_are_reordered()
test_empty_pdg_overrides_key_is_allowed()
test_log_interpolation_and_several_points()
ROOT(root_with_calo_scoring_plane)
test_override_applies_to_both_signs(ROOT)
test_no_file_gives_perfect_identification(ROOT)
test_missing_file(tmp_path)
test_nan_in_the_file_is_rejected(ROOT, tmp_path)
test_invalid_yaml(tmp_path)
test_malformed_files_are_rejected(cfg, message)
test_environment_variables_are_expanded(tmp_path, monkeypatch)