-
Notifications
You must be signed in to change notification settings - Fork 122
/
MultiplyMD.cpp
124 lines (108 loc) · 4.98 KB
/
MultiplyMD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidMDAlgorithms/MultiplyMD.h"
#include "MantidDataObjects/MDBox.h"
#include "MantidDataObjects/MDBoxBase.h"
#include "MantidDataObjects/MDEventFactory.h"
#include "MantidDataObjects/MDEventWorkspace.h"
#include "MantidKernel/System.h"
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
namespace Mantid {
namespace MDAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(MultiplyMD)
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string MultiplyMD::name() const { return "MultiplyMD"; }
/// Algorithm's version for identification. @see Algorithm::version
int MultiplyMD::version() const { return 1; }
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/// Is the operation commutative?
bool MultiplyMD::commutative() const { return true; }
//----------------------------------------------------------------------------------------------
/// Check the inputs and throw if the algorithm cannot be run
void MultiplyMD::checkInputs() {
if (m_rhs_event)
throw std::runtime_error("Cannot multiply by a MDEventWorkspace on the RHS.");
if (m_lhs_event && !m_rhs_scalar)
throw std::runtime_error("A MDEventWorkspace can only be multiplied by a scalar.");
}
//----------------------------------------------------------------------------------------------
/** Perform the operation with MDEventWorkpsace as LHS and a scalar as RHS
* Will do "ws *= scalar"
* @param ws :: MDEventWorkspace being modified
*/
template <typename MDE, size_t nd> void MultiplyMD::execEventScalar(typename MDEventWorkspace<MDE, nd>::sptr ws) {
// Get the scalar multiplying
auto scalar = static_cast<float>(m_rhs_scalar->y(0)[0]);
auto scalarError = static_cast<float>(m_rhs_scalar->e(0)[0]);
float scalarErrorSquared = scalarError * scalarError;
float scalarSquared = scalar * scalar;
// Get all the MDBoxes contained
MDBoxBase<MDE, nd> *parentBox = ws->getBox();
std::vector<API::IMDNode *> boxes;
parentBox->getBoxes(boxes, 1000, true);
bool fileBackedTarget(false);
Kernel::DiskBuffer *dbuff(nullptr);
if (ws->isFileBacked()) {
fileBackedTarget = true;
dbuff = ws->getBoxController()->getFileIO();
}
for (auto &boxe : boxes) {
auto *box = dynamic_cast<MDBox<MDE, nd> *>(boxe);
if (box) {
typename std::vector<MDE> &events = box->getEvents();
size_t ic(events.size());
auto it = events.begin();
auto it_end = events.end();
for (; it != it_end; it++) {
// Multiply weight by a scalar, propagating error
float oldSignal = it->getSignal();
float signal = oldSignal * scalar;
float errorSquared = scalarSquared * it->getErrorSquared() + oldSignal * oldSignal * scalarErrorSquared;
it->setSignal(signal);
it->setErrorSquared(errorSquared);
}
box->releaseEvents();
if (fileBackedTarget && ic > 0) {
Kernel::ISaveable *const pSaver(box->getISaveable());
dbuff->toWrite(pSaver);
}
}
}
// Recalculate the totals
ws->refreshCache();
// Mark file-backed workspace as dirty
ws->setFileNeedsUpdating(true);
}
//----------------------------------------------------------------------------------------------
/// Run the algorithm with an MDEventWorkspace as output
void MultiplyMD::execEvent() {
if (m_lhs_event && !m_rhs_scalar)
throw std::runtime_error("A MDEventWorkspace can only be multiplied by a scalar.");
if (!m_out_event)
throw std::runtime_error("MultiplyMD::execEvent(): Error creating output MDEventWorkspace.");
// Call the method to do the multiplying
CALL_MDEVENT_FUNCTION(this->execEventScalar, m_out_event);
}
//----------------------------------------------------------------------------------------------
/// Run the algorithm with a MDHisotWorkspace as output and operand
void MultiplyMD::execHistoHisto(Mantid::DataObjects::MDHistoWorkspace_sptr out,
Mantid::DataObjects::MDHistoWorkspace_const_sptr operand) {
out->multiply(*operand);
}
//----------------------------------------------------------------------------------------------
/// Run the algorithm with a MDHisotWorkspace as output, scalar and operand
void MultiplyMD::execHistoScalar(Mantid::DataObjects::MDHistoWorkspace_sptr out,
Mantid::DataObjects::WorkspaceSingleValue_const_sptr scalar) {
out->multiply(scalar->y(0)[0], scalar->e(0)[0]);
}
} // namespace MDAlgorithms
} // namespace Mantid