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