Skip to content

Commit

Permalink
Refs #11534 Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonPiccardoSelg committed Aug 5, 2015
1 parent 8a2f8e9 commit d957c91
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Code/Mantid/Framework/Algorithms/src/Q1D2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,10 @@ void Q1D2::exec() {
Mantid::MantidVec::const_iterator countsIterator = YOut.begin();
Mantid::MantidVec::iterator qResolutionIterator = qResolutionOut.begin();
for (;qResolutionIterator!= qResolutionOut.end(); ++countsIterator, ++qResolutionIterator) {
// Divide by the counts of the Qbin
*qResolutionIterator = (*qResolutionIterator)/(*countsIterator);
// Divide by the counts of the Qbin, if the counts are 0, the the qresolution will also be 0
if ((*countsIterator) > 0.0) {
*qResolutionIterator = (*qResolutionIterator)/(*countsIterator);
}
}
}

Expand Down
76 changes: 74 additions & 2 deletions Code/Mantid/Framework/Algorithms/test/Q1D2Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "MantidTestHelpers/WorkspaceCreationHelper.h"

#include <algorithm>

using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::DataHandling;
Expand All @@ -24,6 +26,8 @@ static double flat_cell061Es [] = {8.140295E-03, 8.260089E-03, 8.198814E-03, 8.3
/// defined below this creates some input data
void createInputWorkspaces(int start, int end, Mantid::API::MatrixWorkspace_sptr & input, Mantid::API::MatrixWorkspace_sptr & wave, Mantid::API::MatrixWorkspace_sptr & pixels);
void createInputWorkSpacesForMasking ( Mantid::API::MatrixWorkspace_sptr & input, Mantid::API::MatrixWorkspace_sptr & wave, Mantid::API::MatrixWorkspace_sptr & pixels);
void createQResolutionWorkspace(Mantid::API::MatrixWorkspace_sptr & qResolution, Mantid::API::MatrixWorkspace_sptr & alteredInput, Mantid::API::MatrixWorkspace_sptr & input, double value1, double value2);


class Q1D2Test : public CxxTest::TestSuite
{
Expand Down Expand Up @@ -341,7 +345,7 @@ class Q1D2Test : public CxxTest::TestSuite

Mantid::API::AnalysisDataService::Instance().remove(m_noGrav);
}

void testInvalidInput()
{
Mantid::Algorithms::Q1D2 Q1D;
Expand All @@ -362,8 +366,55 @@ class Q1D2Test : public CxxTest::TestSuite
Q1D.execute();

TS_ASSERT( ! Q1D.isExecuted() )

// Restore the old binning
xData[15] -= 0.001;
}


void testRunsWithQResolution() {
// Arrange
Mantid::API::MatrixWorkspace_sptr qResolution, alteredInput;
const double value1 = 1;
const double value2 = 2;
createQResolutionWorkspace(qResolution, alteredInput, m_inputWS, value1, value2);

Mantid::Algorithms::Q1D2 Q1D;
TS_ASSERT_THROWS_NOTHING( Q1D.initialize() );
TS_ASSERT( Q1D.isInitialized() )

const std::string outputWS("Q1D2Test_result");
Q1D.setProperty("DetBankWorkspace", alteredInput);
Q1D.setProperty("WavelengthAdj", m_wavNorm);
Q1D.setProperty("PixelAdj", m_pixel);
Q1D.setPropertyValue("OutputWorkspace", outputWS);
Q1D.setPropertyValue("OutputBinning", "0.1,-0.02,0.5");
Q1D.setProperty("QResolution", qResolution);
Q1D.setProperty("OutputParts", true);
TS_ASSERT_THROWS_NOTHING(Q1D.execute());

TS_ASSERT( Q1D.isExecuted() )

Mantid::API::MatrixWorkspace_sptr result;
TS_ASSERT_THROWS_NOTHING( result = boost::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>
(Mantid::API::AnalysisDataService::Instance().retrieve(outputWS)) )

Mantid::API::MatrixWorkspace_sptr sumOfCounts;
TS_ASSERT_THROWS_NOTHING( sumOfCounts = boost::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>
(Mantid::API::AnalysisDataService::Instance().retrieve(outputWS+"_sumOfCounts")) )

// That output will be SUM_i(Yin_i*QRES_in_i)/(SUM_i(Y_in_i)) for each q value
// In our test workspace we set QRes_in_1 to 1, this means that all DX values should be SUM_i(Y_in_i)/(SUM_i(Y_in_i))
// which is either 1 or 0. It can be 0 if no data falls into this bin
auto& dataDX = result->dataDx(0);
for (auto it = dataDX.begin(); it != dataDX.end(); ++it) {
TS_ASSERT( (*it==1.0) || (*it==0.0));
}

Mantid::API::AnalysisDataService::Instance().remove(outputWS);
Mantid::API::AnalysisDataService::Instance().remove(outputWS+"_sumOfCounts");
Mantid::API::AnalysisDataService::Instance().remove(outputWS+"_sumOfNormFactors");
}

///stop the constructor from being run every time algorithms test suite is initialised
static Q1D2Test *createSuite() { return new Q1D2Test(); }
static void destroySuite(Q1D2Test *suite) { delete suite; }
Expand Down Expand Up @@ -483,4 +534,25 @@ void createInputWorkSpacesForMasking ( Mantid::API::MatrixWorkspace_sptr & input
{
createInputWorkspaces ( 9001, 9030, input, wave, pixels );
}

void createQResolutionWorkspace(Mantid::API::MatrixWorkspace_sptr & qResolution, Mantid::API::MatrixWorkspace_sptr & alteredInput, Mantid::API::MatrixWorkspace_sptr & input, double value1, double value2) {
//The q resolution workspace is almost the same to the input workspace, except for the y value, we set all Y values to 1
qResolution = Mantid::API::MatrixWorkspace_sptr(input->clone().release());
alteredInput = Mantid::API::MatrixWorkspace_sptr(input->clone().release());

// Populate Y with Value1
for (int i = 0; i < qResolution->getNumberHistograms(); ++i) {
auto& data = qResolution->dataY(i);
std::fill(data.begin(), data.end(), value1);
}

// Populate Y with Value2
for (int i = 0; i < alteredInput->getNumberHistograms(); ++i) {
auto& data = alteredInput->dataY(i);
std::fill(data.begin(), data.end(), value2);
}


}

#endif /*Q1D2Test_H_*/

0 comments on commit d957c91

Please sign in to comment.