15#include "MantidHistogramData/HistogramBuilder.h"
16#include "MantidHistogramData/Rebin.h"
27using namespace Kernel;
28using DataObjects::EventList;
29using DataObjects::EventWorkspace;
31using HistogramData::HistogramBuilder;
46 return "Rebin each spectrum of a workspace independently. There is only one delta allowed per spectrum";
59 declareProperty(
"PreserveEvents",
true,
"False converts event workspaces to histograms");
60 declareProperty(
"FullBinsOnly",
false,
"Omit the final bin if it's width is smaller than the step size");
64 std::map<std::string, std::string> errors;
66 const std::vector<double> xmins =
getProperty(
"XMin");
67 const std::vector<double> xmaxs =
getProperty(
"XMax");
68 const std::vector<double> deltas =
getProperty(
"Delta");
70 const auto numMin = xmins.size();
71 const auto numMax = xmaxs.size();
72 const auto numDelta = deltas.size();
74 if (std::any_of(deltas.cbegin(), deltas.cend(), [](
double d) { return !std::isfinite(d); }))
75 errors[
"Delta"] =
"All must be finite";
76 else if (std::any_of(deltas.cbegin(), deltas.cend(), [](
double d) { return d == 0; }))
77 errors[
"Delta"] =
"All must be nonzero";
82 const auto histnumber = inputWS->getNumberHistograms();
85 errors[
"Delta"] =
"Must specify binning";
86 else if (!(numDelta == 1 || numDelta == histnumber))
90 if (numMin > 1 && numMin != histnumber)
94 if (numMax > 1 && numMax != histnumber)
98 errors[
"InputWorkspace"] =
"InputWorkspace is not a MatrixWorkspace";
110 bool preserveEvents =
getProperty(
"PreserveEvents");
114 bool inPlace = (inputWS == outputWS);
117 const auto histnumber =
static_cast<int>(inputWS->getNumberHistograms());
126 rebin->setProperty(
"InputWorkspace", inputWS);
127 rebin->setProperty(
"PreserveEvents", preserveEvents);
128 rebin->setProperty(
"FullBinsOnly", fullBinsOnly);
129 const std::vector<double> params = {xmins[0], deltas[0], xmaxs[0]};
130 rebin->setProperty(
"Params", params);
143 for (
int hist = 0; hist < histnumber; hist++) {
144 const auto inX = inputWS->x(hist);
145 if (!std::isfinite(xmins[hist]))
146 xmins[hist] = inX.front();
147 if (!std::isfinite(xmaxs[hist]))
148 xmaxs[hist] = inX.back();
151 const bool dist = inputWS->isDistribution();
158 if (preserveEvents) {
160 outputWS = inputWS->clone();
162 auto eventOutputWS = std::dynamic_pointer_cast<EventWorkspace>(outputWS);
164 for (
int hist = 0; hist < histnumber; hist++) {
165 auto xmin = xmins[hist];
166 auto xmax = xmaxs[hist];
167 const auto delta = deltas[hist];
169 HistogramData::BinEdges XValues_new(0);
171 true, fullBinsOnly));
172 EventList &el = eventOutputWS->getSpectrum(hist);
177 g_log.
information() <<
"Creating a Workspace2D from the EventWorkspace " << eventInputWS->getName() <<
".\n";
179 outputWS = DataObjects::create<DataObjects::Workspace2D>(*inputWS);
181 Progress prog(
this, 0.0, 1.0, histnumber);
185 for (
int hist = 0; hist < histnumber; ++hist) {
187 auto xmin = xmins[hist];
188 auto xmax = xmaxs[hist];
189 const auto delta = deltas[hist];
192 const EventList &el = eventInputWS->getSpectrum(hist);
194 HistogramData::BinEdges XValues_new(0);
196 true, fullBinsOnly));
203 HistogramBuilder builder;
204 builder.setX(XValues_new.rawData());
205 builder.setY(y_data);
206 builder.setE(e_data);
207 builder.setDistribution(dist);
208 outputWS->setHistogram(hist, builder.build());
220 const bool isHist = inputWS->isHistogramData();
224 inputWS = inputWS->clone();
225 for (
int hist = 0; hist < histnumber; ++hist) {
226 HistogramBuilder builder;
227 builder.setX(inputWS->histogram(hist).binEdges().rawData());
228 builder.setY(inputWS->readY(hist));
229 builder.setE(inputWS->readE(hist));
230 if (inputWS->hasDx(dist))
231 builder.setDx(inputWS->readDx(hist));
232 builder.setDistribution(dist);
233 inputWS->setHistogram(hist, builder.build());
238 outputWS = DataObjects::create<API::HistoWorkspace>(*inputWS);
240 Progress prog(
this, 0.0, 1.0, histnumber);
243 for (
int hist = 0; hist < histnumber; ++hist) {
245 auto xmin = xmins[hist];
246 auto xmax = xmaxs[hist];
247 const auto delta = deltas[hist];
249 HistogramData::BinEdges XValues_new(0);
253 outputWS->setHistogram(hist, HistogramData::rebin(inputWS->histogram(hist), XValues_new));
258 outputWS->setDistribution(dist);
263 for (
int hist = 0; hist < histnumber; ++hist) {
264 if (inputWS->hasMaskedBins(hist))
266 outputWS->setUnmaskedBins(hist);
273 for (
int hist = 0; hist < histnumber; ++hist) {
274 HistogramBuilder builder;
275 builder.setX(outputWS->histogram(hist).points().rawData());
276 builder.setY(outputWS->readY(hist));
277 builder.setE(outputWS->readE(hist));
278 if (outputWS->hasDx(hist))
279 builder.setDx(outputWS->readDx(hist));
280 builder.setDistribution(dist);
281 outputWS->setHistogram(hist, builder.build());
290 if (xmins.size() == 1 && xmaxs.size() == 1 && deltas.size() == 1)
293 if (xmins.size() == 0 || xmaxs.size() == 0 || deltas.size() == 0)
297 if (!std::equal(xmins.cbegin() + 1, xmins.cend(), xmins.cbegin()))
301 if (!std::equal(xmaxs.cbegin() + 1, xmaxs.cend(), xmaxs.cbegin()))
305 if (!std::equal(deltas.cbegin() + 1, deltas.cend(), deltas.cbegin()))
313 if (array.size() == 0) {
314 array.resize(histnumber, std::numeric_limits<double>::quiet_NaN());
315 }
else if (array.size() == 1) {
316 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(const MantidVec &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
int 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.
void MANTID_KERNEL_DLL rebin(const std::vector< double > &xold, const std::vector< double > &yold, const std::vector< double > &eold, const std::vector< double > &xnew, std::vector< double > &ynew, std::vector< double > &enew, bool distribution, bool addition=false)
Rebins data according to a new output X array.
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.
Helper class which provides the Collimation Length for SANS instruments.
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.