Newer
Older
#ifndef WORKSPACETEST_H_
#define WORKSPACETEST_H_
#include "MantidAPI/ISpectrum.h"
Roman Tolchenov
committed
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidAPI/SpectrumDetectorMapping.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidGeometry/Instrument/ComponentHelper.h"
Russell Taylor
committed
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidKernel/make_cow.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/VMD.h"
#include "MantidTypes/SpectrumDefinition.h"
#include "MantidIndexing/IndexInfo.h"
#include "MantidTestHelpers/FakeObjects.h"
#include "MantidTestHelpers/InstrumentCreationHelper.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/NexusTestHelper.h"
#include <cxxtest/TestSuite.h>
#include <boost/make_shared.hpp>
#include <atomic>
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using Mantid::Indexing::IndexInfo;
Janik Zikovsky
committed
// Declare into the factory.
Janik Zikovsky
committed
/** Create a workspace with numSpectra, with
* each spectrum having one detector, at id = workspace index.
* @param numSpectra
* @return
*/
boost::shared_ptr<MatrixWorkspace> makeWorkspaceWithDetectors(size_t numSpectra,
size_t numBins) {
boost::shared_ptr<MatrixWorkspace> ws2 =
boost::make_shared<WorkspaceTester>();
ws2->initialize(numSpectra, numBins, numBins);
Gigg, Martyn Anthony
committed
auto inst = boost::make_shared<Instrument>("TestInstrument");
// We get a 1:1 map by default so the detector ID should match the spectrum
// number
for (size_t i = 0; i < ws2->getNumberHistograms(); ++i) {
Janik Zikovsky
committed
// Create a detector for each spectra
Detector *det = new Detector("pixel", static_cast<detid_t>(i), inst.get());
Janik Zikovsky
committed
inst->add(det);
inst->markAsDetector(det);
ws2->getSpectrum(i).addDetectorID(static_cast<detid_t>(i));
Gigg, Martyn Anthony
committed
}
ws2->setInstrument(inst);
Janik Zikovsky
committed
return ws2;
Gigg, Martyn Anthony
committed
}
Janik Zikovsky
committed
class MatrixWorkspaceTest : public CxxTest::TestSuite {
Russell Taylor
committed
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static MatrixWorkspaceTest *createSuite() {
return new MatrixWorkspaceTest();
Russell Taylor
committed
}
static void destroySuite(MatrixWorkspaceTest *suite) { delete suite; }
MatrixWorkspaceTest() : ws(boost::make_shared<WorkspaceTester>()) {
ws->initialize(1, 1, 1);
}
void test_set_bad_IndexInfo() {
WorkspaceTester ws;
ws.initialize(3, 1, 1);
IndexInfo bad(2);
TS_ASSERT_THROWS(ws.setIndexInfo(std::move(bad)), std::runtime_error);
}
void test_IndexInfo() {
WorkspaceTester ws;
ws.initialize(3, 1, 1);
const auto &indexInfo = ws.indexInfo();
IndexInfo indices(3);
indices.setSpectrumNumbers({2, 4, 6});
indices.setDetectorIDs({{0}, {1}, {2, 3}});
TS_ASSERT_THROWS_NOTHING(ws.setIndexInfo(std::move(indices)));
TS_ASSERT_EQUALS(indexInfo.spectrumNumber(0), 2);
TS_ASSERT_EQUALS(indexInfo.spectrumNumber(1), 4);
TS_ASSERT_EQUALS(indexInfo.spectrumNumber(2), 6);
TS_ASSERT_EQUALS(indexInfo.detectorIDs(0), (std::vector<detid_t>{0}));
TS_ASSERT_EQUALS(indexInfo.detectorIDs(1), (std::vector<detid_t>{1}));
TS_ASSERT_EQUALS(indexInfo.detectorIDs(2), (std::vector<detid_t>{2, 3}));
}
void test_setIndexInfo() {
// NOTE: This test checks if the IndexInfo set via
// MatrixWorkspace::setIndexInfo() affects data stored in ISpectrum and
// obtained via the legacy interface. THIS TEST SHOULD BE REMOVED ONCE THAT
// INTERFACE IS BEING REMOVED.
WorkspaceTester ws;
ws.initialize(3, 1, 1);
IndexInfo indices(3);
indices.setSpectrumNumbers({2, 4, 6});
indices.setDetectorIDs({{0}, {1}, {2, 3}});
TS_ASSERT_THROWS_NOTHING(ws.setIndexInfo(std::move(indices)));
TS_ASSERT_EQUALS(ws.getSpectrum(0).getSpectrumNo(), 2);
TS_ASSERT_EQUALS(ws.getSpectrum(1).getSpectrumNo(), 4);
TS_ASSERT_EQUALS(ws.getSpectrum(2).getSpectrumNo(), 6);
TS_ASSERT_EQUALS(ws.getSpectrum(0).getDetectorIDs(),
(std::set<detid_t>{0}));
TS_ASSERT_EQUALS(ws.getSpectrum(1).getDetectorIDs(),
(std::set<detid_t>{1}));
TS_ASSERT_EQUALS(ws.getSpectrum(2).getDetectorIDs(),
(std::set<detid_t>{2, 3}));
}
void test_indexInfo_legacy_compatibility() {
// NOTE: This test checks if the IndexInfo reference returned by
// MatrixWorkspace::indexInfo() reflects changes done via the legacy
// interface of ISpectrum. THIS TEST SHOULD BE REMOVED ONCE THAT INTERFACE
// IS BEING REMOVED.
WorkspaceTester ws;
ws.initialize(1, 1, 1);
const auto &indexInfo = ws.indexInfo();
TS_ASSERT_EQUALS(indexInfo.spectrumNumber(0), 1);
TS_ASSERT_EQUALS(indexInfo.detectorIDs(0), std::vector<detid_t>({0}));
ws.getSpectrum(0).setSpectrumNo(7);
ws.getSpectrum(0).addDetectorID(7);
// No changes -- old and new interface should not be mixed!
TS_ASSERT_EQUALS(indexInfo.spectrumNumber(0), 1);
TS_ASSERT_EQUALS(indexInfo.detectorIDs(0), std::vector<detid_t>({0}));
// After getting a new reference we should see the changes.
const auto &indexInfo2 = ws.indexInfo();
TS_ASSERT_EQUALS(indexInfo2.spectrumNumber(0), 7);
TS_ASSERT_EQUALS(indexInfo2.detectorIDs(0), std::vector<detid_t>({0, 7}));
void test_IndexInfo_copy() {
WorkspaceTester ws;
ws.initialize(3, 1, 1);
IndexInfo indices(3);
indices.setSpectrumNumbers({2, 4, 6});
indices.setDetectorIDs({{0}, {1}, {2, 3}});
ws.setIndexInfo(std::move(indices));
// Internally this references data in ISpectrum
const auto &indexInfo = ws.indexInfo();
// This should create a copy, dropping any links to MatrixWorkspace or
// ISpectrum
const auto copy(indexInfo);
TS_ASSERT_EQUALS(copy.spectrumNumber(0), 2);
TS_ASSERT_EQUALS(copy.spectrumNumber(1), 4);
TS_ASSERT_EQUALS(copy.spectrumNumber(2), 6);
TS_ASSERT_EQUALS(copy.detectorIDs(0), (std::vector<detid_t>{0}));
TS_ASSERT_EQUALS(copy.detectorIDs(1), (std::vector<detid_t>{1}));
TS_ASSERT_EQUALS(copy.detectorIDs(2), (std::vector<detid_t>{2, 3}));
// Changing data in workspace affects indexInfo, but not copy
ws.getSpectrum(0).setSpectrumNo(7);
ws.getSpectrum(0).addDetectorID(7);
const auto &indexInfo2 = ws.indexInfo();
TS_ASSERT_EQUALS(indexInfo2.spectrumNumber(0), 7);
TS_ASSERT_EQUALS(indexInfo2.detectorIDs(0), (std::vector<detid_t>{0, 7}));
TS_ASSERT_EQUALS(copy.spectrumNumber(0), 2);
TS_ASSERT_EQUALS(copy.detectorIDs(0), (std::vector<detid_t>{0}));
}
void test_setIndexInfo_shares_spectrumDefinition() {
WorkspaceTester ws;
ws.initialize(3, 1, 1);
IndexInfo indices(3);
indices.setSpectrumNumbers({2, 4, 6});
indices.setDetectorIDs({{0}, {1}, {2, 3}});
auto defs = Kernel::make_cow<std::vector<SpectrumDefinition>>(3);
TS_ASSERT_THROWS_NOTHING(indices.setSpectrumDefinitions(defs));
TS_ASSERT_THROWS_NOTHING(ws.setIndexInfo(std::move(indices)));
TS_ASSERT_EQUALS(ws.indexInfo().spectrumDefinitions().get(), defs.get());
}
void test_toString_Produces_Expected_Contents() {
auto testWS = boost::make_shared<WorkspaceTester>();
testWS->setTitle("A test run");
testWS->getAxis(0)->setUnit("TOF");
testWS->setYUnitLabel("Counts");
Loading
Loading full blame...