k4bench.analysis.expression¶
k4bench.analysis.expression ¶
Per-event quantities written as expressions.
An expression is one number per event, computed from the event's generator
particles (:meth:k4bench.analysis.event_source.EventSource.particles) and
its per-event columns (what each collection holds, the canned quantities of
:mod:k4bench.analysis.event_drivers)::
count(final & charged & (pt > 1))
sum(E, final & (pdg == 22)) / visible_E
n_VertexBarrelCollection + n_InnerTrackerBarrelCollection
Outside a reduction, a name is a per-event column. Inside one, a name is a particle field, and the reduction folds the particles of each event into one number:
count(mask): the particles the mask selects;sum(x),mean(x),max(x),min(x), or with a mask as their second argument,sum(x, mask): over the particles selected, NaN values skipped. An event with none selected has acountandsumof 0 and amean,maxandminof NaN.
Also allowed: numbers, + - * / ** (a power only to a constant of
magnitude at most :data:MAX_EXPONENT), comparisons, which may chain
(0.5 < vr < 10), &, | and ~ between conditions, unary minus,
and abs, sqrt and log10. and, or and not are not: a
condition is an array, so it combines with &, | and ~, the
comparisons in parentheses. A condition used as a number is 1 or 0.
:func:evaluate never hands the text to Python's eval. It parses it
(:mod:ast) and computes only the node types above, in floating point under
np.errstate(all="ignore"): an expression is at most :data:MAX_LENGTH
characters, :data:MAX_NODES nodes and :data:MAX_DEPTH deep, so none is
dear to compute, and an invalid operation gives NaN rather than an error. A
result that is not finite is NaN.
ExpressionError ¶
Bases: ValueError
An expression that cannot be computed, with why in words.
evaluate ¶
evaluate(expression: str, events: DataFrame, particles: Mapping[str, ndarray] | None = None, event_index: ndarray | None = None) -> Series
expression's value in each event: one number per row of events, whose columns are the per-event names, indexed as events.
particles holds one array per particle field, and event_index the
position in events of each particle's event; without them, an
expression with a reduction cannot be computed. Raises
:class:ExpressionError, saying why, for one that cannot be.
Source code in k4bench/analysis/expression.py
parse ¶
expression parsed and checked against the size limits, without
being computed; :class:ExpressionError when it is no expression or is
too large.