From 5e8dcbbb76d6cf2a04a6f325cd1712a6513f085b Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 11:20:49 +0100 Subject: [PATCH 01/17] [Python][UHI] Support the builtin sum action as a bare index The UHI sum action is the builtin `sum` but only `ROOT.uhi.sum` was normalized to `slice(None, None, sum)` in `_compute_uhi_index`. h2 = TH2D with 2x2 bins [[1, 2], [3, 4]] Before After h2[sum, 0:1] TypeError: Unsupported index type: float [4] h2[::sum, 0:1] [4] [4] Assisted-by: Claude Opus 5.5 --- .../ROOT/_pythonization/_uhi/indexing.py | 4 +-- .../pythonizations/test/uhi_indexing.py | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py index 0bf4d341db114..f271275023d89 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py @@ -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): @@ -138,7 +138,7 @@ def _apply_actions(hist, actions, index, unprocessed_index, original_hist): hist = _sum(hist, sum_axes) 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): diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index 1ff3ccf867b84..97cc787742a66 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -2,6 +2,8 @@ Tests to verify that TH1 and derived histograms conform to the UHI Indexing interfaces (setting, accessing and slicing). """ +import builtins + import numpy as np import pytest import ROOT @@ -269,6 +271,31 @@ def test_slicing_with_action_rebin_and_sum(self, hist_setup): assert sliced_hist.GetNbinsX() == hist_setup.GetNbinsX() // 2 assert sliced_hist.GetNbinsY() == 2 + def test_action_builtin_sum(self, hist_setup): + # The UHI sum action is the builtin `sum`, it must behave like ROOT.uhi.sum + if _special_setting(hist_setup): + pytest.skip("Slicing cannot be tested here") + + dim = hist_setup.GetDimension() + + if dim == 1: + assert hist_setup[:: builtins.sum] == hist_setup[::sum] + assert hist_setup[0 : len : builtins.sum] == hist_setup[0:len:sum] + + if dim == 2: + sum_y = hist_setup[builtins.sum, 0:1] + assert isinstance(sum_y, ROOT.TH1) + assert np.array_equal(sum_y.values(), hist_setup[::sum, 0:1].values()) + + sum_x = hist_setup[:, builtins.sum] + assert isinstance(sum_x, ROOT.TH1) + assert np.array_equal(sum_x.values(), hist_setup[:, ::sum].values()) + + if dim == 3: + sliced_hist = hist_setup[:: rebin(2), builtins.sum, 3 : 9 : rebin(3)] + assert isinstance(sliced_hist, ROOT.TH2) + assert np.array_equal(sliced_hist.values(), hist_setup[:: rebin(2), ::sum, 3 : 9 : rebin(3)].values()) + def test_slicing_with_dict_syntax(self, hist_setup): if _special_setting(hist_setup): pytest.skip("Setting cannot be tested here") From 54f1e9f4900a5621994f1f00f145f02a27055c1a Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 11:28:50 +0100 Subject: [PATCH 02/17] [Python][UHI] Fix the sum action on 1D histograms with a non-slice index The integral bounds were read from the `stop` of the raw user index, which is not a slice for `h[sum]`, tuple or dict indices. h = TH1D with 3 bins [1, 2, 3] Before After h[::sum] 6 6 h[sum] AttributeError: no attribute 'stop' 6 h[(slice(None, None, sum),)] AttributeError: no attribute 'stop' 6 h[{0: slice(None, None, sum)}] AttributeError: no attribute 'stop' 6 Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/indexing.py | 13 ++++++------- .../pyroot/pythonizations/test/uhi_indexing.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py index f271275023d89..f4cd923fc4fe2 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py @@ -119,7 +119,7 @@ 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 @@ -131,8 +131,7 @@ def _apply_actions(hist, actions, index, unprocessed_index, original_hist): # 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 = [start, stop - 1] hist = _sum(original_hist, sum_axes, args) else: hist = _sum(hist, sum_axes) @@ -165,7 +164,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. @@ -182,7 +181,7 @@ def _slice_get(self, index, unprocessed_index): target_hist = ROOT.Internal.Slice(self, args_vec) - 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): @@ -231,7 +230,7 @@ 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): @@ -239,7 +238,7 @@ def _getitem(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): diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index 97cc787742a66..a23a36f2db64c 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -296,6 +296,22 @@ def test_action_builtin_sum(self, hist_setup): assert isinstance(sliced_hist, ROOT.TH2) assert np.array_equal(sliced_hist.values(), hist_setup[:: rebin(2), ::sum, 3 : 9 : rebin(3)].values()) + def test_action_sum_index_forms_1d(self, hist_setup): + # A bare sum, a tuple or a dict index must give the same integral as a slice + if _special_setting(hist_setup): + pytest.skip("Slicing cannot be tested here") + + if hist_setup.GetDimension() != 1: + pytest.skip("Only 1D histograms are integrated") + + full_integral = hist_setup.Integral(_underflow(hist_setup, 0), _overflow(hist_setup, 0)) + for action in (sum, builtins.sum): + assert hist_setup[action] == full_integral + assert hist_setup[(slice(None, None, action),)] == full_integral + assert hist_setup[{0: slice(None, None, action)}] == full_integral + assert hist_setup[{0: slice(0, len, action)}] == hist_setup.Integral() + assert hist_setup[(slice(5, 9, action),)] == hist_setup.Integral(6, 9) + def test_slicing_with_dict_syntax(self, hist_setup): if _special_setting(hist_setup): pytest.skip("Setting cannot be tested here") From 732fd96e77df52f622638d7da940969287dab2b1 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 11:56:57 +0100 Subject: [PATCH 03/17] [Python][UHI] Include the flow bins in the sum over all axes of 2D and 3D histograms The sum over all axes of 2D and 3D histograms called `Integral()` without bounds, which excludes the flow bins. h = TH2D with 3x3 bins, filled at (1.5, 1.5), (-1, 1.5) in the x underflow and (1.5, 5) in the y overflow Before After h[::sum, ::sum] 1 3 h[0:len:sum, 0:len:sum] 1 1 h[1, ::sum] 1 2 Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/indexing.py | 10 ++++++---- .../python/ROOT/_pythonization/_uhi/tags.py | 4 ++-- .../pyroot/pythonizations/test/uhi_indexing.py | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py index f4cd923fc4fe2..1785f51ce9469 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py @@ -126,12 +126,14 @@ def _apply_actions(hist, actions, index, original_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 - args = [start, stop - 1] + 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) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py index 3eb242239a111..1877390985302 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py @@ -103,7 +103,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: @@ -117,6 +117,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") diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index a23a36f2db64c..20af7c25b958c 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -245,6 +245,20 @@ def test_slicing_over_everything_with_action_sum(self, hist_setup): sum_xy = hist_setup[::sum, ::sum, :] assert isinstance(sum_xy, ROOT.TH1) + def test_action_sum_over_all_axes_includes_flow(self, hist_setup): + if _special_setting(hist_setup): + pytest.skip("Slicing cannot be tested here") + + dim = hist_setup.GetDimension() + with_flow = [b for axis in range(dim) for b in (_underflow(hist_setup, axis), _overflow(hist_setup, axis))] + without_flow = [b for axis in range(dim) for b in (1, _overflow(hist_setup, axis) - 1)] + assert hist_setup[(slice(None, None, sum),) * dim] == hist_setup.Integral(*with_flow) + assert hist_setup[(slice(0, len, sum),) * dim] == hist_setup.Integral(*without_flow) + + if dim > 1: + # A single bin on the first axis and the full range with flow on the others + assert hist_setup[(1,) + (slice(None, None, sum),) * (dim - 1)] == hist_setup.Integral(2, 2, *with_flow[2:]) + def test_slicing_with_action_rebin_and_sum(self, hist_setup): if _special_setting(hist_setup): pytest.skip("Slicing cannot be tested here") From 6155e82aa71a82751156bac71ade3effce444e35 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 11:57:21 +0100 Subject: [PATCH 04/17] [Python][UHI] Do not reuse the histogram of a previous projection for the sum action `ProjectionX` reuses and refills the histogram with the same name (`h_px`) found in `gDirectory`, so each projection overwrote the previous one. h = TH2D with 4x2 bins [[1, 2], [3, 4], [5, 6], [7, 8]], px = h[:, ::sum], then px_part = h[1:3, ::sum] Before After px [3 7 11 15] [3 7 11 15] px_part [7 11] [7 11] px after px_part [7 11] [3 7 11 15] Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/indexing.py | 8 +++++++- bindings/pyroot/pythonizations/test/uhi_indexing.py | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py index 1785f51ce9469..a04d9af40694e 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py @@ -136,7 +136,13 @@ def _apply_actions(hist, actions, index, original_hist): 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 and a is not sum] diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index 20af7c25b958c..a1e623fd964c6 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -259,6 +259,19 @@ def test_action_sum_over_all_axes_includes_flow(self, hist_setup): # A single bin on the first axis and the full range with flow on the others assert hist_setup[(1,) + (slice(None, None, sum),) * (dim - 1)] == hist_setup.Integral(2, 2, *with_flow[2:]) + def test_action_sum_projections_are_independent(self, hist_setup): + if _special_setting(hist_setup) or hist_setup.GetDimension() == 1: + pytest.skip("Projections cannot be tested here") + + dim = hist_setup.GetDimension() + full_projection = hist_setup[(slice(None),) * (dim - 1) + (slice(None, None, sum),)] + full_values = full_projection.values(flow=True).copy() + + ranged_projection = hist_setup[(slice(2, 5),) * (dim - 1) + (slice(None, None, sum),)] + + assert ranged_projection.GetNbinsX() == 3 + assert np.array_equal(full_projection.values(flow=True), full_values) + def test_slicing_with_action_rebin_and_sum(self, hist_setup): if _special_setting(hist_setup): pytest.skip("Slicing cannot be tested here") From accfcae4d57460d4260c8a09c1c5fefac5300f32 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:49:54 +0100 Subject: [PATCH 05/17] [Python][UHI] Do not slice profiles with the instantiation of Slice for their base class Once `Slice` exists, cppyy uses it for a `TProfile` too, and copying a `TProfile` into a `TH1D` is fatal. h = TH1D, p = TProfile Before After p[1:3] TProfile TProfile h[1:3], then p[1:3] Fatal in : Cannot copy a TProfile in a TH1D TProfile Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/indexing.py | 4 +++- bindings/pyroot/pythonizations/test/uhi_indexing.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py index a04d9af40694e..26671e3141ccc 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py @@ -187,7 +187,9 @@ def _slice_get(self, 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 for a TProfile), which cannot copy it + target_hist = ROOT.Internal.Slice[self.ClassName()](self, args_vec) return _apply_actions(target_hist, actions, index, self) diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index a1e623fd964c6..edfc2300989fc 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -437,5 +437,15 @@ def test_profile_histogram_kind(self): assert h_profile.kind == "MEAN" +class TestSlicingDerivedClasses: + def test_slicing_profile_after_its_base_class(self): + # TProfile derives from TH1D, slicing it must not use the instantiation of Slice for TH1D + h = ROOT.TH1D("h_base_slicing", "", 4, 0, 4) + profile = ROOT.TProfile("p_derived_slicing", "", 4, 0, 4) + + assert h[1:3].GetNbinsX() == 2 + assert isinstance(profile[1:3], ROOT.TProfile) + + if __name__ == "__main__": raise SystemExit(pytest.main(args=[__file__])) From aab977f66e53fad90453c855ca644d3f279e8914 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:00:20 +0100 Subject: [PATCH 06/17] [Python][UHI] Keep the flow bins of each axis when slicing 2D and 3D histograms Each cut bin should land in the flow bin of the axis it was cut from. Before, they all piled into two corner cells. Below the result of h[1:2, :] original slicing BEFORE slicing AFTER of | 1 | 1 1 1 | 1 of | 0 | 0 | 11 of | 2 | 1 | 2 3 | 1 | 1 1 1 | 1 3 | 0 | 1 | 0 3 | 2 | 1 | 2 2 | 1 | 1 1 1 | 1 2 | 0 | 1 | 0 2 | 2 | 1 | 2 1 | 1 | 1 1 1 | 1 1 | 0 | 1 | 0 1 | 2 | 1 | 2 uf | 1 | 1 1 1 | 1 uf | 11 | 0 | 0 uf | 2 | 1 | 2 uf | 1 2 3 | of uf | 1 | of uf | 1 | of Assisted-by: Claude Opus 5.5 --- .../ROOT/_pythonization/_uhi/indexing.py | 10 ++- .../pythonizations/test/uhi_indexing.py | 36 +++++++++ hist/hist/inc/TH1.h | 78 ++++++++++--------- 3 files changed, 87 insertions(+), 37 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py index 26671e3141ccc..aea3eb1c615cf 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py @@ -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 @@ -191,6 +191,14 @@ def _slice_get(self, index): # instantiation for its base class (e.g. Slice 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, self) diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index edfc2300989fc..81289916d2dce 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -25,6 +25,16 @@ def _get_index_for_dimension(hist, index): return (index,) * hist.GetDimension() +def _expected_slice(values, slices): + # Bins outside the slice of an axis fall into the underflow or overflow bin of that axis, + # `values` includes the flow bins and `slices` are (start, stop) ROOT bin numbers + for axis, (start, stop) in enumerate(slices): + underflow = values.take(range(start), axis).sum(axis, keepdims=True) + overflow = values.take(range(stop, values.shape[axis]), axis).sum(axis, keepdims=True) + values = np.concatenate([underflow, values.take(range(start, stop), axis), overflow], axis=axis) + return values + + def _get_slice_indices(slices): import numpy as np @@ -180,6 +190,17 @@ def test_slicing_without_endpoints(self, hist_setup): } self._test_slices_match(hist_setup, slice_ranges, processed_slices) + def test_slicing_keeps_flow_bins_of_each_axis(self, hist_setup): + # TH*C bin contents saturate, so their flow bins cannot hold the sum of the cut bins + if _special_setting(hist_setup) or isinstance(hist_setup, (ROOT.TH1C, ROOT.TH2C, ROOT.TH3C)): + pytest.skip("Slicing cannot be tested here") + + dim = hist_setup.GetDimension() + expected = _expected_slice(hist_setup.values(flow=True), [(3, 6)] * dim) + sliced_hist = hist_setup[(slice(2, 5),) * dim] + + assert np.array_equal(sliced_hist.values(flow=True), expected) + def test_slicing_with_data_coordinates(self, hist_setup): if _special_setting(hist_setup): pytest.skip("Setting cannot be tested here") @@ -259,6 +280,21 @@ def test_action_sum_over_all_axes_includes_flow(self, hist_setup): # A single bin on the first axis and the full range with flow on the others assert hist_setup[(1,) + (slice(None, None, sum),) * (dim - 1)] == hist_setup.Integral(2, 2, *with_flow[2:]) + def test_action_sum_projection_includes_selected_bins(self, hist_setup): + if _special_setting(hist_setup) or hist_setup.GetDimension() != 2: + pytest.skip("Projections are tested here for 2D histograms") + + # Open slices include the flow bins of the summed axis, closed slices do not + assert np.array_equal( + hist_setup[::sum, :].values(flow=True), hist_setup.ProjectionY("_py_full").values(flow=True) + ) + assert np.array_equal( + hist_setup[2:5:sum, :].values(flow=True), hist_setup.ProjectionY("_py_ranged", 3, 5).values(flow=True) + ) + assert np.array_equal( + hist_setup[:, 2::sum].values(flow=True), hist_setup.ProjectionX("_px_open", 3, 11).values(flow=True) + ) + def test_action_sum_projections_are_independent(self, hist_setup): if _special_setting(hist_setup) or hist_setup.GetDimension() == 1: pytest.skip("Projections cannot be tested here") diff --git a/hist/hist/inc/TH1.h b/hist/hist/inc/TH1.h index cbd931b53fc3b..d89a7707b3d67 100644 --- a/hist/hist/inc/TH1.h +++ b/hist/hist/inc/TH1.h @@ -81,7 +81,7 @@ T Slice(const T &histo, std::vector &args) T slicedHisto = histo; using ValueType = std::remove_pointer_t; - slicedHisto.template SliceHistoInPlace(args, slicedHisto.fArray, slicedHisto.fN); + slicedHisto.template SliceHistoInPlace(args, slicedHisto.fArray); return slicedHisto; } @@ -187,12 +187,48 @@ class TH1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker { TH1(const TH1&) = delete; TH1& operator=(const TH1&) = delete; + /** + * \brief Adds the content of each bin of `src` to the corresponding bin of the slice in `dst`. + * + * Bins outside the slice on an axis fall into the underflow or overflow bin of that axis. + * + * \tparam ValueType The type of the arrays. + * \param args The low and upper edges of the slice for each dimension, within the bins of each axis. + * \param src An array with the bin layout of this histogram. + * \param dst A zero-initialized array with the bin layout of the slice. + */ + template + void SliceArray(const std::vector &args, const ValueType *src, ValueType *dst) const + { + const auto ndim = args.size() / 2; + // Bin of the slice corresponding to the given bin of axis d + auto sliceBin = [&args](size_t d, Int_t bin) { + const auto start = args[2 * d], end = args[2 * d + 1]; + return bin < start ? 0 : bin >= end ? end - start + 1 : bin - start + 1; + }; + const Int_t nx = fXaxis.GetNbins() + 2; + const Int_t ny = ndim > 1 ? fYaxis.GetNbins() + 2 : 1; + const Int_t nz = ndim > 2 ? fZaxis.GetNbins() + 2 : 1; + const Int_t sliceNx = args[1] - args[0] + 2; + const Int_t sliceNy = ndim > 1 ? args[3] - args[2] + 2 : 1; + for (Int_t z = 0; z < nz; ++z) { + for (Int_t y = 0; y < ny; ++y) { + for (Int_t x = 0; x < nx; ++x) { + const Int_t sliceX = sliceBin(0, x); + const Int_t sliceY = ndim > 1 ? sliceBin(1, y) : 0; + const Int_t sliceZ = ndim > 2 ? sliceBin(2, z) : 0; + dst[sliceX + sliceNx * (sliceY + sliceNy * sliceZ)] += src[x + nx * (y + ny * z)]; + } + } + } + } + /** * \brief Slices a histogram in place based on the specified bin ranges for each dimension. * * This function modifies the histogram by extracting a sub-region defined by the provided * bin ranges for each dimension. The resulting histogram will have updated bin counts, - * edges, and contents. Bin contents outside the range fall into the flow bins. + * edges, and contents. Bin contents outside the range of an axis fall into the flow bins of that axis. * The histogram's internal data array is freed and reallocated by this function. * This function is used by the python implementation of the Unified Histogram Interface (UHI) * for slicing. @@ -200,10 +236,9 @@ class TH1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker { * \tparam ValueType The type of the histogram's data. * \param args A vector of integers specifying the low and upper edges of the slice for each dimension. * \param dataArray A pointer to the histogram's data array. - * \param fN The size of dataArray. */ template - void SliceHistoInPlace(std::vector &args, ValueType *&dataArray, Int_t &fN) + void SliceHistoInPlace(std::vector &args, ValueType *&dataArray) { constexpr Int_t kMaxDim = 3; Int_t ndim = args.size() / 2; @@ -213,14 +248,13 @@ class TH1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker { } // Compute new bin counts and edges - std::array nBins{}, totalBins{}; + std::array nBins{}; std::array, kMaxDim> edges; for (decltype(ndim) d = 0; d < ndim; ++d) { const auto &axis = (d == 0 ? fXaxis : d == 1 ? fYaxis : fZaxis); auto start = std::max(1, args[d * 2]); auto end = std::min(axis.GetNbins() + 1, args[d * 2 + 1]); nBins[d] = end - start; - totalBins[d] = axis.GetNbins() + 2; args[2 * d] = start; args[2 * d + 1] = end; // Compute new edges @@ -237,36 +271,8 @@ class TH1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker { // Allocate the new array auto *newArr = new ValueType[newSize](); - auto rowIncr = 1 + (ndim > 1 ? (rowSz - 1) : 0); - ValueType under = 0, over = 0; - Bool_t firstUnder = false; - Int_t lastOverIdx = 0; - - // Copy the valid slice bins - size_t dstIdx = 1 + (ndim > 1 ? rowSz : 0) + (ndim > 2 ? planeSz : 0); - for (auto z = (ndim > 2 ? args[4] : 0); z < (ndim > 2 ? args[5] : 1); ++z) { - for (auto y = (ndim > 1 ? args[2] : 0); y < (ndim > 1 ? args[3] : 1); ++y) { - size_t rowStart = z * totalBins[1] * totalBins[0] + y * totalBins[0] + args[0]; - std::copy_n(dataArray + rowStart, nBins[0], newArr + dstIdx); - if (!firstUnder) { - under = std::accumulate(dataArray, dataArray + rowStart, ValueType{}); - firstUnder = true; - } else { - under += std::accumulate(dataArray + rowStart - args[0], dataArray + rowStart, ValueType{}); - } - lastOverIdx = rowStart - args[0] + totalBins[0]; - over += std::accumulate(dataArray + rowStart + nBins[0], dataArray + lastOverIdx, ValueType{}); - dstIdx += rowIncr; - } - if (ndim > 2) { - dstIdx += 2 * rowIncr; - } - } - - // Copy the flow bins - over += std::accumulate(dataArray + lastOverIdx, dataArray + fN, ValueType{}); - newArr[0] = under; - newArr[newSize - 1] = over; + // Move the bin contents to the slice + SliceArray(args, dataArray, newArr); // Assign the new array delete[] dataArray; From 9dde00ce9b6afc3636287dd529d257f62297c327 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:00:48 +0100 Subject: [PATCH 07/17] [UHI] Slice the sum of squares of weights together with the bin contents The Sumw2 array was not sliced, `SetBins` only truncated it to the size of the slice, which keeps the first bins. h = TH1D with 4 bins and Sumw2, one entry of weight 1, 2, 3, 4 in each bin Before After h.variances() [1 4 9 16] [1 4 9 16] h[1:3].variances() [1 4] [4 9] Assisted-by: Claude Opus 5.5 --- bindings/pyroot/pythonizations/test/uhi_indexing.py | 11 +++++++++++ hist/hist/inc/TH1.h | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index 81289916d2dce..37094bb907ac1 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -201,6 +201,17 @@ def test_slicing_keeps_flow_bins_of_each_axis(self, hist_setup): assert np.array_equal(sliced_hist.values(flow=True), expected) + def test_slicing_keeps_sum_of_weights_squared(self, hist_setup): + if _special_setting(hist_setup) or isinstance(hist_setup, (ROOT.TH1C, ROOT.TH2C, ROOT.TH3C)): + pytest.skip("Slicing cannot be tested here") + + hist_setup.Sumw2() + dim = hist_setup.GetDimension() + expected = _expected_slice(hist_setup.variances(flow=True), [(3, 6)] * dim) + sliced_hist = hist_setup[(slice(2, 5),) * dim] + + assert np.array_equal(sliced_hist.variances(flow=True), expected) + def test_slicing_with_data_coordinates(self, hist_setup): if _special_setting(hist_setup): pytest.skip("Setting cannot be tested here") diff --git a/hist/hist/inc/TH1.h b/hist/hist/inc/TH1.h index d89a7707b3d67..95eecda65ee53 100644 --- a/hist/hist/inc/TH1.h +++ b/hist/hist/inc/TH1.h @@ -274,6 +274,13 @@ class TH1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker { // Move the bin contents to the slice SliceArray(args, dataArray, newArr); + // The sum of squares of weights has the same layout as the bin contents + if (fSumw2.fN) { + std::vector newSumw2(newSize); + SliceArray(args, fSumw2.fArray, newSumw2.data()); + fSumw2.Set(newSize, newSumw2.data()); + } + // Assign the new array delete[] dataArray; dataArray = newArr; From 7512e03b98900804354b7308a4c9ec83733541ab Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 11:55:30 +0100 Subject: [PATCH 08/17] [Python][UHI] Support negative bin indices other than -1 Only -1 was mapped to the last bin, other negative indices were passed to ROOT as negative bin numbers. h = TH1D with 5 bins [10, 11, 12, 13, 14] Before After h[-1] 14 14 h[-2] 0 13 h[-5] 0 10 h[-6] 0 IndexError Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/indexing.py | 8 ++++---- bindings/pyroot/pythonizations/test/uhi_indexing.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py index aea3eb1c615cf..b4d860ff0eca0 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py @@ -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 diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index 37094bb907ac1..09fcbc15e30d1 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -50,6 +50,18 @@ def test_access_with_bin_number(self, hist_setup): *_get_index_for_dimension(hist_setup, index + 1) ) + def test_access_with_negative_bin_number(self, hist_setup): + for index in [-1, -2, -10]: + assert hist_setup[_get_index_for_dimension(hist_setup, index)] == hist_setup.GetBinContent( + *_get_index_for_dimension(hist_setup, index + 11) + ) + + with pytest.raises(IndexError): + hist_setup[_get_index_for_dimension(hist_setup, -11)] + + sliced_hist = hist_setup[_get_index_for_dimension(hist_setup, slice(2, -2))] + assert sliced_hist.GetNbinsX() == 6 + def test_access_with_data_coordinates(self, hist_setup): for value in [1, 4]: value_indices = _get_index_for_dimension(hist_setup, value) From 508a809dd9753cd04569d9e5d8eb2c835d91fc21 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 11:56:01 +0100 Subject: [PATCH 09/17] [Python][UHI] Do not modify loc when adding or subtracting an offset `loc` changed `self` and returned it, so every `loc` call registers offsets: Expression Before After h[x] 2 2 h[x + 1] 3 3 h[x] again 3, and x.offset is now 1 2 Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/tags.py | 10 ++++++---- bindings/pyroot/pythonizations/test/uhi_indexing.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py index 1877390985302..895dc08740984 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py @@ -44,14 +44,16 @@ 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: diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index 09fcbc15e30d1..fbd8950d6cf97 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -68,6 +68,16 @@ def test_access_with_data_coordinates(self, hist_setup): loc_indices = tuple(loc(idx) for idx in value_indices) assert hist_setup[loc_indices] == hist_setup.GetBinContent(hist_setup.FindBin(*value_indices)) + def test_access_with_shifted_data_coordinates(self): + h = ROOT.TH1D("h_shifted_loc", "", 10, 0, 10) + h[...] = np.arange(10) + + location = loc(2.5) + assert h[location + 1] == 3 + assert h[location] == 2 + assert h[location - 1] == 1 + assert h[location] == 2 + def test_access_flow_bins(self, hist_setup): for flow_type, index in [(underflow, 0), (overflow, 11)]: flow_indices = (flow_type,) * hist_setup.GetDimension() From b6927545c69937ac76881385e37fe667551e4e68 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 11:56:15 +0100 Subject: [PATCH 10/17] [Python][UHI] Do not extend the axis when locating a bin with loc `FindBin` grows the axis if the histogram is extendable, `FindFixBin` is a simple lookup. Before After Histogram before reading [0, 10], [0 1 2 3 4 5 6 7 8 9] same h[loc(100)] 0.0 0.0 (the overflow bin) Histogram after reading [0, 160], [45 0 0 0 0 0 0 0 0 0] [0, 10], [0 1 2 3 4 5 6 7 8 9] Assisted-by: Claude Opus 5.5 --- .../pythonizations/python/ROOT/_pythonization/_uhi/tags.py | 2 +- bindings/pyroot/pythonizations/test/uhi_indexing.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py index 895dc08740984..a96dd9a5aed0d 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py @@ -57,7 +57,7 @@ def __sub__(self, other: int) -> _loc: 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: diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index fbd8950d6cf97..39bacfa658840 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -78,6 +78,13 @@ def test_access_with_shifted_data_coordinates(self): assert h[location - 1] == 1 assert h[location] == 2 + def test_access_with_data_coordinates_does_not_extend_axis(self): + h = ROOT.TH1D("h_extendable", "", 10, 0, 10) + h.SetCanExtend(ROOT.TH1.kAllAxes) + + assert h[loc(100)] == h[overflow] + assert h.GetXaxis().GetXmax() == 10 + def test_access_flow_bins(self, hist_setup): for flow_type, index in [(underflow, 0), (overflow, 11)]: flow_indices = (flow_type,) * hist_setup.GetDimension() From fbf99fffedd24e19cf89f95444fe6cbd9bd3f69f Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:01:13 +0100 Subject: [PATCH 11/17] [Python][UHI] Return the number of entries as the counts of profiles The counts were computed as values^2 / Sumw2, but the Sumw2 array of a profile holds the sum of w*y^2 of each bin. p = TProfile with 2 bins, filled with y = (1, 3) in bin 0 and y = (2, 2, 5) in bin 1 Before After p.values() [2 3] [2 3] p.counts() [0.4 0.27] [2 3] Assisted-by: Claude Opus 5.5 --- .../ROOT/_pythonization/_uhi/plotting.py | 17 +++++++++++++++++ .../pyroot/pythonizations/test/uhi_plotting.py | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py index 58e300e71df89..e4ae6962b9813 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py @@ -199,9 +199,26 @@ def _variances(self, flow=False) -> np.typing.NDArray[Any]: # noqa: F821 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): diff --git a/bindings/pyroot/pythonizations/test/uhi_plotting.py b/bindings/pyroot/pythonizations/test/uhi_plotting.py index 0842838793ec9..8e4e30c04ab33 100644 --- a/bindings/pyroot/pythonizations/test/uhi_plotting.py +++ b/bindings/pyroot/pythonizations/test/uhi_plotting.py @@ -48,5 +48,23 @@ def test_histogram_values_writable(self, hist_setup): assert values_w.flags.writeable is True +class TestProfilePlotting: + @staticmethod + def _make_profile(name, weighted=False): + profile = ROOT.TProfile(name, "", 4, 0, 4) + if weighted: + profile.Sumw2() + for x, y, w in [(0.5, 1, 2), (0.5, 3, 1), (1.5, 2, 1), (1.5, 2, 1), (1.5, 5, 1)]: + profile.Fill(x, y, w if weighted else 1) + return profile + + def test_counts(self): + assert np.array_equal(self._make_profile("p_counts").counts(), [2, 3, 0, 0]) + + # Effective number of entries (sum of weights)^2 / (sum of weights^2) + weighted_counts = self._make_profile("p_counts_weighted", weighted=True).counts() + assert weighted_counts == pytest.approx([9 / 5, 3, 0, 0]) + + if __name__ == "__main__": raise SystemExit(pytest.main(args=[__file__])) From f59becd7ed323676b21db81e8a3fafd7110294b7 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:02:41 +0100 Subject: [PATCH 12/17] [Python][UHI] Return the variance of the mean as the variances of profiles The variances were the Sumw2 array, which holds the sum of w*y^2 of each bin for a profile. p = TProfile with 2 bins, filled with y = (1, 3) in bin 0 and y = (2, 2, 5) in bin 1 Before After p.values() [2 3] [2 3] p.variances() [10 33] [1 1] Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/plotting.py | 6 +++++- bindings/pyroot/pythonizations/test/uhi_plotting.py | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py index e4ae6962b9813..94fb85c1c46f5 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py @@ -191,8 +191,12 @@ 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 diff --git a/bindings/pyroot/pythonizations/test/uhi_plotting.py b/bindings/pyroot/pythonizations/test/uhi_plotting.py index 8e4e30c04ab33..fa8cb997f5f50 100644 --- a/bindings/pyroot/pythonizations/test/uhi_plotting.py +++ b/bindings/pyroot/pythonizations/test/uhi_plotting.py @@ -65,6 +65,14 @@ def test_counts(self): weighted_counts = self._make_profile("p_counts_weighted", weighted=True).counts() assert weighted_counts == pytest.approx([9 / 5, 3, 0, 0]) + def test_variances(self): + # Variance of the mean of y in each bin, NaN for the bins with less than two entries + variances = self._make_profile("p_variances").variances() + assert np.allclose(variances, [1, 1, np.nan, np.nan], equal_nan=True) + + weighted_variances = self._make_profile("p_variances_weighted", weighted=True).variances() + assert np.allclose(weighted_variances, [2 / 3, 1, np.nan, np.nan], equal_nan=True) + if __name__ == "__main__": raise SystemExit(pytest.main(args=[__file__])) From 0291cf1836d71710c016452d522a100f980de2c3 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:03:51 +0100 Subject: [PATCH 13/17] [hist][UHI] Slice the bin entries of profiles together with the bin contents The bin entries of profiles were not sliced, so the sliced bin contents were divided by the entries of the first bins. p = TProfile with 4 bins, filled with y = 1 in bin 0, y = (2, 4) in bin 1, y = 6 in bin 2 and y = 8 in bin 3 Before After p.values() [1 3 6 8] [1 3 6 8] p[1:3].values() [6 3] [3 6] p[1:3].counts() [1 2] [2 1] Assisted-by: Claude Opus 5.5 --- .../pyroot/pythonizations/test/uhi_indexing.py | 16 ++++++++++++++++ hist/hist/inc/TH1.h | 16 ++++++++++++++++ hist/hist/inc/TProfile.h | 2 ++ hist/hist/inc/TProfile2D.h | 2 ++ hist/hist/inc/TProfile3D.h | 2 ++ 5 files changed, 38 insertions(+) diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index 39bacfa658840..4fa62b8a3f694 100644 --- a/bindings/pyroot/pythonizations/test/uhi_indexing.py +++ b/bindings/pyroot/pythonizations/test/uhi_indexing.py @@ -496,6 +496,22 @@ def test_uhi_projection_preserves_content(self): assert np.allclose(uhi_values, ref_values) +class TestProfileSlicing: + def test_slicing_keeps_means_and_entries(self): + profile = ROOT.TProfile("p_slicing", "", 4, 0, 4) + for x, y in [(-1, 4), (0.5, 1), (0.5, 3), (1.5, 2), (2.5, 6), (3.5, 8)]: + profile.Fill(x, y) + + sliced_profile = profile[1:3] + + assert np.array_equal(sliced_profile.values(), [2, 6]) + assert np.array_equal(sliced_profile.counts(), [1, 1]) + + # The cut bins fall into the flow bins + assert np.array_equal(sliced_profile.values(flow=True), [(4 + 1 + 3) / 3, 2, 6, 8]) + assert np.array_equal(sliced_profile.counts(flow=True), [3, 1, 1, 1]) + + class TestHistogramKind: def test_histogram_kind_no_weights(self): h = ROOT.TH1D("h", "h", 100, -10, 10) diff --git a/hist/hist/inc/TH1.h b/hist/hist/inc/TH1.h index 95eecda65ee53..509ad0a10b8ca 100644 --- a/hist/hist/inc/TH1.h +++ b/hist/hist/inc/TH1.h @@ -60,6 +60,9 @@ class TCollection; class TVirtualFFT; class TVirtualHistPainter; class TRandom; +class TProfile; +class TProfile2D; +class TProfile3D; namespace ROOT::Internal { /** @@ -82,6 +85,19 @@ T Slice(const T &histo, std::vector &args) using ValueType = std::remove_pointer_t; slicedHisto.template SliceHistoInPlace(args, slicedHisto.fArray); + + // Profiles also hold the sum of weights and the sum of squares of weights of each bin + if constexpr (std::is_same_v || std::is_same_v || std::is_same_v) { + auto sliceProfileArray = [&](const TArrayD &original, TArrayD &sliced) { + std::vector values(slicedHisto.fNcells); + histo.SliceArray(args, original.fArray, values.data()); + sliced.Set(slicedHisto.fNcells, values.data()); + }; + sliceProfileArray(histo.fBinEntries, slicedHisto.fBinEntries); + if (histo.fBinSumw2.fN) + sliceProfileArray(histo.fBinSumw2, slicedHisto.fBinSumw2); + slicedHisto.ResetStats(); + } return slicedHisto; } diff --git a/hist/hist/inc/TProfile.h b/hist/hist/inc/TProfile.h index e1b00a80bd4b2..8adb12cb22147 100644 --- a/hist/hist/inc/TProfile.h +++ b/hist/hist/inc/TProfile.h @@ -34,6 +34,8 @@ class TProfile : public TH1D { public: friend class TProfileHelper; friend class TH1Merger; + template + friend T ROOT::Internal::Slice(const T &histo, std::vector &args); protected: TArrayD fBinEntries; ///< number of entries per bin diff --git a/hist/hist/inc/TProfile2D.h b/hist/hist/inc/TProfile2D.h index c7c6e8778e2b6..2321c6e375ccf 100644 --- a/hist/hist/inc/TProfile2D.h +++ b/hist/hist/inc/TProfile2D.h @@ -29,6 +29,8 @@ class TProfile2D : public TH2D { public: friend class TProfileHelper; friend class TH1Merger; + template + friend T ROOT::Internal::Slice(const T &histo, std::vector &args); protected: TArrayD fBinEntries; ///< Number of entries per bin diff --git a/hist/hist/inc/TProfile3D.h b/hist/hist/inc/TProfile3D.h index 1b82c6c5d2d89..bfb3512aff782 100644 --- a/hist/hist/inc/TProfile3D.h +++ b/hist/hist/inc/TProfile3D.h @@ -29,6 +29,8 @@ class TProfile3D : public TH3D { public: friend class TProfileHelper; friend class TH1Merger; + template + friend T ROOT::Internal::Slice(const T &histo, std::vector &args); protected: TArrayD fBinEntries; ///< Number of entries per bin From 003a14d1fd4452bb9da99874ca52b9929a7eee6f Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:04:02 +0100 Subject: [PATCH 14/17] [Python][UHI] Do not serialize the axis range as the axis edges `lower` and `upper` were the edges of the range selected with `SetRange`, while `bins` was the number of bins of the whole axis. h = TH1D with 10 bins in [0, 10], h.GetXaxis().SetRange(3, 5) Before After axes[0] lower 2, upper 5, bins 10 lower 0, upper 10, bins 10 Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/serialization.py | 4 ++-- .../pyroot/pythonizations/test/uhi_serialization_json.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py index 9fc2111c6884f..8e9d6c54b3dc4 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py @@ -26,8 +26,8 @@ def _axis_to_dict(root_axis: ROOT.TAxis, uhi_axis: PlottableAxisBase) -> dict[st """ 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, diff --git a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py index f2668ea726a61..01103f932d279 100644 --- a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py +++ b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py @@ -79,6 +79,14 @@ def test_roundtrip_1d_flow(self): # test that name is preserved assert h.GetName() == h_loaded.GetName() == h_loaded_cls.GetName() + def test_axis_does_not_depend_on_range(self): + h = ROOT.TH1D("h1_range", "h1_range", 10, 0, 10) + h.GetXaxis().SetRange(3, 5) + + axis = h._to_uhi_()["axes"][0] + + assert (axis["lower"], axis["upper"], axis["bins"]) == (0, 10, 10) + def test_uhi_protocol_object(self): values = np.arange(12.0) From 956d8ac0d927b18585ab83bdd01f7a01c4401fa8 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:04:23 +0100 Subject: [PATCH 15/17] [Python][UHI] Serialize axes with variable bin sizes as variable axes Axes with variable bin sizes were serialized as regular axes between their first and last edges. h = TH1D with 3 bins of edges [0, 1, 5, 10] Before After axes[0] regular, lower 0, upper 10, bins 3 variable, edges [0, 1, 5, 10] Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/serialization.py | 9 +++++++++ .../pyroot/pythonizations/test/uhi_serialization_json.py | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py index 8e9d6c54b3dc4..0e36a4a8c9a11 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py @@ -24,6 +24,15 @@ 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.GetXmin(), diff --git a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py index 01103f932d279..dd51c2519886d 100644 --- a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py +++ b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py @@ -87,6 +87,15 @@ def test_axis_does_not_depend_on_range(self): assert (axis["lower"], axis["upper"], axis["bins"]) == (0, 10, 10) + def test_variable_axis(self): + edges = [0.0, 1.0, 5.0, 10.0] + h = ROOT.TH1D("h1_variable", "h1_variable", 3, np.array(edges)) + + axis = h._to_uhi_()["axes"][0] + + assert axis["type"] == "variable" + assert axis["edges"] == edges + def test_uhi_protocol_object(self): values = np.arange(12.0) From 08126815fd1b2ea3e9509aed7304c2fe6d6c3e97 Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:04:48 +0100 Subject: [PATCH 16/17] [Python][UHI] Serialize all storage arrays with the flow bins The values were serialized with the flow bins, but the variances without. h = TH1D with 2 bins and Sumw2, filled with weight 2 in the underflow and weight 3 in bin 0 Before After storage["values"] [2 3 0 0] [2 3 0 0] storage["variances"] [9 0] [4 9 0 0] Assisted-by: Claude Opus 5.5 --- .../python/ROOT/_pythonization/_uhi/serialization.py | 8 ++++---- .../pythonizations/test/uhi_serialization_json.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py index 0e36a4a8c9a11..a9b3bb31da735 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py @@ -79,20 +79,20 @@ def _storage_to_dict(hist: Any) -> dict[str, Any]: } if hist.kind == "MEAN": - storage_dict["variances"] = hist.variances() + storage_dict["variances"] = hist.variances(flow=True) if _hasWeights(hist): 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_squared"] = _get_sum_of_weights_squared(hist, 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" diff --git a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py index dd51c2519886d..1f8e9a8ab1835 100644 --- a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py +++ b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py @@ -168,6 +168,18 @@ def test_roundtrip_2d_flow(self): assert h.GetName() == h_loaded.GetName() + def test_weighted_storage_includes_flow(self): + h = ROOT.TH2D("h2_weighted", "h2_weighted", 3, 0, 3, 2, 0, 2) + h.Sumw2() + h.Fill(-1, 1, 2.0) + h.Fill(1, 1, 3.0) + + storage = h._to_uhi_()["storage"] + + assert storage["type"] == "weighted" + assert np.array_equal(storage["values"], h.values(flow=True)) + assert np.array_equal(storage["variances"], h.variances(flow=True)) + class TestTH3Serialization: def test_roundtrip_3d(self): From 6f4187691600b3672972f6f5d97bd01acaf87f5d Mon Sep 17 00:00:00 2001 From: Silia Taider Date: Tue, 10 Nov 2026 12:05:40 +0100 Subject: [PATCH 17/17] [Python][UHI] Serialize unweighted profiles with the mean storage A profile always has a Sumw2 array (the sum of w*y^2), so every profile was serialized as a `weighted_mean` storage. p = TProfile with 2 bins, filled with y = (1, 3) in bin 0 and y = (2, 2, 5) in bin 1 Before After type weighted_mean mean variances [nan 1 1 nan] [0 2 3 0] counts - [0 2 3 0] sum_of_weights [2 3] (the means) - sum_of_weights_squared [0 10 33 0] (sum of y^2) - Assisted-by: Claude Opus 5.5 --- .../ROOT/_pythonization/_uhi/plotting.py | 4 --- .../ROOT/_pythonization/_uhi/serialization.py | 16 +++++++----- .../test/uhi_serialization_json.py | 25 +++++++++++++++++++ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py index 94fb85c1c46f5..adb11c3d7dd6b 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py @@ -238,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 diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py index a9b3bb31da735..bc820c5f64f91 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py @@ -62,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 @@ -79,12 +81,14 @@ def _storage_to_dict(hist: Any) -> dict[str, Any]: } if hist.kind == "MEAN": - storage_dict["variances"] = hist.variances(flow=True) + # 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, flow=True) + 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(flow=True) diff --git a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py index 1f8e9a8ab1835..17bdf765a7854 100644 --- a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py +++ b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py @@ -96,6 +96,31 @@ def test_variable_axis(self): assert axis["type"] == "variable" assert axis["edges"] == edges + def test_profile_storage(self): + profile = ROOT.TProfile("p_storage", "p_storage", 4, 0, 4) + for x, y in [(0.5, 1), (0.5, 3), (1.5, 2), (1.5, 2), (1.5, 5)]: + profile.Fill(x, y) + + storage = profile._to_uhi_()["storage"] + + # The variances of the storage are the unbiased variances of y + assert storage["type"] == "mean" + assert np.array_equal(storage["counts"], [0, 2, 3, 0, 0, 0]) + assert np.allclose(storage["variances"], [0, 2, 3, 0, 0, 0]) + + def test_weighted_profile_storage(self): + profile = ROOT.TProfile("p_storage_weighted", "p_storage_weighted", 4, 0, 4) + profile.Sumw2() + for x, y, w in [(0.5, 1, 2), (0.5, 3, 1)]: + profile.Fill(x, y, w) + + storage = profile._to_uhi_()["storage"] + + assert storage["type"] == "weighted_mean" + assert np.array_equal(storage["sum_of_weights"], [0, 3, 0, 0, 0, 0]) + assert np.array_equal(storage["sum_of_weights_squared"], [0, 5, 0, 0, 0, 0]) + assert np.allclose(storage["variances"], [0, 2, 0, 0, 0, 0]) + def test_uhi_protocol_object(self): values = np.arange(12.0)