Mantid
Loading...
Searching...
No Matches
Scale.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 +
11
12namespace Mantid::Algorithms {
13
14// Register the algorithm into the AlgorithmFactory
16
17using namespace Kernel;
18using namespace API;
19
20namespace {
21namespace PropertyNames {
22const std::string INPUT_WORKSPACE("InputWorkspace");
23const std::string OUTPUT_WORKSPACE("OutputWorkspace");
24const std::string FACTOR("Factor");
25const std::string OPERATION("Operation");
26} // namespace PropertyNames
27namespace Operation {
28const std::string MULT("Multiply");
29const std::string ADD("Add");
30} // namespace Operation
31} // anonymous namespace
32
36
37 declareProperty(PropertyNames::FACTOR, 1.0, "The value by which to scale the input workspace");
38 std::vector<std::string> operations{Operation::MULT, Operation::ADD};
39 declareProperty(PropertyNames::OPERATION, Operation::MULT, std::make_shared<StringListValidator>(operations),
40 "Whether to multiply by, or add factor");
41}
42
43std::map<std::string, std::string> Scale::validateInputs() {
44 std::map<std::string, std::string> result;
45
46 // don't allow adding with EventWorkspace
47 if (getPropertyValue(PropertyNames::OPERATION) == Operation::ADD) {
49 const auto eventWS = std::dynamic_pointer_cast<const DataObjects::EventWorkspace>(inputWS);
50 if (bool(eventWS)) {
51 result[PropertyNames::INPUT_WORKSPACE] = "Cannot Add to EventWorkspace";
52 }
53 }
54
55 return result;
56}
57
61 const auto inPlace = bool(outputWS == inputWS);
62 if (!inPlace)
63 outputWS = inputWS->clone();
64
65 const double factor = getProperty(PropertyNames::FACTOR);
66 const std::string operation = getPropertyValue(PropertyNames::OPERATION);
67
68 // We require a copy of the workspace if there is Dx
69 MatrixWorkspace_sptr bufferWS;
70 const auto hasDx = inputWS->hasDx(0);
71 if (hasDx) {
72 if (inPlace)
73 bufferWS = inputWS->clone();
74 else
75 bufferWS = inputWS;
76 }
77
78 Progress progress(this, 0.0, 1.0, 2);
79
80 if (operation == Operation::MULT) {
81 progress.report("Multiplying factor...");
82
83 outputWS *= factor;
84 } else if (operation == Operation::ADD) {
85 progress.report("Adding factor...");
86
87 outputWS += factor;
88 } else { // should be impossible to get here
89 std::stringstream msg;
90 msg << "Do not know how to \"" << operation << "\"";
91 throw std::runtime_error(msg.str());
92 }
93
94 progress.report();
95
96 // If there are any Dx values in the input workspace, then
97 // copy them across. We check only the first spectrum.
98 if (hasDx) {
99 const auto numHist = bufferWS->getNumberHistograms();
100 for (size_t index = 0; index < numHist; ++index) {
101 outputWS->setSharedDx(index, bufferWS->sharedDx(index));
102 }
103 }
104
106}
107
108} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
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
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void exec() override
Execution code.
Definition: Scale.cpp:58
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
Definition: Scale.cpp:43
void init() override
Initialisation code.
Definition: Scale.cpp:33
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
static const std::string OUTPUT_WORKSPACE
static const std::string INPUT_WORKSPACE
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54