Mantid
Loading...
Searching...
No Matches
ExtractMask.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 +
14
15namespace Mantid::Algorithms {
16
17// Register the algorithm into the AlgorithmFactory
18DECLARE_ALGORITHM(ExtractMask)
19
21using Kernel::Direction;
22using namespace API;
23using namespace Kernel;
24
29 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
30 "A workspace whose masking is to be extracted");
31 declareProperty("UngroupDetectors", false, "If true, the spectra will be ungrouped and masked individually.");
32 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
33 "A workspace containing the masked spectra as zeroes and ones.");
34
36 std::make_unique<ArrayProperty<detid_t>>("DetectorList", std::make_shared<NullValidator>(), Direction::Output),
37 "A comma separated list or array containing a list of masked "
38 "detector ID's");
39}
40
45 MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
46
47 // convert input to a mask workspace
48 auto inputMaskWS = std::dynamic_pointer_cast<const DataObjects::MaskWorkspace>(inputWS);
49 auto inputWSIsSpecial = bool(inputMaskWS);
50 if (inputWSIsSpecial) {
51 g_log.notice() << "Input workspace is a MaskWorkspace.\n";
52 }
53
54 // List masked of detector IDs
55 std::vector<detid_t> detectorList;
56 const auto &detInfo = inputWS->detectorInfo();
57 const auto &detIds = detInfo.detectorIDs();
58 for (size_t i = 0; i < detInfo.size(); ++i) {
59 if ((inputWSIsSpecial && inputMaskWS->isMasked(detIds[i])) || detInfo.isMasked(i)) {
60 detectorList.emplace_back(detIds[i]);
61 }
62 }
63
64 // Create a new workspace for the results, copy from the input to ensure
65 // that we copy over the instrument and current masking
66 std::shared_ptr<DataObjects::MaskWorkspace> maskWS;
67 if (getProperty("UngroupDetectors"))
68 maskWS = std::make_shared<DataObjects::MaskWorkspace>(inputWS->getInstrument());
69 else
70 maskWS = std::make_shared<DataObjects::MaskWorkspace>(inputWS);
71
72 maskWS->setTitle(inputWS->getTitle());
73
74 // set mask from from detectorList
75 for (auto detectorID : detectorList)
76 try {
77 maskWS->setMasked(detectorID);
78 } catch (std::invalid_argument const &) {
79 g_log.warning() << "Detector ID = " << detectorID << " is masked but does not exist in any output spectra\n ";
80 }
81
82 g_log.information() << maskWS->getNumberMasked() << " spectra are masked\n";
83 g_log.information() << detectorList.size() << " detectors are masked\n";
84 setProperty("OutputWorkspace", maskWS);
85 setProperty("DetectorList", detectorList);
86}
87} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
IntArray detectorList
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
A property class for workspaces.
void exec() override
Execution code.
void init() override
Initialisation code.
Support for a property that holds an array of values.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
Definition Logger.cpp:126
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< const Mantid::Geometry::IDetector > IDetector_const_sptr
Shared pointer to IDetector (const version)
Definition IDetector.h:102
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54