Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
5e8dcbb
[Python][UHI] Support the builtin sum action as a bare index
siliataider Nov 10, 2026
54f1e9f
[Python][UHI] Fix the sum action on 1D histograms with a non-slice index
siliataider Nov 10, 2026
732fd96
[Python][UHI] Include the flow bins in the sum over all axes of 2D an…
siliataider Nov 10, 2026
6155e82
[Python][UHI] Do not reuse the histogram of a previous projection for…
siliataider Nov 10, 2026
accfcae
[Python][UHI] Do not slice profiles with the instantiation of Slice f…
siliataider Nov 10, 2026
aab977f
[Python][UHI] Keep the flow bins of each axis when slicing 2D and 3D …
siliataider Nov 10, 2026
9dde00c
[UHI] Slice the sum of squares of weights together with the bin contents
siliataider Nov 10, 2026
7512e03
[Python][UHI] Support negative bin indices other than -1
siliataider Nov 10, 2026
508a809
[Python][UHI] Do not modify loc when adding or subtracting an offset
siliataider Nov 10, 2026
b692754
[Python][UHI] Do not extend the axis when locating a bin with loc
siliataider Nov 10, 2026
fbf99ff
[Python][UHI] Return the number of entries as the counts of profiles
siliataider Nov 10, 2026
f59becd
[Python][UHI] Return the variance of the mean as the variances of pro…
siliataider Nov 10, 2026
0291cf1
[hist][UHI] Slice the bin entries of profiles together with the bin c…
siliataider Nov 10, 2026
003a14d
[Python][UHI] Do not serialize the axis range as the axis edges
siliataider Nov 10, 2026
956d8ac
[Python][UHI] Serialize axes with variable bin sizes as variable axes
siliataider Nov 10, 2026
0812681
[Python][UHI] Serialize all storage arrays with the flow bins
siliataider Nov 10, 2026
6f41876
[Python][UHI] Serialize unweighted profiles with the mean storage
siliataider Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from contextlib import contextmanager

from .plotting import _values_by_copy
from .tags import _get_axis_len, _overflow, _rebin, _sum, _underflow
from .tags import _get_axis, _get_axis_len, _overflow, _rebin, _sum, _underflow

"""
Implementation of the indexing component of the UHI
Expand Down Expand Up @@ -41,15 +41,15 @@ def _process_index_for_axis(self, index, axis, is_slice_stop=False):
return _get_axis_len(self, axis) + 1 if index is len else index(self, axis)

if isinstance(index, int):
# -1 index returns the last valid bin
if index == -1:
return _overflow(self, axis) - 1
# Negative indices count from the last valid bin
if index < 0:
index += _get_axis_len(self, axis)

# Shift the indices by 1 to align with the UHI convention,
# where 0 corresponds to the first bin, unlike ROOT where 0 represents underflow and 1 is the first bin.
nbins = _get_axis_len(self, axis) + (1 if is_slice_stop else 0)
index = index + 1
if abs(index) > nbins:
if index < 1 or index > nbins:
raise IndexError(f"Histogram index {index - 1} out of range for axis {axis}. Valid range: (0,{nbins})")
return index

Expand All @@ -58,7 +58,7 @@ def _process_index_for_axis(self, index, axis, is_slice_stop=False):

def _compute_uhi_index(self, index, axis, flow=True):
"""Convert tag functors to valid bin indices."""
if isinstance(index, _rebin) or index is _sum:
if isinstance(index, _rebin) or index is _sum or index is sum:
index = slice(None, None, index)

if callable(index) or isinstance(index, int):
Expand Down Expand Up @@ -119,26 +119,33 @@ def _resolve_slice_indices(self, index, axis, flow=True):
return start, stop


def _apply_actions(hist, actions, index, unprocessed_index, original_hist):
def _apply_actions(hist, actions, index, original_hist):
"""Apply rebinning or summing actions to the histogram, returns a new histogram"""
if not actions or all(a is None for a in actions):
return hist

if any(a is _sum or a is sum for a in actions):
sum_axes = tuple(i for i, a in enumerate(actions) if a is _sum or a is sum)
if original_hist.GetDimension() == 1:
# For the integral of a 1D histogram, we need special handling for the flow bins
if len(sum_axes) == original_hist.GetDimension():
# For the integral over all axes, we need special handling for the flow bins
# h[::sum] is equivalent to h.Integral(0, nbins+1)
# h[0:len:sum] is equivalent to h.Integral(1, nbins)
start, stop = index[0].start, index[0].stop
include_oflow = True if unprocessed_index.stop is None else False
args = [start, stop - (1 if not include_oflow else 0)]
args = []
for idx in index:
start, stop = (idx, idx + 1) if isinstance(idx, int) else (idx.start, idx.stop)
args += [start, stop - 1]
hist = _sum(original_hist, sum_axes, args)
else:
hist = _sum(hist, sum_axes)
import ROOT

# Do not register the projection in gDirectory, where ROOT would reuse it for the next projection
with ROOT.TDirectory.TContext(ROOT.nullptr):
hist = _sum(hist, sum_axes)
# No directory owns the projection, so Python has to delete it
ROOT.SetOwnership(hist, True)

if any(isinstance(a, _rebin) for a in actions):
rebins = [a.ngroup if isinstance(a, _rebin) else 1 for a in actions if a is not _sum]
rebins = [a.ngroup if isinstance(a, _rebin) else 1 for a in actions if a is not _sum and a is not sum]
hist = _rebin(rebins)(hist)

if any(a is not None and not (isinstance(a, _rebin) or a is _sum or a is sum) for a in actions):
Expand All @@ -165,7 +172,7 @@ def _get_processed_slices(self, index):
return processed_slices, actions


def _slice_get(self, index, unprocessed_index):
def _slice_get(self, index):
"""
This method creates a new histogram containing only the data from the
specified slice.
Expand All @@ -180,9 +187,19 @@ def _slice_get(self, index, unprocessed_index):
processed_slices, actions = _get_processed_slices(self, index)
args_vec = ROOT.std.vector("Int_t")([item for pair in processed_slices for item in pair])

