Mantid
Loading...
Searching...
No Matches
ScaleInstrumentComponent.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2024 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 {
16namespace DataHandling {
17
18DECLARE_ALGORITHM(ScaleInstrumentComponent)
19
20using namespace Kernel;
21using namespace Geometry;
22using namespace API;
23
25
27 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("Workspace", "", Direction::InOut),
28 "The name of the workspace containing the instrument component to be scaled.");
29 declareProperty("ComponentName", "",
30 "The name of the component to scale. Component names are "
31 "defined in the instrument definition files. A pathname "
32 "delineated by '/' may be used for non-unique name.");
33 declareProperty(std::make_unique<ArrayProperty<double>>("Scalings", "1.0, 1.0, 1.0"),
34 "A 3D vector specifying the scaling factors for the component.");
35
36 declareProperty("ScalePixelSizes", true, "Scale the pixel dimensions of the detector.");
37}
38std::map<std::string, std::string> ScaleInstrumentComponent::validateInputs() {
39 std::map<std::string, std::string> result;
40
41 // Retrieve the workspace property and attempt to cast it to appropriate types
42 Workspace_sptr ws = getProperty("Workspace");
43 MatrixWorkspace_sptr inputW = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
44 DataObjects::PeaksWorkspace_sptr inputP = std::dynamic_pointer_cast<DataObjects::PeaksWorkspace>(ws);
45
46 Instrument_const_sptr inst = nullptr;
47
48 // Determine the type of workspace and get the instrument
49 if (inputW) {
50 inst = inputW->getInstrument();
51 m_componentInfo = inst ? &inputW->mutableComponentInfo() : nullptr;
52 } else if (inputP) {
53 inst = inputP->getInstrument();
54 m_componentInfo = inst ? &inputP->mutableComponentInfo() : nullptr;
55 } else {
56 result["Workspace"] = "Input workspace must be either MatrixWorkspace or PeaksWorkspace.";
57 return result;
58 }
59
60 // Validate instrument extraction
61 if (!inst) {
62 result["Workspace"] = "Could not get a valid instrument from the provided workspace.";
63 return result;
64 }
65
66 // Validate and retrieve the component by name
67 std::string componentName = getProperty("ComponentName");
68 if (componentName.empty()) {
69 result["ComponentName"] = "ComponentName must be provided.";
70 return result;
71 }
72
73 m_comp = inst->getComponentByName(componentName);
74 if (!m_comp) {
75 result["ComponentName"] = "Component with name " + componentName + " was not found.";
76 return result;
77 }
78
79 // Validate component info
80 if (!m_componentInfo) {
81 result["ComponentInfo"] = "Could not get component info from the workspace.";
82 return result;
83 }
84
85 // Check if the component is a detector
86 const auto componentIndex = m_componentInfo->indexOf(m_comp->getComponentID());
87 if (m_componentInfo->isDetector(componentIndex)) {
88 result["ComponentName"] = "Cannot scale a detector. Please provide a non-detector component name.";
89 }
90
91 return result;
92}
93
97
98 std::vector<double> scalingsXYZ = getProperty("Scalings");
99 Kernel::V3D scalings(scalingsXYZ[0], scalingsXYZ[1], scalingsXYZ[2]);
100 const bool scalePixels = getProperty("ScalePixelSizes");
101
102 const auto componentId = m_comp->getComponentID();
103 m_componentInfo->scaleComponent(m_componentInfo->indexOf(componentId), scalings);
104
105 if (scalePixels) {
106 std::vector<size_t> detectors;
107 detectors = m_componentInfo->detectorsInSubtree(m_componentInfo->indexOf(componentId));
108 for (const auto &detector : detectors) {
109 auto oldScale = m_componentInfo->scaleFactor(detector);
111 detector, V3D(oldScale.X() * scalings.X(), oldScale.Y() * scalings.Y(), oldScale.Z() * scalings.Z()));
112 }
113 }
114}
115
116} // namespace DataHandling
117} // namespace Mantid
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
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.
A property class for workspaces.
Mantid::Geometry::IComponent_const_sptr m_comp
void init() override
Implement abstract Algorithm methods.
void exec() override
Implement abstract Algorithm methods.
std::map< std::string, std::string > validateInputs() override
Perform validation of ALL the input properties of the algorithm.
void scaleComponent(const size_t componentIndex, const Kernel::V3D &newScaling)
void setScaleFactor(const size_t componentIndex, const Kernel::V3D &scaleFactor)
std::vector< size_t > detectorsInSubtree(size_t componentIndex) const
bool isDetector(const size_t componentIndex) const
size_t indexOf(Geometry::IComponent const *id) const
Kernel::V3D scaleFactor(const size_t componentIndex) const
Support for a property that holds an array of values.
Class for 3D vectors.
Definition V3D.h:34
constexpr double X() const noexcept
Get x.
Definition V3D.h:238
constexpr double Y() const noexcept
Get y.
Definition V3D.h:239
constexpr double Z() const noexcept
Get z.
Definition V3D.h:240
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.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
Helper class which provides the Collimation Length for SANS instruments.
@ InOut
Both an input & output workspace.
Definition Property.h:55