-
Notifications
You must be signed in to change notification settings - Fork 122
/
FilterByTime.cpp
134 lines (110 loc) · 5.21 KB
/
FilterByTime.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 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 +
#include "MantidAlgorithms/FilterByTime.h"
#include "MantidAPI/Run.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/UnitFactory.h"
namespace Mantid::Algorithms {
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(FilterByTime)
using namespace Kernel;
using namespace API;
using DataObjects::EventList;
using DataObjects::EventWorkspace;
using DataObjects::EventWorkspace_const_sptr;
using DataObjects::EventWorkspace_sptr;
using Types::Core::DateAndTime;
void FilterByTime::init() {
std::string commonHelp("\nYou can only specify the relative or absolute "
"start/stop times, not both.");
declareProperty(std::make_unique<WorkspaceProperty<EventWorkspace>>("InputWorkspace", "", Direction::Input),
"An input event workspace");
declareProperty(std::make_unique<WorkspaceProperty<EventWorkspace>>("OutputWorkspace", "", Direction::Output),
"The name to use for the output workspace");
auto min = std::make_shared<BoundedValidator<double>>();
min->setLower(0.0);
declareProperty("StartTime", 0.0, min,
"The start time, in seconds, since the start of the run. "
"Events before this time are filtered out. \nThe time of the "
"first pulse (i.e. the first entry in the ProtonCharge "
"sample log) is used as the zero. " +
commonHelp);
declareProperty("StopTime", 0.0, min,
"The stop time, in seconds, since the start of the run. "
"Events at or after this time are filtered out. \nThe time "
"of the first pulse (i.e. the first entry in the "
"ProtonCharge sample log) is used as the zero. " +
commonHelp);
std::string absoluteHelp("Specify date and UTC time in ISO8601 format, e.g. 2010-09-14T04:20:12." + commonHelp);
declareProperty("AbsoluteStartTime", "",
"Absolute start time; events before this time are filtered out. " + absoluteHelp);
declareProperty("AbsoluteStopTime", "",
"Absolute stop time; events at of after this time are filtered out. " + absoluteHelp);
}
/** Executes the algorithm
*/
void FilterByTime::exec() {
EventWorkspace_const_sptr inputWS = this->getProperty("InputWorkspace");
// ---- Find the start/end times ----
DateAndTime start, stop;
double start_dbl, stop_dbl;
start_dbl = getProperty("StartTime");
stop_dbl = getProperty("StopTime");
std::string start_str, stop_str;
start_str = getPropertyValue("AbsoluteStartTime");
stop_str = getPropertyValue("AbsoluteStopTime");
if ((start_str != "") && (stop_str != "") && (start_dbl <= 0.0) && (stop_dbl <= 0.0)) {
// Use the absolute string
start = DateAndTime(start_str);
stop = DateAndTime(stop_str);
} else if ((start_str == "") && (stop_str == "") && ((start_dbl > 0.0) || (stop_dbl > 0.0))) {
// Use the relative times in seconds.
DateAndTime first = inputWS->getFirstPulseTime();
DateAndTime last = inputWS->getLastPulseTime();
start = first + start_dbl;
if (stop_dbl > 0.0) {
stop = first + stop_dbl;
} else {
this->getLogger().debug() << "No end filter time specified - assuming last pulse\n";
stop = last + 10000.0; // so we get all events - needs to be past last pulse
}
} else {
// Either both or none were specified
throw std::invalid_argument("You need to specify either the StartTime or "
"StopTime parameters; or both the "
"AbsoluteStartTime and AbsoluteStopTime "
"parameters; but not other combinations.");
}
if (stop <= start)
throw std::invalid_argument("The stop time should be larger than the start time.");
auto outputWS = DataObjects::create<EventWorkspace>(*inputWS);
size_t numberOfSpectra = inputWS->getNumberHistograms();
// Initialise the progress reporting object
Progress prog(this, 0.0, 1.0, numberOfSpectra);
// Loop over the histograms (detector spectra)
PARALLEL_FOR_NO_WSP_CHECK()
for (int64_t i = 0; i < int64_t(numberOfSpectra); ++i) {
PARALLEL_START_INTERRUPT_REGION
// Get the output event list (should be empty)
EventList &output_el = outputWS->getSpectrum(i);
// and this is the input event list
const EventList &input_el = inputWS->getSpectrum(i);
// Perform the filtering
input_el.filterByPulseTime(start, stop, output_el);
prog.report();
PARALLEL_END_INTERRUPT_REGION
}
PARALLEL_CHECK_INTERRUPT_REGION
// Now filter out the run, using the DateAndTime type.
outputWS->mutableRun().filterByTime(start, stop);
setProperty("OutputWorkspace", std::move(outputWS));
}
} // namespace Mantid::Algorithms