Skip to content
Snippets Groups Projects
CreateTransmissionWorkspaceAutoTest.h 5.41 KiB
Newer Older
#ifndef MANTID_ALGORITHMS_CREATETRANSMISSIONWORKSPACEAUTOTEST_H_
#define MANTID_ALGORITHMS_CREATETRANSMISSIONWORKSPACEAUTOTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAlgorithms/CreateTransmissionWorkspaceAuto.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/AlgorithmManager.h"
#include <boost/assign/list_of.hpp>
Owen Arnold's avatar
Owen Arnold committed
#include <boost/lexical_cast.hpp>

using Mantid::Algorithms::CreateTransmissionWorkspaceAuto;
using namespace Mantid::API;
Owen Arnold's avatar
Owen Arnold committed
using namespace Mantid::Kernel;
using namespace boost::assign;
using Mantid::MantidVec;
namespace {
class PropertyFinder {
private:
  const std::string m_propertyName;

public:
  PropertyFinder(const std::string &propertyName)
      : m_propertyName(propertyName) {}
  bool operator()(const PropertyHistories::value_type &candidate) const {
    return candidate->name() == m_propertyName;
Owen Arnold's avatar
Owen Arnold committed
  }
};

template <typename T>
T findPropertyValue(PropertyHistories &histories,
                    const std::string &propertyName) {
  PropertyFinder finder(propertyName);
  auto it = std::find_if(histories.begin(), histories.end(), finder);
  return boost::lexical_cast<T>((*it)->value());
}
class CreateTransmissionWorkspaceAutoTest : public CxxTest::TestSuite {
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
  static CreateTransmissionWorkspaceAutoTest *createSuite() {
Owen Arnold's avatar
Owen Arnold committed
    return new CreateTransmissionWorkspaceAutoTest();
  }
  static void destroySuite(CreateTransmissionWorkspaceAutoTest *suite) {
Owen Arnold's avatar
Owen Arnold committed
    delete suite;
  }

  MatrixWorkspace_sptr m_dataWS;
  CreateTransmissionWorkspaceAutoTest() {
    FrameworkManager::Instance();
Owen Arnold's avatar
Owen Arnold committed
    IAlgorithm_sptr lAlg = AlgorithmManager::Instance().create("Load");
    lAlg->setChild(true);
    lAlg->initialize();
    lAlg->setProperty("Filename", "INTER00013460.nxs");
    lAlg->setPropertyValue("OutputWorkspace", "demo_ws");
    lAlg->execute();
    Workspace_sptr temp = lAlg->getProperty("OutputWorkspace");
    m_dataWS = boost::dynamic_pointer_cast<MatrixWorkspace>(temp);
  ~CreateTransmissionWorkspaceAutoTest() {}
  void test_Init() {
    CreateTransmissionWorkspaceAuto alg;
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
  void test_exec() {
    IAlgorithm_sptr alg =
        AlgorithmManager::Instance().create("CreateTransmissionWorkspaceAuto");
Owen Arnold's avatar
Owen Arnold committed
    alg->setRethrows(true);
    TS_ASSERT_THROWS_NOTHING(alg->initialize());
    TS_ASSERT_THROWS_NOTHING(
        alg->setProperty("FirstTransmissionRun", m_dataWS));
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("OutputWorkspace", "outWS"));
Owen Arnold's avatar
Owen Arnold committed
    alg->execute();
    TS_ASSERT(alg->isExecuted());
    MatrixWorkspace_sptr outWS =
        AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("outWS");
Owen Arnold's avatar
Owen Arnold committed

    auto inst = m_dataWS->getInstrument();
    auto workspaceHistory = outWS->getHistory();
    AlgorithmHistory_const_sptr workerAlgHistory =
        workspaceHistory.getAlgorithmHistory(0)->getChildAlgorithmHistory(0);
    auto vecPropertyHistories = workerAlgHistory->getProperties();

    const double wavelengthMin =
        findPropertyValue<double>(vecPropertyHistories, "WavelengthMin");
    double wavelengthMax =
        findPropertyValue<double>(vecPropertyHistories, "WavelengthMax");
    double monitorBackgroundWavelengthMin = findPropertyValue<double>(
        vecPropertyHistories, "MonitorBackgroundWavelengthMin");
    double monitorBackgroundWavelengthMax = findPropertyValue<double>(
        vecPropertyHistories, "MonitorBackgroundWavelengthMax");
    double monitorIntegrationWavelengthMin = findPropertyValue<double>(
        vecPropertyHistories, "MonitorIntegrationWavelengthMin");
    double monitorIntegrationWavelengthMax = findPropertyValue<double>(
        vecPropertyHistories, "MonitorIntegrationWavelengthMax");
    int i0MonitorIndex =
        findPropertyValue<int>(vecPropertyHistories, "I0MonitorIndex");
    std::string processingInstructions = findPropertyValue<std::string>(
        vecPropertyHistories, "ProcessingInstructions");
    std::vector<std::string> pointDetectorStartStop;
    boost::split(pointDetectorStartStop, processingInstructions,
                 boost::is_any_of(","));
    TS_ASSERT_EQUALS(inst->getNumberParameter("LambdaMin").at(0),
                     wavelengthMin);
    TS_ASSERT_EQUALS(inst->getNumberParameter("LambdaMax").at(0),
                     wavelengthMax);
Owen Arnold's avatar
Owen Arnold committed
    TS_ASSERT_EQUALS(inst->getNumberParameter("MonitorBackgroundMin").at(0),
                     monitorBackgroundWavelengthMin);
Owen Arnold's avatar
Owen Arnold committed
    TS_ASSERT_EQUALS(inst->getNumberParameter("MonitorBackgroundMax").at(0),
                     monitorBackgroundWavelengthMax);
Owen Arnold's avatar
Owen Arnold committed
    TS_ASSERT_EQUALS(inst->getNumberParameter("MonitorIntegralMin").at(0),
                     monitorIntegrationWavelengthMin);
Owen Arnold's avatar
Owen Arnold committed
    TS_ASSERT_EQUALS(inst->getNumberParameter("MonitorIntegralMax").at(0),
                     monitorIntegrationWavelengthMax);
    TS_ASSERT_EQUALS(inst->getNumberParameter("I0MonitorIndex").at(0),
                     i0MonitorIndex);
Owen Arnold's avatar
Owen Arnold committed
    TS_ASSERT_EQUALS(inst->getNumberParameter("PointDetectorStart").at(0),
                     boost::lexical_cast<double>(pointDetectorStartStop.at(0)));
Owen Arnold's avatar
Owen Arnold committed
    TS_ASSERT_EQUALS(inst->getNumberParameter("PointDetectorStop").at(0),
                     boost::lexical_cast<double>(pointDetectorStartStop.at(1)));
#endif /* MANTID_ALGORITHMS_CREATETRANSMISSIONWORKSPACEAUTOTEST_H_ */