27using namespace Kernel;
32 : m_conversionConstant(0.), m_inputWS(), m_LRef(0.), m_Tmin(0.), m_Tmax(0.), m_XSize(0) {}
36 auto wsValidator = std::make_shared<CompositeValidator>();
43 "A workspace with x values in units of TOF and y values in counts");
45 "The name of the workspace to be created as the output of the algorithm");
47 auto validator = std::make_shared<BoundedValidator<double>>();
48 validator->setLower(0.01);
49 declareProperty(
"LRef", 0.0, validator,
"The length of the reference flight path (in metres)");
54 const double TOFisinMicroseconds = 1e6;
55 const double toAngstroms = 1e10;
70 const auto numberOfSpectra =
static_cast<int>(
m_inputWS->getNumberHistograms());
71 g_log.
debug() <<
"Number of spectra in input workspace: " << numberOfSpectra <<
'\n';
85 tempWS->getAxis(0)->unit() = UnitFactory::Instance().create(
"Wavelength");
91 const auto &spectrumInfo =
m_inputWS->spectrumInfo();
92 const double L1 = spectrumInfo.l1();
94 m_progress = std::make_unique<Progress>(
this, 0.0, 1.0, numberOfSpectra);
96 for (
int i = 0; i < numberOfSpectra; ++i) {
97 if (!spectrumInfo.hasDetectors(i)) {
99 g_log.
debug() <<
"Detector information for workspace index " << i <<
" is not available.\n";
100 tempWS->mutableX(i) = 0.0;
101 tempWS->mutableY(i) = 0.0;
102 tempWS->mutableE(i) = 0.0;
114 const double Ld = L1 + spectrumInfo.l2(i);
115 std::vector<double> newX;
116 const std::vector<int> rangeBounds = this->
unwrapX(newX, i, Ld);
119 std::vector<double> newY;
120 std::vector<double> newE;
121 this->
unwrapYandE(tempWS, i, rangeBounds, newY, newE);
124 auto histogram = tempWS->histogram(i);
125 tempWS->setHistogram(i, Mantid::HistogramData::BinEdges(std::move(newX)),
126 Mantid::HistogramData::Counts(std::move(newY)),
127 Mantid::HistogramData::CountStandardDeviations(std::move(newE)));
131 if (!spectrumInfo.isMonitor(i)) {
132 const size_t XLen = tempWS->x(i).size();
140 if (numberOfSpectra > 1) {
147 g_log.
debug() <<
"Rebinned workspace has " << outputWS->getNumberHistograms() <<
" histograms of "
148 << outputWS->blocksize() <<
" bins each\n";
169 std::vector<int> binRange(3, -1);
177 std::vector<double> tempX_L;
185 const auto &xdata =
m_inputWS->x(spectrum);
188 for (
unsigned int bin = 0; bin <
m_XSize; ++bin) {
191 const double tof = xdata[bin];
195 tempX_L.emplace_back(wavelength);
198 if (binRange[0] == -1)
206 newX.emplace_back(wavelength);
212 if (binRange[2] == -1)
221 binRange[0] = binLimits.first;
222 binRange[1] = binLimits.second;
228 if (join->isDefault()) {
229 g_log.
information() <<
"Joining wavelength: " << tempX_L.front() <<
" Angstrom\n";
234 newX.insert(newX.end(), tempX_L.begin(), tempX_L.end());
243 const double &Ld, std::vector<double> &tempX) {
247 const double minT =
m_Tmin + Dt;
248 const double maxT =
m_Tmax - Dt;
254 <<
"). Current spectrum will not be unwrapped.\n";
255 return std::make_pair(0,
static_cast<int>(xdata.size() - 1));
258 int min = 0, max =
static_cast<int>(xdata.size());
259 for (
unsigned int j = 0; j <
m_XSize; ++j) {
260 const double T = xdata[j];
263 tempX.erase(tempX.begin());
264 }
else if (T > maxT) {
265 tempX.erase(tempX.end() - (max - j), tempX.end());
270 return std::make_pair(min, max);
283 const std::vector<int> &rangeBounds, std::vector<double> &newY,
284 std::vector<double> &newE) {
286 std::vector<double> &
Y = newY;
287 std::vector<double> &E = newE;
289 const auto &YIn =
m_inputWS->y(spectrum);
290 const auto &EIn =
m_inputWS->e(spectrum);
291 if (rangeBounds[2] != -1) {
293 Y.assign(YIn.begin() + rangeBounds[2], YIn.end());
294 E.assign(EIn.begin() + rangeBounds[2], EIn.end());
296 if (
m_inputWS->hasMaskedBins(spectrum)) {
298 MatrixWorkspace::MaskList::const_iterator it;
299 for (it = inputMasks.begin(); it != inputMasks.end(); ++it) {
300 if (
static_cast<int>((*it).first) >= rangeBounds[2])
301 tempWS->flagMasked(spectrum, (*it).first - rangeBounds[2], (*it).second);
310 if (rangeBounds[0] != -1 && rangeBounds[1] > 0) {
312 auto YStart = YIn.begin();
313 auto EStart = EIn.begin();
314 Y.insert(
Y.end(), YStart + rangeBounds[0], YStart + rangeBounds[1]);
315 E.insert(E.end(), EStart + rangeBounds[0], EStart + rangeBounds[1]);
317 if (
m_inputWS->hasMaskedBins(spectrum)) {
319 for (
const auto &inputMask : inputMasks) {
320 const auto maskIndex =
static_cast<int>(inputMask.first);
321 if (maskIndex >= rangeBounds[0] && maskIndex < rangeBounds[1])
322 tempWS->flagMasked(spectrum, maskIndex - rangeBounds[0], inputMask.second);
337 const double &max,
const size_t &numBins) {
339 const double step = (max - min) /
static_cast<double>(numBins);
344 childAlg->setPropertyValue(
"OutputWorkspace",
"Anonymous");
347 std::vector<double> paramArray = {min, step, max};
348 childAlg->setProperty<std::vector<double>>(
"Params", paramArray);
349 g_log.
debug() <<
"Rebinning unwrapped data into " << numBins <<
" bins of width " << step
350 <<
" Angstroms, running from " << min <<
" to " << max <<
'\n';
352 childAlg->executeAsChildAlg();
353 return childAlg->getProperty(
"OutputWorkspace");
#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.
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.
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.
std::map< size_t, double > MaskList
Masked bins for each spectrum are stored as a set of pairs containing <bin index, weight>
A validator which checks that a workspace contains raw counts in its bins.
A property class for workspaces.
A validator which checks that the unit of the workspace referred to by a WorkspaceProperty is the exp...
UnwrapMonitor()
Default constructor.
API::MatrixWorkspace_const_sptr m_inputWS
to wavelength
double m_LRef
The 'reference' flightpath.
double m_Tmin
The start of the time-of-flight frame.
void init() override
Initialisation method.
size_t m_XSize
The size of the X vectors in the input workspace.
std::unique_ptr< API::Progress > m_progress
Progress reporting.
API::MatrixWorkspace_sptr rebin(const API::MatrixWorkspace_sptr &workspace, const double &min, const double &max, const size_t &numBins)
Rebins the data into common bins of wavelength.
std::pair< int, int > handleFrameOverlapped(const Mantid::HistogramData::HistogramX &xdata, const double &Ld, std::vector< double > &tempX)
Deals with the (rare) case where the flightpath is longer than the reference Note that in this case b...
double m_Tmax
The end of the time-of-flight frame.
const std::vector< int > unwrapX(std::vector< double > &newX, const int &spectrum, const double &Ld)
Unwraps an X array, converting the units to wavelength along the way.
double m_conversionConstant
The constant used in the conversion from TOF.
void unwrapYandE(const API::MatrixWorkspace_sptr &tempWS, const int &spectrum, const std::vector< int > &rangeBounds, std::vector< double > &newY, std::vector< double > &newE)
Unwraps the Y & E vectors of a spectrum according to the ranges found in unwrapX.
void exec() override
Executes the algorithm.
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 warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
Base class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
MANTID_KERNEL_DLL bool withinAbsoluteDifference(T const x, T const y, S const tolerance)
Test whether x, y are within absolute tolerance tol.
static constexpr double NeutronMass
Mass of the neutron in kg.
static constexpr double h
Planck constant in J*s.
@ Input
An input workspace.
@ Output
An output workspace.