From dded97b7998d0d0198faf79f8f20263bb757b41d Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Wed, 17 Jun 2026 17:37:09 +0200 Subject: [PATCH 1/5] [hist] Implement SetBinContent with std::vector This will be used for the UHI pythonizations. --- hist/histv7/inc/ROOT/RHist.hxx | 33 ++++++++++++++++++++-- hist/histv7/inc/ROOT/RHistEngine.hxx | 41 ++++++++++++++++++++++++++-- hist/histv7/test/hist_engine.cxx | 18 ++++++++++++ hist/histv7/test/hist_hist.cxx | 10 +++++++ 4 files changed, 98 insertions(+), 4 deletions(-) diff --git a/hist/histv7/inc/ROOT/RHist.hxx b/hist/histv7/inc/ROOT/RHist.hxx index a3e0c112a76ca..8a544de10b97c 100644 --- a/hist/histv7/inc/ROOT/RHist.hxx +++ b/hist/histv7/inc/ROOT/RHist.hxx @@ -279,6 +279,34 @@ public: fStats.Taint(); } + /// Set the content of a single bin. + /// + /// \code + /// ROOT::Experimental::RHist hist({/* two dimensions */}); + /// std::vector indices = {3, 5}; + /// int value = /* ... */; + /// hist.SetBinContent(indices, value); + /// \endcode + /// + /// \note Compared to TH1 conventions, the first normal bin has index 0 and underflow and overflow bins are special + /// values. See also the class documentation of RBinIndex. + /// + /// Throws an exception if the number of indices does not match the axis configuration or the bin is not found. + /// + /// \warning Setting the bin content will taint the global histogram statistics. Attempting to access its values, for + /// example calling GetNEntries(), will throw exceptions. + /// + /// \param[in] indices the vector of indices for each axis + /// \param[in] value the new value of the bin content + /// \par See also + /// the \ref SetBinContent(const A &... args) "variadic function template overload" accepting arguments directly + template + void SetBinContent(const std::vector &indices, const V &value) + { + fEngine.SetBinContent(indices, value); + fStats.Taint(); + } + /// Set the content of a single bin. /// /// \code @@ -299,8 +327,9 @@ public: /// /// \param[in] args the arguments for each axis and the new value of the bin content /// \par See also - /// the \ref SetBinContent(const std::array &indices, const V &value) "function overload" accepting - /// `std::array` + /// the function overloads accepting + /// \ref SetBinContent(const std::array &indices, const V &value) const "`std::array`" or + /// \ref SetBinContent(const std::vector &indices, const V &value) const "`std::vector`" template void SetBinContent(const A &...args) { diff --git a/hist/histv7/inc/ROOT/RHistEngine.hxx b/hist/histv7/inc/ROOT/RHistEngine.hxx index 29a4d8f5fdf5f..e958e982254af 100644 --- a/hist/histv7/inc/ROOT/RHistEngine.hxx +++ b/hist/histv7/inc/ROOT/RHistEngine.hxx @@ -298,6 +298,42 @@ public: fBinContents[index.fIndex] = value; } + /// Set the content of a single bin. + /// + /// \code + /// ROOT::Experimental::RHistEngine hist({/* two dimensions */}); + /// std::vector indices = {3, 5}; + /// int value = /* ... */; + /// hist.SetBinContent(indices, value); + /// \endcode + /// + /// \note Compared to TH1 conventions, the first normal bin has index 0 and underflow and overflow bins are special + /// values. See also the class documentation of RBinIndex. + /// + /// Throws an exception if the number of indices does not match the axis configuration or the bin is not found. + /// + /// \param[in] indices the vector of indices for each axis + /// \param[in] value the new value of the bin content + /// \par See also + /// the \ref SetBinContent(const A &... args) "variadic function template overload" accepting arguments directly + template + void SetBinContent(const std::vector &indices, const V &value) + { + // We could rely on RAxes::ComputeGlobalIndex to check the number of arguments, but its exception message might + // be confusing for users. + if (indices.size() != GetNDimensions()) { + throw std::invalid_argument("invalid number of indices passed to SetBinContent"); + } + RLinearizedIndex index = fAxes.ComputeGlobalIndex(indices); + if (!index.fValid) { + throw std::invalid_argument("bin not found in SetBinContent"); + } + assert(index.fIndex < fBinContents.size()); + // To allow conversion, we have to accept value with a template type V to capture any argument. Otherwise it would + // select the variadic function template... + fBinContents[index.fIndex] = value; + } + /// \} // End the group to ensure that all contained member functions are public. @@ -330,8 +366,9 @@ public: /// /// \param[in] args the arguments for each axis and the new value of the bin content /// \par See also - /// the \ref SetBinContent(const std::array &indices, const V &value) "function overload" accepting - /// `std::array` + /// the function overloads accepting + /// \ref SetBinContent(const std::array &indices, const V &value) const "`std::array`" or + /// \ref SetBinContent(const std::vector &indices, const V &value) const "`std::vector`" template void SetBinContent(const A &...args) { diff --git a/hist/histv7/test/hist_engine.cxx b/hist/histv7/test/hist_engine.cxx index 2d5ede53787e2..36482849c78e9 100644 --- a/hist/histv7/test/hist_engine.cxx +++ b/hist/histv7/test/hist_engine.cxx @@ -144,6 +144,10 @@ TEST(RHistEngine, SetBinContent) engine.SetBinContent(indices, 43); EXPECT_EQ(engine.GetBinContent(indices), 43); + const std::vector indicesV = {index}; + engine.SetBinContent(indicesV, 44); + EXPECT_EQ(engine.GetBinContent(indicesV), 44); + // This also works if the value must be converted to the bin content type. RHistEngine engineF({axis}); engineF.SetBinContent(index, 42); @@ -168,6 +172,17 @@ TEST(RHistEngine, SetBinContentInvalidNumberOfArguments) EXPECT_THROW(engine2.SetBinContent(1, 0), std::invalid_argument); EXPECT_NO_THROW(engine2.SetBinContent(1, 2, 0)); EXPECT_THROW(engine2.SetBinContent(1, 2, 3, 0), std::invalid_argument); + + const std::vector indicesV1 = {1}; + const std::vector indicesV2 = {1, 2}; + const std::vector indicesV3 = {1, 2, 3}; + + EXPECT_NO_THROW(engine1.SetBinContent(indicesV1, 0)); + EXPECT_THROW(engine1.SetBinContent(indicesV2, 0), std::invalid_argument); + + EXPECT_THROW(engine2.SetBinContent(indicesV1, 0), std::invalid_argument); + EXPECT_NO_THROW(engine2.SetBinContent(indicesV2, 0)); + EXPECT_THROW(engine2.SetBinContent(indicesV3, 0), std::invalid_argument); } TEST(RHistEngine, SetBinContentNotFound) @@ -177,6 +192,9 @@ TEST(RHistEngine, SetBinContentNotFound) RHistEngine engine({axis}); EXPECT_THROW(engine.SetBinContent(Bins, 0), std::invalid_argument); + + const std::vector indicesV = {Bins}; + EXPECT_THROW(engine.GetBinContent(indicesV), std::invalid_argument); } TEST(RHistEngine, Add) diff --git a/hist/histv7/test/hist_hist.cxx b/hist/histv7/test/hist_hist.cxx index 536ec073ff821..33e9589d8dff4 100644 --- a/hist/histv7/test/hist_hist.cxx +++ b/hist/histv7/test/hist_hist.cxx @@ -107,6 +107,16 @@ TEST(RHist, SetBinContent) EXPECT_TRUE(hist.GetStats().IsTainted()); EXPECT_THROW(hist.GetNEntries(), std::logic_error); } + + { + RHist hist(axis); + ASSERT_FALSE(hist.GetStats().IsTainted()); + const std::vector indices = {2}; + hist.SetBinContent(indices, 42); + EXPECT_EQ(hist.GetBinContent(indices), 42); + EXPECT_TRUE(hist.GetStats().IsTainted()); + EXPECT_THROW(hist.GetNEntries(), std::logic_error); + } } TEST(RHist, Add) From 93681967692edbc1766330446ee7f623e4ef7318 Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Wed, 24 Jun 2026 13:50:03 +0200 Subject: [PATCH 2/5] [hist] Implement RAxisVariant::IsValidIndex It uses the underlying GetLinearizedIndex(), and will be used for the UHI pythonizations. --- hist/histv7/inc/ROOT/RAxisVariant.hxx | 17 ++++++++++++ hist/histv7/test/hist_axis_variant.cxx | 36 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/hist/histv7/inc/ROOT/RAxisVariant.hxx b/hist/histv7/inc/ROOT/RAxisVariant.hxx index bf945da177518..bc1acab43a5ab 100644 --- a/hist/histv7/inc/ROOT/RAxisVariant.hxx +++ b/hist/histv7/inc/ROOT/RAxisVariant.hxx @@ -8,6 +8,7 @@ #include "RBinIndex.hxx" #include "RBinIndexRange.hxx" #include "RCategoricalAxis.hxx" +#include "RLinearizedIndex.hxx" #include "RRegularAxis.hxx" #include "RSliceSpec.hxx" #include "RVariableBinAxis.hxx" @@ -78,6 +79,22 @@ public: } } + /// \return true if the bin index is valid + bool IsValidIndex(RBinIndex index) const + { + RLinearizedIndex linIndex; + if (auto *regular = GetRegularAxis()) { + linIndex = regular->GetLinearizedIndex(index); + } else if (auto *variable = GetVariableBinAxis()) { + linIndex = variable->GetLinearizedIndex(index); + } else if (auto *categorical = GetCategoricalAxis()) { + linIndex = categorical->GetLinearizedIndex(index); + } else { + throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE + } + return linIndex.fValid; + } + /// Get the range of all normal bins. /// /// \return the bin index range from the first to the last normal bin, inclusive diff --git a/hist/histv7/test/hist_axis_variant.cxx b/hist/histv7/test/hist_axis_variant.cxx index 76f89accecae9..e4f3305ae2f83 100644 --- a/hist/histv7/test/hist_axis_variant.cxx +++ b/hist/histv7/test/hist_axis_variant.cxx @@ -17,6 +17,12 @@ TEST(RAxisVariant, RegularAxis) EXPECT_EQ(axis.GetNNormalBins(), Bins); EXPECT_EQ(axis.GetTotalNBins(), Bins + 2); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex::Underflow())); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex(1))); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex::Overflow())); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex(Bins))); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex())); + const auto normal = axis.GetNormalRange(); EXPECT_EQ(std::distance(normal.begin(), normal.end()), Bins); const auto normal12 = axis.GetNormalRange(1, 2); @@ -35,6 +41,12 @@ TEST(RAxisVariant, RegularAxis) EXPECT_EQ(axis.GetNNormalBins(), Bins); EXPECT_EQ(axis.GetTotalNBins(), Bins); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex::Underflow())); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex(1))); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex::Overflow())); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex(Bins))); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex())); + const auto normal = axis.GetNormalRange(); EXPECT_EQ(std::distance(normal.begin(), normal.end()), Bins); const auto normal12 = axis.GetNormalRange(1, 2); @@ -68,6 +80,12 @@ TEST(RAxisVariant, VariableBinAxis) EXPECT_EQ(axis.GetNNormalBins(), Bins); EXPECT_EQ(axis.GetTotalNBins(), Bins + 2); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex::Underflow())); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex(1))); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex::Overflow())); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex(Bins))); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex())); + const auto normal = axis.GetNormalRange(); EXPECT_EQ(std::distance(normal.begin(), normal.end()), Bins); const auto normal12 = axis.GetNormalRange(1, 2); @@ -86,6 +104,12 @@ TEST(RAxisVariant, VariableBinAxis) EXPECT_EQ(axis.GetNNormalBins(), Bins); EXPECT_EQ(axis.GetTotalNBins(), Bins); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex::Underflow())); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex(1))); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex::Overflow())); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex(Bins))); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex())); + const auto normal = axis.GetNormalRange(); EXPECT_EQ(std::distance(normal.begin(), normal.end()), Bins); const auto normal12 = axis.GetNormalRange(1, 2); @@ -114,6 +138,12 @@ TEST(RAxisVariant, CategoricalAxis) EXPECT_EQ(axis.GetNNormalBins(), 3); EXPECT_EQ(axis.GetTotalNBins(), 4); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex::Underflow())); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex(1))); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex::Overflow())); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex(3))); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex())); + const auto normal = axis.GetNormalRange(); EXPECT_EQ(std::distance(normal.begin(), normal.end()), 3); const auto normal12 = axis.GetNormalRange(1, 2); @@ -132,6 +162,12 @@ TEST(RAxisVariant, CategoricalAxis) EXPECT_EQ(axis.GetNNormalBins(), 3); EXPECT_EQ(axis.GetTotalNBins(), 3); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex::Underflow())); + EXPECT_TRUE(axis.IsValidIndex(RBinIndex(1))); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex::Overflow())); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex(3))); + EXPECT_FALSE(axis.IsValidIndex(RBinIndex())); + const auto normal = axis.GetNormalRange(); EXPECT_EQ(std::distance(normal.begin(), normal.end()), 3); const auto normal12 = axis.GetNormalRange(1, 2); From 9d3809c5aa33365d35de73a4590990ee44e6c1c1 Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Fri, 19 Jun 2026 08:51:00 +0200 Subject: [PATCH 3/5] [hist] Implement UHI Indexing: Access and Setting Not included (for now): data coordinates using loc, and setting with an array. --- .../pythonizations/python/CMakeLists.txt | 5 + .../python/ROOT/_pythonization/_histv7.py | 84 ++++++++++++++ hist/histv7/test/CMakeLists.txt | 1 + hist/histv7/test/hist_uhi.py | 109 ++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py create mode 100644 hist/histv7/test/hist_uhi.py diff --git a/bindings/pyroot/pythonizations/python/CMakeLists.txt b/bindings/pyroot/pythonizations/python/CMakeLists.txt index 97027218324f9..c29692730ab89 100644 --- a/bindings/pyroot/pythonizations/python/CMakeLists.txt +++ b/bindings/pyroot/pythonizations/python/CMakeLists.txt @@ -50,6 +50,11 @@ if(roofit) ROOT/_pythonization/_roofit/_utils.py) endif() +if(root7) + list(APPEND PYROOT_EXTRA_PYTHON_SOURCES + ROOT/_pythonization/_histv7.py) +endif() + if(tmva) list(APPEND PYROOT_EXTRA_PYTHON_SOURCES ROOT/_pythonization/_tmva/_crossvalidation.py diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py new file mode 100644 index 0000000000000..2ce07ac5d81c8 --- /dev/null +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py @@ -0,0 +1,84 @@ +from . import pythonization + + +def _is_RBinIndex(index): + return hasattr(type(index), "__cpp_name__") and type(index).__cpp_name__ == "ROOT::Experimental::RBinIndex" + + +def _is_index(index): + import ROOT + + # TODO: implement support for loc + return isinstance(index, int) or _is_RBinIndex(index) or index == ROOT.uhi.underflow or index == ROOT.uhi.overflow + + +def _is_tuple_of_indices(indices): + return all([_is_index(i) for i in indices]) + + +def _translate_index(index): + """Translate a single Python index into an RBinIndex.""" + import ROOT + + # TODO: implement support for loc + + if isinstance(index, int): + return ROOT.Experimental.RBinIndex(index) + elif _is_RBinIndex(index): + return index + elif index == ROOT.uhi.underflow: + return ROOT.Experimental.RBinIndex.Underflow() + elif index == ROOT.uhi.overflow: + return ROOT.Experimental.RBinIndex.Overflow() + else: + raise TypeError(f"unsupported index type: {type(index).__name__}") + + +def _translate_indices(self, indices): + """Translate a tuple of Python indices into a std::vector.""" + if len(indices) != self.GetNDimensions(): + raise IndexError(f"invalid number of indices: expected {self.GetNDimensions()}, but got {len(indices)}") + + import ROOT + + axes = self.GetAxes() + vec = ROOT.std.vector["ROOT::Experimental::RBinIndex"]() + for i, index in enumerate(indices): + binIndex = _translate_index(index) + if not axes[i].IsValidIndex(binIndex): + raise IndexError(f"invalid bin index for axis {i}: {index}") + vec.push_back(binIndex) + return vec + + +def _ensure_tuple(index): + """Ensure that the passed Python index is wrapped in a tuple.""" + return (index,) if not isinstance(index, tuple) else index + + +def _getitem(self, index): + indices = _ensure_tuple(index) + if not _is_tuple_of_indices(indices): + raise ValueError("could not convert indices") + indices = _translate_indices(self, indices) + return self.GetBinContent(indices) + + +def _setitem(self, index, value): + indices = _ensure_tuple(index) + if not _is_tuple_of_indices(indices): + raise ValueError("could not convert indices") + indices = _translate_indices(self, indices) + self.SetBinContent(indices, value) + + +@pythonization("RHistEngine<", ns="ROOT::Experimental", is_prefix=True) +def pythonize_engine(klass): + klass.__getitem__ = _getitem + klass.__setitem__ = _setitem + + +@pythonization("RHist<", ns="ROOT::Experimental", is_prefix=True) +def pythonize_hist(klass): + klass.__getitem__ = _getitem + klass.__setitem__ = _setitem diff --git a/hist/histv7/test/CMakeLists.txt b/hist/histv7/test/CMakeLists.txt index c83ffc6302912..0fa5cc4f2279f 100644 --- a/hist/histv7/test/CMakeLists.txt +++ b/hist/histv7/test/CMakeLists.txt @@ -21,5 +21,6 @@ if(NOT DEFINED histv7_standalone) if(pyroot) ROOT_ADD_PYUNITTEST(hist_py hist.py) + ROOT_ADD_PYUNITTEST(hist_uhi_py hist_uhi.py) endif() endif() diff --git a/hist/histv7/test/hist_uhi.py b/hist/histv7/test/hist_uhi.py new file mode 100644 index 0000000000000..ae8ee2eba8874 --- /dev/null +++ b/hist/histv7/test/hist_uhi.py @@ -0,0 +1,109 @@ +import unittest + +import ROOT + +RBinIndex = ROOT.Experimental.RBinIndex +RHist = ROOT.Experimental.RHist +RHistEngine = ROOT.Experimental.RHistEngine +RRegularAxis = ROOT.Experimental.RRegularAxis + + +class HistEngine_Indexing(unittest.TestCase): + def test_access(self): + Bins = 20 + axis = RRegularAxis(Bins, (0, Bins)) + engine = RHistEngine["int"](axis) + + # Fill and access a normal bin + engine.Fill(1.5) + self.assertEqual(engine[1], 1) + self.assertEqual(engine[RBinIndex(1)], 1) + + with self.assertRaises(IndexError): + engine[Bins] + + with self.assertRaises(IndexError): + engine[0, 1] + + # Fill and access the underflow bin + engine.Fill(-100) + self.assertEqual(engine[ROOT.uhi.underflow], 1) + self.assertEqual(engine[RBinIndex.Underflow()], 1) + + # Fill and access the overflow bin + engine.Fill(100) + self.assertEqual(engine[ROOT.uhi.overflow], 1) + self.assertEqual(engine[RBinIndex.Overflow()], 1) + + # Test multidimensional histogram + engine = RHistEngine["int"](axis, axis) + engine.Fill(1.5, 2.5) + self.assertEqual(engine[1, 2], 1) + self.assertEqual(engine[RBinIndex(1), RBinIndex(2)], 1) + + with self.assertRaises(IndexError): + engine[0] + + def test_setting(self): + Bins = 20 + axis = RRegularAxis(Bins, (0, Bins)) + engine = RHistEngine["int"](axis) + + # Set a normal bin + engine[1] = 2 + self.assertEqual(engine.GetBinContent(RBinIndex(1)), 2) + + with self.assertRaises(IndexError): + engine[Bins] = 2 + + with self.assertRaises(IndexError): + engine[0, 1] = 2 + + # Set the underflow bin + engine[ROOT.uhi.underflow] = 3 + self.assertEqual(engine.GetBinContent(RBinIndex.Underflow()), 3) + + # Set the overflow bin + engine[ROOT.uhi.overflow] = 4 + self.assertEqual(engine.GetBinContent(RBinIndex.Overflow()), 4) + + # Test multidimensional histogram + engine = RHistEngine["int"](axis, axis) + engine[1, 2] = 5 + self.assertEqual(engine.GetBinContent(RBinIndex(1), RBinIndex(2)), 5) + + with self.assertRaises(IndexError): + engine[0] = 5 + + +class Hist_Indexing(unittest.TestCase): + def test_access(self): + Bins = 20 + hist = RHist["int"](Bins, (0, Bins)) + + # Fill and access a normal bin + hist.Fill(1.5) + self.assertEqual(hist[1], 1) + self.assertEqual(hist[RBinIndex(1)], 1) + + with self.assertRaises(IndexError): + hist[Bins] + + with self.assertRaises(IndexError): + hist[0, 1] + + def test_setting(self): + Bins = 20 + hist = RHist["int"](Bins, (0, Bins)) + self.assertFalse(hist.GetStats().IsTainted()) + + # Set a normal bin + hist[1] = 2 + self.assertEqual(hist.GetBinContent(RBinIndex(1)), 2) + self.assertTrue(hist.GetStats().IsTainted()) + + with self.assertRaises(IndexError): + hist[Bins] = 2 + + with self.assertRaises(IndexError): + hist[0, 1] = 2 From c0436ff228ff5ef977e647642b80f71270fecfff Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Tue, 10 Nov 2026 13:32:29 +0100 Subject: [PATCH 4/5] [hist] Implement UHI Indexing: Slicing Expansion of the user argument (dictionaries, ellipsis) will come in the next commit. --- .../python/ROOT/_pythonization/_histv7.py | 117 +++++++++++++++++- hist/histv7/test/hist_uhi.py | 71 +++++++++++ 2 files changed, 184 insertions(+), 4 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py index 2ce07ac5d81c8..0b105984dac12 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py @@ -51,6 +51,113 @@ def _translate_indices(self, indices): return vec +def _is_operation(op): + import ROOT + + return isinstance(op, ROOT.uhi.rebin) or op == ROOT.uhi.sum or op == sum + + +def _translate_operation(op): + """Translate a single Python "object" representing an operation into an object for RSliceSpec.""" + import ROOT + + if isinstance(op, ROOT.uhi.rebin): + return ROOT.Experimental.RSliceSpec.ROperationRebin(op.ngroup) + elif op == ROOT.uhi.sum or op == sum: + return ROOT.Experimental.RSliceSpec.ROperationSum() + else: + raise TypeError(f"unsupported operation type: {type(op).__name__}") + + +def _translate_slice(self, i, axis, s): + """Translate a single Python slice into an RSliceSpec.""" + import ROOT + + RSliceSpec = ROOT.Experimental.RSliceSpec + + if _is_index(s): + index = _translate_index(s) + if not axis.IsValidIndex(index): + raise IndexError(f"invalid bin index for axis {i}: {s}") + if not index.IsNormal(): + raise IndexError("normal bin required in slicing") + + # A single value v is like v:v+1:sum + r = axis.GetNormalRange(index, index + 1) + return RSliceSpec(r, RSliceSpec.ROperationSum()) + + elif _is_operation(s): + operation = _translate_operation(s) + return RSliceSpec(operation) + + elif isinstance(s, slice): + begin = None + if s.start is not None: + begin = _translate_index(s.start) + if not begin.IsNormal(): + raise IndexError("normal bin required in slicing") + + end = None + if s.stop is not None: + if s.stop == len: + end = ROOT.Experimental.RBinIndex(axis.GetNNormalBins()) + else: + end = _translate_index(s.stop) + if not end.IsNormal(): + raise IndexError("normal bin required in slicing") + + operation = None + if s.step is not None: + operation = _translate_operation(s.step) + + if begin is None and end is None: + if operation is None: + return RSliceSpec() + else: + return RSliceSpec(operation) + + # At least one endpoint is given. + elif operation is not None and type(operation).__cpp_name__ == "ROOT::Experimental::RSliceSpec::ROperationSum": + # For sums it makes a difference whether flow bins are included. If only one endpoint is given, we cannot guess. + if begin is None or end is None: + raise IndexError("either none or both endpoints required for sum") + + r = axis.GetNormalRange(begin, end) + return RSliceSpec(r, operation) + + # For other slice operations, it does not make a difference whether the range includes the flow bins. + elif begin is None: + begin = ROOT.Experimental.RBinIndex(0) + elif end is None: + end = ROOT.Experimental.RBinIndex(axis.GetNNormalBins()) + + r = axis.GetNormalRange(begin, end) + if operation is None: + return RSliceSpec(r) + else: + return RSliceSpec(r, operation) + + else: + raise TypeError(f"unsupported index type: {type(s).__name__}") + + +def _translate_slices(self, slices): + """Translate a tuple of Python slices into a std::vector.""" + # TODO: expand ellipsis and dictionaries + + if len(slices) != self.GetNDimensions(): + raise IndexError(f"invalid number of slices: expected {self.GetNDimensions()}, but got {len(slices)}") + + import ROOT + + axes = self.GetAxes() + vec = ROOT.std.vector["ROOT::Experimental::RSliceSpec"]() + for i, s in enumerate(slices): + sliceSpec = _translate_slice(self, i, axes[i], s) + vec.push_back(sliceSpec) + return vec + + def _ensure_tuple(index): """Ensure that the passed Python index is wrapped in a tuple.""" return (index,) if not isinstance(index, tuple) else index @@ -58,10 +165,12 @@ def _ensure_tuple(index): def _getitem(self, index): indices = _ensure_tuple(index) - if not _is_tuple_of_indices(indices): - raise ValueError("could not convert indices") - indices = _translate_indices(self, indices) - return self.GetBinContent(indices) + if _is_tuple_of_indices(indices): + indices = _translate_indices(self, indices) + return self.GetBinContent(indices) + else: + slices = _translate_slices(self, indices) + return self.Slice(slices) def _setitem(self, index, value): diff --git a/hist/histv7/test/hist_uhi.py b/hist/histv7/test/hist_uhi.py index ae8ee2eba8874..41533ed6760c2 100644 --- a/hist/histv7/test/hist_uhi.py +++ b/hist/histv7/test/hist_uhi.py @@ -44,6 +44,68 @@ def test_access(self): with self.assertRaises(IndexError): engine[0] + def test_slicing(self): + Bins = 20 + axis = RRegularAxis(Bins, (0, Bins)) + engine = RHistEngine["int"](axis) + + # Slice without operation + self.assertEqual(engine[:].GetTotalNBins(), 22) + self.assertEqual(engine[3:11].GetTotalNBins(), 10) + self.assertEqual(engine[:11].GetTotalNBins(), 13) + self.assertEqual(engine[3:].GetTotalNBins(), 19) + self.assertEqual(engine[0:len].GetTotalNBins(), 22) + + # The implementation requires normal bins + with self.assertRaises(IndexError): + engine[ROOT.uhi.underflow :] + + # This doesn't even make sense because the endpoint is exclusive... + with self.assertRaises(IndexError): + engine[: ROOT.uhi.overflow] + + # Rebin operation + self.assertEqual(engine[:: ROOT.uhi.rebin(2)].GetTotalNBins(), 12) + self.assertEqual(engine[3 : 11 : ROOT.uhi.rebin(2)].GetTotalNBins(), 6) + # Syntactic sugar without an actual slice + self.assertEqual(engine[ROOT.uhi.rebin(2)].GetTotalNBins(), 12) + + with self.assertRaises(IndexError): + engine[:, :] + + # Test multidimensional slicing + engine = RHistEngine["int"](axis, axis) + self.assertEqual(engine[:, :].GetNDimensions(), 2) + + with self.assertRaises(IndexError): + engine[:] + + # Sum operation + self.assertEqual(engine[:, :: ROOT.uhi.sum].GetNDimensions(), 1) + self.assertEqual(engine[:, 3 : 11 : ROOT.uhi.sum].GetNDimensions(), 1) + self.assertEqual(engine[:, 0 : len : ROOT.uhi.sum].GetNDimensions(), 1) + + # The implementation requires either none or both endpoints + with self.assertRaises(IndexError): + engine[:, 0::sum] + + with self.assertRaises(IndexError): + engine[:, :2:sum] + + # A single value v is like v:v+1:sum + self.assertEqual(engine[:, 2].GetNDimensions(), 1) + + with self.assertRaises(IndexError): + engine[:, Bins] + + with self.assertRaises(IndexError): + engine[:, ROOT.uhi.underflow] + + # Syntactic sugar: builtin sum, and / or without an actual slice + self.assertEqual(engine[:, ::sum].GetNDimensions(), 1) + self.assertEqual(engine[:, ROOT.uhi.sum].GetNDimensions(), 1) + self.assertEqual(engine[:, sum].GetNDimensions(), 1) + def test_setting(self): Bins = 20 axis = RRegularAxis(Bins, (0, Bins)) @@ -92,6 +154,15 @@ def test_access(self): with self.assertRaises(IndexError): hist[0, 1] + def test_slicing(self): + Bins = 20 + hist = RHist["int"](Bins, (0, Bins)) + + self.assertEqual(hist[:].GetTotalNBins(), 22) + + with self.assertRaises(IndexError): + hist[:, :] + def test_setting(self): Bins = 20 hist = RHist["int"](Bins, (0, Bins)) From 2391f8f4baace0ff27d69d57b56a48c605cda1f5 Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Tue, 10 Nov 2026 17:41:05 +0100 Subject: [PATCH 5/5] [hist] Expand user argument for slicing --- .../python/ROOT/_pythonization/_histv7.py | 52 ++++++++++++++----- hist/histv7/test/hist_uhi.py | 30 +++++++++++ 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py index 0b105984dac12..7ab76f8f213a7 100644 --- a/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py +++ b/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_histv7.py @@ -51,6 +51,36 @@ def _translate_indices(self, indices): return vec +def _ensure_tuple(arg): + """Ensure that the passed Python argument is wrapped in a tuple.""" + return (arg,) if not isinstance(arg, tuple) else arg + + +def _expand_slices(slices, ndim): + """Expand user argument into a tuple of Python slices.""" + if isinstance(slices, dict): + # A dictionary sets the slice operation for a subset of the axes + expanded = [slice(None)] * ndim + for axis, value in slices.items(): + if not isinstance(axis, int): + raise IndexError("keys of dictionary must be integers") + if axis >= ndim: + raise IndexError(f"invalid axis key: {axis}") + expanded[axis] = value + return tuple(expanded) + + slices = _ensure_tuple(slices) + + if ... not in slices: + return slices + + if slices.count(...) > 1: + raise IndexError("more than one ellipsis found") + + pos = slices.index(...) + return slices[:pos] + (slice(None),) * (ndim - len(slices) + 1) + slices[pos + 1 :] + + def _is_operation(op): import ROOT @@ -143,7 +173,7 @@ def _translate_slice(self, i, axis, s): def _translate_slices(self, slices): """Translate a tuple of Python slices into a std::vector.""" - # TODO: expand ellipsis and dictionaries + slices = _expand_slices(slices, self.GetNDimensions()) if len(slices) != self.GetNDimensions(): raise IndexError(f"invalid number of slices: expected {self.GetNDimensions()}, but got {len(slices)}") @@ -158,19 +188,15 @@ def _translate_slices(self, slices): return vec -def _ensure_tuple(index): - """Ensure that the passed Python index is wrapped in a tuple.""" - return (index,) if not isinstance(index, tuple) else index - - def _getitem(self, index): - indices = _ensure_tuple(index) - if _is_tuple_of_indices(indices): - indices = _translate_indices(self, indices) - return self.GetBinContent(indices) - else: - slices = _translate_slices(self, indices) - return self.Slice(slices) + if not isinstance(index, dict): + indices = _ensure_tuple(index) + if _is_tuple_of_indices(indices): + indices = _translate_indices(self, indices) + return self.GetBinContent(indices) + + slices = _translate_slices(self, index) + return self.Slice(slices) def _setitem(self, index, value): diff --git a/hist/histv7/test/hist_uhi.py b/hist/histv7/test/hist_uhi.py index 41533ed6760c2..a1f03c7879fed 100644 --- a/hist/histv7/test/hist_uhi.py +++ b/hist/histv7/test/hist_uhi.py @@ -106,6 +106,36 @@ def test_slicing(self): self.assertEqual(engine[:, ROOT.uhi.sum].GetNDimensions(), 1) self.assertEqual(engine[:, sum].GetNDimensions(), 1) + def test_slicing_expand(self): + Bins = 20 + axis = RRegularAxis(Bins, (0, Bins)) + engine = RHistEngine["int"](axis, axis) + + # Ellipsis + self.assertEqual(engine[...].GetNDimensions(), 2) + + sliced = engine[3:11, ...] + self.assertEqual(sliced.GetNDimensions(), 2) + self.assertEqual(sliced.GetAxes()[0].GetNNormalBins(), 8) + + sliced = engine[..., 3:11] + self.assertEqual(sliced.GetNDimensions(), 2) + self.assertEqual(sliced.GetAxes()[1].GetNNormalBins(), 8) + + with self.assertRaises(IndexError): + engine[..., ...] + + # Dictionary + self.assertEqual(engine[{}].GetNDimensions(), 2) + self.assertEqual(engine[{0: slice(None)}].GetNDimensions(), 2) + self.assertEqual(engine[{0: sum}].GetNDimensions(), 1) + + with self.assertRaises(IndexError): + engine[{0.0: sum}] + + with self.assertRaises(IndexError): + engine[{2: slice(None)}] + def test_setting(self): Bins = 20 axis = RRegularAxis(Bins, (0, Bins))