-
Notifications
You must be signed in to change notification settings - Fork 122
/
FilterPeaks.cpp
163 lines (141 loc) · 6.23 KB
/
FilterPeaks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidCrystal/FilterPeaks.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
namespace {
double intensity(const Mantid::Geometry::IPeak &p) { return p.getIntensity(); }
double wavelength(const Mantid::Geometry::IPeak &p) {
return p.getWavelength();
}
double dspacing(const Mantid::Geometry::IPeak &p) { return p.getDSpacing(); }
double tof(const Mantid::Geometry::IPeak &p) { return p.getTOF(); }
double HKLSum(const Mantid::Geometry::IPeak &p) {
return p.getH() + p.getK() + p.getL();
}
double HKL2(const Mantid::Geometry::IPeak &p) {
return p.getH() * p.getH() + p.getK() * p.getK() + p.getL() * p.getL();
}
double QMOD(const Mantid::Geometry::IPeak &p) {
return p.getQSampleFrame().norm();
}
double SN(const Mantid::Geometry::IPeak &p) {
return p.getIntensity() / p.getSigmaIntensity();
}
double RUN(const Mantid::Geometry::IPeak &p) { return p.getRunNumber(); }
} // namespace
namespace Mantid {
namespace Crystal {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(FilterPeaks)
using namespace Kernel;
using namespace API;
using DataObjects::PeaksWorkspace;
using DataObjects::PeaksWorkspace_const_sptr;
using DataObjects::PeaksWorkspace_sptr;
/// Algorithm's name for identification. @see Algorithm::name
const std::string FilterPeaks::name() const { return "FilterPeaks"; }
/// Algorithm's version for identification. @see Algorithm::version
int FilterPeaks::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string FilterPeaks::category() const { return "Crystal\\Peaks"; }
/** Initialize the algorithm's properties.
*/
void FilterPeaks::init() {
declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>(
"InputWorkspace", "", Direction::Input),
"The input workspace");
declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"The filtered workspace");
std::vector<std::string> filters{"h+k+l", "h^2+k^2+l^2", "Intensity",
"Signal/Noise", "QMod", "Wavelength",
"DSpacing", "TOF", "RunNumber"};
declareProperty("FilterVariable", "",
std::make_shared<StringListValidator>(filters),
"The variable on which to filter the peaks");
declareProperty("FilterValue", EMPTY_DBL(),
std::make_shared<MandatoryValidator<double>>(),
"The value of the FilterVariable to compare each peak to");
std::vector<std::string> operation{"<", ">", "=", "!=", "<=", ">="};
declareProperty("Operator", "<",
std::make_shared<StringListValidator>(operation), "");
}
/** Execute the algorithm.
*/
void FilterPeaks::exec() {
PeaksWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
PeaksWorkspace_sptr filteredWS = std::dynamic_pointer_cast<PeaksWorkspace>(
WorkspaceFactory::Instance().createPeaks());
// Copy over ExperimentInfo from input workspace
filteredWS->copyExperimentInfoFrom(inputWS.get());
const double filterValue = getProperty("FilterValue");
const std::string Operator = getProperty("Operator");
const std::string filterVariable = getProperty("FilterVariable");
const auto filterFunction = getFilterVariableFunction(filterVariable);
// Choose which version of the function to use based on the operator
if (Operator == "<")
filterPeaks<std::less<double>>(*inputWS, *filteredWS, filterFunction,
filterValue);
else if (Operator == ">")
filterPeaks<std::greater<double>>(*inputWS, *filteredWS, filterFunction,
filterValue);
else if (Operator == "=")
filterPeaks<std::equal_to<double>>(*inputWS, *filteredWS, filterFunction,
filterValue);
else if (Operator == "!=")
filterPeaks<std::not_equal_to<double>>(*inputWS, *filteredWS,
filterFunction, filterValue);
else if (Operator == "<=")
filterPeaks<std::less_equal<double>>(*inputWS, *filteredWS, filterFunction,
filterValue);
else if (Operator == ">=")
filterPeaks<std::greater_equal<double>>(*inputWS, *filteredWS,
filterFunction, filterValue);
else
throw std::invalid_argument("Unknown Operator " + Operator);
setProperty("OutputWorkspace", filteredWS);
}
/** Get the filter function to use to filter peaks
*
* This will return a std::function object that takes a peak
* and returns a double representing the current value of this
* peak to be compared against the user's filter value.
*
* @param filterVariable :: a string representing the filter function to use.
* e.g. "h+k+l" or "TOF".
* @return a function object which will return the a value for a given peak
*/
FilterPeaks::FilterFunction FilterPeaks::getFilterVariableFunction(
const std::string &filterVariable) const {
FilterFunction filterFunction;
if (filterVariable == "h+k+l")
filterFunction = &HKLSum;
else if (filterVariable == "h^2+k^2+l^2")
filterFunction = &HKL2;
else if (filterVariable == "Intensity")
filterFunction = &intensity;
else if (filterVariable == "Wavelength")
filterFunction = &wavelength;
else if (filterVariable == "DSpacing")
filterFunction = &dspacing;
else if (filterVariable == "TOF")
filterFunction = &tof;
else if (filterVariable == "Signal/Noise")
filterFunction = &SN;
else if (filterVariable == "QMod")
filterFunction = &QMOD;
else if (filterVariable == "RunNumber")
filterFunction = &RUN;
else
throw std::invalid_argument("Unknown FilterVariable: " + filterVariable);
return filterFunction;
}
} // namespace Crystal
} // namespace Mantid