Skip to content

Commit 2a5f2a6

Browse files
committed
[hist] raise error when inf/nan edges in TAxis of fixed binwidth
1 parent 34f9d42 commit 2a5f2a6

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

hist/hist/src/TAxis.cxx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "snprintf.h"
2828

2929
#include <iostream>
30+
#include <cmath>
3031
#include <ctime>
3132
#include <cassert>
3233

@@ -779,10 +780,20 @@ void TAxis::SaveAttributes(std::ostream &out, const char *name, const char *subn
779780

780781
////////////////////////////////////////////////////////////////////////////////
781782
/// Initialize axis with fix bins
783+
///
784+
/// An error is printed if xup or xlow are infinite/nan
785+
/// (due to resulting undefined fixed bin width)
786+
///
787+
/// Set xup <= xlow to force the axis range and number of bins to be automatically
788+
/// deduced after buffer is full or BufferEmpty is called
782789

783790
void TAxis::Set(Int_t nbins, Double_t xlow, Double_t xup)
784791
{
785792
fNbins = nbins;
793+
if (std::isinf(xlow) || std::isinf(xup))
794+
Error("TAxis::Set", "fixed binwidth not compatible with infinite lower/upper edges");
795+
if (std::isnan(xlow) || std::isnan(xup))
796+
Error("TAxis::Set", "lower/upper edges should not be NaN");
786797
fXmin = xlow;
787798
fXmax = xup;
788799
if (!fParent) SetDefaults();
@@ -791,6 +802,8 @@ void TAxis::Set(Int_t nbins, Double_t xlow, Double_t xup)
791802

792803
////////////////////////////////////////////////////////////////////////////////
793804
/// Initialize axis with variable bins
805+
///
806+
/// An error is printed if bin edges are not in strictly increasing order
794807

795808
void TAxis::Set(Int_t nbins, const Float_t *xbins)
796809
{
@@ -801,7 +814,7 @@ void TAxis::Set(Int_t nbins, const Float_t *xbins)
801814
fXbins.fArray[bin] = xbins[bin];
802815
for (bin=1; bin<= fNbins; bin++)
803816
if (fXbins.fArray[bin] <= fXbins.fArray[bin - 1])
804-
Error("TAxis::Set", "bins must be in increasing order");
817+
Error("TAxis::Set", "bin edges must be in increasing order");
805818
fXmin = fXbins.fArray[0];
806819
fXmax = fXbins.fArray[fNbins];
807820
if (!fParent) SetDefaults();
@@ -819,7 +832,7 @@ void TAxis::Set(Int_t nbins, const Double_t *xbins)
819832
fXbins.fArray[bin] = xbins[bin];
820833
for (bin=1; bin<= fNbins; bin++)
821834
if (fXbins.fArray[bin] <= fXbins.fArray[bin - 1])
822-
Error("TAxis::Set", "bins must be in increasing order");
835+
Error("TAxis::Set", "bin edges must be in increasing order");
823836
fXmin = fXbins.fArray[0];
824837
fXmax = fXbins.fArray[fNbins];
825838
if (!fParent) SetDefaults();

hist/hist/src/TH2Poly.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ times, it is better to divide into a small number of cells.
146146

147147
TH2Poly::TH2Poly()
148148
{
149-
Initialize(0., 0., 0., 0., 25, 25);
149+
Initialize(0., 0., 0., 0., 25, 25); // automatic axis range calculation
150150
SetName("NoName");
151151
SetTitle("NoTitle");
152152
SetFloat();

hist/hist/test/test_TH1.cxx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,9 @@ TEST(TH1, SetBufferedSumw2)
318318
// https://github.com/root-project/root/issues/20185
319319
TEST(TAxis, EqualBinEdges)
320320
{
321-
ROOT_EXPECT_ERROR(TAxis _({1, 1}), "TAxis::Set", "bins must be in increasing order");
321+
ROOT_EXPECT_ERROR(TAxis _({1, 1}), "TAxis::Set", "bin edges must be in increasing order");
322+
ROOT_EXPECT_ERROR(TAxis _(1, -std::numeric_limits<double>::infinity(), 0), "TAxis::Set", "fixed binwidth not compatible with infinite lower/upper edges");
323+
ROOT_EXPECT_ERROR(TAxis _(1, 0., std::numeric_limits<double>::infinity()), "TAxis::Set", "fixed binwidth not compatible with infinite lower/upper edges");
324+
ROOT_EXPECT_ERROR(TAxis _(1, std::numeric_limits<double>::quiet_NaN(), 0), "TAxis::Set", "lower/upper edges should not be NaN");
325+
ROOT_EXPECT_ERROR(TAxis _(1, 0, std::numeric_limits<double>::quiet_NaN()), "TAxis::Set", "lower/upper edges should not be NaN");
322326
}

test/stressHistogram.cxx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6269,26 +6269,26 @@ bool testMerge1DWithBuffer(bool allNoLimits)
62696269
// where different axis are used, BUT the largest bin width must be
62706270
// a multiple of the smallest bin width
62716271

6272-
double x1 = 1; double x2 = 0;
6272+
double x1 = 1; double x2 = 0; // i.e. automatic axis range calculation
62736273
if (!allNoLimits) {
62746274
// case when one of the histogram has limits (mix mode)
62756275
x1 = minRange; x2 = maxRange;
62766276
}
62776277

62786278
TH1D* h0 = new TH1D("h0", "h0-Title", numberOfBins, 1, 0);
62796279
TH1D* h1 = new TH1D("h1", "h1-Title", numberOfBins, x1, x2);
6280-
TH1D* h2 = new TH1D("h2", "h2-Title", 1,1,0);
6281-
TH1D* h3 = new TH1D("h3", "h3-Title", 1,1,0);
6282-
TH1D* h4 = new TH1D("h4", "h4-Title", numberOfBins, x1,x2);
6280+
TH1D* h2 = new TH1D("h2", "h2-Title", 1, 1, 0);
6281+
TH1D* h3 = new TH1D("h3", "h3-Title", 1, 1, 0);
6282+
TH1D* h4 = new TH1D("h4", "h4-Title", numberOfBins, x1, x2);
62836283

62846284
h0->Sumw2(); h1->Sumw2();h2->Sumw2();h4->Sumw2();
62856285
// The below histograms will be merged into h0, so they all need to fit into the buffer.
62866286
// Otherwise, the axis ranges will be computed already during the partial merge.
62876287
h0->SetBuffer(nEvents * 10);
6288-
h1->SetBuffer(nEvents*10);
6289-
h2->SetBuffer(nEvents*10);
6290-
h3->SetBuffer(nEvents*10);
6291-
h4->SetBuffer(nEvents*10);
6288+
h1->SetBuffer(nEvents * 10);
6289+
h2->SetBuffer(nEvents * 10);
6290+
h3->SetBuffer(nEvents * 10);
6291+
h4->SetBuffer(nEvents * 10);
62926292

62936293

62946294
for ( Int_t e = 0; e < nEvents; ++e ) {

0 commit comments

Comments
 (0)