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 "MantidKernel/System.h"
12#include <cfloat>
13
14using namespace Mantid::Kernel;
15using namespace Mantid::API;
16using namespace Mantid::DataObjects;
17
18namespace Mantid::MDAlgorithms {
19
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(SetMDUsingMask)
22
23//----------------------------------------------------------------------------------------------
25const std::string SetMDUsingMask::name() const { return "SetMDUsingMask"; }
26
28int SetMDUsingMask::version() const { return 1; }
29
31const std::string SetMDUsingMask::category() const { return "MDAlgorithms\\MDArithmetic"; }
32
33//----------------------------------------------------------------------------------------------
34
35//----------------------------------------------------------------------------------------------
39 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("InputWorkspace", "", Direction::Input),
40 "An input MDHistoWorkspace.");
41 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("MaskWorkspace", "", Direction::Input),
42 "A mask MDHistoWorkspace, where true indicates where to set the value.");
43
44 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("ValueWorkspace", "", Direction::Input,
46 "Workspace to copy to the output workspace over the input. Optional - "
47 "specify this or Value.");
48
49 declareProperty("Value", DBL_MAX,
50 "Single number to set in the output "
51 "workspace. Optional - specify this or "
52 "ValueWorkspace");
53
54 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
55 "An output MDHistoWorkspace.");
56}
57
58//----------------------------------------------------------------------------------------------
62 IMDHistoWorkspace_sptr inIWS = getProperty("InputWorkspace");
63 IMDHistoWorkspace_sptr maskIWS = getProperty("MaskWorkspace");
64 IMDHistoWorkspace_sptr outIWS = getProperty("OutputWorkspace");
65 IMDHistoWorkspace_sptr valueIWS = getProperty("ValueWorkspace");
66 double value = getProperty("Value");
67
68 bool useValueWS = (value == DBL_MAX);
69
70 if (useValueWS && !valueIWS)
71 throw std::invalid_argument("You must specify either ValueWorkspace or Value.");
72 if (!useValueWS && valueIWS)
73 throw std::invalid_argument("You must specify either ValueWorkspace or Value, not both!");
74
75 if (maskIWS->getNumDims() != inIWS->getNumDims())
76 throw std::invalid_argument("Input and Mask workspace need to have the same number of dimensions.");
77 if (maskIWS->getNPoints() != inIWS->getNPoints())
78 throw std::invalid_argument("Input and Mask workspace need to have the same number of points.");
79 if (valueIWS) {
80 if (maskIWS->getNumDims() != valueIWS->getNumDims())
81 throw std::invalid_argument("Input and Value workspace need to have the "
82 "same number of dimensions.");
83 if (maskIWS->getNPoints() != valueIWS->getNPoints())
84 throw std::invalid_argument("Input and Value workspace need to have the same number of points.");
85 }
86
87 if (outIWS != inIWS) {
88 // Not in-place. So clone the input to the output
89 auto clone = createChildAlgorithm("CloneMDWorkspace", 0.0, 0.5, true);
90 clone->setProperty("InputWorkspace", std::dynamic_pointer_cast<IMDWorkspace>(inIWS));
91 clone->executeAsChildAlg();
92 IMDWorkspace_sptr temp = clone->getProperty("OutputWorkspace");
93 outIWS = std::dynamic_pointer_cast<IMDHistoWorkspace>(temp);
94 }
95
96 MDHistoWorkspace_sptr outWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outIWS);
97 MDHistoWorkspace_sptr maskWS = std::dynamic_pointer_cast<MDHistoWorkspace>(maskIWS);
98 MDHistoWorkspace_sptr valueWS = std::dynamic_pointer_cast<MDHistoWorkspace>(valueIWS);
99
100 if (!outWS || !maskWS)
101 throw std::runtime_error("Error creating output workspace.");
102
103 // Set either using the WS or the single value
104 if (useValueWS)
105 outWS->setUsingMask(*maskWS, *valueWS);
106 else
107 outWS->setUsingMask(*maskWS, value, 0.0 /* assume zero error */);
108
109 // Save the output
110 setProperty("OutputWorkspace", outIWS);
111}
112
113} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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.
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
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.
Definition: IMDWorkspace.h:146
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