-
Notifications
You must be signed in to change notification settings - Fork 122
/
ExtractMask.cpp
91 lines (79 loc) · 3.41 KB
/
ExtractMask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAlgorithms/ExtractMask.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidDataObjects/MaskWorkspace.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/MultiThreaded.h"
#include "MantidKernel/NullValidator.h"
namespace Mantid::Algorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ExtractMask)
using Geometry::IDetector_const_sptr;
using Kernel::Direction;
using namespace API;
using namespace Kernel;
/**
* Declare the algorithm properties
*/
void ExtractMask::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
"A workspace whose masking is to be extracted");
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
"A workspace containing the masked spectra as zeroes and ones.");
declareProperty(
std::make_unique<ArrayProperty<detid_t>>("DetectorList", std::make_shared<NullValidator>(), Direction::Output),
"A comma separated list or array containing a list of masked "
"detector ID's");
}
/**
* Execute the algorithm
*/
void ExtractMask::exec() {
MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
// convert input to a mask workspace
auto inputMaskWS = std::dynamic_pointer_cast<const DataObjects::MaskWorkspace>(inputWS);
auto inputWSIsSpecial = bool(inputMaskWS);
if (inputWSIsSpecial) {
g_log.notice() << "Input workspace is a MaskWorkspace.\n";
}
// List masked of detector IDs
std::vector<detid_t> detectorList;
const auto &detInfo = inputWS->detectorInfo();
const auto &detIds = detInfo.detectorIDs();
for (size_t i = 0; i < detInfo.size(); ++i) {
if ((inputWSIsSpecial && inputMaskWS->isMasked(detIds[i])) || detInfo.isMasked(i)) {
detectorList.emplace_back(detIds[i]);
}
}
// Create a new workspace for the results, copy from the input to ensure
// that we copy over the instrument and current masking
auto maskWS = std::make_shared<DataObjects::MaskWorkspace>(inputWS);
maskWS->setTitle(inputWS->getTitle());
const auto &spectrumInfo = inputWS->spectrumInfo();
const auto nHist = static_cast<int64_t>(inputWS->getNumberHistograms());
Progress prog(this, 0.0, 1.0, nHist);
PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *maskWS))
for (int64_t i = 0; i < nHist; ++i) {
PARALLEL_START_INTERRUPT_REGION
bool inputIsMasked(false);
if (spectrumInfo.hasDetectors(i)) {
// special workspaces can mysteriously have the mask bit set
inputIsMasked = (inputWSIsSpecial && inputMaskWS->isMaskedIndex(i)) || spectrumInfo.isMasked(i);
}
maskWS->setMaskedIndex(i, inputIsMasked);
prog.report();
PARALLEL_END_INTERRUPT_REGION
}
PARALLEL_CHECK_INTERRUPT_REGION
g_log.information() << maskWS->getNumberMasked() << " spectra are masked\n";
g_log.information() << detectorList.size() << " detectors are masked\n";
setProperty("OutputWorkspace", maskWS);
setProperty("DetectorList", detectorList);
}
} // namespace Mantid::Algorithms