Mantid
Loading...
Searching...
No Matches
WeightedMeanOfWorkspace.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
13namespace Mantid {
14using namespace API;
15using namespace DataObjects;
16using namespace Geometry;
17using namespace Kernel;
18
19namespace Algorithms {
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(WeightedMeanOfWorkspace)
22
23
24const std::string WeightedMeanOfWorkspace::name() const { return "WeightedMeanOfWorkspace"; }
25
27int WeightedMeanOfWorkspace::version() const { return 1; }
28
30const std::string WeightedMeanOfWorkspace::category() const { return "Arithmetic"; }
31
35 this->declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
36 "An input workspace.");
37 this->declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
38 "An output workspace.");
39}
40
44 MatrixWorkspace_sptr inputWS = this->getProperty("InputWorkspace");
45 // Check if it is an event workspace
46 EventWorkspace_const_sptr eventW = std::dynamic_pointer_cast<const EventWorkspace>(inputWS);
47 if (eventW != nullptr) {
48 throw std::runtime_error("WeightedMeanOfWorkspace cannot handle EventWorkspaces!");
49 }
50 // Create the output workspace
51 MatrixWorkspace_sptr singleValued = WorkspaceFactory::Instance().create("WorkspaceSingleValue", 1, 1, 1);
52 // Calculate weighted mean
53 std::size_t numHists = inputWS->getNumberHistograms();
54 double averageValue = 0.0;
55 double weightSum = 0.0;
56 const auto &spectrumInfo = inputWS->spectrumInfo();
57 for (std::size_t i = 0; i < numHists; ++i) {
58 if (spectrumInfo.hasDetectors(i))
59 if (spectrumInfo.isMonitor(i) || spectrumInfo.isMasked(i))
60 continue;
61 auto &y = inputWS->y(i);
62 auto &e = inputWS->e(i);
63 double weight = 0.0;
64 for (std::size_t j = 0; j < y.size(); ++j) {
65 if (std::isfinite(y[j]) && std::isfinite(e[j])) {
66 weight = 1.0 / (e[j] * e[j]);
67 averageValue += (y[j] * weight);
68 weightSum += weight;
69 }
70 }
71 }
72 singleValued->mutableX(0)[0] = 0.0;
73 singleValued->mutableY(0)[0] = averageValue / weightSum;
74 singleValued->mutableE(0)[0] = std::sqrt(weightSum);
75 this->setProperty("OutputWorkspace", singleValued);
76}
77
78} // namespace Algorithms
79} // namespace Mantid
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
A property class for workspaces.
WeightedMeanOfWorkspace This algorithm calculates the weighted mean for a single workspace from all t...
void exec() override
Execute the algorithm.
int version() const override
Algorithm's version for identification.
void init() override
Initialize the algorithm's properties.
const std::string category() const override
Algorithm's category for identification.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54