Mantid
Loading...
Searching...
No Matches
FindSXPeaksHelper.h
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 +
7#pragma once
8
10#include "MantidCrystal/DllConfig.h"
11#include "MantidHistogramData/HistogramX.h"
12#include "MantidHistogramData/HistogramY.h"
14#include "MantidKernel/System.h"
15#include "MantidKernel/Unit.h"
16
17#include <boost/optional.hpp>
18#include <iterator>
19#include <vector>
20
21namespace Mantid {
22namespace Kernel {
23class ProgressBase;
24}
25} // namespace Mantid
26
27namespace Mantid {
28namespace Crystal {
29namespace FindSXPeaksHelper {
30
32enum class XAxisUnit { TOF, DSPACING };
33
34/* ------------------------------------------------------------------------------------------
35 * Single Crystal peak representation
36 * ------------------------------------------------------------------------------------------
37 */
38struct MANTID_CRYSTAL_DLL SXPeak {
39public:
40 SXPeak(double t, double phi, double intensity, const std::vector<int> &spectral, const size_t wsIndex,
41 const Mantid::API::SpectrumInfo &spectrumInfo);
42
45 bool compare(const SXPeak &rhs, double tolerance) const;
46
49 bool compare(const SXPeak &rhs, const double xTolerance, const double phiTolerance, const double thetaTolerance,
50 const XAxisUnit tofUnits = XAxisUnit::TOF) const;
51
53 Mantid::Kernel::V3D getQ() const;
54
56 SXPeak &operator+=(const SXPeak &rhs);
57
59 void reduce();
60
61 friend std::ostream &operator<<(std::ostream &os, const SXPeak &rhs) {
62 os << rhs.m_tof << "," << rhs.m_twoTheta << "," << rhs.m_phi << "," << rhs.m_intensity << "\n";
63 os << " Spectra";
64 std::copy(rhs.m_spectra.begin(), rhs.m_spectra.end(), std::ostream_iterator<int>(os, ","));
65 return os;
66 }
67
68 const double &getIntensity() const;
69
70 detid_t getDetectorId() const;
71
72private:
74 double m_tof;
76 double m_dSpacing;
78 double m_twoTheta;
80 double m_phi;
84 std::vector<int> m_spectra;
86 double m_LTotal;
88 size_t m_wsIndex;
96 std::string m_qConvention;
97};
98
99using yIt = Mantid::HistogramData::HistogramY::const_iterator;
100using Bound = HistogramData::HistogramX::const_iterator;
101using BoundsIterator = std::pair<Bound, Bound>;
102using PeakList = boost::optional<std::vector<SXPeak>>;
103
105public:
106 PeakContainer(const HistogramData::HistogramY &y);
107 void startRecord(yIt item);
108 void stopRecord(yIt item);
109 void record(yIt item);
110 size_t getNumberOfPointsInPeak() const;
111 yIt getMaxIterator() const;
112
113private:
114 const HistogramData::HistogramY &m_y;
118 double m_maxSignal = -1.;
119};
120
121/* ------------------------------------------------------------------------------------------
122 * Background strategy
123 * ------------------------------------------------------------------------------------------
124 */
125struct MANTID_CRYSTAL_DLL BackgroundStrategy {
126 virtual ~BackgroundStrategy() = default;
127 virtual bool isBelowBackground(const double intensity, const HistogramData::HistogramY &y) const = 0;
128};
129
130struct MANTID_CRYSTAL_DLL AbsoluteBackgroundStrategy : public BackgroundStrategy {
131 AbsoluteBackgroundStrategy(const double background);
132 bool isBelowBackground(const double intensity, const HistogramData::HistogramY &) const override;
133
134private:
135 double m_background = 0.;
136};
137
138struct MANTID_CRYSTAL_DLL PerSpectrumBackgroundStrategy : public BackgroundStrategy {
139 PerSpectrumBackgroundStrategy(const double backgroundMultiplier);
140 bool isBelowBackground(const double intensity, const HistogramData::HistogramY &y) const override;
141
142private:
143 double m_backgroundMultiplier = 1.;
144};
145
146/* ------------------------------------------------------------------------------------------
147 * Peak Finding Strategy
148 * ------------------------------------------------------------------------------------------
149 */
150class MANTID_CRYSTAL_DLL PeakFindingStrategy {
151public:
152 PeakFindingStrategy(const BackgroundStrategy *backgroundStrategy, const API::SpectrumInfo &spectrumInfo,
153 const double minValue = EMPTY_DBL(), const double maxValue = EMPTY_DBL(),
154 const XAxisUnit units = XAxisUnit::TOF);
155 virtual ~PeakFindingStrategy() = default;
156 PeakList findSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y,
157 const int workspaceIndex) const;
158
159protected:
160 BoundsIterator getBounds(const HistogramData::HistogramX &x) const;
161 double calculatePhi(size_t workspaceIndex) const;
162 double getXValue(const HistogramData::HistogramX &x, const size_t peakLocation) const;
163 double convertToTOF(const double xValue, const size_t workspaceIndex) const;
164 virtual PeakList dofindSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y, Bound low,
165 Bound high, const int workspaceIndex) const = 0;
166
168 const double m_minValue = EMPTY_DBL();
169 const double m_maxValue = EMPTY_DBL();
172};
173
174class MANTID_CRYSTAL_DLL StrongestPeaksStrategy : public PeakFindingStrategy {
175public:
176 StrongestPeaksStrategy(const BackgroundStrategy *backgroundStrategy, const API::SpectrumInfo &spectrumInfo,
177 const double minValue = EMPTY_DBL(), const double maxValue = EMPTY_DBL(),
178 const XAxisUnit units = XAxisUnit::TOF);
179 PeakList dofindSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y, Bound low, Bound high,
180 const int workspaceIndex) const override;
181};
182
183class MANTID_CRYSTAL_DLL AllPeaksStrategy : public PeakFindingStrategy {
184public:
185 AllPeaksStrategy(const BackgroundStrategy *backgroundStrategy, const API::SpectrumInfo &spectrumInfo,
186 const double minValue = EMPTY_DBL(), const double maxValue = EMPTY_DBL(),
187 const XAxisUnit units = XAxisUnit::TOF);
188 PeakList dofindSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y, Bound low, Bound high,
189 const int workspaceIndex) const override;
190
191private:
192 std::vector<std::unique_ptr<PeakContainer>>
193 getAllPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y, Bound low, Bound high,
194 const Mantid::Crystal::FindSXPeaksHelper::BackgroundStrategy *backgroundStrategy) const;
195 PeakList convertToSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y,
196 const std::vector<std::unique_ptr<PeakContainer>> &peaks, const int workspaceIndex) const;
197};
198
199/* ------------------------------------------------------------------------------------------
200 * Comparison Strategy
201 * ------------------------------------------------------------------------------------------
202 */
203class MANTID_CRYSTAL_DLL CompareStrategy {
204public:
205 virtual ~CompareStrategy() = default;
206 virtual bool compare(const SXPeak &lhs, const SXPeak &rhs) const = 0;
207};
208
209class MANTID_CRYSTAL_DLL RelativeCompareStrategy : public CompareStrategy {
210public:
211 RelativeCompareStrategy(const double resolution);
212 bool compare(const SXPeak &lhs, const SXPeak &rhs) const override;
213
214private:
215 const double m_resolution;
216};
217
218class MANTID_CRYSTAL_DLL AbsoluteCompareStrategy : public CompareStrategy {
219public:
220 AbsoluteCompareStrategy(const double tofResolution, const double phiResolution, const double twoThetaResolution,
221 const XAxisUnit units = XAxisUnit::TOF);
222 bool compare(const SXPeak &lhs, const SXPeak &rhs) const override;
223
224private:
225 const double m_xUnitResolution;
229};
230
231/* ------------------------------------------------------------------------------------------
232 * PeakListReduction Strategy
233 * ------------------------------------------------------------------------------------------
234 */
235class MANTID_CRYSTAL_DLL ReducePeakListStrategy {
236public:
237 ReducePeakListStrategy(const CompareStrategy *compareStrategy);
238 virtual ~ReducePeakListStrategy() = default;
239 virtual std::vector<SXPeak> reduce(const std::vector<SXPeak> &peaks,
240 Mantid::Kernel::ProgressBase &progress) const = 0;
241
242protected:
244};
245
246class MANTID_CRYSTAL_DLL SimpleReduceStrategy : public ReducePeakListStrategy {
247public:
248 SimpleReduceStrategy(const CompareStrategy *compareStrategy);
249 std::vector<SXPeak> reduce(const std::vector<SXPeak> &peaks, Mantid::Kernel::ProgressBase &progress) const override;
250};
251
252class MANTID_CRYSTAL_DLL FindMaxReduceStrategy : public ReducePeakListStrategy {
253public:
254 FindMaxReduceStrategy(const CompareStrategy *compareStrategy);
255 std::vector<SXPeak> reduce(const std::vector<SXPeak> &peaks, Mantid::Kernel::ProgressBase &progress) const override;
256
257private:
258 std::vector<std::vector<SXPeak *>> getPeakGroups(const std::vector<SXPeak> &peakList,
259 Mantid::Kernel::ProgressBase &progress) const;
260 std::vector<SXPeak> getFinalPeaks(const std::vector<std::vector<SXPeak *>> &peakGroups) const;
261};
262
263} // namespace FindSXPeaksHelper
264} // namespace Crystal
265} // namespace Mantid
const std::vector< double > & rhs
double maxValue
double minValue
double tolerance
API::SpectrumInfo is an intermediate step towards a SpectrumInfo that is part of Instrument-2....
Definition: SpectrumInfo.h:53
virtual bool compare(const SXPeak &lhs, const SXPeak &rhs) const =0
virtual PeakList dofindSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y, Bound low, Bound high, const int workspaceIndex) const =0
virtual std::vector< SXPeak > reduce(const std::vector< SXPeak > &peaks, Mantid::Kernel::ProgressBase &progress) const =0
Class for 3D vectors.
Definition: V3D.h:34
MatrixWorkspace_sptr MANTID_API_DLL operator+=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Adds two workspaces.
Mantid::HistogramData::HistogramY::const_iterator yIt
boost::optional< std::vector< SXPeak > > PeakList
HistogramData::HistogramX::const_iterator Bound
XAxisUnit
enum to determine the units of the workspaces X axis we are searching in
std::pair< Bound, Bound > BoundsIterator
bool compare(const mypair &left, const mypair &right)
Definition: LoadSassena.cpp:91
Helper class which provides the Collimation Length for SANS instruments.
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43
virtual bool isBelowBackground(const double intensity, const HistogramData::HistogramY &y) const =0
friend std::ostream & operator<<(std::ostream &os, const SXPeak &rhs)
int m_nPixels
Number of contributing pixels.
double m_phi
Phi angle for the centre detector of the peak.
double m_tof
TOF for the peak centre.
std::vector< int > m_spectra
Contributing spectra to this peak.
Kernel::V3D m_unitWaveVector
Unit vector in the direction of the wavevector.
double m_LTotal
Detector-sample distance.
double m_intensity
Measured intensity of centre of the peak.
double m_dSpacing
d-spacing at the peak centre
double m_twoTheta
2 * theta angle for then centre detector of the peak
size_t m_wsIndex
Detector workspace index.