15#include "MantidHistogramData/HistogramBuilder.h"
16#include "MantidHistogramData/Rebin.h"
28using namespace Kernel;
29using DataObjects::EventList;
30using DataObjects::EventWorkspace;
32using HistogramData::HistogramBuilder;
47 return "Rebin each spectrum of a workspace independently. There is only one delta allowed per spectrum";
60 declareProperty(
"PreserveEvents",
true,
"False converts event workspaces to histograms");
61 declareProperty(
"FullBinsOnly",
false,
"Omit the final bin if it's width is smaller than the step size");
65 std::map<std::string, std::string> errors;
67 const std::vector<double> xmins =
getProperty(
"XMin");
68 const std::vector<double> xmaxs =
getProperty(
"XMax");
69 const std::vector<double> deltas =
getProperty(
"Delta");
71 const auto numMin = xmins.size();
72 const auto numMax = xmaxs.size();
73 const auto numDelta = deltas.size();
75 if (std::any_of(deltas.cbegin(), deltas.cend(), [](
double d) { return !std::isfinite(d); }))
76 errors[
"Delta"] =
"All must be finite";
77 else if (std::any_of(deltas.cbegin(), deltas.cend(), [](
double d) { return d == 0; }))
78 errors[
"Delta"] =
"All must be nonzero";
83 const auto histnumber = inputWS->getNumberHistograms();
86 errors[
"Delta"] =
"Must specify binning";
87 else if (!(numDelta == 1 || numDelta == histnumber))
91 if (numMin > 1 && numMin != histnumber)
95 if (numMax > 1 && numMax != histnumber)
99 errors[
"InputWorkspace"] =
"InputWorkspace is not a MatrixWorkspace";
111 bool preserveEvents =
getProperty(
"PreserveEvents");
115 bool inPlace = (inputWS == outputWS);
118 const auto histnumber =
static_cast<int>(inputWS->getNumberHistograms());
127 rebin->setProperty(
"InputWorkspace", inputWS);
128 rebin->setProperty(
"PreserveEvents", preserveEvents);
129 rebin->setProperty(
"FullBinsOnly", fullBinsOnly);
130 const std::vector<double> params = {xmins[0], deltas[0], xmaxs[0]};
131 rebin->setProperty(
"Params", params);
144 for (
int hist = 0; hist < histnumber; hist++) {
145 const auto inX = inputWS->x(hist);
146 if (!std::isfinite(xmins[hist]))
147 xmins[hist] = inX.front();
148 if (!std::isfinite(xmaxs[hist]))
149 xmaxs[hist] = inX.back();
152 const bool dist = inputWS->isDistribution();
159 if (preserveEvents) {
161 outputWS = inputWS->clone();
163 auto eventOutputWS = std::dynamic_pointer_cast<EventWorkspace>(outputWS);
165 for (
int hist = 0; hist < histnumber; hist++) {
166 auto xmin = xmins[hist];
167 auto xmax = xmaxs[hist];
168 const auto delta = deltas[hist];
170 std::vector<double> xAxisTmp;
172 HistogramData::BinEdges XValues_new(std::move(xAxisTmp));
173 EventList &el = eventOutputWS->getSpectrum(hist);
178 g_log.
information() <<
"Creating a Workspace2D from the EventWorkspace " << eventInputWS->getName() <<
".\n";
180 outputWS = DataObjects::create<DataObjects::Workspace2D>(*inputWS);
182 Progress prog(
this, 0.0, 1.0, histnumber);
186 for (
int hist = 0; hist < histnumber; ++hist) {
188 auto xmin = xmins[hist];
189 auto xmax = xmaxs[hist];
190 const auto delta = deltas[hist];
193 const EventList &el = eventInputWS->getSpectrum(hist);
195 std::vector<double> xAxisTmp;
197 HistogramData::BinEdges XValues_new(std::move(xAxisTmp));
204 HistogramBuilder builder;
205 builder.setX(XValues_new.rawData());
206 builder.setY(y_data);
207 builder.setE(e_data);
208 builder.setDistribution(dist);
209 outputWS->setHistogram(hist, builder.build());
221 const bool isHist = inputWS->isHistogramData();
225 inputWS = inputWS->clone();
226 for (
int hist = 0; hist < histnumber; ++hist) {
227 HistogramBuilder builder;
228 builder.setX(inputWS->histogram(hist).binEdges().rawData());
229 builder.setY(inputWS->y(hist));
230 builder.setE(inputWS->e(hist));
231 if (inputWS->hasDx(dist))
232 builder.setDx(inputWS->dx(hist));
233 builder.setDistribution(dist);
234 inputWS->setHistogram(hist, builder.build());
239 outputWS = DataObjects::create<API::HistoWorkspace>(*inputWS);
241 Progress prog(
this, 0.0, 1.0, histnumber);
244 for (
int hist = 0; hist < histnumber; ++hist) {
246 auto xmin = xmins[hist];
247 auto xmax = xmaxs[hist];
248 const auto delta = deltas[hist];
250 std::vector<double> xAxisTmp;
252 HistogramData::BinEdges XValues_new(std::move(xAxisTmp));
254 outputWS->setHistogram(hist, HistogramData::rebin(inputWS->histogram(hist), XValues_new));
259 outputWS->setDistribution(dist);
264 for (
int hist = 0; hist < histnumber; ++hist) {
265 if (inputWS->hasMaskedBins(hist))
267 outputWS->setUnmaskedBins(hist);
274 for (
int hist = 0; hist < histnumber; ++hist) {
275 HistogramBuilder builder;
276 builder.setX(outputWS->histogram(hist).points().rawData());
277 builder.setY(outputWS->y(hist));
278 builder.setE(outputWS->e(hist));
279 if (outputWS->hasDx(hist))
280 builder.setDx(outputWS->dx(hist));
281 builder.setDistribution(dist);
282 outputWS->setHistogram(hist, builder.build());
291 if (xmins.size() == 1 && xmaxs.size() == 1 && deltas.size() == 1)
294 if (xmins.size() == 0 || xmaxs.size() == 0 || deltas.size() == 0)
298 if (!std::equal(xmins.cbegin() + 1, xmins.cend(), xmins.cbegin()))
302 if (!std::equal(xmaxs.cbegin() + 1, xmaxs.cend(), xmaxs.cbegin()))
306 if (!std::equal(deltas.cbegin() + 1, deltas.cend(), deltas.cbegin()))
314 if (array.size() == 0) {
315 array.resize(histnumber, std::numeric_limits<double>::quiet_NaN());
316 }
else if (array.size() == 1) {
317 array.resize(histnumber, array[0]);
#define DECLARE_ALGORITHM(classname)
#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.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
void setHistogram(T &&...data)
Sets the Histogram associated with this spectrum.
Helper class for reporting progress from algorithms.
A property class for workspaces.
std::map< std::string, std::string > validateInputs() override
Validate that the input properties are sane.
static void extend_value(int numSpec, std::vector< double > &array)
static bool use_simple_rebin(std::vector< double > xmins, std::vector< double > xmaxs, std::vector< double > deltas)
void exec() override
Execute the algorithm.
const std::string name() const override
Algorithms name for identification.
const std::string category() const override
Algorithm's category for identification.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
void propagateMasks(const API::MatrixWorkspace_const_sptr &inputWS, const API::MatrixWorkspace_sptr &outputWS, const int hist, const bool IgnoreBinErrors=false)
Takes the masks in the input workspace and apportions the weights into the new bins that overlap with...
void generateHistogram(std::span< double const > X, MantidVec &Y, MantidVec &E, bool skipError=false) const override
Generates both the Y and E (error) histograms w.r.t TOF for an EventList with or without WeightedEven...
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 information(const std::string &msg)
Logs at information level.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
Kernel::Logger g_log("DetermineSpinStateOrder")
std::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
void MANTID_KERNEL_DLL rebin(std::span< double const > xold, std::span< double const > yold, std::span< double const > eold, std::span< double const > xnew, std::span< double > ynew, std::span< double > enew, bool distribution, bool addition=false)
The input and output ranges are taken as spans so that the size-checked histogram data types,...
std::size_t MANTID_KERNEL_DLL createAxisFromRebinParams(const std::vector< double > ¶ms, std::vector< double > &xnew, const bool resize_xnew=true, const bool full_bins_only=false, const double xMinHint=std::nan(""), const double xMaxHint=std::nan(""), const bool useReverseLogarithmic=false, const double power=-1)
Creates a new output X array given a 'standard' set of rebinning parameters.
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.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
@ Output
An output workspace.