Mantid
Loading...
Searching...
No Matches
WeightedMeanMD.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 +
9
10using namespace Mantid::Kernel;
11using namespace Mantid::API;
12
13namespace Mantid::MDAlgorithms {
14
15DECLARE_ALGORITHM(WeightedMeanMD)
16
17//----------------------------------------------------------------------------------------------
19bool WeightedMeanMD::commutative() const { return true; }
20
21//----------------------------------------------------------------------------------------------
24 if (!m_lhs_histo || !m_rhs_histo)
25 throw std::invalid_argument(this->name() + " can only be run on a MDHistoWorkspace.");
26}
27
28//----------------------------------------------------------------------------------------------
33 auto lhs = out->createIterator();
34 auto lhs_it = dynamic_cast<MDHistoWorkspaceIterator *>(lhs.get());
35 auto rhs = operand->createIterator();
36 auto rhs_it = dynamic_cast<MDHistoWorkspaceIterator *>(rhs.get());
37
38 if (!lhs_it || !rhs_it) {
39 throw std::logic_error("Histo iterators have wrong type.");
40 }
41
42 do {
43 double lhs_s = lhs_it->getSignal();
44 double lhs_err = lhs_it->getError();
45 double rhs_s = rhs_it->getSignal();
46 double rhs_err = rhs_it->getError();
47 double signal = 0;
48 double error_sq = 0;
49 if ((lhs_err > 0.0) && (rhs_err > 0.0)) {
50 double rhs_err_sq = rhs_err * rhs_err;
51 double lhs_err_sq = lhs_err * lhs_err;
52 double s = (rhs_s / rhs_err_sq) + (lhs_s / lhs_err_sq);
53 double e = rhs_err_sq * lhs_err_sq / (rhs_err_sq + lhs_err_sq);
54 signal = s * e;
55 error_sq = e;
56 } else if ((rhs_err > 0) && (lhs_err == 0)) {
57 signal = rhs_s;
58 error_sq = rhs_err * rhs_err;
59 } else if ((lhs_err > 0) && (rhs_err == 0)) {
60 signal = lhs_s;
61 error_sq = lhs_err * lhs_err;
62 }
63
64 size_t pos = lhs_it->getLinearIndex();
65 out->setSignalAt(pos, signal);
66 out->setErrorSquaredAt(pos, error_sq);
67 } while (lhs_it->next() && rhs_it->next());
68}
69
70//----------------------------------------------------------------------------------------------
74 throw std::runtime_error(this->name() + " can only be run with two MDHistoWorkspaces as inputs");
75}
76
77//----------------------------------------------------------------------------------------------
79void WeightedMeanMD::execEvent() { throw std::runtime_error(this->name() + " can only be run on a MDHistoWorkspace."); }
80
81} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
const std::vector< double > & rhs
An implementation of IMDIterator that iterates through a MDHistoWorkspace.
Mantid::DataObjects::MDHistoWorkspace_sptr m_rhs_histo
Mantid::DataObjects::MDHistoWorkspace_sptr m_lhs_histo
WeightedMeanMD : Find the weighted mean of two MDWorkspaces.
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.
const std::string name() const override
Algorithm's name for identification.
void checkInputs() override
Check the inputs and throw if the algorithm cannot be run.
void execHistoHisto(Mantid::DataObjects::MDHistoWorkspace_sptr out, Mantid::DataObjects::MDHistoWorkspace_const_sptr operand) override
Run the algorithm with a MDHisotWorkspace as output and operand.
void execEvent() override
Run the algorithm with an MDEventWorkspace as output.
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.