Mantid
Loading...
Searching...
No Matches
ExtractSpectra.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 +
9
12#include "MantidAPI/TextAxis.h"
14#include "MantidIndexing/Extract.h"
15#include "MantidIndexing/IndexInfo.h"
18
19#include <algorithm>
20
21namespace {
23const double xBoundaryTolerance = 1.0e-15;
24} // namespace
25
26namespace Mantid::Algorithms {
27
28using namespace Kernel;
29using namespace API;
30using namespace DataObjects;
31using namespace HistogramData;
32using Types::Event::TofEvent;
33
34// Register the algorithm into the AlgorithmFactory
35DECLARE_ALGORITHM(ExtractSpectra)
36
37
38const std::string ExtractSpectra::name() const { return "ExtractSpectra"; }
39
41int ExtractSpectra::version() const { return 1; }
42
44const std::string ExtractSpectra::category() const { return "Transforms\\Splitting"; }
45
47const std::string ExtractSpectra::summary() const {
48 return "Extracts a list of spectra from a workspace and places them in a new "
49 "workspace.";
50}
51
53std::map<std::string, std::string> ExtractSpectra::validateInputs() {
54 std::map<std::string, std::string> helpMessages;
55 if (!isDefault("XMin") && !isDefault("XMax")) {
56 const double xmin = getProperty("XMin");
57 const double xmax = getProperty("XMax");
58 if (xmin > xmax) {
59 helpMessages["XMin"] = "XMin must be less than XMax";
60 helpMessages["XMax"] = "XMax must be greater than XMin";
61 }
62 }
63
64 if (!isDefault("StartWorkspaceIndex")) {
65 const int minSpec_i = getProperty("StartWorkspaceIndex");
66 auto minSpec = static_cast<size_t>(minSpec_i);
67 API::MatrixWorkspace_sptr ws = getProperty("InputWorkspace");
68 const size_t numberOfSpectra = ws->indexInfo().globalSize();
69 int maxSpec_i = getProperty("EndWorkspaceIndex");
70 auto maxSpec = static_cast<size_t>(maxSpec_i);
71 if (isEmpty(maxSpec_i))
72 maxSpec = numberOfSpectra - 1;
73 if (maxSpec < minSpec) {
74 helpMessages["StartWorkspaceIndex"] = "StartWorkspaceIndex must be less than or equal to EndWorkspaceIndex";
75 helpMessages["EndWorkspaceIndex"] = "EndWorkspaceIndex must be greater than or equal to StartWorkspaceIndex";
76 }
77 }
78
79 return helpMessages;
80}
81
85 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input), "The input workspace");
86 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
87 "Name of the output workspace");
88
89 declareProperty("XMin", EMPTY_DBL(),
90 "An X value that is within the first "
91 "(lowest X value) bin that will be "
92 "retained\n"
93 "(default: workspace min)");
94 declareProperty("XMax", EMPTY_DBL(),
95 "An X value that is in the highest X "
96 "value bin to be retained (default: max "
97 "X)");
98 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
99 mustBePositive->setLower(0);
100 declareProperty("StartWorkspaceIndex", 0, mustBePositive,
101 "The index number of the first entry in the Workspace that "
102 "will be loaded\n"
103 "(default: first entry in the Workspace)");
104 // As the property takes ownership of the validator pointer, have to take care
105 // to pass in a unique pointer to each property.
106 declareProperty("EndWorkspaceIndex", EMPTY_INT(), mustBePositive,
107 "The index number of the last entry in the Workspace to be loaded\n"
108 "(default: last entry in the Workspace)");
109 declareProperty(std::make_unique<ArrayProperty<size_t>>("WorkspaceIndexList"),
110 "A comma-separated list of individual workspace indices to "
111 "read. Only used if\n"
112 "explicitly set. The WorkspaceIndexList is only used if the "
113 "DetectorList is empty.");
114
115 declareProperty(std::make_unique<ArrayProperty<detid_t>>("DetectorList"),
116 "A comma-separated list of individual detector IDs to read. "
117 "Only used if\n"
118 "explicitly set. When specifying the WorkspaceIndexList and "
119 "DetectorList property,\n"
120 "the latter is being selected.");
121}
122
128 m_inputWorkspace = getProperty("InputWorkspace");
129 m_isHistogramData = m_inputWorkspace->isHistogramData();
130 m_commonBoundaries = m_inputWorkspace->isCommonBins();
131 this->checkProperties();
132
133 if (m_workspaceIndexList.empty()) {
134 MatrixWorkspace_sptr out = getProperty("OutputWorkspace");
135 // No spectra extracted, but not in-place, clone input before cropping.
136 if (out != m_inputWorkspace)
138 } else {
139 auto extract = std::make_shared<ExtractSpectra2>();
140 setupAsChildAlgorithm(extract);
141 extract->setWorkspaceInputProperties(
142 "InputWorkspace", m_inputWorkspace, IndexType::WorkspaceIndex,
143 std::vector<int64_t>(m_workspaceIndexList.begin(), m_workspaceIndexList.end()));
144 extract->execute();
145 m_inputWorkspace = extract->getProperty("OutputWorkspace");
146 }
147 setProperty("OutputWorkspace", m_inputWorkspace);
148
149 // don't trim x-range if those values are not specified
150 if (isDefault("XMin") && isDefault("XMax"))
151 return;
152
153 eventW = std::dynamic_pointer_cast<EventWorkspace>(m_inputWorkspace);
154 if (eventW)
155 this->execEvent();
156 else
157 this->execHistogram();
158}
159
162 auto size = static_cast<int>(m_inputWorkspace->getNumberHistograms());
163 auto croppedCommonXHistogram = getCroppedXHistogram(*m_inputWorkspace);
164 Progress prog(this, 0.0, 1.0, size);
165 for (int i = 0; i < size; ++i) {
166 if (m_commonBoundaries) {
167 this->cropCommon(*m_inputWorkspace, croppedCommonXHistogram, i);
168 } else {
169 this->cropRagged(*m_inputWorkspace, i);
170 }
172 prog.report();
173 }
174}
175
183 const auto hist = workspace.histogram(0);
184 auto begin = m_minXIndex;
185 auto end = histXMaxIndex();
186
187 auto cropped(hist);
188 cropped.resize(end - begin);
189
190 auto xEnd = hist.xMode() == Histogram::XMode::Points ? end : end + 1;
191 cropped.mutableX().assign(hist.x().begin() + begin, hist.x().begin() + xEnd);
192 return cropped.sharedX();
193}
194
202 const auto hist = workspace.histogram(index);
203 auto begin = m_minXIndex;
204 auto end = histXMaxIndex();
205
206 auto cropped(hist);
207 cropped.resize(end - begin);
208
209 cropped.setSharedX(XHistogram);
210
211 if (cropped.sharedY())
212 cropped.mutableY().assign(hist.y().begin() + begin, hist.y().begin() + end);
213 if (cropped.sharedE())
214 cropped.mutableE().assign(hist.e().begin() + begin, hist.e().begin() + end);
215 if (cropped.sharedDx())
216 cropped.mutableDx().assign(hist.dx().begin() + begin, hist.dx().begin() + end);
217
218 workspace.setHistogram(index, cropped);
219}
220
226 auto &Y = workspace.mutableY(index);
227 auto &E = workspace.mutableE(index);
228 const size_t size = Y.size();
229 size_t startX = this->getXMinIndex(index);
230 if (startX > size)
231 startX = size;
232 for (size_t i = 0; i < startX; ++i) {
233 Y[i] = 0.0;
234 E[i] = 0.0;
235 }
236 size_t endX = this->getXMaxIndex(index);
237 if (endX > 0 && m_isHistogramData)
238 endX -= 1;
239 for (size_t i = endX; i < size; ++i) {
240 Y[i] = 0.0;
241 E[i] = 0.0;
242 }
243}
244
245namespace { // anonymous namespace
246template <class T> void filterEventsHelper(std::vector<T> &events, const double xmin, const double xmax) {
247 events.erase(std::remove_if(events.begin(), events.end(),
248 [xmin, xmax](const T &event) {
249 const double tof = event.tof();
250 return bool(tof < xmin || tof > xmax);
251 }),
252 events.end());
253}
254} // namespace
255
264 // use min/max from workspace if the values aren't supplied by the user
265 const double minX_val = isDefault("XMin") ? eventW->getTofMin() : getProperty("XMin");
266 const double maxX_val = isDefault("XMax") ? eventW->getTofMax() : getProperty("XMax");
267
268 BinEdges binEdges(2);
269 if (m_commonBoundaries) {
270 auto &oldX = m_inputWorkspace->x(0);
271 binEdges = BinEdges(oldX.begin() + m_minXIndex, oldX.begin() + m_maxXIndex);
272 }
273 if (m_maxXIndex - m_minXIndex < 2) {
274 // create new output X axis
275 binEdges = {minX_val, maxX_val};
276 }
277
278 Progress prog(this, 0.0, 1.0, eventW->getNumberHistograms());
280 for (int i = 0; i < static_cast<int>(eventW->getNumberHistograms()); ++i) {
282 EventList &el = eventW->getSpectrum(i);
283
284 if (!el.empty()) {
285 switch (el.getEventType()) {
286 case TOF: {
287 filterEventsHelper(el.getEvents(), minX_val, maxX_val);
288 break;
289 }
290 case WEIGHTED: {
291 filterEventsHelper(el.getWeightedEvents(), minX_val, maxX_val);
292 break;
293 }
294 case WEIGHTED_NOTIME: {
295 filterEventsHelper(el.getWeightedEventsNoTime(), minX_val, maxX_val);
296 break;
297 }
298 }
299 }
300
301 // If the X axis is NOT common, then keep the initial X axis, just clear the
302 // events, otherwise:
303 if (m_commonBoundaries) {
304 const auto oldDx = el.pointStandardDeviations();
305 el.setHistogram(binEdges);
306 if (oldDx) {
307 auto end = histXMaxIndex();
308 el.setPointStandardDeviations(oldDx.begin() + m_minXIndex, oldDx.begin() + end);
309 }
310 }
312 prog.report();
314 }
316}
317
320 auto end = histXMaxIndex();
321 if (workspace.hasMaskedBins(i)) {
322 MatrixWorkspace::MaskList filteredMask;
323 for (const auto &mask : workspace.maskedBins(i)) {
324 const size_t maskIndex = mask.first;
325 if (maskIndex >= m_minXIndex && maskIndex < end)
326 filteredMask[maskIndex - m_minXIndex] = mask.second;
327 }
328 if (filteredMask.size() > 0)
329 workspace.setMaskedBins(i, filteredMask);
330 else
331 workspace.setUnmaskedBins(i);
332 }
333}
334
344 m_minXIndex = this->getXMinIndex();
345 m_maxXIndex = this->getXMaxIndex();
346 const size_t xSize = m_inputWorkspace->x(0).size();
347 if (m_minXIndex > 0 || m_maxXIndex < xSize) {
348 if (m_minXIndex > m_maxXIndex) {
349 throw std::out_of_range("XMin must be less than XMax");
350 }
351 m_croppingInX = true;
352 if (m_commonBoundaries && !std::dynamic_pointer_cast<EventWorkspace>(m_inputWorkspace) &&
354 m_minXIndex--;
356 if (m_isHistogramData) {
357 m_maxXIndex += 1;
358 }
359 }
360 }
361 if (!m_commonBoundaries) {
362 m_minXIndex = 0;
363 m_maxXIndex = static_cast<int>(m_inputWorkspace->x(0).size());
364 }
365
366 // The hierarchy of inputs is (one is being selected):
367 // 1. DetectorList
368 // 2. WorkspaceIndexList
369 // 3. Start and stop index
370 std::vector<detid_t> detectorList = getProperty("DetectorList");
371 if (!detectorList.empty()) {
372 m_workspaceIndexList = m_inputWorkspace->getIndicesFromDetectorIDs(detectorList);
373 } else {
374 m_workspaceIndexList = getProperty("WorkspaceIndexList");
375
376 if (m_workspaceIndexList.empty()) {
377 int minSpec_i = getProperty("StartWorkspaceIndex");
378 auto minSpec = static_cast<size_t>(minSpec_i);
379 const size_t numberOfSpectra = m_inputWorkspace->indexInfo().globalSize();
380 int maxSpec_i = getProperty("EndWorkspaceIndex");
381 auto maxSpec = static_cast<size_t>(maxSpec_i);
382 if (isEmpty(maxSpec_i))
383 maxSpec = numberOfSpectra - 1;
384 if (maxSpec - minSpec + 1 != numberOfSpectra) {
385 m_workspaceIndexList.reserve(maxSpec - minSpec + 1);
386 for (size_t i = minSpec; i <= maxSpec; ++i)
387 m_workspaceIndexList.emplace_back(i);
388 }
389 }
390 }
391} // namespace Algorithms
392
399size_t ExtractSpectra::getXMinIndex(const size_t wsIndex) {
400 double minX_val = getProperty("XMin");
401 size_t xIndex = 0;
402 if (!isEmpty(minX_val)) { // A value has been passed to the algorithm, check
403 // it and maybe store it
404 const auto &X = m_inputWorkspace->x(wsIndex);
405 if (m_commonBoundaries && minX_val > X.back()) {
406 std::stringstream msg;
407 msg << "XMin is greater than the largest X value (" << minX_val << " > " << X.back() << ")";
408 throw std::out_of_range(msg.str());
409 }
410 // Reduce cut-off value slightly to allow for rounding errors
411 // when trying to exactly hit a bin boundary.
412 minX_val -= std::abs(minX_val * xBoundaryTolerance);
413 xIndex = std::lower_bound(X.begin(), X.end(), minX_val) - X.begin();
414 }
415 return xIndex;
416}
417
424size_t ExtractSpectra::getXMaxIndex(const size_t wsIndex) {
425 const auto &X = m_inputWorkspace->x(wsIndex);
426 size_t xIndex = X.size();
427 // get the value that the user entered if they entered one at all
428 double maxX_val = getProperty("XMax");
429 if (!isEmpty(maxX_val)) { // we have a user value, check it and maybe store it
430 if (m_commonBoundaries && maxX_val < X.front()) {
431 std::stringstream msg;
432 msg << "XMax is less than the smallest X value (" << maxX_val << " < " << X.front() << ")";
433 throw std::out_of_range(msg.str());
434 }
435 // Increase cut-off value slightly to allow for rounding errors
436 // when trying to exactly hit a bin boundary.
437 maxX_val += std::abs(maxX_val * xBoundaryTolerance);
438 xIndex = std::upper_bound(X.begin(), X.end(), maxX_val) - X.begin();
439 }
440 return xIndex;
441}
442
444 if (m_isHistogramData) {
445 return m_maxXIndex - 1;
446 }
447 return m_maxXIndex;
448}
449
450} // namespace Mantid::Algorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
IntArray detectorList
#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.
bool isDefault(const std::string &name) const
static bool isEmpty(const NumT toCheck)
checks that the value was not set by users, uses the value in empty double/int.
void setupAsChildAlgorithm(const Algorithm_sptr &algorithm, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true)
Setup algorithm as child algorithm.
Base MatrixWorkspace Abstract Class.
std::map< size_t, double > MaskList
Masked bins for each spectrum are stored as a set of pairs containing <bin index, weight>
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
Extracts specified spectra from a workspace and places them in a new workspace.
void checkProperties()
Retrieves the optional input properties and checks that they have valid values.
void cropCommon(API::MatrixWorkspace &workspace, Kernel::cow_ptr< Mantid::HistogramData::HistogramX > XHistogram, int index)
Crops the given workspace in accordance with m_minX and m_maxX.
bool m_isHistogramData
Flag indicating whether we're dealing with histogram data.
std::size_t m_minXIndex
The bin index to start the cropped workspace from.
std::vector< size_t > m_workspaceIndexList
The list of workspaces to extract.
void execHistogram()
Execute the algorithm in case of a histogrammed data.
std::size_t m_maxXIndex
The bin index to end the cropped workspace at.
const Kernel::cow_ptr< Mantid::HistogramData::HistogramX > getCroppedXHistogram(const API::MatrixWorkspace &workspace)
Returns a pointer to a cropped X Histogram to be used as the X Histogram for each of the spectra in t...
std::size_t getXMaxIndex(const size_t wsIndex=0)
Find the X index corresponding to (or just within) the value given in the XMax property.
std::map< std::string, std::string > validateInputs() override
Validate the input properties are sane.
void init() override
Initialize the algorithm's properties.
void execEvent()
Executes the algorithm.
void cropRagged(API::MatrixWorkspace &workspace, int index)
Zeroes all data points outside the X values given.
int version() const override
Algorithm's version for identification.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
bool m_croppingInX
Flag indicating whether XMin and/or XMax has been set.
API::MatrixWorkspace_sptr m_inputWorkspace
The input workspace.
DataObjects::EventWorkspace_sptr eventW
void exec() override
Executes the algorithm.
bool m_commonBoundaries
Flag indicating whether the input workspace has common boundaries.
const std::string category() const override
Algorithm's category for identification.
void propagateBinMasking(API::MatrixWorkspace &workspace, const int i) const
Propagate bin masking if there is any.
std::size_t getXMinIndex(const size_t wsIndex=0)
Find the X index corresponding to (or just within) the value given in the XMin property.
A class for holding :
Definition EventList.h:57
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 report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Implements a copy on write data template.
Definition cow_ptr.h:41
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.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition EmptyValues.h:24
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition EmptyValues.h:42
STL namespace.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54