Source code for analyzer.modules.common.axis

from attrs import define
import abc
import hist
from cattrs.strategies import include_subclasses


@define(frozen=True)
[docs] class Axis(abc.ABC): @abc.abstractmethod
[docs] def toHist(self): ...
@define(frozen=True)
[docs] class IntegerAxis(Axis):
[docs] name: str
[docs] start: int
[docs] stop: int
[docs] unit: str | None = None
[docs] def toHist(self): a = hist.axis.Integer(self.start, self.stop, name=self.name) if self.unit: a.unit = self.unit return a
@define(frozen=True)
[docs] class RegularAxis(Axis):
[docs] bins: int
[docs] start: float
[docs] stop: float
[docs] name: str = ""
[docs] unit: str | None = None
[docs] def toHist(self): a = hist.axis.Regular(self.bins, self.start, self.stop, name=self.name) if self.unit: a.unit = self.unit return a
@define(frozen=True)
[docs] class VariableAxis(Axis):
[docs] edges: list[float | tuple[float, float, int]]
[docs] name: str
[docs] unit: str | None = None
[docs] def toHist(self): import numpy as np def toList(x): if isinstance(x,tuple): return list(float(y) for y in np.arange(*x)) else: return [x] a = hist.axis.Variable( [y for x in self.edges for y in toList(x)], name=self.name ) if self.unit: a.unit = self.unit return a
[docs] def configureConverter(conv): # union_strategy = ft.partial(configure_tagged_union, tag_name="module_name") include_subclasses(Axis, conv) # , union_strategy=union_strategy)