/
FilterPeaks.h
98 lines (86 loc) · 3.73 KB
/
FilterPeaks.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2012 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 +
#pragma once
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IPeaksWorkspace.h"
#include "MantidCrystal/DllConfig.h"
namespace Mantid {
namespace Crystal {
/** FilterPeaks : Filter a peaks workspace based on a set number of queries to
provide a new, filtered peaks workspace.
*/
class MANTID_CRYSTAL_DLL FilterPeaks final : public API::Algorithm {
public:
const std::string name() const override;
/// Summary of algorithms purpose
const std::string summary() const override {
return "Filters the peaks in a peaks workspace based upon the valur of a "
"chosen variable.";
}
int version() const override;
const std::vector<std::string> seeAlso() const override { return {"CreatePeaksWorkspace"}; }
const std::string category() const override;
private:
/// Typedef for the function to get the variable we're filtering against
using FilterFunction = std::function<double(const Geometry::IPeak &)>;
using FilterFunctionStr = std::function<std::string(const Geometry::IPeak &)>;
/// Override for algorithm init
void init() override;
/// Override for algorithm exec
void exec() override;
/// Get a function to filter peaks by
FilterFunction getFilterVariableFunction(const std::string &filterVariable) const;
/** Filter the input peaks workspace using a comparator and a value selection
* function
*
* The template parameter is a comparator function such as std::less to use to
* compare the filterValue against the corresponding value of the peak.
*
* @param inputWS :: the input peaks workspace containing peaks to be filtered
* @param filteredWS :: the output peaks workspace which will contain a subset
* of the inputWS
* @param filterFunction :: function extracting the value to filter on from
* the peak object
* @param filterValue :: the value to compare the filter variable against
*/
template <typename Comparator>
void filterPeaks(const Mantid::API::IPeaksWorkspace &inputWS, Mantid::API::IPeaksWorkspace &filteredWS,
const FilterFunction &filterFunction, const double filterValue) {
Comparator operatorFunc;
for (int i = 0; i < inputWS.getNumberPeaks(); ++i) {
const Geometry::IPeak ¤tPeak = inputWS.getPeak(i);
const auto currentValue = filterFunction(currentPeak);
if (operatorFunc(currentValue, filterValue))
filteredWS.addPeak(currentPeak);
}
}
/**
* @brief Select peaks from the input peak workspace by checking a string
* value
*
* @tparam Comparator
* @param inputWS :: the input peaks workspace containing peaks to be filtered
* @param filteredWS :: the output peaks workspace which will contain a subset
* of the inputWS
* @param filterFunction :: function extracting the value to filter on from
* the peak object
* @param filterValue :: the string value to check
*/
template <typename Comparator>
void filterPeaksStr(const Mantid::API::IPeaksWorkspace &inputWS, Mantid::API::IPeaksWorkspace &filteredWS,
const FilterFunctionStr &filterFunction, const std::string &filterValue) {
Comparator operatorFunc;
for (int i = 0; i < inputWS.getNumberPeaks(); ++i) {
const Geometry::IPeak ¤tPeak = inputWS.getPeak(i);
const auto currentValue = filterFunction(currentPeak);
if (operatorFunc(currentValue, filterValue))
filteredWS.addPeak(currentPeak);
}
}
};
} // namespace Crystal
} // namespace Mantid