Mantid
Loading...
Searching...
No Matches
EvaluateMDFunction.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 +
14
15namespace Mantid::MDAlgorithms {
16
19
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(EvaluateMDFunction)
22
23//----------------------------------------------------------------------------------------------
26EvaluateMDFunction::EvaluateMDFunction() { useAlgorithm("EvaluateFunction"); }
27
28//----------------------------------------------------------------------------------------------
29
31int EvaluateMDFunction::version() const { return 1; }
32
34const std::string EvaluateMDFunction::category() const { return "MDAlgorithms\\Creation"; }
35
37const std::string EvaluateMDFunction::summary() const { return "Evaluates an MD function on a MD histo workspace."; }
38
39//----------------------------------------------------------------------------------------------
43 declareProperty(std::make_unique<WorkspaceProperty<API::IMDHistoWorkspace>>("InputWorkspace", "", Direction::Input),
44 "An input workspace that provides dimensions for the output.");
45 declareProperty(std::make_unique<API::FunctionProperty>("Function", Direction::InOut),
46 "Parameters defining the fitting function and its initial values");
47 declareProperty(std::make_unique<WorkspaceProperty<API::IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
48 "An output workspace.");
49}
50
51//----------------------------------------------------------------------------------------------
55 API::IMDHistoWorkspace_sptr input = getProperty("InputWorkspace");
56
57 auto cloner = API::AlgorithmManager::Instance().create("CloneMDWorkspace");
58 cloner->initialize();
59 cloner->setChild(true);
60 cloner->setProperty("InputWorkspace", input);
61 cloner->setPropertyValue("OutputWorkspace", "_");
62 cloner->execute();
63
64 API::IMDWorkspace_sptr clone = cloner->getProperty("OutputWorkspace");
65 API::IMDHistoWorkspace_sptr output = std::dynamic_pointer_cast<API::IMDHistoWorkspace>(clone);
66
67 if (!output)
68 throw std::runtime_error("Cannot create output workspace");
69
70 API::IFunction_sptr function = getProperty("Function");
71 function->setWorkspace(output);
72
73 API::FunctionDomainMD domain(output);
74 API::FunctionValues values(domain);
75
76 function->function(domain, values);
77
78 double *data = values.getPointerToCalculated(0);
79 size_t length = values.size();
80 auto outputData = output->mutableSignalArray();
81 std::copy(data, data + length, outputData);
82
83 setProperty("OutputWorkspace", output);
84}
85
86} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Implements a domain for MD functions (IFunctionMD).
A class to store values calculated by a function.
double * getPointerToCalculated(size_t i)
Get a pointer to calculated data at index i.
size_t size() const
Return the number of values.
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
EvaluateMDFunction : TODO: DESCRIPTION.
void exec() override
Execute the algorithm.
const std::string category() const override
Algorithm's category for identification.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
int version() const override
Algorithm's version for identification.
void init() override
Initialize the algorithm's properties.
std::shared_ptr< IMDHistoWorkspace > IMDHistoWorkspace_sptr
shared pointer to Mantid::API::IMDHistoWorkspace
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition: IFunction.h:732
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
Definition: IMDWorkspace.h:146
Describes the direction (within an algorithm) of a Property.
Definition: Property.h:50
@ InOut
Both an input & output workspace.
Definition: Property.h:55
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54