Unverified Commit 20a6481f authored by Gigg, Martyn Anthony's avatar Gigg, Martyn Anthony Committed by GitHub
Browse files

Merge pull request #28231 from mantidproject/28179_Rebin2D_index_out_of_range

Stop for loop in Rebin2D asking for next element after the last bin
parents 652d30c3 8e904715
...@@ -84,7 +84,7 @@ void Rebin2D::exec() { ...@@ -84,7 +84,7 @@ void Rebin2D::exec() {
"If it is a spectra axis try running ConvertSpectrumAxis first."); "If it is a spectra axis try running ConvertSpectrumAxis first.");
} }
const auto &oldXEdges = inputWS->x(0); const auto &oldXEdges = inputWS->binEdges(0);
const size_t numXBins = inputWS->blocksize(); const size_t numXBins = inputWS->blocksize();
const size_t numYBins = inputWS->getNumberHistograms(); const size_t numYBins = inputWS->getNumberHistograms();
// This will convert plain NumericAxis to bin edges while // This will convert plain NumericAxis to bin edges while
......
...@@ -9,10 +9,11 @@ ...@@ -9,10 +9,11 @@
#include "MantidAPI/BinEdgeAxis.h" #include "MantidAPI/BinEdgeAxis.h"
#include "MantidAlgorithms/Rebin2D.h" #include "MantidAlgorithms/Rebin2D.h"
#include "MantidDataObjects/RebinnedOutput.h" #include "MantidDataObjects/RebinnedOutput.h"
#include "MantidHistogramData/Histogram.h"
#include "MantidKernel/Timer.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h" #include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include <cxxtest/TestSuite.h> #include <cxxtest/TestSuite.h>
//#include "../../TestHelpers/src/WorkspaceCreationHelper.cpp"
#include "MantidKernel/Timer.h"
using Mantid::Algorithms::Rebin2D; using Mantid::Algorithms::Rebin2D;
using namespace Mantid::API; using namespace Mantid::API;
...@@ -26,7 +27,8 @@ namespace { ...@@ -26,7 +27,8 @@ namespace {
/// Return the input workspace. All Y values are 2 and E values sqrt(2) /// Return the input workspace. All Y values are 2 and E values sqrt(2)
MatrixWorkspace_sptr makeInputWS(const bool distribution, MatrixWorkspace_sptr makeInputWS(const bool distribution,
const bool perf_test = false, const bool perf_test = false,
const bool small_bins = false) { const bool small_bins = false,
const bool isHisto = true) {
size_t nhist(0), nbins(0); size_t nhist(0), nbins(0);
double x0(0.0), deltax(0.0); double x0(0.0), deltax(0.0);
...@@ -46,20 +48,31 @@ MatrixWorkspace_sptr makeInputWS(const bool distribution, ...@@ -46,20 +48,31 @@ MatrixWorkspace_sptr makeInputWS(const bool distribution,
} }
} }
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::create2DWorkspaceBinned( MatrixWorkspace_sptr ws;
int(nhist), int(nbins), x0, deltax);
// We need something other than a spectrum axis, call this one theta
auto thetaAxis = std::make_unique<BinEdgeAxis>(nhist + 1);
for (size_t i = 0; i < nhist + 1; ++i) {
thetaAxis->setValue(i, -0.5 + static_cast<double>(i));
}
ws->replaceAxis(1, std::move(thetaAxis));
if (distribution) { if (isHisto) {
Mantid::API::WorkspaceHelpers::makeDistribution(ws); ws = WorkspaceCreationHelper::create2DWorkspaceBinned(
int(nhist), int(nbins), x0, deltax);
// We need something other than a spectrum axis, call this one theta
auto thetaAxis = std::make_unique<BinEdgeAxis>(nhist + 1);
for (size_t i = 0; i < nhist + 1; ++i) {
thetaAxis->setValue(i, -0.5 + static_cast<double>(i));
}
ws->replaceAxis(1, std::move(thetaAxis));
if (distribution) {
Mantid::API::WorkspaceHelpers::makeDistribution(ws);
}
} else {
// create histograms with points (which cannot be a distirbution)
ws = WorkspaceCreationHelper::create2DWorkspacePoints(
int(nhist), int(nbins), x0 + 0.5, deltax);
// convert axis from spectrum to theta
auto thetaAxis = std::make_unique<NumericAxis>(nhist);
for (size_t i = 0; i < nhist; ++i) {
thetaAxis->setValue(i, static_cast<double>(i));
}
ws->replaceAxis(1, std::move(thetaAxis));
} }
return ws; return ws;
} }
...@@ -185,6 +198,32 @@ public: ...@@ -185,6 +198,32 @@ public:
} }
} }
void test_BothAxes_PointData() {
MatrixWorkspace_sptr inputWS =
makeInputWS(false, false, false, false); // 10 spectra, 10 points
MatrixWorkspace_sptr outputWS =
runAlgorithm(inputWS, "5.,1.8,15", "-0.5,2.5,9.5");
TS_ASSERT_EQUALS(outputWS->getNumberHistograms(), 4);
TS_ASSERT_EQUALS(outputWS->blocksize(), 6);
double errors[6] = {3., 3., 3., 3., 3., 2.236067977};
const double epsilon(1e-08);
for (size_t i = 0; i < outputWS->getNumberHistograms(); ++i) {
const auto &y = outputWS->y(i);
const auto &e = outputWS->e(i);
const size_t numBins = y.size();
for (size_t j = 0; j < numBins; ++j) {
if (j < 5) {
TS_ASSERT_DELTA(y[j], 9, epsilon);
} else {
// Last bin
TS_ASSERT_DELTA(y[j], 5, epsilon);
}
TS_ASSERT_DELTA(e[j], errors[j], epsilon);
}
}
}
void test_Zero_Area_Bins_NoFractionalBinning() { void test_Zero_Area_Bins_NoFractionalBinning() {
MatrixWorkspace_sptr inputWS = makeInputWS(false); MatrixWorkspace_sptr inputWS = makeInputWS(false);
const auto nhist = inputWS->getNumberHistograms(); const auto nhist = inputWS->getNumberHistograms();
......
...@@ -608,7 +608,7 @@ void rebinToFractionalOutput(const Quadrilateral &inputQ, ...@@ -608,7 +608,7 @@ void rebinToFractionalOutput(const Quadrilateral &inputQ,
RebinnedOutput &outputWS, RebinnedOutput &outputWS,
const std::vector<double> &verticalAxis, const std::vector<double> &verticalAxis,
const RebinnedOutput_const_sptr &inputRB) { const RebinnedOutput_const_sptr &inputRB) {
const auto &inX = inputWS->x(i); const auto &inX = inputWS->binEdges(i);
const auto &inY = inputWS->y(i); const auto &inY = inputWS->y(i);
const auto &inE = inputWS->e(i); const auto &inE = inputWS->e(i);
double signal = inY[j]; double signal = inY[j];
......
...@@ -170,6 +170,13 @@ Mantid::DataObjects::Workspace2D_sptr ...@@ -170,6 +170,13 @@ Mantid::DataObjects::Workspace2D_sptr
create2DWorkspaceBinned(size_t nhist, size_t numVals, double x0 = 0.0, create2DWorkspaceBinned(size_t nhist, size_t numVals, double x0 = 0.0,
double deltax = 1.0); double deltax = 1.0);
/** Create a 2D workspace with this many point-histograms and bins.
* Filled with Y = 2.0 and E = M_SQRT2
*/
Mantid::DataObjects::Workspace2D_sptr
create2DWorkspacePoints(size_t nhist, size_t numVals, double x0 = 0.0,
double deltax = 1.0);
/** Create a 2D workspace with this many histograms and bins. The bins are /** Create a 2D workspace with this many histograms and bins. The bins are
* assumed to be non-uniform and given by the input array * assumed to be non-uniform and given by the input array
* Filled with Y = 2.0 and E = sqrt(2.0)w * Filled with Y = 2.0 and E = sqrt(2.0)w
......
...@@ -322,6 +322,17 @@ Workspace2D_sptr create2DWorkspaceBinned(size_t nhist, size_t numVals, ...@@ -322,6 +322,17 @@ Workspace2D_sptr create2DWorkspaceBinned(size_t nhist, size_t numVals,
return create<Workspace2D>(nhist, Histogram(x, y, e)); return create<Workspace2D>(nhist, Histogram(x, y, e));
} }
/** Create a 2D workspace with this many point-histograms and bins.
* Filled with Y = 2.0 and E = M_SQRT2w
*/
Workspace2D_sptr create2DWorkspacePoints(size_t nhist, size_t numVals,
double x0, double deltax) {
Points x(numVals, LinearGenerator(x0, deltax));
Counts y(numVals, 2);
CountStandardDeviations e(numVals, M_SQRT2);
return create<Workspace2D>(nhist, Histogram(x, y, e));
}
/** Create a 2D workspace with this many histograms and bins. The bins are /** Create a 2D workspace with this many histograms and bins. The bins are
* assumed to be non-uniform and given by the input array * assumed to be non-uniform and given by the input array
* Filled with Y = 2.0 and E = M_SQRT2w * Filled with Y = 2.0 and E = M_SQRT2w
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment