b2cd752
From af2e5c8b1bf17a0a0e1c1492735395bdd49ffd65 Mon Sep 17 00:00:00 2001
b2cd752
From: Mattias Ellert <mattias.ellert@physics.uu.se>
b2cd752
Date: Mon, 29 Jun 2020 12:57:43 +0200
b2cd752
Subject: [PATCH] Fix off-by-one error in the histogram v7 bin iterator
b2cd752
b2cd752
The histogram bin iterator should start at 1 and end at N + 1, not
b2cd752
start at 0 and end at N. (As for all iterators, the end element is the
b2cd752
invalid element after the last one.)
b2cd752
b2cd752
Fixes an assertion in the histhistv7testUnit test
b2cd752
b2cd752
[----------] 2 tests from BinIterNBins
b2cd752
[ RUN      ] BinIterNBins.NumBins
b2cd752
/usr/include/c++/10/bits/stl_vector.h:1045: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = float; _Alloc = std::allocator<float>; std::vector<_Tp, _Alloc>::reference = float&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]: Assertion '__builtin_expect(__n < this->size(), true)' failed.
b2cd752
---
b2cd752
 hist/histv7/inc/ROOT/RHist.hxx | 4 ++--
b2cd752
 hist/histv7/test/biniter.cxx   | 2 +-
b2cd752
 2 files changed, 3 insertions(+), 3 deletions(-)
b2cd752
b2cd752
diff --git a/hist/histv7/inc/ROOT/RHist.hxx b/hist/histv7/inc/ROOT/RHist.hxx
b2cd752
index ebb319f1ef..9521fe182f 100644
b2cd752
--- a/hist/histv7/inc/ROOT/RHist.hxx
b2cd752
+++ b/hist/histv7/inc/ROOT/RHist.hxx
b2cd752
@@ -158,9 +158,9 @@ public:
b2cd752
    /// Get the uncertainty on the content of the bin at `x`.
b2cd752
    double GetBinUncertainty(const CoordArray_t &x) const { return fImpl->GetBinUncertainty(x); }
b2cd752
 
b2cd752
-   const_iterator begin() const { return const_iterator(*fImpl); }
b2cd752
+   const_iterator begin() const { return const_iterator(*fImpl, 1); }
b2cd752
 
b2cd752
-   const_iterator end() const { return const_iterator(*fImpl, fImpl->GetNBinsNoOver()); }
b2cd752
+   const_iterator end() const { return const_iterator(*fImpl, fImpl->GetNBinsNoOver() + 1); }
b2cd752
 
b2cd752
    /// Swap *this and other.
b2cd752
    ///
b2cd752
diff --git a/hist/histv7/test/biniter.cxx b/hist/histv7/test/biniter.cxx
b2cd752
index aa1c2dfef9..1f940aa8e7 100644
b2cd752
--- a/hist/histv7/test/biniter.cxx
b2cd752
+++ b/hist/histv7/test/biniter.cxx
b2cd752
@@ -45,7 +45,7 @@ TEST(BinIterNBins, BinRef) {
b2cd752
   double founduncert = -1.;
b2cd752
   RH2F::CoordArray_t foundcoord{};
b2cd752
 
b2cd752
-  int nBins = 0;
b2cd752
+  int nBins = 1;
b2cd752
   for (auto bin: h) {
b2cd752
     auto binCenter = bin.GetCenter();
b2cd752
     if (std::fabs(binCenter[0] - x) < 0.1 && std::fabs(binCenter[1] - y) < 0.1) {
b2cd752
-- 
b2cd752
2.26.2
b2cd752