/
RemoveMaskedSpectra.cpp
128 lines (107 loc) · 4.5 KB
/
RemoveMaskedSpectra.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "MantidAlgorithms/RemoveMaskedSpectra.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/TextAxis.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/MaskWorkspace.h"
namespace Mantid {
namespace Algorithms {
using namespace Kernel;
using namespace API;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(RemoveMaskedSpectra)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
RemoveMaskedSpectra::RemoveMaskedSpectra() {}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
RemoveMaskedSpectra::~RemoveMaskedSpectra() {}
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string RemoveMaskedSpectra::name() const {
return "RemoveMaskedSpectra";
}
/// Algorithm's version for identification. @see Algorithm::version
int RemoveMaskedSpectra::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string RemoveMaskedSpectra::category() const {
return "Transforms\\Splitting";
}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string RemoveMaskedSpectra::summary() const {
return "Extracts unmasked spectra from a workspace and places them in a new "
"workspace.";
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void RemoveMaskedSpectra::init() {
declareProperty(
make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
"An input workspace.");
declareProperty(make_unique<WorkspaceProperty<>>("MaskedWorkspace", "",
Direction::Input,
PropertyMode::Optional),
"If given but not as a MaskWorkspace, the masking from "
"this workspace will be used. If given as a "
"MaskWorkspace, the masking is read from its Y values.");
declareProperty(make_unique<WorkspaceProperty<>>("OutputWorkspace", "",
Direction::Output),
"An output workspace.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void RemoveMaskedSpectra::exec() {
MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
MatrixWorkspace_sptr maskedWorkspace = getProperty("MaskedWorkspace");
if (!maskedWorkspace) {
maskedWorkspace = inputWorkspace;
} else if (inputWorkspace->getNumberHistograms() !=
maskedWorkspace->getNumberHistograms()) {
throw std::runtime_error(
"Masked workspace has a different number of spectra.");
}
// Find indices of the unmasked spectra.
std::vector<size_t> indices;
makeIndexList(indices, maskedWorkspace.get());
auto extract = createChildAlgorithm("ExtractSpectra", 0, 1);
extract->initialize();
extract->setRethrows(true);
extract->setProperty("InputWorkspace", inputWorkspace);
extract->setProperty("WorkspaceIndexList", indices);
extract->execute();
MatrixWorkspace_sptr outputWorkspace =
extract->getProperty("OutputWorkspace");
setProperty("OutputWorkspace", outputWorkspace);
}
//----------------------------------------------------------------------------------------------
/// Fill in a vector with spectra indices to be extracted.
/// @param indices :: A reference to a vector to fill with the indices.
/// @param maskedWorkspace :: A workspace with masking information.
void RemoveMaskedSpectra::makeIndexList(
std::vector<size_t> &indices, const API::MatrixWorkspace *maskedWorkspace) {
auto mask = dynamic_cast<const DataObjects::MaskWorkspace *>(maskedWorkspace);
if (mask) {
for (size_t i = 0; i < mask->getNumberHistograms(); ++i) {
if (mask->readY(i)[0] == 0.0) {
indices.push_back(static_cast<size_t>(i));
}
}
} else {
for (size_t i = 0; i < maskedWorkspace->getNumberHistograms(); ++i) {
Geometry::IDetector_const_sptr det;
try {
det = maskedWorkspace->getDetector(i);
} catch (Exception::NotFoundError &) {
continue;
}
if (!det->isMasked()) {
indices.push_back(static_cast<size_t>(i));
}
}
}
}
} // namespace Algorithms
} // namespace Mantid