Newer
Older
#ifndef MANTID_MDEVENTS_CONVERTTODIFFRACTIONMDWORKSPACETEST2_H_
#define MANTID_MDEVENTS_CONVERTTODIFFRACTIONMDWORKSPACETEST2_H_
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/IAlgorithm.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidMDAlgorithms/ConvertToDiffractionMDWorkspace2.h"
Janik Zikovsky
committed
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/MDEventsTestHelper.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
Janik Zikovsky
committed
#include <cxxtest/TestSuite.h>
Janik Zikovsky
committed
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
Janik Zikovsky
committed
using namespace Mantid::DataObjects;
using namespace Mantid::MDAlgorithms;
class ConvertToDiffractionMDWorkspace2Test : public CxxTest::TestSuite {
ConvertToDiffractionMDWorkspace2 alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
Janik Zikovsky
committed
Janik Zikovsky
committed
/** Test various combinations of OutputDimensions parameter */
void test_OutputDimensions_Parameter() {
EventWorkspace_sptr in_ws = Mantid::DataObjects::MDEventsTestHelper::
createDiffractionEventWorkspace(10);
Janik Zikovsky
committed
AnalysisDataService::Instance().addOrReplace("testInEW", in_ws);
Janik Zikovsky
committed
IAlgorithm_sptr alg;
Janik Zikovsky
committed
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 6, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "OutputDimensions", "Q (lab frame)");
TS_ASSERT(alg->isExecuted());
Janik Zikovsky
committed
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"testOutMD"));
Janik Zikovsky
committed
TS_ASSERT(ws);
if (!ws)
return;
TS_ASSERT_EQUALS(ws->getDimension(0)->getName(), "Q_lab_x");
TS_ASSERT_EQUALS(ws->getSpecialCoordinateSystem(), Mantid::Kernel::QLab);
// TODO: Now you can add differenc dimension types to each other, but this
// should be fixed
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 8, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "Append", "1", "OutputDimensions",
"HKL");
TS_ASSERT(alg->isExecuted());
Janik Zikovsky
committed
// If Append is False, then it does work. The workspace gets replaced
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 8, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "Append", "0", "OutputDimensions",
"HKL");
TS_ASSERT(alg->isExecuted());
Janik Zikovsky
committed
// Let's remove the old workspace and try again - it will work.
Janik Zikovsky
committed
AnalysisDataService::Instance().remove("testOutMD");
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 8, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "Append", "1", "OutputDimensions",
"HKL");
TS_ASSERT(alg->isExecuted());
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"testOutMD"));
Janik Zikovsky
committed
TS_ASSERT(ws);
if (!ws)
return;
TS_ASSERT_EQUALS(ws->getDimension(0)->getName(), "[H,0,0]");
TS_ASSERT_EQUALS(ws->getSpecialCoordinateSystem(), Mantid::Kernel::HKL);
Janik Zikovsky
committed
AnalysisDataService::Instance().remove("testOutMD");
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 6, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "OutputDimensions", "Q (sample frame)");
TS_ASSERT(alg->isExecuted());
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"testOutMD"));
if (!ws)
return;
TS_ASSERT_EQUALS(ws->getDimension(0)->getName(), "Q_sample_x");
TS_ASSERT_EQUALS(ws->getSpecialCoordinateSystem(), Mantid::Kernel::QSample);
Janik Zikovsky
committed
void do_test_MINITOPAZ(EventType type, size_t numTimesToAdd = 1,
bool OneEventPerBin = false,
bool MakeWorkspace2D = false,
size_t nEventsRetrieved = 400) {
Janik Zikovsky
committed
int numEventsPer = 100;
EventWorkspace_sptr in_ws = Mantid::DataObjects::MDEventsTestHelper::
createDiffractionEventWorkspace(numEventsPer);
Janik Zikovsky
committed
if (type == WEIGHTED)
in_ws *= 2.0;
if (type == WEIGHTED_NOTIME) {
for (size_t i = 0; i < in_ws->getNumberHistograms(); i++) {
EventList &el = in_ws->getSpectrum(i);
Janik Zikovsky
committed
el.compressEvents(0.0, &el);
}
}
Janik Zikovsky
committed
// Rebin the workspace to have a manageable number bins
AnalysisDataService::Instance().addOrReplace("inputWS", in_ws);
FrameworkManager::Instance().exec("Rebin", 8, "InputWorkspace", "inputWS",
"OutputWorkspace", "inputWS", "Params",
"0, 500, 16e3", "PreserveEvents",
MakeWorkspace2D ? "0" : "1");
ConvertToDiffractionMDWorkspace2 alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setPropertyValue("InputWorkspace", "inputWS");
alg.setProperty("OneEventPerBin", OneEventPerBin);
alg.setPropertyValue("Extents", "-50, 50");
alg.setPropertyValue("OutputWorkspace", "test_md3");
TS_ASSERT_THROWS_NOTHING(alg.execute();)
TS_ASSERT(alg.isExecuted())
Janik Zikovsky
committed
MDEventWorkspace3::sptr ws;
Janik Zikovsky
committed
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"test_md3"));
Janik Zikovsky
committed
TS_ASSERT(ws);
Janik Zikovsky
committed
size_t npoints = ws->getNPoints();
// # of points != # of bins exactly because some are off the extents
TS_ASSERT_EQUALS(ws->getNumExperimentInfo(), 1);
TSM_ASSERT("ExperimentInfo object is valid", ws->getExperimentInfo(0));
Janik Zikovsky
committed
// Add to an existing MDEW
for (size_t i = 1; i < numTimesToAdd; i++) {
std::cout << "Iteration " << i << '\n';
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setPropertyValue("InputWorkspace", "inputWS");
alg.setProperty("Append", true);
Janik Zikovsky
committed
alg.setPropertyValue("OutputWorkspace", "test_md3");
TS_ASSERT_THROWS_NOTHING(alg.execute();)
TS_ASSERT(alg.isExecuted())
Janik Zikovsky
committed
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"test_md3"));
Janik Zikovsky
committed
TS_ASSERT(ws);
if (!ws)
return;
TS_ASSERT_EQUALS(
npoints * (i + 1),
ws->getNPoints()); // There are now twice as many points as before
TS_ASSERT_EQUALS(ws->getNumExperimentInfo(), (i + 1));
TSM_ASSERT("ExperimentInfo object is valid",
ws->getExperimentInfo(static_cast<uint16_t>(i)));
Janik Zikovsky
committed
}
Janik Zikovsky
committed
AnalysisDataService::Instance().remove("test_md3");
}
void test_MINITOPAZ() { do_test_MINITOPAZ(TOF); }
void test_MINITOPAZ_Weighted() { do_test_MINITOPAZ(WEIGHTED); }
void test_MINITOPAZ_addToExistingWorkspace() { do_test_MINITOPAZ(TOF, 2); }
void test_MINITOPAZ_OneEventPerBin_fromEventWorkspace() {
do_test_MINITOPAZ(TOF, 1, true, false);
Janik Zikovsky
committed
void test_MINITOPAZ_OneEventPerBin_fromWorkspace2D() {
do_test_MINITOPAZ(TOF, 1, true, true);
}
void test_MINITOPAZ_fromWorkspace2D() {
// this is questionable change, indicating that ConvertToMD and
// CovertToDiffractionWorkspace treat 0 differently
do_test_MINITOPAZ(TOF, 1, false, true, 399);
};
#endif /* MANTID_MDEVENTS_MAKEDIFFRACTIONMDEVENTWORKSPACETEST_H_ */