target_hist = ROOT.Internal.Slice(self, args_vec)
# Instantiate Slice for the class of the histogram, otherwise a profile can be passed to the
# instantiation for its base class (e.g. Slice<TH1D> for a TProfile), which cannot copy it
target_hist = ROOT.Internal.Slice[self.ClassName()](self, args_vec)

# The bins cut by the slice fall into the flow bins of the sliced histogram,
# so the summed axes only include the flow bins of open slices
for axis, ((start, stop), action) in enumerate(zip(processed_slices, actions)):
if action is _sum or action is sum:
first = 0 if start == _underflow(self, axis) else 1
last = _get_axis_len(target_hist, axis) + (1 if stop > _overflow(self, axis) else 0)
_get_axis(target_hist, axis).SetRange(first, last)

return _apply_actions(target_hist, actions, index, unprocessed_index, self)
return _apply_actions(target_hist, actions, index, self)


def _slice_set(self, index, unprocessed_index, value):
Expand Down Expand Up @@ -231,15 +248,15 @@ def _slice_set(self, index, unprocessed_index, value):

ROOT.Internal.SetSliceContent(self, value, slice_edges)

_apply_actions(self, actions, index, unprocessed_index, self)
_apply_actions(self, actions, index, self)


def _getitem(self, index):
uhi_index = _compute_common_index(self, index)
if all(isinstance(i, int) for i in uhi_index):
return self.GetBinContent(*uhi_index)
if any(isinstance(i, slice) for i in uhi_index):
return _slice_get(self, uhi_index, index)
return _slice_get(self, uhi_index)


def _setitem(self, index, value):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,38 @@ def _variances(self, flow=False) -> np.typing.NDArray[Any]: # noqa: F821
sum_of_weights_squared = _get_sum_of_weights_squared(self, flow=flow)

if _kind(self) == Kind.MEAN:
# For profiles, the values are the means and the Sumw2 array holds the sum of w*y^2 of each bin.
# Return the variance of the mean: the unbiased variance of y divided by the sum of weights
counts = self.counts(flow=flow)
variances = sum_of_weights_squared.copy()
bin_entries = _get_profile_bin_array(self, self.GetBinEntries, flow=flow)
with np.errstate(divide="ignore", invalid="ignore"):
variances = (sum_of_weights_squared / bin_entries - sum_of_weights**2) * counts / (counts - 1) / bin_entries
variances[counts <= 1] = np.nan
return variances

return sum_of_weights_squared


def _get_profile_bin_array(self, getter, flow=False) -> np.typing.NDArray[Any]: # noqa: F821
"""
Return `getter(bin)` for each bin of a profile as a numpy array, `bin` being the global bin number.
"""
from itertools import product

import numpy as np

offset = 0 if flow else 1
dimensions = [range(offset, _get_axis_len(self, axis, flow=flow) + offset) for axis in range(self.GetDimension())]
return np.array([getter(self.GetBin(*bin)) for bin in product(*dimensions)]).reshape(_shape(self, flow=flow))


def _counts(self, flow=False) -> np.typing.NDArray[Any]: # noqa: F821
import numpy as np

if _kind(self) == Kind.MEAN:
# Number of entries, or effective number of entries for weighted profiles
return _get_profile_bin_array(self, self.GetBinEffectiveEntries, flow=flow)

sum_of_weights = self.values(flow=flow)

if not _hasWeights(self):
Expand All @@ -217,10 +238,6 @@ def _counts(self, flow=False) -> np.typing.NDArray[Any]: # noqa: F821
)


def _get_sum_of_weights(self) -> np.typing.NDArray[Any]: # noqa: F821
return self.values()


