Mantid
Loading...
Searching...
No Matches
RemoveMaskedSpectra.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 +
8
11#include "MantidAPI/TextAxis.h"
13
14namespace Mantid::Algorithms {
15
16using namespace Kernel;
17using namespace API;
18
19// Register the algorithm into the AlgorithmFactory
20DECLARE_ALGORITHM(RemoveMaskedSpectra)
21
22
23const std::string RemoveMaskedSpectra::name() const { return "RemoveMaskedSpectra"; }
24
26int RemoveMaskedSpectra::version() const { return 1; }
27
29const std::string RemoveMaskedSpectra::category() const { return "Transforms\\Splitting"; }
30
32const std::string RemoveMaskedSpectra::summary() const {
33 return "Extracts unmasked spectra from a workspace and places them in a new "
34 "workspace.";
35}
36
40 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input), "An input workspace.");
42 std::make_unique<WorkspaceProperty<>>("MaskedWorkspace", "", Direction::Input, PropertyMode::Optional),
43 "If given but not as a MaskWorkspace, the masking from "
44 "this workspace will be used. If given as a "
45 "MaskWorkspace, the masking is read from its Y values.");
46 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
47 "An output workspace.");
48}
49
53 MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
54 MatrixWorkspace_sptr maskedWorkspace = getProperty("MaskedWorkspace");
55
56 if (!maskedWorkspace) {
57 maskedWorkspace = inputWorkspace;
58 } else if (inputWorkspace->getNumberHistograms() != maskedWorkspace->getNumberHistograms()) {
59 throw std::runtime_error("Masked workspace has a different number of spectra.");
60 }
61
62 // Find indices of the unmasked spectra.
63 std::vector<size_t> indices;
64 makeIndexList(indices, maskedWorkspace.get());
65
66 auto extract = createChildAlgorithm("ExtractSpectra", 0, 1);
67 extract->initialize();
68 extract->setRethrows(true);
69
70 extract->setProperty("InputWorkspace", inputWorkspace);
71 extract->setProperty("WorkspaceIndexList", indices);
72
73 extract->execute();
74
75 MatrixWorkspace_sptr outputWorkspace = extract->getProperty("OutputWorkspace");
76 setProperty("OutputWorkspace", outputWorkspace);
77}
78
82void RemoveMaskedSpectra::makeIndexList(std::vector<size_t> &indices, const API::MatrixWorkspace *maskedWorkspace) {
83 auto mask = dynamic_cast<const DataObjects::MaskWorkspace *>(maskedWorkspace);
84 if (mask) {
85 for (size_t i = 0; i < mask->getNumberHistograms(); ++i) {
86 if (mask->y(i)[0] == 0.0) {
87 indices.emplace_back(i);
88 }
89 }
90 } else {
91 const auto &spectrumInfo = maskedWorkspace->spectrumInfo();
92 for (size_t i = 0; i < maskedWorkspace->getNumberHistograms(); ++i) {
93 if (!spectrumInfo.hasDetectors(i))
94 continue;
95 if (!spectrumInfo.isMasked(i))
96 indices.emplace_back(i);
97 }
98 }
99}
100
101} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
const SpectrumInfo & spectrumInfo() const
Return a reference to the SpectrumInfo object.
Base MatrixWorkspace Abstract Class.
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
A property class for workspaces.
RemoveMaskedSpectra removes all masked spectra.
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 makeIndexList(std::vector< size_t > &indices, const API::MatrixWorkspace *maskedWorkspace)
Fill in a vector with spectra indices to be extracted.
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.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54