Mantid
Loading...
Searching...
No Matches
RemoveSpectra.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 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 +
16#include "MantidIndexing/SpectrumIndexSet.h"
18
19namespace Mantid::Algorithms {
20
21using namespace Mantid::API;
22using namespace Mantid::Kernel;
23using namespace Mantid::DataObjects;
24using namespace Mantid::Indexing;
25
26DECLARE_ALGORITHM(RemoveSpectra)
27
28void RemoveSpectra::init() {
29 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input));
30 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output));
31 declareProperty(std::make_unique<ArrayProperty<size_t>>("WorkspaceIndices", Direction::Input),
32 "A comma-separated list of individual workspace indices to remove");
33 declareProperty("RemoveMaskedSpectra", false,
34 "Whether or not to remove spectra that have been masked from "
35 "the inputworkspace.",
37 declareProperty("RemoveSpectraWithNoDetector", false,
38 "Whether or not to remove spectra that have no attached detector.", Direction::Input);
39}
40
41std::map<std::string, std::string> RemoveSpectra::validateInputs() {
42 std::map<std::string, std::string> errors;
43 const MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
44 const auto workspace2D = std::dynamic_pointer_cast<const Workspace2D>(inputWS);
45 const auto eventWS = std::dynamic_pointer_cast<const EventWorkspace>(inputWS);
46 if (!workspace2D && !eventWS) {
47 errors.insert(std::make_pair("InputWorkspace", "A none-Workspace2D or EventWorkspace has been provided."));
48 }
49
50 const std::vector<size_t> indexList = getProperty("WorkspaceIndices");
51 for (const auto &index : indexList) {
52 // index >= 0 is assumed as it should be a const long unsinged int
53 if (index >= inputWS->getNumberHistograms()) {
54 errors.insert(std::make_pair("WorkspaceIndices", "Passed Workspace Index: " + std::to_string(index) +
55 " is not valid for passed InputWorkspace"));
56 }
57 }
58
59 return errors;
60}
61
62namespace {
63std::vector<size_t> discoverSpectraWithNoDetector(const MatrixWorkspace_sptr &inputWS) {
64 std::vector<size_t> specIDs;
65 const auto &spectrumInfo = inputWS->spectrumInfo();
66 for (auto i = 0u; i < inputWS->getNumberHistograms(); ++i) {
67 if (!spectrumInfo.hasDetectors(i))
68 specIDs.emplace_back(i);
69 }
70 return specIDs;
71}
72
73std::vector<size_t> discoverSpectraWithMask(const MatrixWorkspace_sptr &inputWS) {
74 std::vector<size_t> specIDs;
75 const auto &spectrumInfo = inputWS->spectrumInfo();
76 for (auto i = 0u; i < inputWS->getNumberHistograms(); ++i) {
77 if (!spectrumInfo.hasDetectors(i))
78 continue;
79 if (spectrumInfo.isMasked(i)) {
80 specIDs.emplace_back(i);
81 }
82 }
83 return specIDs;
84}
85
86template <class T> bool evaluateIfSpectrumIsInList(std::vector<size_t> &specList, T spectrum) {
87 const auto it = std::find(specList.begin(), specList.end(), spectrum->getSpectrumNo());
88 return it != specList.end();
89}
90} // namespace
91
93 const MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
94 std::vector<size_t> specList = getProperty("WorkspaceIndices");
95 const bool removeMaskedSpectra = getProperty("RemoveMaskedSpectra");
96 const bool removeSpectraWithNoDetector = getProperty("RemoveSpectraWithNoDetector");
97
98 if (specList.empty() && removeMaskedSpectra && removeSpectraWithNoDetector) {
99 g_log.warning("Nothing passed to the RemoveSpectra algorithm to remove so "
100 "nothing happened");
101 setProperty("OutputWorkspace", inputWS);
102 return;
103 }
104
105 if (removeMaskedSpectra) {
106 const auto extraSpectra = discoverSpectraWithMask(inputWS);
107 specList.insert(specList.end(), extraSpectra.begin(), extraSpectra.end());
108 }
109
110 if (removeSpectraWithNoDetector) {
111 const auto extraSpectra = discoverSpectraWithNoDetector(inputWS);
112 specList.insert(specList.end(), extraSpectra.begin(), extraSpectra.end());
113 }
114
115 if (specList.empty()) {
116 g_log.debug("No spectra to delete in RemoveSpectra");
117 setProperty("OutputWorkspace", inputWS);
118 return;
119 }
120
121 auto outputWS = copySpectraFromInputToOutput(inputWS, specList);
122
123 g_log.debug(std::to_string(specList.size()) + " spectra removed.");
124
125 setProperty("OutputWorkspace", outputWS);
126}
127
129 const std::vector<size_t> &specList) {
130 std::vector<size_t> indicesToExtract;
131 for (size_t i = 0; i < inputWS->getNumberHistograms(); ++i) {
132 if (std::find(specList.begin(), specList.end(), i) == specList.end()) {
133 indicesToExtract.emplace_back(i);
134 }
135 }
136
137 if (indicesToExtract.empty()) {
138 return inputWS;
139 }
140
141 auto extractSpectra = createChildAlgorithm("ExtractSpectra");
142 extractSpectra->setProperty("InputWorkspace", inputWS);
143 extractSpectra->setProperty("WorkspaceIndexList", indicesToExtract);
144 extractSpectra->executeAsChildAlg();
145
146 MatrixWorkspace_sptr outputWS = extractSpectra->getProperty("OutputWorkspace");
147 return outputWS;
148}
149} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
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
Kernel::Logger & g_log
Definition: Algorithm.h:451
A property class for workspaces.
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
void exec() override
Virtual method - must be overridden by concrete algorithm.
MatrixWorkspace_sptr copySpectraFromInputToOutput(MatrixWorkspace_sptr inputWS, const std::vector< size_t > &specList)
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54