Mantid
Loading...
Searching...
No Matches
FilterBadPulses.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#include "MantidAPI/Run.h"
15#include "MantidKernel/V3D.h"
16
17namespace Mantid::Algorithms {
18// Register the algorithm into the algorithm factory
19DECLARE_ALGORITHM(FilterBadPulses)
20
21using namespace Kernel;
22using namespace API;
23using DataObjects::EventWorkspace;
26using std::size_t;
27
28namespace { // anonymous namespace for some internal variables
30const std::string INT_CHARGE_NAME("gd_prtn_chrg");
32const std::string LOG_CHARGE_NAME("proton_charge");
33} // namespace
34
36const std::string FilterBadPulses::name() const { return "FilterBadPulses"; }
37
39int FilterBadPulses::version() const { return 1; }
40
42const std::string FilterBadPulses::category() const { return "Events\\EventFiltering"; }
43
46 declareProperty(std::make_unique<WorkspaceProperty<EventWorkspace>>("InputWorkspace", "", Direction::Input),
47 "An event workspace");
48 declareProperty(std::make_unique<WorkspaceProperty<EventWorkspace>>("OutputWorkspace", "", Direction::Output),
49 "The name to use for the output workspace");
50 auto range = std::make_shared<BoundedValidator<double>>();
51 range->setBounds(0., 100.);
52 declareProperty("LowerCutoff", 95., range, "The percentage of the average to use as the lower bound");
53}
54
57 // the input workspace into the event workspace we already know it is
58 EventWorkspace_sptr inputWS = this->getProperty("InputWorkspace");
59
60 // get the run object
61 const API::Run &runlogs = inputWS->run();
62
63 // see if the gd_prtn_charge log has anything useful to say
64 if (runlogs.hasProperty(INT_CHARGE_NAME)) {
65 auto value = runlogs.getPropertyValueAsType<double>(INT_CHARGE_NAME);
66 if (value <= 0.) {
67 throw std::runtime_error("Found no integrated charge value in " + INT_CHARGE_NAME);
68 }
69 } else {
70 this->g_log.warning() << "Failed to find \"" << INT_CHARGE_NAME << "\" in run object.\n";
71 }
72
73 // get the proton charge exists in the run object
74 if (!runlogs.hasProperty(LOG_CHARGE_NAME)) {
75 throw std::runtime_error("Failed to find \"" + LOG_CHARGE_NAME + "\" in sample logs");
76 }
77 auto *pcharge_log = dynamic_cast<Kernel::TimeSeriesProperty<double> *>(runlogs.getLogData(LOG_CHARGE_NAME));
78 if (!pcharge_log) {
79 throw std::logic_error("Failed to find \"" + LOG_CHARGE_NAME + "\" in sample logs");
80 }
81 Kernel::TimeSeriesPropertyStatistics stats = pcharge_log->getStatistics();
82
83 // check that the maximum value is greater than zero
84 if (stats.maximum <= 0.) {
85 throw std::runtime_error("Maximum value of charge is not greater than zero (" + LOG_CHARGE_NAME + ")");
86 }
87
88 // set the range
89 double min_percent = this->getProperty("LowerCutoff");
90 min_percent *= .01; // convert it to a percentage (0<x<1)
91 double min_pcharge = stats.mean * min_percent;
92 double max_pcharge = stats.maximum * 1.1; // make sure everything high is in
93 if (min_pcharge >= max_pcharge) {
94 throw std::runtime_error("proton_charge window filters out all of the data");
95 }
96 this->g_log.information() << "Filtering pcharge outside of " << min_pcharge << " to " << max_pcharge << '\n';
97 size_t inputNumEvents = inputWS->getNumberEvents();
98
99 // Child Algorithme does all of the actual work - do not set the output
100 // workspace
101 auto filterAlgo = createChildAlgorithm("FilterByLogValue", 0., 1.);
102 filterAlgo->setProperty("InputWorkspace", inputWS);
103 filterAlgo->setProperty("LogName", "proton_charge");
104 filterAlgo->setProperty("MinimumValue", min_pcharge);
105 filterAlgo->setProperty("MaximumValue", max_pcharge);
106 filterAlgo->execute();
107
108 // just grab the child's output workspace
109 EventWorkspace_sptr outputWS = filterAlgo->getProperty("OutputWorkspace");
110 size_t outputNumEvents = outputWS->getNumberEvents();
111 this->setProperty("OutputWorkspace", outputWS);
112
113 // log the number of events deleted
114 double percent = static_cast<double>(inputNumEvents - outputNumEvents) / static_cast<double>(inputNumEvents);
115 percent *= 100.;
116 if (percent > 10.) {
117 this->g_log.warning() << "Deleted " << (inputNumEvents - outputNumEvents) << " of " << inputNumEvents << " events ("
118 << static_cast<int>(percent) << "%)\n";
119 } else {
120 this->g_log.notice() << "Deleted " << (inputNumEvents - outputNumEvents) << " of " << inputNumEvents << " events ("
121 << static_cast<float>(percent) << "%)"
122 << " by proton charge from " << min_pcharge << " to " << max_pcharge
123 << " with mean = " << stats.mean << "\n";
124 }
125}
126
127} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double value
The value of the point.
Definition: FitMW.cpp:51
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
bool hasProperty(const std::string &name) const
Does the property exist on the object.
Definition: LogManager.cpp:265
Kernel::Property * getLogData(const std::string &name) const
Access a single log entry.
Definition: LogManager.h:129
HeldType getPropertyValueAsType(const std::string &name) const
Get the value of a property as the given TYPE.
Definition: LogManager.cpp:332
This class stores information regarding an experimental run as a series of log entries.
Definition: Run.h:38
A property class for workspaces.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
void exec() override
Executes the algorithm.
const std::string category() const override
Algorithm's category for identification overriding a virtual method.
void init() override
Initialise the properties.
int version() const override
Algorithm's version for identification overriding a virtual method.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
Definition: Logger.cpp:95
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
A specialised Property class for holding a series of time-value pairs.
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
Struct holding some useful statistics for a TimeSeriesProperty.