Mantid
Loading...
Searching...
No Matches
ExtractUnmaskedSpectra.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 +
10
11namespace Mantid::Algorithms {
12
15
16// Register the algorithm into the AlgorithmFactory
17DECLARE_ALGORITHM(ExtractUnmaskedSpectra)
18
19//----------------------------------------------------------------------------------------------
20
21
22const std::string ExtractUnmaskedSpectra::name() const { return "ExtractUnmaskedSpectra"; }
23
25int ExtractUnmaskedSpectra::version() const { return 1; }
26
28const std::string ExtractUnmaskedSpectra::category() const { return "Transforms\\Splitting"; }
29
31const std::string ExtractUnmaskedSpectra::summary() const {
32 return "Extracts unmasked spectra from a workspace and places them in a new "
33 "workspace.";
34}
35
36//----------------------------------------------------------------------------------------------
40 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input), "An input workspace.");
42 std::make_unique<WorkspaceProperty<>>("MaskWorkspace", "", Direction::Input, API::PropertyMode::Optional),
43 "An optional mask workspace.");
44 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
45 "An output workspace.");
46}
47
48//----------------------------------------------------------------------------------------------
52 // Get the input workspace
53 API::MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
54 // Get the masked workspace (optional).
55 API::MatrixWorkspace_sptr maskedWorkspace = getProperty("MaskWorkspace");
56
57 // Define the mask
59 if (maskedWorkspace) {
60 if (std::dynamic_pointer_cast<API::IMaskWorkspace>(maskedWorkspace)) {
61 mask = maskedWorkspace;
62 } else {
63 auto extractMask = createChildAlgorithm("ExtractMask");
64 extractMask->setProperty("InputWorkspace", maskedWorkspace);
65 extractMask->executeAsChildAlg();
66 mask = extractMask->getProperty("OutputWorkspace");
67 }
68 } else {
69 auto extractMask = createChildAlgorithm("ExtractMask");
70 extractMask->setProperty("InputWorkspace", inputWorkspace);
71 extractMask->executeAsChildAlg();
72 mask = extractMask->getProperty("OutputWorkspace");
73 }
74
75 std::vector<size_t> indicesToExtract;
76 auto nSpectra = inputWorkspace->getNumberHistograms();
77 indicesToExtract.reserve(nSpectra);
78
79 // Find the unmasked spectra
80 for (size_t index = 0; index < nSpectra; ++index) {
81 if (mask->readY(index)[0] < 1.0) {
82 indicesToExtract.emplace_back(index);
83 }
84 }
85
86 // Extract the unmasked spectra.
87 auto extractSpectra = createChildAlgorithm("ExtractSpectra");
88 extractSpectra->setProperty("InputWorkspace", inputWorkspace);
89 extractSpectra->setProperty("WorkspaceIndexList", indicesToExtract);
90 extractSpectra->executeAsChildAlg();
91
92 // Store the output
93 API::MatrixWorkspace_sptr outputWorkspace = extractSpectra->getProperty("OutputWorkspace");
94 setProperty("OutputWorkspace", outputWorkspace);
95}
96
97} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
int nSpectra
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.
ExtractUnmaskedSpectra : TODO: DESCRIPTION.
int version() const override
Algorithm's version for identification.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
const std::string category() const override
Algorithm's category for identification.
void exec() override
Execute the algorithm.
void init() override
Initialize the algorithm's properties.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
STL namespace.
Describes the direction (within an algorithm) of a Property.
Definition: Property.h:50
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54