Mantid
Loading...
Searching...
No Matches
LoadMuonStrategy.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2020 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source
5// & Institut Laue - Langevin
6// SPDX - License - Identifier: GPL - 3.0 +
7
10#include "MantidAPI/Run.h"
11#include "MantidAPI/TableRow.h"
15#include "MantidKernel/Logger.h"
17#include <vector>
18
19namespace Mantid::DataHandling {
20
27DataObjects::TableWorkspace_sptr createTimeZeroTable(const size_t numSpec, const std::vector<double> &timeZeros) {
29 std::dynamic_pointer_cast<Mantid::DataObjects::TableWorkspace>(
30 Mantid::API::WorkspaceFactory::Instance().createTable("TableWorkspace"));
31 timeZeroTable->addColumn("double", "time zero");
32
33 for (size_t specNum = 0; specNum < numSpec; ++specNum) {
34 Mantid::API::TableRow row = timeZeroTable->appendRow();
35 row << timeZeros[specNum];
36 }
37
38 return timeZeroTable;
39}
40
41// Constructor
43 : m_logger(g_log), m_filename(std::move(filename)), m_nexusLoader(nexusLoader) {}
44
52 m_logger.information("Loading grouping information from IDF");
53
54 auto instrument = localWorkspace.getInstrument();
55 auto &run = localWorkspace.run();
56 std::string mainFieldDirection = run.getLogData("main_field_direction")->value();
57 API::GroupingLoader groupLoader(instrument, mainFieldDirection);
58 try {
59 const auto idfGrouping = groupLoader.getGroupingFromIDF();
60 return idfGrouping->toTable();
61 } catch (const std::runtime_error &) {
62 auto dummyGrouping = std::make_shared<API::Grouping>();
63 if (instrument->getNumberDetectors() != 0) {
64 dummyGrouping = groupLoader.getDummyGrouping();
65 } else {
66 // Make sure it uses the right number of detectors
67 std::string numDetectors = "1-" + std::to_string(localWorkspace.getNumberHistograms());
68 dummyGrouping->groups.emplace_back(std::move(numDetectors));
69 dummyGrouping->groupNames.emplace_back("all");
70 }
71 return dummyGrouping->toTable();
72 }
73}
78std::vector<detid_t>
80 size_t numberOfSpectra = localWorkspace.getNumberHistograms();
81 std::vector<detid_t> loadedDetectors;
82 loadedDetectors.reserve(numberOfSpectra);
83 for (size_t spectraIndex = 0; spectraIndex < numberOfSpectra; spectraIndex++) {
84 const auto detIdSet = localWorkspace.getSpectrum(spectraIndex).getDetectorIDs();
85 // each spectrum should only point to one detector in the Muon file
86 loadedDetectors.emplace_back(*detIdSet.begin());
87 }
88 return loadedDetectors;
89}
90
97std::optional<DataObjects::TableWorkspace_sptr>
98LoadMuonStrategy::createDetectorGroupingTable(const std::vector<detid_t> &detectorsLoaded,
99 const std::optional<std::vector<detid_t>> &grouping) const {
100 if (!grouping) {
101 m_logger.information("No grouping information is provided in the Nexus file");
102 return std::nullopt;
103 }
104 auto groupingIDs = *grouping;
105 if (detectorsLoaded.size() != groupingIDs.size()) {
106 m_logger.information() << "The number of groupings in the provided Nexus file (" << groupingIDs.size()
107 << ") does not match the number of loaded detectors (" << detectorsLoaded.size() << ").";
108 return std::nullopt;
109 }
110
111 std::map<detid_t, std::vector<detid_t>> groupingMap;
112 for (size_t i = 0; i < detectorsLoaded.size(); ++i) {
113 // Add detector ID to the list of group detectors. Detector ID is always
114 groupingMap[groupingIDs[i]].emplace_back(detectorsLoaded[i]);
115 }
116
117 auto detectorGroupingTable = std::dynamic_pointer_cast<DataObjects::TableWorkspace>(
118 API::WorkspaceFactory::Instance().createTable("TableWorkspace"));
119 detectorGroupingTable->addColumn("vector_int", "Detectors");
120 for (const auto &group : groupingMap) {
121 if (group.first != 0) { // Skip 0 group
122 API::TableRow newRow = detectorGroupingTable->appendRow();
123 newRow << group.second;
124 }
125 }
126 return detectorGroupingTable;
127}
135 const std::vector<double> &deadTimes) const {
136 auto deadTimesTable = std::dynamic_pointer_cast<DataObjects::TableWorkspace>(
137 API::WorkspaceFactory::Instance().createTable("TableWorkspace"));
138
139 deadTimesTable->addColumn("int", "spectrum");
140 deadTimesTable->addColumn("double", "dead-time");
141
142 for (size_t i = 0; i < detectorsLoaded.size(); i++) {
143 API::TableRow row = deadTimesTable->appendRow();
144 row << detectorsLoaded[i] << deadTimes[i];
145 }
146
147 return deadTimesTable;
148}
149
150} // namespace Mantid::DataHandling
const Run & run() const
Run details object access.
Geometry::Instrument_const_sptr getInstrument() const
Returns the parameterized instrument.
GroupingLoader : Loads instrument grouping from IDF file.
std::shared_ptr< Grouping > getDummyGrouping()
Returns a "dummy" grouping of a single group with all the detectors in it.
std::shared_ptr< Grouping > getGroupingFromIDF() const
Load the grouping from the instrument's IDF.
const std::set< detid_t > & getDetectorIDs() const
Get a const reference to the detector IDs set.
Kernel::Property * getLogData(const std::string &name) const
Access a single log entry.
Definition LogManager.h:140
TableRow represents a row in a TableWorkspace.
Definition TableRow.h:39
std::optional< DataObjects::TableWorkspace_sptr > createDetectorGroupingTable(const std::vector< detid_t > &specToLoad, const std::optional< std::vector< detid_t > > &grouping) const
Creates Detector Grouping Table .
DataObjects::TableWorkspace_sptr createDeadTimeTable(const std::vector< detid_t > &detectorsLoaded, const std::vector< double > &deadTimes) const
Creates the deadtime table for the loaded detectors .
std::vector< detid_t > getLoadedDetectorsFromWorkspace(const DataObjects::Workspace2D &localWorkspace) const
Determines the detectors loaded in the input workspace.
API::Workspace_sptr loadDefaultDetectorGrouping(const DataObjects::Workspace2D &localWorkspace) const
Loads default detector grouping, if this isn't present return dummy grouping.
LoadMuonStrategy(Kernel::Logger &g_log, std::string filename, LoadMuonNexusV2NexusHelper &nexusLoader)
Concrete workspace implementation.
Definition Workspace2D.h:30
std::size_t getNumberHistograms() const override
Returns the histogram number.
Histogram1D & getSpectrum(const size_t index) override
Return the underlying ISpectrum ptr at the given workspace index.
Definition Workspace2D.h:64
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
virtual std::string value() const =0
Returns the value of the property as a string.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Kernel::Logger g_log("ExperimentInfo")
static logger object
DataObjects::TableWorkspace_sptr createTimeZeroTable(const size_t numSpec, const std::vector< double > &timeZeros)
Creates a timezero table for the loaded detectors.
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)