Mantid
Loading...
Searching...
No Matches
MoveInstrumentComponent.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 +
14
15namespace Mantid::DataHandling {
16
17// Register the algorithm into the algorithm factory
18DECLARE_ALGORITHM(MoveInstrumentComponent)
19
20using namespace Kernel;
21using namespace Geometry;
22using namespace API;
23
26
29 // When used as a Child Algorithm the workspace name is not used - hence the
30 // "Anonymous" to satisfy the validator
31 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("Workspace", "Anonymous", Direction::InOut),
32 "The name of the workspace for which the new instrument "
33 "configuration will have an effect. Any other workspaces "
34 "stored in the analysis data service will be unaffected.");
35 declareProperty("ComponentName", "",
36 "The name of the component to move. Component names are "
37 "defined in the instrument definition files. A pathname "
38 "delited by '/' may be used for non-unique name.");
39 declareProperty("DetectorID", -1,
40 "The ID of the detector to move. If both "
41 "the component name and the detector ID "
42 "are set the latter will be used.");
43 declareProperty("X", 0.0, "The x-part of the new location vector.");
44 declareProperty("Y", 0.0, "The y-part of the new location vector.");
45 declareProperty("Z", 0.0, "The z-part of the new location vector.");
46 declareProperty("RelativePosition", true,
47 "The property defining how the (X,Y,Z) vector should be "
48 "interpreted. If true it is a vector relative to the initial "
49 "component's position. Otherwise it is a new position in the "
50 "absolute co-ordinates.");
51}
52
58 // Get the input workspace
59 Workspace_sptr ws = getProperty("Workspace");
60 MatrixWorkspace_sptr inputW = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
61 DataObjects::PeaksWorkspace_sptr inputP = std::dynamic_pointer_cast<DataObjects::PeaksWorkspace>(ws);
62
63 // Get some stuff from the input workspace
65 if (inputW) {
66 inst = inputW->getInstrument();
67 if (!inst)
68 throw std::runtime_error("Could not get a valid instrument from the "
69 "MatrixWorkspace provided as input");
70 } else if (inputP) {
71 inst = inputP->getInstrument();
72 if (!inst)
73 throw std::runtime_error("Could not get a valid instrument from the "
74 "PeaksWorkspace provided as input");
75 } else {
76 if (!inst)
77 throw std::runtime_error("Could not get a valid instrument from the "
78 "workspace and it does not seem to be valid as "
79 "input (must be either MatrixWorkspace or "
80 "PeaksWorkspace");
81 }
82 const std::string ComponentName = getProperty("ComponentName");
83 const int DetID = getProperty("DetectorID");
84 const double X = getProperty("X");
85 const double Y = getProperty("Y");
86 const double Z = getProperty("Z");
87 const bool relativePosition = getProperty("RelativePosition");
88
90 // Find the component to move
91 if (DetID != -1) {
92 comp = inst->getDetector(DetID);
93 if (comp == nullptr) {
94 std::ostringstream mess;
95 mess << "Detector with ID " << DetID << " was not found.";
96 g_log.error(mess.str());
97 throw std::runtime_error(mess.str());
98 }
99 } else if (!ComponentName.empty()) {
100 comp = inst->getComponentByName(ComponentName);
101 if (comp == nullptr) {
102 std::ostringstream mess;
103 mess << "Component with name " << ComponentName << " was not found.";
104 g_log.error(mess.str());
105 throw std::runtime_error(mess.str());
106 }
107 } else {
108 g_log.error("DetectorID or ComponentName must be given.");
109 throw std::invalid_argument("DetectorID or ComponentName must be given.");
110 }
111
112 auto &componentInfo = inputW ? inputW->mutableComponentInfo() : inputP->mutableComponentInfo();
113 auto compIndex = componentInfo.indexOf(comp->getComponentID());
114 if (ComponentInfoBankHelpers::isDetectorFixedInBank(componentInfo, compIndex)) {
115 // DetectorInfo makes changing positions possible but we keep the old
116 // behavior of ignoring position changes for Structured banks.
117 g_log.warning("Component is fixed within a structured bank, moving is not "
118 "possible, doing nothing.");
119 return;
120 }
121
122 // Do the move
123 V3D position(X, Y, Z);
124 if (relativePosition)
125 position += comp->getPos();
126
127 const auto componentId = comp->getComponentID();
128 componentInfo.setPosition(componentInfo.indexOf(componentId), position);
129}
130
131} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double position
Definition: GetAllEi.cpp:154
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 exec() override
Overwrites Algorithm method.
void init() override
Overwrites Algorithm method.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
Class for 3D vectors.
Definition: V3D.h:34
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< PeaksWorkspace > PeaksWorkspace_sptr
Typedef for a shared pointer to a peaks workspace.
MANTID_GEOMETRY_DLL bool isDetectorFixedInBank(const ComponentInfo &compInfo, const size_t detIndex)
Tests whether or not the detector is within a fixed bank.
std::shared_ptr< const IComponent > IComponent_const_sptr
Typdef of a shared pointer to a const IComponent.
Definition: IComponent.h:161
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
@ InOut
Both an input & output workspace.
Definition: Property.h:55