FairShip
Loading...
Searching...
No Matches
test_split_provenance.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
3
4import itertools
5import math
6
7import numpy as np
8import pytest
9from split_provenance import DECAY, K_NO_PROCESS, NOT_SPLIT, SURVIVOR, SplitProvenance
10
11G4 = 13 # any Geant4 process other than kPNoProcess
12
13
14def synthetic_event(pruned=()):
15 """A primary M with a per-step split kaon K, a split-once kaon K2 and a sibling.
16
17 K (set 0, weight 1) is split at t = 2..6 with decay probabilities P, two
18 clones per step. Geant4 decays it at t = 4 on a split step, so it is
19 continued by C, which ends by interacting at t = 7. Secondaries are created
20 along the line with the line's weight at that time. K2 (set 1) decays at
21 t = 3 into four clones of weight 1/4. Clones are pushed with K's mother.
22 """
23 P = {2.0: 0.2, 3.0: 0.25, 4.0: 0.3, 5.0: 0.15, 6.0: 0.4}
24 line = {1.0: 1.0}
25 w = 1.0
26 clones = []
27 for t, p in P.items():
28 clones += [(t, w * p / 2)] * 2
29 w *= 1 - p
30 line[t] = w
31 rows: dict[str, tuple] = {} # name: (mother, set, role, split weight, start, weight, proc)
32 rows["M"] = (None, -1, NOT_SPLIT, 0.0, 0.0, 1.0, 0)
33 rows["s"] = ("M", -1, NOT_SPLIT, 0.0, 1.0, 1.0, G4)
34 rows["K"] = ("M", 0, SURVIVOR, 1.0, 1.0, line[4.0], G4)
35 rows["C"] = ("M", 0, SURVIVOR, 1.0, 4.0, line[6.0], K_NO_PROCESS)
36 for k, (t, wc) in enumerate(clones):
37 rows[f"c{k}"] = ("M", 0, DECAY, 1.0, t, wc, K_NO_PROCESS)
38 rows["y1"] = ("K", -1, NOT_SPLIT, 0.0, 2.5, line[2.0], G4)
39 rows["y2"] = ("C", -1, NOT_SPLIT, 0.0, 5.5, line[5.0], G4)
40 rows["z"] = ("C", -1, NOT_SPLIT, 0.0, 7.0, line[6.0], G4)
41 rows["mu_a"] = ("c2", -1, NOT_SPLIT, 0.0, 3.0, clones[2][1], G4) # from a decay at t = 3
42 rows["mu_b"] = ("c6", -1, NOT_SPLIT, 0.0, 5.0, clones[6][1], G4) # from a decay at t = 5
43 rows["K2"] = ("M", 1, SURVIVOR, 1.0, 1.0, 1.0, G4)
44 for k in range(4):
45 rows[f"d{k}"] = ("M", 1, DECAY, 1.0, 3.0, 0.25, K_NO_PROCESS)
46 rows["mu_c"] = ("d0", -1, NOT_SPLIT, 0.0, 3.0, 0.25, G4)
47 names = [n for n in rows if n not in pruned]
48 index = {n: i for i, n in enumerate(names)}
49 cols = [[] for _ in range(7)]
50 for n in names:
51 mother, *rest = rows[n]
52 cols[0].append(index[mother] if mother is not None else -1)
53 for c, v in zip(cols[1:], rest):
54 c.append(v)
55 return SplitProvenance(*cols), index, line
56
57
58PRUNED = ("c1", "c9", "d2", "d3")
59
60
61@pytest.fixture
62def event():
63 return synthetic_event(PRUNED)
64
65
67 prov, ix, _ = event
68 assert not prov.compatible(ix["c0"], ix["c2"]) # two decay times
69 assert not prov.compatible(ix["d0"], ix["d1"]) # two clones of one decay
70 assert not prov.compatible(ix["mu_a"], ix["mu_b"]) # their muons
71
72
74 prov, ix, _ = event
75 assert prov.compatible(ix["mu_a"], ix["y1"]) # y1 emitted at 2.5, decay at 3
76 assert not prov.compatible(ix["c0"], ix["y1"]) # decay at 2, before y1
77 assert not prov.compatible(ix["mu_b"], ix["y2"]) # decay at 5, y2 at 5.5
78 assert not prov.compatible(ix["mu_a"], ix["z"]) # z needs survival to the end
79 assert prov.compatible(ix["y1"], ix["y2"]) # both on the survivor line
80
81
83 prov, ix, _ = event
84 assert prov.compatible(ix["mu_a"], ix["mu_c"]) # different kaons
85 assert prov.compatible(ix["mu_a"], ix["s"]) # real sibling
86
87
89 prov, ix, line = event
90 pw = prov.pair_weight
91 assert pw(ix["mu_a"], ix["y1"]) == pytest.approx(prov.weight[ix["mu_a"]])
92 assert pw(ix["mu_b"], ix["y2"]) == 0.0
93 assert pw(ix["y1"], ix["y2"]) == pytest.approx(line[5.0]) # survive to 5.5
94 assert pw(ix["mu_a"], ix["mu_c"]) == pytest.approx(prov.weight[ix["mu_a"]] * 0.25)
95 assert pw(ix["s"], ix["mu_a"]) == pytest.approx(prov.weight[ix["mu_a"]])
96
97
98def _desplit_rates(prov, n_draws, seed):
99 rng = np.random.default_rng(seed)
100 keep = np.array([prov.desplit(rng).keeps_track for _ in range(n_draws)])
101 return keep, keep.mean(axis=0)
102
103
105 prov, ix, line = event
106 n = 40000
107 keep, rate = _desplit_rates(prov, n, seed=1)
108 expected = {name: prov.weight[i] for name, i in ix.items()}
109 # Survivor tracks exist from their start: the original always, the
110 # continuation if the line survived to t = 4.
111 expected.update(K=1.0, K2=1.0, C=line[4.0])
112 for name, i in ix.items():
113 p = expected[name]
114 tol = 4 * math.sqrt(max(p * (1 - p), 1e-12) / n) + 1e-12
115 assert rate[i] == pytest.approx(p, abs=tol), name
116 # Pairs of non-survivor tracks: kept together at the pair weight.
117 others = [i for name, i in ix.items() if name not in ("K", "C", "K2", "M")]
118 for i, j in itertools.combinations(others, 2):
119 p = prov.pair_weight(i, j)
120 together = np.mean(keep[:, i] & keep[:, j])
121 tol = 4 * math.sqrt(max(p * (1 - p), 1e-12) / n) + 1e-12
122 assert together == pytest.approx(p, abs=tol), (i, j)
123
124
126 full, ix_full, _ = synthetic_event()
127 pruned, ix, _ = synthetic_event(PRUNED)
128 for a, b in [("mu_a", "y1"), ("y1", "y2"), ("mu_a", "mu_c"), ("mu_b", "z")]:
129 assert pruned.pair_weight(ix[a], ix[b]) == pytest.approx(full.pair_weight(ix_full[a], ix_full[b]))
130
131
133 prov, _, _ = event
134 a = prov.desplit(np.random.default_rng(7)).keeps_track
135 b = prov.desplit(np.random.default_rng(7)).keeps_track
136 assert np.array_equal(a, b)
137
138
140 ROOT = root_with_shipdata
141 if not hasattr(ROOT.ShipMCTrack, "GetSplitSet"):
142 pytest.skip("libShipData with split sets not available")
143 mother = ROOT.ShipMCTrack(321, -1, 0.0, 0.0, 50.0, 0.4937, 0.0, 0.0, 0.0, 1.0, 0, 0, 0, 1.0)
144 clone = ROOT.ShipMCTrack(321, -1, 0.0, 0.0, 50.0, 0.4937, 0.0, 0.0, 10.0, 2.0, 0, 0, 1, 0.5)
145 clone.SetSplitSet(0, DECAY, 1.0)
146 clone.SetProcID(K_NO_PROCESS)
147 mother.SetSplitSet(0, SURVIVOR, 1.0)
148 mother.SetProcID(G4) # the constructor defaults to kPNoProcess
149 prov = SplitProvenance.from_tracks([mother, clone])
150 assert prov.sets[0]["decays"] == [1]
151 assert prov.sets[0]["survivors"] == [0]
152 assert prov.continuation.tolist() == [False, False]
test_from_tracks_reads_ship_mc_track(root_with_shipdata)
test_decay_versus_survivor_line_follows_time(event)
test_desplit_reproduces_weights_and_pair_weights(event)
_desplit_rates(prov, n_draws, seed)
test_independent_splits_and_siblings_are_compatible(event)
test_alternatives_of_one_decay_are_incompatible(event)