Mantid
Loading...
Searching...
No Matches
UnaryOperationMD.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 +
13#include "MantidKernel/System.h"
14
15using namespace Mantid::Kernel;
16using namespace Mantid::API;
17using namespace Mantid::DataObjects;
18
19namespace Mantid::MDAlgorithms {
20
21//----------------------------------------------------------------------------------------------
23const std::string UnaryOperationMD::name() const { return "UnaryOperationMD"; }
24
26int UnaryOperationMD::version() const { return 1; }
27
29const std::string UnaryOperationMD::category() const { return "MDAlgorithms\\MDArithmetic"; }
30
31//----------------------------------------------------------------------------------------------
36 "A MDEventWorkspace or MDHistoWorkspace on which to apply "
37 "the operation.");
39 "Name of the output MDEventWorkspace or MDHistoWorkspace.");
40 this->initExtraProperties();
41}
42
43//----------------------------------------------------------------------------------------------
46
47//----------------------------------------------------------------------------------------------
51 // Get the properties
54
55 // For MatrixWorkspace's ...
56 if (std::dynamic_pointer_cast<MatrixWorkspace>(m_in)) {
57 // Pass-through to the same function without "MD"
58 std::string matrixAlg = this->name();
59 matrixAlg = matrixAlg.substr(0, matrixAlg.size() - 2);
60 auto alg = createChildAlgorithm(matrixAlg);
61 // Copy all properties from THIS to the non-MD version
62 std::vector<Property *> props = this->getProperties();
63 for (auto prop : props) {
64 alg->setPropertyValue(prop->name(), prop->value());
65 }
66 alg->execute();
67 // Copy the output too
68 MatrixWorkspace_sptr outMW = alg->getProperty("OutputWorkspace");
69 IMDWorkspace_sptr out = std::dynamic_pointer_cast<IMDWorkspace>(outMW);
70 setProperty("OutputWorkspace", out);
71 return;
72 }
73
74 // Check for validity
75 m_in_event = std::dynamic_pointer_cast<IMDEventWorkspace>(m_in);
76 m_in_histo = std::dynamic_pointer_cast<MDHistoWorkspace>(m_in);
77 this->checkInputs();
78
79 if (m_out != m_in) {
80 // B = f(A) -> So first we clone A (lhs) into B
81 auto clone = createChildAlgorithm("CloneMDWorkspace", 0.0, 0.5, true);
82 clone->setProperty("InputWorkspace", m_in);
83 clone->executeAsChildAlg();
84 m_out = clone->getProperty("OutputWorkspace");
85 }
86
87 // Okay, at this point we are ready to do, e.g.,
88 // "log(m_out)"
89 if (!m_out)
90 throw std::runtime_error("Error creating the output workspace");
91
92 IMDEventWorkspace_sptr m_out_event = std::dynamic_pointer_cast<IMDEventWorkspace>(m_out);
93 MDHistoWorkspace_sptr m_out_histo = std::dynamic_pointer_cast<MDHistoWorkspace>(m_out);
94
95 // Call the appropriate sub-function
96 if (m_out_event)
97 this->execEvent(m_out_event);
98 else if (m_out_histo)
99 this->execHisto(m_out_histo);
100 else {
101 throw std::runtime_error("Unexpected output workspace type. Expected MDEventWorkspace or "
102 "MDHistoWorkspace, got " +
103 m_out->id());
104 }
105
106 // Give the output
107 setProperty("OutputWorkspace", m_out);
108}
109
110} // namespace Mantid::MDAlgorithms
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
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
const std::vector< Kernel::Property * > & getProperties() const override
Get the list of managed properties.
Definition: Algorithm.cpp:2050
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
Mantid::API::IMDWorkspace_sptr m_out
Output workspace.
const std::string category() const override
Algorithm's category for identification.
virtual void execHisto(Mantid::DataObjects::MDHistoWorkspace_sptr out)=0
Run the algorithm with a MDHistoWorkspace.
virtual const std::string outputPropName() const
The name of the output workspace property.
virtual void execEvent(Mantid::API::IMDEventWorkspace_sptr out)=0
Run the algorithm on a MDEventWorkspace.
virtual void initExtraProperties()
Optional method to be subclassed to add properties.
int version() const override
Algorithm's version for identification.
virtual void checkInputs()=0
Check the inputs and throw if the algorithm cannot be run.
Mantid::API::IMDEventWorkspace_sptr m_in_event
Input workspace (MDEvent)
virtual const std::string inputPropName() const
The name of the input workspace property.
const std::string name() const override
Algorithm's name for identification.
void init() override
Initialize the algorithm's properties.
void exec() override
Execute the algorithm.
Mantid::DataObjects::MDHistoWorkspace_sptr m_in_histo
Input workspace (MDHisto)
Mantid::API::IMDWorkspace_sptr m_in
Input workspace.
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
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< MDHistoWorkspace > MDHistoWorkspace_sptr
A shared pointer to a MDHistoWorkspace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54