Mantid
Loading...
Searching...
No Matches
DivideMD.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
13using namespace Mantid::Kernel;
14using namespace Mantid::API;
15using namespace Mantid::DataObjects;
16
17namespace Mantid::MDAlgorithms {
18
19// Register the algorithm into the AlgorithmFactory
20DECLARE_ALGORITHM(DivideMD)
21
22//----------------------------------------------------------------------------------------------
24const std::string DivideMD::name() const { return "DivideMD"; }
25
27int DivideMD::version() const { return 1; }
28
29//----------------------------------------------------------------------------------------------
30
31//----------------------------------------------------------------------------------------------
33bool DivideMD::commutative() const { return false; }
34
35//----------------------------------------------------------------------------------------------
38 if (m_rhs_event)
39 throw std::runtime_error("Cannot divide by a MDEventWorkspace on the RHS.");
41 throw std::runtime_error("A MDEventWorkspace can only be divided by a scalar.");
42}
43
44//----------------------------------------------------------------------------------------------
49template <typename MDE, size_t nd> void DivideMD::execEventScalar(typename MDEventWorkspace<MDE, nd>::sptr ws) {
50 // Get the scalar multiplying
51 auto scalar = float(m_rhs_scalar->y(0)[0]);
52 auto scalarError = float(m_rhs_scalar->e(0)[0]);
53 float scalarErrorSquared = scalarError * scalarError;
54 float inverseScalarSquared = 1.f / (scalar * scalar);
55
56 // Get all the MDBoxes contained
57 MDBoxBase<MDE, nd> *parentBox = ws->getBox();
58 std::vector<API::IMDNode *> boxes;
59 parentBox->getBoxes(boxes, 1000, true);
60
61 bool fileBackedTarget(false);
62 Kernel::DiskBuffer *dbuff(nullptr);
63 if (ws->isFileBacked()) {
64 fileBackedTarget = true;
65 dbuff = ws->getBoxController()->getFileIO();
66 }
67
68 for (auto &boxe : boxes) {
69 auto *box = dynamic_cast<MDBox<MDE, nd> *>(boxe);
70 if (box) {
71 size_t ic(0);
72 typename std::vector<MDE> &events = box->getEvents();
73 auto it = events.begin();
74 auto it_end = events.end();
75 for (; it != it_end; it++) {
76 // Multiply weight by a scalar, propagating error
77 float oldSignal = it->getSignal();
78 float signal = oldSignal / scalar;
79 float errorSquared = it->getErrorSquared() * inverseScalarSquared +
80 scalarErrorSquared * oldSignal * oldSignal * inverseScalarSquared * inverseScalarSquared;
81 it->setSignal(signal);
82 it->setErrorSquared(errorSquared);
83 ic++;
84 }
85
86 box->releaseEvents();
87 if (fileBackedTarget && ic > 0) {
88 Kernel::ISaveable *const pSaver(box->getISaveable());
89 dbuff->toWrite(pSaver);
90 }
91 }
92 }
93 // Recalculate the totals
94 ws->refreshCache();
95 // Mark file-backed workspace as dirty
96 ws->setFileNeedsUpdating(true);
97}
98
99//----------------------------------------------------------------------------------------------
103 throw std::runtime_error("A MDEventWorkspace can only be divided by a scalar.");
104 if (!m_out_event)
105 throw std::runtime_error("DivideMD::execEvent(): Error creating output MDEventWorkspace.");
106 // Call the method to do the dividing
108}
109
110//----------------------------------------------------------------------------------------------
116
117//----------------------------------------------------------------------------------------------
121 out->divide(scalar->y(0)[0], scalar->e(0)[0]);
122}
123
124} // namespace Mantid::MDAlgorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
#define CALL_MDEVENT_FUNCTION(funcname, workspace)
Macro that makes it possible to call a templated method for a MDEventWorkspace using a IMDEventWorksp...
void setFileNeedsUpdating(bool value)
Sets the marker set to true when a file-backed workspace needs its back-end file updated (by calling ...
virtual void getBoxes(std::vector< IMDNode * > &boxes, size_t maxDepth, bool leafOnly)=0
Fill a vector with all the boxes who are the childred of this one up to a certain depth.
Templated super-class of a multi-dimensional event "box".
Definition MDBoxBase.h:50
Templated class for a multi-dimensional event "box".
Definition MDBox.h:45
std::shared_ptr< MDEventWorkspace< MDE, nd > > sptr
Typedef for a shared pointer of this kind of event workspace.
void refreshCache() override
Refresh the cache of # of points, signal, and error.
Mantid::API::BoxController_sptr getBoxController() override
Returns the BoxController used in this workspace.
Buffer objects that need to be written out to disk so as to optimize writing operations.
Definition DiskBuffer.h:42
void toWrite(ISaveable *item)
Call this method when an object is ready to be written out to disk.
An interface for objects that can be cached or saved to disk.
Definition ISaveable.h:28
Mantid::API::IMDEventWorkspace_sptr m_rhs_event
Mantid::API::IMDEventWorkspace_sptr m_lhs_event
For checkInputs.
Mantid::DataObjects::WorkspaceSingleValue_sptr m_rhs_scalar
Mantid::API::IMDEventWorkspace_sptr m_out_event
Output MDEventWorkspace.
DivideMD : divide operation for MDWorkspaces.
Definition DivideMD.h:19
void execHistoScalar(Mantid::DataObjects::MDHistoWorkspace_sptr out, Mantid::DataObjects::WorkspaceSingleValue_const_sptr scalar) override
Run the algorithm with a MDHisotWorkspace as output, scalar and operand.
Definition DivideMD.cpp:119
void execHistoHisto(Mantid::DataObjects::MDHistoWorkspace_sptr out, Mantid::DataObjects::MDHistoWorkspace_const_sptr operand) override
Run the algorithm with a MDHisotWorkspace as output and operand.
Definition DivideMD.cpp:112
void execEventScalar(typename Mantid::DataObjects::MDEventWorkspace< MDE, nd >::sptr ws)
Perform the operation with MDEventWorkpsace as LHS and a scalar as RHS Will do "ws /= scalar".
Definition DivideMD.cpp:49
bool commutative() const override
Is the operation commutative?
Definition DivideMD.cpp:33
void checkInputs() override
Check the inputs and throw if the algorithm cannot be run.
Definition DivideMD.cpp:37
void execEvent() override
Run the algorithm with an MDEventWorkspace as output.
Definition DivideMD.cpp:101
int version() const override
Algorithm's version for identification.
Definition DivideMD.cpp:27
std::shared_ptr< const WorkspaceSingleValue > WorkspaceSingleValue_const_sptr
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.
STL namespace.