Mantid
Loading...
Searching...
No Matches
MaskBins.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 +
8#include "MantidAPI/Algorithm.tcc"
12
13#include <limits>
14#include <sstream>
15
16using Mantid::HistogramData::BinEdges;
17
18namespace Mantid::Algorithms {
19
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(MaskBins)
22
23using namespace Kernel;
24using namespace API;
25using DataObjects::EventWorkspace;
28
30 declareWorkspaceInputProperties<MatrixWorkspace>("InputWorkspace",
31 "The name of the input workspace. Must contain histogram data.",
32 std::make_shared<HistogramValidator>());
33 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
34 "The name of the Workspace containing the masked bins.");
35
36 // This validator effectively makes these properties mandatory
37 // Would be nice to have an explicit validator for this, but
38 // MandatoryValidator is already taken!
39 auto required = std::make_shared<BoundedValidator<double>>();
40 required->setUpper(std::numeric_limits<double>::max() * 0.99);
41 declareProperty("XMin", std::numeric_limits<double>::max(), required, "The value to start masking from.");
42 declareProperty("XMax", std::numeric_limits<double>::max(), required, "The value to end masking at.");
43
44 this->declareProperty(std::make_unique<ArrayProperty<int64_t>>("SpectraList"),
45 "Deprecated, use InputWorkspaceIndexSet.");
46}
47
52 // Check for valid X limits
53 m_startX = getProperty("XMin");
54 m_endX = getProperty("XMax");
55
56 if (m_startX > m_endX) {
57 std::stringstream msg;
58 msg << "XMax (" << m_endX << ") must be greater than XMin (" << m_startX << ")";
59 g_log.error(msg.str());
60 throw std::invalid_argument(msg.str());
61 }
62
63 // Copy indices from legacy property
64 std::vector<int64_t> spectraList = this->getProperty("SpectraList");
65 if (!spectraList.empty()) {
66 if (!isDefault("InputWorkspaceIndexSet"))
67 throw std::runtime_error("Cannot provide both InputWorkspaceIndexSet and "
68 "SpectraList at the same time.");
69 setProperty("InputWorkspaceIndexSet", spectraList);
70 g_log.warning("The 'SpectraList' property is deprecated. Use "
71 "'InputWorkspaceIndexSet' instead.");
72 }
73
75 std::tie(inputWS, indexSet) = getWorkspaceAndIndices<MatrixWorkspace>("InputWorkspace");
76
77 // Only create the output workspace if it's different to the input one
78 MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
79 if (outputWS != inputWS) {
80 outputWS = inputWS->clone();
81 setProperty("OutputWorkspace", outputWS);
82 }
83
84 if (std::dynamic_pointer_cast<const EventWorkspace>(inputWS)) {
85 this->execEvent();
86 } else {
87 MantidVec::difference_type startBin(0), endBin(0);
88
89 // If the binning is the same throughout, we only need to find the index
90 // limits once
91 const bool commonBins = inputWS->isCommonBins();
92 if (commonBins) {
93 auto X = inputWS->binEdges(0);
94 this->findIndices(X, startBin, endBin);
95 }
96
97 Progress progress(this, 0.0, 1.0, indexSet.size());
98 // Parallel running has problems with a race condition, leading to
99 // occaisional test failures and crashes
100
101 for (const auto wi : indexSet) {
102 MantidVec::difference_type startBinLoop(startBin), endBinLoop(endBin);
103 if (!commonBins)
104 this->findIndices(outputWS->binEdges(wi), startBinLoop, endBinLoop);
105
106 // Loop over masking each bin in the range
107 for (auto j = static_cast<int>(startBinLoop); j < static_cast<int>(endBinLoop); ++j) {
108 outputWS->maskBin(wi, j);
109 }
110 progress.report();
111 }
112 }
113}
114
118 MatrixWorkspace_sptr outputMatrixWS = getProperty("OutputWorkspace");
119 auto outputWS = std::dynamic_pointer_cast<EventWorkspace>(outputMatrixWS);
120
121 Progress progress(this, 0.0, 1.0, outputWS->getNumberHistograms() * 2);
122
123 outputWS->sortAll(Mantid::DataObjects::TOF_SORT, &progress);
124
126 for (int i = 0; i < static_cast<int>(indexSet.size()); // NOLINT
127 ++i) {
129 outputWS->getSpectrum(indexSet[i]).maskTof(m_startX, m_endX);
130 progress.report();
132 }
134
135 outputWS->clearMRU();
136}
137
143void MaskBins::findIndices(const BinEdges &X, MantidVec::difference_type &startBin,
144 MantidVec::difference_type &endBin) {
145 startBin = std::distance(X.begin(), std::upper_bound(X.cbegin(), X.cend(), m_startX));
146 if (startBin != 0)
147 --startBin;
148 auto last = std::lower_bound(X.cbegin(), X.cend(), m_endX);
149 if (last == X.cend())
150 --last;
151 endBin = std::distance(X.cbegin(), last);
152}
153
154} // 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
bool isDefault(const std::string &name) const
Definition: Algorithm.cpp:2084
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
double m_endX
The range end point.
Definition: MaskBins.h:67
double m_startX
The range start point.
Definition: MaskBins.h:66
void execEvent()
Execution code for EventWorkspaces.
Definition: MaskBins.cpp:117
Indexing::SpectrumIndexSet indexSet
the list of Spectra (workspace index) to load
Definition: MaskBins.h:68
void exec() override
Execution code.
Definition: MaskBins.cpp:51
void init() override
Initialisation code.
Definition: MaskBins.cpp:29
void findIndices(const HistogramData::BinEdges &X, MantidVec::difference_type &startBin, MantidVec::difference_type &endBin)
Finds the indices of the bins at the limits of the range given.
Definition: MaskBins.cpp:143
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
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
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
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::shared_ptr< EventWorkspace > EventWorkspace_sptr
shared pointer to the EventWorkspace 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
@ Output
An output workspace.
Definition: Property.h:54