Mantid
Loading...
Searching...
No Matches
CopyInstrumentParameters.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
13#include <algorithm>
14
15namespace Mantid::Algorithms {
16
17using std::size_t;
18
19// Register the algorithm into the AlgorithmFactory
20DECLARE_ALGORITHM(CopyInstrumentParameters)
21
22using namespace Kernel;
23using namespace API;
24using namespace Geometry;
26
28 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
29 "Name of the workspace giving the instrument");
30 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::InOut),
31 "Name of the workspace receiving the instrument");
32}
33
39
40 // Get the giving workspace
41 m_givingWorkspace = getProperty("InputWorkspace");
42
43 // Get the receiving workspace
44 m_receivingWorkspace = getProperty("OutputWorkspace");
45
46 // Retrieve and validate the input properties
47 this->checkProperties();
48
50 auto Name1 = m_givingWorkspace->getInstrumentName();
51 auto Name2 = m_receivingWorkspace->getInstrumentName();
52 const auto &componentInfoGiv = m_givingWorkspace->componentInfo();
53 const auto &detectorInfoGiv = m_givingWorkspace->detectorInfo();
54
55 // The legacy parameters (including masking, positions and rotations), written as
56 // ParameterMap::asString() writes them but naming components in the receiving instrument.
57 // readParameterMap() warns about and skips any component the receiving instrument lacks.
58 std::string targParams;
59 for (const auto &[index, param] : m_givingWorkspace->getInstrument()->legacyParameters()) {
60 std::string targComponent;
61 if (componentInfoGiv.isDetector(index)) {
62 targComponent = "detID:" + std::to_string(detectorInfoGiv.detectorIDs()[index]);
63 } else {
64 targComponent = componentInfoGiv.fullName(index);
65 targComponent.replace(targComponent.find(Name1), Name1.size(), Name2);
66 }
67 targParams += ParameterMap::asStringEntry(targComponent, *param);
68 }
69
70 // Clear old parameters, and the fields stored in DetectorInfo and ComponentInfo (masking, positions, rotations),
71 // returning the receiving instrument to its base geometry before the copied parameters are applied.
72 auto clear = createChildAlgorithm("ClearInstrumentParameters");
73 clear->setProperty("Workspace", m_receivingWorkspace);
74 clear->executeAsChildAlg();
75 // ExperimentInfo::readParameterMap deals with extracting legacy information
76 // from ParameterMap.
77 m_receivingWorkspace->readParameterMap(targParams);
78 } else {
79 // Same base instrument, copying the instrument is equivalent to copying the
80 // parameters in the ParameterMap and the DetectorInfo.
81 m_receivingWorkspace->setInstrument(m_givingWorkspace->getInstrument());
82 }
83}
84
89
90 // A component ID is the address of the base component, so the IDs of the roots identify the base instruments
91 const auto &componentInfoGiv = m_givingWorkspace->componentInfo();
92 const auto &componentInfoRec = m_receivingWorkspace->componentInfo();
93 if (componentInfoGiv.componentID(componentInfoGiv.root()) != componentInfoRec.componentID(componentInfoRec.root())) {
95 g_log.warning() << "The base instrument in the output workspace is not the "
96 "same as the base instrument in the input workspace.\n";
97 }
98}
99} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:547
std::map< DeltaEMode::Type, std::string > index
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
void clear() override
Clears all properties under management.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
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.
Kernel::Logger & g_log
Definition Algorithm.h:426
A property class for workspaces.
void checkProperties()
Checks whether the two workspaces have the same base instrument, recording the result in m_different_...
API::MatrixWorkspace_sptr m_givingWorkspace
The giving workspace.
bool m_different_instrument_sp
indicates that source workspace instrument and target workspace instrument have different share point...
API::MatrixWorkspace_sptr m_receivingWorkspace
The receiving workspace.
static std::string asStringEntry(const std::string &component, const Parameter &parameter)
Returns the asString() entry for one parameter of a component, given as "detID:<id>" or by full name.
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
std::string to_string(const wide_integer< Bits, Signed > &n)
@ InOut
Both an input & output workspace.
Definition Property.h:56
@ Input
An input workspace.
Definition Property.h:54