Mantid
Loading...
Searching...
No Matches
SpectrumInfo.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 +
12#include "MantidTypes/SpectrumDefinition.h"
13
14#include <boost/python/class.hpp>
15#include <boost/python/copy_const_reference.hpp>
16#include <boost/python/list.hpp>
17#include <boost/python/return_value_policy.hpp>
18#include <boost/python/tuple.hpp>
19
20using Mantid::SpectrumDefinition;
27using namespace boost::python;
28
29// Helper method to make the python iterator
33
34PyObject *geographicalAngles(const SpectrumInfo &spectrumInfo, const size_t index) {
35 const auto angles = spectrumInfo.geographicalAngles(index);
36 return incref(make_tuple(angles.first, angles.second).ptr());
37}
38
39namespace {
41V3D observerOrSample(const SpectrumInfo &self, const object &observer) {
42 return observer.is_none() ? self.samplePosition() : extract<V3D>(observer)();
43}
44
46V3D axisOrBeam(const SpectrumInfo &self, const object &axis) {
47 if (!axis.is_none())
48 return extract<V3D>(axis)();
49 const auto beamLine = self.samplePosition() - self.sourcePosition();
50 if (beamLine.nullVector())
51 throw Mantid::Kernel::Exception::InstrumentDefinitionError("Source and sample are at same position!");
52 return beamLine;
53}
54
55double twoTheta(const SpectrumInfo &self, const size_t index, const object &observer, const object &axis) {
56 if (observer.is_none() && axis.is_none())
57 return self.twoTheta(index);
58 return self.twoTheta(index, observerOrSample(self, observer), axisOrBeam(self, axis));
59}
60
61double signedTwoTheta(const SpectrumInfo &self, const size_t index, const object &observer, const object &axis,
62 const object &instrumentUp) {
63 if (observer.is_none() && axis.is_none() && instrumentUp.is_none())
64 return self.signedTwoTheta(index);
65 if (instrumentUp.is_none())
66 return self.signedTwoTheta(index, observerOrSample(self, observer), axisOrBeam(self, axis));
67 return self.signedTwoTheta(index, observerOrSample(self, observer), axisOrBeam(self, axis),
68 extract<V3D>(instrumentUp)());
69}
70} // namespace
71
72// Export SpectrumInfo
74 class_<SpectrumInfo, boost::noncopyable>("SpectrumInfo", no_init)
75 .def("__iter__", make_pyiterator)
76 .def("__len__", &SpectrumInfo::size, arg("self"), "Returns the number of spectra.")
77 .def("size", &SpectrumInfo::size, arg("self"), "Returns the number of spectra.")
78 .def("isMonitor", &SpectrumInfo::isMonitor, (arg("self"), arg("index")),
79 "Returns True if the detector(s) associated with the spectrum are "
80 "monitors.")
81 .def("isMasked", &SpectrumInfo::isMasked, (arg("self"), arg("index")),
82 "Returns True if the detector(s) associated with the spectrum are "
83 "masked.")
84 .def("setMasked", &SpectrumInfo::setMasked, (arg("self"), arg("index"), arg("masked")),
85 "Set the mask flag of the spectrum with the given index.")
86 .def("twoTheta", &twoTheta, (arg("self"), arg("index"), arg("observer") = object(), arg("axis") = object()),
87 "Returns the average angle in radians between the vectors from the observer to the detector(s) and the "
88 "axis. The observer defaults to the sample position and the axis to the beam direction, giving the "
89 "scattering angle 2 theta. With both defaults, monitors raise an error.")
90 .def("signedTwoTheta", &signedTwoTheta,
91 (arg("self"), arg("index"), arg("observer") = object(), arg("axis") = object(),
92 arg("instrumentUp") = object()),
93 "Returns the average signed angle in radians between the vectors from the observer to the detector(s) and "
94 "the axis. The observer defaults to the sample position, the axis to the beam direction and instrumentUp "
95 "to the instrument's theta-sign axis, giving the signed scattering angle 2 theta. With all defaults, "
96 "monitors raise an error.")
97 .def("azimuthal", &SpectrumInfo::azimuthal, (arg("self"), arg("index")),
98 "Returns the out-of-plane angle in radians angle w.r.t. to "
99 "vecPointingHorizontal "
100 "direction.")
101 .def("phi", &SpectrumInfo::phi, (arg("self"), arg("index")),
102 "Returns the angle in radians of the spectrum position about the lab-frame z axis, atan2(y, x).")
103 .def("phiOffset", &SpectrumInfo::phiOffset, (arg("self"), arg("index"), arg("offset")),
104 "Returns phi reflected about the offset (radians): offset - phi for phi >= 0, otherwise "
105 "-(offset + phi).")
106 .def("geographicalAngles", &geographicalAngles, (arg("self"), arg("index")),
107 "Returns the latitude and longitude for given spectrum index. "
108 "The returned value is a pair of (latitude, longitude)")
109 .def("l1", &SpectrumInfo::l1, arg("self"), "Returns the distance from the source to the sample.")
110 .def("l2", &SpectrumInfo::l2, (arg("self"), arg("index")),
111 "Returns the distance from the sample to the spectrum.")
112 .def("hasDetectors", &SpectrumInfo::hasDetectors, (arg("self")),
113 "Returns True if the spectrum is associated with detectors in the "
114 "instrument.")
115 .def("hasUniqueDetector", &SpectrumInfo::hasUniqueDetector, (arg("self"), arg("index")),
116 "Returns True if the spectrum is associated with exactly one "
117 "detector.")
118 .def("position", &SpectrumInfo::position, (arg("self"), arg("index")),
119 "Returns the absolute position of the spectrum with the given "
120 "index.")
121 .def("sourcePosition", &SpectrumInfo::sourcePosition, arg("self"), "Returns the absolute source position.")
122 .def("samplePosition", &SpectrumInfo::samplePosition, arg("self"), "Returns the absolute sample position.")
123 .def("getSpectrumDefinition", &SpectrumInfo::spectrumDefinition, return_value_policy<return_by_value>(),
124 (arg("self"), arg("index")),
125 "Returns the SpectrumDefinition of the spectrum with the given "
126 "index.")
127 .def("detectorCount", &SpectrumInfo::detectorCount, arg("self"),
128 "Returns the total number of detectors used across spectrum info.")
129 .def("difcUncalibrated", &SpectrumInfo::difcUncalibrated, (arg("self"), arg("index")),
130 "Return the uncalibrated difc diffractometer constant")
131 .def("diffractometerConstants",
132 (UnitParametersMap (SpectrumInfo::*)(const size_t) const) & SpectrumInfo::diffractometerConstants,
133 (arg("self"), arg("index")), "Return the diffractometer constants");
134}
std::map< DeltaEMode::Type, std::string > index
PyObject * geographicalAngles(const SpectrumInfo &spectrumInfo, const size_t index)
void export_SpectrumInfo()
SpectrumInfoPythonIterator make_pyiterator(SpectrumInfo &spectrumInfo)
API::SpectrumInfo is an intermediate step towards a SpectrumInfo that is part of Instrument-2....
double signedTwoTheta(const size_t index) const
Returns the signed scattering angle 2 theta in radians (angle w.r.t.
double phiOffset(const size_t index, const double offset) const
Returns phi() reflected about offset (radians): offset - phi for phi >= 0 and -(offset + phi) otherwi...
bool isMonitor(const size_t index) const
Returns true if the detector(s) associated with the spectrum are monitors.
Kernel::V3D sourcePosition() const
Returns the source position.
Kernel::UnitParametersMap diffractometerConstants(const size_t index, std::vector< detid_t > &uncalibratedDets) const
Calculate average diffractometer constants (DIFA, DIFC, TZERO) of detectors associated with this spec...
void setMasked(const size_t index, bool masked)
Set the mask flag of the spectrum with given index.
Kernel::V3D samplePosition() const
Returns the sample position.
bool hasDetectors(const size_t index) const
Returns true if the spectrum is associated with detectors in the instrument.
double twoTheta(const size_t index) const
Returns the scattering angle 2 theta in radians (angle w.r.t.
double difcUncalibrated(const size_t index) const
Calculate average uncalibrated DIFC value of detectors associated with this spectrum.
Kernel::V3D position(const size_t index) const
Returns the position of the spectrum with given index.
double l2(const size_t index) const
Returns L2 (distance from sample to spectrum).
double azimuthal(const size_t index) const
Returns the out-of-plane angle in radians (angle w.r.t.
bool isMasked(const size_t index) const
Returns true if the detector(s) associated with the spectrum are masked.
bool hasUniqueDetector(const size_t index) const
Returns true if the spectrum is associated with exactly one detector.
const SpectrumDefinition & spectrumDefinition(const size_t index) const
Returns a const reference to the SpectrumDefinition of the spectrum.
double phi(const size_t index) const
Returns the angle of the spectrum position about the lab-frame z axis, atan2(y, x),...
size_t size() const
Returns the size of the SpectrumInfo, i.e., the number of spectra.
double l1() const
Returns L1 (distance from source to sample).
std::pair< double, double > geographicalAngles(const size_t index) const
Calculate latitude and longitude for given spectrum index.
Exception for errors associated with the instrument definition.
Definition Exception.h:220
Class for 3D vectors.
Definition V3D.h:35
std::unordered_map< UnitParams, double > UnitParametersMap
Definition Unit.h:29