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 +
10
11using namespace Mantid::Kernel;
12using namespace Mantid::API;
13
14namespace Mantid::MDAlgorithms {
15
16DECLARE_ALGORITHM(WeightedMeanMD)
17
18//----------------------------------------------------------------------------------------------
20bool WeightedMeanMD::commutative() const { return true; }
21
22//----------------------------------------------------------------------------------------------
25 if (!m_lhs_histo || !m_rhs_histo)
26 throw std::invalid_argument(this->name() + " can only be run on a MDHistoWorkspace.");
27}
28
29//----------------------------------------------------------------------------------------------
34 auto lhs = out->createIterator();
35 auto lhs_it = dynamic_cast<MDHistoWorkspaceIterator *>(lhs.get());
36 auto rhs = operand->createIterator();
37 auto rhs_it = dynamic_cast<MDHistoWorkspaceIterator *>(rhs.get());
38
39 if (!lhs_it || !rhs_it) {
40 throw std::logic_error("Histo iterators have wrong type.");
41 }
42
43 do {
44 double lhs_s = lhs_it->getSignal();
45 double lhs_err = lhs_it->getError();
46 double rhs_s = rhs_it->getSignal();
47 double rhs_err = rhs_it->getError();
48 double signal = 0;
49 double error_sq = 0;
50 if ((lhs_err > 0.0) && (rhs_err > 0.0)) {
51 double rhs_err_sq = rhs_err * rhs_err;
52 double lhs_err_sq = lhs_err * lhs_err;
53 double s = (rhs_s / rhs_err_sq) + (lhs_s / lhs_err_sq);
54 double e = rhs_err_sq * lhs_err_sq / (rhs_err_sq + lhs_err_sq);
55 signal = s * e;
56 error_sq = e;
57 } else if ((rhs_err > 0) && (lhs_err == 0)) {
58 signal = rhs_s;
59 error_sq = rhs_err * rhs_err;
60 } else if ((lhs_err > 0) && (rhs_err == 0)) {
61 signal = lhs_s;
62 error_sq = lhs_err * lhs_err;
63 }
64
65 size_t pos = lhs_it->getLinearIndex();
66 out->setSignalAt(pos, signal);
67 out->setErrorSquaredAt(pos, error_sq);
68 } while (lhs_it->next() && rhs_it->next());
69}
70
71//----------------------------------------------------------------------------------------------
75 throw std::runtime_error(this->name() + " can only be run with two MDHistoWorkspaces as inputs");
76}
77
78//----------------------------------------------------------------------------------------------
80void WeightedMeanMD::execEvent() { throw std::runtime_error(this->name() + " can only be run on a MDHistoWorkspace."); }
81
82} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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.