Mantid
Loading...
Searching...
No Matches
SetMDUsingMask.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 +
11#include <cfloat>
12
13using namespace Mantid::Kernel;
14using namespace Mantid::API;
15using namespace Mantid::DataObjects;
16
17namespace Mantid::MDAlgorithms {
18
19// Register the algorithm into the AlgorithmFactory
20DECLARE_ALGORITHM(SetMDUsingMask)
21
22//----------------------------------------------------------------------------------------------
24const std::string SetMDUsingMask::name() const { return "SetMDUsingMask"; }
25
27int SetMDUsingMask::version() const { return 1; }
28
30const std::string SetMDUsingMask::category() const { return "MDAlgorithms\\MDArithmetic"; }
31
32//----------------------------------------------------------------------------------------------
33
34//----------------------------------------------------------------------------------------------
38 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("InputWorkspace", "", Direction::Input),
39 "An input MDHistoWorkspace.");
40 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("MaskWorkspace", "", Direction::Input),
41 "A mask MDHistoWorkspace, where true indicates where to set the value.");
42
43 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("ValueWorkspace", "", Direction::Input,
45 "Workspace to copy to the output workspace over the input. Optional - "
46 "specify this or Value.");
47
48 declareProperty("Value", DBL_MAX,
49 "Single number to set in the output "
50 "workspace. Optional - specify this or "
51 "ValueWorkspace");
52
53 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
54 "An output MDHistoWorkspace.");
55}
56
57//----------------------------------------------------------------------------------------------
61 IMDHistoWorkspace_sptr inIWS = getProperty("InputWorkspace");
62 IMDHistoWorkspace_sptr maskIWS = getProperty("MaskWorkspace");
63 IMDHistoWorkspace_sptr outIWS = getProperty("OutputWorkspace");
64 IMDHistoWorkspace_sptr valueIWS = getProperty("ValueWorkspace");
65 double value = getProperty("Value");
66
67 bool useValueWS = (value == DBL_MAX);
68
69 if (useValueWS && !valueIWS)
70 throw std::invalid_argument("You must specify either ValueWorkspace or Value.");
71 if (!useValueWS && valueIWS)
72 throw std::invalid_argument("You must specify either ValueWorkspace or Value, not both!");
73
74 if (maskIWS->getNumDims() != inIWS->getNumDims())
75 throw std::invalid_argument("Input and Mask workspace need to have the same number of dimensions.");
76 if (maskIWS->getNPoints() != inIWS->getNPoints())
77 throw std::invalid_argument("Input and Mask workspace need to have the same number of points.");
78 if (valueIWS) {
79 if (maskIWS->getNumDims() != valueIWS->getNumDims())
80 throw std::invalid_argument("Input and Value workspace need to have the "
81 "same number of dimensions.");
82 if (maskIWS->getNPoints() != valueIWS->getNPoints())
83 throw std::invalid_argument("Input and Value workspace need to have the same number of points.");
84 }
85
86 if (outIWS != inIWS) {
87 // Not in-place. So clone the input to the output
88 auto clone = createChildAlgorithm("CloneMDWorkspace", 0.0, 0.5, true);
89 clone->setProperty("InputWorkspace", std::dynamic_pointer_cast<IMDWorkspace>(inIWS));
90 clone->executeAsChildAlg();
91 IMDWorkspace_sptr temp = clone->getProperty("OutputWorkspace");
92 outIWS = std::dynamic_pointer_cast<IMDHistoWorkspace>(temp);
93 }
94
95 MDHistoWorkspace_sptr outWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outIWS);
96 MDHistoWorkspace_sptr maskWS = std::dynamic_pointer_cast<MDHistoWorkspace>(maskIWS);
97 MDHistoWorkspace_sptr valueWS = std::dynamic_pointer_cast<MDHistoWorkspace>(valueIWS);
98
99 if (!outWS || !maskWS)
100 throw std::runtime_error("Error creating output workspace.");
101
102 // Set either using the WS or the single value
103 if (useValueWS)
104 outWS->setUsingMask(*maskWS, *valueWS);
105 else
106 outWS->setUsingMask(*maskWS, value, 0.0 /* assume zero error */);
107
108 // Save the output
109 setProperty("OutputWorkspace", outIWS);
110}
111
112} // namespace Mantid::MDAlgorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
double value
The value of the point.
Definition FitMW.cpp:51
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.
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.
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
Algorithm to set a MDHistoWorkspace in points determined by a mask boolean MDHistoWorkspace.
void init() override
Initialize the algorithm's properties.
const std::string category() const override
Algorithm's category for identification.
void exec() override
Execute the algorithm.
int version() const override
Algorithm's version for identification.
std::shared_ptr< IMDHistoWorkspace > IMDHistoWorkspace_sptr
shared pointer to Mantid::API::IMDHistoWorkspace
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
std::shared_ptr< MDHistoWorkspace > MDHistoWorkspace_sptr
A shared pointer to a MDHistoWorkspace.
STL namespace.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54