Skip to content

Commit 1c021bd

Browse files
committed
[hist] Fix casts in TH[123]L::SetBinContent
1 parent 2e88d40 commit 1c021bd

File tree

7 files changed

+35
-4
lines changed

7 files changed

+35
-4
lines changed

hist/hist/inc/TH1.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ class TH1L: public TH1, public TArrayL64 {
862862

863863
protected:
864864
Double_t RetrieveBinContent(Int_t bin) const override { return Double_t (fArray[bin]); }
865-
void UpdateBinContent(Int_t bin, Double_t content) override { fArray[bin] = Int_t (content); }
865+
void UpdateBinContent(Int_t bin, Double_t content) override { fArray[bin] = Long64_t (content); }
866866
};
867867

868868
TH1L operator*(Double_t c1, const TH1L &h1);

hist/hist/inc/TH2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class TH2L : public TH2, public TArrayL64 {
325325

326326
protected:
327327
Double_t RetrieveBinContent(Int_t bin) const override { return Double_t (fArray[bin]); }
328-
void UpdateBinContent(Int_t bin, Double_t content) override { fArray[bin] = Int_t (content); }
328+
void UpdateBinContent(Int_t bin, Double_t content) override { fArray[bin] = Long64_t (content); }
329329

330330
ClassDefOverride(TH2L,0) //2-Dim histograms (one 64 bit integer per channel)
331331
};

hist/hist/inc/TH3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class TH3L : public TH3, public TArrayL64 {
343343

344344
protected:
345345
Double_t RetrieveBinContent(Int_t bin) const override { return Double_t (fArray[bin]); }
346-
void UpdateBinContent(Int_t bin, Double_t content) override { fArray[bin] = Int_t (content); }
346+
void UpdateBinContent(Int_t bin, Double_t content) override { fArray[bin] = Long64_t (content); }
347347

348348
ClassDefOverride(TH3L,0) //3-Dim histograms (one 64 bit integer per channel)
349349
};

hist/hist/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ROOT_ADD_GTEST(testTH2PolyAdd test_TH2Poly_Add.cxx LIBRARIES Hist Matrix MathCor
1111
ROOT_ADD_GTEST(testTH2PolyGetNumberOfBins test_TH2Poly_GetNumberOfBins.cxx LIBRARIES Hist Matrix MathCore RIO)
1212
ROOT_ADD_GTEST(testTHn THn.cxx LIBRARIES Hist Matrix MathCore RIO)
1313
ROOT_ADD_GTEST(testTH1 test_TH1.cxx LIBRARIES Hist)
14+
ROOT_ADD_GTEST(testTH2 test_TH2.cxx LIBRARIES Hist)
1415
ROOT_ADD_GTEST(testTH3 test_TH3.cxx LIBRARIES Hist)
1516
ROOT_ADD_GTEST(testTHStack test_THStack.cxx LIBRARIES Hist)
1617
ROOT_ADD_GTEST(testProject3Dname test_Project3D_name.cxx LIBRARIES Hist)

hist/hist/test/test_TH1.cxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,12 @@ TEST(TAxis, EqualBinEdges)
320320
{
321321
ROOT_EXPECT_ERROR(TAxis _({1, 1}), "TAxis::Set", "bins must be in increasing order");
322322
}
323+
324+
TEST(TH1L, SetBinContent)
325+
{
326+
TH1L h("", "", 1, 0, 1);
327+
// Something that does not fit into Int_t, but is exactly representable in Double_t
328+
static constexpr long long Large = 1LL << 42;
329+
h.SetBinContent(1, Large);
330+
EXPECT_EQ(h.GetBinContent(1), Large);
331+
}

hist/hist/test/test_TH2.cxx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "gtest/gtest.h"
2+
3+
#include "TH2.h"
4+
5+
TEST(TH2L, SetBinContent)
6+
{
7+
TH2L h("", "", 1, 0, 1, 1, 0, 1);
8+
// Something that does not fit into Int_t, but is exactly representable in Double_t
9+
static constexpr long long Large = 1LL << 42;
10+
h.SetBinContent(1, 1, Large);
11+
EXPECT_EQ(h.GetBinContent(1, 1), Large);
12+
}

hist/hist/test/test_TH3.cxx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
#include <random>
66
#include <thread>
77

8+
TEST(TH3L, SetBinContent)
9+
{
10+
TH3L h("", "", 1, 0, 1, 1, 0, 1, 1, 0, 1);
11+
// Something that does not fit into Int_t, but is exactly representable in Double_t
12+
static constexpr long long Large = 1LL << 42;
13+
h.SetBinContent(1, 1, 1, Large);
14+
EXPECT_EQ(h.GetBinContent(1, 1, 1), Large);
15+
}
16+
817
#ifdef __cpp_lib_atomic_ref
918

1019
TEST(TH3D, FillThreadSafe)
@@ -64,4 +73,4 @@ TEST(TH3D, FillAtomicNoSumW2)
6473
EXPECT_THROW(ROOT::Internal::FillThreadSafe(h0, 1., 2., 3., 4.), std::logic_error);
6574
}
6675

67-
#endif
76+
#endif

0 commit comments

Comments
 (0)