13#include "MantidIndexing/IndexInfo.h"
15#include "MantidNexusGeometry/AbstractLogger.h"
16#include "MantidNexusGeometry/NexusGeometryParser.h"
17#include "MantidTypes/SpectrumDefinition.h"
31template <
typename T>
int countEntriesOfType(
const T &entry,
const std::string &nxClass) {
32 return static_cast<int>(std::count_if(entry.groups().cbegin(), entry.groups().cend(),
33 [&nxClass](
const auto &
group) { return group.nxclass == nxClass; }));
37std::vector<Mantid::Nexus::NXClassInfo> findEntriesOfType(
const T &entry,
const std::string &nxClass) {
38 std::vector<Mantid::Nexus::NXClassInfo> result;
39 std::copy_if(entry.groups().cbegin(), entry.groups().cend(), std::back_inserter(result),
40 [&nxClass](
const auto &
group) { return group.nxclass == nxClass; });
52 const auto instrumentsCount = countEntriesOfType(entry,
"NXinstrument");
53 if (instrumentsCount == 1) {
59 if (instr.containsGroup(
"detector") ||
60 (instr.containsGroup(
"physical_detectors") && instr.containsGroup(
"physical_monitors"))) {
85 g_log.
information() <<
"Instrument layout not recognised. Spectra mappings not loaded.";
92 const std::string &parent = mtd_entry.
name();
93 auto result = findEntriesOfType(mtd_entry,
"NXinstrument");
94 if (result.size() != 1) {
95 g_log.
warning(
"We are expecting a single NXinstrument. No mappings will be loaded");
112 for (
const auto &
group : inst.groups()) {
113 if (
group.nxclass ==
"NXdetector" ||
group.nxclass ==
"NXmonitor") {
118 spectra_block.
load();
119 }
catch (std::runtime_error &) {
123 const size_t nSpecEntries = spectra_block.
dim0();
125 size_t currentSize = spectrumNumbers.size();
126 spectrumNumbers.resize(currentSize + nSpecEntries, 0);
128 for (
size_t i = 0; i < nSpecEntries; ++i) {
129 spectrumNumbers[i + currentSize] = data[i];
134 size_t nDetEntries = det_index.
dim0();
135 currentSize = detectorIds.size();
137 detectorIds.resize(currentSize + nDetEntries, 0);
138 for (
size_t i = 0; i < nDetEntries; ++i) {
139 detectorIds[i + currentSize] = data[i];
145 size_t nDetCounts = det_counts.
dim0();
146 currentSize = detectorCounts.size();
148 detectorCounts.resize(currentSize + nDetCounts, 0);
150 for (
size_t i = 0; i < nDetCounts; ++i) {
151 const int dataVal = data[i];
153 detectorCounts[i + currentSize] = dataVal;
156 if (nDetCounts != nSpecEntries) {
157 throw std::runtime_error(
"Bad file. Has different number of entries in "
158 "spec and detector_count datasets");
160 if (dataSum != nDetEntries) {
161 throw std::runtime_error(
"Bad file. detector_counts sum does not match "
162 "the number of detectors given by number of "
163 "detector_list entries");
188 const std::string &filePath) {
194 std::ostringstream parent_;
195 parent_ <<
"mantid_workspace_" << entryNumber;
196 const std::string &parent(parent_.str());
198 NexusGeometry::NexusGeometryParser::createInstrument(filePath, parent, NexusGeometry::makeLogger(&logger));
202 const auto &detInfo = matrixWs->detectorInfo();
204 Indexing::IndexInfo info(spectrumNumbers);
209 std::vector<SpectrumDefinition> definitions;
210 definitions.reserve(spectrumNumbers.size());
211 size_t detCounter = 0;
212 for (
size_t i = 0; i < spectrumNumbers.size(); ++i) {
214 size_t counts = detectorCounts[i];
215 SpectrumDefinition def;
219 for (
size_t j = 0; j < counts; ++j, ++detCounter) {
220 def.add(detInfo.indexOf(detectorIds[detCounter]));
222 definitions.emplace_back(def);
224 info.setSpectrumDefinitions(definitions);
225 matrixWs->setIndexInfo(info);
228 }
catch (std::exception &e) {
230 }
catch (H5::Exception &e) {
231 logger.
warning(e.getDetailMsg());
239 if (descriptor.
isEntry(
"/mantid_workspace_1"))
#define DECLARE_NEXUS_FILELOADER_ALGORITHM(classname)
DECLARE_NEXUS_FILELOADER_ALGORITHM should be used in place of the standard DECLARE_ALGORITHM macro wh...
Base MatrixWorkspace Abstract Class.
A property class for workspaces.
Base Workspace Abstract Class.
bool loadNexusGeometry(Mantid::API::Workspace &ws, size_t entryNumber, Kernel::Logger &logger, const std::string &filePath) override
Load nexus geometry and apply to workspace.
InstrumentLayout m_instrumentLayout
void extractMappingInfoNew(const Mantid::Nexus::NXEntry &mtd_entry)
Extract mapping information where it is build across NXDetectors.
std::unordered_map< std::string, std::vector< Indexing::SpectrumNumber > > m_spectrumNumberss
void readSpectraToDetectorMapping(Mantid::Nexus::NXEntry &mtd_entry, Mantid::API::MatrixWorkspace &ws) override
int version() const override
Algorithm's version for identification.
std::unordered_map< std::string, std::vector< int > > m_detectorCountss
int confidence(Nexus::NexusDescriptor &descriptor) const override
Returns a confidence value that this algorithm can load a file.
std::unordered_map< std::string, std::vector< Mantid::detid_t > > m_detectorIdss
void readInstrumentGroup(Mantid::Nexus::NXEntry &mtd_entry, API::MatrixWorkspace &local_workspace)
Read the spectra.
int confidence(Nexus::NexusDescriptor &descriptor) const override
Returns a confidence value that this algorithm can load a file.
The Logger class is in charge of the publishing messages from the framework through various channels.
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
bool containsGroup(const std::string &query) const
Returns whether an individual group (or group) is present.
void close()
Close this class.
NXInt openNXInt(const std::string &name) const
Creates and opens an integer dataset.
Templated class implementation of NXDataSet.
void load()
Read all of the datablock in.
container_T< T > & vecBuffer()
Returns a the internal buffer.
dimsize_t dim0() const
Returns the number of elements along the first dimension.
Implements NXdetector Nexus class.
Implements NXentry Nexus class.
NXInstrument openNXInstrument(const std::string &name) const
Opens a NXInstrument.
std::string name() const
Returns the name of the object.
bool isEntry(const std::string &entryName, const std::string &groupClass) const noexcept
Checks if a full-address entry exists for a particular groupClass in a Nexus dataset.
Kernel::Logger g_log("ExperimentInfo")
static logger object
InstrumentLayout
Layout information relating to detector-spectra mappings.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
Header for a base Nexus::Exception.
Describes the direction (within an algorithm) of a Property.