21using namespace DataObjects;
22using namespace Kernel;
23using HistogramData::BinEdges;
26using std::stringstream;
47 "An output workspace.");
50 "A comma separated list of the XMin for every spectrum. (Optional)");
52 "A comma separated list of the XMax for every spectrum. (Optional)");
54 auto min = std::make_shared<BoundedValidator<int>>();
56 declareProperty(
"NumberBins", 0, min,
"Number of bins to split up each spectrum into.");
57 declareProperty(
"LogBinning",
false,
"Use logarithmic binning. If false use constant step sizes.");
60 "Keep the output workspace as an EventWorkspace, if the "
61 "input has events (default).\n"
62 "If the input and output EventWorkspace names are the same, "
63 "only the X bins are set, which is very quick.\n"
64 "If false, then the workspace gets converted to a "
65 "Workspace2D histogram.");
72 map<string, string> errors;
75 if ((!xmins.empty()) && (!xmaxs.empty())) {
76 if (xmins.size() != xmaxs.size()) {
78 msg <<
"XMin and XMax do not define same number of spectra (" << xmins.size() <<
" != " << xmaxs.size() <<
")";
79 errors.emplace(
"XMax", msg.str());
81 size_t size = xmins.size();
82 for (
size_t i = 0; i < size; ++i) {
83 if (xmins[i] >= xmaxs[i]) {
85 msg <<
"XMin (" << xmins[i] <<
") cannot be greater than XMax (" << xmaxs[i] <<
")";
86 errors.emplace(
"XMax", msg.str());
108 const size_t numSpectra = inputWS->getNumberHistograms();
111 if (xmins.size() == 1 && numSpectra > xmins.size()) {
112 const double value = xmins.front();
113 xmins.insert(xmins.end(), numSpectra - xmins.size(),
value);
115 if (xmaxs.size() == 1 && numSpectra > xmaxs.size()) {
116 const double value = xmaxs.front();
117 xmaxs.insert(xmaxs.end(), numSpectra - xmaxs.size(),
value);
121 const bool updateXMins = xmins.empty();
122 const bool updateXMaxs = xmaxs.empty();
127 double xmin_wksp = inputWS->getXMin();
128 double xmax_wksp = inputWS->getXMax();
130 if (inputEventWS !=
nullptr && inputEventWS->getNumberEvents() > 0) {
131 xmin_wksp = inputEventWS->getTofMin();
132 xmax_wksp = inputEventWS->getTofMax();
135 for (
size_t i = 0; i < numSpectra; ++i) {
137 if (updateXMins || updateXMaxs) {
138 const auto &xvalues = inputWS->x(i);
140 const auto minimum = xvalues.front();
141 if (std::isnan(minimum) || minimum >= xmax_wksp) {
142 xmins.emplace_back(xmin_wksp);
144 xmins.emplace_back(minimum);
148 const auto maximum = xvalues.back();
149 if (std::isnan(maximum) || maximum <= xmin_wksp) {
150 xmaxs.emplace_back(xmax_wksp);
152 xmaxs.emplace_back(maximum);
158 if (xmins[i] >= xmaxs[i]) {
159 if (!msg.str().empty())
161 msg <<
"at wksp_index=" << i <<
" XMin >= XMax (" << xmins[i] <<
" >= " << xmaxs[i] <<
")";
194 int numBoundaries(0);
198 reqNumBoundaries -= 1;
200 expNumBoundaries += 1;
202 vector<double> params;
203 params.emplace_back(xmin);
204 params.emplace_back(0.);
205 params.emplace_back(xmax);
210 throw std::invalid_argument(
"Cannot calculate log of xmin=0");
212 throw std::invalid_argument(
"Cannot calculate log of xmax=0");
213 if (xmin < 0. && xmax > 0.) {
214 std::stringstream msg;
215 msg <<
"Cannot calculate logorithmic binning that changes sign (xmin=" << xmin <<
", xmax=" << xmax <<
")";
216 throw std::invalid_argument(msg.str());
219 const int MAX_ITER(100);
222 double delta = (log(xmax) - log(xmin)) /
static_cast<double>(
m_numBins);
225 for (
int numIter = 0; numIter < MAX_ITER; ++numIter) {
226 params[1] = -1. *
delta;
228 params[2] = xmax +
delta;
231 if (numBoundaries == expNumBoundaries) {
232 double diff = (xmax - xValues.back());
234 g_log.
debug() <<
"Didn't get the exact xmax value: [xmax - xValues.back()=" << diff
235 <<
"] [relative diff = " <<
fabs(100. * diff / xmax) <<
"%]\n";
236 g_log.
debug() <<
"Resetting final x-value to xmax\n";
237 *(xValues.rbegin()) = xmax;
240 }
else if (numBoundaries > expNumBoundaries)
242 delta *= (1. + shift);
248 delta *= (1. - shift);
255 params[1] = (xmax - xmin) /
static_cast<double>(reqNumBoundaries);
259 if (numBoundaries != expNumBoundaries) {
260 g_log.
warning() <<
"Did not generate the requested number of bins: generated " << numBoundaries <<
" requested "
261 << expNumBoundaries <<
"(xmin=" << xmin <<
", xmax=" << xmax <<
")\n";
275 bool inPlace = (inputWS == outputWS);
278 const auto numSpectra =
static_cast<int>(inputWS->getNumberHistograms());
290 throw std::runtime_error(
error);
292 bool common_limits =
true;
294 double xmin_common = xmins[0];
295 double xmax_common = xmaxs[0];
296 for (
size_t i = 1; i < xmins.size(); ++i) {
297 if (xmins[i] != xmin_common) {
298 common_limits =
false;
301 if (xmaxs[i] != xmax_common) {
302 common_limits =
false;
308 g_log.
debug() <<
"Common limits between all spectra\n";
310 g_log.
debug() <<
"Does not have common limits between all spectra\n";
315 if (inputEventWS !=
nullptr) {
318 g_log.
debug() <<
"Rebinning event workspace in place\n";
320 g_log.
debug() <<
"Rebinning event workspace out of place\n";
321 outputWS = inputWS->clone();
323 auto outputEventWS = std::dynamic_pointer_cast<EventWorkspace>(outputWS);
330 outputEventWS->setAllX(xValues);
333 Progress prog(
this, 0.0, 1.0, numSpectra);
337 for (
int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
340 const double delta = this->
determineBinning(xValues.mutableRawData(), xmins[wkspIndex], xmaxs[wkspIndex]);
341 g_log.
debug() <<
"delta[wkspindex=" << wkspIndex <<
"] = " <<
delta <<
" xmin=" << xmins[wkspIndex]
342 <<
" xmax=" << xmaxs[wkspIndex] <<
"\n";
343 outputEventWS->setHistogram(wkspIndex, xValues);
353 g_log.
information() <<
"Creating a Workspace2D from the EventWorkspace " << inputEventWS->getName() <<
".\n";
354 outputWS = create<DataObjects::Workspace2D>(*inputWS, numSpectra, HistogramData::BinEdges(
m_numBins + 1));
357 Progress prog(
this, 0.0, 1.0, numSpectra);
361 for (
int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
367 g_log.
debug() <<
"delta[wkspindex=" << wkspIndex <<
"] = " <<
delta <<
"\n";
368 outputWS->setBinEdges(wkspIndex, xValues);
371 const EventList &el = inputEventWS->getSpectrum(wkspIndex);
377 outputWS->mutableY(wkspIndex) = y_data;
378 outputWS->mutableE(wkspIndex) = e_data;
387 for (
int i = 1; i < inputWS->axes(); i++) {
388 outputWS->replaceAxis(i, std::unique_ptr<Axis>(inputWS->getAxis(i)->clone(outputWS.get())));
389 outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit();
393 for (
int i = 0; i < outputWS->axes(); ++i) {
394 outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit();
396 outputWS->setYUnit(inputEventWS->YUnit());
397 outputWS->setYUnitLabel(inputEventWS->YUnitLabel());
408 ChildAlg->initialize();
409 ChildAlg->setProperty(
"InputWorkspace", inputWS);
411 inputWS = ChildAlg->getProperty(
"OutputWorkspace");
419 if (inputWS->axes() > 1)
420 outputWS->replaceAxis(1, std::unique_ptr<Axis>(inputWS->getAxis(1)->clone(outputWS.get())));
422 Progress prog(
this, 0.0, 1.0, numSpectra);
424 for (
int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
428 const MantidVec &XValues = inputWS->readX(wkspIndex);
429 const MantidVec &YValues = inputWS->readY(wkspIndex);
430 const MantidVec &YErrors = inputWS->readE(wkspIndex);
434 MantidVec &YValues_new = outputWS->dataY(wkspIndex);
435 MantidVec &YErrors_new = outputWS->dataE(wkspIndex);
440 g_log.
debug() <<
"delta[wkspindex=" << wkspIndex <<
"] = " <<
delta <<
"\n";
445 }
catch (std::exception &ex) {
446 g_log.
error() <<
"Error in rebin function: " << ex.what() <<
'\n';
451 outputWS->setBinEdges(wkspIndex, XValues_new);
462 for (
int wkspIndex = 0; wkspIndex < numSpectra; ++wkspIndex) {
463 if (inputWS->hasMaskedBins(wkspIndex))
469 for (
int i = 0; i < outputWS->axes(); ++i) {
470 outputWS->getAxis(i)->unit() = inputWS->getAxis(i)->unit();
476 ChildAlg->initialize();
479 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, 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...
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.
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...
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
Kernel::Logger g_log("DetermineSpinStateOrder")
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.