From f20ff02377bdc513528468dfbf5dad0021444da2 Mon Sep 17 00:00:00 2001 From: Owen Arnold Date: Fri, 19 Dec 2014 11:42:32 +0000 Subject: [PATCH] refs #10820. Axis labelling introduced and tested. I've put in a switch to recover the original behaviour. --- .../src/ConvertMDHistoToMatrixWorkspace.cpp | 43 +++++++++++++++---- .../ConvertMDHistoToMatrixWorkspaceTest.h | 17 ++++++-- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/Code/Mantid/Framework/Algorithms/src/ConvertMDHistoToMatrixWorkspace.cpp b/Code/Mantid/Framework/Algorithms/src/ConvertMDHistoToMatrixWorkspace.cpp index bb613f00b4de..f8dd3389748d 100644 --- a/Code/Mantid/Framework/Algorithms/src/ConvertMDHistoToMatrixWorkspace.cpp +++ b/Code/Mantid/Framework/Algorithms/src/ConvertMDHistoToMatrixWorkspace.cpp @@ -26,9 +26,24 @@ struct NullDeleter { } }; -size_t findXAxis(const VMD &start, const VMD &end, CoordTransform const *const transform, - IMDHistoWorkspace const *const inputWorkspace, - Logger &logger, const size_t id) { +/** +Find the dimension to use as the plot axis. + +@param start : start point in final frame +@param end : end point in final frame +@param transform : transform to original frame +@param inputWorkspace : inputWorkspace +@param logger : log object +@param id : id, or current index for the dimension to use as the x-plot +dimension + +@return id/index of the dimension with the longest span in the original +coordinate system. +*/ +size_t findXAxis(const VMD &start, const VMD &end, + CoordTransform const *const transform, + IMDHistoWorkspace const *const inputWorkspace, Logger &logger, + const size_t id) { // Find the start and end points in the original workspace VMD originalStart = transform->applyVMD(start); @@ -46,8 +61,8 @@ size_t findXAxis(const VMD &start, const VMD &end, CoordTransform const *const t return dimIndex; } - auto originalWS = - boost::dynamic_pointer_cast(inputWorkspace->getOriginalWorkspace(nOriginalWorkspaces - 1)); + auto originalWS = boost::dynamic_pointer_cast( + inputWorkspace->getOriginalWorkspace(nOriginalWorkspaces - 1)); for (size_t d = 0; d < diff.getNumDims(); d++) { if (fabs(diff[d]) > largest || @@ -87,6 +102,11 @@ void ConvertMDHistoToMatrixWorkspace::init() { Kernel::IValidator_sptr( new Kernel::ListValidator(normalizations)), "Signal normalization method"); + + declareProperty( + new PropertyWithValue("FindXAxis", true, Direction::Input), + "If True, tries to automatically determine the dimension to use as the " + "output x-axis. Applies to line cut MD workspaces."); } /// Execute the algorithm @@ -134,7 +154,8 @@ void ConvertMDHistoToMatrixWorkspace::make1DWorkspace() { // All the way through in the single dimension start[d] = dim->getMinimum(); end[d] = dim->getMaximum(); - id = d; // We take the first non integrated dimension to be the diemnsion of interest. + id = d; // We take the first non integrated dimension to be the diemnsion + // of interest. } else { // Mid point along each dimension start[d] = (dim->getMaximum() + dim->getMinimum()) / 2.0f; @@ -184,8 +205,12 @@ void ConvertMDHistoToMatrixWorkspace::make1DWorkspace() { assert(X.size() == outputWorkspace->dataX(0).size()); - // We look to the original workspace if possbible to find the dimension of interest to plot against. - id = findXAxis(start, end, transform.get(), inputWorkspace.get(), g_log, id); + const bool autoFind = this->getProperty("FindXAxis"); + if (autoFind) { + // We look to the original workspace if possbible to find the dimension of + // interest to plot against. + id = findXAxis(start, end, transform.get(), inputWorkspace.get(), g_log, id); + } for (size_t i = 0; i < X.size(); ++i) { // Coordinates in the workspace being plotted @@ -198,7 +223,7 @@ void ConvertMDHistoToMatrixWorkspace::make1DWorkspace() { boost::shared_ptr labelX = boost::dynamic_pointer_cast( Kernel::UnitFactory::Instance().create("Label")); - labelX->setLabel(alongDim); + labelX->setLabel(inputWorkspace->getDimension(id)->getName()); outputWorkspace->getAxis(0)->unit() = labelX; outputWorkspace->setYUnitLabel("Signal"); diff --git a/Code/Mantid/Framework/Algorithms/test/ConvertMDHistoToMatrixWorkspaceTest.h b/Code/Mantid/Framework/Algorithms/test/ConvertMDHistoToMatrixWorkspaceTest.h index 62e0e951d01a..7a3cfbb15cff 100644 --- a/Code/Mantid/Framework/Algorithms/test/ConvertMDHistoToMatrixWorkspaceTest.h +++ b/Code/Mantid/Framework/Algorithms/test/ConvertMDHistoToMatrixWorkspaceTest.h @@ -319,7 +319,7 @@ class ConvertMDHistoToMatrixWorkspaceTest : public CxxTest::TestSuite convert_alg.execute(); MatrixWorkspace_sptr out_ws = convert_alg.getProperty("OutputWorkspace"); - //TS_ASSERT_EQUALS("X", out_ws->getAxis(0)->unit()->label()); + TSM_ASSERT_EQUALS("Wrong dimension auto selected for output x-axis", "X", out_ws->getDimension(0)->getName()); TS_ASSERT_EQUALS(out_ws->getNumberHistograms(), 1); auto first_x_spectra = out_ws->readX(0); @@ -377,12 +377,21 @@ class ConvertMDHistoToMatrixWorkspaceTest : public CxxTest::TestSuite convert_alg.execute(); MatrixWorkspace_sptr out_ws = convert_alg.getProperty("OutputWorkspace"); - //TS_ASSERT_EQUALS("X", out_ws->getAxis(0)->unit()->label()); + TSM_ASSERT_EQUALS("Wrong dimension auto selected for output x-axis", "Y", out_ws->getDimension(0)->getName()); TS_ASSERT_EQUALS(out_ws->getNumberHistograms(), 1); auto first_x_spectra = out_ws->readX(0); - TSM_ASSERT_DELTA("First coordinate in the incorrect position. Incorrect transformation.", first_x_spectra.front(), -8, 1e-3); - TSM_ASSERT_DELTA( "Last coordinate in the incorrect position. Incorrect transformation.", first_x_spectra.back(), 8, 1e-3); + TSM_ASSERT_DELTA("Last coordinate in the incorrect position. Incorrect transformation.", first_x_spectra.back(), 8, 1e-3); + + // Run it again, this time with FindXAxis set off. + convert_alg.setProperty("FindXAxis", false); + convert_alg.execute(); + out_ws = convert_alg.getProperty("OutputWorkspace"); + TSM_ASSERT_EQUALS("FindXAxis if off", "X", out_ws->getDimension(0)->getName()); + TS_ASSERT_EQUALS(out_ws->getNumberHistograms(), 1); + first_x_spectra = out_ws->readX(0); + TSM_ASSERT_DELTA("First coordinate in the incorrect position. Incorrect transformation.", first_x_spectra.front(), -5, 1e-3); + TSM_ASSERT_DELTA("Last coordinate in the incorrect position. Incorrect transformation.", first_x_spectra.back(), 5, 1e-3); }