1"""Python representation of computed observable values.
3The C++ observable layer returns ``ObservableValue`` objects containing an
4observable id, a numerical prediction, and optionally a bin range. This module
5keeps the public Python type lightweight and immutable while preserving explicit
6conversion helpers to and from the bound C++ object.
9from __future__
import annotations
11from dataclasses
import dataclass
12from typing
import Optional, Tuple
14from pyhyperiso.phyperiso.pyhyperiso.observable
import ObservableValue
as _CppObservableValue
21 if not isinstance(value, ObservableId):
22 raise TypeError(f
"{name} must be un ObservableId Python, received {type(value)!r}.")
30@dataclass(frozen=True)
32 """Value returned by an observable computation.
35 id: Internal observable identifier.
36 value: Numerical prediction or experimental value.
37 bin: Optional bin range ``(low, high)``. ``None`` means that the
38 observable is unbinned or that the C++ value did not carry bin
42 >>> from pyhyperiso.core.BusinessLogic.ObservableValue import ObservableValue
43 >>> from pyhyperiso.core.Common.GeneralEnum import Observables
44 >>> ov = ObservableValue.from_observable(Observables.BR_BS_MUMU, 3.6e-9)
51 bin: Optional[Tuple[float, float]] =
None
54 """Validate and normalize the immutable dataclass fields.
57 TypeError: If ``id`` is not an ``ObservableId`` or if ``bin`` is
58 neither ``None`` nor a two-entry tuple.
61 if self.
binbin is not None:
63 raise TypeError(
"bin must be None ou un tuple (low, high).")
64 object.__setattr__(self,
"bin", (float(self.
binbin[0]), float(self.
binbin[1])))
65 object.__setattr__(self,
"value", float(self.
valuevalue))
72 bin: Optional[Tuple[float, float]] =
None,
73 ) ->
"ObservableValue":
74 """Create an observable value from a public observable enum.
77 obs: Public observable enum value.
78 value: Numerical value to store.
79 bin: Optional bin range ``(low, high)`` for binned observables.
82 A normalized ``ObservableValue`` instance.
85 TypeError: If ``obs`` is not an ``Observables`` enum value.
87 if not isinstance(obs, Observables):
88 raise TypeError(f
"obs must be un Observables Python, received {type(obs)!r}.")
92 def from_cpp(cls, cpp_obj) -> "ObservableValue":
93 """Wrap a bound C++ ``ObservableValue`` object.
96 cpp_obj: Bound C++ object exposing ``id``, ``value`` and optional
100 Equivalent Python ``ObservableValue`` instance.
104 py_bin =
None if b
is None else (float(b[0]), float(b[1]))
105 return cls(id=py_id, value=float(cpp_obj.value), bin=py_bin)
108 """Convert this value to the bound C++ representation.
111 A C++ ``ObservableValue`` pybind11 object.
115 return _CppObservableValue(cpp_id, float(self.
valuevalue))
static IdOf< ObservableTag > to_id(Observables e)
Converts an enum value to an IdOf<Tag>.
"ObservableValue" from_cpp(cls, cpp_obj)
"ObservableValue" from_observable(cls, Observables obs, float value, Optional[Tuple[float, float]] bin=None)
ObservableId _require_observable_id(ObservableId value, str name="observable id")
_cpp_observable_id(ObservableId value)