Mantid
Loading...
Searching...
No Matches
ParallelEventLoader.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10#include "MantidIndexing/IndexInfo.h"
11#include "MantidParallel/IO/EventLoader.h"
12#include "MantidTypes/Event/TofEvent.h"
13#include "MantidTypes/SpectrumDefinition.h"
14
15namespace Mantid::DataHandling {
16
18std::vector<int32_t> bankOffsets(const API::ExperimentInfo &ws, const std::string &filename,
19 const std::string &groupName, const std::vector<std::string> &bankNames) {
20 // Read an event ID for each bank. This is always a detector ID since
21 // bankOffsetsSpectrumNumbers is used otherwise. It is assumed that detector
22 // IDs within a bank are contiguous.
23 const auto &idToBank = Parallel::IO::EventLoader::makeAnyEventIdToBankMap(filename, groupName, bankNames);
24
25 const auto &detInfo = ws.detectorInfo();
26 const auto &detIds = detInfo.detectorIDs();
27 int32_t spectrumIndex{0}; // *global* index
28 std::vector<int32_t> bankOffsets(bankNames.size(), 0);
29 for (size_t i = 0; i < detInfo.size(); ++i) {
30 // Used only in LoadEventNexus so we know there is a 1:1 mapping, omitting
31 // monitors.
32 if (!detInfo.isMonitor(i)) {
33 detid_t detId = detIds[i];
34 // The offset is the difference between the event ID and the spectrum
35 // index and can then be used to translate from the former to the latter
36 // by simple subtraction. If no eventId could be read for a bank it
37 // implies that there are no events, so any offset will do since it is
38 // unused, keeping as initialized to 0 above.
39 if (idToBank.count(detId) == 1) {
40 size_t bank = idToBank.at(detId);
41 bankOffsets[bank] = detId - spectrumIndex;
42 }
43 spectrumIndex++;
44 }
45 }
46 return bankOffsets;
47}
48
51std::vector<int32_t> bankOffsetsSpectrumNumbers(const API::MatrixWorkspace &ws, const std::string &filename,
52 const std::string &groupName,
53 const std::vector<std::string> &bankNames) {
54 // Read an event ID for each bank. This is always a spectrum number since
55 // bankOffsets is used otherwise. It is assumed that spectrum numbers within a
56 // bank are contiguous.
57 const auto &idToBank = Parallel::IO::EventLoader::makeAnyEventIdToBankMap(filename, groupName, bankNames);
58
59 // *Global* vector of spectrum numbers.
60 const auto &specNums = ws.indexInfo().spectrumNumbers();
61 int32_t spectrumIndex{0}; // *global* index
62 std::vector<int32_t> offsets(bankNames.size(), 0);
63 for (auto i : specNums) {
64 // In contrast to the case of event ID = detector ID we know that any
65 // spectrum number has a corresponding event ID, i.e., we do not need
66 // special handling for monitors.
67 auto specNum = static_cast<specnum_t>(i);
68 // See comment in bankOffsets regarding this offset computation.
69 if (idToBank.count(specNum) == 1) {
70 size_t bank = idToBank.at(specNum);
71 offsets[bank] = specNum - spectrumIndex;
72 }
73 spectrumIndex++;
74 }
75 return offsets;
76}
77
78std::vector<std::vector<Types::Event::TofEvent> *> getResultVector(DataObjects::EventWorkspace &ws) {
79 const size_t size = ws.getNumberHistograms();
80
81 std::vector<std::vector<Types::Event::TofEvent> *> eventLists(size, nullptr);
82 for (size_t i = 0; i < size; ++i)
83 getEventsFrom(ws.getSpectrum(i), eventLists[i]);
84 return eventLists;
85}
86
87std::vector<int32_t> getOffsets(const DataObjects::EventWorkspace &ws, const std::string &filename,
88 const std::string &groupName, const std::vector<std::string> &bankNames,
89 const bool eventIDIsSpectrumNumber) {
90 const auto offsets = eventIDIsSpectrumNumber ? bankOffsetsSpectrumNumbers(ws, filename, groupName, bankNames)
91 : bankOffsets(ws, filename, groupName, bankNames);
92 return offsets;
93}
94
96void ParallelEventLoader::loadMPI(DataObjects::EventWorkspace &ws, const std::string &filename,
97 const std::string &groupName, const std::vector<std::string> &bankNames,
98 const bool eventIDIsSpectrumNumber) {
99 std::vector<std::vector<Types::Event::TofEvent> *> eventLists = getResultVector(ws);
100 std::vector<int32_t> offsets = getOffsets(ws, filename, groupName, bankNames, eventIDIsSpectrumNumber);
101 Parallel::IO::EventLoader::load(ws.indexInfo().communicator(), filename, groupName, bankNames, offsets, eventLists);
102}
103
107 const std::string &groupName, const std::vector<std::string> &bankNames,
108 const bool eventIDIsSpectrumNumber, const bool precalcEvents) {
109 auto eventLists = getResultVector(ws);
110 std::vector<int32_t> offsets = getOffsets(ws, filename, groupName, bankNames, eventIDIsSpectrumNumber);
111 Parallel::IO::EventLoader::load(filename, groupName, bankNames, offsets, eventLists, precalcEvents);
112}
113
114} // namespace Mantid::DataHandling
This class is shared by a few Workspace types and holds information related to a particular experimen...
const Geometry::DetectorInfo & detectorInfo() const
Return a const reference to the DetectorInfo object.
Base MatrixWorkspace Abstract Class.
const Indexing::IndexInfo & indexInfo() const
Returns a const reference to the IndexInfo object of the workspace.
static void loadMPI(DataObjects::EventWorkspace &ws, const std::string &filename, const std::string &groupName, const std::vector< std::string > &bankNames, const bool eventIDIsSpectrumNumber)
Load events from given banks into given EventWorkspace using MPI.
static void loadMultiProcess(DataObjects::EventWorkspace &ws, const std::string &filename, const std::string &groupName, const std::vector< std::string > &bankNames, const bool eventIDIsSpectrumNumber, const bool precalcEvents)
Load events from given banks into given EventWorkspace using boost::interprocess.
This class is intended to fulfill the design specified in <https://github.com/mantidproject/documents...
EventList & getSpectrum(const size_t index) override
Return the underlying ISpectrum ptr at the given workspace index.
std::size_t getNumberHistograms() const override
Get the number of histograms, usually the same as the number of pixels or detectors.
const std::vector< detid_t > & detectorIDs() const
Returns a sorted vector of all detector IDs.
std::vector< int32_t > bankOffsets(const API::ExperimentInfo &ws, const std::string &filename, const std::string &groupName, const std::vector< std::string > &bankNames)
Return offset between global spectrum index and detector ID for given banks.
std::vector< std::vector< Types::Event::TofEvent > * > getResultVector(DataObjects::EventWorkspace &ws)
std::vector< int32_t > getOffsets(const DataObjects::EventWorkspace &ws, const std::string &filename, const std::string &groupName, const std::vector< std::string > &bankNames, const bool eventIDIsSpectrumNumber)
std::vector< int32_t > bankOffsetsSpectrumNumbers(const API::MatrixWorkspace &ws, const std::string &filename, const std::string &groupName, const std::vector< std::string > &bankNames)
Return offset between global spectrum index and spectrum number for given banks.
DLLExport void getEventsFrom(EventList &el, std::vector< Types::Event::TofEvent > *&events)
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16