Mantid
Loading...
Searching...
No Matches
CropToComponent.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 +
12#include "MantidIndexing/Conversion.h"
13#include "MantidIndexing/GlobalSpectrumIndex.h"
14#include "MantidIndexing/IndexInfo.h"
16
17namespace {
18std::vector<size_t> getDetectorIndices(const Mantid::API::MatrixWorkspace &workspace,
19 const std::vector<std::string> &componentNames) {
20 const auto &compInfo = workspace.componentInfo();
21 const auto instrument = workspace.getInstrument();
22 std::vector<size_t> detIndices;
23 for (const auto &componentName : componentNames) {
24 const auto comp = instrument->getComponentByName(componentName);
25 const auto compIndex = compInfo.indexOf(comp->getComponentID());
26 const auto indices = compInfo.detectorsInSubtree(compIndex);
27 detIndices.insert(detIndices.end(), indices.begin(), indices.end());
28 }
29 return detIndices;
30}
31} // namespace
32
33namespace Mantid::Algorithms {
34
37
38// Register the algorithm into the AlgorithmFactory
39DECLARE_ALGORITHM(CropToComponent)
40
41//----------------------------------------------------------------------------------------------
42
43
44const std::string CropToComponent::name() const { return "CropToComponent"; }
45
47int CropToComponent::version() const { return 1; }
48
50const std::string CropToComponent::category() const { return "Transforms\\Splitting"; }
51
53const std::string CropToComponent::summary() const { return "Crops a workspace to a set of components."; }
54
55//----------------------------------------------------------------------------------------------
60 std::make_unique<WorkspaceProperty<Mantid::API::MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
61 "An input workspace.");
63 std::make_unique<WorkspaceProperty<Mantid::API::MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
64 "An output workspace.");
65 declareProperty(std::make_unique<Mantid::Kernel::ArrayProperty<std::string>>("ComponentNames"),
66 "List of component names which are used to crop the workspace."
67 "to.");
68}
69
70//----------------------------------------------------------------------------------------------
74 // Get the names of the components
75 std::vector<std::string> componentNames = getProperty("ComponentNames");
76 Mantid::API::MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
77
78 // Get all detectors
79 const auto &detectorIndices = getDetectorIndices(*inputWorkspace, componentNames);
80
81 // Get the corresponding workspace indices from the detectors
82 const auto &workspaceIndices = inputWorkspace->indexInfo().globalSpectrumIndicesFromDetectorIndices(detectorIndices);
83
84 // Run ExtractSpectra in order to obtain the cropped workspace
85 auto extract_alg = Mantid::API::AlgorithmManager::Instance().createUnmanaged("ExtractSpectra");
86 extract_alg->setChild(true);
87 extract_alg->initialize();
88 extract_alg->setProperty("InputWorkspace", inputWorkspace);
89 extract_alg->setProperty("OutputWorkspace", "dummy");
90 extract_alg->setProperty("WorkspaceIndexList", Indexing::castVector<size_t>(workspaceIndices));
91 extract_alg->execute();
92 Mantid::API::MatrixWorkspace_sptr outputWorkspace = extract_alg->getProperty("OutputWorkspace");
93
94 // Set the output
95 setProperty("OutputWorkspace", outputWorkspace);
96}
97
98std::map<std::string, std::string> CropToComponent::validateInputs() {
99 std::map<std::string, std::string> result;
100 Mantid::API::MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
101
102 if (!inputWorkspace) {
103 result["InputWorkspace"] = "The InputWorkspace must be a MatrixWorkspace.";
104 return result;
105 }
106
107 std::vector<std::string> componentNames = getProperty("ComponentNames");
108
109 // Make sure that the component exists on the input workspace
110 auto instrument = inputWorkspace->getInstrument();
111 for (auto &componentName : componentNames) {
112 auto detector = instrument->getComponentByName(componentName);
113 if (!detector) {
114 std::string message =
115 "The component name " + componentName + " does not exist on the workspace. Specify a valid component.";
116 result["ComponentNames"] = message;
117 break;
118 }
119 }
120 return result;
121}
122
123} // namespace Mantid::Algorithms
#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
Base MatrixWorkspace Abstract Class.
A property class for workspaces.
CropToComponent : Crops a workspace to a set of components.
void init() override final
Initialize the algorithm's properties.
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
void exec() override final
Execute the algorithm.
int version() const override final
Algorithm's version for identification.
const std::string summary() const override final
Algorithm's summary for use in the GUI and help.
const std::string category() const override final
Algorithm's category for identification.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
STL namespace.
Describes the direction (within an algorithm) of a Property.
Definition: Property.h:50
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54