4"""Provenance of split kaon/pion tracks: which tracks can exist together.
6Splitting (exitHadronAbsorber, --kaon-pion-splits) replaces the decay of a
7kaon or pion by weighted alternatives. They are mutually exclusive histories,
8not particles that exist together, but in the MC track tree they look like
9siblings of the split track. ShipMCTrack records them in split sets:
11* every track created for one split track shares its split set
12 (GetSplitSet(), -1 if none);
13* the split track and its continuations are the survivor line
14 (kSplitSurvivor), the clones each stand for one of its decays
15 (kSplitDecay), with probability weight / GetSplitWeight();
16* GetSplitWeight() is the weight of the split track before splitting.
18Single-particle quantities need none of this: sum weights as usual. Anything
19that combines several tracks of an event does:
21* compatible(i, j): can tracks i and j exist in the same real event? Two
22 different decay alternatives of one set cannot. A decay at time t and a
23 track that left the survivor line at time t' can only if t' < t.
24* pair_weight(i, j): the weight that makes pair observables unbiased,
25 w_i w_j / w_a, where w_a is the weight of the line at the point where the
26 two tracks part. It is 0 for incompatible pairs.
27* desplit(rng): draw one outcome per split set with the right probabilities
28 and return which tracks exist in that analog-like event. Each kept track
29 then stands for the event with the weight of its primary. Useful for
30 multiplicities, coincidences, triggers and occupancy, which weights cannot
33The weights of a survivor line's secondaries must be the line's weight when
34they were created. Times are the start times of the tracks (ns).
38 prov = SplitProvenance.from_tracks(event.MCTrack)
39 w = prov.pair_weight(mu1, mu2) # 0 if the muons are alternatives
40 keep = prov.desplit(rng).keeps_track # analog-like selection of tracks
44from dataclasses
import dataclass
48NOT_SPLIT, SURVIVOR, DECAY = 0, 1, 2
52@dataclass(frozen=True)
54 """How a lineage passes through one split set."""
64 """Split-set structure of one event; see the module docstring."""
66 def __init__(self, mother, split_set, split_role, split_weight, start_t, weight, proc_id=None):
67 self.
mother = np.asarray(mother, dtype=int)
71 self.
start_t = np.asarray(start_t, dtype=float)
72 self.
weight = np.asarray(weight, dtype=float)
76 np.zeros(len(self.
mother), dtype=bool)
78 else (np.asarray(proc_id, dtype=int) == K_NO_PROCESS) & (self.
split_role == SURVIVOR)
81 for i
in np.flatnonzero(self.
split_set >= 0):
82 entry = self.
sets.setdefault(
85 entry[
"decays" if self.
split_role[i] == DECAY
else "survivors"].append(int(i))
86 for entry
in self.
sets.values():
87 entry[
"decays"].sort(key=
lambda c: self.
start_t[c])
88 entry[
"survivors"].sort(key=
lambda c: self.
start_t[c])
89 entry[
"stored"] = float(sum(self.
weight[c]
for c
in entry[
"decays"]))
94 """From a sequence of ShipMCTrack (e.g. event.MCTrack)."""
108 return cls([], [], [], [], [], [], [])
109 return cls(*zip(*rows))
117 """Track i and its ancestors, nearest first."""
120 while self.
mother[line[-1]] >= 0
and len(line) <= len(self):
121 line.append(int(self.
mother[line[-1]]))
126 """Weight of track i when it was created (before any split of its own)."""
130 """Weight of the survivor line where survivor track a starts."""
137 return self.
weight[earlier[-1]]
if earlier
else self.
weight[a]
139 def _links(self, i, t_obs, at_start=False):
140 """Split sets on the lineage of i (at most one member each, since members are siblings).
142 If i is itself on a survivor line, it is observed at t_obs with its
143 stored weight (the weight at the end of its path, e.g. at the scoring
144 plane where tracks stop), or with at_start at its own start.
148 for k, a
in enumerate(line):
154 elif k == 0
and at_start:
157 links[int(s)] =
_Link(int(s), SURVIVOR, a, t_obs, self.
weight[a])
166 """Whether tracks i and j can exist in the same real event.
168 t_i, t_j: when a survivor-line track is itself the observed object (e.g.
169 a hit of the split kaon), the time of that observation.
172 for s
in li.keys() & lj.keys():
174 if a.member == b.member:
176 if a.role == DECAY
and b.role == DECAY:
178 if a.role == DECAY
and not b.branch_time < a.branch_time:
180 if b.role == DECAY
and not a.branch_time < b.branch_time:
185 """Weight of the pair (i, j) for unbiased pair observables; 0 if incompatible."""
186 if i == j
or not self.
compatible(i, j, t_i, t_j):
190 m = next((a
for a
in line_i
if a
in on_j),
None)
198 ci = line_i[line_i.index(m) - 1]
199 cj = line_j[line_j.index(m) - 1]
204 a, b = self.
_links(i, t_i)[int(s)], self.
_links(j, t_j)[int(s)]
206 w_a = b.branch_weight
207 elif b.role == DECAY:
208 w_a = a.branch_weight
210 w_a = max(a.branch_weight, b.branch_weight)
219 """Draw one outcome per split set; see Desplit."""
224 """One analog-like realisation of a split event.
226 For every split set one outcome is drawn with probability proportional to
227 its weight: one of the stored decay alternatives, or "something else" (a
228 decay whose clones were pruned, or no decay at all). In the latter case a
229 second number decides how far along the survivor line the track got, so
230 that survivor-line tracks are kept consistently. A kept track stands for
231 the event with the weight of its primary, analog_weight(i).
237 for s, e
in prov.sets.items():
238 u = rng.random() * e[
"W"]
239 chosen, cum =
None, 0.0
240 for c
in e[
"decays"]:
241 if u < cum + prov.weight[c]:
244 cum += prov.weight[c]
246 self.
choice[s] = (chosen, u - e[
"stored"])
251 chosen, v = self.
choice[s]
252 if chosen
is not None:
253 return branch_time < prov.start_t[chosen]
254 later = sum(prov.weight[c]
for c
in prov.sets[s][
"decays"]
if prov.start_t[c] > branch_time)
255 return v < branch_weight - later
258 """Whether track i exists, or for t_obs its observation at that time."""
259 links = self.
prov._links(i, math.inf, at_start=
True)
if t_obs
is None else self.
prov._links(i, t_obs)
260 for s, link
in links.items():
261 if link.role == DECAY:
262 if self.
choice[s][0] != link.member:
264 elif not self.
_survives_to(s, link.branch_time, link.branch_weight):
269 """Whether a hit of track i at time t exists (matters for survivor-line tracks)."""
270 return self.
keeps(i, t_obs=t)
273 """Weight a kept track carries in the de-split event: that of its primary."""
274 return self.
prov.initial_weight(self.
prov.lineage(i)[-1])
__init__(self, prov, rng)
_survives_to(self, s, branch_time, branch_weight)
keeps(self, i, t_obs=None)
_line_weight_at_start(self, a)
compatible(self, i, j, t_i=math.inf, t_j=math.inf)
__init__(self, mother, split_set, split_role, split_weight, start_t, weight, proc_id=None)
pair_weight(self, i, j, t_i=math.inf, t_j=math.inf)
_links(self, i, t_obs, at_start=False)