21using namespace DataObjects;
22using namespace Kernel;
23using HistogramData::BinEdges;
26using std::stringstream;
50 "An output workspace.");
53 "A comma separated list of the XMin for every spectrum. (Optional)");
55 "A comma separated list of the XMax for every spectrum. (Optional)");
57 auto min = std::make_shared<BoundedValidator<int>>();
59 declareProperty(
"NumberBins", 0, min,
"Number of bins to split up each spectrum into.");
60 declareProperty(
"LogBinning",
false,
"Use logarithmic binning. If false use constant step sizes.");
63 "Keep the output workspace as an EventWorkspace, if the "
64 "input has events (default).\n"
65 "If the input and output EventWorkspace names are the same, "
66 "only the X bins are set, which is very quick.\n"
67 "If false, then the workspace gets converted to a "
68 "Workspace2D histogram.");
75 map<string, string> errors;
78 if ((!xmins.empty()) && (!xmaxs.empty())) {
79 if (xmins.size() != xmaxs.size()) {
81 msg <<
"XMin and XMax do not define same number of spectra (" << xmins.size() <<
" != " << xmaxs.size() <<
")";
82 errors.emplace(
"XMax", msg.str());
84 size_t size = xmins.size();
85 for (
size_t i = 0; i < size; ++i) {
86 if (xmins[i] >= xmaxs[i]) {
88 msg <<
"XMin (" << xmins[i] <<
") cannot be greater than XMax (" << xmaxs[i] <<
")";
89 errors.emplace(
"XMax", msg.str());
111 const size_t numSpectra = inputWS->getNumberHistograms();
114 if (xmins.size() == 1 && numSpectra > xmins.size()) {
115 const double value = xmins.front();
116 xmins.insert(xmins.end(), numSpectra - xmins.size(),
value);
118 if (xmaxs.size() == 1 && numSpectra > xmaxs.size()) {
119 const double value = xmaxs.front();
120 xmaxs.insert(xmaxs.end(), numSpectra - xmaxs.size(),
value);
124 const bool updateXMins = xmins.empty();
125 const bool updateXMaxs = xmaxs.empty();
130 double xmin_wksp = inputWS->getXMin();
131 double xmax_wksp = inputWS->getXMax();
133 if (inputEventWS !=
nullptr && inputEventWS->getNumberEvents() > 0) {
134 xmin_wksp = inputEventWS->getTofMin();
135 xmax_wksp = inputEventWS->getTofMax();
138 for (
size_t i = 0; i < numSpectra; ++i) {
140 if (updateXMins || updateXMaxs) {
141 const auto &xvalues = inputWS->x(i);
143 const auto minimum = xvalues.front();
144 if (std::isnan(minimum) || minimum >= xmax_wksp) {
145 xmins.emplace_back(xmin_wksp);
147 xmins.emplace_back(minimum);
151 const auto maximum = xvalues.back();
152 if (std::isnan(maximum) || maximum <= xmin_wksp) {
153 xmaxs.emplace_back(xmax_wksp);
155 xmaxs.emplace_back(maximum);
161 if (xmins[i] >= xmaxs[i]) {
162 if (!msg.str().empty())
164 msg <<
"at wksp_index=" << i <<
" XMin >= XMax (" << xmins[i] <<
" >= " << xmaxs[i] <<
")";
197 int numBoundaries(0);
201 reqNumBoundaries -= 1;
203 expNumBoundaries += 1;
205 vector<double> params;
206 params.emplace_back(xmin);
207 params.emplace_back(0.);
208 params.emplace_back(xmax);
213 throw std::invalid_argument(
"Cannot calculate log of xmin=0");
215 throw std::invalid_argument(
"Cannot calculate log of xmax=0");
216 if (xmin < 0. && xmax > 0.) {
217 std::stringstream msg;
218 msg <<
"Cannot calculate logorithmic binning that changes sign (xmin=" << xmin <<
", xmax=" << xmax <<
")";
219 throw std::invalid_argument(msg.str());
222 const int MAX_ITER(100);
225 double delta = (log(xmax) - log(xmin)) /
static_cast<double>(
m_numBins);
228 for (
int numIter = 0; numIter < MAX_ITER; ++numIter) {
229 params[1] = -1. *
delta;
231 params[2] = xmax +
delta;
234 if (numBoundaries == expNumBoundaries) {
235 double diff = (xmax - xValues.back());
237 g_log.
debug() <<
"Didn't get the exact xmax value: [xmax - xValues.back()=" << diff
238 <<
"] [relative diff = " <<
fabs(100. * diff / xmax) <<
"%]\n";
239 g_log.
debug() <<
"Resetting final x-value to xmax\n";
240 *(xValues.rbegin()) = xmax;
243 }
else if (numBoundaries > expNumBoundaries)
245 delta *= (1. + shift);
251 delta *= (1. - shift);
258 params[1] = (xmax - xmin) /
static_cast<double>(reqNumBoundaries);
262 if (numBoundaries != expNumBoundaries) {
263 g_log.
warning() <<
"Did not generate the requested number of bins: generated " << numBoundaries <<
" requested "
264 << expNumBoundaries <<
"(xmin=" << xmin <<
", xmax=" << xmax <<
")\n";
278 bool inPlace = (inputWS == outputWS);
281 const auto numSpectra =
static_cast<int>(inputWS->getNumberHistograms());
293 throw std::runtime_error(
error);
295 bool common_limits =
true;
297 double xmin_common = xmins[0];
298 double xmax_common = xmaxs[0];
299 for (
size_t i = 1; i < xmins.size(); ++i) {
300 if (xmins[i] != xmin_common) {
301 common_limits =
false;
304 if (xmaxs[i] != xmax_common) {
305 common_limits =
false;
311 g_log.
debug() <<
"Common limits between all spectra\n";
313 g_log.
debug() <<
"Does not have common limits between all spectra\n";
318 if (inputEventWS !=
nullptr) {
321 g_log.
debug() <<
"Rebinning event workspace in place\n";
323 g_log.
debug() <<
"Rebinning event workspace out of place\n";
324 outputWS = inputWS->clone();
326 auto outputEventWS = std::dynamic_pointer_cast<EventWorkspace>(outputWS);
333 outputEventWS->setAllX(xValues);
336 Progress prog(
this, 0.0, 1.0, numSpectra);
340 for (
int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
343 const double delta = this->
determineBinning(xValues.mutableRawData(), xmins[wkspIndex], xmaxs[wkspIndex]);
344 g_log.
debug() <<
"delta[wkspindex=" << wkspIndex <<
"] = " <<
delta <<
" xmin=" << xmins[wkspIndex]
345 <<
" xmax=" << xmaxs[wkspIndex] <<
"\n";
346 outputEventWS->setHistogram(wkspIndex, xValues);
356 g_log.
information() <<
"Creating a Workspace2D from the EventWorkspace " << inputEventWS->getName() <<
".\n";
357 outputWS = create<DataObjects::Workspace2D>(*inputWS, numSpectra, HistogramData::BinEdges(
m_numBins + 1));
360 Progress prog(
this, 0.0, 1.0, numSpectra);
364 for (
int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
370 g_log.
debug() <<
"delta[wkspindex=" << wkspIndex <<
"] = " <<
delta <<
"\n";
371 outputWS->setBinEdges(wkspIndex, xValues);
374 const EventList &el = inputEventWS->getSpectrum(wkspIndex);
380 outputWS->mutableY(wkspIndex) = y_data;
381 outputWS->mutableE(wkspIndex) = e_data;
390 for (
int i = 1; i < inputWS->axes(); i++) {
391 outputWS->replaceAxis(i, std::unique_ptr<Axis>(inputWS->getAxis(i)->clone(outputWS.get())));
392 outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit();
396 for (
int i = 0; i < outputWS->axes(); ++i) {
397 outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit();
399 outputWS->setYUnit(inputEventWS->YUnit());
400 outputWS->setYUnitLabel(inputEventWS->YUnitLabel());
411 ChildAlg->initialize();
412 ChildAlg->setProperty(
"InputWorkspace", inputWS);
414 inputWS = ChildAlg->getProperty(
"OutputWorkspace");
422 if (inputWS->axes() > 1)
423 outputWS->replaceAxis(1, std::unique_ptr<Axis>(inputWS->getAxis(1)->clone(outputWS.get())));
425 Progress prog(
this, 0.0, 1.0, numSpectra);
427 for (
int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
431 const MantidVec &XValues = inputWS->readX(wkspIndex);
432 const MantidVec &YValues = inputWS->readY(wkspIndex);
433 const MantidVec &YErrors = inputWS->readE(wkspIndex);
437 MantidVec &YValues_new = outputWS->dataY(wkspIndex);
438 MantidVec &YErrors_new = outputWS->dataE(wkspIndex);
443 g_log.
debug() <<
"delta[wkspindex=" << wkspIndex <<
"] = " <<
delta <<
"\n";
448 }
catch (std::exception &ex) {
449 g_log.
error() <<
"Error in rebin function: " << ex.what() <<
'\n';
454 outputWS->setBinEdges(wkspIndex, XValues_new);
465 for (
int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
466 if (inputWS->hasMaskedBins(wkspIndex))
472 for (
int i = 0; i < outputWS->axes(); ++i) {
473 outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit();
479 ChildAlg->initialize();
482 outputWS = ChildAlg->getProperty(
"OutputWorkspace");
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
#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.
Helper class for reporting progress from algorithms.
A property class for workspaces.
void propagateMasks(const API::MatrixWorkspace_const_sptr &inputWS, const API::MatrixWorkspace_sptr &outputWS, int hist)
Takes the masks in the input workspace and apportions the weights into the new bins that overlap with...
ResampleX : TODO: DESCRIPTION.
void setOptions(const int numBins, const bool useLogBins, const bool isDist)
MADE PUBLIC FOR TESTING ONLY - DO NOT USE.
void exec() override
Execute the algorithm.
const std::string name() const override
Algorithm's name for identification.
const std::string category() const override
Algorithm's category for identification.
std::map< std::string, std::string > validateInputs() override
More complicated checks of parameters and their relations.
const std::string alias() const override
Algorithm's aliases.
double determineBinning(MantidVec &xValues, const double xmin, const double xmax)
MADE PUBLIC FOR TESTING ONLY - DO NOT USE.
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
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 debug(const std::string &msg)
Logs at debug level.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
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.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
string determineXMinMax(const MatrixWorkspace_sptr &inputWS, vector< double > &xmins, vector< double > &xmaxs)
Determine the min and max x-values for each spectrum and error check the pairs.
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.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
@ Input
An input workspace.
@ Output
An output workspace.