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"
16
17namespace Mantid::Algorithms {
18// Register the algorithm into the algorithm factory
19DECLARE_ALGORITHM(FilterByTime)
20
21using namespace Kernel;
22using namespace API;
23using DataObjects::EventList;
24using DataObjects::EventWorkspace;
27using Types::Core::DateAndTime;
28
29namespace {
30namespace PropertyNames {
31const std::string INPUT_WKSP("InputWorkspace");
32const std::string OUTPUT_WKSP("OutputWorkspace");
33const std::string START_TIME("StartTime");
34const std::string STOP_TIME("StopTime");
35const std::string ABS_START("AbsoluteStartTime");
36const std::string ABS_STOP("AbsoluteStopTime");
37} // namespace PropertyNames
38} // namespace
39
41 std::string commonHelp("\nYou can only specify the relative or absolute "
42 "start/stop times, not both.");
43
45 "An input event workspace");
46
49 "The name to use for the output workspace");
50
51 auto min = std::make_shared<BoundedValidator<double>>();
52 min->setLower(0.0);
53 declareProperty(PropertyNames::START_TIME, 0.0, min,
54 "The start time since the start of the run. "
55 "Use an integer value for time in nanoseconds. "
56 "Use a floating-point value for time in seconds. "
57 "Events before this time are filtered out. \nThe time of the "
58 "first pulse (i.e. the first entry in the \"proton_charge\" "
59 "sample log) is used as the zero. " +
60 commonHelp);
61
62 declareProperty(PropertyNames::STOP_TIME, 0.0, min,
63 "The stop time since the start of the run. "
64 "Use an integer value for time in nanoseconds. "
65 "Use a floating-point value for time in seconds. "
66 "Events at or after this time are filtered out. \nThe AbsoluteStartTime "
67 "or the time of the first pulse (i.e. the first entry in the "
68 "\"proton_charge\" sample log) is used as the zero. " +
69 commonHelp);
70
71 auto dateTime = std::make_shared<DateTimeValidator>();
72 dateTime->allowEmpty(true);
73 std::string absoluteHelp("Specify date and UTC time in ISO8601 format, e.g. 2010-09-14T04:20:12." + commonHelp);
74 declareProperty(PropertyNames::ABS_START, "", dateTime,
75 "Absolute start time; events before this time are filtered out. " + absoluteHelp);
76
77 declareProperty(PropertyNames::ABS_STOP, "", dateTime,
78 "Absolute stop time; events at of after this time are filtered out. " + absoluteHelp);
79}
80
81std::map<std::string, std::string> FilterByTime::validateInputs() {
82 std::map<std::string, std::string> errors;
83
84 const std::string msg_double_spec("You need to specify either the relative or absolute parameter, but not both");
85
86 if ((!isDefault(PropertyNames::START_TIME)) && (!isDefault(PropertyNames::ABS_START))) {
87 errors[PropertyNames::START_TIME] = msg_double_spec;
88 errors[PropertyNames::ABS_START] = msg_double_spec;
89 }
90
91 if ((!isDefault(PropertyNames::STOP_TIME)) && (!isDefault(PropertyNames::ABS_STOP))) {
92 errors[PropertyNames::STOP_TIME] = msg_double_spec;
93 errors[PropertyNames::ABS_STOP] = msg_double_spec;
94 }
95
96 return errors;
97}
98
103
104 // find the start time
105 DateAndTime start;
106 const double startRelative = getProperty(PropertyNames::START_TIME);
107 if (!isDefault(PropertyNames::ABS_START)) {
108 // absolute start time is specified
109 start = DateAndTime(getPropertyValue(PropertyNames::ABS_START));
110 } else {
111 // get run start from the log manager
112 const auto startOfRun = inputWS->run().getFirstPulseTime();
113 start = startOfRun + startRelative;
114 }
115
116 // find the stop time
117 DateAndTime stop;
118 if (!isDefault(PropertyNames::ABS_STOP)) {
119 // absolute time
120 stop = DateAndTime(getPropertyValue(PropertyNames::ABS_STOP));
121 } else if (!isDefault(PropertyNames::STOP_TIME)) {
122 // stop time is relative to start time
123 const double stopRelative = getProperty(PropertyNames::STOP_TIME);
124 stop = start - startRelative + stopRelative;
125 } else {
126 this->getLogger().debug("No end filter time specified - assuming last pulse");
127 stop = inputWS->run().getLastPulseTime();
128 stop += 10000.0; // so we get all events - needs to be past last pulse
129 }
130
131 // verify that stop is after start
132 if (stop <= start) {
133 std::stringstream msg;
134 msg << "The stop time (" << stop << ") should be larger than the start time (" << start << ")";
135 throw std::invalid_argument(msg.str());
136 }
137
138 auto outputWS = DataObjects::create<EventWorkspace>(*inputWS);
139
140 size_t numberOfSpectra = inputWS->getNumberHistograms();
141
142 // Initialise the progress reporting object
143 Progress prog(this, 0.0, 1.0, numberOfSpectra);
144
145 // Loop over the histograms (detector spectra)
147 for (int64_t i = 0; i < int64_t(numberOfSpectra); ++i) {
149
150 // Get the output event list (should be empty)
151 EventList &output_el = outputWS->getSpectrum(i);
152 // and this is the input event list
153 const EventList &input_el = inputWS->getSpectrum(i);
154
155 // Perform the filtering
156 input_el.filterByPulseTime(start, stop, output_el);
157
158 prog.report();
160 }
162
163 // Now filter out the run, using the DateAndTime type.
164 auto timeroi = outputWS->mutableRun().getTimeROI(); // make a copy
165 if (timeroi.useAll()) {
166 // trim in for where it should be used
167 timeroi.addROI(start, stop);
168 } else {
169 // only use the overlap region
170 timeroi.update_intersection(TimeROI(start, stop));
171 }
172 outputWS->mutableRun().setTimeROI(timeroi);
173 outputWS->mutableRun().removeDataOutsideTimeROI();
174 setProperty(PropertyNames::OUTPUT_WKSP, std::move(outputWS));
175}
176
177} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
#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...
#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.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
bool isDefault(const std::string &name) const
Kernel::Logger & getLogger() const
Returns a reference to the logger.
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
void exec() override
Executes the algorithm.
void init() override
Virtual method - must be overridden by concrete algorithm.
A class for holding :
Definition EventList.h:57
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...
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:145
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
TimeROI : Object that holds information about when the time measurement was active.
Definition TimeROI.h:18
std::string const OUTPUT_WKSP("OutputWorkspace")
std::string const INPUT_WKSP("InputWorkspace")
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
const std::string OUTPUT_WKSP("OutputWorkspace")
const std::string INPUT_WKSP("InputWorkspace")
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54