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"
16#include <vector>
17
18namespace Mantid::DataHandling {
19
26DataObjects::TableWorkspace_sptr createTimeZeroTable(const size_t numSpec, const std::vector<double> &timeZeros) {
28 std::dynamic_pointer_cast<Mantid::DataObjects::TableWorkspace>(
29 Mantid::API::WorkspaceFactory::Instance().createTable("TableWorkspace"));
30 timeZeroTable->addColumn("double", "time zero");
31
32 for (size_t specNum = 0; specNum < numSpec; ++specNum) {
33 Mantid::API::TableRow row = timeZeroTable->appendRow();
34 row << timeZeros[specNum];
35 }
36
37 return timeZeroTable;
38}
39
40// Constructor
42 : m_logger(g_log), m_filename(std::move(filename)), m_nexusLoader(nexusLoader) {}
43
51 m_logger.information("Loading grouping information from IDF");
52
53 auto instrument = localWorkspace.getInstrument();
54 auto &run = localWorkspace.run();
55 std::string mainFieldDirection = run.getLogData("main_field_direction")->value();
56 API::GroupingLoader groupLoader(instrument, mainFieldDirection);
57 try {
58 const auto idfGrouping = groupLoader.getGroupingFromIDF();
59 return idfGrouping->toTable();
60 } catch (const std::runtime_error &) {
61 auto dummyGrouping = std::make_shared<API::Grouping>();
62 if (instrument->getNumberDetectors() != 0) {
63 dummyGrouping = groupLoader.getDummyGrouping();
64 } else {
65 // Make sure it uses the right number of detectors
66 std::string numDetectors = "1-" + std::to_string(localWorkspace.getNumberHistograms());
67 dummyGrouping->groups.emplace_back(std::move(numDetectors));
68 dummyGrouping->groupNames.emplace_back("all");
69 }
70 return dummyGrouping->toTable();
71 }
72}
77std::vector<detid_t>
79 size_t numberOfSpectra = localWorkspace.getNumberHistograms();
80 std::vector<detid_t> loadedDetectors;
81 loadedDetectors.reserve(numberOfSpectra);
82 for (size_t spectraIndex = 0; spectraIndex < numberOfSpectra; spectraIndex++) {
83 const auto detIdSet = localWorkspace.getSpectrum(spectraIndex).getDetectorIDs();
84 // each spectrum should only point to one detector in the Muon file
85 loadedDetectors.emplace_back(*detIdSet.begin());
86 }
87 return loadedDetectors;
88}
89
96std::optional<DataObjects::TableWorkspace_sptr>
97LoadMuonStrategy::createDetectorGroupingTable(const std::vector<detid_t> &detectorsLoaded,
98 const std::optional<std::vector<detid_t>> &grouping) const {
99 if (!grouping) {
100 m_logger.information("No grouping information is provided in the Nexus file");
101 return std::nullopt;
102 }
103 auto groupingIDs = *grouping;
104 if (detectorsLoaded.size() != groupingIDs.size()) {
105 m_logger.information() << "The number of groupings in the provided Nexus file (" << groupingIDs.size()
106 << ") does not match the number of loaded detectors (" << detectorsLoaded.size() << ").";
107 return std::nullopt;
108 }
109
110 std::map<detid_t, std::vector<detid_t>> groupingMap;
111 for (size_t i = 0; i < detectorsLoaded.size(); ++i) {
112 // Add detector ID to the list of group detectors. Detector ID is always
113 groupingMap[groupingIDs[i]].emplace_back(detectorsLoaded[i]);
114 }
115
116 auto detectorGroupingTable = std::dynamic_pointer_cast<DataObjects::TableWorkspace>(
117 API::WorkspaceFactory::Instance().createTable("TableWorkspace"));
118 detectorGroupingTable->addColumn("vector_int", "Detectors");
119 for (const auto &group : groupingMap) {
120 if (group.first != 0) { // Skip 0 group
121 API::TableRow newRow = detectorGroupingTable->appendRow();
122 newRow << group.second;
123 }
124 }
125 return detectorGroupingTable;
126}
134 const std::vector<double> &deadTimes) const {
135 auto deadTimesTable = std::dynamic_pointer_cast<DataObjects::TableWorkspace>(
136 API::WorkspaceFactory::Instance().createTable("TableWorkspace"));
137
138 deadTimesTable->addColumn("int", "spectrum");
139 deadTimesTable->addColumn("double", "dead-time");
140
141 for (size_t i = 0; i < detectorsLoaded.size(); i++) {
142 API::TableRow row = deadTimesTable->appendRow();
143 row << detectorsLoaded[i] << deadTimes[i];
144 }
145
146 return deadTimesTable;
147}
148
149} // 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:141
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:29
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:62
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)