15#include "MantidHistogramData/HistogramBuilder.h"
16#include "MantidHistogramData/Rebin.h"
26using namespace Kernel;
27using DataObjects::EventList;
28using DataObjects::EventWorkspace;
30using HistogramData::HistogramBuilder;
45 return "Rebin each spectrum of a workspace independently. There is only one delta allowed per spectrum";
58 declareProperty(
"PreserveEvents",
true,
"False converts event workspaces to histograms");
59 declareProperty(
"FullBinsOnly",
false,
"Omit the final bin if it's width is smaller than the step size");
63 std::map<std::string, std::string> errors;
65 const std::vector<double> xmins =
getProperty(
"XMin");
66 const std::vector<double> xmaxs =
getProperty(
"XMax");
67 const std::vector<double> deltas =
getProperty(
"Delta");
69 const auto numMin = xmins.size();
70 const auto numMax = xmaxs.size();
71 const auto numDelta = deltas.size();
73 if (std::any_of(deltas.cbegin(), deltas.cend(), [](
double d) { return !std::isfinite(d); }))
74 errors[
"Delta"] =
"All must be finite";
75 else if (std::any_of(deltas.cbegin(), deltas.cend(), [](
double d) { return d == 0; }))
76 errors[
"Delta"] =
"All must be nonzero";
81 const auto histnumber = inputWS->getNumberHistograms();
84 errors[
"Delta"] =
"Must specify binning";
85 else if (!(numDelta == 1 || numDelta == histnumber))
89 if (numMin > 1 && numMin != histnumber)
93 if (numMax > 1 && numMax != histnumber)
97 errors[
"InputWorkspace"] =
"InputWorkspace is not a MatrixWorkspace";
109 bool preserveEvents =
getProperty(
"PreserveEvents");
113 bool inPlace = (inputWS == outputWS);
116 const auto histnumber =
static_cast<int>(inputWS->getNumberHistograms());
125 rebin->setProperty(
"InputWorkspace", inputWS);
126 rebin->setProperty(
"PreserveEvents", preserveEvents);
127 rebin->setProperty(
"FullBinsOnly", fullBinsOnly);
128 const std::vector<double> params = {xmins[0], deltas[0], xmaxs[0]};
129 rebin->setProperty(
"Params", params);
142 for (
int hist = 0; hist < histnumber; hist++) {
143 const auto inX = inputWS->x(hist);
144 if (!std::isfinite(xmins[hist]))
145 xmins[hist] = inX.front();
146 if (!std::isfinite(xmaxs[hist]))
147 xmaxs[hist] = inX.back();
150 const bool dist = inputWS->isDistribution();
157 if (preserveEvents) {
159 outputWS = inputWS->clone();
161 auto eventOutputWS = std::dynamic_pointer_cast<EventWorkspace>(outputWS);
163 for (
int hist = 0; hist < histnumber; hist++) {
164 auto xmin = xmins[hist];
165 auto xmax = xmaxs[hist];
166 const auto delta = deltas[hist];
168 std::vector<double> xAxisTmp;
170 HistogramData::BinEdges XValues_new(std::move(xAxisTmp));
171 EventList &el = eventOutputWS->getSpectrum(hist);
176 g_log.
information() <<
"Creating a Workspace2D from the EventWorkspace " << eventInputWS->getName() <<
".\n";
178 outputWS = DataObjects::create<DataObjects::Workspace2D>(*inputWS);
180 Progress prog(
this, 0.0, 1.0, histnumber);
184 for (
int hist = 0; hist < histnumber; ++hist) {
186 auto xmin = xmins[hist];
187 auto xmax = xmaxs[hist];
188 const auto delta = deltas[hist];
191 const EventList &el = eventInputWS->getSpectrum(hist);
193 std::vector<double> xAxisTmp;
195 HistogramData::BinEdges XValues_new(std::move(xAxisTmp));
202 HistogramBuilder builder;
203 builder.setX(XValues_new.rawData());
204 builder.setY(y_data);
205 builder.setE(e_data);
206 builder.setDistribution(dist);
207 outputWS->setHistogram(hist, builder.build());
219 const bool isHist = inputWS->isHistogramData();
223 inputWS = inputWS->clone();
224 for (
int hist = 0; hist < histnumber; ++hist) {
225 HistogramBuilder builder;
226 builder.setX(inputWS->histogram(hist).binEdges().rawData());
227 builder.setY(inputWS->readY(hist));
228 builder.setE(inputWS->readE(hist));
229 if (inputWS->hasDx(dist))
230 builder.setDx(inputWS->readDx(hist));
231 builder.setDistribution(dist);
232 inputWS->setHistogram(hist, builder.build());
237 outputWS = DataObjects::create<API::HistoWorkspace>(*inputWS);
239 Progress prog(
this, 0.0, 1.0, histnumber);
242 for (
int hist = 0; hist < histnumber; ++hist) {
244 auto xmin = xmins[hist];
245 auto xmax = xmaxs[hist];
246 const auto delta = deltas[hist];
248 std::vector<double> xAxisTmp;
250 HistogramData::BinEdges XValues_new(std::move(xAxisTmp));
252 outputWS->setHistogram(hist, HistogramData::rebin(inputWS->histogram(hist), XValues_new));
257 outputWS->setDistribution(dist);
262 for (
int hist = 0; hist < histnumber; ++hist) {
263 if (inputWS->hasMaskedBins(hist))
265 outputWS->setUnmaskedBins(hist);
272 for (
int hist = 0; hist < histnumber; ++hist) {
273 HistogramBuilder builder;
274 builder.setX(outputWS->histogram(hist).points().rawData());
275 builder.setY(outputWS->readY(hist));
276 builder.setE(outputWS->readE(hist));
277 if (outputWS->hasDx(hist))
278 builder.setDx(outputWS->readDx(hist));
279 builder.setDistribution(dist);
280 outputWS->setHistogram(hist, builder.build());
289 if (xmins.size() == 1 && xmaxs.size() == 1 && deltas.size() == 1)
292 if (xmins.size() == 0 || xmaxs.size() == 0 || deltas.size() == 0)
296 if (!std::equal(xmins.cbegin() + 1, xmins.cend(), xmins.cbegin()))
300 if (!std::equal(xmaxs.cbegin() + 1, xmaxs.cend(), xmaxs.cbegin()))
304 if (!std::equal(deltas.cbegin() + 1, deltas.cend(), deltas.cbegin()))
312 if (array.size() == 0) {
313 array.resize(histnumber, std::numeric_limits<double>::quiet_NaN());
314 }
else if (array.size() == 1) {
315 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
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::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.