Mantid
Loading...
Searching...
No Matches
MDBoxBase.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
11#include "MantidAPI/IMDNode.h"
18#include "MantidKernel/VMD.h"
19#include <iosfwd>
20#include <mutex>
21
23#define MDBOX_TRACK_CENTROID
24
25namespace Mantid {
26namespace DataObjects {
27
28#ifndef __INTEL_COMPILER // As of July 13, the packing has no effect for the
29 // Intel compiler and produces a warning
30#pragma pack(push, 4) // Ensure the structure is no larger than it needs to
31#endif
32
33//===============================================================================================
50class MANTID_DATAOBJECTS_DLL MDBoxBase : public Mantid::API::IMDNode {
51public:
52 //-----------------------------------------------------------------------------------------------
53 MDBoxBase(Mantid::API::BoxController *const boxController = nullptr, const uint32_t depth = 0,
54 const size_t boxID = UNDEF_SIZET);
55
56 MDBoxBase(Mantid::API::BoxController *const boxController, const uint32_t depth, const size_t boxID,
57 const std::vector<Mantid::Geometry::MDDimensionExtents<coord_t>> &extentsVector);
58
60
62 std::string getEventType() const override { return MDE::getTypeName(); }
65 unsigned int getCoordType() const override { return sizeof(coord_t); }
66
67 bool isLeaf() const override final { return getNumChildren() == 0; }
68
71 size_t getID() const override { return m_fileID; }
74 void setID(const size_t &newID) override { m_fileID = newID; }
75 // -------------------------------- Parents/Children-Related
76 // -------------------------------------------
78 void setParent(IMDNode *parent) override { m_parent = parent; }
79
81 IMDNode *getParent() override { return m_parent; }
82
84 const IMDNode *getParent() const override { return m_parent; }
85
87 const IMDNode *getBoxAtCoord(const coord_t * /*coords*/) override { return this; }
88
89 // -------------------------------- Events-Related
90 // -------------------------------------------
94 void getEventsData(std::vector<coord_t> & /*coordTable*/, size_t &nColumns) const override { nColumns = 0; }
100 void setEventsData(const std::vector<coord_t> & /*coordTable*/) override {}
102 virtual std::vector<MDE> *getEventsCopy() = 0;
103
104 //----------------------------------------------------------------------------------------------------------------------
106 virtual size_t addEvent(const MDE &point) = 0;
108 virtual size_t addEventUnsafe(const MDE &point) = 0;
109 //----------------------------------------------------------------------------------------------------------------------
110 // add range of events
111 virtual size_t addEvents(const std::vector<MDE> &events);
112 virtual size_t addEventsUnsafe(const std::vector<MDE> &events);
113 //----------------------------------------------------------------------------------------------------------------------
119 virtual void centerpointBin(MDBin<MDE, nd> &bin, bool *fullyContained) const = 0;
120
122 virtual void generalBin(MDBin<MDE, nd> &bin, Mantid::Geometry::MDImplicitFunction &function) const = 0;
123
125 void integrateSphere(Mantid::API::CoordTransform &radiusTransform, const coord_t radiusSquared, signal_t &signal,
126 signal_t &errorSquared, const coord_t innerRadiusSquared = 0.0,
127 const bool useOnePercentBackgroundCorrection = true) const override = 0;
128
130 void centroidSphere(Mantid::API::CoordTransform &radiusTransform, const coord_t radiusSquared, coord_t *centroid,
131 signal_t &signal) const override = 0;
132
134 void integrateCylinder(Mantid::API::CoordTransform &radiusTransform, const coord_t radius, const coord_t length,
135 signal_t &signal, signal_t &errorSquared,
136 std::vector<signal_t> &signal_fit) const override = 0;
137
138 // -------------------------------------------------------------------------------------------
140 Mantid::API::BoxController *getBoxController() const override { return m_BoxController; }
142 Mantid::API::BoxController *getBoxController() override { return m_BoxController; }
143
144 // -------------------------------- Geometry/vertexes-Related
145 // -------------------------------------------
146
147 std::vector<Mantid::Kernel::VMD> getVertexes() const override;
148 std::unique_ptr<coord_t[]> getVertexesArray(size_t &numVertices) const override;
149 std::unique_ptr<coord_t[]> getVertexesArray(size_t &numVertices, const size_t outDimensions,
150 const bool *maskDim) const override;
151 void transformDimensions(std::vector<double> &scaling, std::vector<double> &offset) override;
152
153 //-----------------------------------------------------------------------------------------------
163 void setExtents(size_t dim, double min, double max) {
164 if (dim >= nd)
165 throw std::invalid_argument("Invalid dimension passed to MDBox::setExtents");
166
167 extents[dim].setExtents(min, max);
168 // volume has to be recalculated as extents have changed;
169 this->calcVolume();
170 }
175 void setExtents(double min[nd], double max[nd]) {
176 for (size_t dim = 0; dim < nd; dim++) {
177 this->extents[dim].setExtents(min[dim], max[dim]);
178 }
179 this->calcVolume();
180 }
181
182 //-----------------------------------------------------------------------------------------------
184 Mantid::Geometry::MDDimensionExtents<coord_t> &getExtents(size_t dim) override { return extents[dim]; }
185
186 //-----------------------------------------------------------------------------------------------
188 std::string getExtentsStr() const {
189 std::string mess;
190 size_t ndm1 = nd - 1;
191 if (ndm1 > 32)
192 return mess;
193
194 for (size_t d = 0; d < ndm1; ++d)
195 mess += extents[d].extentsStr() + ",";
196
197 mess += extents[ndm1].extentsStr();
198 return mess;
199 }
200
203 coord_t getBoxSize(size_t d) { return extents[d].getSize(); }
204
205 //-----------------------------------------------------------------------------------------------
210 void getCenter(coord_t *const center) const override {
211 for (size_t d = 0; d < nd; ++d)
212 center[d] = extents[d].getCentre();
213 }
214
215 //-----------------------------------------------------------------------------------------------
219 inline void calcVolume() override {
220 double volume(1);
221 for (size_t d = 0; d < nd; d++) {
222 volume *= double(extents[d].getSize());
223 }
226 m_inverseVolume = coord_t(1. / volume);
227 }
228
229 //-----------------------------------------------------------------------------------------------
232 signal_t getSignal() const override { return m_signal; }
233
234 //-----------------------------------------------------------------------------------------------
237 signal_t getError() const override { return sqrt(m_errorSquared); }
238
239 //-----------------------------------------------------------------------------------------------
242 signal_t getErrorSquared() const override { return m_errorSquared; }
243
244 //-----------------------------------------------------------------------------------------------
248 virtual signal_t getTotalWeight() const { return m_totalWeight; }
249
250 //-----------------------------------------------------------------------------------------------
255 void setSignal(const signal_t signal) override { m_signal = signal; }
256
257 //-----------------------------------------------------------------------------------------------
262 void setErrorSquared(const signal_t ErrorSquared) override { m_errorSquared = ErrorSquared; }
263
264 //-----------------------------------------------------------------------------------------------
268 virtual void setTotalWeight(const signal_t total) { m_totalWeight = total; }
269
270 //-----------------------------------------------------------------------------------------------
274 signal_t getSignalNormalized() const override { return m_signal * m_inverseVolume; }
275
276 //-----------------------------------------------------------------------------------------------
280 virtual signal_t getErrorSquaredNormalized() const { return m_errorSquared * m_inverseVolume; }
281
282 //-----------------------------------------------------------------------------------------------
286 uint32_t getDepth() const override { return m_depth; }
287
288 //-----------------------------------------------------------------------------------------------
292 void setDepth(uint32_t depth) { m_depth = depth; }
293
294 //-----------------------------------------------------------------------------------------------
296 coord_t getVolume() const { return 1.0f / m_inverseVolume; }
297
298 //-----------------------------------------------------------------------------------------------
300 coord_t getInverseVolume() const override { return m_inverseVolume; }
301
302 //-----------------------------------------------------------------------------------------------
305 void setInverseVolume(const coord_t invVolume) override { m_inverseVolume = invVolume; }
306
307 virtual void calculateGridCaches() {}
308
309protected:
316 template <typename EventIterator> void calcCaches(const EventIterator &begin, const EventIterator &end);
321
322 mutable coord_t m_centroid[nd];
326
330
334
337
340
342 uint32_t m_depth;
343
346
349 size_t m_fileID;
351 std::mutex m_dataMutex;
352
353private:
355
356public:
358 using sptr = std::shared_ptr<MDBoxBase<MDE, nd>>;
359
360}; //(end class MDBoxBase)
361
362#ifndef __INTEL_COMPILER
363#pragma pack(pop) // Return to default packing size
364#endif
365
366template <typename MDE, size_t nd>
367template <typename EventIterator>
368void MDBoxBase<MDE, nd>::calcCaches(const EventIterator &begin, const EventIterator &end) {
369 m_signal = 0;
370 m_errorSquared = 0;
371 m_totalWeight = 0;
372 coord_t *centroid = m_centroid;
373 std::fill_n(centroid, nd, 0.0f);
374 for (auto it = begin; it != end; ++it) {
375 auto evSignal = it->getSignal();
376 m_signal += evSignal;
377 m_errorSquared += it->getErrorSquared();
379 ++m_totalWeight;
380 for (auto d = 0u; d < nd; d++) {
381 // Total up the coordinate weighted by the signal.
382 centroid[d] += it->getCenter(d) * static_cast<coord_t>(evSignal);
383 }
384 }
385
386 // Normalize by the total signal
387 const coord_t reciprocal = 1.0f / static_cast<coord_t>(m_signal);
388 for (size_t d = 0; d < nd; ++d) {
389 centroid[d] *= reciprocal;
390 }
391}
392
393} // namespace DataObjects
394} // namespace Mantid
#define UNDEF_SIZET
Definition: MDTypes.h:60
#define TMDE_CLASS
Macro to make declaring template classes faster.
Definition: MDTypes.h:58
double radius
Definition: Rasterize.cpp:31
This class is used by MDBox and MDGridBox in order to intelligently determine optimal behavior.
Definition: BoxController.h:33
Unique SingleValueParameter Declaration for InputNDimensions.
MDBin : Class describing a single bin in a dense, Multidimensional histogram.
Definition: MDBin.h:32
Templated super-class of a multi-dimensional event "box".
Definition: MDBoxBase.h:50
std::unique_ptr< coord_t[]> getVertexesArray(size_t &numVertices) const override
virtual size_t addEvents(const std::vector< MDE > &events)
MDBoxBase(Mantid::API::BoxController *const boxController=nullptr, const uint32_t depth=0, const size_t boxID=UNDEF_SIZET)
virtual size_t addEventUnsafe(const MDE &point)=0
Add a single event, with no mutex locking.
virtual signal_t getTotalWeight() const
Returns the total weight of all events within.
Definition: MDBoxBase.h:248
const IMDNode * getBoxAtCoord(const coord_t *) override
Returns the lowest-level box at the given coordinates.
Definition: MDBoxBase.h:87
virtual void generalBin(MDBin< MDE, nd > &bin, Mantid::Geometry::MDImplicitFunction &function) const =0
General binning method for any shape.
void setExtents(double min[nd], double max[nd])
Set the extents of this box.
Definition: MDBoxBase.h:175
std::string getExtentsStr() const
Returns the extents as a string, for convenience.
Definition: MDBoxBase.h:188
MDBoxBase(const MDBoxBase< MDE, nd > &box, Mantid::API::BoxController *const otherBC)
virtual void centerpointBin(MDBin< MDE, nd > &bin, bool *fullyContained) const =0
Perform centerpoint binning of events.
virtual std::vector< MDE > * getEventsCopy()=0
Return a copy of contained events.
void setID(const size_t &newID) override
sets the special id, which specify the position of this node in the chain linearly ordered nodes
Definition: MDBoxBase.h:74
void setSignal(const signal_t signal) override
Sets the integrated signal from all points within (mostly used for testing)
Definition: MDBoxBase.h:255
signal_t getSignal() const override
Returns the integrated signal from all points within.
Definition: MDBoxBase.h:232
std::shared_ptr< MDBoxBase< MDE, nd > > sptr
Convenience typedef for a shared pointer to a this type of class.
Definition: MDBoxBase.h:358
signal_t m_errorSquared
Cached total error (squared) from all points within.
Definition: MDBoxBase.h:329
void calcCaches(const EventIterator &begin, const EventIterator &end)
Calculates caches if the events are known.
Definition: MDBoxBase.h:368
virtual signal_t getErrorSquaredNormalized() const
Returns the integrated error squared from all points within, normalized for the cell volume.
Definition: MDBoxBase.h:280
void setParent(IMDNode *parent) override
Return a pointer to the parent box.
Definition: MDBoxBase.h:78
MDBoxBase(Mantid::API::BoxController *const boxController, const uint32_t depth, const size_t boxID, const std::vector< Mantid::Geometry::MDDimensionExtents< coord_t > > &extentsVector)
void getCenter(coord_t *const center) const override
Get the center of the box.
Definition: MDBoxBase.h:210
coord_t getInverseVolume() const override
Return the inverse of the volume of the cell.
Definition: MDBoxBase.h:300
virtual void calculateGridCaches()
Definition: MDBoxBase.h:307
void setDepth(uint32_t depth)
For testing, mostly: set the recursion depth of this box.
Definition: MDBoxBase.h:292
bool isLeaf() const override final
Is this node a leaf: getNumChildren() == 0.
Definition: MDBoxBase.h:67
uint32_t getDepth() const override
For testing, mostly: return the recursion depth of this box.
Definition: MDBoxBase.h:286
signal_t getErrorSquared() const override
Returns the integrated error squared from all points within.
Definition: MDBoxBase.h:242
IMDNode * getParent() override
Return a pointer to the parent box.
Definition: MDBoxBase.h:81
void transformDimensions(std::vector< double > &scaling, std::vector< double > &offset) override
std::mutex m_dataMutex
Mutex for modifying the event list or box averages.
Definition: MDBoxBase.h:351
coord_t getBoxSize(size_t d)
For testing: return the internal-stored size of each box in each dimension.
Definition: MDBoxBase.h:203
signal_t m_signal
Cached total signal from all points within.
Definition: MDBoxBase.h:325
signal_t getError() const override
Returns the integrated error from all points within.
Definition: MDBoxBase.h:237
virtual void setTotalWeight(const signal_t total)
Sets the total weight from all points within (mostly used for testing)
Definition: MDBoxBase.h:268
size_t m_fileID
The id which specify location of this box in a linear chain of ordered boxes (e.g.
Definition: MDBoxBase.h:349
void setEventsData(const std::vector< coord_t > &) override
The method to convert the table of data into vector of events Used to convert from a vector of values...
Definition: MDBoxBase.h:100
void calcVolume() override
Compute the volume of the box by simply multiplying each dimension range.
Definition: MDBoxBase.h:219
void centroidSphere(Mantid::API::CoordTransform &radiusTransform, const coord_t radiusSquared, coord_t *centroid, signal_t &signal) const override=0
Find the centroid around a sphere.
Mantid::API::BoxController * getBoxController() const override
Definition: MDBoxBase.h:140
coord_t m_inverseVolume
Inverse of the volume of the cell, to be used for normalized signal.
Definition: MDBoxBase.h:339
coord_t getVolume() const
Return the volume of the cell.
Definition: MDBoxBase.h:296
void integrateSphere(Mantid::API::CoordTransform &radiusTransform, const coord_t radiusSquared, signal_t &signal, signal_t &errorSquared, const coord_t innerRadiusSquared=0.0, const bool useOnePercentBackgroundCorrection=true) const override=0
Sphere (peak) integration.
virtual size_t addEvent(const MDE &point)=0
Add a single event.
void integrateCylinder(Mantid::API::CoordTransform &radiusTransform, const coord_t radius, const coord_t length, signal_t &signal, signal_t &errorSquared, std::vector< signal_t > &signal_fit) const override=0
Cylinder (peak) integration.
std::string getEventType() const override
Definition: MDBoxBase.h:62
std::vector< Mantid::Kernel::VMD > getVertexes() const override
void setErrorSquared(const signal_t ErrorSquared) override
Sets the integrated error squared from all points within (mostly used for testing)
Definition: MDBoxBase.h:262
virtual size_t addEventsUnsafe(const std::vector< MDE > &events)
const IMDNode * getParent() const override
Return a pointer to the parent box (const)
Definition: MDBoxBase.h:84
signal_t getSignalNormalized() const override
Returns the integrated signal from all points within, normalized for the cell volume.
Definition: MDBoxBase.h:274
uint32_t m_depth
Recursion depth.
Definition: MDBoxBase.h:342
size_t getID() const override
Definition: MDBoxBase.h:71
Mantid::API::BoxController * getBoxController() override
Definition: MDBoxBase.h:142
void setInverseVolume(const coord_t invVolume) override
Sets the inverse of the volume of the cell.
Definition: MDBoxBase.h:305
std::unique_ptr< coord_t[]> getVertexesArray(size_t &numVertices, const size_t outDimensions, const bool *maskDim) const override
Mantid::API::BoxController *const m_BoxController
The box splitting controller, shared with all boxes in the hierarchy.
Definition: MDBoxBase.h:336
Mantid::Geometry::MDDimensionExtents< coord_t > & getExtents(size_t dim) override
Get the extents for this box.
Definition: MDBoxBase.h:184
void setExtents(size_t dim, double min, double max)
Set the extents of this box.
Definition: MDBoxBase.h:163
void getEventsData(std::vector< coord_t > &, size_t &nColumns) const override
The method to convert events in a box into a table of coodrinates/signal/errors casted into coord_t t...
Definition: MDBoxBase.h:94
MDBoxBase(const MDBoxBase< MDE, nd > &box)
unsigned int getCoordType() const override
Definition: MDBoxBase.h:65
Mantid::API::IMDNode * m_parent
Pointer to the parent of this box. NULL if no parent.
Definition: MDBoxBase.h:345
signal_t m_totalWeight
Cached total weight of all events Set when refreshCache() is called.
Definition: MDBoxBase.h:333
Simple class that holds the extents (min/max) of a given dimension in a MD workspace or MDBox.
An "ImplicitFunction" defining a hyper-cuboid-shaped region in N dimensions.
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