14#include "MantidIndexing/Extract.h"
15#include "MantidIndexing/IndexInfo.h"
23const double xBoundaryTolerance = 1.0e-15;
28using namespace Kernel;
30using namespace DataObjects;
31using namespace HistogramData;
32using Types::Event::TofEvent;
48 return "Extracts a list of spectra from a workspace and places them in a new "
54 std::map<std::string, std::string> helpMessages;
59 helpMessages[
"XMin"] =
"XMin must be less than XMax";
60 helpMessages[
"XMax"] =
"XMax must be greater than XMin";
65 const int minSpec_i =
getProperty(
"StartWorkspaceIndex");
66 auto minSpec =
static_cast<size_t>(minSpec_i);
68 const size_t numberOfSpectra = ws->indexInfo().globalSize();
70 auto maxSpec =
static_cast<size_t>(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";
87 "Name of the output workspace");
90 "An X value that is within the first "
91 "(lowest X value) bin that will be "
93 "(default: workspace min)");
95 "An X value that is in the highest X "
96 "value bin to be retained (default: max "
98 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
99 mustBePositive->setLower(0);
101 "The index number of the first entry in the Workspace that "
103 "(default: first entry in the Workspace)");
107 "The index number of the last entry in the Workspace to be loaded\n"
108 "(default: last entry in the Workspace)");
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.");
116 "A comma-separated list of individual detector IDs to read. "
118 "explicitly set. When specifying the WorkspaceIndexList and "
119 "DetectorList property,\n"
120 "the latter is being selected.");
139 auto extract = std::make_shared<ExtractSpectra2>();
141 extract->setWorkspaceInputProperties(
164 Progress prog(
this, 0.0, 1.0, size);
165 for (
int i = 0; i < size; ++i) {
183 const auto hist =
workspace.histogram(0);
188 cropped.resize(end - begin);
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();
207 cropped.resize(end - begin);
209 cropped.setSharedX(XHistogram);
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);
228 const size_t size =
Y.size();
232 for (
size_t i = 0; i < startX; ++i) {
239 for (
size_t i = endX; i < size; ++i) {
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);
268 BinEdges binEdges(2);
275 binEdges = {minX_val, maxX_val};
280 for (
int i = 0; i < static_cast<int>(
eventW->getNumberHistograms()); ++i) {
285 switch (el.getEventType()) {
287 filterEventsHelper(el.getEvents(), minX_val, maxX_val);
291 filterEventsHelper(el.getWeightedEvents(), minX_val, maxX_val);
295 filterEventsHelper(el.getWeightedEventsNoTime(), minX_val, maxX_val);
304 const auto oldDx = el.pointStandardDeviations();
305 el.setHistogram(binEdges);
308 el.setPointStandardDeviations(oldDx.begin() +
m_minXIndex, oldDx.begin() + end);
323 for (
const auto &mask :
workspace.maskedBins(i)) {
324 const size_t maskIndex = mask.first;
326 filteredMask[maskIndex -
m_minXIndex] = mask.second;
328 if (filteredMask.size() > 0)
329 workspace.setMaskedBins(i, filteredMask);
349 throw std::out_of_range(
"XMin must be less than XMax");
377 int minSpec_i =
getProperty(
"StartWorkspaceIndex");
378 auto minSpec =
static_cast<size_t>(minSpec_i);
381 auto maxSpec =
static_cast<size_t>(maxSpec_i);
383 maxSpec = numberOfSpectra - 1;
384 if (maxSpec - minSpec + 1 != numberOfSpectra) {
386 for (
size_t i = minSpec; i <= maxSpec; ++i)
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());
412 minX_val -= std::abs(minX_val * xBoundaryTolerance);
413 xIndex = std::lower_bound(
X.begin(),
X.end(), minX_val) -
X.begin();
426 size_t xIndex =
X.size();
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());
437 maxX_val += std::abs(maxX_val * xBoundaryTolerance);
438 xIndex = std::upper_bound(
X.begin(),
X.end(), maxX_val) -
X.begin();
#define DECLARE_ALGORITHM(classname)
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
#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.
A property class for workspaces.
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.
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.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
@ Input
An input workspace.
@ Output
An output workspace.