23using Mantid::HistogramData::Frequencies;
24using Mantid::HistogramData::FrequencyStandardDeviations;
25using Mantid::HistogramData::Histogram;
44 return "Creates a workspace of background data from a user-supplied set of "
45 "points. This workspace can then be subtracted from the original "
55 "Input workspace containing data and background");
57 "Table containing user-defined background points");
60 "Workspace containing background to be subtracted");
89 std::vector<size_t> blankRows;
90 const auto isZero = [](
const double n) {
return !(
fabs(
n) > std::numeric_limits<double>::epsilon()); };
91 for (
size_t i = table->rowCount() - 1; i > 0; i--) {
97 }
else if (isZero(
y)) {
98 blankRows.emplace_back(i);
101 for (
const auto &row : blankRows) {
102 table->removeRow(row);
106 std::vector<std::pair<std::string, bool>> sortArgs;
107 sortArgs.emplace_back(table->getColumn(0)->name(),
true);
108 table->sort(sortArgs);
119 const auto &xPoints = data->points(0);
122 if (background->Double(0, 0) > xPoints[0]) {
123 background->insertRow(0);
125 firstRow << xPoints.front() << background->
Double(0, 1);
128 const size_t endIndex = background->rowCount() - 1;
129 if (background->Double(endIndex, 0) < xPoints.back()) {
131 lastRow << xPoints.back() << background->
Double(endIndex, 1);
146 auto outputWS = data->clone();
148 const auto &xPoints = outputWS->points(0);
149 const auto &xBinEdges = outputWS->binEdges(0);
150 std::vector<double> yBackground;
151 std::vector<double> eBackground(xPoints.size(), 0);
155 for (
const auto &
x : xPoints) {
156 const double y = lerp.value(
x);
157 yBackground.emplace_back(
y);
160 auto histogram = outputWS->histogram(0);
161 if (histogram.yMode() == Histogram::YMode::Frequencies) {
162 histogram.setFrequencies(yBackground);
163 histogram.setFrequencyStandardDeviations(eBackground);
165 if (data->isHistogramData() &&
169 histogram.setCounts(Frequencies(yBackground), xBinEdges);
170 histogram.setCountStandardDeviations(FrequencyStandardDeviations(eBackground), xBinEdges);
172 histogram.setCounts(yBackground);
173 histogram.setCountStandardDeviations(eBackground);
178 for (
size_t spec = 0; spec < outputWS->getNumberHistograms(); spec++) {
180 outputWS->setHistogram(spec, histogram);
201 const auto xColumn = background->getColumn(0);
202 const auto yColumn = background->getColumn(1);
203 for (
size_t i = 0; i < background->rowCount(); i++) {
204 double x = xColumn->cell<
double>(i);
205 double y = yColumn->cell<
double>(i);
220 std::map<std::string, std::string> errors;
222 const static std::string pointsProp =
"BackgroundPoints", inputProp =
"InputWorkspace";
225 if (pointsTable->columnCount() != 2) {
226 errors[pointsProp] =
"Table of points must have two columns (X, Y)";
228 for (
size_t col = 0; col < pointsTable->columnCount(); col++) {
229 const std::string colType = pointsTable->getColumn(col)->type();
230 if (colType !=
"double" && colType !=
"int") {
231 errors[pointsProp] =
"Table of points must have numeric columns";
234 if (pointsTable->rowCount() < 2) {
235 errors[pointsProp] =
"Table of points must contain at least two points";
241 if (inputWS->getNumberHistograms() == 0 || inputWS->blocksize() < 2) {
242 errors[inputProp] =
"Input workspace must contain some data";
244 if (!inputWS->isCommonBins()) {
245 errors[inputProp] =
"Input workspace must have common bins";
#define DECLARE_ALGORITHM(classname)
IPeaksWorkspace_sptr workspace
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.
TableRow represents a row in a TableWorkspace.
double & Double(size_t col)
Returns a reference to the element in position col if its type is double.
A property class for workspaces.
CreateUserDefinedBackground : Given an input workspace containing data with a background and a table ...
void init() override
Declare properties and initialise algorithm.
API::MatrixWorkspace_sptr createBackgroundWorkspace(const API::ITableWorkspace_const_sptr &background, const API::MatrixWorkspace_const_sptr &data) const
Create new background workspace with data interpolated from table.
const std::string category() const override
Category algorithm belongs to.
void exec() override
Execute algorithm.
static const std::string AUTODISTRIBUTIONKEY
Key name for "normalize histogram to bin width" option on plots.
void cleanUpTable(API::ITableWorkspace_sptr &table) const
Sort table and remove blank rows.
void extendBackgroundToData(API::ITableWorkspace_sptr &background, const API::MatrixWorkspace_const_sptr &data) const
Extend background to cover range of data.
std::map< std::string, std::string > validateInputs() override
Verify input properties and return errors.
const std::string summary() const override
Description of algorithm.
int version() const override
Version number.
Kernel::Interpolation getInterpolator(const API::ITableWorkspace_const_sptr &background, const API::MatrixWorkspace_const_sptr &workspace) const
Set up and return an interpolator object ready for use.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
Provide interpolation over a series of points.
void setMethod(const std::string &method)
set interpolation method
void setXUnit(const std::string &unit)
set x-axis unit
void setYUnit(const std::string &unit)
set y-axis unit
void addPoint(const double &xx, const double &yy)
add data point
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< const ITableWorkspace > ITableWorkspace_const_sptr
shared pointer to Mantid::API::ITableWorkspace (const version)
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
Describes the direction (within an algorithm) of a Property.
@ Input
An input workspace.
@ Output
An output workspace.