Mantid
Loading...
Searching...
No Matches
FilterByXValue.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 +
9
10namespace Mantid::Algorithms {
11// Register the algorithm into the AlgorithmFactory
12DECLARE_ALGORITHM(FilterByXValue)
13
14using namespace Kernel;
15using namespace API;
16using namespace DataObjects;
17
19const std::string FilterByXValue::name() const { return "FilterByXValue"; }
21int FilterByXValue::version() const { return 1; }
23const std::string FilterByXValue::category() const { return "Events\\EventFiltering"; }
24
26 declareProperty(std::make_unique<WorkspaceProperty<EventWorkspace>>("InputWorkspace", "", Direction::Input),
27 "The input workspace.");
28 declareProperty(std::make_unique<WorkspaceProperty<EventWorkspace>>("OutputWorkspace", "", Direction::Output),
29 "The output workspace.");
30 declareProperty("XMin", EMPTY_DBL(),
31 "The minimum X value (in the units of "
32 "the input workspace) for which events "
33 "will be retained\n"
34 "(default: event list min)");
35 declareProperty("XMax", EMPTY_DBL(),
36 "The maximum X value (in the units of "
37 "the input workspace) for which events "
38 "will be retained. Must be greater than "
39 "XMin.\n"
40 "(default: event list max)");
41}
42
43std::map<std::string, std::string> FilterByXValue::validateInputs() {
44 std::map<std::string, std::string> errors;
45
46 const double xmin = getProperty("XMin");
47 const double xmax = getProperty("XMax");
48
49 if (isEmpty(xmin) && isEmpty(xmax)) {
50 errors["XMin"] = "At least one of XMin/XMax must be specified.";
51 errors["XMax"] = "At least one of XMin/XMax must be specified.";
52 return errors;
53 }
54
55 if (!isEmpty(xmin) && !isEmpty(xmax) && xmax <= xmin) {
56 errors["XMin"] = "XMin must be less than XMax.";
57 errors["XMax"] = "XMin must be less than XMax.";
58 }
59
60 return errors;
61}
62
64 // Get the properties
65 EventWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
66 EventWorkspace_sptr outputWS = getProperty("OutputWorkspace");
67 double xmin = getProperty("XMin");
68 double xmax = getProperty("XMax");
69 // We need to reduce XMin & increase XMax slightly as we want to keep events
70 // with exactly those values
71 xmin *= 0.999999999;
72 xmax *= 1.000000001;
73
74 const auto numSpec = static_cast<int>(inputWS->getNumberHistograms());
75
76 // Check if we're doing thing in-place.
77 if (inputWS != outputWS) {
78 // TODO: Make this more efficient by only copying over the events that pass
79 // the
80 // filter rather than copying everything and then removing some. This should
81 // entail new methods (e.g. iterators) on EventList as this algorithm
82 // shouldn't
83 // need to know about the type of the events (e.g. weighted).
84 outputWS = inputWS->clone();
85 setProperty("OutputWorkspace", outputWS);
86 }
87
88 Progress prog(this, 0.0, 1.0, numSpec);
89 // Loop over the workspace, removing the events that don't pass the filter
91 for (int spec = 0; spec < numSpec; ++spec) {
93
94 EventList &events = outputWS->getSpectrum(spec);
95 // Sort to make getting the tof min/max faster (& since maskTof will sort
96 // anyway)
97 events.sortTof();
98 if (!isEmpty(xmin)) {
99 const double list_xmin = events.getTofMin();
100 if (xmin > list_xmin)
101 events.maskTof(list_xmin, xmin);
102 // Despite the name, maskTof really only does filtering which is what we
103 // want
104 }
105 if (!isEmpty(xmax)) {
106 const double list_xmax = events.getTofMax();
107 // Need to scale up list_xmax slightly to avoid retaining the last event
108 // in the list
109 if (xmax < list_xmax)
110 events.maskTof(xmax, list_xmax * 1.000000001);
111 }
112
113 prog.report();
115 }
117}
118
119} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
Definition: MultiThreaded.h:94
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
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
static bool isEmpty(const NumT toCheck)
checks that the value was not set by users, uses the value in empty double/int.
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void exec() override
Virtual method - must be overridden by concrete algorithm.
const std::string name() const override
Algorithm's name for identification.
const std::string category() const override
Algorithm's category for identification.
void init() override
Virtual method - must be overridden by concrete algorithm.
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
int version() const override
Algorithm's version for identification.
A class for holding :
Definition: EventList.h:56
void sortTof() const
Sort events by TOF in one thread.
Definition: EventList.cpp:947
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
std::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
std::shared_ptr< EventWorkspace > EventWorkspace_sptr
shared pointer to the EventWorkspace class
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
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