def _get_sum_of_weights_squared(self, flow=False) -> np.typing.NDArray[Any]: # noqa: F821
import numpy as np

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ def _axis_to_dict(root_axis: ROOT.TAxis, uhi_axis: PlottableAxisBase) -> dict[st
"""
Return a dictionary representation of the given ROOT axis.
"""
if root_axis.IsVariableBinSize():
return {
"type": "variable",
"edges": [root_axis.GetBinLowEdge(i) for i in range(1, root_axis.GetNbins() + 2)],
"underflow": uhi_axis.traits.underflow,
"overflow": uhi_axis.traits.overflow,
"circular": uhi_axis.traits.circular,
}

return {
"type": "regular",
"lower": root_axis.GetBinLowEdge(root_axis.GetFirst()),
"upper": root_axis.GetBinUpEdge(root_axis.GetLast()),
"lower": root_axis.GetXmin(),
"upper": root_axis.GetXmax(),
"bins": root_axis.GetNbins(),
"underflow": uhi_axis.traits.underflow,
"overflow": uhi_axis.traits.overflow,
Expand All @@ -53,12 +62,14 @@ def _axis_from_dict(axis_dict: dict[str, Any]) -> list[Any]:


def _storage_to_dict(hist: Any) -> dict[str, Any]:
from .plotting import _get_sum_of_weights, _get_sum_of_weights_squared, _hasWeights
import numpy as np

from .plotting import _get_profile_bin_array, _hasWeights

"""
Logic:
- If histogram is a profile (TProfile*) --> Kind="MEAN":
- if histogram has Sumw2: type is weighted_mean_storage (if _hasWeights(hist))
- if profile has the sum of squares of weights (TProfile::Sumw2): type is weighted_mean_storage
- else: storage type is mean_storage
- Else (TH1*/TH2*/TH3*) --> Kind="COUNT":
- if histogram has Sumw2: type is weighted_storage
Expand All @@ -70,20 +81,22 @@ def _storage_to_dict(hist: Any) -> dict[str, Any]:
}

if hist.kind == "MEAN":
storage_dict["variances"] = hist.variances()
# The storage holds the variance of y, while variances() returns the variance of the mean
sum_of_weights = _get_profile_bin_array(hist, hist.GetBinEntries, flow=True)
storage_dict["variances"] = np.nan_to_num(hist.variances(flow=True) * sum_of_weights)

if _hasWeights(hist):
if hist.GetBinSumw2().fN:
storage_dict["type"] = "weighted_mean"
storage_dict["sum_of_weights"] = _get_sum_of_weights(hist)
storage_dict["sum_of_weights_squared"] = _get_sum_of_weights_squared(hist)
storage_dict["sum_of_weights"] = sum_of_weights
storage_dict["sum_of_weights_squared"] = _get_profile_bin_array(hist, hist.GetBinSumw2().At, flow=True)
else:
storage_dict["type"] = "mean"
storage_dict["counts"] = hist.counts()
storage_dict["counts"] = hist.counts(flow=True)

else: # COUNT
if _hasWeights(hist):
storage_dict["type"] = "weighted"
storage_dict["variances"] = hist.variances()
storage_dict["variances"] = hist.variances(flow=True)
else:
if hist.ClassName().endswith("I"):
storage_dict["type"] = "int"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,20 @@ def __init__(self, value: float) -> None:

def __add__(self, other: int) -> _loc:
if isinstance(other, int):
self.offset += other
return self
shifted = _loc(self.value)
shifted.offset = self.offset + other
return shifted
raise TypeError(f"Unsupported type for addition: {type(other).__name__}. Expected an integer.")

def __sub__(self, other: int) -> _loc:
if isinstance(other, int):
self.offset -= other
return self
shifted = _loc(self.value)
shifted.offset = self.offset - other
return shifted
raise TypeError(f"Unsupported type for substraction: {type(other).__name__}. Expected an integer.")

def __call__(self, hist: Any, axis: int) -> int:
return _get_axis(hist, axis).FindBin(self.value) + self.offset
return _get_axis(hist, axis).FindFixBin(self.value) + self.offset


class _rebin:
Expand Down Expand Up @@ -103,7 +105,7 @@ def _invalid_axis(axis, dim):
elif axis == (1,):
return hist.ProjectionX()
elif axis == (0, 1):
return hist.Integral()
return hist.Integral(*args)
else:
return _invalid_axis(axis, dim)
if dim == 3:
Expand All @@ -117,6 +119,6 @@ def _invalid_axis(axis, dim):
(1, 2): "x",
}
if axis == (0, 1, 2):
return hist.Integral()
return hist.Integral(*args)
return hist.Project3D(project_map[axis]) if axis in project_map else _invalid_axis(axis, dim)
raise NotImplementedError(f"Summing not implemented for {dim}D histograms")
Loading
Loading