Skip to content

Commit

Permalink
[hist] Apply suggestions by Jonas R.
Browse files Browse the repository at this point in the history
Implement suggestions by Jonas R. to make FindFixBin more robust to numerical errors
  • Loading branch information
lmoneta committed Jan 7, 2025
1 parent 07396f9 commit 3959abc
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions hist/hist/src/TAxis.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -404,20 +404,21 @@ Int_t TAxis::DoFindFixBin(Double_t x) const
{
int bin = 0;

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / alma9 modules_off runtime_cxxmodules=Off

unused variable ‘bin’ [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / alma9 march_native CMAKE_BUILD_TYPE=RelWithDebInfo, CMAKE_CXX_FLAGS=-march=native, CMAKE_C_FLAGS=-march=native, fortran=OFF

unused variable ‘bin’ [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / alma8 LLVM_ENABLE_ASSERTIONS=On

unused variable ‘bin’ [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / mac13 ARM64 LLVM_ENABLE_ASSERTIONS=On, builtin_zlib=ON

unused variable 'bin' [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / mac15 ARM64 LLVM_ENABLE_ASSERTIONS=On, CMAKE_CXX_STANDARD=20

unused variable 'bin' [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / alma9-clang clang LLVM_ENABLE_ASSERTIONS=On, CMAKE_C_COMPILER=clang, CMAKE_CXX_COMPILER=clang++

unused variable 'bin' [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / alma9 LLVM_ENABLE_ASSERTIONS=On, CMAKE_BUILD_TYPE=Debug

unused variable ‘bin’ [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / ubuntu22 imt=Off, LLVM_ENABLE_ASSERTIONS=On, CMAKE_BUILD_TYPE=Debug

unused variable ‘bin’ [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / ubuntu2410 LLVM_ENABLE_ASSERTIONS=On, CMAKE_BUILD_TYPE=Debug

unused variable ‘bin’ [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / ubuntu2404 LLVM_ENABLE_ASSERTIONS=On, CMAKE_BUILD_TYPE=Debug

unused variable ‘bin’ [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / mac-beta ARM64 LLVM_ENABLE_ASSERTIONS=On, CMAKE_CXX_STANDARD=20

unused variable 'bin' [-Wunused-variable]

Check warning on line 405 in hist/hist/src/TAxis.cxx

View workflow job for this annotation

GitHub Actions / mac14 X64 LLVM_ENABLE_ASSERTIONS=On, CMAKE_CXX_STANDARD=20

unused variable 'bin' [-Wunused-variable]
if (!fXbins.fN) { //*-* fix bins
bin = 1 + int (fNbins * (x-fXmin)/(fXmax-fXmin) );
// apply correction when x is at the bin edge value
// (computed as in GetBinLowEdge) a numerical error in the
// above division can cause a migration in a neighbour bin.
double binwidth = (fXmax - fXmin) / Double_t(fNbins);
double upEdge = fXmin + (bin) * binwidth;
double lowEdge = fXmin + (bin - 1) * binwidth;
if (upEdge <= x) bin += 1;
if (lowEdge > x) bin -= 1;
} else { //*-* variable bin sizes
//for (bin =1; x >= fXbins.fArray[bin]; bin++);
bin = 1 + TMath::BinarySearch(fXbins.fN,fXbins.fArray,x);
// shift x and fXmax by fXmin:
x = x - fXmin;
const double b = fXmax - fXmin;
const double s = fNbins * x; // scaled version of x
double m = std::floor(s / b);
// iterative correction in case of floating point errors due to floating point division
while (m * b >= s)
m = m - 1;
while ((m + 1) * b < s)
m = m + 1;
return 1 + Int_t(m);
} else { //*-* variable bin sizes
// for (bin =1; x >= fXbins.fArray[bin]; bin++);
return 1 + TMath::BinarySearch(fXbins.fN, fXbins.fArray, x);
}
return bin;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 3959abc

Please sign in to comment.