21#include <boost/function.hpp>
31using namespace Kernel;
33using namespace DataObjects;
34using namespace HistogramData;
50 return "Performs a unit change on the X values of a workspace";
56 auto wsValidator = std::make_shared<CompositeValidator>();
61 "Name of the input workspace");
63 "Name of the output workspace, can be the same as the input");
65 "The name of the units to convert to (must be one of those "
70 "Name of a TableWorkspace containing the detector parameters "
71 "to use instead of the IDF.");
74 "If true (default is false), rebins after conversion to "
75 "ensure that all spectra in the output workspace\n"
76 "have identical bin boundaries. This option is not "
78 "http://www.mantidproject.org/ConvertUnits).");
81 "When checked, if the Input Workspace contains Points\n"
82 "the algorithm ConvertToHistogram will be run to convert\n"
83 "the Points to Bins. The Output Workspace will contains Bins.");
102 using namespace Geometry;
114 auto spectraColumnTmp = paramWS->getColumn(
"spectra");
115 auto l1ColumnTmp = paramWS->getColumn(
"l1");
116 auto l2ColumnTmp = paramWS->getColumn(
"l2");
117 auto twoThetaColumnTmp = paramWS->getColumn(
"twotheta");
118 auto efixedColumnTmp = paramWS->getColumn(
"efixed");
119 auto emodeColumnTmp = paramWS->getColumn(
"emode");
125 const auto &l1Column = paramWS->getColVector<
double>(
"l1");
126 const auto &l2Column = paramWS->getColVector<
double>(
"l2");
127 const auto &twoThetaColumn = paramWS->getColVector<
double>(
"twotheta");
128 const auto &efixedColumn = paramWS->getColVector<
double>(
"efixed");
129 const auto &emodeColumn = paramWS->getColVector<
int>(
"emode");
130 const auto &spectraColumn = paramWS->getColVector<
int>(
"spectra");
137 std::vector<double> emptyVec;
138 int failedDetectorCount = 0;
141 size_t checkIndex = 0;
142 int checkSpecNo = inputWS->getSpectrum(checkIndex).getSpectrumNo();
143 auto checkSpecIter = std::find(spectraColumn.begin(), spectraColumn.end(), checkSpecNo);
144 if (checkSpecIter != spectraColumn.end()) {
145 size_t detectorRow = std::distance(spectraColumn.begin(), checkSpecIter);
147 auto checkXValues = inputWS->readX(checkIndex);
149 auto checkFromUnit = std::unique_ptr<Unit>(fromUnit->clone());
150 auto checkOutputUnit = std::unique_ptr<Unit>(outputUnit->clone());
152 {UnitParams::twoTheta, twoThetaColumn[detectorRow]},
153 {UnitParams::efixed, efixedColumn[detectorRow]}};
154 checkFromUnit->toTOF(checkXValues, emptyVec, l1Column[detectorRow], emodeColumn[detectorRow],
pmap);
156 checkOutputUnit->fromTOF(checkXValues, emptyVec, l1Column[detectorRow], emodeColumn[detectorRow],
pmap);
164 auto &spectrumInfo = outputWS->mutableSpectrumInfo();
167 for (int64_t i = 0; i < numberOfSpectra_i; ++i) {
171 std::size_t wsid = i;
173 if (spectrumInfo.hasDetectors(i)) {
176 auto &det = spectrumInfo.detector(i);
177 int specNo = det.getID();
181 g_log.
debug() <<
"###### Spectra #" << specNo <<
" ==> Workspace ID:" << wsid <<
'\n';
184 std::vector<int>::const_iterator specIter;
186 specIter = std::find(spectraColumn.begin(), spectraColumn.end(), specNo);
187 if (specIter != spectraColumn.end()) {
188 const size_t detectorRow = std::distance(spectraColumn.begin(), specIter);
189 const double l1 = l1Column[detectorRow];
190 const double l2 = l2Column[detectorRow];
192 const double efixed = efixedColumn[detectorRow];
193 const int emode = emodeColumn[detectorRow];
195 if (
g_log.
is(Logger::Priority::PRIO_DEBUG)) {
196 g_log.
debug() <<
"specNo from detector table = " << spectraColumn[detectorRow] <<
'\n';
198 g_log.
debug() <<
"###### Spectra #" << specNo <<
" ==> Det Table Row:" << detectorRow <<
'\n';
206 auto localFromUnit = std::unique_ptr<Unit>(fromUnit->clone());
207 auto localOutputUnit = std::unique_ptr<Unit>(outputUnit->clone());
209 std::vector<double> values(outputWS->x(wsid).begin(), outputWS->x(wsid).end());
213 localFromUnit->toTOF(values, emptyVec, l1, emode,
pmap);
215 localOutputUnit->fromTOF(values, emptyVec, l1, emode,
pmap);
217 outputWS->mutableX(wsid) = values;
221 eventWS->getSpectrum(wsid).convertUnitsViaTof(localFromUnit.get(), localOutputUnit.get());
226 failedDetectorCount++;
227 outputWS->getSpectrum(wsid).clearData();
228 if (spectrumInfo.hasDetectors(wsid))
229 spectrumInfo.setMasked(wsid,
true);
234 failedDetectorCount++;
239 outputWS->getSpectrum(i).clearData();
245 if (failedDetectorCount != 0) {
246 g_log.
information() <<
"Something went wrong for " << failedDetectorCount <<
" spectra. Masking spectrum.\n";
#define DECLARE_ALGORITHM(classname)
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
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...
Helper class for reporting progress from algorithms.
A property class for workspaces.
A validator which checks that the unit of the workspace referred to by a WorkspaceProperty is the exp...
ConvertUnitsUsingDetectorTable : Converts the units in which a workspace is represented,...
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
const std::string category() const override
Algorithm's category for identification.
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
void storeEModeOnWorkspace(API::MatrixWorkspace_sptr outputWS) override
This implementation does NOT stores the emode in the provided workspace.
API::MatrixWorkspace_sptr convertViaTOF(Kernel::Unit_const_sptr fromUnit, API::MatrixWorkspace_const_sptr inputWS) override
Convert the workspace units using TOF as an intermediate step in the conversion.
bool m_inputEvents
to histogram workspaces.
Kernel::Unit_sptr m_outputUnit
The unit we're going to.
std::size_t m_numberOfSpectra
The number of spectra in the input workspace.
API::MatrixWorkspace_sptr setupOutputWorkspace(const API::MatrixWorkspace_const_sptr &inputWS)
Create an output workspace of the appropriate (histogram or event) type and copy over the data.
Exception for errors associated with the instrument definition.
void debug(const std::string &msg)
Logs at debug level.
bool is(int level) const
Returns true if at least the given log level is set.
void information(const std::string &msg)
Logs at information level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Kernel::Logger g_log("ExperimentInfo")
static logger object
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::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
std::shared_ptr< EventWorkspace > EventWorkspace_sptr
shared pointer to the EventWorkspace class
constexpr double deg2rad
Defines units/enum for Crystal work.
std::unordered_map< UnitParams, double > UnitParametersMap
std::shared_ptr< const Unit > Unit_const_sptr
Shared pointer to the Unit base class (const version)
Generate a tableworkspace to store the calibration results.
Describes the direction (within an algorithm) of a Property.
@ Input
An input workspace.
@ Output
An output workspace.