Mantid
Loading...
Searching...
No Matches
MatrixWorkspaceMDIterator.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 "MantidKernel/System.h"
13#include "MantidKernel/VMD.h"
15
16using namespace Mantid::Kernel;
17using namespace Mantid::API;
18
19namespace Mantid::API {
20
21//----------------------------------------------------------------------------------------------
30 Mantid::Geometry::MDImplicitFunction *function, size_t beginWI,
31 size_t endWI)
32 : m_ws(workspace), m_pos(0), m_max(0), m_function(function), m_errorIsCached(false),
33 m_spectrumInfo(m_ws->spectrumInfo()) {
34 if (!m_ws)
35 throw std::runtime_error("MatrixWorkspaceMDIterator::ctor() NULL MatrixWorkspace");
36 m_center = VMD(2);
39
40 m_beginWI = beginWI;
42 throw std::runtime_error("MatrixWorkspaceMDIterator: Beginning workspace "
43 "index passed is too high.");
44
45 // End point (handle default)
46 m_endWI = endWI;
49 if (m_endWI < m_beginWI)
50 throw std::runtime_error("MatrixWorkspaceMDIterator: End point is before the start point.");
51
52 // calculate the indices and the largest index we accept
53 m_max = 0;
55 for (size_t i = m_beginWI; i < m_endWI; ++i) {
56 m_startIndices.emplace_back(m_max);
57 m_max += m_ws->readY(i).size();
58 }
59
60 m_xIndex = 0;
61 // Trigger the calculation for the first index
62 m_workspaceIndex = size_t(-1); // This makes sure calcWorkspacePos() updates
64}
65
66//----------------------------------------------------------------------------------------------
68size_t MatrixWorkspaceMDIterator::getDataSize() const { return size_t(m_max); }
69
70//----------------------------------------------------------------------------------------------
77 m_pos = static_cast<uint64_t>(index); // index into the unraveled workspace
78 const auto lower = std::lower_bound(m_startIndices.begin(), m_startIndices.end(), index);
79 m_xIndex = m_pos - (*lower); // index into the Y[] array of the spectrum
80 size_t newWI = m_beginWI + std::distance(m_startIndices.begin(), lower);
81 calcWorkspacePos(newWI);
82}
83
84//----------------------------------------------------------------------------------------------
87 if (newWI >= m_endWI)
88 return;
89
90 if (newWI != m_workspaceIndex) {
91 m_workspaceIndex = newWI;
92 // Copy the vectors. This is more thread-safe
95 m_errorIsCached = false;
97
98 // Find the vertical bin size
100 NumericAxis *ax1 = dynamic_cast<NumericAxis *>(m_ws->getAxis(1));
101 if (ax1) {
102 const MantidVec &yVals = ax1->getValues();
103 if (yVals.size() > 1) {
104 if (m_workspaceIndex < yVals.size() - 1)
106 else
108 }
109 }
110 }
111}
112
113//----------------------------------------------------------------------------------------------
118bool MatrixWorkspaceMDIterator::valid() const { return (m_pos < m_max); }
119
120//----------------------------------------------------------------------------------------------
126 if (m_function) {
127 do {
128 m_pos++;
129 m_xIndex++;
130 if (m_xIndex >= m_Y.size()) {
131 m_xIndex = 0;
133 }
134 this->getCenter();
135 // Keep incrementing until you are in the implicit function
137 // Is the iteration finished?
138 return (m_pos < m_max);
139 } else {
140 // Go through every point;
141 m_pos++;
142 m_xIndex++;
143 if (m_xIndex >= m_Y.size()) {
144 m_xIndex = 0;
146 }
147 return (m_pos < m_max);
148 }
149}
150
151//----------------------------------------------------------------------------------------------
155 this->jumpTo(m_pos + skip);
156 return (m_pos < m_max);
157}
158
159//----------------------------------------------------------------------------------------------
162 // What is our normalization factor?
163 switch (m_normalization) {
164 case NoNormalization:
165 return m_Y[m_xIndex];
167 return m_Y[m_xIndex] / (m_verticalBinSize * (m_X[m_xIndex + 1] - m_X[m_xIndex]));
169 return m_Y[m_xIndex];
170 }
171 return std::numeric_limits<signal_t>::quiet_NaN();
172}
173
174//----------------------------------------------------------------------------------------------
177 // What is our normalization factor?
178 switch (m_normalization) {
179 case NoNormalization:
180 return getError();
182 return getError() / (m_verticalBinSize * (m_X[m_xIndex + 1] - m_X[m_xIndex]));
184 return getError();
185 }
186 return std::numeric_limits<signal_t>::quiet_NaN();
187}
188
191
194 if (!m_errorIsCached) {
196 m_errorIsCached = true;
197 }
198
199 return m_E[m_xIndex];
200}
201
202//----------------------------------------------------------------------------------------------
204std::unique_ptr<coord_t[]> MatrixWorkspaceMDIterator::getVertexesArray(size_t & /*numVertices*/) const {
205 throw std::runtime_error("MatrixWorkspaceMDIterator::getVertexesArray() not implemented yet");
206}
207
208std::unique_ptr<coord_t[]> MatrixWorkspaceMDIterator::getVertexesArray(size_t & /*numVertices*/,
209 const size_t /*outDimensions*/,
210 const bool * /*maskDim*/) const {
211 throw std::runtime_error("MatrixWorkspaceMDIterator::getVertexesArray() not implemented yet");
212}
213
214//----------------------------------------------------------------------------------------------
217 // Place the point in X dimension
218 if (m_isBinnedData)
219 m_center[0] = VMD_t((m_X[m_xIndex] + m_X[m_xIndex + 1]) / 2.0);
220 else
221 m_center[0] = VMD_t(m_X[m_xIndex]);
222 return m_center;
223}
224
225//----------------------------------------------------------------------------------------------
228size_t MatrixWorkspaceMDIterator::getNumEvents() const { return 1; }
229
230//----------------------------------------------------------------------------------------------
232uint16_t MatrixWorkspaceMDIterator::getInnerExpInfoIndex(size_t /*index*/) const { return 0; }
233
235uint16_t MatrixWorkspaceMDIterator::getInnerGoniometerIndex(size_t /*index*/) const { return 0; }
236
238int32_t MatrixWorkspaceMDIterator::getInnerDetectorID(size_t /*index*/) const { return 0; }
239
241coord_t MatrixWorkspaceMDIterator::getInnerPosition(size_t /*index*/, size_t dimension) const {
242 return this->getCenter()[dimension];
243}
244
246signal_t MatrixWorkspaceMDIterator::getInnerSignal(size_t /*index*/) const { return this->getSignal(); }
247
249signal_t MatrixWorkspaceMDIterator::getInnerError(size_t /*index*/) const { return this->getError(); }
250
258 return true;
259 }
261}
262
268 throw std::runtime_error("MatrixWorkspaceMDIterator does not implement findNeighbourIndexes");
269}
270
276 throw std::runtime_error("MatrixWorkspaceMDIterator does not implement "
277 "findNeighbourIndexesFaceTouching");
278}
279
281 throw std::runtime_error("MatrixWorkspaceMDIterator does not implement getLinearIndex");
282}
283
284bool MatrixWorkspaceMDIterator::isWithinBounds(const size_t /*index*/) const {
285 throw std::runtime_error("MatrixWorkspaceMDIterator does not implement isWithinBounds");
286}
287
288} // namespace Mantid::API
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
specnum_t m_max
double lower
lower and upper bounds on the multiplier, if known
Mantid::API::MDNormalization m_normalization
Normalization method for getNormalizedSignal()
Definition: IMDIterator.h:125
std::unique_ptr< coord_t[]> getVertexesArray(size_t &numVertices) const override
Return a list of vertexes defining the volume pointed to.
Mantid::Kernel::VMD m_center
Coordinates of the center at the current iterator pos.
uint16_t getInnerExpInfoIndex(size_t index) const override
For a given event/point in this box, return the associated experiment-info index.
Mantid::Geometry::MDImplicitFunction * m_function
Implicit function to limit volume searched.
uint64_t m_max
The maximum linear index in the workspace.
signal_t getSignal() const override
Returns the signal for this box, same as innerSignal.
uint16_t getInnerGoniometerIndex(size_t index) const override
For a given event/point in this box, return the associated experiment-info index.
bool m_isBinnedData
Is the matrix workspace binned (true) e.g. Y vector is 1 shorter than X.
bool getIsMasked() const override
Getter for the masked state of the workspace.
size_t getNumEvents() const override
Returns the number of events/points contained in this box.
double m_verticalBinSize
For numeric axes, this is the size of the bin in the vertical direction.
MantidVec m_X
Cached copies of X,Y,E at current workspace index.
size_t getLinearIndex() const override
Get the linear index.
bool next() override
Advance to the next cell.
bool m_errorIsCached
Error vector has been cached?
std::vector< size_t > findNeighbourIndexesFaceTouching() const override
Find neighbour indexes face touching.
void jumpTo(size_t index) override
Jump to the index^th cell.
signal_t getInnerSignal(size_t index) const override
Returns the signal of a given event.
std::vector< size_t > findNeighbourIndexes() const override
Find neighbour indexes.
Mantid::Geometry::IMDDimension_const_sptr m_dimY
The Y (vertical, e.g. spectra) dimension.
size_t m_workspaceIndex
Workspace index of the spectrum we are looking at.
int32_t getInnerDetectorID(size_t index) const override
For a given event/point in this box, return the detector ID.
signal_t getInnerError(size_t index) const override
Returns the error of a given event.
uint64_t m_pos
The linear position/index into the MDHistoWorkspace.
bool isWithinBounds(size_t index) const override
Is index reachable by the iterator.
signal_t getNormalizedError() const override
Returns the normalized error for this box.
const SpectrumInfo & m_spectrumInfo
SpectrumInfo object, used for masking information.
signal_t getError() const override
Returns the error for this box, same as innerError.
coord_t getInnerPosition(size_t index, size_t dimension) const override
Returns the position of a given event for a given dimension.
size_t m_endWI
Workspace index at which the iterator ends.
size_t m_xIndex
x-index, index into the Y[] data array of the spectrum.
const MatrixWorkspace * m_ws
Workspace being iterated.
Mantid::Kernel::VMD getCenter() const override
Returns the position of the center of the box pointed to.
size_t m_beginWI
Workspace index at which the iterator begins.
void calcWorkspacePos(size_t newWI)
Calculate the workspace index/x index for this iterator position.
MatrixWorkspaceMDIterator(const MatrixWorkspace *workspace, Mantid::Geometry::MDImplicitFunction *function, size_t beginWI=0, size_t endWI=size_t(-1))
Constructor.
signal_t getNormalizedSignal() const override
Returns the normalized signal for this box.
std::vector< size_t > m_startIndices
vector of starting index of the unraveled data array
Base MatrixWorkspace Abstract Class.
const MantidVec & readE(std::size_t const index) const
Deprecated, use e() instead.
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
const MantidVec & readY(std::size_t const index) const
Deprecated, use y() instead.
virtual Axis * getAxis(const std::size_t &axisIndex) const
Get a non owning pointer to a workspace axis.
const MantidVec & readX(std::size_t const index) const
Deprecated, use x() instead.
std::shared_ptr< const Mantid::Geometry::IMDDimension > getDimension(size_t index) const override
Get a dimension.
virtual bool isHistogramData() const
Returns true if the workspace contains data in histogram form (as opposed to point-like)
Class to represent a numeric axis of a workspace.
Definition: NumericAxis.h:29
virtual const std::vector< double > & getValues() const
Return a const reference to the values.
bool hasDetectors(const size_t index) const
Returns true if the spectrum is associated with detectors in the instrument.
bool isMasked(const size_t index) const
Returns true if the detector(s) associated with the spectrum are masked.
An "ImplicitFunction" defining a hyper-cuboid-shaped region in N dimensions.
virtual bool isPointContained(const coord_t *coords)
Is a point in MDimensions contained by this ImplicitFunction? If the point is bounded by ALL planes c...
@ VolumeNormalization
Divide the signal by the volume of the box/bin.
Definition: IMDIterator.h:29
@ NumEventsNormalization
Divide the signal by the number of events that contributed to it.
Definition: IMDIterator.h:31
@ NoNormalization
Don't normalize = return raw counts.
Definition: IMDIterator.h:27
VMDBase< VMD_t > VMD
Define the VMD as using the double or float data type.
Definition: VMD.h:86
float VMD_t
Underlying data type for the VMD type.
Definition: VMD.h:83
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition: MDTypes.h:27
double signal_t
Typedef for the signal recorded in a MDBox, etc.
Definition: MDTypes.h:36
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition: cow_ptr.h:172