Mantid
Loading...
Searching...
No Matches
FilterPeaks.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 +
13
15
16namespace {
17
18// Filter functions
19double intensity(const Mantid::Geometry::IPeak &p) { return p.getIntensity(); }
20
21double wavelength(const Mantid::Geometry::IPeak &p) { return p.getWavelength(); }
22
23double dspacing(const Mantid::Geometry::IPeak &p) { return p.getDSpacing(); }
24
25double tof(const Mantid::Geometry::IPeak &p) { return p.getTOF(); }
26
27double HKLSum(const Mantid::Geometry::IPeak &p) { return p.getH() + p.getK() + p.getL(); }
28
29double HKL2(const Mantid::Geometry::IPeak &p) {
30 return p.getH() * p.getH() + p.getK() * p.getK() + p.getL() * p.getL();
31}
32
33double QMOD(const Mantid::Geometry::IPeak &p) { return p.getQSampleFrame().norm(); }
34
35double SN(const Mantid::Geometry::IPeak &p) { return p.getIntensity() / p.getSigmaIntensity(); }
36
37double RUN(const Mantid::Geometry::IPeak &p) { return p.getRunNumber(); }
38
39std::string BANKNAME(const Mantid::Geometry::IPeak &p) {
40 const Peak &fullPeak = dynamic_cast<const Peak &>(p);
41 return fullPeak.getBankName();
42}
43
44} // namespace
45
46// namespace
47namespace Mantid::Crystal {
48// Register the algorithm into the AlgorithmFactory
49DECLARE_ALGORITHM(FilterPeaks)
50
51using namespace Kernel;
52using namespace API;
53using API::IPeaksWorkspace;
56
58const std::string FilterPeaks::name() const { return "FilterPeaks"; }
60int FilterPeaks::version() const { return 1; }
62const std::string FilterPeaks::category() const { return "Crystal\\Peaks"; }
63
67 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("InputWorkspace", "", Direction::Input),
68 "The input workspace");
69 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("OutputWorkspace", "", Direction::Output),
70 "The filtered workspace");
71
72 // filter by property
73 const std::string FILTER("Filter Options");
74 std::vector<std::string> filters{"h+k+l", "h^2+k^2+l^2", "Intensity", "Signal/Noise", "QMod",
75 "Wavelength", "DSpacing", "TOF", "RunNumber"};
76 declareProperty("FilterVariable", "h+k+l", std::make_shared<StringListValidator>(filters),
77 "The variable on which to filter the peaks");
78
79 declareProperty("FilterValue", EMPTY_DBL(), "The value of the FilterVariable to compare each peak to");
80
81 std::vector<std::string> operation{"<", ">", "=", "!=", "<=", ">="};
82 declareProperty("Operator", "<", std::make_shared<StringListValidator>(operation), "");
83 setPropertyGroup("FilterVariable", FILTER);
84 setPropertyGroup("FilterValue", FILTER);
85 setPropertyGroup("Operator", FILTER);
86
87 // select by bankname
88 const std::string SELECT("Select Bank by Name");
89 std::vector<std::string> action{"=", "!="};
90 declareProperty("Criterion", "=", std::make_shared<StringListValidator>(action), "");
91 declareProperty("BankName", "", "Selected bank name, empty means skip selection. Applicable only to PeaksWorkspace");
92 setPropertyGroup("Criterion", SELECT);
93 setPropertyGroup("BankName", SELECT);
94}
95
99 IPeaksWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
100 IPeaksWorkspace_sptr filteredWS =
101 std::dynamic_pointer_cast<IPeaksWorkspace>(WorkspaceFactory::Instance().createPeaks(inputWS->id()));
102
103 // Copy over ExperimentInfo from input workspace
104 filteredWS->copyExperimentInfoFrom(inputWS.get());
105
106 const double filterValue = getProperty("FilterValue");
107 const std::string Operator = getProperty("Operator");
108 const std::string filterVariable = getProperty("FilterVariable");
109
110 const std::string bankname = getProperty("BankName");
111 const std::string criterion = getProperty("Criterion");
112
113 if (!bankname.empty() && (inputWS->id() == "PeaksWorkspace")) {
114 FilterFunctionStr filterFunction = &BANKNAME;
115 IPeaksWorkspace_sptr selectedWS = filteredWS->clone();
116
117 if (criterion == "=")
118 filterPeaksStr<std::equal_to<std::string>>(*inputWS, *selectedWS, filterFunction, bankname);
119 else if (criterion == "!=")
120 filterPeaksStr<std::not_equal_to<std::string>>(*inputWS, *selectedWS, filterFunction, bankname);
121 else
122 throw std::invalid_argument("Unsupported operator " + criterion + " for BankName filter");
123
124 inputWS = selectedWS;
125 setProperty("OutputWorkspace", selectedWS);
126 }
127
128 if (!isDefault("FilterValue")) {
129 const auto filterFunction = getFilterVariableFunction(filterVariable);
130 // Choose which version of the function to u.se based on the operator
131 if (Operator == "<")
132 filterPeaks<std::less<double>>(*inputWS, *filteredWS, filterFunction, filterValue);
133 else if (Operator == ">")
134 filterPeaks<std::greater<double>>(*inputWS, *filteredWS, filterFunction, filterValue);
135 else if (Operator == "=")
136 filterPeaks<std::equal_to<double>>(*inputWS, *filteredWS, filterFunction, filterValue);
137 else if (Operator == "!=")
138 filterPeaks<std::not_equal_to<double>>(*inputWS, *filteredWS, filterFunction, filterValue);
139 else if (Operator == "<=")
140 filterPeaks<std::less_equal<double>>(*inputWS, *filteredWS, filterFunction, filterValue);
141 else if (Operator == ">=")
142 filterPeaks<std::greater_equal<double>>(*inputWS, *filteredWS, filterFunction, filterValue);
143 else
144 throw std::invalid_argument("Unknown Operator " + Operator);
145
146 setProperty("OutputWorkspace", filteredWS);
147 }
148}
149
161 FilterFunction filterFunction;
162 if (filterVariable == "h+k+l")
163 filterFunction = &HKLSum;
164 else if (filterVariable == "h^2+k^2+l^2")
165 filterFunction = &HKL2;
166 else if (filterVariable == "Intensity")
167 filterFunction = &intensity;
168 else if (filterVariable == "Wavelength")
169 filterFunction = &wavelength;
170 else if (filterVariable == "DSpacing")
171 filterFunction = &dspacing;
172 else if (filterVariable == "TOF")
173 filterFunction = &tof;
174 else if (filterVariable == "Signal/Noise")
175 filterFunction = &SN;
176 else if (filterVariable == "QMod")
177 filterFunction = &QMOD;
178 else if (filterVariable == "RunNumber")
179 filterFunction = &RUN;
180 else
181 throw std::invalid_argument("Unknown FilterVariable: " + filterVariable);
182 return filterFunction;
183}
184
185} // namespace Mantid::Crystal
#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
bool isDefault(const std::string &name) const
Definition: Algorithm.cpp:2084
A property class for workspaces.
void init() override
Override for algorithm init.
Definition: FilterPeaks.cpp:66
const std::string name() const override
Algorithm's name for identification.
Definition: FilterPeaks.cpp:58
FilterFunction getFilterVariableFunction(const std::string &filterVariable) const
Get a function to filter peaks by.
void exec() override
Override for algorithm exec.
Definition: FilterPeaks.cpp:98
const std::string category() const override
Algorithm's category for identification.
Definition: FilterPeaks.cpp:62
std::function< double(const Geometry::IPeak &)> FilterFunction
Typedef for the function to get the variable we're filtering against.
Definition: FilterPeaks.h:34
std::function< std::string(const Geometry::IPeak &)> FilterFunctionStr
Definition: FilterPeaks.h:35
int version() const override
Algorithm's version for identification.
Definition: FilterPeaks.cpp:60
Structure describing a single-crystal peak.
Definition: Peak.h:34
std::string getBankName() const
Find the name of the bank that is the parent of the detector.
Definition: Peak.cpp:353
Structure describing a single-crystal peak.
Definition: IPeak.h:26
virtual double getH() const =0
virtual double getTOF() const =0
virtual double getSigmaIntensity() const =0
virtual double getWavelength() const =0
virtual double getDSpacing() const =0
virtual double getK() const =0
virtual int getRunNumber() const =0
virtual Mantid::Kernel::V3D getQSampleFrame() const =0
virtual double getIntensity() const =0
virtual double getL() const =0
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
double norm() const noexcept
Definition: V3D.h:263
std::shared_ptr< IPeaksWorkspace > IPeaksWorkspace_sptr
shared pointer to Mantid::API::IPeaksWorkspace
std::shared_ptr< const IPeaksWorkspace > IPeaksWorkspace_const_sptr
shared pointer to Mantid::API::IPeaksWorkspace (const version)
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54