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 +
16#include "MantidKernel/System.h"
17
18using namespace Mantid::Kernel;
19using namespace Mantid::API;
20using namespace Mantid::Geometry;
21
22namespace Mantid::Algorithms {
23
24// Register the algorithm into the AlgorithmFactory
25DECLARE_ALGORITHM(ResizeRectangularDetector)
26
27//----------------------------------------------------------------------------------------------
29const std::string ResizeRectangularDetector::name() const { return "ResizeRectangularDetector"; }
30
32int ResizeRectangularDetector::version() const { return 1; }
33
35const std::string ResizeRectangularDetector::category() const { return "DataHandling\\Instrument"; }
36
37//----------------------------------------------------------------------------------------------
38
39//----------------------------------------------------------------------------------------------
43 // When used as a Child Algorithm the workspace name is not used - hence the
44 // "Anonymous" to satisfy the validator
45 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("Workspace", "Anonymous", Direction::InOut));
46 declareProperty("ComponentName", "", "The name of the RectangularDetector to resize.");
47 declareProperty("ScaleX", 1.0, "The scaling factor in the X direction. Default 1.0");
48 declareProperty("ScaleY", 1.0, "The scaling factor in the Y direction. Default 1.0");
49}
50
51//----------------------------------------------------------------------------------------------
55 // Get the input workspace
56 Workspace_sptr ws = getProperty("Workspace");
57 MatrixWorkspace_sptr inputW = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
58 DataObjects::PeaksWorkspace_sptr inputP = std::dynamic_pointer_cast<DataObjects::PeaksWorkspace>(ws);
59
60 // Get some stuff from the input workspace
61 Instrument_sptr inst;
62 if (inputW) {
63 inst = std::const_pointer_cast<Instrument>(inputW->getInstrument());
64 if (!inst)
65 throw std::runtime_error("Could not get a valid instrument from the "
66 "MatrixWorkspace provided as input");
67
68 } else if (inputP) {
69 inst = std::const_pointer_cast<Instrument>(inputP->getInstrument());
70 if (!inst)
71 throw std::runtime_error("Could not get a valid instrument from the "
72 "PeaksWorkspace provided as input");
73 } else {
74 throw std::runtime_error("Could not get a valid instrument from the "
75 "workspace and it does not seem to be valid as "
76 "input (must be either MatrixWorkspace or "
77 "PeaksWorkspace");
78 }
79
80 std::string ComponentName = getPropertyValue("ComponentName");
81 double ScaleX = getProperty("ScaleX");
82 double ScaleY = getProperty("ScaleY");
83
84 if (ComponentName.empty())
85 throw std::runtime_error("You must specify a ComponentName.");
86
88
89 comp = inst->getComponentByName(ComponentName);
90 if (!comp)
91 throw std::runtime_error("Component with name " + ComponentName + " was not found.");
92
93 RectangularDetector_const_sptr det = std::dynamic_pointer_cast<const RectangularDetector>(comp);
94 if (!det)
95 throw std::runtime_error("Component with name " + ComponentName + " is not a RectangularDetector.");
96
97 auto input = std::dynamic_pointer_cast<ExperimentInfo>(ws);
98 Geometry::ParameterMap &pmap = input->instrumentParameters();
99 auto oldscalex = pmap.getDouble(det->getName(), std::string("scalex"));
100 auto oldscaley = pmap.getDouble(det->getName(), std::string("scaley"));
101 // Add a parameter for the new scale factors
102 pmap.addDouble(det->getComponentID(), "scalex", ScaleX);
103 pmap.addDouble(det->getComponentID(), "scaley", ScaleY);
104 pmap.clearPositionSensitiveCaches();
105
106 // Positions of detectors are now stored in DetectorInfo, so we must update
107 // positions there.
108 // This algorithm is setting the absolute scale factor. Since there may be a
109 // previous scaling we have to factor that out.
110 double relscalex = ScaleX;
111 double relscaley = ScaleY;
112 if (!oldscalex.empty())
113 relscalex /= oldscalex[0];
114 if (!oldscaley.empty())
115 relscaley /= oldscaley[0];
116 applyRectangularDetectorScaleToComponentInfo(input->mutableComponentInfo(), comp->getComponentID(), relscalex,
117 relscaley);
118}
119
120} // 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
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
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:34
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
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< const IComponent > IComponent_const_sptr
Typdef of a shared pointer to a const IComponent.
Definition: IComponent.h:161
std::shared_ptr< const RectangularDetector > RectangularDetector_const_sptr
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