Mantid
Loading...
Searching...
No Matches
BinaryOperationMDTestHelper.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7/*********************************************************************************
8 * PLEASE READ THIS!!!!!!!
9 *
10 * These functions MAY ONLY be used in a test in the MDAlgorithms package.
11 *********************************************************************************/
13
22
23using namespace Mantid;
24using namespace Mantid::MDAlgorithms;
25using namespace Mantid::API;
26using namespace Mantid::DataObjects;
27
29
33 MDHistoWorkspace_sptr histo_masked;
34 MDHistoWorkspace_sptr histo_zero;
39
40 histo_A = MDEventsTestHelper::makeFakeMDHistoWorkspace(2.0, 2, 5, 10.0, 1.0);
41 histo_B = MDEventsTestHelper::makeFakeMDHistoWorkspace(3.0, 2, 5, 10.0, 1.0);
42 histo_masked = MDEventsTestHelper::makeFakeMDHistoWorkspace(2.0, 2, 5, 10.0, 1.0);
43 histo_zero = MDEventsTestHelper::makeFakeMDHistoWorkspace(0.0, 2, 5, 10.0, 0.0);
44 event_A = MDEventsTestHelper::makeMDEW<2>(3, 0.0, 10.0, 1);
45 event_B = MDEventsTestHelper::makeMDEW<2>(3, 0.0, 10.0, 1);
47 AnalysisDataService::Instance().addOrReplace("histo_A", histo_A);
48 AnalysisDataService::Instance().addOrReplace("histo_B", histo_B);
49 AnalysisDataService::Instance().addOrReplace("histo_masked", histo_masked);
50 AnalysisDataService::Instance().addOrReplace("histo_zero", histo_zero);
51 AnalysisDataService::Instance().addOrReplace("event_A", event_A);
52 AnalysisDataService::Instance().addOrReplace("event_B", event_B);
53 AnalysisDataService::Instance().addOrReplace("scalar", scalar);
54
55 FrameworkManager::Instance().exec("MaskMD", 6, "Workspace", "histo_masked", "Dimensions", "x,y", "Extents",
56 "0,10,0,10");
57}
58
60MDHistoWorkspace_sptr doTest(const std::string &algoName, const std::string &lhs, const std::string &rhs,
61 const std::string &outName, bool succeeds, const std::string &otherProp,
62 const std::string &otherPropValue) {
64
65 auto alg = AlgorithmManager::Instance().create(algoName);
66 alg->initialize();
67 alg->setPropertyValue("LHSWorkspace", lhs);
68 alg->setPropertyValue("RHSWorkspace", rhs);
69 alg->setPropertyValue("OutputWorkspace", outName);
70 if (!otherProp.empty())
71 alg->setPropertyValue(otherProp, otherPropValue);
72 alg->execute();
73 if (succeeds) {
74 if (!alg->isExecuted())
75 throw std::runtime_error("Algorithm " + algoName + " did not succeed.");
76 IMDWorkspace_sptr out = std::dynamic_pointer_cast<IMDWorkspace>(AnalysisDataService::Instance().retrieve(outName));
77 if (!out)
78 throw std::runtime_error("Algorithm " + algoName + " did not create the output workspace.");
79 return std::dynamic_pointer_cast<MDHistoWorkspace>(out);
80 } else {
81 if (alg->isExecuted())
82 throw std::runtime_error("Algorithm " + algoName + " did not fail as expected.");
83 return (MDHistoWorkspace_sptr());
84 }
85}
86
87} // namespace BinaryOperationMDTestHelper
88
90
91MDHistoWorkspace_sptr doTest(const std::string &algoName, const std::string &inName, const std::string &outName,
92 bool succeeds, const std::string &otherProp, const std::string &otherPropValue) {
94 IMDEventWorkspace_sptr event = MDEventsTestHelper::makeMDEW<2>(3, 0.0, 10.0, 1);
96 AnalysisDataService::Instance().addOrReplace("histo", histo);
97 AnalysisDataService::Instance().addOrReplace("event", event);
98 AnalysisDataService::Instance().addOrReplace("scalar", scalar);
99
100 auto alg = AlgorithmManager::Instance().create(algoName);
101 alg->initialize();
102 alg->setPropertyValue("InputWorkspace", inName);
103 alg->setPropertyValue("OutputWorkspace", outName);
104 if (!otherProp.empty())
105 alg->setPropertyValue(otherProp, otherPropValue);
106 alg->execute();
107 if (succeeds) {
108 if (!alg->isExecuted())
109 throw std::runtime_error("Algorithm " + algoName + " did not succeed.");
110 IMDWorkspace_sptr out = std::dynamic_pointer_cast<IMDWorkspace>(AnalysisDataService::Instance().retrieve(outName));
111 if (!out)
112 throw std::runtime_error("Algorithm " + algoName + " did not create the output workspace.");
113 return std::dynamic_pointer_cast<MDHistoWorkspace>(out);
114 } else {
115 if (alg->isExecuted())
116 throw std::runtime_error("Algorithm " + algoName + " did not fail as expected.");
117 return (MDHistoWorkspace_sptr());
118 }
119}
120} // namespace UnaryOperationMDTestHelper
const std::vector< double > & rhs
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
DLLExport Mantid::DataObjects::MDHistoWorkspace_sptr doTest(const std::string &algoName, const std::string &lhs, const std::string &rhs, const std::string &outName, bool succeeds=true, const std::string &otherProp="", const std::string &otherPropValue="")
Run a binary algorithm.
std::shared_ptr< IMDEventWorkspace > IMDEventWorkspace_sptr
Shared pointer to Mantid::API::IMDEventWorkspace.
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
Definition: IMDWorkspace.h:146
static MatrixWorkspace_sptr createWorkspaceSingleValue(const double &rhsValue)
Creates a temporary single value workspace the error is set to zero.
MDHistoWorkspace_sptr makeFakeMDHistoWorkspace(double signal, size_t numDims, size_t numBins=10, coord_t max=10.0, double errorSquared=1.0, const std::string &name="", double numEvents=1.0)
Make a fake n-dimensional MDHistoWorkspace.
std::shared_ptr< WorkspaceSingleValue > WorkspaceSingleValue_sptr
shared pointer to the WorkspaceSingleValue class
std::shared_ptr< MDHistoWorkspace > MDHistoWorkspace_sptr
A shared pointer to a MDHistoWorkspace.
Helper class which provides the Collimation Length for SANS instruments.
DLLExport Mantid::DataObjects::MDHistoWorkspace_sptr doTest(const std::string &algoName, const std::string &inName, const std::string &outName, bool succeeds=true, const std::string &otherProp="", const std::string &otherPropValue="")
Run a unary algorithm.