Mantid
Loading...
Searching...
No Matches
RebinnedOutput.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
10
11#include <algorithm>
12#include <iterator>
13#include <sstream>
14
15namespace Mantid::DataObjects {
16
17DECLARE_WORKSPACE(RebinnedOutput)
18
19
23const std::string RebinnedOutput::id() const { return "RebinnedOutput"; }
24
34void RebinnedOutput::init(const std::size_t &NVectors, const std::size_t &XLength, const std::size_t &YLength) {
35 Workspace2D::init(NVectors, XLength, YLength);
36 std::size_t nHist = this->getNumberHistograms();
37 this->fracArea.resize(nHist);
38 for (std::size_t i = 0; i < nHist; ++i) {
39 this->fracArea[i].resize(YLength);
40 }
41}
42
43void RebinnedOutput::init(const HistogramData::Histogram &histogram) {
45 std::size_t nHist = this->getNumberHistograms();
46 this->fracArea.resize(nHist);
47 for (std::size_t i = 0; i < nHist; ++i) {
48 this->fracArea[i].resize(histogram.size());
49 }
50}
51
57MantidVec &RebinnedOutput::dataF(const std::size_t index) { return this->fracArea[index]; }
58
65const MantidVec &RebinnedOutput::dataF(const std::size_t index) const { return this->fracArea[index]; }
66
73const MantidVec &RebinnedOutput::readF(const std::size_t index) const { return this->fracArea[index]; }
74
80void RebinnedOutput::setF(const std::size_t index, const MantidVecPtr &F) { this->fracArea[index] = *F; }
81
86void RebinnedOutput::scaleF(const double scale) {
87 std::size_t nHist = this->getNumberHistograms();
88 for (std::size_t i = 0; i < nHist; ++i) {
89 MantidVec &frac = this->dataF(i);
90 std::transform(frac.begin(), frac.end(), frac.begin(), [scale](double x) { return scale * x; });
91 }
92}
93
98
99 // Checks that the fractions are not all zeros.
100 auto nHist = static_cast<int>(this->getNumberHistograms());
101 bool frac_all_zeros = true;
102 for (int i = 0; i < nHist; ++i) {
103 const MantidVec &frac = this->readF(i);
104 if (std::accumulate(frac.begin(), frac.end(), 0.) != 0) {
105 frac_all_zeros = false;
106 break;
107 }
108 }
109 return !frac_all_zeros;
110}
111
120void RebinnedOutput::finalize(bool hasSqrdErrs) {
121 if (m_finalized && hasSqrdErrs == m_hasSqrdErrs)
122 return;
123
124 // Checks that the fractions are not all zeros.
125 if (!this->nonZeroF())
126 return;
127
128 // Fix the squared error representation before returning
129 auto nHist = static_cast<int>(this->getNumberHistograms());
130 if (m_finalized) {
132 for (int i = 0; i < nHist; ++i) {
133 MantidVec &err = this->dataE(i);
134 MantidVec &frac = this->dataF(i);
135 if (hasSqrdErrs) {
136 std::transform(err.begin(), err.end(), frac.begin(), err.begin(), std::divides<double>());
137 } else {
138 std::transform(err.begin(), err.end(), frac.begin(), err.begin(), std::multiplies<double>());
139 }
140 }
141
142 // Sets flag so subsequent algorithms know to correctly treat data
143 m_hasSqrdErrs = hasSqrdErrs;
144 return;
145 }
146
148 for (int i = 0; i < nHist; ++i) {
149 MantidVec &data = this->dataY(i);
150 MantidVec &err = this->dataE(i);
151 MantidVec &frac = this->dataF(i);
152 std::transform(data.begin(), data.end(), frac.begin(), data.begin(), std::divides<double>());
153 std::transform(err.begin(), err.end(), frac.begin(), err.begin(), std::divides<double>());
154 if (hasSqrdErrs) {
155 std::transform(err.begin(), err.end(), frac.begin(), err.begin(), std::divides<double>());
156 }
157 }
158 // Sets flags so subsequent algorithms know to correctly treat data
159 m_finalized = true;
160 m_hasSqrdErrs = hasSqrdErrs;
161}
162
170 if (!m_finalized || !this->nonZeroF())
171 return;
172
173 auto nHist = static_cast<int>(this->getNumberHistograms());
175 for (int i = 0; i < nHist; ++i) {
176 MantidVec &data = this->dataY(i);
177 MantidVec &err = this->dataE(i);
178 MantidVec &frac = this->dataF(i);
179 std::transform(data.begin(), data.end(), frac.begin(), data.begin(), std::multiplies<double>());
180 std::transform(err.begin(), err.end(), frac.begin(), err.begin(), std::multiplies<double>());
181 if (this->m_hasSqrdErrs) {
182 std::transform(err.begin(), err.end(), frac.begin(), err.begin(), std::multiplies<double>());
183 }
184 }
185 // Sets flag so subsequent algorithms know to correctly treat data
186 m_finalized = false;
187}
188
189} // namespace Mantid::DataObjects
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define DECLARE_WORKSPACE(classname)
virtual MantidVec & dataE(const std::size_t index)
Deprecated, use mutableE() instead. Returns the error data.
HistogramData::Histogram histogram(const size_t index) const
Returns the Histogram at the given workspace index.
virtual MantidVec & dataY(const std::size_t index)
Deprecated, use mutableY() instead. Returns the y data.
const MantidVec & readF(std::size_t const index) const
Returns a read-only (i.e. const) reference to the specified F array.
void scaleF(const double scale)
Multiply the fractional area arrays by a scale factor.
bool m_hasSqrdErrs
Flag to indiciate if the finalized data used squared errors.
virtual MantidVec & dataF(const std::size_t index)
Returns the fractional area.
void finalize(bool hasSqrdErrs=true)
Finalize to fractional area representation.
void unfinalize()
Undo fractional area representation.
bool nonZeroF() const
Returns if the fractional area is non zero.
bool m_finalized
Flag to indicate if finalize has been called, and if errors/variance used.
void setF(const std::size_t index, const MantidVecPtr &F)
Set the fractional area array for a given index.
void init(const std::size_t &NVectors, const std::size_t &XLength, const std::size_t &YLength) override
Called by initialize() in MatrixWorkspace.
std::vector< MantidVec > fracArea
A vector that holds the 1D vectors for the fractional area.
std::size_t getNumberHistograms() const override
Returns the histogram number.
std::vector< std::unique_ptr< Histogram1D > > data
A vector that holds the 1D histograms.
Definition: Workspace2D.h:97
void init(const std::size_t &NVectors, const std::size_t &XLength, const std::size_t &YLength) override
Called by initialize()
Definition: Workspace2D.cpp:52
Implements a copy on write data template.
Definition: cow_ptr.h:41
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
Definition: MultiThreaded.h:22
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition: cow_ptr.h:172
STL namespace.