Mantid
Loading...
Searching...
No Matches
MDHistoWorkspace.h
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#pragma once
8
13#include "MantidDataObjects/DllConfig.h"
19
20namespace Mantid {
21namespace DataObjects {
22
37class MANTID_DATAOBJECTS_DLL MDHistoWorkspace : public API::IMDHistoWorkspace {
38public:
44
45 MDHistoWorkspace(std::vector<Mantid::Geometry::MDHistoDimension_sptr> &dimensions,
47 MDHistoWorkspace(std::vector<Mantid::Geometry::IMDDimension_sptr> &dimensions,
50
52 std::unique_ptr<MDHistoWorkspace> clone() const { return std::unique_ptr<MDHistoWorkspace>(doClone()); }
53
55 std::unique_ptr<MDHistoWorkspace> cloneEmpty() const { return std::unique_ptr<MDHistoWorkspace>(doCloneEmpty()); }
56
57 void init(std::vector<Mantid::Geometry::MDHistoDimension_sptr> &dimensions);
58 void init(std::vector<Mantid::Geometry::IMDDimension_sptr> &dimensions);
59
60 void cacheValues();
61
62 const std::string id() const override { return "MDHistoWorkspace"; }
63
64 size_t getMemorySize() const override;
65
68 uint64_t getNPoints() const override { return m_length; }
70 uint64_t getNEvents() const override;
71 std::vector<std::unique_ptr<Mantid::API::IMDIterator>>
72 createIterators(size_t suggestedNumCores = 1,
73 Mantid::Geometry::MDImplicitFunction *function = nullptr) const override;
74
75 LinePlot getLinePlot(const Mantid::Kernel::VMD &start, const Mantid::Kernel::VMD &end,
76 Mantid::API::MDNormalization normalize) const override;
77
78 LinePlot getLineData(const Mantid::Kernel::VMD &start, const Mantid::Kernel::VMD &end,
79 Mantid::API::MDNormalization normalize) const override;
80
81 void checkWorkspaceSize(const MDHistoWorkspace &other, const std::string &operation);
82
83 // --------------------------------------------------------------------------------------------
85 void add(const MDHistoWorkspace &b);
86 void add(const signal_t signal, const signal_t error);
87
89 void subtract(const MDHistoWorkspace &b);
90 void subtract(const signal_t signal, const signal_t error);
91
93 void multiply(const MDHistoWorkspace &b_ws);
94 void multiply(const signal_t signal, const signal_t error);
95
97 void divide(const MDHistoWorkspace &b_ws);
98 void divide(const signal_t signal, const signal_t error);
99
100 void log(double filler = 0.0);
101 void log10(double filler = 0.0);
102 void exp();
103 void power(double exponent);
104
105 // --------------------------------------------------------------------------------------------
106
108 MDHistoWorkspace &operator|=(const MDHistoWorkspace &b);
109 MDHistoWorkspace &operator^=(const MDHistoWorkspace &b);
110 void operatorNot();
111
112 void lessThan(const MDHistoWorkspace &b);
113 void lessThan(const signal_t signal);
114 void greaterThan(const MDHistoWorkspace &b);
115 void greaterThan(const signal_t signal);
116 void equalTo(const MDHistoWorkspace &b, const signal_t tolerance = 1e-5);
117 void equalTo(const signal_t signal, const signal_t tolerance = 1e-5);
118
119 void setUsingMask(const MDHistoWorkspace &mask, const MDHistoWorkspace &values);
120 void setUsingMask(const MDHistoWorkspace &mask, const signal_t signal, const signal_t error);
121
122 // --------------------------------------------------------------------------------------------
127 const size_t *getIndexMultiplier() const { return indexMultiplier.data(); }
128
130 const signal_t *getSignalArray() const override { return m_signals.data(); }
131
134 coord_t getInverseVolume() const override { return m_inverseVolume; }
135
137 const signal_t *getErrorSquaredArray() const override { return m_errorsSquared.data(); }
138
141 const signal_t *getNumEventsArray() const override { return m_numEvents.data(); }
142
145 const bool *getMaskArray() const { return m_masks.get(); }
146
149 const coord_t *getBinWidths() const { return m_boxLength.data(); }
150
153 signal_t *mutableSignalArray() override { return m_signals.data(); }
154
157 signal_t *mutableErrorSquaredArray() override { return m_errorsSquared.data(); }
158
161 signal_t *mutableNumEventsArray() override { return m_numEvents.data(); }
162
165 bool *mutableMaskArray() { return m_masks.get(); }
166
168 Kernel::SpecialCoordinateSystem getSpecialCoordinateSystem() const override;
169
171 void setCoordinateSystem(const Kernel::SpecialCoordinateSystem coordinateSystem) override;
172
173 void setTo(signal_t signal, signal_t errorSquared, signal_t numEvents) override;
174
175 void applyImplicitFunction(Mantid::Geometry::MDImplicitFunction *function, signal_t signal, signal_t errorSquared);
176
177 std::unique_ptr<coord_t[]> getVertexesArray(size_t linearIndex, size_t &numVertices) const;
178
179 Kernel::VMD getCenter(size_t linearIndex) const override;
180
182 signal_t getSignalAtCoord(const coord_t *coords, const Mantid::API::MDNormalization &normalization) const override;
183
185 // or 0 if masked
186 signal_t getSignalWithMaskAtCoord(const coord_t *coords,
187 const Mantid::API::MDNormalization &normalization) const override;
188
190 void setSignalAt(size_t index, signal_t value) override { m_signals[index] = value; }
191
193 void setErrorSquaredAt(size_t index, signal_t value) override { m_errorsSquared[index] = value; }
194
196 void setNumEventsAt(size_t index, signal_t value) { m_numEvents[index] = value; }
197
200 signal_t getNumEventsAt(size_t index) const { return m_numEvents[index]; }
201
203 signal_t getErrorAt(size_t index) const override { return std::sqrt(m_errorsSquared[index]); }
204
207 signal_t getErrorAt(size_t index1, size_t index2) const override {
208 return std::sqrt(m_errorsSquared[index1 + indexMultiplier[0] * index2]);
209 }
210
213 signal_t getErrorAt(size_t index1, size_t index2, size_t index3) const override {
214 return std::sqrt(m_errorsSquared[index1 + indexMultiplier[0] * index2 + indexMultiplier[1] * index3]);
215 }
216
219 signal_t getErrorAt(size_t index1, size_t index2, size_t index3, size_t index4) const override {
220 return std::sqrt(m_errorsSquared[index1 + indexMultiplier[0] * index2 + indexMultiplier[1] * index3 +
221 indexMultiplier[2] * index4]);
222 }
223
227 bool getIsMaskedAt(size_t index) const { return m_masks[index]; }
228
230 signal_t getSignalAt(size_t index) const override { return m_signals[index]; }
231
234 signal_t getSignalAt(size_t index1, size_t index2) const override {
235 return m_signals[index1 + indexMultiplier[0] * index2];
236 }
237
240 signal_t getSignalAt(size_t index1, size_t index2, size_t index3) const override {
241 return m_signals[index1 + indexMultiplier[0] * index2 + indexMultiplier[1] * index3];
242 }
243
246 signal_t getSignalAt(size_t index1, size_t index2, size_t index3, size_t index4) const override {
247 return m_signals[index1 + indexMultiplier[0] * index2 + indexMultiplier[1] * index3 + indexMultiplier[2] * index4];
248 }
249
251 signal_t getSignalNormalizedAt(size_t index) const override { return m_signals[index] * m_inverseVolume; }
252
255 signal_t getSignalNormalizedAt(size_t index1, size_t index2) const override {
256 return m_signals[index1 + indexMultiplier[0] * index2] * m_inverseVolume;
257 }
258
261 signal_t getSignalNormalizedAt(size_t index1, size_t index2, size_t index3) const override {
262 return m_signals[index1 + indexMultiplier[0] * index2 + indexMultiplier[1] * index3] * m_inverseVolume;
263 }
264
267 signal_t getSignalNormalizedAt(size_t index1, size_t index2, size_t index3, size_t index4) const override {
268 return m_signals[index1 + indexMultiplier[0] * index2 + indexMultiplier[1] * index3 + indexMultiplier[2] * index4] *
269 m_inverseVolume;
270 }
271
274 signal_t getErrorNormalizedAt(size_t index) const override {
275 return std::sqrt(m_errorsSquared[index]) * m_inverseVolume;
276 }
277
280 signal_t getErrorNormalizedAt(size_t index1, size_t index2) const override {
281 return getErrorAt(index1, index2) * m_inverseVolume;
282 }
283
286 signal_t getErrorNormalizedAt(size_t index1, size_t index2, size_t index3) const override {
287 return getErrorAt(index1, index2, index3) * m_inverseVolume;
288 }
289
292 signal_t getErrorNormalizedAt(size_t index1, size_t index2, size_t index3, size_t index4) const override {
293 return getErrorAt(index1, index2, index3, index4) * m_inverseVolume;
294 }
295
296 //---------------------------------------------------------------------------------------------
299 signal_t &errorSquaredAt(size_t index) override {
300 if (index < m_length)
301 return m_errorsSquared[index];
302 else
303 throw std::invalid_argument("MDHistoWorkspace::array index out of range");
304 }
305
308 signal_t &signalAt(size_t index) override {
309 if (index < m_length)
310 return m_signals[index];
311 else
312 throw std::invalid_argument("MDHistoWorkspace::array index out of range");
313 }
314
315 //---------------------------------------------------------------------------------------------
316 size_t getLinearIndex(size_t index1, size_t index2) const override {
317 if (this->getNumDims() != 2)
318 throw std::runtime_error("Workspace does not have 2 dimensions!");
319 return index1 + indexMultiplier[0] * index2;
320 }
321
322 size_t getLinearIndex(size_t index1, size_t index2, size_t index3) const override {
323 if (this->getNumDims() != 3)
324 throw std::runtime_error("Workspace does not have 3 dimensions!");
325 return index1 + indexMultiplier[0] * index2 + indexMultiplier[1] * index3;
326 }
327
328 size_t getLinearIndex(size_t index1, size_t index2, size_t index3, size_t index4) const override {
329 if (this->getNumDims() != 4)
330 throw std::runtime_error("Workspace does not have 4 dimensions!");
331 return index1 + indexMultiplier[0] * index2 + indexMultiplier[1] * index3 + indexMultiplier[2] * index4;
332 }
333
338 size_t getLinearIndex(size_t *index) const {
339 size_t nd = this->getNumDims();
340 size_t out = index[0];
341 for (size_t d = 1; d < nd; d++)
342 out += indexMultiplier[d - 1] * index[d];
343 return out;
344 }
345
346 size_t getLinearIndexAtCoord(const coord_t *coords) const;
347
352 signal_t &operator[](const size_t &index) override {
353 if (index < m_length)
354 return m_signals[index];
355 else
356 throw std::invalid_argument("MDHistoWorkspace::array index out of range");
357 }
358
361 virtual std::vector<signal_t> getSignalDataVector() const;
362 virtual std::vector<signal_t> getErrorDataVector() const;
363
365 void setMDMasking(std::unique_ptr<Mantid::Geometry::MDImplicitFunction> maskingRegion) override;
367 void setMDMaskAt(const size_t &index, bool mask);
368
370 void clearMDMasking() override;
372 uint64_t sumNContribEvents() const;
373 void updateSum() { m_nEventsContributed = sumNContribEvents(); }
374
376 static size_t sizeOfElement();
377
379 Mantid::API::MDNormalization displayNormalization() const override;
380
382 Mantid::API::MDNormalization displayNormalizationHisto() const override;
383
384 void setDisplayNormalization(const Mantid::API::MDNormalization &preferredNormalization) override;
385
387 bool isMDHistoWorkspace() const override { return true; }
388
389private:
390 MDHistoWorkspace *doClone() const override { return new MDHistoWorkspace(*this); }
391
392 MDHistoWorkspace *doCloneEmpty() const override { return new MDHistoWorkspace(nullptr); }
393
394 void makeSingleBinWithNaN(std::vector<coord_t> &x, std::vector<signal_t> &y, std::vector<signal_t> &e) const;
395
396 void initVertexesArray();
397
400
402 std::vector<signal_t> m_signals;
403
405 std::vector<signal_t> m_errorsSquared;
406
408 std::vector<signal_t> m_numEvents;
409
411 size_t m_length;
412
415 std::vector<size_t> indexMultiplier;
417 std::vector<size_t> m_indexMaker;
419 std::vector<size_t> m_indexMax;
420
423
425 std::vector<coord_t> m_vertexesArray;
426
428 std::vector<coord_t> m_boxLength;
429
431 std::vector<coord_t> m_origin;
433 mutable uint64_t m_nEventsContributed;
434
436
439
440 // Get ordered list of boundaries in position-along-the-line coordinates
441 std::set<coord_t> getBinBoundariesOnLine(const Kernel::VMD &start, const Kernel::VMD &end, size_t nd,
442 const Kernel::VMD &dir, coord_t length) const;
443
444 signal_t getNormalizationFactor(const API::MDNormalization &normalize, size_t linearIndex) const;
445
446protected:
447 LinePlot getLinePoints(const Mantid::Kernel::VMD &start, const Mantid::Kernel::VMD &end,
448 Mantid::API::MDNormalization normalize, const bool bin_centres) const;
449
452
455 std::unique_ptr<bool[]> m_masks;
456};
457
459using MDHistoWorkspace_sptr = std::shared_ptr<MDHistoWorkspace>;
460
462using MDHistoWorkspace_const_sptr = std::shared_ptr<const MDHistoWorkspace>;
463
464} // namespace DataObjects
465} // namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
double error
Definition: IndexPeaks.cpp:133
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
double tolerance
Abstract interface to MDHistoWorkspace, for use in exposing to Python.
signal_t & signalAt(size_t index) override
signal_t getErrorNormalizedAt(size_t index1, size_t index2, size_t index3, size_t index4) const override
Get the signal at the specified index given in 4 dimensions (typically X,Y,Z,t), normalized by cell v...
coord_t m_inverseVolume
Inverse of the volume of EACH cell.
size_t getLinearIndex(size_t index1, size_t index2, size_t index3) const override
bool isMDHistoWorkspace() const override
Return if this workspace is a MDHistoWorkspace. Will always return true.
std::unique_ptr< bool[]> m_masks
Linear array of masks for each bin.
const signal_t * getNumEventsArray() const override
signal_t getNumEventsAt(size_t index) const
Returns the number of contributing events from the bin at the specified index.
MDHistoWorkspace & operator=(const MDHistoWorkspace &other)=delete
const size_t * getIndexMultiplier() const
std::vector< size_t > m_indexMax
Max index into each dimension.
signal_t getSignalNormalizedAt(size_t index1, size_t index2, size_t index3, size_t index4) const override
Get the signal at the specified index given in 4 dimensions (typically X,Y,Z,t), normalized by cell v...
signal_t getSignalNormalizedAt(size_t index) const override
Get the signal at the specified index, normalized by cell volume.
std::unique_ptr< MDHistoWorkspace > cloneEmpty() const
Returns a default-initialized clone of the workspace.
size_t numDimensions
Number of dimensions in this workspace.
signal_t getErrorNormalizedAt(size_t index1, size_t index2) const override
Get the signal at the specified index given in 4 dimensions (typically X,Y,Z,t), normalized by cell v...
signal_t getErrorAt(size_t index) const override
Get the error of the signal at the specified index.
size_t getLinearIndex(size_t index1, size_t index2) const override
const coord_t * getBinWidths() const
Return the aray of bin withs (the linear length of a box) for each dimension.
signal_t getErrorAt(size_t index1, size_t index2, size_t index3, size_t index4) const override
Get the error at the specified index given in 4 dimensions (typically X,Y,Z,t)
signal_t getSignalAt(size_t index) const override
Get the signal at the specified index.
void setNumEventsAt(size_t index, signal_t value)
Sets the number of contributing events in the bin at the specified index.
std::unique_ptr< MDHistoWorkspace > clone() const
Returns a clone of the workspace.
std::vector< coord_t > m_origin
Vector of the origin in each dimension.
MDHistoWorkspace * doCloneEmpty() const override
Virtual cloneEmpty method.
signal_t getErrorAt(size_t index1, size_t index2, size_t index3) const override
Get the error at the specified index given in 4 dimensions (typically X,Y,Z,t)
uint64_t m_nEventsContributed
the number of events, contributed into the workspace;
signal_t & operator[](const size_t &index) override
Array subscript operator.
coord_t getInverseVolume() const override
uint64_t getNPoints() const override
Get the number of points (bins in this case) associated with the workspace;.
std::vector< signal_t > m_errorsSquared
Linear array of errors for each bin.
std::vector< coord_t > m_boxLength
Vector of the length of the box in each dimension.
signal_t getSignalAt(size_t index1, size_t index2, size_t index3, size_t index4) const override
Get the signal at the specified index given in 4 dimensions (typically X,Y,Z,t)
size_t m_length
Length of the m_signals / m_errorsSquared arrays.
const std::string id() const override
A string ID for the class.
size_t getLinearIndex(size_t index1, size_t index2, size_t index3, size_t index4) const override
MDHistoWorkspace & operator&=(const MDHistoWorkspace &b)
std::vector< size_t > m_indexMaker
For converting to/from linear index to tdimensions.
const signal_t * getErrorSquaredArray() const override
void makeSingleBinWithNaN(std::vector< coord_t > &x, std::vector< signal_t > &y, std::vector< signal_t > &e) const
std::vector< coord_t > m_vertexesArray
Pre-calculated vertexes array for the 0th box.
signal_t & errorSquaredAt(size_t index) override
signal_t getSignalNormalizedAt(size_t index1, size_t index2, size_t index3) const override
Get the signal at the specified index given in 4 dimensions (typically X,Y,Z,t), normalized by cell v...
signal_t getErrorNormalizedAt(size_t index) const override
Get the error of the signal at the specified index, normalized by cell volume.
bool getIsMaskedAt(size_t index) const
Getter for the masking at a specified linear index.
signal_t getSignalNormalizedAt(size_t index1, size_t index2) const override
Get the signal at the specified index given in 4 dimensions (typically X,Y,Z,t), normalized by cell v...
Mantid::API::MDNormalization m_displayNormalization
Display normalization to use.
signal_t getErrorAt(size_t index1, size_t index2) const override
Get the error at the specified index given in 4 dimensions (typically X,Y,Z,t)
const signal_t * getSignalArray() const override
signal_t getErrorNormalizedAt(size_t index1, size_t index2, size_t index3) const override
Get the signal at the specified index given in 4 dimensions (typically X,Y,Z,t), normalized by cell v...
std::vector< size_t > indexMultiplier
To find the index into the linear array, dim0 + indexMultiplier[0]*dim1 + ...
signal_t getSignalAt(size_t index1, size_t index2) const override
Get the signal at the specified index given in 4 dimensions (typically X,Y,Z,t)
std::vector< signal_t > m_signals
Linear array of signals for each bin.
void setSignalAt(size_t index, signal_t value) override
Sets the signal at the specified index.
void setErrorSquaredAt(size_t index, signal_t value) override
Sets the error (squared) at the specified index.
size_t getLinearIndex(size_t *index) const
Get the linear index into the array.
signal_t * mutableErrorSquaredArray() override
MDHistoWorkspace * doClone() const override
Virtual clone method. Not implemented to force implementation in children.
signal_t getSignalAt(size_t index1, size_t index2, size_t index3) const override
Get the signal at the specified index given in 4 dimensions (typically X,Y,Z,t)
signal_t * mutableNumEventsArray() override
std::vector< signal_t > m_numEvents
Number of contributing events for each bin.
Kernel::SpecialCoordinateSystem m_coordSystem
An "ImplicitFunction" defining a hyper-cuboid-shaped region in N dimensions.
MatrixWorkspace_sptr MANTID_API_DLL operator/=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Divide two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator+=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Adds two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator-=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Subtracts two workspaces.
MDNormalization
Enum describing different ways to normalize the signal in a MDWorkspace.
Definition: IMDIterator.h:25
@ NoNormalization
Don't normalize = return raw counts.
Definition: IMDIterator.h:27
MatrixWorkspace_sptr MANTID_API_DLL operator*=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Multiply two workspaces.
std::shared_ptr< MDHistoWorkspace > MDHistoWorkspace_sptr
A shared pointer to a MDHistoWorkspace.
std::shared_ptr< const MDHistoWorkspace > MDHistoWorkspace_const_sptr
A shared pointer to a const MDHistoWorkspace.
std::shared_ptr< MDHistoDimension > MDHistoDimension_sptr
Shared pointer to a MDHistoDimension.
SpecialCoordinateSystem
Special coordinate systems for Q3D.
Helper class which provides the Collimation Length for SANS instruments.
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
Holds X, Y, E for a line plot.
Definition: IMDWorkspace.h:48