Mantid
Loading...
Searching...
No Matches
MultiplyRange.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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
15#include "MantidHistogramData/Histogram.h"
17
18namespace Mantid::Algorithms {
19
20// Algorithm must be declared
21DECLARE_ALGORITHM(MultiplyRange)
22
23using namespace Kernel;
24using namespace API;
25using namespace DataObjects;
26using namespace HistogramData;
27
29 // Declare an input workspace property.
30 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
31 "The name of the input workspace.");
32 // Declare an output workspace property.
33 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
34 "The name of the output workspace.");
35
36 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
37 mustBePositive->setLower(0);
38 // StartBin
39 declareProperty("StartBin", 0, mustBePositive, "Bin index to start from");
40 // EndBin
41 declareProperty("EndBin", EMPTY_INT(), mustBePositive, "Bin index to finish at");
42 // factor
43 declareProperty("Factor", 0.0, "The value by which to multiply the input data range");
44}
45
49 // Get the input workspace and other properties
50 MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
51 int startBin = getProperty("StartBin");
52 int endBin = getProperty("EndBin");
53 double factor = getProperty("Factor");
54
55 // A few checks on the input properties
56 const auto specSize = static_cast<int>(inputWS->blocksize());
57 if (isEmpty(endBin))
58 endBin = specSize - 1;
59
60 if (endBin >= specSize) {
61 g_log.error("EndBin out of range!");
62 throw std::out_of_range("EndBin out of range!");
63 }
64 if (endBin < startBin) {
65 g_log.error("StartBin must be less than or equal to EndBin");
66 throw std::out_of_range("StartBin must be less than or equal to EndBin");
67 }
68
69 // Only create the output workspace if it's different to the input one
70 MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
71 if (outputWS != inputWS) {
72 outputWS = create<MatrixWorkspace>(*inputWS);
73 setProperty("OutputWorkspace", outputWS);
74 }
75
76 // Get the count of histograms in the input workspace
77 const auto histogramCount = static_cast<int>(inputWS->getNumberHistograms());
78 Progress progress(this, 0.0, 1.0, histogramCount);
79 // Loop over spectra
80 PARALLEL_FOR_IF(Kernel::threadSafe(*inputWS, *outputWS))
81 for (int i = 0; i < histogramCount; ++i) {
83 outputWS->setHistogram(i, inputWS->histogram(i));
84 auto &newY = outputWS->mutableY(i);
85 auto &newE = outputWS->mutableE(i);
86
87 // Now multiply the requested range
88 using std::placeholders::_1;
89 std::transform(newY.begin() + startBin, newY.begin() + endBin + 1, newY.begin() + startBin,
90 std::bind(std::multiplies<double>(), _1, factor));
91 std::transform(newE.begin() + startBin, newE.begin() + endBin + 1, newE.begin() + startBin,
92 std::bind(std::multiplies<double>(), _1, factor));
93
94 progress.report();
96 }
98}
99
100} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
#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
Kernel::Logger & g_log
Definition: Algorithm.h:451
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
static bool isEmpty(const NumT toCheck)
checks that the value was not set by users, uses the value in empty double/int.
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void init() override
Initialisation code.
void exec() override
Execution code.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
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
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54