Mantid
Loading...
Searching...
No Matches
EQSANSInstrument.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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
10#include <utility>
11
16
23/*
24 * Read a parameter from the instrument description
25 */
26double readInstrumentParameter(const std::string &parameter, const API::MatrixWorkspace_sptr &dataWS) {
27 std::vector<double> pars = dataWS->getInstrument()->getNumberParameter(parameter);
28 if (pars.empty())
29 throw Kernel::Exception::InstrumentDefinitionError("Unable to find [" + parameter + "] instrument parameter");
30 return pars[0];
31}
32
33/*
34 * Return the detector ID corresponding to the [x,y] pixel coordinates.
35 */
36int getDetectorFromPixel(const int &pixel_x, const int &pixel_y, const API::MatrixWorkspace_sptr &dataWS) {
37 int ny_pixels = static_cast<int>(readInstrumentParameter("number-of-y-pixels", dataWS));
38 return ny_pixels * pixel_x + pixel_y;
39}
40
41/*
42 * Returns the real-space coordinates corresponding to the
43 * given pixel coordinates [m].
44 */
45void getCoordinateFromPixel(const double &pixel_x, const double &pixel_y, const API::MatrixWorkspace_sptr &dataWS,
46 double &x, double &y) {
47 const int nx_pixels = static_cast<int>(readInstrumentParameter("number-of-x-pixels", dataWS));
48 const int ny_pixels = static_cast<int>(readInstrumentParameter("number-of-y-pixels", dataWS));
49 const double pixel_size_x = readInstrumentParameter("x-pixel-size", dataWS);
50 const double pixel_size_y = readInstrumentParameter("y-pixel-size", dataWS);
51 x = (nx_pixels / 2.0 - 0.5 - pixel_x) * pixel_size_x / 1000.0;
52 y = (pixel_y - ny_pixels / 2.0 + 0.5) * pixel_size_y / 1000.0;
53}
54
55/*
56 * Returns the pixel coordinates corresponding to the given real-space position.
57 * This assumes that the center of the detector is aligned
58 * with the beam. An additional offset may need to be applied
59 * @param x: real-space x coordinate [m]
60 * @param y: real-space y coordinate [m]
61 */
62void getPixelFromCoordinate(const double &x, const double &y, const API::MatrixWorkspace_sptr &dataWS, double &pixel_x,
63 double &pixel_y) {
64 const int nx_pixels = static_cast<int>(readInstrumentParameter("number-of-x-pixels", dataWS));
65 const int ny_pixels = static_cast<int>(readInstrumentParameter("number-of-y-pixels", dataWS));
66 const double pixel_size_x = readInstrumentParameter("x-pixel-size", dataWS);
67 const double pixel_size_y = readInstrumentParameter("y-pixel-size", dataWS);
68 pixel_x = -x / pixel_size_x * 1000.0 + nx_pixels / 2.0 - 0.5;
69 pixel_y = y / pixel_size_y * 1000.0 + ny_pixels / 2.0 - 0.5;
70}
71
72/*
73 * Returns the default beam center position, or the pixel location
74 * of real-space coordinates (0,0).
75 */
76void getDefaultBeamCenter(const API::MatrixWorkspace_sptr &dataWS, double &pixel_x, double &pixel_y) {
77 getPixelFromCoordinate(0.0, 0.0, dataWS, pixel_x, pixel_y);
78}
79
80} // namespace Mantid::WorkflowAlgorithms::EQSANSInstrument
Exception for errors associated with the instrument definition.
Definition: Exception.h:220
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
void getDefaultBeamCenter(const API::MatrixWorkspace_sptr &dataWS, double &pixel_x, double &pixel_y)
double readInstrumentParameter(const std::string &parameter, const API::MatrixWorkspace_sptr &dataWS)
File change history is stored at: https://github.com/mantidproject/mantid Code Documentation is avail...
int getDetectorFromPixel(const int &pixel_x, const int &pixel_y, const API::MatrixWorkspace_sptr &dataWS)
void getPixelFromCoordinate(const double &x, const double &y, const API::MatrixWorkspace_sptr &dataWS, double &pixel_x, double &pixel_y)
void getCoordinateFromPixel(const double &pixel_x, const double &pixel_y, const API::MatrixWorkspace_sptr &dataWS, double &x, double &y)