-
Notifications
You must be signed in to change notification settings - Fork 122
/
SetMDUsingMask.cpp
113 lines (92 loc) · 5.01 KB
/
SetMDUsingMask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidMDAlgorithms/SetMDUsingMask.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidAPI/IMDWorkspace.h"
#include "MantidDataObjects/MDHistoWorkspace.h"
#include "MantidKernel/System.h"
#include <cfloat>
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
namespace Mantid::MDAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SetMDUsingMask)
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string SetMDUsingMask::name() const { return "SetMDUsingMask"; }
/// Algorithm's version for identification. @see Algorithm::version
int SetMDUsingMask::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string SetMDUsingMask::category() const { return "MDAlgorithms\\MDArithmetic"; }
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void SetMDUsingMask::init() {
declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("InputWorkspace", "", Direction::Input),
"An input MDHistoWorkspace.");
declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("MaskWorkspace", "", Direction::Input),
"A mask MDHistoWorkspace, where true indicates where to set the value.");
declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("ValueWorkspace", "", Direction::Input,
PropertyMode::Optional),
"Workspace to copy to the output workspace over the input. Optional - "
"specify this or Value.");
declareProperty("Value", DBL_MAX,
"Single number to set in the output "
"workspace. Optional - specify this or "
"ValueWorkspace");
declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
"An output MDHistoWorkspace.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void SetMDUsingMask::exec() {
IMDHistoWorkspace_sptr inIWS = getProperty("InputWorkspace");
IMDHistoWorkspace_sptr maskIWS = getProperty("MaskWorkspace");
IMDHistoWorkspace_sptr outIWS = getProperty("OutputWorkspace");
IMDHistoWorkspace_sptr valueIWS = getProperty("ValueWorkspace");
double value = getProperty("Value");
bool useValueWS = (value == DBL_MAX);
if (useValueWS && !valueIWS)
throw std::invalid_argument("You must specify either ValueWorkspace or Value.");
if (!useValueWS && valueIWS)
throw std::invalid_argument("You must specify either ValueWorkspace or Value, not both!");
if (maskIWS->getNumDims() != inIWS->getNumDims())
throw std::invalid_argument("Input and Mask workspace need to have the same number of dimensions.");
if (maskIWS->getNPoints() != inIWS->getNPoints())
throw std::invalid_argument("Input and Mask workspace need to have the same number of points.");
if (valueIWS) {
if (maskIWS->getNumDims() != valueIWS->getNumDims())
throw std::invalid_argument("Input and Value workspace need to have the "
"same number of dimensions.");
if (maskIWS->getNPoints() != valueIWS->getNPoints())
throw std::invalid_argument("Input and Value workspace need to have the same number of points.");
}
if (outIWS != inIWS) {
// Not in-place. So clone the input to the output
auto clone = createChildAlgorithm("CloneMDWorkspace", 0.0, 0.5, true);
clone->setProperty("InputWorkspace", std::dynamic_pointer_cast<IMDWorkspace>(inIWS));
clone->executeAsChildAlg();
IMDWorkspace_sptr temp = clone->getProperty("OutputWorkspace");
outIWS = std::dynamic_pointer_cast<IMDHistoWorkspace>(temp);
}
MDHistoWorkspace_sptr outWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outIWS);
MDHistoWorkspace_sptr maskWS = std::dynamic_pointer_cast<MDHistoWorkspace>(maskIWS);
MDHistoWorkspace_sptr valueWS = std::dynamic_pointer_cast<MDHistoWorkspace>(valueIWS);
if (!outWS || !maskWS)
throw std::runtime_error("Error creating output workspace.");
// Set either using the WS or the single value
if (useValueWS)
outWS->setUsingMask(*maskWS, *valueWS);
else
outWS->setUsingMask(*maskWS, value, 0.0 /* assume zero error */);
// Save the output
setProperty("OutputWorkspace", outIWS);
}
} // namespace Mantid::MDAlgorithms