Mantid
Loading...
Searching...
No Matches
GroupToXResolution.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
13#include "MantidHistogramData/HistogramBuilder.h"
16
17#include <boost/math/special_functions/pow.hpp>
18
19namespace {
20namespace Prop {
21std::string const FRACTION{"FractionOfDx"};
22std::string const INPUT_WS{"InputWorkspace"};
23std::string const OUTPUT_WS{"OutputWorkspace"};
24} // namespace Prop
25constexpr double FWHM_GAUSSIAN_EQUIVALENT{0.68};
26} // namespace
27
28namespace Mantid::Algorithms {
29// Register the algorithm into the AlgorithmFactory
30DECLARE_ALGORITHM(GroupToXResolution)
31
32
33const std::string GroupToXResolution::name() const { return "GroupToXResolution"; }
34
36int GroupToXResolution::version() const { return 1; }
37
39const std::string GroupToXResolution::category() const { return "Transforms\\Rebin"; }
40
42const std::string GroupToXResolution::summary() const {
43 return "Groups points within intervals given by the Dx into single points";
44}
45
49 auto inputValidator = std::make_shared<Kernel::CompositeValidator>();
50 inputValidator->add(std::make_shared<API::WorkspaceHasDxValidator>());
51 constexpr bool acceptHistograms{false};
52 inputValidator->add(std::make_shared<API::HistogramValidator>(acceptHistograms));
54 std::make_unique<API::WorkspaceProperty<>>(Prop::INPUT_WS, "", Kernel::Direction::Input, inputValidator),
55 "An input workspace with Dx values.");
56 declareProperty(std::make_unique<API::WorkspaceProperty<>>(Prop::OUTPUT_WS, "", Kernel::Direction::Output),
57 "The grouped workspace.");
58 auto positive = std::make_shared<Kernel::BoundedValidator<double>>();
59 positive->setLower(0.);
60 positive->setLowerExclusive(true);
61 declareProperty(Prop::FRACTION, 0.2, positive, "A fraction of Dx to group the points to.");
62}
63
64std::map<std::string, std::string> GroupToXResolution::validateInputs() {
65 std::map<std::string, std::string> issues;
66 API::MatrixWorkspace_const_sptr inWS = getProperty(Prop::INPUT_WS);
67 if (inWS->getNumberHistograms() != 1) {
68 issues[Prop::INPUT_WS] = "The workspace should contain only a single histogram.";
69 }
70 return issues;
71}
72
76 using boost::math::pow;
77 API::MatrixWorkspace_const_sptr inWS = getProperty(Prop::INPUT_WS);
78 double const groupingFraction = getProperty(Prop::FRACTION);
79 HistogramData::Histogram h(HistogramData::Histogram::XMode::Points, HistogramData::Histogram::YMode::Counts);
80 auto const &inXs = inWS->x(0);
81 auto const &inYs = inWS->y(0);
82 auto const &inEs = inWS->e(0);
83 auto const &inDxs = inWS->dx(0);
84 std::vector<double> outXs;
85 outXs.reserve(inXs.size());
86 std::vector<double> outYs;
87 outYs.reserve(inXs.size());
88 std::vector<double> outEs;
89 outEs.reserve(inXs.size());
90 std::vector<double> outDxs;
91 outDxs.reserve(inXs.size());
92 size_t pointIndex{0};
93 double begin = inXs.front();
94 while (true) {
95 auto const Dx = inDxs[pointIndex];
96 if (Dx <= 0.) {
97 throw std::out_of_range("Nonpositive DX value in the workspace.");
98 }
99 auto const width = groupingFraction * Dx;
100 auto const end = inXs[pointIndex] + width;
101 auto const beginXIterator = std::lower_bound(inXs.cbegin(), inXs.cend(), begin);
102 auto const endXIterator = std::lower_bound(inXs.cbegin(), inXs.cend(), end);
103 auto const pickSize = static_cast<size_t>(std::distance(beginXIterator, endXIterator));
104 if (pickSize > 0) {
105 auto const offset = static_cast<size_t>(std::distance(inXs.cbegin(), beginXIterator));
106 double xSum{0.};
107 double ySum{0.};
108 double eSquaredSum{0.};
109 for (size_t pickIndex = offset; pickIndex < offset + pickSize; ++pickIndex) {
110 xSum += inXs[pickIndex];
111 ySum += inYs[pickIndex];
112 eSquaredSum += pow<2>(inEs[pickIndex]);
113 }
114 outXs.emplace_back(xSum / static_cast<double>(pickSize));
115 outYs.emplace_back(ySum / static_cast<double>(pickSize));
116 outEs.emplace_back(std::sqrt(eSquaredSum) / static_cast<double>(pickSize));
117 auto const groupedXWidth = *std::prev(endXIterator) - *beginXIterator;
118 outDxs.emplace_back(std::sqrt(pow<2>(inDxs[pointIndex]) + pow<2>(FWHM_GAUSSIAN_EQUIVALENT * groupedXWidth)));
119 } else {
120 throw std::out_of_range("Failed to group. Is the X data sorted in ascending order?");
121 }
122 begin = end;
123 if (begin > inXs.back()) {
124 break;
125 }
126 pointIndex += pickSize;
127 }
128 HistogramData::HistogramBuilder constructionYard;
129 constructionYard.setX(std::move(outXs));
130 constructionYard.setY(std::move(outYs));
131 constructionYard.setE(std::move(outEs));
132 constructionYard.setDx(std::move(outDxs));
133 constructionYard.setDistribution(false);
134 API::MatrixWorkspace_sptr outWS = DataObjects::create<DataObjects::Workspace2D>(*inWS, constructionYard.build());
135 setProperty(Prop::OUTPUT_WS, outWS);
136}
137
138} // namespace Mantid::Algorithms
#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.
GroupToXResolution : Groups points within intervals defined by Dx into a single point.
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
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.
void exec() override
Execute the algorithm.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
static constexpr double h
Planck constant in J*s.
String constants for algorithm's properties.
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54