FairShip
Loading...
Searching...
No Matches
event_histogram Namespace Reference

Classes

class  EventHistogram

Functions

int make_event_key (int run, int event)
 _poisson1_cdf ()
np.ndarray replica_weights (int key, int n_replicas)
 _buffer (array, n)
 _cells (h)
 _sumw2 (h)
 _inner2 (h)
int generated_events (tfile)

Variables

tuple _MASK64 = (1 << 64) - 1
 _CDF = _poisson1_cdf()

Detailed Description

Histograms with event-level uncertainties for samples with split branches.

All branches of an event (split kaon/pion clones, their decay products and
secondaries) share the history of the primary interaction, so they are not
independent entries and sqrt(sum w^2) underestimates the error, by a large
factor when many clones of one decay land in the same bin. The independent unit
is the event: sum the weights of an event per bin first,

    t_eb = sum_{i in e, x_i in b} w_i,

and treat the per-event sums as the observations. With N generated events,
including the ones that left no entry,

    mu_b = sum_e t_eb / N,    sigma_b^2 = (sum_e t_eb^2 / N - mu_b^2) / (N - 1).

This is the numpy counterpart of the C++ class EventHistogram (libShipData).
Both write the same ROOT objects, which hadd merges correctly, and give the same
bootstrap replicas for the same event keys::

    h = EventHistogram("p_mu", np.geomspace(5, 400, 21), covariance=True)
    for event in tree:
        h.fill(momenta, weights)
        h.end_event(make_event_key(run, event_id))
    h.add_empty_events(n_generated - h.n_events)
    h.values(), h.errors(), h.n_eff()

Function Documentation

◆ _buffer()

event_histogram._buffer ( array,
n )
protected

Definition at line 73 of file event_histogram.py.

73def _buffer(array, n):
74 array.reshape((n,))
75 return np.frombuffer(array, dtype=np.float64, count=n)
76
77

◆ _cells()

event_histogram._cells ( h)
protected
Writable view of the bin contents of a ROOT histogram, flows included.

Definition at line 78 of file event_histogram.py.

78def _cells(h):
79 """Writable view of the bin contents of a ROOT histogram, flows included."""
80 return _buffer(h.GetArray(), h.GetNcells())
81
82

◆ _inner2()

event_histogram._inner2 ( h)
protected
In-range contents of a TH2D as a writable [x bin, y bin] view.

Definition at line 87 of file event_histogram.py.

87def _inner2(h):
88 """In-range contents of a TH2D as a writable [x bin, y bin] view."""
89 ny, nx = h.GetNbinsY(), h.GetNbinsX()
90 return _cells(h).reshape(ny + 2, nx + 2)[1:-1, 1:-1].T
91
92

◆ _poisson1_cdf()

event_histogram._poisson1_cdf ( )
protected
Poisson(1) distribution function, accumulated as in the C++ code.

k = 0..19, so searchsorted caps the weight at 20 like the C++ inversion.

Definition at line 44 of file event_histogram.py.

44def _poisson1_cdf():
45 """Poisson(1) distribution function, accumulated as in the C++ code.
46
47 k = 0..19, so searchsorted caps the weight at 20 like the C++ inversion.
48 """
49 p = math.exp(-1.0)
50 cdf = [p]
51 for k in range(1, 20):
52 p /= k
53 cdf.append(cdf[-1] + p)
54 return np.array(cdf)
55
56

◆ _sumw2()

event_histogram._sumw2 ( h)
protected

Definition at line 83 of file event_histogram.py.

83def _sumw2(h):
84 return _buffer(h.GetSumw2().GetArray(), h.GetNcells())
85
86

◆ generated_events()

int event_histogram.generated_events ( tfile)
Number of generated events of a FairShip output file, from its FileSummary.

FairShip only writes events with activity, so the number of entries of the
tree is not the normalisation.

Definition at line 308 of file event_histogram.py.

308def generated_events(tfile) -> int:
309 """Number of generated events of a FairShip output file, from its FileSummary.
310
311 FairShip only writes events with activity, so the number of entries of the
312 tree is not the normalisation.
313 """
314 summary = tfile.Get("FileSummary")
315 if not summary:
316 raise KeyError(f"{tfile.GetName()} has no FileSummary")
317 fsr = json.loads(str(summary))
318 for key in ("nev", "nEvents"):
319 if key in fsr:
320 return int(fsr[key])
321 raise KeyError(f"FileSummary of {tfile.GetName()} has no event count ('nev' or 'nEvents')")

◆ make_event_key()

int event_histogram.make_event_key ( int run,
int event )
Key of an event for the bootstrap replicas, as EventHistogram::MakeEventKey.

Definition at line 39 of file event_histogram.py.

39def make_event_key(run: int, event: int) -> int:
40 """Key of an event for the bootstrap replicas, as EventHistogram::MakeEventKey."""
41 return ((run << 32) ^ event) & _MASK64
42
43

◆ replica_weights()

np.ndarray event_histogram.replica_weights ( int key,
int n_replicas )
Poisson(1) weights of one event for replicas 0..n-1 (EventHistogram::ReplicaWeight).

Definition at line 60 of file event_histogram.py.

60def replica_weights(key: int, n_replicas: int) -> np.ndarray:
61 """Poisson(1) weights of one event for replicas 0..n-1 (EventHistogram::ReplicaWeight)."""
62 r = np.arange(1, n_replicas + 1, dtype=np.uint64)
63 with np.errstate(over="ignore"):
64 x = np.uint64(key & _MASK64) ^ (r * np.uint64(0xD1B54A32D192ED03))
65 x = x + np.uint64(0x9E3779B97F4A7C15)
66 x = (x ^ (x >> np.uint64(30))) * np.uint64(0xBF58476D1CE4E5B9)
67 x = (x ^ (x >> np.uint64(27))) * np.uint64(0x94D049BB133111EB)
68 x = x ^ (x >> np.uint64(31))
69 u = (x >> np.uint64(11)).astype(np.float64) * 2.0**-53
70 return np.searchsorted(_CDF, u, side="left")
71
72

Variable Documentation

◆ _CDF

event_histogram._CDF = _poisson1_cdf()
protected

Definition at line 57 of file event_histogram.py.

◆ _MASK64

tuple event_histogram._MASK64 = (1 << 64) - 1
protected

Definition at line 36 of file event_histogram.py.