diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/indexing.py index 0bf4d341db114..b4d860ff0eca0 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 @@ -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 @@ -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): @@ -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): @@ -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. @@ -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 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): @@ -231,7 +248,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 +256,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/python/ROOT/_pythonization/_uhi/plotting.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py index 58e300e71df89..adb11c3d7dd6b 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/plotting.py @@ -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): @@ -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 diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py index 9fc2111c6884f..bc820c5f64f91 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/serialization.py @@ -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, @@ -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 @@ -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" diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py index 3eb242239a111..a96dd9a5aed0d 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_uhi/tags.py @@ -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: @@ -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: @@ -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") diff --git a/bindings/pyroot/pythonizations/test/uhi_indexing.py b/bindings/pyroot/pythonizations/test/uhi_indexing.py index 1ff3ccf867b84..4fa62b8a3f694 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 @@ -23,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 @@ -38,12 +50,41 @@ 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) 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_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() @@ -178,6 +219,28 @@ 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_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") @@ -243,6 +306,48 @@ 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_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") + + 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") @@ -269,6 +374,47 @@ 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_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") @@ -350,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) @@ -367,5 +529,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__])) diff --git a/bindings/pyroot/pythonizations/test/uhi_plotting.py b/bindings/pyroot/pythonizations/test/uhi_plotting.py index 0842838793ec9..fa8cb997f5f50 100644 --- a/bindings/pyroot/pythonizations/test/uhi_plotting.py +++ b/bindings/pyroot/pythonizations/test/uhi_plotting.py @@ -48,5 +48,31 @@ 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]) + + 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__])) diff --git a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py index f2668ea726a61..17bdf765a7854 100644 --- a/bindings/pyroot/pythonizations/test/uhi_serialization_json.py +++ b/bindings/pyroot/pythonizations/test/uhi_serialization_json.py @@ -79,6 +79,48 @@ 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_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_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) @@ -151,6 +193,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): diff --git a/hist/hist/inc/TH1.h b/hist/hist/inc/TH1.h index cbd931b53fc3b..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 { /** @@ -81,7 +84,20 @@ 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); + + // 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; } @@ -187,12 +203,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 +252,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 +264,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 +287,15 @@ 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; - } - } + // Move the bin contents to the slice + SliceArray(args, dataArray, newArr); - // Copy the flow bins - over += std::accumulate(dataArray + lastOverIdx, dataArray + fN, ValueType{}); - newArr[0] = under; - newArr[newSize - 1] = over; + // 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; 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