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 const auto [min_pcharge, max_pcharge, mean] = runlogs.getBadPulseRange(LOG_CHARGE_NAME, getProperty("LowerCutoff"));
74
75 this->g_log.information() << "Filtering pcharge outside of " << min_pcharge << " to " << max_pcharge << '\n';
76 size_t inputNumEvents = inputWS->getNumberEvents();
77
78 // Child Algorithme does all of the actual work - do not set the output
79 // workspace
80 auto filterAlgo = createChildAlgorithm("FilterByLogValue", 0., 1.);
81 filterAlgo->setProperty("InputWorkspace", inputWS);
82 filterAlgo->setProperty("LogName", LOG_CHARGE_NAME);
83 filterAlgo->setProperty("MinimumValue", min_pcharge);
84 filterAlgo->setProperty("MaximumValue", max_pcharge);
85 filterAlgo->execute();
86
87 // just grab the child's output workspace
88 EventWorkspace_sptr outputWS = filterAlgo->getProperty("OutputWorkspace");
89 size_t outputNumEvents = outputWS->getNumberEvents();
90 this->setProperty("OutputWorkspace", outputWS);
91
92 // log the number of events deleted
93 double percent = static_cast<double>(inputNumEvents - outputNumEvents) / static_cast<double>(inputNumEvents);
94 percent *= 100.;
95 if (percent > 10.) {
96 this->g_log.warning() << "Deleted " << (inputNumEvents - outputNumEvents) << " of " << inputNumEvents << " events ("
97 << static_cast<int>(percent) << "%)\n";
98 } else {
99 this->g_log.notice() << "Deleted " << (inputNumEvents - outputNumEvents) << " of " << inputNumEvents << " events ("
100 << static_cast<float>(percent) << "%)"
101 << " by proton charge from " << min_pcharge << " to " << max_pcharge << " with mean = " << mean
102 << "\n";
103 }
104}
105
106} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
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.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
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.
Kernel::Logger & g_log
Definition Algorithm.h:422
bool hasProperty(const std::string &name) const
Does the property exist on the object.
HeldType getPropertyValueAsType(const std::string &name) const
Get the value of a property as the given TYPE.
This class stores information regarding an experimental run as a series of log entries.
Definition Run.h:35
std::tuple< double, double, double > getBadPulseRange(const std::string &logname="proton_charge", const double &cutoff=95.) const
determine the range of bad pulses to filter
Definition Run.cpp:398
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:126
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
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