Mantid
Loading...
Searching...
No Matches
FilterByTime.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 +
8#include "MantidAPI/Run.h"
15
16namespace Mantid::Algorithms {
17// Register the algorithm into the algorithm factory
18DECLARE_ALGORITHM(FilterByTime)
19
20using namespace Kernel;
21using namespace API;
22using DataObjects::EventList;
23using DataObjects::EventWorkspace;
26using Types::Core::DateAndTime;
27
29 std::string commonHelp("\nYou can only specify the relative or absolute "
30 "start/stop times, not both.");
31
32 declareProperty(std::make_unique<WorkspaceProperty<EventWorkspace>>("InputWorkspace", "", Direction::Input),
33 "An input event workspace");
34
35 declareProperty(std::make_unique<WorkspaceProperty<EventWorkspace>>("OutputWorkspace", "", Direction::Output),
36 "The name to use for the output workspace");
37
38 auto min = std::make_shared<BoundedValidator<double>>();
39 min->setLower(0.0);
40 declareProperty("StartTime", 0.0, min,
41 "The start time, in seconds, since the start of the run. "
42 "Events before this time are filtered out. \nThe time of the "
43 "first pulse (i.e. the first entry in the ProtonCharge "
44 "sample log) is used as the zero. " +
45 commonHelp);
46
47 declareProperty("StopTime", 0.0, min,
48 "The stop time, in seconds, since the start of the run. "
49 "Events at or after this time are filtered out. \nThe time "
50 "of the first pulse (i.e. the first entry in the "
51 "ProtonCharge sample log) is used as the zero. " +
52 commonHelp);
53
54 std::string absoluteHelp("Specify date and UTC time in ISO8601 format, e.g. 2010-09-14T04:20:12." + commonHelp);
55 declareProperty("AbsoluteStartTime", "",
56 "Absolute start time; events before this time are filtered out. " + absoluteHelp);
57
58 declareProperty("AbsoluteStopTime", "",
59 "Absolute stop time; events at of after this time are filtered out. " + absoluteHelp);
60}
61
65 EventWorkspace_const_sptr inputWS = this->getProperty("InputWorkspace");
66
67 // ---- Find the start/end times ----
68 DateAndTime start, stop;
69
70 double start_dbl, stop_dbl;
71 start_dbl = getProperty("StartTime");
72 stop_dbl = getProperty("StopTime");
73
74 std::string start_str, stop_str;
75 start_str = getPropertyValue("AbsoluteStartTime");
76 stop_str = getPropertyValue("AbsoluteStopTime");
77
78 if ((start_str != "") && (stop_str != "") && (start_dbl <= 0.0) && (stop_dbl <= 0.0)) {
79 // Use the absolute string
80 start = DateAndTime(start_str);
81 stop = DateAndTime(stop_str);
82 } else if ((start_str == "") && (stop_str == "") && ((start_dbl > 0.0) || (stop_dbl > 0.0))) {
83 // Use the relative times in seconds.
84 DateAndTime first = inputWS->getFirstPulseTime();
85 DateAndTime last = inputWS->getLastPulseTime();
86 start = first + start_dbl;
87 if (stop_dbl > 0.0) {
88 stop = first + stop_dbl;
89 } else {
90 this->getLogger().debug() << "No end filter time specified - assuming last pulse\n";
91 stop = last + 10000.0; // so we get all events - needs to be past last pulse
92 }
93 } else {
94 // Either both or none were specified
95 throw std::invalid_argument("You need to specify either the StartTime or "
96 "StopTime parameters; or both the "
97 "AbsoluteStartTime and AbsoluteStopTime "
98 "parameters; but not other combinations.");
99 }
100
101 if (stop <= start)
102 throw std::invalid_argument("The stop time should be larger than the start time.");
103
104 auto outputWS = DataObjects::create<EventWorkspace>(*inputWS);
105
106 size_t numberOfSpectra = inputWS->getNumberHistograms();
107
108 // Initialise the progress reporting object
109 Progress prog(this, 0.0, 1.0, numberOfSpectra);
110
111 // Loop over the histograms (detector spectra)
113 for (int64_t i = 0; i < int64_t(numberOfSpectra); ++i) {
115
116 // Get the output event list (should be empty)
117 EventList &output_el = outputWS->getSpectrum(i);
118 // and this is the input event list
119 const EventList &input_el = inputWS->getSpectrum(i);
120
121 // Perform the filtering
122 input_el.filterByPulseTime(start, stop, output_el);
123
124 prog.report();
126 }
128
129 // Now filter out the run, using the DateAndTime type.
130 outputWS->mutableRun().filterByTime(start, stop);
131 setProperty("OutputWorkspace", std::move(outputWS));
132}
133
134} // 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_FOR_NO_WSP_CHECK()
#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_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
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & getLogger() const
Returns a reference to the logger.
Definition: Algorithm.cpp:1660
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void exec() override
Executes the algorithm.
void init() override
Virtual method - must be overridden by concrete algorithm.
A class for holding :
Definition: EventList.h:56
void filterByPulseTime(Types::Core::DateAndTime start, Types::Core::DateAndTime stop, EventList &output) const
Filter this EventList into an output EventList, using keeping only events within the >= start and < e...
Definition: EventList.cpp:3709
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
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
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54