Mantid
Loading...
Searching...
No Matches
WeightedMean.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 +
8
9namespace Mantid::Algorithms {
10
11// Algorithm must be declared
12DECLARE_ALGORITHM(WeightedMean)
13
14bool WeightedMean::checkCompatibility(const API::MatrixWorkspace_const_sptr lhs,
15 const API::MatrixWorkspace_const_sptr rhs) const {
16 if (lhs->YUnit() != rhs->YUnit()) {
17 g_log.error("The two workspaces are not compatible because they have "
18 "different units for the data (Y).");
19 return false;
20 }
21 if (lhs->isDistribution() != rhs->isDistribution()) {
22 g_log.error("The two workspaces are not compatible because one is flagged "
23 "as a distribution.");
24 return false;
25 }
26
28}
29
40 // in order to be size compatible then the workspaces must be identically
41 // sized
42 if (lhs->size() == rhs->size()) {
43 return "";
44 } else {
45 return "Workspaces not identically sized.";
46 }
47}
48
49void WeightedMean::performBinaryOperation(const HistogramData::Histogram &lhs, const HistogramData::Histogram &rhs,
50 HistogramData::HistogramY &YOut, HistogramData::HistogramE &EOut) {
51 const size_t bins = lhs.size();
52 for (size_t j = 0; j < bins; ++j) {
53 if (lhs.e()[j] > 0.0 && rhs.e()[j] > 0.0) {
54 const double err1 = lhs.e()[j] * lhs.e()[j];
55 const double err2 = rhs.e()[j] * rhs.e()[j];
56 YOut[j] = (lhs.y()[j] / err1) + (rhs.y()[j] / err2);
57 EOut[j] = (err1 * err2) / (err1 + err2);
58 YOut[j] *= EOut[j];
59 EOut[j] = sqrt(EOut[j]);
60 } else if (lhs.e()[j] > 0.0 && rhs.e()[j] <= 0.0) {
61 YOut[j] = lhs.y()[j];
62 EOut[j] = lhs.e()[j];
63 } else if (lhs.e()[j] <= 0.0 && rhs.e()[j] > 0.0) {
64 YOut[j] = rhs.y()[j];
65 EOut[j] = rhs.e()[j];
66 } else {
67 YOut[j] = 0.0;
68 EOut[j] = 0.0;
69 }
70 }
71}
72
73void WeightedMean::performBinaryOperation(const HistogramData::Histogram &lhs, const double rhsY, const double rhsE,
74 HistogramData::HistogramY &YOut, HistogramData::HistogramE &EOut) {
75 assert(lhs.size() == 1);
76 // If we get here we've got two single column workspaces so it's easy.
77 if (lhs.e()[0] > 0.0 && rhsE > 0.0) {
78 const double err1 = lhs.e()[0] * lhs.e()[0];
79 const double err2 = rhsE * rhsE;
80 YOut[0] = (lhs.y()[0] / err1) + (rhsY / err2);
81 EOut[0] = (err1 * err2) / (err1 + err2);
82 YOut[0] *= EOut[0];
83 EOut[0] = sqrt(EOut[0]);
84 } else if (lhs.e()[0] > 0.0 && rhsE <= 0.0) {
85 YOut[0] = lhs.y()[0];
86 EOut[0] = lhs.e()[0];
87 } else if (lhs.e()[0] <= 0.0 && rhsE > 0.0) {
88 YOut[0] = rhsY;
89 EOut[0] = rhsE;
90 } else {
91 YOut[0] = 0.0;
92 EOut[0] = 0.0;
93 }
94}
95
96} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
const std::vector< double > & rhs
virtual bool checkCompatibility(const API::MatrixWorkspace_const_sptr lhs, const API::MatrixWorkspace_const_sptr rhs) const
Checks the compatibility of the two workspaces.
An algorithm to calculate the weighted mean of two workspaces.
Definition: WeightedMean.h:27
void performBinaryOperation(const HistogramData::Histogram &lhs, const HistogramData::Histogram &rhs, HistogramData::HistogramY &YOut, HistogramData::HistogramE &EOut) override
Carries out the binary operation on a single spectrum, with another spectrum as the right-hand operan...
std::string checkSizeCompatibility(const API::MatrixWorkspace_const_sptr lhs, const API::MatrixWorkspace_const_sptr rhs) const override
Performs a simple check to see if the sizes of two workspaces are identically sized.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)