21#include <unordered_map>
35 "The name of the workspace to take as input.");
38 "If left empty then all workspace indices are used.");
39 setPropertySettings(
"WorkspaceIndices",
42 declareProperty(
"IncludeData",
false,
"Include the first value from each spectrum.");
43 setPropertySettings(
"IncludeData",
46 declareProperty<bool>(
"IncludeDetectorPosition",
false,
47 "Include the absolute position of the detector group for each spectrum.",
Direction::Input);
48 setPropertySettings(
"IncludeDetectorPosition",
51 declareProperty<bool>(
"OneRowPerDetectorID",
false,
52 "Order rows in table by detector IDs, with each detector ID having its own row. When "
53 "OneRowPerDetectorID=true the table iterates over all detector IDs, so WorkspaceIndices is "
54 "ignored and the row count equals the detector count.",
56 setPropertySettings(
"OneRowPerDetectorID",
61 "The name of the outputted detector table workspace, if left empty then "
62 "the input workspace name + \"-Detectors\" is used.");
72 if (
auto peaks = std::dynamic_pointer_cast<IPeaksWorkspace>(inputWs)) {
73 table = peaks->createDetectorTable();
78 if ((
ws = std::dynamic_pointer_cast<MatrixWorkspace>(inputWs))) {
79 if (
ws->getInstrument()->getSample() ==
nullptr) {
80 throw std::runtime_error(
"Matrix workspace has no instrument information");
93 throw std::runtime_error(
"Detector table can only be created for matrix and peaks workspaces.");
97 if (
table ==
nullptr) {
98 throw std::runtime_error(
"Unknown error while creating detector table workspace");
102 setPropertyValue(
"DetectorTableWorkspace", inputWs->getName() +
"-Detectors");
114 std::map<std::string, std::string> validationOutput;
117 const auto matrix = std::dynamic_pointer_cast<MatrixWorkspace>(inputWS);
120 const int numSpectra =
static_cast<int>(matrix->getNumberHistograms());
121 const std::vector<int> indices =
getProperty(
"WorkspaceIndices");
123 if (std::any_of(indices.cbegin(), indices.cend(),
124 [numSpectra](
const auto index) { return (index >= numSpectra) || (index < 0); })) {
125 validationOutput[
"WorkspaceIndices"] =
"One or more indices out of range of available spectra.";
129 return validationOutput;
144 ws->getEFixed(detector);
145 }
catch (std::invalid_argument &) {
147 }
catch (std::runtime_error &) {
157 beamAxisIndex =
ws->getInstrument()->getReferenceFrame()->pointingAlongBeam();
171 std::vector<std::pair<std::string, std::string>> colNames;
172 colNames.emplace_back(
"int",
"Index");
173 colNames.emplace_back(
"int",
"Spectrum No");
175 colNames.emplace_back(
"int",
"Detector ID(s)");
177 colNames.emplace_back(
"str",
"Detector ID(s)");
180 colNames.emplace_back(
"str",
"Time Indexes");
182 colNames.emplace_back(
"double",
"Data Value");
183 colNames.emplace_back(
"double",
"Data Error");
186 colNames.emplace_back(
"double",
"R");
187 colNames.emplace_back(
"double",
"Theta");
189 colNames.emplace_back(
"double",
"Q elastic");
191 colNames.emplace_back(
"double",
"Phi");
192 colNames.emplace_back(
"str",
"Monitor");
194 colNames.emplace_back(
"double",
"DIFA");
195 colNames.emplace_back(
"double",
"DIFC");
196 colNames.emplace_back(
"double",
"DIFC - Uncalibrated");
197 colNames.emplace_back(
"double",
"TZERO");
200 colNames.emplace_back(
"V3D",
"Position");
204 for (
size_t col = 0; col < colNames.size(); ++col) {
205 auto column =
table->addColumn(colNames.at(col).first, colNames.at(col).second);
206 column->setPlotType(0);
225 theta *= 180.0 / M_PI;
226 }
catch (
const std::exception &ex) {
244 std::set<int> timeIndexSet;
246 timeIndexSet.insert(
int(def.second));
267 }
catch (std::runtime_error &) {
284 difa = diffConsts[UnitParams::difa];
285 difc = diffConsts[UnitParams::difc];
287 tzero = diffConsts[UnitParams::tzero];
291 TableRow colValues =
table->getRow(
static_cast<size_t>(row));
292 colValues << static_cast<int>(data.
wsIndex);
296 colValues << static_cast<int>(*data.
detIds.begin());
307 colValues << data.
R << data.
theta;
311 colValues << data.
phi;
326 throw std::runtime_error(
"No detectors found.");
328 const std::vector<std::string> ¶meters =
331 (!parameters.empty() && find(parameters.begin(), parameters.end(),
"Always") != parameters.end());
336 data.
wsIndex =
static_cast<int>(wsIndex);
338 auto &spectrum =
ws->getSpectrum(wsIndex);
339 data.
specNo = spectrum.getSpectrumNo();
340 data.
detIds =
dynamic_cast<const std::set<int> &
>(spectrum.getDetectorIDs());
349 data.
q =
getQ(wsIndex);
367 auto wsIndex =
static_cast<size_t>(
workspaceIndices[
static_cast<size_t>(row)]);
373 }
catch (
const std::exception &) {
375 errorData.
wsIndex =
static_cast<int>(wsIndex);
379 errorData.
dataY0 =
ws->y(wsIndex)[0];
380 errorData.
dataE0 =
ws->e(wsIndex)[0];
394 size_t rowsCounter = 0;
396 wsIndexToChunkStart[i] = rowsCounter;
397 rowsCounter +=
ws->getSpectrum(
static_cast<size_t>(
workspaceIndices[i])).getDetectorIDs().size();
399 std::vector<DetectorRowData> allRowsData(rowsCounter);
409 auto wsIndex =
static_cast<size_t>(
workspaceIndices[
static_cast<size_t>(i)]);
417 }
catch (
const std::exception &) {
418 data.
wsIndex =
static_cast<int>(wsIndex);
427 size_t chunkStart = wsIndexToChunkStart[
static_cast<size_t>(i)];
428 auto detIds =
dynamic_cast<const std::set<int> &
>(
ws->getSpectrum(wsIndex).getDetectorIDs());
429 for (
int detId : detIds) {
430 data.
detIds = std::set<int>({detId});
431 allRowsData[chunkStart++] = data;
438 std::unordered_map<int, DetectorRowData> detIdToData;
439 detIdToData.reserve(allRowsData.size());
440 for (
auto &rowData : allRowsData) {
441 if (!rowData.detIds.empty()) {
442 detIdToData[*rowData.detIds.begin()] = std::move(rowData);
448 table->setRowCount(workspaceDetectorIds.size());
450 for (
int detId : workspaceDetectorIds) {
452 auto it = detIdToData.find(detId);
453 if (it != detIdToData.end()) {
479 std::string truncated;
480 size_t ndets = elements.size();
481 auto iter = elements.begin();
482 auto itEnd = elements.end();
486 auto revIter = elements.rbegin();
490 for (; iter != itEnd; ++iter) {
494 if (!truncated.empty()) {
495 truncated.pop_back();
#define DECLARE_ALGORITHM(classname)
std::map< DeltaEMode::Type, std::string > index
#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_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
Show a property as enabled when the workspace pointed to by another is of a given type.
double signedTwoTheta(const size_t index) const
Returns the signed scattering angle 2 theta in radians (angle w.r.t.
bool isMonitor(const size_t index) const
Returns true if the detector(s) associated with the spectrum are monitors.
Kernel::UnitParametersMap diffractometerConstants(const size_t index, std::vector< detid_t > &uncalibratedDets) const
Calculate average diffractometer constants (DIFA, DIFC, TZERO) of detectors associated with this spec...
bool hasDetectors(const size_t index) const
Returns true if the spectrum is associated with detectors in the instrument.
double twoTheta(const size_t index) const
Returns the scattering angle 2 theta in radians (angle w.r.t.
double difcUncalibrated(const size_t index) const
Calculate average uncalibrated DIFC value of detectors associated with this spectrum.
Kernel::V3D position(const size_t index) const
Returns the position of the spectrum with given index.
double l2(const size_t index) const
Returns L2 (distance from sample to spectrum).
const SpectrumDefinition & spectrumDefinition(const size_t index) const
Returns a const reference to the SpectrumDefinition of the spectrum.
const Geometry::IDetector & detector(const size_t index) const
Return a const reference to the detector or detector group of the spectrum with given index.
TableRow represents a row in a TableWorkspace.
A property class for workspaces.
void populateTableByDetID()
API::MatrixWorkspace_sptr ws
void setup()
Creates table workspace of detector information from a given workspace.
void exec() override
Execution code.
Geometry::PointingAlong beamAxisIndex
const std::string getTimeIndexes(size_t wsIndex)
bool includeDetectorPosition
void writeRowToTable(const int row, const DetectorRowData &data)
void getSphericalCoordinates(size_t wsIndex, double &R, double &theta, double &phi)
double getQ(size_t wsIndex)
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
const API::SpectrumInfo * spectrumInfo
API::ITableWorkspace_sptr table
DetectorRowData calculateWsIdxData(size_t wsIndex)
bool signedThetaParamRetrieved
void getDiffConst(size_t wsIndex, double &difa, double &difc, double &difcUnc, double &tzero)
const Geometry::DetectorInfo * detectorInfo
std::vector< int > workspaceIndices
const std::vector< detid_t > & detectorIDs() const
Returns a sorted vector of all detector IDs.
virtual std::vector< std::string > getStringParameter(const std::string &pname, bool recursive=true) const =0
Get a parameter defined as a string.
Support for a property that holds an array of values.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void error(const std::string &msg)
Logs at error level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
static double convertToElasticQ(const double theta, const double efixed)
Convert to ElasticQ from Energy.
void getSpherical(double &R, double &theta, double &phi) const noexcept
Return the vector's position in spherical coordinates.
This functor is used as the deleter object of a shared_ptr to effectively erase ownership Raw pointer...
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::string createTruncatedList(const std::set< int > &elements)
Converts a list to a string, shortened if necessary.
std::shared_ptr< const Mantid::Geometry::IDetector > IDetector_const_sptr
Shared pointer to IDetector (const version)
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.
int32_t detid_t
Typedef for a detector ID.
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
@ Output
An output workspace.