From 6380e422fdd795d99aeed6ea02be239c43f82f6f Mon Sep 17 00:00:00 2001 From: philippe554 Date: Fri, 20 Nov 2026 15:09:34 +0100 Subject: [PATCH] [hist] Replace C limit macros with std::numeric_limits (cherry picked from commit 42ea3cd333203041b4212bb6c1ef23c9b96b6eea) --- hist/hist/inc/TH1.h | 5 +-- hist/hist/src/TH1.cxx | 36 ++++++++++++------- hist/hist/src/TH2.cxx | 29 ++++++++++----- hist/hist/src/TH3.cxx | 29 ++++++++++----- hist/hist/src/THLimitsFinder.cxx | 4 ++- .../spectrumpainter/src/TSpectrum2Painter.cxx | 4 ++- tutorials/hist/hist103_THnSparse_hist.C | 6 ++-- 7 files changed, 77 insertions(+), 36 deletions(-) diff --git a/hist/hist/inc/TH1.h b/hist/hist/inc/TH1.h index 509ad0a10b8ca..cea137bec283e 100644 --- a/hist/hist/inc/TH1.h +++ b/hist/hist/inc/TH1.h @@ -45,6 +45,7 @@ #include #include +#include #include #include #include @@ -556,11 +557,11 @@ class TH1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker { virtual Int_t GetDimension() const { return fDimension; } virtual Double_t GetKurtosis(Int_t axis=1) const; virtual void GetLowEdge(Double_t *edge) const; - virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const; + virtual Double_t GetMaximum(Double_t maxval = std::numeric_limits::max()) const; virtual Int_t GetMaximumBin() const; virtual Int_t GetMaximumBin(Int_t &locmax, Int_t &locmay, Int_t &locmaz) const; virtual Double_t GetMaximumStored() const {return fMaximum;} - virtual Double_t GetMinimum(Double_t minval=-FLT_MAX) const; + virtual Double_t GetMinimum(Double_t minval = -std::numeric_limits::max()) const; virtual Int_t GetMinimumBin() const; virtual Int_t GetMinimumBin(Int_t &locmix, Int_t &locmiy, Int_t &locmiz) const; virtual Double_t GetMinimumStored() const {return fMinimum;} diff --git a/hist/hist/src/TH1.cxx b/hist/hist/src/TH1.cxx index f4465a04f2862..73cbaf85abea2 100644 --- a/hist/hist/src/TH1.cxx +++ b/hist/hist/src/TH1.cxx @@ -8791,7 +8791,7 @@ Double_t TH1::GetMaximum(Double_t maxval) const Int_t ylast = fYaxis.GetLast(); Int_t zfirst = fZaxis.GetFirst(); Int_t zlast = fZaxis.GetLast(); - Double_t maximum = -FLT_MAX, value; + Double_t maximum = -std::numeric_limits::max(), value; for (binz=zfirst;binz<=zlast;binz++) { for (biny=yfirst;biny<=ylast;biny++) { for (binx=xfirst;binx<=xlast;binx++) { @@ -8831,7 +8831,7 @@ Int_t TH1::GetMaximumBin(Int_t &locmax, Int_t &locmay, Int_t &locmaz) const Int_t ylast = fYaxis.GetLast(); Int_t zfirst = fZaxis.GetFirst(); Int_t zlast = fZaxis.GetLast(); - Double_t maximum = -FLT_MAX, value; + Double_t maximum = -std::numeric_limits::max(), value; locm = locmax = locmay = locmaz = 0; for (binz=zfirst;binz<=zlast;binz++) { for (biny=yfirst;biny<=ylast;biny++) { @@ -8881,7 +8881,7 @@ Double_t TH1::GetMinimum(Double_t minval) const Int_t ylast = fYaxis.GetLast(); Int_t zfirst = fZaxis.GetFirst(); Int_t zlast = fZaxis.GetLast(); - Double_t minimum=FLT_MAX, value; + Double_t minimum = std::numeric_limits::max(), value; for (binz=zfirst;binz<=zlast;binz++) { for (biny=yfirst;biny<=ylast;biny++) { for (binx=xfirst;binx<=xlast;binx++) { @@ -8919,7 +8919,7 @@ Int_t TH1::GetMinimumBin(Int_t &locmix, Int_t &locmiy, Int_t &locmiz) const Int_t ylast = fYaxis.GetLast(); Int_t zfirst = fZaxis.GetFirst(); Int_t zlast = fZaxis.GetLast(); - Double_t minimum = FLT_MAX, value; + Double_t minimum = std::numeric_limits::max(), value; locm = locmix = locmiy = locmiz = 0; for (binz=zfirst;binz<=zlast;binz++) { for (biny=yfirst;biny<=ylast;biny++) { @@ -10120,7 +10120,8 @@ TH1I::TH1I(const TH1I &h1i) : TH1(), TArrayI() void TH1I::AddBinContent(Int_t bin) { - if (fArray[bin] < INT_MAX) fArray[bin]++; + if (fArray[bin] < std::numeric_limits::max()) + fArray[bin]++; } //////////////////////////////////////////////////////////////////////////////// @@ -10131,9 +10132,14 @@ void TH1I::AddBinContent(Int_t bin) void TH1I::AddBinContent(Int_t bin, Double_t w) { Long64_t newval = fArray[bin] + Long64_t(w); - if (newval > -INT_MAX && newval < INT_MAX) {fArray[bin] = Int_t(newval); return;} - if (newval < -INT_MAX) fArray[bin] = -INT_MAX; - if (newval > INT_MAX) fArray[bin] = INT_MAX; + if (newval > -std::numeric_limits::max() && newval < std::numeric_limits::max()) { + fArray[bin] = Int_t(newval); + return; + } + if (newval < -std::numeric_limits::max()) + fArray[bin] = -std::numeric_limits::max(); + if (newval > std::numeric_limits::max()) + fArray[bin] = std::numeric_limits::max(); } //////////////////////////////////////////////////////////////////////////////// @@ -10307,7 +10313,8 @@ TH1L::TH1L(const TH1L &h1l) : TH1(), TArrayL64() void TH1L::AddBinContent(Int_t bin) { - if (fArray[bin] < LLONG_MAX) fArray[bin]++; + if (fArray[bin] < std::numeric_limits::max()) + fArray[bin]++; } //////////////////////////////////////////////////////////////////////////////// @@ -10318,9 +10325,14 @@ void TH1L::AddBinContent(Int_t bin) void TH1L::AddBinContent(Int_t bin, Double_t w) { Long64_t newval = fArray[bin] + Long64_t(w); - if (newval > -LLONG_MAX && newval < LLONG_MAX) {fArray[bin] = newval; return;} - if (newval < -LLONG_MAX) fArray[bin] = -LLONG_MAX; - if (newval > LLONG_MAX) fArray[bin] = LLONG_MAX; + if (newval > -std::numeric_limits::max() && newval < std::numeric_limits::max()) { + fArray[bin] = newval; + return; + } + if (newval < -std::numeric_limits::max()) + fArray[bin] = -std::numeric_limits::max(); + if (newval > std::numeric_limits::max()) + fArray[bin] = std::numeric_limits::max(); } //////////////////////////////////////////////////////////////////////////////// diff --git a/hist/hist/src/TH2.cxx b/hist/hist/src/TH2.cxx index 8a109161cdd9d..f9122c6632b1c 100644 --- a/hist/hist/src/TH2.cxx +++ b/hist/hist/src/TH2.cxx @@ -28,6 +28,7 @@ #include "TVirtualHistPainter.h" #include +#include #include "Rebin2DHelpers.h" @@ -3450,7 +3451,8 @@ TH2I::TH2I(const TH2I &h2i) : TH2(), TArrayI() void TH2I::AddBinContent(Int_t bin) { - if (fArray[bin] < INT_MAX) fArray[bin]++; + if (fArray[bin] < std::numeric_limits::max()) + fArray[bin]++; } @@ -3462,9 +3464,14 @@ void TH2I::AddBinContent(Int_t bin) void TH2I::AddBinContent(Int_t bin, Double_t w) { Long64_t newval = fArray[bin] + Long64_t(w); - if (newval > -INT_MAX && newval < INT_MAX) {fArray[bin] = Int_t(newval); return;} - if (newval < -INT_MAX) fArray[bin] = -INT_MAX; - if (newval > INT_MAX) fArray[bin] = INT_MAX; + if (newval > -std::numeric_limits::max() && newval < std::numeric_limits::max()) { + fArray[bin] = Int_t(newval); + return; + } + if (newval < -std::numeric_limits::max()) + fArray[bin] = -std::numeric_limits::max(); + if (newval > std::numeric_limits::max()) + fArray[bin] = std::numeric_limits::max(); } @@ -3678,7 +3685,8 @@ TH2L::TH2L(const TH2L &h2l) : TH2(), TArrayL64() void TH2L::AddBinContent(Int_t bin) { - if (fArray[bin] < LLONG_MAX) fArray[bin]++; + if (fArray[bin] < std::numeric_limits::max()) + fArray[bin]++; } @@ -3690,9 +3698,14 @@ void TH2L::AddBinContent(Int_t bin) void TH2L::AddBinContent(Int_t bin, Double_t w) { Long64_t newval = fArray[bin] + Long64_t(w); - if (newval > -LLONG_MAX && newval < LLONG_MAX) {fArray[bin] = newval; return;} - if (newval < -LLONG_MAX) fArray[bin] = -LLONG_MAX; - if (newval > LLONG_MAX) fArray[bin] = LLONG_MAX; + if (newval > -std::numeric_limits::max() && newval < std::numeric_limits::max()) { + fArray[bin] = newval; + return; + } + if (newval < -std::numeric_limits::max()) + fArray[bin] = -std::numeric_limits::max(); + if (newval > std::numeric_limits::max()) + fArray[bin] = std::numeric_limits::max(); } diff --git a/hist/hist/src/TH3.cxx b/hist/hist/src/TH3.cxx index de7a3de5e0aa7..19d9e773b870c 100644 --- a/hist/hist/src/TH3.cxx +++ b/hist/hist/src/TH3.cxx @@ -27,6 +27,7 @@ #include #include +#include #include @@ -4167,7 +4168,8 @@ TH3I::TH3I(const TH3I &h3i) : TH3(), TArrayI() void TH3I::AddBinContent(Int_t bin) { - if (fArray[bin] < INT_MAX) fArray[bin]++; + if (fArray[bin] < std::numeric_limits::max()) + fArray[bin]++; } @@ -4179,9 +4181,14 @@ void TH3I::AddBinContent(Int_t bin) void TH3I::AddBinContent(Int_t bin, Double_t w) { Long64_t newval = fArray[bin] + Long64_t(w); - if (newval > -INT_MAX && newval < INT_MAX) {fArray[bin] = Int_t(newval); return;} - if (newval < -INT_MAX) fArray[bin] = -INT_MAX; - if (newval > INT_MAX) fArray[bin] = INT_MAX; + if (newval > -std::numeric_limits::max() && newval < std::numeric_limits::max()) { + fArray[bin] = Int_t(newval); + return; + } + if (newval < -std::numeric_limits::max()) + fArray[bin] = -std::numeric_limits::max(); + if (newval > std::numeric_limits::max()) + fArray[bin] = std::numeric_limits::max(); } @@ -4373,7 +4380,8 @@ TH3L::TH3L(const TH3L &h3l) : TH3(), TArrayL64() void TH3L::AddBinContent(Int_t bin) { - if (fArray[bin] < LLONG_MAX) fArray[bin]++; + if (fArray[bin] < std::numeric_limits::max()) + fArray[bin]++; } @@ -4385,9 +4393,14 @@ void TH3L::AddBinContent(Int_t bin) void TH3L::AddBinContent(Int_t bin, Double_t w) { Long64_t newval = fArray[bin] + Long64_t(w); - if (newval > -LLONG_MAX && newval < LLONG_MAX) {fArray[bin] = newval; return;} - if (newval < -LLONG_MAX) fArray[bin] = -LLONG_MAX; - if (newval > LLONG_MAX) fArray[bin] = LLONG_MAX; + if (newval > -std::numeric_limits::max() && newval < std::numeric_limits::max()) { + fArray[bin] = newval; + return; + } + if (newval < -std::numeric_limits::max()) + fArray[bin] = -std::numeric_limits::max(); + if (newval > std::numeric_limits::max()) + fArray[bin] = std::numeric_limits::max(); } diff --git a/hist/hist/src/THLimitsFinder.cxx b/hist/hist/src/THLimitsFinder.cxx index 007758af0cc58..1caff786b79c8 100644 --- a/hist/hist/src/THLimitsFinder.cxx +++ b/hist/hist/src/THLimitsFinder.cxx @@ -13,6 +13,7 @@ #include "THLimitsFinder.h" #include +#include THLimitsFinder *THLimitsFinder::fgLimitsFinder = nullptr; @@ -222,7 +223,8 @@ void THLimitsFinder::Optimize(Double_t A1, Double_t A2, Int_t nold , L20: awidth = (ah-al)/Double_t(ntemp); timemulti = 1; - if (awidth >= FLT_MAX) goto LOK; //in float.h + if (awidth >= std::numeric_limits::max()) + goto LOK; if (awidth <= 0) goto LOK; // If time representation, bin width should be rounded to seconds diff --git a/hist/spectrumpainter/src/TSpectrum2Painter.cxx b/hist/spectrumpainter/src/TSpectrum2Painter.cxx index 74b093d84f76a..389393e2b3fd7 100644 --- a/hist/spectrumpainter/src/TSpectrum2Painter.cxx +++ b/hist/spectrumpainter/src/TSpectrum2Painter.cxx @@ -31,6 +31,7 @@ Pysica Slovaca Vol. 54/ 4 (2004), pp. 385-400. */ #include +#include #include "TROOT.h" #include "TColor.h" @@ -488,7 +489,8 @@ void TSpectrum2Painter::ColorModel(unsigned ui, unsigned ui1, unsigned ui2, a = fRainbow1Step; a = ui*a; a = ui1+a; - if (a >= UINT_MAX) uinc1 = UINT_MAX; + if (a >= std::numeric_limits::max()) + uinc1 = std::numeric_limits::max(); else uinc1 = (unsigned)a; upom = uinc1%256; i = (uinc1-upom)/256; diff --git a/tutorials/hist/hist103_THnSparse_hist.C b/tutorials/hist/hist103_THnSparse_hist.C index 70d9cdd513890..94d7ad8fa5d7a 100644 --- a/tutorials/hist/hist103_THnSparse_hist.C +++ b/tutorials/hist/hist103_THnSparse_hist.C @@ -49,9 +49,7 @@ #include "TStyle.h" #include "TSystem.h" -#ifndef INT_MAX -#define INT_MAX std::numeric_limits::max() -#endif +#include class TTimeHists { public: @@ -217,7 +215,7 @@ void TTimeHists::SetupHist(EHist hist) gSystem->GetMemInfo(&meminfo); Int_t size = 1; for (Int_t d = 0; d < fDim; ++d) { - if ((Int_t)(size * sizeof(Float_t)) > INT_MAX / (fBins + 2) || + if ((Int_t)(size * sizeof(Float_t)) > std::numeric_limits::max() / (fBins + 2) || (meminfo.fMemFree > 0 && meminfo.fMemFree / 2 < (Int_t)(size * sizeof(Float_t) / 1000 / 1000))) throw std::bad_alloc(); size *= (fBins + 2);