Skip to content
Snippets Groups Projects
LoadNexusProcessedTest.h 58.3 KiB
Newer Older
               "with an index",
               AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
                   "ws_loaded_1"));
    TSM_ASSERT("Can access workspace via name which is the name of the file "
               "with an index",
               AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
                   "ws_loaded_2"));
    // Clean up
    AnalysisDataService::Instance().remove("ws_loaded_1");
    AnalysisDataService::Instance().remove("ws_loaded_2");
    if (!tempFile.empty() && Poco::File(tempFile).exists()) {
      Poco::File(tempFile).remove();
    }
  }

  void do_load_multiperiod_workspace(bool fast) {
Owen Arnold's avatar
Owen Arnold committed
    LoadNexusProcessed loader;
    loader.setChild(true);
    loader.initialize();
    loader.setPropertyValue("Filename", "POLREF00004699_nexus.nxs");
    loader.setPropertyValue("OutputWorkspace", "ws");
    loader.setProperty("FastMultiPeriod", fast);

    TS_ASSERT(loader.execute());
Owen Arnold's avatar
Owen Arnold committed

    Workspace_sptr outWS = loader.getProperty("OutputWorkspace");
    WorkspaceGroup_sptr asGroupWS =
        std::dynamic_pointer_cast<WorkspaceGroup>(outWS);
Owen Arnold's avatar
Owen Arnold committed
    TSM_ASSERT("We expect a group workspace back", asGroupWS);
    TSM_ASSERT_EQUALS("We expect the size to be 2", 2, asGroupWS->size());
    MatrixWorkspace_sptr period1 =
        std::dynamic_pointer_cast<MatrixWorkspace>(asGroupWS->getItem(0));
    MatrixWorkspace_sptr period2 =
        std::dynamic_pointer_cast<MatrixWorkspace>(asGroupWS->getItem(1));
    TSM_ASSERT("We expect the group workspace is multiperiod",
               asGroupWS->isMultiperiod());
    TSM_ASSERT_EQUALS("X-data should be identical", period1->x(0),
                      period2->x(0));
    TSM_ASSERT_DIFFERS("Y-data should be different", period1->y(0),
                       period2->y(0));
    TSM_ASSERT_DIFFERS("E-data should be different", period1->e(0),
                       period2->e(0));
Owen Arnold's avatar
Owen Arnold committed

    TS_ASSERT(period1->getInstrument());
    TS_ASSERT(period2->getInstrument());

    auto period1Logs = period1->run().getLogData();
    auto period2Logs = period2->run().getLogData();

    TSM_ASSERT_EQUALS("We expect to have the same number of log entries",
                      period1Logs.size(), period2Logs.size());
    TSM_ASSERT_THROWS("Should only have a period 1 entry",
                      period1->run().getLogData("period 2"),
                      Exception::NotFoundError &);
    TSM_ASSERT_THROWS("Should only have a period 2 entry",
                      period2->run().getLogData("period 1"),
                      Exception::NotFoundError &);
  void test_load_multiperiod_workspace_fast() {
Owen Arnold's avatar
Owen Arnold committed
    do_load_multiperiod_workspace(true /*Use optimised route*/);
  }

  void test_load_multiperiod_workspace_old() {
Owen Arnold's avatar
Owen Arnold committed
    do_load_multiperiod_workspace(false /*Use old route*/);
  }
  void test_load_workspace_empty_textaxis() {
    // filename workspaceEmptyTextAxis
    LoadNexusProcessed loader;
    loader.setChild(true);
    TS_ASSERT_THROWS_NOTHING(loader.initialize());
    TS_ASSERT_THROWS_NOTHING(
        loader.setPropertyValue("Filename", "workspaceEmptyTextAxis.nxs"));
    TS_ASSERT_THROWS_NOTHING(loader.setPropertyValue("OutputWorkspace", "ws"));

    TS_ASSERT(loader.execute());
    TS_ASSERT(loader.isExecuted());

    Workspace_const_sptr ws = loader.getProperty("OutputWorkspace");
    const auto outWS = std::dynamic_pointer_cast<const MatrixWorkspace>(ws);
    const size_t numBins = outWS->blocksize();
    for (size_t i = 0; i < numBins; ++i) {
      TS_ASSERT_EQUALS(outWS->x(0)[i], i);
      TS_ASSERT_EQUALS(outWS->y(0)[i], i);
    }
  }

  void test_load_workspace_with_textaxis() {
    // filename workspaceWithTextAxis
    LoadNexusProcessed loader;
    loader.setChild(true);
    TS_ASSERT_THROWS_NOTHING(loader.initialize());
    TS_ASSERT_THROWS_NOTHING(
        loader.setPropertyValue("Filename", "workspaceWithTextAxis.nxs"));
    TS_ASSERT_THROWS_NOTHING(loader.setPropertyValue("OutputWorkspace", "ws"));

    TS_ASSERT(loader.execute());
    TS_ASSERT(loader.isExecuted());

    Workspace_const_sptr ws = loader.getProperty("OutputWorkspace");
    const auto outWS = std::dynamic_pointer_cast<const MatrixWorkspace>(ws);
    const size_t numBins = outWS->blocksize();
    for (size_t i = 0; i < numBins; ++i) {
      TS_ASSERT_EQUALS(outWS->x(0)[i], i);
      TS_ASSERT_EQUALS(outWS->y(0)[i], i);
    }
  }

Nick Draper's avatar
Nick Draper committed
  void test_Log_invalid_value_filtering_survives_save_and_load() {
    LoadNexus alg;

    TS_ASSERT_THROWS_NOTHING(alg.initialize());
    TS_ASSERT(alg.isInitialized());
    testFile = "ENGINX00228061_log_alarm_data.nxs";
    alg.setPropertyValue("Filename", testFile);
    testFile = alg.getPropertyValue("Filename");

    alg.setPropertyValue("OutputWorkspace", output_ws);

    TS_ASSERT_THROWS_NOTHING(alg.execute());

    // Test some aspects of the file
    MatrixWorkspace_sptr workspace;
    workspace = std::dynamic_pointer_cast<MatrixWorkspace>(
        AnalysisDataService::Instance().retrieve(output_ws));
Nick Draper's avatar
Nick Draper committed

Nick Draper's avatar
Nick Draper committed
    check_log(workspace, "cryo_temp1", 1, 3, 7.0);

    SaveNexusProcessed save;
    save.initialize();
    save.setPropertyValue("InputWorkspace", output_ws);
    std::string filename = "LoadNexusProcessed_test_Log_invalid_value_"
                           "filtering_survives_save_and_load.nxs";
    save.setPropertyValue("Filename", filename);
    filename = save.getPropertyValue("Filename");
    save.execute();
    LoadNexusProcessed load;
    load.initialize();
    load.setPropertyValue("Filename", filename);
    load.setPropertyValue("OutputWorkspace", output_ws);
    load.execute();

    workspace = std::dynamic_pointer_cast<MatrixWorkspace>(
        AnalysisDataService::Instance().retrieve(output_ws));
    TS_ASSERT(workspace.get());
    check_log(workspace, "cryo_temp1", 1, 3, 7.0);
    if (Poco::File(filename).exists())
      Poco::File(filename).remove();
  }

  void test_log_filtering_survives_save_and_load() {
    LoadNexus alg;
Nick Draper's avatar
Nick Draper committed
    std::string group_ws = "test_log_filtering_survives_save_and_load";
    TS_ASSERT_THROWS_NOTHING(alg.initialize());
    TS_ASSERT(alg.isInitialized());
    testFile = "POLREF00014966.nxs";
    alg.setPropertyValue("Filename", testFile);
    testFile = alg.getPropertyValue("Filename");

    alg.setPropertyValue("OutputWorkspace", group_ws);

    TS_ASSERT_THROWS_NOTHING(alg.execute());

    // Test some aspects of the file
Nick Draper's avatar
Nick Draper committed
    MatrixWorkspace_sptr workspace;
    workspace = std::dynamic_pointer_cast<MatrixWorkspace>(
        AnalysisDataService::Instance().retrieve(group_ws + "_1"));
Nick Draper's avatar
Nick Draper committed
    // should be filtered
    check_log(workspace, "raw_uah_log", 429, 17, 99.4740982879);
Nick Draper's avatar
Nick Draper committed
    // should not be filtered
    check_log(workspace, "periods", 37, 1, 1);
    check_log(workspace, "period 1", 36, 505, true);
    check_log(workspace, "running", 72, 501, true);

    SaveNexusProcessed save;
    save.initialize();
    save.setProperty("InputWorkspace", workspace);
    std::string filename =
        "LoadNexusProcessed_test_log_filtering_survives_save_and_load.nxs";
    save.setPropertyValue("Filename", filename);
    filename = save.getPropertyValue("Filename");
    save.execute();
    LoadNexusProcessed load;
    load.initialize();
    load.setPropertyValue("Filename", filename);
    load.setPropertyValue("OutputWorkspace", output_ws);
    load.execute();

Nick Draper's avatar
Nick Draper committed
    auto reloadedWorkspace = std::dynamic_pointer_cast<MatrixWorkspace>(
        AnalysisDataService::Instance().retrieve(output_ws));
    // should not change as should be filtered as before
Nick Draper's avatar
Nick Draper committed
    check_log(reloadedWorkspace, "raw_uah_log", 429, 17, 99.4740982879);
Nick Draper's avatar
Nick Draper committed
    // should not change as should not be filtered as before
Nick Draper's avatar
Nick Draper committed
    check_log(reloadedWorkspace, "periods", 37, 1, 1);
    check_log(reloadedWorkspace, "period 1", 36, 505, true);
    check_log(reloadedWorkspace, "running", 72, 501, true);

    if (Poco::File(filename).exists())
      Poco::File(filename).remove();
  }

  void test_load_leanElasticPeakWorkspace() {
    // generate a lean elastic peak workspace with two peaks
    auto lpws = std::make_shared<LeanElasticPeaksWorkspace>();
    // add peaks
    LeanElasticPeak pk1(V3D(0.0, 0.0, 6.28319), 2.0);     // (100)
    LeanElasticPeak pk2(V3D(6.28319, 0.0, 6.28319), 1.0); // (110)
    lpws->addPeak(pk1);
    lpws->addPeak(pk2);

    // save the lean elastic peak workspace to temp
    NexusTestHelper nexusHelper(true);
    nexusHelper.createFile("testLoadLeanElasticPeaksWorkspace.nxs");
    lpws->saveNexus(nexusHelper.file.get());

    // load it back with the loader
    LoadNexusProcessed load;
    load.initialize();
    load.setProperty("Filename", nexusHelper.file.get());
    load.setProperty("OutputWorkspace", "lpws_loaded");
    load.execute();
    auto lpws_loaded = std::dynamic_pointer_cast<LeanElasticPeaksWorkspace>(
        AnalysisDataService::Instance().retrieve("lpws_loaded"));

    // confirm that the peaks are the same in original
    // and the loaded lean elastic peak workspace
    TS_ASSERT_EQUALS(lpws->getNumberPeaks(), lpws_loaded->getNumberPeaks());
    // --pk1
    TS_ASSERT_DELTA(lpws->getPeak(0).getWavelength(), pk1.getWavelength(),
                    1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(0).getFinalEnergy(), pk1.getFinalEnergy(),
                    1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(0).getH(), pk1.getH(), 1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(0).getK(), pk1.getK(), 1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(0).getL(), pk1.getL(), 1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(0).getQLabFrame().X(), pk1.getQLabFrame().X(),
                    1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(0).getQLabFrame().Y(), pk1.getQLabFrame().Y(),
                    1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(0).getQLabFrame().Z(), pk1.getQLabFrame().Z(),
                    1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(1).getWavelength(), pk2.getWavelength(),
                    1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(1).getFinalEnergy(), pk2.getFinalEnergy(),
                    1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(1).getH(), pk2.getH(), 1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(1).getK(), pk2.getK(), 1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(1).getL(), pk2.getL(), 1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(1).getQLabFrame().X(), pk2.getQLabFrame().X(),
                    1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(1).getQLabFrame().Y(), pk2.getQLabFrame().Y(),
                    1e-5);
    TS_ASSERT_DELTA(lpws->getPeak(1).getQLabFrame().Z(), pk2.getQLabFrame().Z(),
                    1e-5);
Nick Draper's avatar
Nick Draper committed
  void check_log(Mantid::API::MatrixWorkspace_sptr &workspace,
                 const std::string &logName, const int noOfEntries,
                 const int firstInterval, const TYPE firstValue) {
    TS_ASSERT(workspace.get());
    auto run = workspace->run();

    auto prop = run.getLogData(logName);
    TSM_ASSERT(logName + " Log was not found", prop);
    if (prop) {
      auto log =
          dynamic_cast<TimeSeriesProperty<TYPE> *>(run.getLogData(logName));
      TSM_ASSERT(logName + " Log was not the expected type", log);
      if (log) {
        // middle value is invalid and is filtered out
        TSM_ASSERT_EQUALS(logName + " Log size not as expected", log->size(),
                          noOfEntries);
        TSM_ASSERT_EQUALS(logName + " Log first interval not as expected",
                          log->nthInterval(0).length().total_seconds(),
                          firstInterval);
        templated_equality_check(logName + " Log first value not as expected",
                                 log->nthValue(0), firstValue);
      }
    }
  }

Nick Draper's avatar
Nick Draper committed
  // There is also an explicit instantiation of this for doubles
  template <typename TYPE>
  void templated_equality_check(const std::string &message, const TYPE value,
                                const TYPE refValue) {
    TSM_ASSERT_EQUALS(message, value, refValue);
  }
  void doHistoryTest(const MatrixWorkspace_sptr &matrix_ws) {
    const WorkspaceHistory history = matrix_ws->getHistory();
    int nalgs = static_cast<int>(history.size());
    TS_ASSERT_EQUALS(nalgs, 4);
    if (nalgs == 4) {
      TS_ASSERT_EQUALS(history[0]->name(), "LoadRaw");
      TS_ASSERT_EQUALS(history[1]->name(), "AlignDetectors");
      TS_ASSERT_EQUALS(history[2]->name(), "DiffractionFocussing");
      TS_ASSERT_EQUALS(history[3]->name(), "LoadNexusProcessed");
  /**
   * Do a few standard checks that are repeated in multiple tests of
   * partial event data loading
   *
   * @param alg initialized and parameterized load algorithm
   * @param nSpectra expected number of spectra
   * @param nHistory expected number of entries in the algorithm
   * history
   **/
  void doCommonEventLoadChecks(LoadNexusProcessed &alg, size_t nSpectra,
                               size_t nHistory) {
    TS_ASSERT_THROWS_NOTHING(alg.execute());
    TS_ASSERT(alg.isExecuted());

    // Test basic props of the ws
    Workspace_sptr workspace;
    TS_ASSERT_THROWS_NOTHING(
        workspace = AnalysisDataService::Instance().retrieve(output_ws));
    TS_ASSERT(workspace);
    if (!workspace)
      return;
    TS_ASSERT(workspace.get());

    EventWorkspace_sptr ews =
        std::dynamic_pointer_cast<EventWorkspace>(workspace);
    TS_ASSERT(ews);
    if (!ews)
      return;
    TS_ASSERT(ews.get());
    TS_ASSERT_EQUALS(ews->getNumberHistograms(), nSpectra);

    TS_ASSERT_EQUALS(ews->getHistory().size(), nHistory);
  }

  /*
   * Does a few common checks for using a single spectra property
   * such as spectrumMin or spectrumMax. Expects the algorithm
   * passed in to be configured for the test
   *
   * @param alg The configured algorithm to be executed
   * @param expectedSize The number of spectra which should be present
   */
  void doSpectrumMinOrMaxTest(LoadNexusProcessed &alg,
                              const size_t expectedSize) {
    TS_ASSERT_THROWS_NOTHING(alg.execute());

    // Test some aspects of the file
    Workspace_sptr workspace;
    TS_ASSERT_THROWS_NOTHING(
        workspace = AnalysisDataService::Instance().retrieve(output_ws));
    TS_ASSERT(workspace.get());

    MatrixWorkspace_sptr matrix_ws =
        std::dynamic_pointer_cast<MatrixWorkspace>(workspace);
    TS_ASSERT(matrix_ws.get());

    // Testing the number of histograms
    TS_ASSERT_EQUALS(matrix_ws->getNumberHistograms(), expectedSize);

    // Test history
    doHistoryTest(matrix_ws);

    std::shared_ptr<const Mantid::Geometry::Instrument> inst =
        matrix_ws->getInstrument();
    TS_ASSERT_EQUALS(inst->getName(), "GEM");
    TS_ASSERT_EQUALS(inst->getSource()->getPos().Z(), -17);
  }

  /**
   * Does a few common checks for using spectra lists with/without
   * spectrum min and/or max being set. Expects the algorithm
   * passed in to be configured for this test.
   *
   * @param alg The configured algorithm to executed
   * @param expectedSpectra The IDs of the spectrum loaded which should
   * be present
   */
  void doSpectrumListTests(LoadNexusProcessed &alg,
                           const std::vector<int> &expectedSpectra) {
    TS_ASSERT_THROWS_NOTHING(alg.execute());
    TS_ASSERT(alg.isExecuted());

    // Test some aspects of the file
    Workspace_sptr workspace;
    TS_ASSERT_THROWS_NOTHING(
        workspace = AnalysisDataService::Instance().retrieve(output_ws));
    TS_ASSERT(workspace.get());

    MatrixWorkspace_sptr matrix_ws =
        std::dynamic_pointer_cast<MatrixWorkspace>(workspace);
    TS_ASSERT(matrix_ws.get());

    // Test spectrum numbers are as expected
    size_t index(0);
    int seenSpectra(0);
    for (const auto spectrum : expectedSpectra) {
      TS_ASSERT_EQUALS(matrix_ws->getSpectrum(index).getSpectrumNo(), spectrum);
      ++index;
      ++seenSpectra;
    }

    TS_ASSERT_EQUALS(seenSpectra, expectedSpectra.size());

    doHistoryTest(matrix_ws);

    std::shared_ptr<const Mantid::Geometry::Instrument> inst =
        matrix_ws->getInstrument();
    TS_ASSERT_EQUALS(inst->getName(), "GEM");
    TS_ASSERT_EQUALS(inst->getSource()->getPos().Z(), -17);
  }

  void writeTmpEventNexus() {
    // return;
    if (!m_savedTmpEventFile.empty() &&
        Poco::File(m_savedTmpEventFile).exists())
    std::vector<std::vector<int>> groups(6);
    groups[0].emplace_back(9);
    groups[0].emplace_back(12);
    groups[1].emplace_back(5);
    groups[1].emplace_back(10);
    groups[2].emplace_back(20);
    groups[2].emplace_back(21);
    groups[3].emplace_back(10);
    groups[4].emplace_back(50);
    groups[5].emplace_back(15);
    groups[5].emplace_back(20);
    EventWorkspace_sptr ws =
        WorkspaceCreationHelper::createGroupedEventWorkspace(groups, 30, 1.0);
    ws->getSpectrum(4).clear();
    TS_ASSERT_EQUALS(ws->getNumberHistograms(), groups.size());

    SaveNexusProcessed alg;
    alg.initialize();
    alg.setProperty("InputWorkspace", std::dynamic_pointer_cast<Workspace>(ws));
    m_savedTmpEventFile = "LoadNexusProcessed_TmpEvent.nxs";
    alg.setPropertyValue("Filename", m_savedTmpEventFile);
    alg.setPropertyValue("Title",
                         "Tmp test event workspace as NexusProcessed file");

    TS_ASSERT_THROWS_NOTHING(alg.execute());
    TS_ASSERT(alg.isExecuted());

    // Get absolute path to the saved file
    m_savedTmpEventFile = alg.getPropertyValue("Filename");
  }

  void clearTmpEventNexus() {
    // remove saved/re-loaded test event data file
    if (!m_savedTmpEventFile.empty() &&
        Poco::File(m_savedTmpEventFile).exists())
      Poco::File(m_savedTmpEventFile).remove();
  }

  void doTestLoadAndSaveHistogramWS(bool useXErrors = false,
                                    bool numericAxis = false,
                                    bool legacyXErrors = false) {
    // Test SaveNexusProcessed/LoadNexusProcessed on a histogram workspace with
    // x errors

    // Create histogram workspace with two spectra and 4 points
    std::vector<double> x1{1, 2, 3};
    std::vector<double> dx1{3, 2};
    std::vector<double> y1{1, 2};
    std::vector<double> x2{1, 2, 3};
    std::vector<double> dx2{3, 2};
    std::vector<double> y2{1, 2};
    MatrixWorkspace_sptr inputWs = WorkspaceFactory::Instance().create(
        "Workspace2D", 2, x1.size(), y1.size());
    inputWs->mutableX(0) = x1;
    inputWs->mutableX(1) = x2;
    inputWs->mutableY(0) = y1;
    inputWs->mutableY(1) = y2;
      inputWs->setPointStandardDeviations(0, dx1);
      inputWs->setPointStandardDeviations(1, dx2);
        inputWs->dataDx(0).emplace_back(1);
        inputWs->dataDx(1).emplace_back(1);
      auto numericAxis = std::make_unique<NumericAxis>(2);
      numericAxis->setValue(0, 10.0);
      numericAxis->setValue(1, 20.0);
      inputWs->replaceAxis(1, std::move(numericAxis));

    // Save workspace
    IAlgorithm_sptr save =
        AlgorithmManager::Instance().create("SaveNexusProcessed");
    save->initialize();
    TS_ASSERT(save->isInitialized());
    TS_ASSERT_THROWS_NOTHING(save->setProperty("InputWorkspace", inputWs));
    TS_ASSERT_THROWS_NOTHING(save->setPropertyValue(
        "Filename", "TestSaveAndLoadNexusProcessed.nxs"));
    TS_ASSERT_THROWS_NOTHING(save->execute());

    // Load workspace
    IAlgorithm_sptr load =
        AlgorithmManager::Instance().create("LoadNexusProcessed");
    load->initialize();
    TS_ASSERT(load->isInitialized());
    TS_ASSERT_THROWS_NOTHING(load->setPropertyValue(
        "Filename", "TestSaveAndLoadNexusProcessed.nxs"));
    TS_ASSERT_THROWS_NOTHING(
        load->setPropertyValue("OutputWorkspace", "output"));
    TS_ASSERT_THROWS_NOTHING(load->execute());

    // Check spectra in loaded workspace
    MatrixWorkspace_sptr outputWs =
        AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("output");
    TS_ASSERT_EQUALS(1, outputWs->getSpectrum(0).getSpectrumNo());
    TS_ASSERT_EQUALS(2, outputWs->getSpectrum(1).getSpectrumNo());
    TS_ASSERT_EQUALS(inputWs->x(0), outputWs->x(0));
    TS_ASSERT_EQUALS(inputWs->x(1), outputWs->x(1));
    TS_ASSERT_EQUALS(inputWs->y(0), outputWs->y(0));
    TS_ASSERT_EQUALS(inputWs->y(1), outputWs->y(1));
    TS_ASSERT_EQUALS(inputWs->e(0), outputWs->e(0));
    TS_ASSERT_EQUALS(inputWs->e(1), outputWs->e(1));
    if (useXErrors) {
      TSM_ASSERT("Should have an x error", outputWs->hasDx(0));
      TS_ASSERT_EQUALS(dx1, outputWs->dx(0).rawData());
      TS_ASSERT_EQUALS(dx2, outputWs->dx(1).rawData());
    // Axes
    auto axis1 = outputWs->getAxis(1);
    if (numericAxis) {
      TS_ASSERT(axis1->isNumeric());
      TS_ASSERT_DELTA(10.0, axis1->getValue(0), 1e-10);
      TS_ASSERT_DELTA(20.0, axis1->getValue(1), 1e-10);
    } else {
      TS_ASSERT(axis1->isSpectra());
      TS_ASSERT_DELTA(1.0, axis1->getValue(0), 1e-10);
      TS_ASSERT_DELTA(2.0, axis1->getValue(1), 1e-10);
    }

    // Remove workspace and saved nexus file
    AnalysisDataService::Instance().remove("output");
    Poco::File("TestSaveAndLoadNexusProcessed.nxs").remove();
  }

  void doTestLoadAndSavePointWS(bool useXErrors = false) {
    // Test SaveNexusProcessed/LoadNexusProcessed on a point-like workspace

    // Create histogram workspace with two spectra and 4 points
    std::vector<double> x1{1, 2, 3};
    std::vector<double> dx1{3, 2, 1};
    std::vector<double> y1{1, 2, 3};
    std::vector<double> x2{10, 20, 30};
    std::vector<double> dx2{30, 22, 10};
    std::vector<double> y2{10, 20, 30};
    MatrixWorkspace_sptr inputWs = WorkspaceFactory::Instance().create(
        "Workspace2D", 2, x1.size(), y1.size());
    inputWs->mutableX(0) = x1;
    inputWs->mutableX(1) = x2;
    inputWs->mutableY(0) = y1;
    inputWs->mutableY(1) = y2;
      inputWs->setPointStandardDeviations(0, dx1);
      inputWs->setPointStandardDeviations(1, dx2);
    }

    // Save workspace
    IAlgorithm_sptr save =
        AlgorithmManager::Instance().create("SaveNexusProcessed");
    save->initialize();
    TS_ASSERT(save->isInitialized());
    TS_ASSERT_THROWS_NOTHING(save->setProperty("InputWorkspace", inputWs));
    TS_ASSERT_THROWS_NOTHING(save->setPropertyValue(
        "Filename", "TestSaveAndLoadNexusProcessed.nxs"));
    TS_ASSERT_THROWS_NOTHING(save->execute());

    // Load workspace
    IAlgorithm_sptr load =
        AlgorithmManager::Instance().create("LoadNexusProcessed");
    load->initialize();
    TS_ASSERT(load->isInitialized());
    TS_ASSERT_THROWS_NOTHING(load->setPropertyValue(
        "Filename", "TestSaveAndLoadNexusProcessed.nxs"));
    TS_ASSERT_THROWS_NOTHING(
        load->setPropertyValue("OutputWorkspace", "output"));
    TS_ASSERT_THROWS_NOTHING(load->execute());

    // Check spectra in loaded workspace
    MatrixWorkspace_sptr outputWs =
        AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("output");
    TS_ASSERT_EQUALS(inputWs->x(0), outputWs->x(0));
    TS_ASSERT_EQUALS(inputWs->x(1), outputWs->x(1));
    TS_ASSERT_EQUALS(inputWs->y(0), outputWs->y(0));
    TS_ASSERT_EQUALS(inputWs->y(1), outputWs->y(1));
    TS_ASSERT_EQUALS(inputWs->e(0), outputWs->e(0));
    TS_ASSERT_EQUALS(inputWs->e(1), outputWs->e(1));
    if (useXErrors) {
      TSM_ASSERT("Should have an x error", outputWs->hasDx(0));
      TS_ASSERT_EQUALS(inputWs->dx(0).rawData(), outputWs->dx(0).rawData());
      TS_ASSERT_EQUALS(inputWs->dx(1).rawData(), outputWs->dx(1).rawData());
    }

    // Remove workspace and saved nexus file
    AnalysisDataService::Instance().remove("output");
    Poco::File("TestSaveAndLoadNexusProcessed.nxs").remove();
  }

  /// Saved using SaveNexusProcessed and re-used in several load event tests
  std::string m_savedTmpEventFile;
  static const EventType m_savedTmpType = TOF;
template <>
void LoadNexusProcessedTest::templated_equality_check(
Nick Draper's avatar
Nick Draper committed
    const std::string &message, const double value, const double refValue) {
  TSM_ASSERT_DELTA(message, value, refValue, 1e-5);
}

//------------------------------------------------------------------------------
// Performance test
//------------------------------------------------------------------------------

class LoadNexusProcessedTestPerformance : public CxxTest::TestSuite {
  void testHistogramWorkspace() {
    LoadNexusProcessed loader;
    loader.initialize();
    loader.setPropertyValue("Filename", "PG3_733_focussed.nxs");
    loader.setPropertyValue("OutputWorkspace", "ws");
    TS_ASSERT(loader.execute());

  void testPeaksWorkspace() {
    LoadNexusProcessed loader;
    loader.initialize();
    loader.setPropertyValue("Filename", "24954_allpeaksbyhand.nxs");
    loader.setPropertyValue("OutputWorkspace", "peaks");
    TS_ASSERT(loader.execute());
  }