Skip to content
Snippets Groups Projects
LoadNexusProcessedTest.h 58.3 KiB
Newer Older
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FileFinder.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/WorkspaceHistory.h"
#include "MantidDataHandling/Load.h"
#include "MantidDataHandling/LoadInstrument.h"
#include "MantidDataHandling/LoadNexusProcessed.h"
#include "MantidDataHandling/SaveNexusProcessed.h"
#include "MantidDataObjects/LeanElasticPeaksWorkspace.h"
#include "MantidDataObjects/Peak.h"
#include "MantidDataObjects/PeakShapeSpherical.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/IDTypes.h"
#include "MantidGeometry/Instrument/InstrumentDefinitionParser.h"
#include "MantidTestHelpers/NexusTestHelper.h"
#include "SaveNexusProcessedTest.h"

Campbell, Stuart's avatar
Campbell, Stuart committed
#include <Poco/File.h>
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
using namespace Mantid::Geometry;
Janik Zikovsky's avatar
Janik Zikovsky committed
using namespace Mantid::Kernel;
using namespace Mantid::DataObjects;
// Note that this suite tests an old version of Nexus processed files that we
// continue to support.
// LoadRawSaveNxsLoadNxs tests the current version of Nexus processed by loading
// a newly created Nexus processed file.
// LoadRawSaveNxsLoadNxs should be run when making any changes to
// LoadNexusProcessed
// in addition to this test.

class LoadNexusProcessedTest : public CxxTest::TestSuite {
  static LoadNexusProcessedTest *createSuite() {
Owen Arnold's avatar
Owen Arnold committed
    return new LoadNexusProcessedTest();
  }
  static void destroySuite(LoadNexusProcessedTest *suite) { delete suite; }
  LoadNexusProcessedTest()
      : testFile("GEM38370_Focussed_Legacy.nxs"), output_ws("nxstest"),
        m_savedTmpEventFile("") {}
  ~LoadNexusProcessedTest() override {
    AnalysisDataService::Instance().clear();
  void testFastMultiPeriodDefault() {
    LoadNexusProcessed alg;
    alg.initialize();
    TS_ASSERT(alg.isInitialized());
    const bool bFastMultiPeriod = alg.getProperty("FastMultiPeriod");
    TSM_ASSERT("Should defalt to offering fast multiperiod loading",
               bFastMultiPeriod);
  void testProcessedFile() {
    LoadNexusProcessed alg;

    TS_ASSERT_THROWS_NOTHING(alg.initialize());
    TS_ASSERT(alg.isInitialized());
    alg.setPropertyValue("Filename", testFile);
    alg.setPropertyValue("OutputWorkspace", output_ws);

    TS_ASSERT_THROWS_NOTHING(alg.execute());
    // Test some aspects of the file
    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 proton charge from the sample block
    TS_ASSERT_DELTA(matrix_ws->run().getProtonCharge(), 30.14816, 1e-5);
    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 testNexusProcessed_Min_Max() {

    LoadNexusProcessed alg;

    TS_ASSERT_THROWS_NOTHING(alg.initialize());
    TS_ASSERT(alg.isInitialized());
Owen Arnold's avatar
Owen Arnold committed
    testFile = "focussed.nxs";
    alg.setPropertyValue("Filename", testFile);
    alg.setPropertyValue("OutputWorkspace", output_ws);
Owen Arnold's avatar
Owen Arnold committed
    alg.setPropertyValue("SpectrumMin", "2");
    alg.setPropertyValue("SpectrumMax", "4");
    const std::vector<int> expectedSpectra = {3, 4, 5};
    doSpectrumListTests(alg, expectedSpectra);
  void testNexusProcessed_List() {
    LoadNexusProcessed alg;

    TS_ASSERT_THROWS_NOTHING(alg.initialize());
    TS_ASSERT(alg.isInitialized());
Owen Arnold's avatar
Owen Arnold committed
    testFile = "focussed.nxs";
    alg.setPropertyValue("Filename", testFile);
    alg.setPropertyValue("OutputWorkspace", output_ws);
Owen Arnold's avatar
Owen Arnold committed
    alg.setPropertyValue("SpectrumList", "1,2,3,4");
    const std::vector<int> expectedSpectra = {2, 3, 4, 5};
    doSpectrumListTests(alg, expectedSpectra);
  void testNexusProcessed_Min_Max_List() {
    LoadNexusProcessed alg;

    TS_ASSERT_THROWS_NOTHING(alg.initialize());
    TS_ASSERT(alg.isInitialized());
Owen Arnold's avatar
Owen Arnold committed
    testFile = "focussed.nxs";
    alg.setPropertyValue("Filename", testFile);
    alg.setPropertyValue("OutputWorkspace", output_ws);
Owen Arnold's avatar
Owen Arnold committed
    alg.setPropertyValue("SpectrumMin", "1");
    alg.setPropertyValue("SpectrumMax", "3");
    alg.setPropertyValue("SpectrumList", "4,5");
    const std::vector<int> expectedSpectra = {2, 3, 4, 5, 6};
    doSpectrumListTests(alg, expectedSpectra);
  void testNexusProcessed_Min() {
    LoadNexusProcessed alg;

    TS_ASSERT_THROWS_NOTHING(alg.initialize());
    TS_ASSERT(alg.isInitialized());
Owen Arnold's avatar
Owen Arnold committed
    testFile = "focussed.nxs";
    alg.setPropertyValue("Filename", testFile);
    alg.setPropertyValue("OutputWorkspace", output_ws);
Owen Arnold's avatar
Owen Arnold committed
    alg.setPropertyValue("SpectrumMin", "4");

    doSpectrumMinOrMaxTest(alg, 3);
  void testNexusProcessed_Max() {
    LoadNexusProcessed alg;

    TS_ASSERT_THROWS_NOTHING(alg.initialize());
    TS_ASSERT(alg.isInitialized());
Owen Arnold's avatar
Owen Arnold committed
    testFile = "focussed.nxs";
    alg.setPropertyValue("Filename", testFile);
    alg.setPropertyValue("OutputWorkspace", output_ws);
Owen Arnold's avatar
Owen Arnold committed
    alg.setPropertyValue("SpectrumMax", "3");

    doSpectrumMinOrMaxTest(alg, 3);
Owen Arnold's avatar
Owen Arnold committed
  }

  // Saving and reading masking correctly
  void testMasked() {
Owen Arnold's avatar
Owen Arnold committed
    LoadNexusProcessed alg;

    TS_ASSERT_THROWS_NOTHING(alg.initialize());
    TS_ASSERT(alg.isInitialized());
Owen Arnold's avatar
Owen Arnold committed
    testFile = "focussed.nxs";
    alg.setPropertyValue("Filename", testFile);
    testFile = alg.getPropertyValue("Filename");

    alg.setPropertyValue("OutputWorkspace", output_ws);

    TS_ASSERT_THROWS_NOTHING(alg.execute());

Loading
Loading full blame...