Mantid
Loading...
Searching...
No Matches
CloneWorkspace.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 +
13
14namespace Mantid::Algorithms {
15
16// Register the algorithm into the AlgorithmFactory
17DECLARE_ALGORITHM(CloneWorkspace)
18
19using namespace Kernel;
20using namespace API;
21using namespace DataObjects;
22
24 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InputWorkspace", "", Direction::Input),
25 "Name of the input workspace. Must be a MatrixWorkspace (2D or "
26 "EventWorkspace), a PeaksWorkspace or a MDEventWorkspace.");
27 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputWorkspace", "", Direction::Output),
28 "Name of the newly created cloned workspace.");
29}
30
32 Workspace_sptr inputWorkspace = getProperty("InputWorkspace");
33 Workspace_const_sptr outputWorkspace = getProperty("OutputWorkspace");
34 if (inputWorkspace == outputWorkspace) {
35 g_log.information("Inplace operation requested, doing nothing");
36 return; // nothing to do
37 }
38
39 MatrixWorkspace_const_sptr inputMatrix = std::dynamic_pointer_cast<const MatrixWorkspace>(inputWorkspace);
40 IMDWorkspace_sptr inputMD = std::dynamic_pointer_cast<IMDWorkspace>(inputWorkspace);
41 ITableWorkspace_const_sptr iTableWS = std::dynamic_pointer_cast<const ITableWorkspace>(inputWorkspace);
42
43 if (inputMatrix || iTableWS) {
44 // Workspace::clone() is polymorphic, we can use the same for all types
45 Workspace_sptr outputWS(inputWorkspace->clone());
46 setProperty("OutputWorkspace", outputWS);
47 } else if (inputMD) {
48 // Call the CloneMDWorkspace algo to handle MDEventWorkspace
49 auto alg = createChildAlgorithm("CloneMDWorkspace", 0.0, 1.0, true);
50 alg->setProperty("InputWorkspace", inputMD);
51 alg->setPropertyValue("OutputWorkspace", getPropertyValue("OutputWorkspace"));
52 alg->executeAsChildAlg();
53 IMDWorkspace_sptr outputWS = alg->getProperty("OutputWorkspace");
54 setProperty("OutputWorkspace", std::dynamic_pointer_cast<Workspace>(outputWS));
55 } else
56 throw std::runtime_error("Expected a MatrixWorkspace, PeaksWorkspace, "
57 "MDEventWorkspace, or a MDHistoWorkspace. Cannot "
58 "clone this type of workspace.");
59}
60
61} // 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
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
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
A property class for workspaces.
void init() override
Initialisation code.
void exec() override
Execution code.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< const ITableWorkspace > ITableWorkspace_const_sptr
shared pointer to Mantid::API::ITableWorkspace (const version)
std::shared_ptr< const Workspace > Workspace_const_sptr
shared pointer to Mantid::API::Workspace (const version)
Definition: Workspace_fwd.h:22
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
Definition: IMDWorkspace.h:146
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54