Mantid
Loading...
Searching...
No Matches
EventWorkspaceCollection.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 +
8#include "MantidAPI/Axis.h"
10#include "MantidAPI/Run.h"
11#include "MantidAPI/Sample.h"
15#include "MantidIndexing/IndexInfo.h"
16#include "MantidKernel/Logger.h"
18
19#include <algorithm>
20#include <memory>
21#include <set>
22#include <unordered_set>
23#include <vector>
24
25namespace Mantid::DataHandling {
26
27using namespace Mantid::DataObjects;
28using namespace Mantid::Kernel;
29using namespace Mantid::API;
30
31namespace {
32
33// Logger for this class
34auto g_log = Kernel::Logger("EventWorkspaceCollection");
35
44void copyLogs(const EventWorkspace_sptr &from, EventWorkspace_sptr &to) {
45 // from the logs, get all the properties that don't overwrite any
46 // prop. already set in the sink workspace (like 'filename').
47 auto props = from->mutableRun().getLogData();
48 for (auto &prop : props) {
49 if (!to->mutableRun().hasProperty(prop->name())) {
50 to->mutableRun().addLogData(prop->clone());
51 }
52 }
53}
54} // namespace
55
58EventWorkspaceCollection::EventWorkspaceCollection() : m_WsVec(1, createEmptyEventWorkspace()) {}
59
65 // Create the output workspace
67 // Make sure to initialize.
68 // We can use dummy numbers for arguments, for event workspace it doesn't
69 // matter
70 eventWS->initialize(1, 1, 1);
71
72 // Set the units
73 eventWS->getAxis(0)->unit() = UnitFactory::Instance().create("TOF");
74 eventWS->setYUnit("Counts");
75
76 return eventWS;
77}
78
79void EventWorkspaceCollection::setNPeriods(size_t nPeriods, std::unique_ptr<const TimeSeriesProperty<int>> &periodLog) {
80
81 // Create vector where size is the number of periods and initialize workspaces
82 // in each.
83 auto temp = m_WsVec[0];
84 m_WsVec = std::vector<DataObjects::EventWorkspace_sptr>(nPeriods);
85
86 std::vector<int> periodNumbers = periodLog->valuesAsVector();
87 std::unordered_set<int> uniquePeriods(periodNumbers.begin(), periodNumbers.end());
88 const bool addBoolTimeSeries = (uniquePeriods.size() == nPeriods);
89
90 auto logCreator = ISISRunLogs(temp->run());
91 logCreator.addStatusLog(temp->mutableRun());
92
93 for (size_t i = 0; i < m_WsVec.size(); ++i) {
94 const auto periodNumber = int(i + 1);
96 m_WsVec[i]->copyExperimentInfoFrom(temp.get());
97 if (addBoolTimeSeries) {
98 try {
99 logCreator.addPeriodLogs(periodNumber, m_WsVec[i]->mutableRun());
100 } catch (const std::runtime_error &e) {
101 // The addition of period related logs may result in runtime errors, for instance, if the log
102 // filter contains two entries with identical times as duplicated entries will be removed:
103 // "Cannot guess ending value from a TimeSeriesProperty that contains only a single time"
104 // Under these circumstances, it is advisable to omit the period-related logs.
105
106 g_log.warning(e.what());
107 }
108 }
109 copyLogs(temp,
110 m_WsVec[i]); // Copy all logs from dummy workspace to period workspaces.
111 m_WsVec[i]->setInstrument(temp->getInstrument());
112 }
113}
114
116 for (auto &ws : m_WsVec) {
117 ws->getSpectrum(wi).reserve(size);
118 }
119}
120
121size_t EventWorkspaceCollection::nPeriods() const { return m_WsVec.size(); }
122
124
127 if (this->nPeriods() == 1) {
128 final = getSingleHeldWorkspace();
129 } else {
130 auto wsg = std::make_shared<API::WorkspaceGroup>();
131 for (const auto &ws : m_WsVec) {
132 wsg->addWorkspace(ws);
133 }
134 final = wsg;
135 }
136 return final;
137}
138
140const API::Run &EventWorkspaceCollection::run() const { return m_WsVec[0]->run(); }
143EventList &EventWorkspaceCollection::getSpectrum(const size_t index) { return m_WsVec[0]->getSpectrum(index); }
145 return m_WsVec[0]->getSpectrum(index);
146}
148 // For each workspace, update all the spectrum numbers
149 for (auto &ws : m_WsVec) {
150 size_t counter = 0;
151 for (auto spectrum : uniqueSpectra) {
152 ws->getSpectrum(counter).setSpectrumNo(spectrum);
153 ++counter;
154 }
155 }
156}
157
158void EventWorkspaceCollection::setSpectrumNumberForAllPeriods(const size_t spectrumNumber, const specnum_t specid) {
159 for (auto &ws : m_WsVec) {
160 auto &spec = ws->getSpectrum(spectrumNumber);
161 spec.setSpectrumNo(specid);
162 }
163}
164
165void EventWorkspaceCollection::setDetectorIdsForAllPeriods(const size_t spectrumNumber, const detid_t id) {
166 for (auto &ws : m_WsVec) {
167 auto &spec = ws->getSpectrum(spectrumNumber);
168 spec.setDetectorID(id);
169 }
170}
171
172Mantid::API::Axis *EventWorkspaceCollection::getAxis(const size_t &i) const { return m_WsVec[0]->getAxis(i); }
173size_t EventWorkspaceCollection::getNumberHistograms() const { return m_WsVec[0]->getNumberHistograms(); }
174
176 const size_t periodNumber) const {
177 return m_WsVec[periodNumber]->getSpectrum(workspace_index);
178}
179
180DataObjects::EventList &EventWorkspaceCollection::getSpectrum(const size_t workspace_index, const size_t periodNumber) {
181 return m_WsVec[periodNumber]->getSpectrum(workspace_index);
182}
183
185 return m_WsVec[0]->getSpectrumToWorkspaceIndexVector(offset);
186}
188 bool dothrow) const {
189 return m_WsVec[0]->getDetectorIDToWorkspaceIndexVector(offset, dothrow);
190}
191
192Types::Core::DateAndTime EventWorkspaceCollection::getFirstPulseTime() const { return m_WsVec[0]->getFirstPulseTime(); }
193void EventWorkspaceCollection::setAllX(const HistogramData::BinEdges &x) {
194 for (auto &ws : m_WsVec) {
195 ws->setAllX(x);
196 }
197}
199 return m_WsVec[0]->getNumberEvents(); // Should be the sum across all periods?
200}
201
202void EventWorkspaceCollection::setIndexInfo(const Indexing::IndexInfo &indexInfo) {
203 const HistogramData::BinEdges edges(2);
204 std::for_each(m_WsVec.begin(), m_WsVec.end(),
205 [&indexInfo, &edges](auto &ws) { ws = create<EventWorkspace>(*ws, indexInfo, edges); });
206}
207
209 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&inst](auto &ws) { ws->setInstrument(inst); });
210}
211void EventWorkspaceCollection::setMonitorWorkspace(const std::shared_ptr<API::MatrixWorkspace> &monitorWS) {
212 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&monitorWS](auto &ws) { ws->setMonitorWorkspace(monitorWS); });
213 // TODO, do we really set the same monitor on all periods???
214}
216 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&map](auto &ws) { ws->updateSpectraUsing(map); });
217}
218
220 std::for_each(m_WsVec.begin(), m_WsVec.end(), [flag](auto &ws) { ws->mutableSample().setGeometryFlag(flag); });
221}
222
224 std::for_each(m_WsVec.begin(), m_WsVec.end(), [flag](auto &ws) { ws->mutableSample().setThickness(flag); });
225}
227 std::for_each(m_WsVec.begin(), m_WsVec.end(), [flag](auto &ws) { ws->mutableSample().setHeight(flag); });
228}
230 std::for_each(m_WsVec.begin(), m_WsVec.end(), [flag](auto &ws) { ws->mutableSample().setWidth(flag); });
231}
232
233void EventWorkspaceCollection::setTitle(const std::string &title) {
234 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&title](auto &ws) { ws->setTitle(title); });
235}
236
238 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&func](auto &ws) { func(ws); });
239}
240
243 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&func](auto &ws) { ws = func(ws); });
244}
245
246//-----------------------------------------------------------------------------
250 // Since there is a mutex lock around sorting, EventWorkspaces are always
251 // safe.
252 return true;
253}
254
255} // namespace Mantid::DataHandling
std::map< DeltaEMode::Type, std::string > index
Class to represent the axis of a workspace.
Definition Axis.h:30
Defines a class to aid in creating ISIS specific run logs for periods, status etc.
Definition ISISRunLogs.h:28
void setSpectrumNo(specnum_t num)
Sets the spectrum number of this spectrum.
This class stores information regarding an experimental run as a series of log entries.
Definition Run.h:35
This class stores information about the sample used in particular run.
Definition Sample.h:33
A minimal class to hold the mapping between the spectrum number and its related detector ID numbers f...
void applyFilterInPlace(const boost::function< void(API::MatrixWorkspace_sptr)> &func)
void setDetectorIdsForAllPeriods(const size_t spectrumNumber, const detid_t id)
std::vector< DataObjects::EventWorkspace_sptr > m_WsVec
Vector of EventWorkspaces.
void setIndexInfo(const Indexing::IndexInfo &indexInfo)
DataObjects::EventWorkspace_sptr createEmptyEventWorkspace() const
Create Empty EventWorkspaces.
std::vector< size_t > getDetectorIDToWorkspaceIndexVector(Mantid::specnum_t &offset, bool dothrow) const
Geometry::Instrument_const_sptr getInstrument() const
void setMonitorWorkspace(const std::shared_ptr< API::MatrixWorkspace > &monitorWS)
Mantid::API::Axis * getAxis(const size_t &i) const
void updateSpectraUsing(const API::SpectrumDetectorMapping &map)
const DataObjects::EventList & getSpectrum(const size_t workspace_index, const size_t periodNumber) const
void setSpectrumNumberForAllPeriods(const size_t spectrumNumber, const specnum_t specid)
void setInstrument(const Geometry::Instrument_const_sptr &inst)
void setNPeriods(size_t nPeriods, std::unique_ptr< const Kernel::TimeSeriesProperty< int > > &periodLog)
virtual bool threadSafe() const
Returns true if the EventWorkspace is safe for multithreaded operations.
void setSpectrumNumbersFromUniqueSpectra(const std::set< int > &uniqueSpectra)
void applyFilter(const boost::function< DataObjects::EventWorkspace_sptr(DataObjects::EventWorkspace_sptr)> &func)
DataObjects::EventWorkspace_sptr getSingleHeldWorkspace()
std::vector< size_t > getSpectrumToWorkspaceIndexVector(Mantid::specnum_t &offset) const
A class for holding :
Definition EventList.h:57
std::size_t getNumberEvents() const override
Return the number of events in the list.
This class is intended to fulfill the design specified in <https://github.com/mantidproject/documents...
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
A specialised Property class for holding a series of time-value pairs.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< EventWorkspace > EventWorkspace_sptr
shared pointer to the EventWorkspace class
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
int32_t detid_t
Typedef for a detector ID.
int32_t specnum_t
Typedef for a spectrum Number.
Definition IDTypes.h:14