Mantid
Loading...
Searching...
No Matches
MaskBinsIf.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 +
10#include "MantidAPI/Progress.h"
12#include "MantidHistogramData/HistogramIterator.h"
14
15#include <muParser.h>
16
17namespace {
18
29mu::Parser makeParser(double &y, double &e, double &x, double &dx, double &s, const std::string &criterion) {
30 mu::Parser muParser;
31 muParser.DefineVar("y", &y);
32 muParser.DefineVar("e", &e);
33 muParser.DefineVar("x", &x);
34 muParser.DefineVar("dx", &dx);
35 muParser.DefineVar("s", &s);
36 muParser.SetExpr(criterion);
37 return muParser;
38}
39} // namespace
40
41namespace Mantid::Algorithms {
42
43using namespace API;
44using namespace Kernel;
45
46// Register the algorithm into the AlgorithmFactory
47DECLARE_ALGORITHM(MaskBinsIf)
48
49//----------------------------------------------------------------------------------------------
52void MaskBinsIf::init() {
53 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
54 "An input workspace.");
55 declareProperty("Criterion", "",
56 "Masking criterion as a muparser expression; y: bin count, "
57 "e: bin error, x: bin center, dx: bin center error, s: "
58 "spectrum axis value.");
59 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
60 "An output workspace.");
61}
62
63//----------------------------------------------------------------------------------------------
66std::map<std::string, std::string> MaskBinsIf::validateInputs() {
67 std::map<std::string, std::string> issues;
68 const std::string criterion = getPropertyValue("Criterion");
69 if (criterion.empty()) {
70 issues["Criterion"] = "The criterion expression provided is empty";
71 } else {
72 double y = 0., e = 0., x = 0., dx = 0., s = 0.;
73 mu::Parser parser = makeParser(y, e, x, dx, s, criterion);
74 try {
75 parser.Eval();
76 } catch (mu::Parser::exception_type &exception) {
77 issues["Criterion"] = "Invalid expression given: " + exception.GetMsg();
78 }
79 }
80
81 return issues;
82}
83
84//----------------------------------------------------------------------------------------------
88 const std::string criterion = getPropertyValue("Criterion");
89 MatrixWorkspace_const_sptr inputWorkspace = getProperty("InputWorkspace");
90 MatrixWorkspace_sptr outputWorkspace = getProperty("OutputWorkspace");
91 if (inputWorkspace != outputWorkspace) {
92 outputWorkspace = inputWorkspace->clone();
93 }
94 const auto verticalAxis = outputWorkspace->getAxis(1);
95 const auto numericAxis = dynamic_cast<NumericAxis *>(verticalAxis);
96 const auto spectrumAxis = dynamic_cast<SpectraAxis *>(verticalAxis);
97 const bool spectrumOrNumeric = numericAxis || spectrumAxis;
98 if (!spectrumOrNumeric) {
99 throw std::runtime_error("Vertical axis must be NumericAxis or SpectraAxis");
100 }
101 const auto numberHistograms = static_cast<int64_t>(outputWorkspace->getNumberHistograms());
102 auto progress = std::make_unique<Progress>(this, 0., 1., numberHistograms);
104 for (int64_t index = 0; index < numberHistograms; ++index) {
106 double y, e, x, dx;
107 double s = spectrumOrNumeric ? verticalAxis->getValue(index) : 0.;
108 mu::Parser parser = makeParser(y, e, x, dx, s, criterion);
109 const auto &spectrum = outputWorkspace->histogram(index);
110 const bool hasDx = outputWorkspace->hasDx(index);
111 for (auto it = spectrum.begin(); it != spectrum.end(); ++it) {
112 const auto bin = std::distance(spectrum.begin(), it);
113 y = it->counts();
114 x = it->center();
115 e = it->countStandardDeviation();
116 dx = hasDx ? it->centerError() : 0.;
117 if (parser.Eval() != 0.) {
118 outputWorkspace->maskBin(index, bin);
119 }
120 }
121 progress->report();
123 }
125 setProperty("OutputWorkspace", outputWorkspace);
126}
127
128} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#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...
Definition: MultiThreaded.h:94
#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_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
Class to represent a numeric axis of a workspace.
Definition: NumericAxis.h:29
Class to represent the spectra axis of a workspace.
Definition: SpectraAxis.h:31
A property class for workspaces.
MaskBinsIf : Masks bins based on muparser expression.
Definition: MaskBinsIf.h:17
void exec() override
Execute the algorithm.
Definition: MaskBinsIf.cpp:87
std::map< std::string, std::string > validateInputs() override
Validate the inputs.
Definition: MaskBinsIf.cpp:66
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
Definition: MultiThreaded.h:22
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54