Mantid
Loading...
Searching...
No Matches
CopyLogs.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
10#include "MantidAPI/Run.h"
13
14namespace Mantid::Algorithms {
15
16// Register the algorithm into the AlgorithmFactory
17DECLARE_ALGORITHM(CopyLogs)
18
19using namespace API;
20using namespace Kernel;
21
22//----------------------------------------------------------------------------------------------
24const std::string CopyLogs::name() const { return "CopyLogs"; }
25
27int CopyLogs::version() const { return 1; }
28
30const std::string CopyLogs::category() const { return "Utility\\Workspaces"; }
31
32//----------------------------------------------------------------------------------------------
33
34//----------------------------------------------------------------------------------------------
38 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
39 "Workspace to copy logs from.");
40 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::InOut),
41 "Workspace to copy logs to.");
42
43 // options for the type of strategy to take
44 std::vector<std::string> strategies{"WipeExisting", "MergeKeepExisting", "MergeReplaceExisting"};
45
46 auto strategiesValidator = std::make_shared<StringListValidator>(strategies);
47 declareProperty("MergeStrategy", "MergeReplaceExisting", strategiesValidator,
48 "The type of merge strategy to use on the logs");
49}
50
51//----------------------------------------------------------------------------------------------
55 MatrixWorkspace_sptr inputWs = getProperty("InputWorkspace");
56 MatrixWorkspace_sptr outputWs = getProperty("OutputWorkspace");
57
58 // get logs from input workspace
59 const Run &inputRun = inputWs->run();
60 const auto &inputLogs = inputRun.getLogData();
61
62 // get run from output workspace
63 Run &outputRun = outputWs->mutableRun();
64
65 const std::string mode = getProperty("MergeStrategy");
66
67 if (mode == "WipeExisting") {
68 wipeExisting(inputLogs, outputRun);
69 } else if (mode == "MergeKeepExisting") {
70 mergeKeepExisting(inputLogs, outputRun);
71 } else if (mode == "MergeReplaceExisting") {
72 mergeReplaceExisting(inputLogs, outputRun);
73 } else {
74 throw std::runtime_error("Cannot copy logs using unknown merge strategy");
75 }
76
77 if (!outputWs->getName().empty())
78 setProperty("OutputWorkspace", outputWs->getName());
79}
80
85void CopyLogs::mergeReplaceExisting(const std::vector<Kernel::Property *> &inputLogs, Run &outputRun) {
86 for (auto prop : inputLogs) {
87 // if the log exists, remove and replace it
88 if (outputRun.hasProperty(prop->name())) {
89 outputRun.removeLogData(prop->name());
90 }
91 outputRun.addLogData(prop->clone());
92 }
93}
94
99void CopyLogs::mergeKeepExisting(const std::vector<Kernel::Property *> &inputLogs, Run &outputRun) {
100 for (auto prop : inputLogs) {
101 // add the log only if it doesn't already exist
102 if (!outputRun.hasProperty(prop->name())) {
103 outputRun.addLogData(prop->clone());
104 }
105 }
106}
107
112void CopyLogs::wipeExisting(const std::vector<Kernel::Property *> &inputLogs, Run &outputRun) {
113 auto outputLogs = outputRun.getLogData();
114
115 // remove each of the logs from the second workspace
116 for (auto &outputLog : outputLogs) {
117 outputRun.removeLogData(outputLog->name());
118 }
119
120 // add all the logs from the new workspace
121 for (auto inputLog : inputLogs) {
122 outputRun.addLogData(inputLog->clone());
123 }
124}
125
126} // 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
void addLogData(Kernel::Property *p)
Add a log entry.
Definition: LogManager.h:115
bool hasProperty(const std::string &name) const
Does the property exist on the object.
Definition: LogManager.cpp:265
Kernel::Property * getLogData(const std::string &name) const
Access a single log entry.
Definition: LogManager.h:129
void removeLogData(const std::string &name, const bool delproperty=true)
Remove a named log entry.
Definition: LogManager.h:140
This class stores information regarding an experimental run as a series of log entries.
Definition: Run.h:38
A property class for workspaces.
void mergeReplaceExisting(const std::vector< Kernel::Property * > &inputLogs, API::Run &outputRun)
appends new logs and overwrites existing logs.
Definition: CopyLogs.cpp:85
void init() override
Initialize the algorithm's properties.
Definition: CopyLogs.cpp:37
int version() const override
Algorithm's version for identification.
Definition: CopyLogs.cpp:27
void exec() override
Execute the algorithm.
Definition: CopyLogs.cpp:54
void mergeKeepExisting(const std::vector< Kernel::Property * > &inputLogs, API::Run &outputRun)
appends new logs but leaves exisitng logs untouched.
Definition: CopyLogs.cpp:99
const std::string name() const override
Algorithm's name for identification.
Definition: CopyLogs.cpp:24
const std::string category() const override
Algorithm's category for identification.
Definition: CopyLogs.cpp:30
void wipeExisting(const std::vector< Kernel::Property * > &inputLogs, API::Run &outputRun)
appends new logs and removes all existing logs.
Definition: CopyLogs.cpp:112
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ InOut
Both an input & output workspace.
Definition: Property.h:55
@ Input
An input workspace.
Definition: Property.h:53