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 +
11
12#include <algorithm>
13
14namespace Mantid::Algorithms {
15
16using std::size_t;
17
18// Register the algorithm into the AlgorithmFactory
19DECLARE_ALGORITHM(CopyInstrumentParameters)
20
21using namespace Kernel;
22using namespace API;
23using namespace Geometry;
25
27 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
28 "Name of the workspace giving the instrument");
29 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::InOut),
30 "Name of the workspace receiving the instrument");
31}
32
38
39 // Get the giving workspace
40 m_givingWorkspace = getProperty("InputWorkspace");
41
42 // Get the receiving workspace
43 m_receivingWorkspace = getProperty("OutputWorkspace");
44
45 // Retrieve and validate the input properties
46 this->checkProperties();
47
49 Instrument_const_sptr inst1 = m_givingWorkspace->getInstrument();
50 Instrument_const_sptr inst2 = m_receivingWorkspace->getInstrument();
51 auto Name1 = inst1->getName();
52 auto Name2 = inst2->getName();
53
55
56 // Get legacy ParameterMap, i.e., including masking, positions, rotations
57 // stored in map (instead of DetectorInfo).
58 const auto &givParams = inst1->makeLegacyParameterMap();
59 for (const auto &item : *givParams) {
60 IComponent *oldComponent = item.first;
61
62 const Geometry::IComponent *targComp = nullptr;
63
64 auto *pOldDet = dynamic_cast<IDetector *>(oldComponent);
65 if (pOldDet) {
66 detid_t detID = pOldDet->getID();
67 targComp = inst2->getBaseDetector(detID);
68 if (!targComp) {
69 g_log.warning() << "Target instrument does not have detector with ID " << detID << '\n';
70 continue;
71 }
72 } else {
73 std::string source_name = oldComponent->getFullName();
74 size_t nameStart = source_name.find(Name1);
75 std::string targ_name = source_name.replace(nameStart, nameStart + Name1.size(), Name2);
76 // existingComponents.
77 auto spTargComp = inst2->getComponentByName(targ_name);
78 if (!spTargComp) {
79 g_log.warning() << "Target instrument does not have component with full name: " << targ_name << '\n';
80 continue;
81 }
82 targComp = spTargComp->getBaseComponent();
83 }
84
85 // create shared pointer to independent copy of original parameter. Would
86 // be easy and nice to have cow_pointer instead of shared_ptr in the
87 // parameter map.
88 auto param = Parameter_sptr(item.second->clone());
89 // add new parameter to the maps for existing target component
90 targMap.add(targComp, param);
91 }
92
93 // Clear old parameters. We also want to clear fields stored in DetectorInfo
94 // (masking, positions, rotations). By setting the base instrument (which
95 // does not include a ParameterMap or DetectorInfo) we make use of the
96 // mechanism in ExperimentInfo that builds a clean DetectorInfo from the
97 // instrument being set.
98 m_receivingWorkspace->setInstrument(inst2->baseInstrument());
99 // ExperimentInfo::readParameterMap deals with extracting legacy information
100 // from ParameterMap.
101 m_receivingWorkspace->readParameterMap(targMap.asString());
102 } else {
103 // Same base instrument, copying the instrument is equivalent to copying the
104 // parameters in the ParameterMap and the DetectorInfo.
105 m_receivingWorkspace->setInstrument(m_givingWorkspace->getInstrument());
106 }
107}
108
114
115 // Check that both workspaces have an instrument
116 Instrument_const_sptr inst = m_givingWorkspace->getInstrument();
117 if (!inst) {
118 throw std::invalid_argument("Input workspace has no instrument");
119 }
120 Instrument_const_sptr inst2 = m_receivingWorkspace->getInstrument();
121 if (!inst2) {
122 throw std::invalid_argument("Output workspace has no instrument");
123 }
124
125 Instrument_const_sptr baseInstGiv = inst->baseInstrument();
126 Instrument_const_sptr baseInstRec = inst2->baseInstrument();
127
128 // Check that both workspaces have the same instrument name
129 if (baseInstRec != baseInstGiv) {
131 g_log.warning() << "The base instrument in the output workspace is not the "
132 "same as the base instrument in the input workspace.\n";
133 }
134}
135
137 const std::map<std::string, Parallel::StorageMode> &storageModes) const {
138 const auto in = storageModes.at("InputWorkspace");
139 const auto out = storageModes.at("InputWorkspace");
140 // Source instrument avaible only on master rank, so copying not possible if
141 // target requires it on non-master ranks.
142 if (in == Parallel::StorageMode::MasterOnly && in != out)
143 return Parallel::ExecutionMode::Invalid;
144 return Parallel::getCorrespondingExecutionMode(out);
145}
146
147} // 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
Kernel::Logger & g_log
Definition: Algorithm.h:451
A property class for workspaces.
void checkProperties()
Retrieves the properties and checks that they have valid values.
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.
Parallel::ExecutionMode getParallelExecutionMode(const std::map< std::string, Parallel::StorageMode > &storageModes) const override
Get correct execution mode based on input storage modes for an MPI run.
base class for Geometric IComponent
Definition: IComponent.h:51
virtual IComponent const * getBaseComponent() const =0
Returns const pointer to base component if this component is parametrized.
virtual std::string getFullName() const =0
Get the IComponent full path name.
Interface class for detector objects.
Definition: IDetector.h:43
std::string asString() const
Returns a string with all component names, parameter names and values.
void add(const std::string &type, const IComponent *comp, const std::string &name, const std::string &value, const std::string *const pDescription=nullptr, const std::string &visible="true")
Method for adding a parameter providing its value as a string.
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
std::shared_ptr< Parameter > Parameter_sptr
Typedef for the shared pointer.
Definition: Parameter.h:195
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21
@ InOut
Both an input & output workspace.
Definition: Property.h:55
@ Input
An input workspace.
Definition: Property.h:53