Mantid
Loading...
Searching...
No Matches
MaskWorkspace.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 +
7#include <utility>
8
13#include "MantidKernel/System.h"
14
15#include <algorithm>
16
17namespace Mantid::DataObjects {
18using std::set;
19using std::size_t;
20
21// Register the workspace
22DECLARE_WORKSPACE(MaskWorkspace)
23
24namespace { // keep these constants only within this file.
26const double LIVE_VALUE = 0.;
27
32const double DEAD_VALUE = 1.;
33
35const double ERROR_VALUE = 0.;
36} // namespace
37
38//--------------------------------------------------------------------------
39
45MaskWorkspace::MaskWorkspace(std::size_t numvectors) {
46 this->init(numvectors, 1, 1);
47 this->clearMask();
48}
49
57MaskWorkspace::MaskWorkspace(const Mantid::Geometry::Instrument_const_sptr &instrument, const bool includeMonitors)
58 : SpecialWorkspace2D(instrument, includeMonitors) {
59 this->clearMask();
60}
61
69 this->clearMask();
70}
71
72//--------------------------------------------------------------------------
73
75 std::size_t nHist = this->getNumberHistograms();
76 for (std::size_t i = 0; i < nHist; ++i) {
77 this->dataY(i)[0] = LIVE_VALUE;
78 this->dataE(i)[0] = ERROR_VALUE;
79 }
80
81 // Clear the mask flags
83}
84
89 size_t numMasked(0);
90 const size_t numWksp(this->getNumberHistograms());
91 for (size_t i = 0; i < numWksp; i++) {
92 if (this->isMaskedIndex(i)) // quick check the value
93 {
94 numMasked++;
95 } else if (this->hasInstrument()) {
96 const set<detid_t> ids = this->getDetectorIDs(i);
97 if (this->isMasked(ids)) // slow and correct check with the real method
98 numMasked += ids.size();
99 } else {
100 std::stringstream errss;
101 errss << "No instrument is associated with mask workspace " << this->getName();
102 throw std::runtime_error(errss.str());
103 }
104 }
105 return numMasked;
106}
107
113 set<detid_t> detIDs;
114 /*
115 * Note
116 * This test originally just checked if there was an instument and ignored
117 * the number of detectors
118 */
119 if (this->hasInstrument()) {
120 size_t numHist(this->getNumberHistograms());
121 for (size_t i = 0; i < numHist; i++) {
122 if (this->isMaskedIndex(i)) {
123 set<detid_t> temp = this->getDetectorIDs(i);
124 detIDs.insert(temp.begin(), temp.end());
125 }
126 }
127 }
128
129 return detIDs;
130}
131
137 set<size_t> indices;
138
139 size_t numHist(this->getNumberHistograms());
140 for (size_t i = 0; i < numHist; i++) {
141 if (this->isMaskedIndex(i)) {
142 indices.insert(i);
143 }
144 }
145
146 return indices;
147}
148
149//--------------------------------------------------------------------------------------------
154bool MaskWorkspace::isMasked(const detid_t detectorID) const {
155 if (!this->hasInstrument()) {
156 std::stringstream msg;
157 if (!this->getInstrument())
158 msg << "There is no instrument associated with workspace \'" << this->getName() << "\'";
159 else
160 msg << "There is no proper instrument associated with workspace \'" << this->getName()
161 << "\'. Number of detectors = " << this->getInstrument()->getNumberDetectors();
162 throw std::runtime_error(msg.str());
163 }
164
165 // return true if the value isn't zero
166 if (this->getValue(detectorID, LIVE_VALUE) != LIVE_VALUE) {
167 return true;
168 }
169
170 // the mask bit on the workspace can be set
171 // Performance wise, it is not optimal to call detectorInfo() for every index,
172 // but this method seems to be used rarely enough to justify this until the
173 // Instrument-2.0 implementation has progressed far enough to make this cheap.
174 const auto &detectorInfo = this->detectorInfo();
175 try {
176 return detectorInfo.isMasked(detectorInfo.indexOf(detectorID));
177 } catch (std::out_of_range &) {
178 // The workspace can contain bad detector IDs. DetectorInfo::indexOf throws.
179 return false;
180 }
181}
182
186bool MaskWorkspace::isMasked(const std::set<detid_t> &detectorIDs) const {
187 if (detectorIDs.empty()) {
188 return false;
189 }
190
191 const auto it = std::find_if_not(detectorIDs.cbegin(), detectorIDs.cend(),
192 [this](const auto detectorID) { return this->isMasked(detectorID); });
193 return it == detectorIDs.cend();
194}
195
199bool MaskWorkspace::isMaskedIndex(const std::size_t wkspIndex) const {
200 return (this->dataY(wkspIndex)[0] != LIVE_VALUE); // if is not live it should masked
201}
202
209void MaskWorkspace::setMasked(const detid_t detectorID, const bool mask) {
210 double value(LIVE_VALUE);
211 if (mask)
212 value = DEAD_VALUE;
213
214 this->setValue(detectorID, value, ERROR_VALUE);
215}
216
221void MaskWorkspace::setMasked(const std::set<detid_t> &detectorIDs, const bool mask) {
222 for (auto detectorID : detectorIDs) {
223 this->setMasked(detectorID, mask);
224 }
225}
226
227void MaskWorkspace::setMaskedIndex(const std::size_t wkspIndex, const bool mask) {
228 double value(LIVE_VALUE);
229 if (mask)
230 value = DEAD_VALUE;
231
232 this->dataY(wkspIndex)[0] = value;
233}
234
239const std::string MaskWorkspace::id() const { return "MaskWorkspace"; }
240
241//--------------------------------------------------------------------------------------------
244void MaskWorkspace::copyFrom(std::shared_ptr<const SpecialWorkspace2D> sourcews) {
246}
247
251const std::string MaskWorkspace::toString() const {
252 std::ostringstream os;
254 os << "Masked: " << getNumberMasked() << "\n";
255 return os.str();
256}
257
258//--------------------------------------------------------------------------------------------
264 bool hasinst;
266 if (inst) {
267 hasinst = inst->getNumberDetectors() > 0;
268 } else
269 hasinst = false;
270
271 return hasinst;
272}
273
274} // namespace Mantid::DataObjects
275
277
278namespace Mantid::Kernel {
279
280template <>
282IPropertyManager::getValue<Mantid::DataObjects::MaskWorkspace_sptr>(const std::string &name) const {
283 auto *prop = dynamic_cast<PropertyWithValue<Mantid::DataObjects::MaskWorkspace_sptr> *>(getPointerToProperty(name));
284 if (prop) {
285 return *prop;
286 } else {
287 std::string message =
288 "Attempt to assign property " + name + " to incorrect type. Expected shared_ptr<MaskWorkspace>.";
289 throw std::runtime_error(message);
290 }
291}
292
293template <>
295IPropertyManager::getValue<Mantid::DataObjects::MaskWorkspace_const_sptr>(const std::string &name) const {
296 auto *prop = dynamic_cast<PropertyWithValue<Mantid::DataObjects::MaskWorkspace_sptr> *>(getPointerToProperty(name));
297 if (prop) {
298 return prop->operator()();
299 } else {
300 std::string message =
301 "Attempt to assign property " + name + " to incorrect type. Expected const shared_ptr<MaskWorkspace>.";
302 throw std::runtime_error(message);
303 }
304}
305
306} // namespace Mantid::Kernel
307
double value
The value of the point.
Definition: FitMW.cpp:51
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
#define DECLARE_WORKSPACE(classname)
Geometry::DetectorInfo & mutableDetectorInfo()
Return a non-const reference to the DetectorInfo object.
const Geometry::DetectorInfo & detectorInfo() const
Return a const reference to the DetectorInfo object.
Geometry::Instrument_const_sptr getInstrument() const
Returns the parameterized instrument.
virtual MantidVec & dataE(const std::size_t index)
Deprecated, use mutableE() instead. Returns the error data.
virtual MantidVec & dataY(const std::size_t index)
Deprecated, use mutableY() instead. Returns the y data.
const std::string & getName() const override
Get the workspace name.
Definition: Workspace.cpp:58
const std::string toString() const override
Return human-readable string.
void clearMask()
Clear original incorrect mask.
void setMaskedIndex(const std::size_t wkspIndex, const bool mask=true)
const std::string id() const override
Gets the name of the workspace type.
bool isMaskedIndex(const std::size_t wkspIndex) const
Use this method with MaskWorkspace that doesn't have an instrument.
void copyFrom(std::shared_ptr< const SpecialWorkspace2D > sourcews) override
Copy the set up from another workspace.
bool hasInstrument() const
Check whether any instrument associated.
void setMasked(const detid_t detectorID, const bool mask=true) override
Mask an individual pixel.
bool isMasked(const detid_t detectorID) const override
std::set< detid_t > getMaskedDetectors() const
MaskWorkspace::getMaskedDetectors.
std::set< std::size_t > getMaskedWkspIndices() const
MaskWorkspace::getMaskedWkspIndices.
std::size_t getNumberMasked() const override
double getValue(const detid_t detectorID) const
Return the special value (Y) in the workspace at the given detector ID.
const std::string toString() const override
Return human-readable string.
void init(const size_t &NVectors, const size_t &XLength, const size_t &YLength) override
Sets the size of the workspace and initializes arrays to zero.
std::set< detid_t > getDetectorIDs(const std::size_t workspaceIndex) const
Return the detector ID at the given workspace index (i.e., spectrum/histogram index)
virtual void copyFrom(std::shared_ptr< const SpecialWorkspace2D > sourcews)
Duplicate SpecialWorkspace2D.
void setValue(const detid_t detectorID, const double value, const double error=0.)
Set the special value (Y) in the workspace at the given detector ID.
std::size_t getNumberHistograms() const override
Returns the histogram number.
bool isMasked(const size_t index) const
Returns true if the detector is masked.
void clearMaskFlags()
Sets all mask flags to false (unmasked).
size_t indexOf(const detid_t id) const
Returns the index of the detector with the given detector ID.
The concrete, templated class for properties.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< const MaskWorkspace > MaskWorkspace_const_sptr
shared pointer to a const MaskWorkspace
Definition: MaskWorkspace.h:67
std::shared_ptr< MaskWorkspace > MaskWorkspace_sptr
shared pointer to the MaskWorkspace class
Definition: MaskWorkspace.h:64
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.