Mantid
Loading...
Searching...
No Matches
ResizeRectangularDetector.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 +
15
16using namespace Mantid::Kernel;
17using namespace Mantid::API;
18using namespace Mantid::Geometry;
19
20namespace Mantid::Algorithms {
21
22// Register the algorithm into the AlgorithmFactory
23DECLARE_ALGORITHM(ResizeRectangularDetector)
24
25//----------------------------------------------------------------------------------------------
27const std::string ResizeRectangularDetector::name() const { return "ResizeRectangularDetector"; }
28
30int ResizeRectangularDetector::version() const { return 1; }
31
33const std::string ResizeRectangularDetector::category() const { return "DataHandling\\Instrument"; }
34
35//----------------------------------------------------------------------------------------------
36
37//----------------------------------------------------------------------------------------------
41 // When used as a Child Algorithm the workspace name is not used - hence the
42 // "Anonymous" to satisfy the validator
43 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("Workspace", "Anonymous", Direction::InOut));
44 declareProperty("ComponentName", "", "The name of the RectangularDetector to resize.");
45 declareProperty("ScaleX", 1.0, "The scaling factor in the X direction. Default 1.0");
46 declareProperty("ScaleY", 1.0, "The scaling factor in the Y direction. Default 1.0");
47}
48
49//----------------------------------------------------------------------------------------------
53 // Get the input workspace
54 Workspace_sptr ws = getProperty("Workspace");
55 MatrixWorkspace_sptr inputW = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
56 DataObjects::PeaksWorkspace_sptr inputP = std::dynamic_pointer_cast<DataObjects::PeaksWorkspace>(ws);
57
58 // Get some stuff from the input workspace
59 Instrument_sptr inst;
60 if (inputW) {
61 inst = std::const_pointer_cast<Instrument>(inputW->getInstrument());
62 if (!inst)
63 throw std::runtime_error("Could not get a valid instrument from the "
64 "MatrixWorkspace provided as input");
65
66 } else if (inputP) {
67 inst = std::const_pointer_cast<Instrument>(inputP->getInstrument());
68 if (!inst)
69 throw std::runtime_error("Could not get a valid instrument from the "
70 "PeaksWorkspace provided as input");
71 } else {
72 throw std::runtime_error("Could not get a valid instrument from the "
73 "workspace and it does not seem to be valid as "
74 "input (must be either MatrixWorkspace or "
75 "PeaksWorkspace");
76 }
77
78 std::string ComponentName = getPropertyValue("ComponentName");
79 double ScaleX = getProperty("ScaleX");
80 double ScaleY = getProperty("ScaleY");
81
82 if (ComponentName.empty())
83 throw std::runtime_error("You must specify a ComponentName.");
84
85 auto input = std::dynamic_pointer_cast<ExperimentInfo>(ws);
86 auto &componentInfo = input->mutableComponentInfo();
87
88 size_t componentIndex;
89 try {
90 componentIndex = componentInfo.indexOfAny(ComponentName);
91 } catch (std::invalid_argument &) {
92 throw std::runtime_error("Component with name " + ComponentName + " was not found.");
93 }
94 if (!componentInfo.isGridDetector(componentIndex)) {
95 throw std::runtime_error("Component with name " + ComponentName + " is not a RectangularDetector.");
96 }
97 Geometry::ParameterMap &pmap = input->instrumentParameters();
98 const std::string &resolvedName = componentInfo.name(componentIndex);
99 auto oldscalex = pmap.getDouble(resolvedName, std::string("scalex"));
100 auto oldscaley = pmap.getDouble(resolvedName, std::string("scaley"));
101 // Add a parameter for the new scale factors
102 auto *componentID = const_cast<IComponent *>(componentInfo.componentID(componentIndex));
103 pmap.addDouble(componentID, "scalex", ScaleX);
104 pmap.addDouble(componentID, "scaley", ScaleY);
105 pmap.clearPositionSensitiveCaches();
106
107 // Positions of detectors are now stored in DetectorInfo, so we must update
108 // positions there.
109 // This algorithm is setting the absolute scale factor. Since there may be a
110 // previous scaling we have to factor that out.
111 double relscalex = ScaleX;
112 double relscaley = ScaleY;
113 if (!oldscalex.empty())
114 relscalex /= oldscalex[0];
115 if (!oldscaley.empty())
116 relscaley /= oldscaley[0];
117 applyRectangularDetectorScaleToComponentInfo(componentInfo, componentID, relscalex, relscaley);
118}
119
120} // namespace Mantid::Algorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:542
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
A property class for workspaces.
ResizeRectangularDetector : TODO: DESCRIPTION.
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
const std::string category() const override
Algorithm's category for identification.
Takes a workspace and adjusts all the time bin values by the same multiplicative factor.
Definition ScaleX.h:35
base class for Geometric IComponent
Definition IComponent.h:53
Parameter map iterator typedef.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
MANTID_API_DLL void applyRectangularDetectorScaleToComponentInfo(Geometry::ComponentInfo &componentInfo, Geometry::IComponent *componentId, const double scaleX, const double scaleY)
Helpers for resizing RectangularDetectors.
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< Instrument > Instrument_sptr
Shared pointer to an instrument object.
Generate a tableworkspace to store the calibration results.
STL namespace.
@ InOut
Both an input & output workspace.
Definition Property.h:55