Commit f2889ad4 authored by Stephen's avatar Stephen
Browse files

Add additional unit tests for LoadMuonNexusV2

parent 80c627fe
......@@ -19,6 +19,8 @@ class DLLExport LoadMuonStrategy {
public:
// Constructor
LoadMuonStrategy(Kernel::Logger &g_log, const std::string &filename);
// Virtual destructor
virtual ~LoadMuonStrategy() {}
// Load muon log data
virtual void loadMuonLogData() = 0;
// Returns the good frames from the nexus entry
......
......@@ -113,7 +113,7 @@ void LoadMuonNexusV2::init() {
"detector grouping stored in the file (if any).");
}
void LoadMuonNexusV2::execLoader() {
this->setRethrows(true);
// prepare nexus entry
m_entrynumber = getProperty("EntryNumber");
m_filename = getPropertyValue("Filename");
......
......@@ -181,6 +181,5 @@ std::vector<detid_t> SinglePeriodLoadMuonStrategy::getLoadedDetectors() {
}
return loadedDetectors;
}
} // namespace DataHandling
} // namespace Mantid
......@@ -19,6 +19,7 @@
#include <cxxtest/TestSuite.h>
#include <cmath>
#include <vector>
using namespace Mantid::API;
using namespace Mantid::Kernel;
......@@ -28,14 +29,22 @@ using namespace Mantid::DataObjects;
class LoadMuonNexusV2Test : public CxxTest::TestSuite {
private:
// helper methods
std::string createSpectraList(const std::vector<int> &spectraList) {
std::ostringstream oss;
std::copy(std::begin(spectraList), std::end(spectraList),
std::ostream_iterator<int>(oss, ","));
std::string spectraStringList(oss.str());
return spectraStringList;
}
public:
void tearDown() override { AnalysisDataService::Instance().clear(); }
void testExec() {
LoadMuonNexusV2 ld;
ld.initialize();
ld.setPropertyValue("Filename", "EMU00103638.nxs_v2");
ld.setPropertyValue("OutputWorkspace", "outWS");
ld.setPropertyValue("DeadTimeTable", "DeadTimeTable");
ld.setPropertyValue("DetectorGroupingTable", "DetectorGroupingTable");
TS_ASSERT_THROWS_NOTHING(ld.execute());
TS_ASSERT(ld.isExecuted());
......@@ -56,6 +65,13 @@ public:
double timeZero = ld.getProperty("TimeZero");
TS_ASSERT_DELTA(timeZero, 0.1599999, 1e-5);
// Check that timeZero has been applied to the output spectra
// as LoadISISNexus does not do this.
// First time reading should be shifted by time zero.
TS_ASSERT_DELTA(output2D->x(3)[0], -0.1599999, 1e-5);
TS_ASSERT_DELTA(output2D->x(67)[0], -0.1599999, 1e-5);
TS_ASSERT_DELTA(output2D->x(81)[0], -0.1599999, 1e-5);
// Check the unit has been set correctly
TS_ASSERT_EQUALS(output2D->getAxis(0)->unit()->unitID(), "Label");
TS_ASSERT(!output2D->isDistribution());
......@@ -67,7 +83,185 @@ public:
TS_ASSERT_EQUALS(0.0, field);
}
void testExecWithGroupingTable() {}
void testExecWithDeadtimeTable() {
LoadMuonNexusV2 ld;
ld.initialize();
ld.setPropertyValue("Filename", "EMU00103638.nxs_v2");
ld.setPropertyValue("OutputWorkspace", "outWS");
const std::string deadTimeWSName = "LoadMuonNexusV2Test_DeadTimes";
ld.setPropertyValue("DeadTimeTable", deadTimeWSName);
TS_ASSERT_THROWS_NOTHING(ld.execute());
TS_ASSERT(ld.isExecuted());
// Verify that the output workspace exists
MatrixWorkspace_sptr output_ws;
TS_ASSERT_THROWS_NOTHING(
output_ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"outWS"));
Workspace2D_sptr output2D =
std::dynamic_pointer_cast<Workspace2D>(output_ws);
// Check detector grouping table
TableWorkspace_sptr deadTimeTable;
TS_ASSERT_THROWS_NOTHING(
deadTimeTable =
AnalysisDataService::Instance().retrieveWS<TableWorkspace>(
deadTimeWSName));
TS_ASSERT(deadTimeTable);
// Check number of rows and columns
TS_ASSERT_EQUALS(deadTimeTable->columnCount(), 2);
TS_ASSERT_EQUALS(deadTimeTable->rowCount(),
output2D->getNumberHistograms());
// Check Deadtimes
TS_ASSERT_DELTA(deadTimeTable->Double(0, 1), -0.0095861498, 1e-6);
TS_ASSERT_DELTA(deadTimeTable->Double(20, 1), 0.0067306999, 1e-6);
TS_ASSERT_DELTA(deadTimeTable->Double(62, 1), 0.0073113599, 1e-6);
}
void testExecWithGroupingTable() {
LoadMuonNexusV2 ld;
ld.initialize();
ld.setPropertyValue("Filename", "EMU00103638.nxs_v2");
ld.setPropertyValue("OutputWorkspace", "outWS");
const std::string groupingWSName = "LoadMuonNexusV2Test_Grouping";
ld.setPropertyValue("DetectorGroupingTable", groupingWSName);
TS_ASSERT_THROWS_NOTHING(ld.execute());
TS_ASSERT(ld.isExecuted());
// Verify that the output workspace exists
MatrixWorkspace_sptr output_ws;
TS_ASSERT_THROWS_NOTHING(
output_ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"outWS"));
Workspace2D_sptr output2D =
std::dynamic_pointer_cast<Workspace2D>(output_ws);
// Check detector grouping table
TableWorkspace_sptr groupingTable;
TS_ASSERT_THROWS_NOTHING(
groupingTable =
AnalysisDataService::Instance().retrieveWS<TableWorkspace>(
groupingWSName));
TS_ASSERT(groupingTable);
// Check number of rows and columns
TS_ASSERT_EQUALS(groupingTable->columnCount(), 1);
TS_ASSERT_EQUALS(groupingTable->rowCount(), 2);
// Check grouping
std::vector<int> testGroupingVec;
// Half the detectors are in the first group
for (int i = 1; i < 49; ++i)
testGroupingVec.emplace_back(i);
TS_ASSERT_EQUALS(groupingTable->cell<std::vector<int>>(0, 0),
testGroupingVec);
testGroupingVec.clear();
for (int i = 49; i < output2D->getNumberHistograms() + 1; ++i)
testGroupingVec.emplace_back(i);
TS_ASSERT_EQUALS(groupingTable->cell<std::vector<int>>(1, 0),
testGroupingVec);
}
void testExecWithSpectraList() {
std::vector<int> spectraIntegerList = {1, 21, 63};
auto spectraList = createSpectraList(spectraIntegerList);
LoadMuonNexusV2 ld;
ld.initialize();
ld.setPropertyValue("Filename", "EMU00103638.nxs_v2");
ld.setPropertyValue("OutputWorkspace", "outWS");
ld.setPropertyValue("SpectrumList", spectraList);
const std::string deadTimeWSName = "LoadMuonNexusV2Test_DeadTimes";
ld.setPropertyValue("DeadTimeTable", deadTimeWSName);
const std::string groupingWSName = "LoadMuonNexusV2Test_Grouping";
ld.setPropertyValue("DetectorGroupingTable", groupingWSName);
TS_ASSERT_THROWS_NOTHING(ld.execute());
TS_ASSERT(ld.isExecuted());
// Verify that the output workspace exists
MatrixWorkspace_sptr output_ws;
TS_ASSERT_THROWS_NOTHING(
output_ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"outWS"));
Workspace2D_sptr output2D =
std::dynamic_pointer_cast<Workspace2D>(output_ws);
// Test correct spectra loaded
TS_ASSERT_EQUALS(output2D->getNumberHistograms(), 3);
// Check that spectra maps to correct detector
for (size_t i = 0; i < 3; ++i) {
auto detectorgroup = output2D->getSpectrum(i).getDetectorIDs();
TS_ASSERT_EQUALS(detectorgroup.size(), 1);
TS_ASSERT_EQUALS(*detectorgroup.begin(), spectraIntegerList[i]);
}
// Check detector grouping table
TableWorkspace_sptr deadTimeTable;
TS_ASSERT_THROWS_NOTHING(
deadTimeTable =
AnalysisDataService::Instance().retrieveWS<TableWorkspace>(
deadTimeWSName));
TS_ASSERT(deadTimeTable);
// Check number of rows and columns
TS_ASSERT_EQUALS(deadTimeTable->columnCount(), 2);
TS_ASSERT_EQUALS(deadTimeTable->rowCount(), 3);
// Check Deadtimes
TS_ASSERT_DELTA(deadTimeTable->Double(0, 1), -0.0095861498, 1e-6);
TS_ASSERT_DELTA(deadTimeTable->Double(1, 1), 0.0067306999, 1e-6);
TS_ASSERT_DELTA(deadTimeTable->Double(2, 1), 0.0073113599, 1e-6);
}
void testExecWithSpectraMax() {
int specMax = 24;
LoadMuonNexusV2 ld;
ld.initialize();
ld.setPropertyValue("Filename", "EMU00103638.nxs_v2");
ld.setPropertyValue("OutputWorkspace", "outWS");
ld.setPropertyValue("SpectrumMax", "24");
const std::string deadTimeWSName = "LoadMuonNexusV2Test_DeadTimes";
ld.setPropertyValue("DeadTimeTable", deadTimeWSName);
const std::string groupingWSName = "LoadMuonNexusV2Test_Grouping";
ld.setPropertyValue("DetectorGroupingTable", groupingWSName);
TS_ASSERT_THROWS_NOTHING(ld.execute());
TS_ASSERT(ld.isExecuted());
// Verify that the output workspace exists
MatrixWorkspace_sptr output_ws;
TS_ASSERT_THROWS_NOTHING(
output_ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"outWS"));
Workspace2D_sptr output2D =
std::dynamic_pointer_cast<Workspace2D>(output_ws);
// Test correct spectra loaded
TS_ASSERT_EQUALS(output2D->getNumberHistograms(), specMax);
// Check that spectra maps to correct detector
auto detectorgroup = output2D->getSpectrum(specMax - 1).getDetectorIDs();
TS_ASSERT_EQUALS(detectorgroup.size(), 1);
TS_ASSERT_EQUALS(*detectorgroup.begin(), specMax);
}
void testLoadThrowsIfEntryNumberOutOfRange() {
LoadMuonNexusV2 ld;
ld.initialize();
ld.setPropertyValue("Filename", "EMU00103638.nxs_v2");
ld.setPropertyValue("OutputWorkspace", "outWS");
ld.setPropertyValue("EntryNumber", "10");
TS_ASSERT_THROWS(ld.execute(), const std::invalid_argument &)
}
void testLoadThrowsIfInvalidSpectraProperties() {
std::vector<int> spectraIntegerList = {1, 123, 157};
auto spectraList = createSpectraList(spectraIntegerList);
LoadMuonNexusV2 ld;
ld.initialize();
ld.setPropertyValue("Filename", "EMU00103638.nxs_v2");
ld.setPropertyValue("OutputWorkspace", "outWS");
ld.setPropertyValue("SpectrumList", spectraList);
TS_ASSERT_THROWS(ld.execute(), const std::invalid_argument &)
}
};
//------------------------------------------------------------------------------
......
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