14#include "MantidIndexing/Extract.h"
15#include "MantidIndexing/IndexInfo.h"
25const double xBoundaryTolerance = 1.0e-15;
30using namespace Kernel;
32using namespace DataObjects;
33using namespace HistogramData;
34using Types::Event::TofEvent;
50 return "Extracts a list of spectra from a workspace and places them in a new "
56 std::map<std::string, std::string> helpMessages;
61 helpMessages[
"XMin"] =
"XMin must be less than XMax";
62 helpMessages[
"XMax"] =
"XMax must be greater than XMin";
67 const int minSpec_i =
getProperty(
"StartWorkspaceIndex");
68 auto minSpec =
static_cast<size_t>(minSpec_i);
70 const size_t numberOfSpectra = ws->indexInfo().globalSize();
72 auto maxSpec =
static_cast<size_t>(maxSpec_i);
74 maxSpec = numberOfSpectra - 1;
75 if (maxSpec < minSpec) {
76 helpMessages[
"StartWorkspaceIndex"] =
"StartWorkspaceIndex must be less than or equal to EndWorkspaceIndex";
77 helpMessages[
"EndWorkspaceIndex"] =
"EndWorkspaceIndex must be greater than or equal to StartWorkspaceIndex";
89 "Name of the output workspace");
92 "An X value that is within the first "
93 "(lowest X value) bin that will be "
95 "(default: workspace min)");
97 "An X value that is in the highest X "
98 "value bin to be retained (default: max "
100 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
101 mustBePositive->setLower(0);
103 "The index number of the first entry in the Workspace that "
105 "(default: first entry in the Workspace)");
109 "The index number of the last entry in the Workspace to be loaded\n"
110 "(default: last entry in the Workspace)");
112 "A comma-separated list of individual workspace indices to "
113 "read. Only used if\n"
114 "explicitly set. The WorkspaceIndexList is only used if the "
115 "DetectorList is empty.");
118 "A comma-separated list of individual detector IDs to read. "
120 "explicitly set. When specifying the WorkspaceIndexList and "
121 "DetectorList property,\n"
122 "the latter is being selected.");
141 auto extract = std::make_shared<ExtractSpectra2>();
143 extract->setWorkspaceInputProperties(
166 Progress prog(
this, 0.0, 1.0, size);
167 for (
int i = 0; i < size; ++i) {
185 const auto hist =
workspace.histogram(0);
190 cropped.resize(end - begin);
192 auto xEnd = hist.xMode() == Histogram::XMode::Points ? end : end + 1;
193 cropped.mutableX().assign(hist.x().begin() + begin, hist.x().begin() + xEnd);
194 return cropped.sharedX();
209 cropped.resize(end - begin);
211 cropped.setX(XHistogram);
213 if (cropped.sharedY())
214 cropped.mutableY().assign(hist.y().begin() + begin, hist.y().begin() + end);
215 if (cropped.sharedE())
216 cropped.mutableE().assign(hist.e().begin() + begin, hist.e().begin() + end);
217 if (cropped.sharedDx())
218 cropped.mutableDx().assign(hist.dx().begin() + begin, hist.dx().begin() + end);
230 const size_t size =
Y.size();
234 for (
size_t i = 0; i < startX; ++i) {
241 for (
size_t i = endX; i < size; ++i) {
248template <
class T>
void filterEventsHelper(std::vector<T> &events,
const double xmin,
const double xmax) {
249 events.erase(std::remove_if(events.begin(), events.end(),
250 [xmin, xmax](
const T &event) {
251 const double tof = event.tof();
252 return bool(tof < xmin || tof > xmax);
270 BinEdges binEdges(2);
277 binEdges = {minX_val, maxX_val};
282 for (
int i = 0; i < static_cast<int>(
eventW->getNumberHistograms()); ++i) {
287 switch (el.getEventType()) {
289 filterEventsHelper(el.getEvents(), minX_val, maxX_val);
293 filterEventsHelper(el.getWeightedEvents(), minX_val, maxX_val);
297 filterEventsHelper(el.getWeightedEventsNoTime(), minX_val, maxX_val);
306 const auto oldDx = el.pointStandardDeviations();
307 el.setHistogram(binEdges);
310 el.setPointStandardDeviations(oldDx.begin() +
m_minXIndex, oldDx.begin() + end);
325 for (
const auto &mask :
workspace.maskedBins(i)) {
326 const size_t maskIndex = mask.first;
328 filteredMask[maskIndex -
m_minXIndex] = mask.second;
330 if (filteredMask.size() > 0)
331 workspace.setMaskedBins(i, filteredMask);
351 throw std::out_of_range(
"XMin must be less than XMax");
377 std::vector<size_t> indicesVector =
getProperty(
"WorkspaceIndexList");
381 int minSpec_i =
getProperty(
"StartWorkspaceIndex");
382 auto minSpec =
static_cast<size_t>(minSpec_i);
385 auto maxSpec =
static_cast<size_t>(maxSpec_i);
387 maxSpec = numberOfSpectra - 1;
388 if (maxSpec - minSpec + 1 != numberOfSpectra) {
389 for (
size_t i = minSpec; i <= maxSpec; ++i)
409 std::stringstream msg;
410 msg <<
"XMin is greater than the largest X value (" << minX_val <<
" > " <<
X.back() <<
")";
411 throw std::out_of_range(msg.str());
415 minX_val -= std::abs(minX_val * xBoundaryTolerance);
416 xIndex = std::lower_bound(
X.begin(),
X.end(), minX_val) -
X.begin();
429 size_t xIndex =
X.size();
434 std::stringstream msg;
435 msg <<
"XMax is less than the smallest X value (" << maxX_val <<
" < " <<
X.front() <<
")";
436 throw std::out_of_range(msg.str());
440 maxX_val += std::abs(maxX_val * xBoundaryTolerance);
441 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.