Mantid
Loading...
Searching...
No Matches
MinusMD.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(MinusMD)
21
22//----------------------------------------------------------------------------------------------
24const std::string MinusMD::name() const { return "MinusMD"; }
25
27int MinusMD::version() const { return 1; }
28
29//----------------------------------------------------------------------------------------------
30
31//----------------------------------------------------------------------------------------------
33bool MinusMD::commutative() const { return false; }
34
35//----------------------------------------------------------------------------------------------
38 if (m_lhs_event || m_rhs_event) {
40 throw std::runtime_error("Cannot subtract a MDHistoWorkspace and a "
41 "MDEventWorkspace (only MDEventWorkspace - "
42 "MDEventWorkspace is allowed).");
44 throw std::runtime_error("Cannot subtract a MDEventWorkspace and a "
45 "scalar (only MDEventWorkspace - "
46 "MDEventWorkspace is allowed).");
47 }
48}
49
50//----------------------------------------------------------------------------------------------
57template <typename MDE, size_t nd> void MinusMD::doMinus(typename MDEventWorkspace<MDE, nd>::sptr ws1) {
58 typename MDEventWorkspace<MDE, nd>::sptr ws2 = std::dynamic_pointer_cast<MDEventWorkspace<MDE, nd>>(m_operand_event);
59 if (!ws1 || !ws2)
60 throw std::runtime_error("Incompatible workspace types passed to MinusMD.");
61
62 MDBoxBase<MDE, nd> *box1 = ws1->getBox();
63 MDBoxBase<MDE, nd> *box2 = ws2->getBox();
64
65 Progress prog(this, 0.0, 0.4, box2->getBoxController()->getTotalNumMDBoxes());
66
67 // How many events you started with
68 size_t initial_numEvents = ws1->getNPoints();
69
70 // Make a leaf-only iterator through all boxes with events in the RHS
71 // workspace
72 MDBoxIterator<MDE, nd> it2(box2, 1000, true);
73 do {
74 auto *box = dynamic_cast<MDBox<MDE, nd> *>(it2.getBox());
75 if (box) {
76 // Copy the events from WS2 and add them into WS1
77 const std::vector<MDE> &events = box->getConstEvents();
78
79 // Perform a copy while flipping the signal
80 std::vector<MDE> eventsCopy;
81 eventsCopy.reserve(events.size());
82 for (auto it = events.begin(); it != events.end(); it++) {
83 MDE eventCopy(*it);
84 eventCopy.setSignal(-eventCopy.getSignal());
85 eventsCopy.emplace_back(eventCopy);
86 }
87 // Add events, with bounds checking
88 box1->addEvents(eventsCopy);
89 box->releaseEvents();
90 }
91 prog.report("Substracting Events");
92 } while (it2.next());
93
94 this->progress(0.41, "Splitting Boxes");
95
96 // This is freed in the destructor of the ThreadPool class,
97 // it should not be a memory leak
98 auto prog2 = new Progress(this, 0.4, 0.9, 100);
100 ThreadPool tp(ts, 0, prog2);
101 ws1->splitAllIfNeeded(ts);
102 prog2->resetNumSteps(ts->size(), 0.4, 0.6);
103 tp.joinAll();
104
105 this->progress(0.95, "Refreshing cache");
106 ws1->refreshCache();
107
108 // Set a marker that the file-back-end needs updating if the # of events
109 // changed.
110 if (ws1->getNPoints() != initial_numEvents)
111 ws1->setFileNeedsUpdating(true);
112}
113
114//----------------------------------------------------------------------------------------------
117 // Now we add m_operand_event into m_out_event.
119
120 // Clear masking (box flags) from the output workspace
121 m_out_event->clearMDMasking();
122
123 // Set to the output
124 setProperty("OutputWorkspace", m_out_event);
125}
126
127//----------------------------------------------------------------------------------------------
133
134//----------------------------------------------------------------------------------------------
138 out->subtract(scalar->y(0)[0], scalar->e(0)[0]);
139}
140
141} // namespace Mantid::MDAlgorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
static std::unique_ptr< QThreadPool > tp
#define CALL_MDEVENT_FUNCTION(funcname, workspace)
Macro that makes it possible to call a templated method for a MDEventWorkspace using a IMDEventWorksp...
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
size_t getTotalNumMDBoxes() const
Return the total number of MD Boxes, irrespective of depth.
void setFileNeedsUpdating(bool value)
Sets the marker set to true when a file-backed workspace needs its back-end file updated (by calling ...
Helper class for reporting progress from algorithms.
Definition Progress.h:25
Templated super-class of a multi-dimensional event "box".
Definition MDBoxBase.h:50
virtual size_t addEvents(const std::vector< MDE > &events)
Add several events, starting and stopping at particular point in a vector.
Mantid::API::BoxController * getBoxController() const override
Definition MDBoxBase.h:140
MDBoxIterator: iterate through MDBoxBase hierarchy down to a given maximum depth.
bool next() override
Advance to the next cell.
MDBoxBase< MDE, nd > * getBox() const
Return a pointer to the current box pointed to by the iterator.
Templated class for a multi-dimensional event "box".
Definition MDBox.h:45
const std::vector< MDE > & getConstEvents() const
Get vector of constant events to use.
Definition MDBox.hxx:269
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.
uint64_t getNPoints() const override
Returns the total number of points (events) in this workspace.
void splitAllIfNeeded(Kernel::ThreadScheduler *ts) override
Goes through all the sub-boxes and splits them if they contain enough events to be worth it.
Templated class holding data about a neutron detection event in N-dimensions (for example,...
Definition MDLeanEvent.h:60
float getSignal() const
Returns the signal (weight) of this event.
void setSignal(const float newSignal)
Set the signal of the event.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
A Thread Pool implementation that keeps a certain number of threads running (normally,...
Definition ThreadPool.h:36
A First-In-First-Out Thread Scheduler.
The ThreadScheduler object defines how tasks are allocated to threads and in what order.
virtual size_t size()=0
Returns the size of the queue.
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_operand_event
Operand MDEventWorkspace.
Mantid::DataObjects::WorkspaceSingleValue_sptr m_lhs_scalar
Mantid::DataObjects::MDHistoWorkspace_sptr m_rhs_histo
Mantid::DataObjects::MDHistoWorkspace_sptr m_lhs_histo
Mantid::API::IMDEventWorkspace_sptr m_out_event
Output MDEventWorkspace.
MinusMD : minus operation for MDWorkspaces.
Definition MinusMD.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 MinusMD.cpp:136
bool commutative() const override
Is the operation commutative?
Definition MinusMD.cpp:33
void checkInputs() override
Check the inputs and throw if the algorithm cannot be run.
Definition MinusMD.cpp:37
int version() const override
Algorithm's version for identification.
Definition MinusMD.cpp:27
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 MinusMD.cpp:129
void doMinus(typename Mantid::DataObjects::MDEventWorkspace< MDE, nd >::sptr ws)
Perform the subtraction.
Definition MinusMD.cpp:57
void execEvent() override
Run the algorithm with an MDEventWorkspace as output.
Definition MinusMD.cpp:116
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.