Mantid
Loading...
Searching...
No Matches
PlusMD.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 +
14
15using namespace Mantid::Kernel;
16using namespace Mantid::DataObjects;
17using namespace Mantid::API;
18
19namespace Mantid::MDAlgorithms {
20
21// Register the algorithm into the AlgorithmFactory
23
24//----------------------------------------------------------------------------------------------
31template <typename MDE, size_t nd> void PlusMD::doPlus(typename MDEventWorkspace<MDE, nd>::sptr ws1) {
32 typename MDEventWorkspace<MDE, nd>::sptr ws2 = std::dynamic_pointer_cast<MDEventWorkspace<MDE, nd>>(m_operand_event);
33 if (!ws1 || !ws2)
34 throw std::runtime_error("Incompatible workspace types passed to PlusMD.");
35
36 MDBoxBase<MDE, nd> *box1 = ws1->getBox();
37 MDBoxBase<MDE, nd> *box2 = ws2->getBox();
38
39 Progress prog(this, 0.0, 0.4, box2->getBoxController()->getTotalNumMDBoxes());
40
41 // How many events you started with
42 size_t initial_numEvents = ws1->getNPoints();
43
44 // Make a leaf-only iterator through all boxes with events in the RHS
45 // workspace
46 // TODO: OMP
47 MDBoxIterator<MDE, nd> it2(box2, 1000, true);
48 do {
49 auto *box = dynamic_cast<MDBox<MDE, nd> *>(it2.getBox());
50 if (box && !box->getIsMasked()) {
51 // Copy the events from WS2 and add them into WS1
52 const std::vector<MDE> &events = box->getConstEvents();
53 // Add events, with bounds checking
54 box1->addEvents(events);
55 box->releaseEvents();
56 }
57 prog.report("Adding Events");
58 } while (it2.next());
59
60 this->progress(0.41, "Splitting Boxes");
61 // This is freed in the destructor of the ThreadPool class,
62 // it should not be a memory leak
63 auto prog2 = new Progress(this, 0.4, 0.9, 100);
65 ThreadPool tp(ts, 0, prog2);
66 ws1->splitAllIfNeeded(ts);
67 prog2->resetNumSteps(ts->size(), 0.4, 0.6);
68 tp.joinAll();
69
71 // if (ws1->isFileBacked())
72 //{
73 // // flusush disk kernel buffer and save all still in memory
74 // ws1->getBoxController()->getFileIO()->flushCache();
75 // // Flush the data writes to disk from nexus IO buffer
76 // ws1->getBoxController()->getFileIO()->flushData();
77 //}
78 // if(ws2->isFileBacked())
79 //{
80 // // flusush disk kernel buffer and save all still in memory
81 // ws2->getBoxController()->getFileIO()->flushCache();
82 // // Flush the data writes to disk from nexus IO buffer
83 // ws2->getBoxController()->getFileIO()->flushData();
84
85 // //// Flush anything else in the to-write buffer
86 // //BoxController_sptr bc = ws1->getBoxController();
87
88 // //prog.resetNumSteps(bc->getTotalNumMDBoxes(), 0.6, 1.0);
89 // //MDBoxIterator<MDE,nd> it1(box1, 1000, true);
90 // //while (true)
91 // //{
92 // // MDBox<MDE,nd> * box = dynamic_cast<MDBox<MDE,nd> *>(it1.getBox());
93 // // if (box)
94 // // {
95 // // // Something was maybe added to this box
96 // // if (box->getEventVectorSize() > 0)
97 // // {
98 // // // By getting the events, this will merge the newly added and the
99 // cached events.
100 // // box->getEvents();
101 // // // The MRU to-write cache will optimize writes by reducing seek
102 // times
103 // // box->releaseEvents();
104 // // }
105 // // }
106 // // prog.report("Saving");
107 // // if (!it1.next()) break;
108 // //}
109 // //bc->getDiskBuffer().flushCache();
110
111 //}
112
113 this->progress(0.95, "Refreshing cache");
114 ws1->refreshCache();
115
116 // Set a marker that the file-back-end needs updating if the # of events
117 // changed.
118 if (ws1->getNPoints() != initial_numEvents)
119 ws1->setFileNeedsUpdating(true);
120}
121
122//----------------------------------------------------------------------------------------------
124bool PlusMD::commutative() const { return true; }
125
126//----------------------------------------------------------------------------------------------
129 if (m_lhs_event || m_rhs_event) {
131 throw std::runtime_error("Cannot sum a MDHistoWorkspace and a "
132 "MDEventWorkspace (only MDEventWorkspace + "
133 "MDEventWorkspace is allowed).");
135 throw std::runtime_error("Cannot sum a MDEventWorkspace and a scalar "
136 "(only MDEventWorkspace + MDEventWorkspace is "
137 "allowed).");
138 }
139}
140
141//----------------------------------------------------------------------------------------------
147
148//----------------------------------------------------------------------------------------------
154
155//----------------------------------------------------------------------------------------------
159 // Now we add m_operand_event into m_out_event.
161
162 // Clear masking (box flags) from the output workspace
163 m_out_event->clearMDMasking();
164
165 // Set to the output
166 setProperty("OutputWorkspace", m_out_event);
167}
168
169} // namespace Mantid::MDAlgorithms
#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...
size_t getTotalNumMDBoxes() const
Return the total number of MD Boxes, irrespective of depth.
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
Templated class for the multi-dimensional event workspace.
std::shared_ptr< MDEventWorkspace< MDE, nd > > sptr
Typedef for a shared pointer of this kind of event workspace.
Templated class holding data about a neutron detection event in N-dimensions (for example,...
Definition MDLeanEvent.h:60
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::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.
Sum two MDWorkspaces together.
Definition PlusMD.h:21
bool commutative() const override
Is the operation commutative?
Definition PlusMD.cpp:124
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 PlusMD.cpp:150
void checkInputs() override
Check the inputs and throw if the algorithm cannot be run.
Definition PlusMD.cpp:128
void doPlus(typename Mantid::DataObjects::MDEventWorkspace< MDE, nd >::sptr ws)
Perform the adding.
Definition PlusMD.cpp:31
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 PlusMD.cpp:143
void execEvent() override
Run the algorithm with an MDEventWorkspace as output.
Definition PlusMD.cpp:158
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.