Mantid
Loading...
Searching...
No Matches
SpectraAxis.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//----------------------------------------------------------------------
15#include "MantidKernel/Unit.h"
17
18namespace Mantid::API {
19
20using std::size_t;
21
25SpectraAxis::SpectraAxis(const MatrixWorkspace *const parentWorkspace)
26 : Axis(), m_parentWS(parentWorkspace), m_edges() {
27 this->unit() = std::make_shared<Kernel::Units::Label>("Spectrum", "");
28}
29
34Axis *SpectraAxis::clone(const MatrixWorkspace *const parentWorkspace) {
35 auto newAxis = new SpectraAxis(parentWorkspace);
36 // A couple of base class members need copying over manually
37 newAxis->title() = this->title();
38 newAxis->unit() = this->unit();
39 return newAxis;
40}
41
47Axis *SpectraAxis::clone(const std::size_t length, const MatrixWorkspace *const parentWorkspace) {
49 // In this implementation, there's no difference between the clone methods -
50 // call the other one
51 return clone(parentWorkspace);
52}
53
54std::size_t SpectraAxis::length() const { return m_parentWS->getNumberHistograms(); }
55
63double SpectraAxis::operator()(const std::size_t &index, const std::size_t &verticalIndex) const {
64 UNUSED_ARG(verticalIndex)
65 if (index >= length()) {
66 throw Kernel::Exception::IndexError(index, length() - 1, "SpectraAxis: Index out of range.");
67 }
68
69 return static_cast<double>(m_parentWS->getSpectrum(index).getSpectrumNo());
70}
71
77void SpectraAxis::setValue(const std::size_t &index, const double &value) {
80 throw std::domain_error("setValue method cannot be used on a SpectraAxis.");
81}
82
89size_t SpectraAxis::indexOfValue(const double value) const {
90 if (m_edges.empty()) // lazy-instantiation
91 {
93 const size_t npts = m_edges.size() - 1;
94 for (size_t i = 0; i < npts - 1; ++i) {
95 m_edges[i + 1] = 0.5 * (this->getValue(i) + this->getValue(i + 1));
96 }
97 // ends
98 m_edges[0] = this->getValue(0) - (m_edges[1] - this->getValue(0));
99 m_edges[npts] = this->getValue(npts - 1) + (this->getValue(npts - 1) - m_edges[npts - 1]);
100 }
102}
103
109specnum_t SpectraAxis::spectraNo(const std::size_t &index) const {
110 if (index >= length()) {
111 throw Kernel::Exception::IndexError(index, length() - 1, "SpectraAxis: Index out of range.");
112 }
113
115}
116
122 size_t nel = length();
123
124 if (nel == 0)
125 throw std::runtime_error("getSpectraIndexMap(), zero elements");
126 spec2index_map map;
127 for (size_t i = 0; i < nel; ++i) {
128 map.emplace(m_parentWS->getSpectrum(i).getSpectrumNo(), i);
129 }
130 return map;
131}
132
137bool SpectraAxis::operator==(const Axis &axis2) const {
138 if (length() != axis2.length()) {
139 return false;
140 }
141 const auto *spec2 = dynamic_cast<const SpectraAxis *>(&axis2);
142 if (!spec2) {
143 return false;
144 }
145 for (size_t i = 0; i < length(); ++i) {
146 if (spectraNo(i) != axis2.spectraNo(i))
147 return false;
148 }
149 // All good if we get to here
150 return true;
151}
152
158std::string SpectraAxis::label(const std::size_t &index) const { return "sp-" + std::to_string(spectraNo(index)); }
159
162
165
166} // namespace Mantid::API
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
Class to represent the axis of a workspace.
Definition: Axis.h:30
const std::string & title() const
Returns the user-defined title for this axis.
Definition: Axis.cpp:20
virtual specnum_t spectraNo(const std::size_t &index) const
Get the spectrum number.
Definition: Axis.cpp:60
virtual std::size_t length() const =0
Get the length of the axis.
const std::shared_ptr< Kernel::Unit > & unit() const
The unit for this axis.
Definition: Axis.cpp:28
double getValue(const std::size_t &index, const std::size_t &verticalIndex=0) const
Gets the value at the specified index.
Definition: Axis.cpp:51
specnum_t getSpectrumNo() const
Definition: ISpectrum.cpp:123
Base MatrixWorkspace Abstract Class.
virtual ISpectrum & getSpectrum(const size_t index)=0
Return the underlying ISpectrum ptr at the given workspace index.
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
Class to represent the spectra axis of a workspace.
Definition: SpectraAxis.h:31
size_t indexOfValue(const double value) const override
Finds the index of the given value on the axis.
Definition: SpectraAxis.cpp:89
spec2index_map getSpectraIndexMap() const
Returns a map where spectra is the key and index is the value This is used for efficient search of sp...
specnum_t spectraNo(const std::size_t &index) const override
Returns the spectrum number at the position given (Spectra axis only)
double operator()(const std::size_t &index, const std::size_t &verticalIndex=0) const override
Get the axis value at the position given.
Definition: SpectraAxis.cpp:63
double getMin() const override
returns min value defined on axis
std::string label(const std::size_t &index) const override
Returns a text label which shows the value at index and identifies the type of the axis.
Axis * clone(const MatrixWorkspace *const parentWorkspace) override
Virtual constructor.
Definition: SpectraAxis.cpp:34
std::vector< double > m_edges
List of edge values for quick searching of values as if this is binned data.
Definition: SpectraAxis.h:64
double getMax() const override
returns max value defined on axis
const MatrixWorkspace *const m_parentWS
A pointer to the workspace holding the axis.
Definition: SpectraAxis.h:61
SpectraAxis()
Default constructor.
void setValue(const std::size_t &index, const double &value) override
Sets the axis value at a given position.
Definition: SpectraAxis.cpp:77
bool operator==(const Axis &) const override
Check if two axis defined as spectra or numeric axis are equivalent.
std::size_t length() const override
Get the length of the axis.
Definition: SpectraAxis.cpp:54
Exception for index errors.
Definition: Exception.h:284
size_t MANTID_KERNEL_DLL indexOfValueFromEdges(const std::vector< double > &bin_edges, const double value)
Gets the bin of a value from a vector of bin edges.
std::unordered_map< specnum_t, size_t > spec2index_map
Map with key = spectrum number, value = workspace index.
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16
std::string to_string(const wide_integer< Bits, Signed > &n)