-
Notifications
You must be signed in to change notification settings - Fork 122
/
MaskDetectorsInShape.cpp
85 lines (74 loc) · 3.33 KB
/
MaskDetectorsInShape.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
// 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 "MantidDataHandling/MaskDetectorsInShape.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/MandatoryValidator.h"
namespace Mantid {
namespace DataHandling {
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(MaskDetectorsInShape)
using namespace Kernel;
using namespace API;
void MaskDetectorsInShape::init() {
declareProperty(std::make_unique<WorkspaceProperty<>>("Workspace", "", Direction::InOut), "The input workspace");
declareProperty("ShapeXML", "", std::make_shared<MandatoryValidator<std::string>>(),
"The XML definition of the user defined shape.");
declareProperty("IncludeMonitors", false,
"Whether to include monitors if "
"they are contained in the shape "
"(default false)");
}
void MaskDetectorsInShape::exec() {
// Get the input workspace
MatrixWorkspace_sptr WS = getProperty("Workspace");
const bool includeMonitors = getProperty("IncludeMonitors");
const std::string shapeXML = getProperty("ShapeXML");
std::vector<int> foundDets = runFindDetectorsInShape(WS, shapeXML, includeMonitors);
if (foundDets.empty()) {
g_log.information("No detectors were found in the shape, nothing was masked");
return;
}
runMaskDetectors(WS, foundDets);
setProperty("Workspace", WS);
}
/// Run the FindDetectorsInShape Child Algorithm
std::vector<int> MaskDetectorsInShape::runFindDetectorsInShape(const API::MatrixWorkspace_sptr &workspace,
const std::string &shapeXML,
const bool includeMonitors) {
IAlgorithm_sptr alg = createChildAlgorithm("FindDetectorsInShape");
alg->setPropertyValue("IncludeMonitors", includeMonitors ? "1" : "0");
alg->setPropertyValue("ShapeXML", shapeXML);
alg->setProperty<MatrixWorkspace_sptr>("Workspace", workspace);
try {
if (!alg->execute()) {
throw std::runtime_error("FindDetectorsInShape Child Algorithm has not "
"executed successfully\n");
}
} catch (std::runtime_error &) {
g_log.error("Unable to successfully execute FindDetectorsInShape Child Algorithm");
throw;
}
progress(0.5);
// extract the results
return alg->getProperty("DetectorList");
}
void MaskDetectorsInShape::runMaskDetectors(const API::MatrixWorkspace_sptr &workspace,
const std::vector<int> &detectorIds) {
auto &detectorInfo = workspace->mutableDetectorInfo();
for (const auto &id : detectorIds)
detectorInfo.setMasked(detectorInfo.indexOf(id), true);
const auto &spectrumInfo = workspace->spectrumInfo();
for (size_t i = 0; i < spectrumInfo.size(); ++i)
if (spectrumInfo.hasDetectors(i) && spectrumInfo.isMasked(i))
workspace->getSpectrum(i).clearData();
progress(1);
}
} // namespace DataHandling
} // namespace Mantid