26using namespace Kernel;
31 : m_conversionConstant(0.), m_inputWS(), m_LRef(0.), m_Tmin(0.), m_Tmax(0.), m_XSize(0) {}
35 auto wsValidator = std::make_shared<CompositeValidator>();
42 "A workspace with x values in units of TOF and y values in counts");
44 "The name of the workspace to be created as the output of the algorithm");
46 auto validator = std::make_shared<BoundedValidator<double>>();
47 validator->setLower(0.01);
48 declareProperty(
"LRef", 0.0, validator,
"The length of the reference flight path (in metres)");
53 const double TOFisinMicroseconds = 1e6;
54 const double toAngstroms = 1e10;
69 const auto numberOfSpectra =
static_cast<int>(
m_inputWS->getNumberHistograms());
70 g_log.
debug() <<
"Number of spectra in input workspace: " << numberOfSpectra <<
'\n';
90 const auto &spectrumInfo =
m_inputWS->spectrumInfo();
91 const double L1 = spectrumInfo.l1();
93 m_progress = std::make_unique<Progress>(
this, 0.0, 1.0, numberOfSpectra);
95 for (
int i = 0; i < numberOfSpectra; ++i) {
96 if (!spectrumInfo.hasDetectors(i)) {
98 g_log.
debug() <<
"Detector information for workspace index " << i <<
" is not available.\n";
99 tempWS->mutableX(i) = 0.0;
100 tempWS->mutableY(i) = 0.0;
101 tempWS->mutableE(i) = 0.0;
113 const double Ld = L1 + spectrumInfo.l2(i);
114 std::vector<double> newX;
115 const std::vector<int> rangeBounds = this->
unwrapX(newX, i, Ld);
118 std::vector<double> newY;
119 std::vector<double> newE;
120 this->
unwrapYandE(tempWS, i, rangeBounds, newY, newE);
123 auto histogram = tempWS->histogram(i);
124 tempWS->setHistogram(i, Mantid::HistogramData::BinEdges(std::move(newX)),
125 Mantid::HistogramData::Counts(std::move(newY)),
126 Mantid::HistogramData::CountStandardDeviations(std::move(newE)));
130 if (!spectrumInfo.isMonitor(i)) {
131 const size_t XLen = tempWS->x(i).size();
139 if (numberOfSpectra > 1) {
146 g_log.
debug() <<
"Rebinned workspace has " << outputWS->getNumberHistograms() <<
" histograms of "
147 << outputWS->blocksize() <<
" bins each\n";
168 std::vector<int> binRange(3, -1);
176 std::vector<double> tempX_L;
184 const auto &xdata =
m_inputWS->x(spectrum);
187 for (
unsigned int bin = 0; bin <
m_XSize; ++bin) {
190 const double tof = xdata[bin];
194 tempX_L.emplace_back(wavelength);
197 if (binRange[0] == -1)
205 newX.emplace_back(wavelength);
207 if (tof ==
m_Tmax && std::abs(wavelength - tempX_L.front()) < 1.0e-5)
211 if (binRange[2] == -1)
220 binRange[0] = binLimits.first;
221 binRange[1] = binLimits.second;
227 if (join->isDefault()) {
228 g_log.
information() <<
"Joining wavelength: " << tempX_L.front() <<
" Angstrom\n";
233 newX.insert(newX.end(), tempX_L.begin(), tempX_L.end());
242 const double &Ld, std::vector<double> &tempX) {
246 const double minT =
m_Tmin + Dt;
247 const double maxT =
m_Tmax - Dt;
253 <<
"). Current spectrum will not be unwrapped.\n";
254 return std::make_pair(0,
static_cast<int>(xdata.size() - 1));
257 int min = 0, max =
static_cast<int>(xdata.size());
258 for (
unsigned int j = 0; j <
m_XSize; ++j) {
259 const double T = xdata[j];
262 tempX.erase(tempX.begin());
263 }
else if (T > maxT) {
264 tempX.erase(tempX.end() - (max - j), tempX.end());
269 return std::make_pair(min, max);
282 const std::vector<int> &rangeBounds, std::vector<double> &newY,
283 std::vector<double> &newE) {
285 std::vector<double> &
Y = newY;
286 std::vector<double> &E = newE;
288 const auto &YIn =
m_inputWS->y(spectrum);
289 const auto &EIn =
m_inputWS->e(spectrum);
290 if (rangeBounds[2] != -1) {
292 Y.assign(YIn.begin() + rangeBounds[2], YIn.end());
293 E.assign(EIn.begin() + rangeBounds[2], EIn.end());
295 if (
m_inputWS->hasMaskedBins(spectrum)) {
297 MatrixWorkspace::MaskList::const_iterator it;
298 for (it = inputMasks.begin(); it != inputMasks.end(); ++it) {
299 if (
static_cast<int>((*it).first) >= rangeBounds[2])
300 tempWS->flagMasked(spectrum, (*it).first - rangeBounds[2], (*it).second);
309 if (rangeBounds[0] != -1 && rangeBounds[1] > 0) {
311 auto YStart = YIn.begin();
312 auto EStart = EIn.begin();
313 Y.insert(
Y.end(), YStart + rangeBounds[0], YStart + rangeBounds[1]);
314 E.insert(E.end(), EStart + rangeBounds[0], EStart + rangeBounds[1]);
316 if (
m_inputWS->hasMaskedBins(spectrum)) {
318 for (
const auto &inputMask : inputMasks) {
319 const auto maskIndex =
static_cast<int>(inputMask.first);
320 if (maskIndex >= rangeBounds[0] && maskIndex < rangeBounds[1])
321 tempWS->flagMasked(spectrum, maskIndex - rangeBounds[0], inputMask.second);
336 const double &max,
const size_t &numBins) {
338 const double step = (max - min) /
static_cast<double>(numBins);
343 childAlg->setPropertyValue(
"OutputWorkspace",
"Anonymous");
346 std::vector<double> paramArray = {min, step, max};
347 childAlg->setProperty<std::vector<double>>(
"Params", paramArray);
348 g_log.
debug() <<
"Rebinning unwrapped data into " << numBins <<
" bins of width " << step
349 <<
" Angstroms, running from " << min <<
" to " << max <<
'\n';
351 childAlg->executeAsChildAlg();
352 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
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.