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#include "MantidKernel/System.h"
13
14using namespace Mantid::Kernel;
15using namespace Mantid::API;
16using namespace Mantid::DataObjects;
17
18namespace Mantid::MDAlgorithms {
19
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(MinusMD)
22
23//----------------------------------------------------------------------------------------------
25const std::string MinusMD::name() const { return "MinusMD"; }
26
28int MinusMD::version() const { return 1; }
29
30//----------------------------------------------------------------------------------------------
31
32//----------------------------------------------------------------------------------------------
34bool MinusMD::commutative() const { return false; }
35
36//----------------------------------------------------------------------------------------------
39 if (m_lhs_event || m_rhs_event) {
41 throw std::runtime_error("Cannot subtract a MDHistoWorkspace and a "
42 "MDEventWorkspace (only MDEventWorkspace - "
43 "MDEventWorkspace is allowed).");
45 throw std::runtime_error("Cannot subtract a MDEventWorkspace and a "
46 "scalar (only MDEventWorkspace - "
47 "MDEventWorkspace is allowed).");
48 }
49}
50
51//----------------------------------------------------------------------------------------------
58template <typename MDE, size_t nd> void MinusMD::doMinus(typename MDEventWorkspace<MDE, nd>::sptr ws1) {
59 typename MDEventWorkspace<MDE, nd>::sptr ws2 = std::dynamic_pointer_cast<MDEventWorkspace<MDE, nd>>(m_operand_event);
60 if (!ws1 || !ws2)
61 throw std::runtime_error("Incompatible workspace types passed to MinusMD.");
62
63 MDBoxBase<MDE, nd> *box1 = ws1->getBox();
64 MDBoxBase<MDE, nd> *box2 = ws2->getBox();
65
66 Progress prog(this, 0.0, 0.4, box2->getBoxController()->getTotalNumMDBoxes());
67
68 // How many events you started with
69 size_t initial_numEvents = ws1->getNPoints();
70
71 // Make a leaf-only iterator through all boxes with events in the RHS
72 // workspace
73 MDBoxIterator<MDE, nd> it2(box2, 1000, true);
74 do {
75 auto *box = dynamic_cast<MDBox<MDE, nd> *>(it2.getBox());
76 if (box) {
77 // Copy the events from WS2 and add them into WS1
78 const std::vector<MDE> &events = box->getConstEvents();
79
80 // Perform a copy while flipping the signal
81 std::vector<MDE> eventsCopy;
82 eventsCopy.reserve(events.size());
83 for (auto it = events.begin(); it != events.end(); it++) {
84 MDE eventCopy(*it);
85 eventCopy.setSignal(-eventCopy.getSignal());
86 eventsCopy.emplace_back(eventCopy);
87 }
88 // Add events, with bounds checking
89 box1->addEvents(eventsCopy);
90 box->releaseEvents();
91 }
92 prog.report("Substracting Events");
93 } while (it2.next());
94
95 this->progress(0.41, "Splitting Boxes");
96
97 // This is freed in the destructor of the ThreadPool class,
98 // it should not be a memory leak
99 auto prog2 = new Progress(this, 0.4, 0.9, 100);
101 ThreadPool tp(ts, 0, prog2);
102 ws1->splitAllIfNeeded(ts);
103 prog2->resetNumSteps(ts->size(), 0.4, 0.6);
104 tp.joinAll();
105
106 this->progress(0.95, "Refreshing cache");
107 ws1->refreshCache();
108
109 // Set a marker that the file-back-end needs updating if the # of events
110 // changed.
111 if (ws1->getNPoints() != initial_numEvents)
112 ws1->setFileNeedsUpdating(true);
113}
114
115//----------------------------------------------------------------------------------------------
118 // Now we add m_operand_event into m_out_event.
120
121 // Clear masking (box flags) from the output workspace
122 m_out_event->clearMDMasking();
123
124 // Set to the output
125 setProperty("OutputWorkspace", m_out_event);
126}
127
128//----------------------------------------------------------------------------------------------
132 out->subtract(*operand);
133}
134
135//----------------------------------------------------------------------------------------------
139 out->subtract(scalar->y(0)[0], scalar->e(0)[0]);
140}
141
142} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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.
Definition: Algorithm.cpp:231
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)
Mantid::API::BoxController * getBoxController() const override
Definition: MDBoxBase.h:140
MDBoxIterator: iterate through MDBoxBase hierarchy down to a given maximum depth.
Definition: MDBoxIterator.h:28
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.
Definition: MDBoxIterator.h:39
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.
uint64_t getNPoints() const override
std::shared_ptr< MDEventWorkspace< MDE, nd > > sptr
Typedef for a shared pointer of this kind of event workspace.
void splitAllIfNeeded(Kernel::ThreadScheduler *ts) override
Split all boxes that exceed the split threshold.
void refreshCache() override
Refresh the cache (integrated signal of each box)
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.
Definition: MDLeanEvent.h:277
void setSignal(const float newSignal)
Set the signal of the event.
Definition: MDLeanEvent.h:296
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.
Definition: ProgressBase.h:51
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:137
bool commutative() const override
Is the operation commutative?
Definition: MinusMD.cpp:34
void checkInputs() override
Check the inputs and throw if the algorithm cannot be run.
Definition: MinusMD.cpp:38
int version() const override
Algorithm's version for identification.
Definition: MinusMD.cpp:28
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:130
void doMinus(typename Mantid::DataObjects::MDEventWorkspace< MDE, nd >::sptr ws)
Perform the subtraction.
Definition: MinusMD.cpp:58
void execEvent() override
Run the algorithm with an MDEventWorkspace as output.
Definition: MinusMD.cpp:117
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.