Mantid
Loading...
Searching...
No Matches
SmoothData.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 +
13
14namespace Mantid::Algorithms {
15
16// Register the algorithm into the AlgorithmFactory
17DECLARE_ALGORITHM(SmoothData)
18
19using namespace Kernel;
20using namespace API;
21using HistogramData::Histogram;
22
24 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
25 "Name of the input workspace");
26 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
27 "The name of the workspace to be created as the output of the algorithm");
28 std::vector<int> npts0{3};
29 auto min = std::make_shared<Kernel::ArrayBoundedValidator<int>>();
30 min->setLower(3);
31 // The number of points to use in the smoothing.
32 declareProperty(std::make_unique<ArrayProperty<int>>("NPoints", std::move(npts0), std::move(min), Direction::Input),
33 "The number of points to average over (minimum 3). If an even number is\n"
34 "given, it will be incremented by 1 to make it odd (default value 3)");
36 "GroupingWorkspace", "", Direction::Input, PropertyMode::Optional),
37 "Optional: GroupingWorkspace to use for vector of NPoints.");
38}
39
41 // Get the input properties
42 inputWorkspace = getProperty("InputWorkspace");
43
44 std::vector<int> nptsGroup = getProperty("NPoints");
45 Mantid::DataObjects::GroupingWorkspace_sptr groupWS = getProperty("GroupingWorkspace");
46 if (groupWS) {
47 udet2group.clear();
48 int64_t nGroups;
49 groupWS->makeDetectorIDToGroupVector(udet2group, nGroups);
50 }
51
52 // Check that the number of points in the smoothing isn't larger than the
53 // spectrum length
54 const auto vecSize = static_cast<int>(inputWorkspace->blocksize());
55
56 // Create the output workspace
58
59 Progress progress(this, 0.0, 1.0, inputWorkspace->getNumberHistograms());
61 for (int i = 0; i < static_cast<int>(inputWorkspace->getNumberHistograms()); ++i) {
63 int npts = nptsGroup[0];
64 if (groupWS) {
65 const int group = validateSpectrumInGroup(static_cast<size_t>(i));
66 if (group == -1) {
67 npts = 3;
68 } else {
69 assert(group != 0);
70 npts = nptsGroup[group - 1];
71 }
72 }
73 if (npts >= vecSize) {
74 g_log.error("The number of averaging points requested is larger than the "
75 "spectrum length");
76 throw std::out_of_range("The number of averaging points requested is "
77 "larger than the spectrum length");
78 }
79 // Number of smoothing points must always be an odd number, so add 1 if it
80 // isn't.
81 if (!(npts % 2)) {
82 g_log.information("Adding 1 to number of smoothing points, since it must "
83 "always be odd");
84 ++npts;
85 }
86
87 outputWorkspace->setHistogram(i, smooth(inputWorkspace->histogram(i), npts));
88
89 progress.report();
91 } // Loop over spectra
93
94 // Set the output workspace to its property
95 setProperty("OutputWorkspace", outputWorkspace);
96} // namespace Algorithms
97//=============================================================================
104 const auto &dets = inputWorkspace->getSpectrum(wi).getDetectorIDs();
105 if (dets.empty()) // Not in group
106 {
107 g_log.debug() << wi << " <- this workspace index is empty!\n";
108 return -1;
109 }
110
111 auto it = dets.cbegin();
112 if (*it < 0) // bad pixel id
113 return -1;
114
115 try { // what if index out of range?
116 const int group = udet2group.at(*it);
117 if (group <= 0)
118 return -1;
119 ++it;
120 for (; it != dets.end(); ++it) // Loop all other udets
121 {
122 if (udet2group.at(*it) != group)
123 return -1;
124 }
125 return group;
126 } catch (...) {
127 }
128
129 return -1;
130}
131
133Histogram smooth(const Histogram &histogram, unsigned int const npts) {
134 if (npts < 3)
135 return histogram;
136
137 Histogram smoothed(histogram);
138
139 auto const &Y = histogram.y().rawData();
140 auto const &E = histogram.e().rawData();
141 auto &newY = smoothed.mutableY();
142 auto &newE = smoothed.mutableE();
145 return smoothed;
146}
147
148} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
#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...
#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.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
std::vector< int > udet2group
Definition SmoothData.h:62
void init() override
Initialisation code.
API::MatrixWorkspace_const_sptr inputWorkspace
Definition SmoothData.h:63
int validateSpectrumInGroup(size_t wi)
Verify that all the contributing detectors to a spectrum belongs to the same group.
void exec() override
Execution code.
Support for a property that holds an array of values.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
HistogramData::Histogram smooth(const HistogramData::Histogram &histogram, unsigned int const npts)
std::shared_ptr< GroupingWorkspace > GroupingWorkspace_sptr
shared pointer to the GroupingWorkspace class
std::vector< T > boxcarErrorSmooth(std::vector< T > const &input, unsigned int const numPoints)
Performs boxcar (moving average) smoothing on the input data, using error propagation formula.
std::vector< T > boxcarSmooth(std::vector< T > const &input, unsigned int const numPoints)
Performs boxcar (moving average) smoothing on the input data.
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.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54