26using Mantid::HistogramData::HistogramE;
27using Mantid::HistogramData::HistogramX;
28using Mantid::HistogramData::HistogramY;
35using namespace Kernel;
42 auto sourceValidator = std::make_shared<CompositeValidator>();
46 "Workspace containing the input data");
48 "The name to give the output workspace");
50 auto vsValidator = std::make_shared<CompositeValidator>();
54 "An optional histogram workspace in the units of TOF defining background "
55 "for removal during rebinning."
56 "The workspace has to have single value or contain the same number of "
57 "spectra as the \"InputWorkspace\" and single Y value per each spectra,"
58 "representing flat background in the background time region. "
59 "If such workspace is present, the value of the flat background provided "
60 "by this workspace is removed "
61 "from each spectra of the rebinned workspace. This works for histogram "
62 "and event workspace when events are not retained "
63 "but actually useful mainly for removing background while rebinning an "
64 "event workspace in the units different from TOF.");
68 std::make_shared<Kernel::StringListValidator>(dE_modes),
69 "The energy conversion mode used to define the conversion "
70 "from the units of the InputWorkspace to TOF",
73 "When background is subtracted, signals in some time "
74 "channels may become negative.\n"
75 "If this option is true, signal in such bins is nullified "
76 "and the module of the removed signal"
77 "is added to the error. If false, the negative signal and "
78 "correspondent errors remain unchanged",
94 bool nullifyNegative =
getProperty(
"NullifyNegativeValues");
96 if (!(bkgWksp->getNumberHistograms() == 1 || inputWS->getNumberHistograms() == bkgWksp->getNumberHistograms())) {
97 throw std::invalid_argument(
" Background Workspace: " + bkgWksp->getName() +
98 " should have the same number of spectra as "
99 "source workspace or be a single histogram "
107 bool inPlace = (inputWS == outputWS);
109 const auto histnumber =
static_cast<int>(inputWS->getNumberHistograms());
120 Progress prog(
this, 0.0, 1.0, histnumber);
122 for (
int hist = 0; hist < histnumber; ++hist) {
125 auto &XValues = outputWS->mutableX(hist);
128 auto &YValues = outputWS->mutableY(hist);
129 auto &YErrors = outputWS->mutableE(hist);
148 : m_WSUnit(), m_bgWs(), m_wkWS(), m_spectrumInfo(nullptr), m_pgLog(nullptr), m_inPlace(true),
149 m_singleValueBackground(false), m_NBg(0), m_dtBg(1), m_ErrSq(0), m_Emode(
DeltaEMode::Elastic),
150 m_nullifyNegative(false), m_previouslyRemovedBkgMode(false) {}
166 Kernel::Logger *pLog,
int nThreads,
bool inPlace,
bool nullifyNegative) {
174 std::string bgUnits = bkgWS->getAxis(0)->unit()->unitID();
175 if (bgUnits !=
"TOF")
176 throw std::invalid_argument(
" Background Workspace: " + bkgWS->getName() +
" should be in the units of TOF");
178 if (!(bkgWS->getNumberHistograms() == 1 || sourceWS->getNumberHistograms() == bkgWS->getNumberHistograms()))
179 throw std::invalid_argument(
" Background Workspace: " + bkgWS->getName() +
180 " should have the same number of spectra as "
181 "source workspace or be a single histogram "
184 auto WSUnit = sourceWS->getAxis(0)->unit();
186 throw std::invalid_argument(
" Source Workspace: " + sourceWS->getName() +
" should have units");
194 for (
int i = 0; i < nThreads; i++) {
195 m_WSUnit.emplace_back(std::unique_ptr<Kernel::Unit>(WSUnit->clone()));
199 if (bkgWS->getNumberHistograms() <= 1)
202 const auto &dataX = bkgWS->x(0);
203 const auto &dataY = bkgWS->y(0);
204 const auto &dataE = bkgWS->e(0);
206 m_dtBg = dataX[1] - dataX[0];
227 int threadNum)
const {
229 double dtBg, IBg, ErrBgSq;
235 auto &dataX =
m_bgWs->x(nHist);
236 auto &dataY =
m_bgWs->y(nHist);
237 auto &dataE =
m_bgWs->e(nHist);
238 dtBg = (dataX[1] - dataX[0]);
240 ErrBgSq = dataE[0] * dataE[0];
247 auto &XValues =
m_wkWS->x(nHist);
248 auto &YValues =
m_wkWS->y(nHist);
249 auto &YErrors =
m_wkWS->e(nHist);
257 x_data[0] = XValues[0];
259 for (
size_t i = 0; i < y_data.size(); i++) {
260 double X = XValues[i + 1];
262 double Jack = std::fabs((tof2 - tof1) / dtBg);
263 double normBkgrnd = IBg * Jack;
264 double errBkgSq = ErrBgSq * Jack * Jack;
267 y_data[i] -= normBkgrnd;
271 e_data[i] = std::sqrt((errBkgSq + e_data[i] * e_data[i]));
274 y_data[i] = YValues[i] - normBkgrnd;
275 e_data[i] = std::sqrt((errBkgSq + YErrors[i] * YErrors[i]));
281 double prev_rem_bkg = -y_data[i];
282 e_data[i] = e_data[i] > prev_rem_bkg ? e_data[i] : prev_rem_bkg;
287 e_data[i] = e_data[i] > normBkgrnd ? e_data[i] : normBkgrnd;
297 m_pgLog->
debug() <<
" Can not remove background for the spectra with number (id)" << nHist;
#define DECLARE_ALGORITHM(classname)
#define PARALLEL_THREAD_NUMBER
#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_GET_MAX_THREADS
#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.
A validator which checks that a workspace contains histogram data (the default) or point data as requ...
A validator which checks that a workspace has a valid instrument.
Helper class for reporting progress from algorithms.
double l1() const
Returns L1 (distance from source to sample).
void getDetectorValues(const Kernel::Unit &inputUnit, const Kernel::Unit &outputUnit, const Kernel::DeltaEMode::Type emode, const bool signedTheta, int64_t wsIndex, Kernel::UnitParametersMap &pmap) const
Get the detector values relevant to unit conversion for a workspace index.
A property class for workspaces.
A validator which checks that the unit of the workspace referred to by a WorkspaceProperty is the exp...
Kernel::DeltaEMode::Type m_Emode
const API::SpectrumInfo * m_spectrumInfo
std::vector< std::unique_ptr< Kernel::Unit > > m_WSUnit
bool m_singleValueBackground
bool m_previouslyRemovedBkgMode
void removeBackground(int nHist, HistogramData::HistogramX &x_data, HistogramData::HistogramY &y_data, HistogramData::HistogramE &e_data, int threadNum=0) const
Method removes background from vectors which represent a histogram data for a single spectra.
API::MatrixWorkspace_const_sptr m_wkWS
void initialize(const API::MatrixWorkspace_const_sptr &bkgWS, const API::MatrixWorkspace_sptr &sourceWS, Kernel::DeltaEMode::Type emode, Kernel::Logger *pLog=nullptr, int nThreads=1, bool inPlace=true, bool nullifyNegative=false)
Initialization method:
BackgroundHelper()
Constructor.
API::MatrixWorkspace_const_sptr m_bgWs
void init() override
Initialization method.
void exec() override
Executes the rebin algorithm.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
BackgroundHelper m_BackgroundHelper
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug 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...
The base units (abstract) class.
virtual double singleToTOF(const double x) const =0
Convert a single X value to TOF.
void initialize(const double &_l1, const int &_emode, const UnitParametersMap ¶ms)
Initialize the unit to perform conversion using singleToTof() and singleFromTof()
Time of flight in microseconds.
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
std::unordered_map< UnitParams, double > UnitParametersMap
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.
Generate a tableworkspace to store the calibration results.
Defines the possible energy transfer modes:
static const std::vector< std::string > availableTypes()
Returns the string list of available modes.
static Type fromString(const std::string &modeStr)
Returns the emode from the given string.
Type
Define the available energy transfer modes It is important to assign enums proper numbers,...
@ Input
An input workspace.
@ Output
An output workspace.