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 declareProperty("MoveFixedDetectors", false,
52 "Whether to allow moving of individual detector pixels located within a structured bank. "
53 "If set to false then a request to move a detector pixel within a structured bank will be ignored. "
54 "The default value for this property is set to false to maintain backwards compatibility.");
55}
56
62 // Get the input workspace
63 Workspace_sptr ws = getProperty("Workspace");
64 MatrixWorkspace_sptr inputW = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
65 DataObjects::PeaksWorkspace_sptr inputP = std::dynamic_pointer_cast<DataObjects::PeaksWorkspace>(ws);
66
67 // Get some stuff from the input workspace
69 if (inputW) {
70 inst = inputW->getInstrument();
71 if (!inst)
72 throw std::runtime_error("Could not get a valid instrument from the "
73 "MatrixWorkspace provided as input");
74 } else if (inputP) {
75 inst = inputP->getInstrument();
76 if (!inst)
77 throw std::runtime_error("Could not get a valid instrument from the "
78 "PeaksWorkspace provided as input");
79 } else {
80 if (!inst)
81 throw std::runtime_error("Could not get a valid instrument from the "
82 "workspace and it does not seem to be valid as "
83 "input (must be either MatrixWorkspace or "
84 "PeaksWorkspace");
85 }
86 const std::string ComponentName = getProperty("ComponentName");
87 const int DetID = getProperty("DetectorID");
88 const double X = getProperty("X");
89 const double Y = getProperty("Y");
90 const double Z = getProperty("Z");
91 const bool relativePosition = getProperty("RelativePosition");
92 const bool moveFixedDetectors = getProperty("MoveFixedDetectors");
93
95 // Find the component to move
96 if (DetID != -1) {
97 comp = inst->getDetector(DetID);
98 if (comp == nullptr) {
99 std::ostringstream mess;
100 mess << "Detector with ID " << DetID << " was not found.";
101 g_log.error(mess.str());
102 throw std::runtime_error(mess.str());
103 }
104 } else if (!ComponentName.empty()) {
105 comp = inst->getComponentByName(ComponentName);
106 if (comp == nullptr) {
107 std::ostringstream mess;
108 mess << "Component with name " << ComponentName << " was not found.";
109 g_log.error(mess.str());
110 throw std::runtime_error(mess.str());
111 }
112 } else {
113 g_log.error("DetectorID or ComponentName must be given.");
114 throw std::invalid_argument("DetectorID or ComponentName must be given.");
115 }
116
117 auto &componentInfo = inputW ? inputW->mutableComponentInfo() : inputP->mutableComponentInfo();
118 auto compIndex = componentInfo.indexOf(comp->getComponentID());
119 if (!moveFixedDetectors && ComponentInfoBankHelpers::isDetectorFixedInBank(componentInfo, compIndex)) {
120 // DetectorInfo makes changing positions possible but we keep the old
121 // behavior of ignoring position changes for Structured banks.
122 g_log.warning("Component is fixed within a structured bank, moving is not "
123 "possible, doing nothing.");
124 return;
125 }
126
127 // Do the move
128 V3D position(X, Y, Z);
129 if (relativePosition)
130 position += comp->getPos();
131
132 const auto componentId = comp->getComponentID();
133 componentInfo.setPosition(componentInfo.indexOf(componentId), position);
134}
135
136} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
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.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
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:108
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
Class for 3D vectors.
Definition V3D.h:34
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
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:167
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