Mantid
Loading...
Searching...
No Matches
FilterByTime2.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 +
12#include "MantidKernel/System.h"
13
14using namespace Mantid::Kernel;
15using namespace Mantid::API;
16
17namespace Mantid::Algorithms {
18
19//-----------------------------------------------------------------------
21 std::string commonHelp("\nYou can only specify the relative or absolute "
22 "start/stop times, not both.");
23
25 std::make_unique<WorkspaceProperty<DataObjects::EventWorkspace>>("InputWorkspace", "", Direction::Input),
26 "An input event workspace");
27
29 std::make_unique<WorkspaceProperty<DataObjects::EventWorkspace>>("OutputWorkspace", "", Direction::Output),
30 "The name to use for the output workspace");
31
32 auto min = std::make_shared<BoundedValidator<double>>();
33 min->setLower(0.0);
34 declareProperty("StartTime", 0.0, min,
35 "The start time, in seconds, since the start of the run. "
36 "Events before this time are filtered out. \nThe time of the "
37 "first pulse (i.e. the first entry in the ProtonCharge "
38 "sample log) is used as the zero. " +
39 commonHelp);
40
41 declareProperty("StopTime", 0.0,
42 "The stop time, in seconds, since the start of the run. "
43 "Events at or after this time are filtered out. \nThe time "
44 "of the first pulse (i.e. the first entry in the "
45 "ProtonCharge sample log) is used as the zero. " +
46 commonHelp);
47
48 std::string absoluteHelp("Specify date and UTC time in ISO8601 format, e.g. 2010-09-14T04:20:12." + commonHelp);
49 declareProperty("AbsoluteStartTime", "",
50 "Absolute start time; events before this time are filtered out. " + absoluteHelp);
51
52 declareProperty("AbsoluteStopTime", "",
53 "Absolute stop time; events at of after this time are filtered out. " + absoluteHelp);
54}
55
56//----------------------------------------------------------------------------------------------
60 DataObjects::EventWorkspace_const_sptr inWS = this->getProperty("InputWorkspace");
61 if (!inWS) {
62 g_log.error() << "Input is not EventWorkspace\n";
63 throw std::invalid_argument("Input is not EventWorksapce");
64 } else {
65 g_log.debug() << "DB5244 InputWorkspace Name = " << inWS->getName() << '\n';
66 }
67
68 double starttime = this->getProperty("StartTime");
69 double stoptime = this->getProperty("StopTime");
70 std::string absstarttime = this->getProperty("AbsoluteStartTime");
71 std::string absstoptime = this->getProperty("AbsoluteStopTime");
72
73 std::string start, stop;
74 if ((!absstarttime.empty()) && (!absstoptime.empty()) && (starttime <= 0.0) && (stoptime <= 0.0)) {
75 // Use the absolute string
76 start = absstarttime;
77 stop = absstoptime;
78 } else if ((!absstarttime.empty() || !absstoptime.empty()) && (starttime > 0.0 || stoptime > 0.0)) {
79 throw std::invalid_argument("It is not allowed to provide both absolute time and relative time.");
80 } else {
81 // Use second
82 std::stringstream ss;
83 ss << starttime;
84 start = ss.str();
85 std::stringstream ss2;
86 ss2 << stoptime;
87 stop = ss2.str();
88 }
89
90 // 1. Generate Filters
91 g_log.debug() << "\nDB441: About to generate Filter. StartTime = " << starttime << " StopTime = " << stoptime
92 << '\n';
93
94 API::Algorithm_sptr genfilter = createChildAlgorithm("GenerateEventsFilter", 0.0, 20.0, true, 1);
95 genfilter->initialize();
96 genfilter->setPropertyValue("InputWorkspace", inWS->getName());
97 genfilter->setPropertyValue("OutputWorkspace", "FilterWS");
98 genfilter->setProperty("StartTime", start);
99 genfilter->setProperty("StopTime", stop);
100 genfilter->setProperty("UnitOfTime", "Seconds");
101 genfilter->setProperty("FastLog", false);
102
103 bool sucgen = genfilter->execute();
104 if (!sucgen) {
105 g_log.error() << "Unable to generate event filters\n";
106 throw std::runtime_error("Unable to generate event filters");
107 } else {
108 g_log.debug() << "Filters are generated. \n";
109 }
110
111 API::Workspace_sptr filterWS = genfilter->getProperty("OutputWorkspace");
112 if (!filterWS) {
113 g_log.error() << "Unable to retrieve generated SplittersWorkspace object "
114 "from AnalysisDataService.\n";
115 throw std::runtime_error("Unable to retrieve Splittersworkspace. ");
116 }
117
118 // 2. Filter events
119 g_log.debug() << "\nAbout to filter events. "
120 << "\n";
121
122 API::Algorithm_sptr filter = createChildAlgorithm("FilterEvents", 20.0, 100.0, true, 1);
123 filter->initialize();
124 filter->setPropertyValue("InputWorkspace", inWS->getName());
125 filter->setPropertyValue("OutputWorkspaceBaseName", "ResultWS");
126 filter->setProperty("SplitterWorkspace", filterWS);
127 filter->setProperty("FilterByPulseTime", true);
128
129 bool sucfilt = filter->execute();
130 if (!sucfilt) {
131 g_log.error() << "Unable to filter events\n";
132 throw std::runtime_error("Unable to filter events");
133 } else {
134 g_log.debug() << "Filter events is successful. \n";
135 }
136
137 DataObjects::EventWorkspace_sptr optws = filter->getProperty("OutputWorkspace_0");
138
139 this->setProperty("OutputWorkspace", optws);
140}
141
142} // namespace Mantid::Algorithms
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
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
Kernel::Logger & g_log
Definition: Algorithm.h:451
A property class for workspaces.
void exec() override
Executes the algorithm.
void init() override
Sets documentation strings for this algorithm.
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 error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
Definition: Algorithm.h:61
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