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 +
12#include "MantidAPI/IFunction.h"
15
16namespace Mantid::MDAlgorithms {
17
20
21// Register the algorithm into the AlgorithmFactory
22DECLARE_ALGORITHM(EvaluateMDFunction)
23
24//----------------------------------------------------------------------------------------------
27EvaluateMDFunction::EvaluateMDFunction() { useAlgorithm("EvaluateFunction"); }
28
29//----------------------------------------------------------------------------------------------
30
32int EvaluateMDFunction::version() const { return 1; }
33
35const std::string EvaluateMDFunction::category() const { return "MDAlgorithms\\Creation"; }
36
38const std::string EvaluateMDFunction::summary() const { return "Evaluates an MD function on a MD histo workspace."; }
39
40//----------------------------------------------------------------------------------------------
44 declareProperty(std::make_unique<WorkspaceProperty<API::IMDHistoWorkspace>>("InputWorkspace", "", Direction::Input),
45 "An input workspace that provides dimensions for the output.");
46 declareProperty(std::make_unique<API::FunctionProperty>("Function", Direction::InOut),
47 "Parameters defining the fitting function and its initial values");
48 declareProperty(std::make_unique<WorkspaceProperty<API::IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
49 "An output workspace.");
50}
51
52//----------------------------------------------------------------------------------------------
56 API::IMDHistoWorkspace_sptr input = getProperty("InputWorkspace");
57
58 auto cloner = API::AlgorithmManager::Instance().create("CloneMDWorkspace");
59 cloner->initialize();
60 cloner->setChild(true);
61 cloner->setProperty("InputWorkspace", input);
62 cloner->setPropertyValue("OutputWorkspace", "_");
63 cloner->execute();
64
65 API::IMDWorkspace_sptr clone = cloner->getProperty("OutputWorkspace");
66 API::IMDHistoWorkspace_sptr output = std::dynamic_pointer_cast<API::IMDHistoWorkspace>(clone);
67
68 if (!output)
69 throw std::runtime_error("Cannot create output workspace");
70
71 API::IFunction_sptr function = getProperty("Function");
72 function->setWorkspace(output);
73
74 API::FunctionDomainMD domain(output);
75 API::FunctionValues values(domain);
76
77 function->function(domain, values);
78
79 double *data = values.getPointerToCalculated(0);
80 size_t length = values.size();
81 auto outputData = output->mutableSignalArray();
82 std::copy(data, data + length, outputData);
83
84 setProperty("OutputWorkspace", output);
85}
86
87} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:547
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
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.
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:749
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
Describes the direction (within an algorithm) of a Property.
Definition Property.h:51
@ InOut
Both an input & output workspace.
Definition Property.h:56
@ Input
An input workspace.
Definition Property.h:54
@ Output
An output workspace.
Definition Property.h:55