Mantid
Loading...
Searching...
No Matches
ResetNegatives.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 +
11#include "MantidKernel/System.h"
12
13using namespace Mantid::Kernel;
14using namespace Mantid::API;
15using std::size_t;
16
17namespace Mantid::Algorithms {
18
19// Register the algorithm into the AlgorithmFactory
20DECLARE_ALGORITHM(ResetNegatives)
21
22//----------------------------------------------------------------------------------------------
24const std::string ResetNegatives::name() const { return "ResetNegatives"; }
25
27int ResetNegatives::version() const { return 1; }
28
30const std::string ResetNegatives::category() const { return "CorrectionFunctions\\SpecialCorrections"; }
31
32//----------------------------------------------------------------------------------------------
35 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input), "An input workspace.");
36 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
37 "An output workspace.");
38 declareProperty("AddMinimum", true, "Add the minimum value of the spectrum to bring it up to zero.");
39 declareProperty("ResetValue", 0., "Reset negative values to this number (default=0)");
40 setPropertySettings("ResetValue", std::make_unique<EnabledWhenProperty>("AddMinimum", IS_NOT_DEFAULT));
41}
42
43//----------------------------------------------------------------------------------------------
46 MatrixWorkspace_sptr inputWS = this->getProperty("InputWorkspace");
47 MatrixWorkspace_sptr outputWS = this->getProperty("OutputWorkspace");
48
49 // get the minimum for each spectrum
50 auto alg = createChildAlgorithm("Min", 0., .1);
51 alg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputWS);
52 alg->executeAsChildAlg();
53 MatrixWorkspace_const_sptr minWS = alg->getProperty("OutputWorkspace");
54
55 // determine if there is anything to do
56 auto nHist = static_cast<int64_t>(minWS->getNumberHistograms());
57 bool hasNegative = false;
58 for (int64_t i = 0; i < nHist; i++) {
59 if (minWS->y(i)[0] < 0.0) {
60 hasNegative = true;
61 break;
62 }
63 }
64
65 // get out early if there is nothing to do
66 if (!hasNegative) {
67 g_log.information() << "No values are negative. Copying InputWorkspace to "
68 "OutputWorkspace\n";
69 if (inputWS != outputWS) {
70 auto clone = createChildAlgorithm("CloneWorkspace", .1, 1.);
71 clone->setProperty<Workspace_sptr>("InputWorkspace", inputWS);
72 clone->executeAsChildAlg();
73
74 Workspace_sptr temp = clone->getProperty("OutputWorkspace");
75 setProperty("OutputWorkspace", std::dynamic_pointer_cast<MatrixWorkspace>(temp));
76 }
77 return;
78 }
79
80 // sort the event list to make it fast and thread safe
82 std::dynamic_pointer_cast<const DataObjects::EventWorkspace>(inputWS);
83 if (eventWS)
84 eventWS->sortAll(DataObjects::TOF_SORT, nullptr);
85
86 Progress prog(this, 0.1, 1.0, 2 * nHist);
87
88 // generate output workspace - copy X and dY
89 outputWS = API::WorkspaceFactory::Instance().create(inputWS);
90 PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *outputWS))
91 for (int64_t i = 0; i < nHist; i++) {
93 const auto index = static_cast<size_t>(i);
94 outputWS->setHistogram(index, inputWS->histogram(index));
95 prog.report();
97 }
99
100 // do the actual work
101 if (this->getProperty("AddMinimum")) {
102 this->pushMinimum(minWS, outputWS, prog);
103 } else {
104 this->changeNegatives(minWS, this->getProperty("ResetValue"), outputWS, prog);
105 }
106
107 setProperty("OutputWorkspace", outputWS);
108}
109
110namespace { // anonymous namespace
114inline double fixZero(const double value) { return value != -0. ? value : 0.; }
115} // namespace
116
127 Progress &prog) {
128 int64_t nHist = minWS->getNumberHistograms();
129 PARALLEL_FOR_IF(Kernel::threadSafe(*wksp, *minWS))
130 for (int64_t i = 0; i < nHist; i++) {
132 double minValue = minWS->y(i)[0];
133 if (minValue <= 0) {
134 minValue *= -1.;
135 auto &y = wksp->mutableY(i);
136 std::transform(y.begin(), y.end(), y.begin(), [minValue](double value) { return fixZero(value + minValue); });
137 }
138 prog.report();
140 }
142}
143
154void ResetNegatives::changeNegatives(const MatrixWorkspace_const_sptr &minWS, const double spectrumNegativeValues,
155 const MatrixWorkspace_sptr &wksp, Progress &prog) {
156 int64_t nHist = wksp->getNumberHistograms();
157 PARALLEL_FOR_IF(Kernel::threadSafe(*minWS, *wksp))
158 for (int64_t i = 0; i < nHist; i++) {
160 if (minWS->y(i)[0] <= 0.) // quick check to see if there is a reason to bother
161 {
162 auto &y = wksp->mutableY(i);
163 for (double &value : y) {
164 if (value < 0.) {
165 value = spectrumNegativeValues;
166 } else
167 value = fixZero(value);
168 }
169 }
170 prog.report();
172 }
174}
175
176} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double minValue
double value
The value of the point.
Definition: FitMW.cpp:51
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.
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
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
ResetNegatives : Reset negative values to something else.
void changeNegatives(const API::MatrixWorkspace_const_sptr &minWS, const double spectrumNegativeValues, const API::MatrixWorkspace_sptr &wksp, API::Progress &prog)
Search each spectrum for y-values that are less than zero and reset them to the supplied value.
int version() const override
function to return a version of the algorithm, must be overridden in all algorithms
void pushMinimum(const API::MatrixWorkspace_const_sptr &minWS, const API::MatrixWorkspace_sptr &wksp, API::Progress &prog)
Add -1.
void init() override
Virtual method - must be overridden by concrete algorithm.
const std::string category() const override
function to return a category of the algorithm.
void exec() override
Virtual method - must be overridden by concrete algorithm.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertySettings(const std::string &name, std::unique_ptr< IPropertySettings > settings)
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
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::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
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
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54