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