Mantid
Loading...
Searching...
No Matches
MaskDetectorsInShape.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
14
15namespace Mantid::DataHandling {
16// Register the algorithm into the algorithm factory
17DECLARE_ALGORITHM(MaskDetectorsInShape)
18
19using namespace Kernel;
20using namespace API;
21
23 declareProperty(std::make_unique<WorkspaceProperty<>>("Workspace", "", Direction::InOut), "The input workspace");
24 declareProperty("ShapeXML", "", std::make_shared<MandatoryValidator<std::string>>(),
25 "The XML definition of the user defined shape.");
26 declareProperty("IncludeMonitors", false,
27 "Whether to include monitors if "
28 "they are contained in the shape "
29 "(default false)");
30}
31
33 // Get the input workspace
34 MatrixWorkspace_sptr WS = getProperty("Workspace");
35
36 const bool includeMonitors = getProperty("IncludeMonitors");
37 const std::string shapeXML = getProperty("ShapeXML");
38
39 std::vector<int> foundDets = runFindDetectorsInShape(WS, shapeXML, includeMonitors);
40 if (foundDets.empty()) {
41 g_log.information("No detectors were found in the shape, nothing was masked");
42 return;
43 }
44 runMaskDetectors(WS, foundDets);
45 setProperty("Workspace", WS);
46}
47
50 const std::string &shapeXML,
51 const bool includeMonitors) {
52 auto alg = createChildAlgorithm("FindDetectorsInShape");
53 alg->setPropertyValue("IncludeMonitors", includeMonitors ? "1" : "0");
54 alg->setPropertyValue("ShapeXML", shapeXML);
55 alg->setProperty<MatrixWorkspace_sptr>("Workspace", workspace);
56 try {
57 if (!alg->execute()) {
58 throw std::runtime_error("FindDetectorsInShape Child Algorithm has not "
59 "executed successfully\n");
60 }
61 } catch (std::runtime_error &) {
62 g_log.error("Unable to successfully execute FindDetectorsInShape Child Algorithm");
63 throw;
64 }
65 progress(0.5);
66
67 // extract the results
68 return alg->getProperty("DetectorList");
69}
70
72 const std::vector<int> &detectorIds) {
73 auto &detectorInfo = workspace->mutableDetectorInfo();
74 for (const auto &id : detectorIds)
75 detectorInfo.setMasked(detectorInfo.indexOf(id), true);
76 const auto &spectrumInfo = workspace->spectrumInfo();
77 for (size_t i = 0; i < spectrumInfo.size(); ++i)
78 if (spectrumInfo.hasDetectors(i) && spectrumInfo.isMasked(i))
79 workspace->getSpectrum(i).clearData();
80 progress(1);
81}
82
83} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
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
Kernel::Logger & g_log
Definition: Algorithm.h:451
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
A property class for workspaces.
void runMaskDetectors(const API::MatrixWorkspace_sptr &workspace, const std::vector< int > &detectorIds)
Calls MaskDetectors as a Child Algorithm.
void exec() override
Virtual method - must be overridden by concrete algorithm.
std::vector< int > runFindDetectorsInShape(const API::MatrixWorkspace_sptr &workspace, const std::string &shapeXML, const bool includeMonitors)
Run the FindDetectorsInShape Child Algorithm.
void init() override
Virtual method - must be overridden by concrete algorithm.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
Validator to check that a property is not left empty.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ InOut
Both an input & output workspace.
Definition: Property.h:55