19#include <unordered_set>
43 if (
const auto group = std::dynamic_pointer_cast<WorkspaceGroup>(
workspace)) {
44 for (
int i = 0; i <
group->getNumberOfEntries(); ++i)
45 correctWorkspace(
group->getItem(i), log);
49 if (
const auto matrixWorkspace = std::dynamic_pointer_cast<MatrixWorkspace>(
workspace))
56 const auto instrument =
workspace.getInstrument();
57 if (!instrument || !isCorrectionEnabled(*instrument))
60 const auto detectorIDs = instrument->getDetectorIDs(
false);
61 if (detectorIDs.empty())
63 const std::unordered_set<detid_t> knownIDs(detectorIDs.cbegin(), detectorIDs.cend());
69 std::size_t corrected = 0;
70 std::size_t unmappable = 0;
71 for (std::size_t i = 0; i <
workspace.getNumberHistograms(); ++i) {
72 auto &spectrum =
workspace.getSpectrum(i);
73 const auto &fileIDs = spectrum.getDetectorIDs();
74 if (std::all_of(fileIDs.cbegin(), fileIDs.cend(), [&knownIDs](
const detid_t id) { return knownIDs.count(id) > 0; }))
77 const auto matchingID =
static_cast<detid_t>(spectrum.getSpectrumNo());
78 if (knownIDs.count(matchingID)) {
79 spectrum.setDetectorID(matchingID);
82 spectrum.clearDetectorIDs();
87 if (corrected == 0 && unmappable == 0)
91 log.
information() << instrument->getName() <<
": " << corrected
92 <<
" spectra referenced detectors not in the instrument definition and were mapped to the "
93 "detector of the same ID.\n";
95 log.
information() << unmappable <<
" spectra had no detector of a matching ID and were left without detectors.\n";
102 for (
const auto *
name : {
"OutputWorkspace",
"MonitorWorkspace"}) {
IPeaksWorkspace_sptr workspace
Base class from which all concrete algorithm classes should be derived.
bool existsProperty(const std::string &name) const override
Checks whether the named property is already in the list of managed property.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Base MatrixWorkspace Abstract Class.
std::vector< std::string > getStringParameter(const std::string &pname, bool recursive=true) const override
Get a parameter defined as a string.
The Logger class is in charge of the publishing messages from the framework through various channels.
void information(const std::string &msg)
Logs at information level.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
MANTID_DATAHANDLING_DLL void correctLoadedWorkspaces(const API::Algorithm &loader, Kernel::Logger &log)
Applies correctSpectraMapping to whichever workspaces a loader declared: its output,...
MANTID_DATAHANDLING_DLL bool correctSpectraMapping(API::MatrixWorkspace &workspace, Kernel::Logger &log)
Corrects spectrum-to-detector mappings for files that use detector IDs not recognized by the instrume...
MANTID_DATAHANDLING_DLL const std::string SPECTRA_MAP_SOURCE
The specific instrument parameter and target value that enables the correction.
MANTID_DATAHANDLING_DLL const std::string SPECTRA_MAP_FROM_INSTRUMENT
int32_t detid_t
Typedef for a detector ID.