Mantid
Loading...
Searching...
No Matches
MDGeometry.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 +
10
14
15#include <Poco/NObserver.h>
16#include <memory>
17#include <utility>
18
19using namespace Mantid::Kernel;
20using namespace Mantid::API;
21using namespace Mantid::Geometry;
22
23namespace Mantid::API {
24
26public:
30
33 // Stop watching once object is deleted
34 API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_delete_observer);
35 }
37 API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_replace_observer);
38 }
39 }
40
42 if (!m_observingDelete) {
43 API::AnalysisDataService::Instance().notificationCenter.addObserver(m_delete_observer);
44 m_observingDelete = true;
45 }
46 }
47
49 if (!m_observingReplace) {
50 API::AnalysisDataService::Instance().notificationCenter.addObserver(m_replace_observer);
51 m_observingReplace = true;
52 }
53 }
54
58
62
63private:
65
69
71 bool m_observingDelete{false};
72 bool m_observingReplace{false};
73};
74
75//----------------------------------------------------------------------------------------------
79 : m_dimensions(), m_originalWorkspaces(), m_origin(), m_transforms_FromOriginal(), m_transforms_ToOriginal(),
80 m_notificationHelper(std::make_unique<MDGeometryNotificationHelper>(*this)), m_Wtransf(3, 3, true),
81 m_basisVectors() {}
82
83//----------------------------------------------------------------------------------------------
86MDGeometry::MDGeometry(const MDGeometry &other) { *this = other; }
87
89 m_dimensions = std::vector<std::shared_ptr<IMDDimension>>{};
90 m_originalWorkspaces = std::vector<std::shared_ptr<Workspace>>{};
91 m_origin = other.m_origin;
92 m_transforms_FromOriginal = std::vector<std::shared_ptr<const CoordTransform>>{};
93 m_transforms_ToOriginal = std::vector<std::shared_ptr<const CoordTransform>>{};
94 m_notificationHelper = std::make_unique<MDGeometryNotificationHelper>(*this);
95 m_Wtransf = other.m_Wtransf;
96 m_basisVectors = other.m_basisVectors;
97
98 // Perform a deep copy of the dimensions
99 std::vector<Mantid::Geometry::IMDDimension_sptr> dimensions;
100 for (size_t d = 0; d < other.getNumDims(); d++) {
101 // Copy the dimension
102 auto dim = std::make_shared<MDHistoDimension>(other.getDimension(d).get());
103 dimensions.emplace_back(dim);
104 }
105 this->initGeometry(dimensions);
106
107 // Perform a deep copy of the coordinate transformations
108 std::vector<CoordTransform_const_sptr>::const_iterator it;
109 for (it = other.m_transforms_FromOriginal.begin(); it != other.m_transforms_FromOriginal.end(); ++it) {
110 if (*it)
111 m_transforms_FromOriginal.emplace_back(CoordTransform_const_sptr((*it)->clone()));
112 else
114 }
115
116 for (it = other.m_transforms_ToOriginal.begin(); it != other.m_transforms_ToOriginal.end(); ++it) {
117 if (*it)
118 m_transforms_ToOriginal.emplace_back(CoordTransform_const_sptr((*it)->clone()));
119 else
121 }
122
123 // Copy the references to the original workspaces
124 // This will also set up the delete observer to listen to those workspaces
125 // being deleted.
126 for (size_t i = 0; i < other.m_originalWorkspaces.size(); i++)
128
129 return *this;
130}
131
139
144
145//----------------------------------------------------------------------------------------------
149
150//----------------------------------------------------------------------------------------------
156void MDGeometry::initGeometry(const std::vector<Mantid::Geometry::IMDDimension_sptr> &dimensions) {
157 // Copy the dimensions array
158 m_dimensions = dimensions;
159 // Make sure the basis vectors are big enough
161}
162
163// --------------------------------------------------------------------------------------------
165size_t MDGeometry::getNumDims() const { return m_dimensions.size(); }
166
169 return std::count_if(m_dimensions.cbegin(), m_dimensions.cend(),
170 [](const auto &dimension) { return !dimension->getIsIntegrated(); });
171}
172
173// --------------------------------------------------------------------------------------------
178std::shared_ptr<const Mantid::Geometry::IMDDimension> MDGeometry::getDimension(size_t index) const {
179 if (index >= m_dimensions.size())
180 throw std::runtime_error("Workspace does not have a dimension at that index.");
181 return m_dimensions[index];
182}
183
184// --------------------------------------------------------------------------------------------
189std::shared_ptr<const Mantid::Geometry::IMDDimension> MDGeometry::getDimensionWithId(std::string id) const {
190 auto dimension = std::find_if(
191 m_dimensions.begin(), m_dimensions.end(),
192 [&id](const std::shared_ptr<const Mantid::Geometry::IMDDimension> &dim) { return dim->getDimensionId() == id; });
193 if (dimension != m_dimensions.end())
194 return *dimension;
195 else
196 throw std::invalid_argument("Dimension tagged " + id + " was not found in the Workspace");
197}
198
199// --------------------------------------------------------------------------------------------
204 using namespace Mantid::Geometry;
205 VecIMDDimension_const_sptr vecCollapsedDimensions;
206 std::copy_if(m_dimensions.cbegin(), m_dimensions.cend(), std::back_inserter(vecCollapsedDimensions),
207 [](const auto &dimension) { return !dimension->getIsIntegrated(); });
208 return vecCollapsedDimensions;
209}
210
211//-----------------------------------------------------------------------------------------------
213std::vector<coord_t> MDGeometry::estimateResolution() const {
214 std::vector<coord_t> out;
215 for (size_t d = 0; d < this->getNumDims(); d++)
216 out.emplace_back(this->getDimension(d)->getBinWidth());
217 return out;
218}
219
220//-----------------------------------------------------------------------------------------------
227size_t MDGeometry::getDimensionIndexByName(const std::string &name) const {
228 const auto it = std::find_if(m_dimensions.cbegin(), m_dimensions.cend(),
229 [&name](const auto &dimension) { return dimension->getName() == name; });
230 if (it != m_dimensions.cend()) {
231 return std::distance(m_dimensions.cbegin(), it);
232 }
233 throw std::runtime_error("Dimension named '" + name + "' was not found in the IMDWorkspace.");
234}
235
236//-----------------------------------------------------------------------------------------------
243size_t MDGeometry::getDimensionIndexById(const std::string &id) const {
244 const auto it = std::find_if(m_dimensions.cbegin(), m_dimensions.cend(),
245 [&id](const auto &dimension) { return dimension->getDimensionId() == id; });
246 if (it != m_dimensions.cend()) {
247 return std::distance(m_dimensions.cbegin(), it);
248 }
249 throw std::runtime_error("Dimension with id '" + id + "' was not found in the IMDWorkspace.");
250}
251
252// --------------------------------------------------------------------------------------------
255void MDGeometry::addDimension(const std::shared_ptr<Mantid::Geometry::IMDDimension> &dim) {
256 m_dimensions.emplace_back(dim);
257}
258
259// --------------------------------------------------------------------------------------------
263 m_dimensions.emplace_back(std::shared_ptr<Mantid::Geometry::IMDDimension>(dim));
264}
265
266// --------------------------------------------------------------------------------------------
268std::shared_ptr<const Mantid::Geometry::IMDDimension> MDGeometry::getXDimension() const {
269 if (this->getNumDims() < 1)
270 throw std::runtime_error("Workspace does not have any dimensions!");
271 return this->getDimension(0);
272}
273
275std::shared_ptr<const Mantid::Geometry::IMDDimension> MDGeometry::getYDimension() const {
276 if (this->getNumDims() < 2)
277 throw std::runtime_error("Workspace does not have a Y dimension.");
278 return this->getDimension(1);
279}
280
282std::shared_ptr<const Mantid::Geometry::IMDDimension> MDGeometry::getZDimension() const {
283 if (this->getNumDims() < 3)
284 throw std::runtime_error("Workspace does not have a Z dimension.");
285 return this->getDimension(2);
286}
287
289std::shared_ptr<const Mantid::Geometry::IMDDimension> MDGeometry::getTDimension() const {
290 if (this->getNumDims() < 4)
291 throw std::runtime_error("Workspace does not have a T dimension.");
292 return this->getDimension(3);
293}
294
295// --------------------------------------------------------------------------------------------
302 if (index >= m_basisVectors.size())
303 throw std::invalid_argument("getBasisVector(): invalid index");
304 return m_basisVectors[index];
305}
306
313 if (index >= m_basisVectors.size())
314 throw std::invalid_argument("getBasisVector(): invalid index");
315 return m_basisVectors[index];
316}
317
324 if (index >= m_basisVectors.size())
325 throw std::invalid_argument("getBasisVector(): invalid index");
327}
328
333 auto normalized = std::find_if(m_basisVectors.begin(), m_basisVectors.end(),
334 [](const Mantid::Kernel::VMD &basisVector) { return basisVector.length() != 1.0; });
335 return normalized == m_basisVectors.end();
336}
337
338//---------------------------------------------------------------------------------------------------
342 if (index >= m_originalWorkspaces.size())
343 return false;
344 return bool(m_originalWorkspaces[index]);
345}
346
349
350//---------------------------------------------------------------------------------------------------
361std::shared_ptr<Workspace> MDGeometry::getOriginalWorkspace(size_t index) const {
362 if (index >= m_originalWorkspaces.size())
363 throw std::runtime_error("MDGeometry::getOriginalWorkspace() invalid index.");
365}
366
367//---------------------------------------------------------------------------------------------------
378void MDGeometry::setOriginalWorkspace(std::shared_ptr<Workspace> ws, size_t index) {
379 if (index >= m_originalWorkspaces.size())
380 m_originalWorkspaces.resize(index + 1);
381 m_originalWorkspaces[index] = std::move(ws);
382 m_notificationHelper->watchForWorkspaceDeletions();
383 m_notificationHelper->watchForWorkspaceReplace();
384}
385
386//---------------------------------------------------------------------------------------------------
399void MDGeometry::transformDimensions(std::vector<double> const &scaling, std::vector<double> const &offset) {
400 if (scaling.size() != m_dimensions.size())
401 throw std::invalid_argument("MDGeometry::transformDimensions(): "
402 "scaling.size() must be equal to number of "
403 "dimensions.");
404 if (offset.size() != m_dimensions.size())
405 throw std::invalid_argument("MDGeometry::transformDimensions(): "
406 "offset.size() must be equal to number of "
407 "dimensions.");
408 for (size_t d = 0; d < m_dimensions.size(); d++) {
410 coord_t min = (dim->getMinimum() * static_cast<coord_t>(scaling[d])) + static_cast<coord_t>(offset[d]);
411 coord_t max = (dim->getMaximum() * static_cast<coord_t>(scaling[d])) + static_cast<coord_t>(offset[d]);
412 if (min < max)
413 dim->setRange(dim->getNBins(), min, max);
414 else
415 dim->setRange(dim->getNBins(), max, min);
416 }
417 // Clear the original workspace
418 setOriginalWorkspace(std::shared_ptr<Workspace>());
420 setTransformToOriginal(nullptr);
421}
422
423//---------------------------------------------------------------------------------------------------
432void MDGeometry::deleteNotificationReceived(const std::shared_ptr<const Workspace> &deleted) {
433 for (auto &original : m_originalWorkspaces) {
434 if (original) {
435 // Compare the pointer being deleted to the one stored as the original.
436 if (original == deleted) {
437 // Clear the reference
438 original.reset();
439 }
440 }
441 }
442}
443
444//---------------------------------------------------------------------------------------------------
453void MDGeometry::replaceNotificationReceived(const std::shared_ptr<const Workspace> &replaced) {
454 for (auto &original : m_originalWorkspaces) {
455 if (original) {
456 // Compare the pointer being replaced to the one stored as the original.
457 if (original == replaced) {
458 // Clear the reference
459 original.reset();
460 }
461 }
462 }
463}
464
465//---------------------------------------------------------------------------------------------------
477 if (index >= m_transforms_FromOriginal.size())
478 throw std::runtime_error("MDGeometry::getTransformFromOriginal(): invalid index.");
479 return m_transforms_FromOriginal[index].get();
480}
481
482//---------------------------------------------------------------------------------------------------
499
500//---------------------------------------------------------------------------------------------------
513 if (index >= m_transforms_ToOriginal.size())
514 throw std::runtime_error("MDGeometry::getTransformFromOriginal(): invalid index.");
515 return m_transforms_ToOriginal[index].get();
516}
517
518//---------------------------------------------------------------------------------------------------
536
537//---------------------------------------------------------------------------------------------------
539std::string MDGeometry::getGeometryXML() const {
543 // Add all dimensions.
544 const size_t nDimensions = this->getNumDims();
545 for (size_t i = 0; i < nDimensions; i++) {
546 xmlBuilder.addOrdinaryDimension(this->getDimension(i));
547 }
548 // Add mapping dimensions
549 if (nDimensions > 0) {
550 xmlBuilder.addXDimension(this->getXDimension());
551 }
552 if (nDimensions > 1) {
553 xmlBuilder.addYDimension(this->getYDimension());
554 }
555 if (nDimensions > 2) {
556 xmlBuilder.addZDimension(this->getZDimension());
557 }
558 if (nDimensions > 3) {
559 xmlBuilder.addTDimension(this->getTDimension());
560 }
561 // Create the xml.
562 return xmlBuilder.create();
563}
564
570
576
577} // namespace Mantid::API
std::string name
Definition Run.cpp:60
std::map< DeltaEMode::Type, std::string > index
std::vector< T > const * vec
Unique SingleValueParameter Declaration for InputNDimensions.
MDGeometryNotificationHelper(MDGeometry &parent)
Poco::NObserver< MDGeometryNotificationHelper, WorkspacePreDeleteNotification > m_delete_observer
Poco delete notification observer object.
void deleteNotificationReceived(Mantid::API::WorkspacePreDeleteNotification_ptr notice)
bool m_observingDelete
Set to True when the m_delete_observer is observing workspace deletions.
Poco::NObserver< MDGeometryNotificationHelper, WorkspaceBeforeReplaceNotification > m_replace_observer
void replaceNotificationReceived(Mantid::API::WorkspaceBeforeReplaceNotification_ptr notice)
Describes the geometry (i.e.
Definition MDGeometry.h:36
Kernel::DblMatrix m_Wtransf
the matrix which transforms momentums from orthogonal Q-system to Orthogonal HKL or non-orthogonal HK...
Definition MDGeometry.h:152
Mantid::API::CoordTransform const * getTransformFromOriginal(size_t index=0) const
Get the Coordinate Transformation that goes from the original workspace to this workspace's coordinat...
void initGeometry(const std::vector< std::shared_ptr< Geometry::IMDDimension > > &dimensions)
Initialize the geometry.
std::shared_ptr< const Mantid::Geometry::IMDDimension > getTDimension() const
Get the t-dimension mapping.
std::vector< Mantid::Kernel::VMD > m_basisVectors
Vector of the basis vector (in the original workspace) for each dimension of this workspace.
Definition MDGeometry.h:157
virtual ~MDGeometry()
Destructor.
size_t getNumberTransformsToOriginal() const
Get the number of transforms defined to the original coordinate system.
std::vector< std::shared_ptr< const Mantid::API::CoordTransform > > m_transforms_FromOriginal
Coordinate Transformation that goes from the original workspace to this workspace's coordinates.
Definition MDGeometry.h:137
Mantid::Kernel::VMD m_origin
Vector of the origin (in the original workspace) that corresponds to 0,0,0... in this workspace.
Definition MDGeometry.h:133
bool allBasisNormalized() const
virtual std::shared_ptr< const Mantid::Geometry::IMDDimension > getDimension(size_t index) const
Get a dimension.
void addDimension(const std::shared_ptr< Mantid::Geometry::IMDDimension > &dim)
Add a dimension.
void replaceNotificationReceived(const std::shared_ptr< const Workspace > &deleted)
Function called when observer detects a workspace is replaced.
std::vector< std::shared_ptr< const Geometry::IMDDimension > > getNonIntegratedDimensions() const
Get non-collapsed dimensions.
void clearTransforms()
Clear transforms.
void setOriginalWorkspace(std::shared_ptr< Workspace > ws, size_t index=0)
Set the "original" workspace (the workspace that was the source for a binned MDWorkspace).
size_t getDimensionIndexById(const std::string &id) const
Get the index of the dimension that matches the ID given.
virtual std::shared_ptr< const Mantid::Geometry::IMDDimension > getDimensionWithId(std::string id) const
Get a dimension.
size_t numOriginalWorkspaces() const
void setBasisVector(size_t index, const Mantid::Kernel::VMD &vec)
Set the basis vector (in the original workspace) for a dimension of this workspace.
std::vector< std::shared_ptr< Geometry::IMDDimension > > m_dimensions
Vector of the dimensions used, in the order X Y Z t, etc.
Definition MDGeometry.h:125
virtual size_t getNumDims() const
void transformDimensions(std::vector< double > const &scaling, std::vector< double > const &offset)
Transform the dimensions contained in this geometry x' = x*scaling + offset.
void clearOriginalWorkspaces()
Clear original workspaces.
virtual size_t getNumNonIntegratedDims() const
std::vector< std::shared_ptr< const Mantid::API::CoordTransform > > m_transforms_ToOriginal
Coordinate Transformation that goes from this workspace's coordinates to the original workspace coord...
Definition MDGeometry.h:141
Mantid::API::CoordTransform const * getTransformToOriginal(size_t index=0) const
Get the Coordinate Transformation that goes from THIS workspace's coordinates to the ORIGINAL workspa...
bool hasOriginalWorkspace(size_t index=0) const
void setTransformFromOriginal(Mantid::API::CoordTransform *transform, size_t index=0)
Sets the Coordinate Transformation that goes from the original workspace to this workspace's coordina...
virtual std::vector< coord_t > estimateResolution() const
void setTransformToOriginal(Mantid::API::CoordTransform *transform, size_t index=0)
Sets the Coordinate Transformation that goes from THIS workspace's coordinates to the ORIGINAL worksp...
std::string getGeometryXML() const
std::shared_ptr< Workspace > getOriginalWorkspace(size_t index=0) const
Get the "original" workspace (the workspace that was the source for a binned MDWorkspace).
Mantid::Kernel::VMD & getBasisVector(size_t index)
Get the basis vector (in the original workspace) for a dimension of this workspace.
MDGeometry & operator=(const MDGeometry &other)
size_t getDimensionIndexByName(const std::string &name) const
Get the index of the dimension that matches the name given.
std::shared_ptr< const Mantid::Geometry::IMDDimension > getXDimension() const
Get the x-dimension mapping.
std::vector< std::shared_ptr< Workspace > > m_originalWorkspaces
Pointer to the original workspace(s), if this workspace is a coordinate transformation from an origin...
Definition MDGeometry.h:129
MDGeometry()
Constructor.
size_t getNumberTransformsFromOriginal() const
Get the number of transforms defined from the original coordinate system.
void deleteNotificationReceived(const std::shared_ptr< const Workspace > &replaced)
Function called when observer objects recieves a notification.
std::shared_ptr< const Mantid::Geometry::IMDDimension > getYDimension() const
Get the y-dimension mapping.
std::shared_ptr< const Mantid::Geometry::IMDDimension > getZDimension() const
Get the z-dimension mapping.
std::unique_ptr< MDGeometryNotificationHelper > m_notificationHelper
Helper that deals with notifications and observing the ADS.
Definition MDGeometry.h:144
The class describes one dimension of multidimensional dataset representing an orthogonal dimension an...
bool addXDimension(const IMDDimension_const_sptr &dimension) const
Add x dimension.
bool addZDimension(const IMDDimension_const_sptr &dimension) const
Add z dimension.
bool addYDimension(const IMDDimension_const_sptr &dimension) const
Add y dimension.
bool addTDimension(const IMDDimension_const_sptr &dimension) const
Add t dimension.
const std::string & create() const
Create the xml.
bool addOrdinaryDimension(IMDDimension_const_sptr dimensionToAdd) const
Add a dimension that is neither considered x, y, z or t.
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::PreDeleteNotification > & WorkspacePreDeleteNotification_ptr
std::shared_ptr< const CoordTransform > CoordTransform_const_sptr
const Poco::AutoPtr< Mantid::Kernel::DataService< Mantid::API::Workspace >::BeforeReplaceNotification > & WorkspaceBeforeReplaceNotification_ptr
std::shared_ptr< IMDDimension > IMDDimension_sptr
Shared Pointer for IMDDimension. Frequently used type in framework.
std::vector< IMDDimension_const_sptr > VecIMDDimension_const_sptr
Vector of constant shared pointers to IMDDimensions.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition MDTypes.h:27
STL namespace.