Mantid
Loading...
Searching...
No Matches
CopyDataRange.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 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
12
13#include <algorithm>
14#include <utility>
15
16using namespace Mantid::API;
17using namespace Mantid::Kernel;
18using namespace Mantid::HistogramData;
19
20namespace {
21
22void copyDataRange(const MatrixWorkspace_const_sptr &inputWorkspace, const MatrixWorkspace_sptr &destWorkspace,
23 int const &specMin, int const &specMax, int const &xMinIndex, int const &xMaxIndex,
24 int yInsertionIndex, int const &xInsertionIndex) {
25 for (auto specIndex = specMin; specIndex <= specMax; ++specIndex) {
26 std::copy(inputWorkspace->y(specIndex).begin() + xMinIndex, inputWorkspace->y(specIndex).begin() + xMaxIndex + 1,
27 destWorkspace->mutableY(yInsertionIndex).begin() + xInsertionIndex);
28 std::copy(inputWorkspace->e(specIndex).begin() + xMinIndex, inputWorkspace->e(specIndex).begin() + xMaxIndex + 1,
29 destWorkspace->mutableE(yInsertionIndex).begin() + xInsertionIndex);
30 ++yInsertionIndex;
31 }
32}
33
34void copyDataRange(const MatrixWorkspace_const_sptr &inputWorkspace, const MatrixWorkspace_sptr &destWorkspace,
35 int const &specMin, int const &specMax, double const &xMin, double const &xMax, int yInsertionIndex,
36 int const &xInsertionIndex) {
37 auto const xMinIndex = static_cast<int>(inputWorkspace->yIndexOfX(xMin, 0));
38 auto const xMaxIndex = static_cast<int>(inputWorkspace->yIndexOfX(xMax, 0));
39
40 copyDataRange(inputWorkspace, destWorkspace, specMin, specMax, xMinIndex, xMaxIndex, yInsertionIndex,
41 xInsertionIndex);
42}
43
44} // namespace
45
46namespace Mantid::Algorithms {
47
48DECLARE_ALGORITHM(CopyDataRange)
49
50
51const std::string CopyDataRange::name() const { return "CopyDataRange"; }
52
54int CopyDataRange::version() const { return 1; }
55
57const std::string CopyDataRange::category() const { return "Utility\\Workspaces"; }
58
60const std::string CopyDataRange::summary() const {
61 return "Replaces a range of data in the destination workspace with a "
62 "specified continuous range of data from the input workspace";
63}
64
66
67 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
68 "The workspace containing a range of data to be used for the "
69 "replacement.");
70
71 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("DestWorkspace", "", Direction::Input),
72 "The workspace to have range of data replaced.");
73
74 auto const positiveInt = std::make_shared<Kernel::BoundedValidator<int>>();
75 positiveInt->setLower(0);
76 auto const anyDouble = std::make_shared<Kernel::BoundedValidator<double>>();
77
78 declareProperty("StartWorkspaceIndex", 0, positiveInt, "The index denoting the start of the spectra range.");
79
80 declareProperty("EndWorkspaceIndex", EMPTY_INT(), positiveInt, "The index denoting the end of the spectra range.");
81
82 declareProperty("XMin", EMPTY_DBL(), anyDouble,
83 "An X value that is equal to the lowest point to copy (point data), or "
84 "an X value that is within the first bin to copy (histogram data).");
85
86 declareProperty("XMax", EMPTY_DBL(), anyDouble,
87 "An X value that is equal to the highest point to copy (point data), or "
88 "an X value that is within the last bin to copy (histogram data).");
89
90 declareProperty("InsertionYIndex", 0, positiveInt,
91 "The index denoting the histogram position for the start of "
92 "the data replacement in the DestWorkspace.");
93
94 declareProperty("InsertionXIndex", 0, positiveInt,
95 "The index denoting the x position for the start of the data "
96 "replacement in the DestWorkspace.");
97
98 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
99 "The name to give the output workspace.");
100}
101
102std::map<std::string, std::string> CopyDataRange::validateInputs() {
103 std::map<std::string, std::string> errors;
104
105 MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
106 MatrixWorkspace_sptr destWorkspace = getProperty("DestWorkspace");
107
108 int const specMinIndex = getProperty("StartWorkspaceIndex");
109 int const specMaxIndex = getProperty("EndWorkspaceIndex");
110 double const xMin = getProperty("XMin");
111 double const xMax = getProperty("XMax");
112
113 int const yInsertionIndex = getProperty("InsertionYIndex");
114 int const xInsertionIndex = getProperty("InsertionXIndex");
115
116 if (!inputWorkspace) {
117 errors["InputWorkspace"] = "The InputWorkspace must be a MatrixWorkspace.";
118 }
119 if (!destWorkspace) {
120 errors["DestWorkspace"] = "The DestWorkspace must be a MatrixWorkspace.";
121 }
122 if (!errors.empty()) {
123 return errors;
124 }
125
126 try {
127 auto const xMinIndex = inputWorkspace->yIndexOfX(xMin, 0, 0.000001);
128 auto const xMaxIndex = inputWorkspace->yIndexOfX(xMax, 0, 0.000001);
129
130 if (xMinIndex > xMaxIndex)
131 errors["XMin"] = "XMin must come after XMax.";
132
133 if (destWorkspace->y(0).size() <= static_cast<std::size_t>(xInsertionIndex) + xMaxIndex - xMinIndex)
134 errors["InsertionXIndex"] = "The x data range selected will not fit into "
135 "the destination workspace.";
136
137 } catch (std::exception const &ex) {
138 errors["XMin"] = ex.what();
139 errors["XMax"] = ex.what();
140 }
141
142 if (specMaxIndex >= static_cast<int>(inputWorkspace->getNumberHistograms()))
143 errors["EndWorkspaceIndex"] = "The EndWorkspaceIndex is larger than the number of histograms in the "
144 "input workspace.";
145 if (specMinIndex > specMaxIndex)
146 errors["StartWorkspaceIndex"] = "The StartWorkspaceIndex must be smaller than the EndWorkspaceIndex.";
147
148 if (static_cast<int>(destWorkspace->getNumberHistograms()) <= yInsertionIndex + specMaxIndex - specMinIndex)
149 errors["InsertionYIndex"] = "The y data range selected will not fit into "
150 "the destination workspace.";
151
152 return errors;
153}
154
156 MatrixWorkspace_const_sptr inputWorkspace = getProperty("InputWorkspace");
157 MatrixWorkspace_const_sptr destWorkspace = getProperty("DestWorkspace");
158 int const specMinIndex = getProperty("StartWorkspaceIndex");
159 int const specMaxIndex = getProperty("EndWorkspaceIndex");
160 double const xMin = getProperty("XMin");
161 double const xMax = getProperty("XMax");
162 int const yInsertionIndex = getProperty("InsertionYIndex");
163 int const xInsertionIndex = getProperty("InsertionXIndex");
164 MatrixWorkspace_sptr outputWorkspace = getProperty("OutputWorkspace");
165
166 if (destWorkspace != outputWorkspace)
167 outputWorkspace = destWorkspace->clone();
168
169 copyDataRange(inputWorkspace, outputWorkspace, specMinIndex, specMaxIndex, xMin, xMax, yInsertionIndex,
170 xInsertionIndex);
171
172 setProperty("OutputWorkspace", outputWorkspace);
173}
174
175} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
A property class for workspaces.
CopyDataRange : This algorithm takes a continuous block of data from an input workspace specified by ...
Definition: CopyDataRange.h:21
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
int version() const override
Algorithm's version for identification.
std::string const summary() const override
Algorithm's summary for use in the GUI and help.
std::string const category() const override
Algorithm's category for identification.
void exec() override
Virtual method - must be overridden by concrete 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.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54