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"
9#include "MantidAPI/Run.h"
10#include "MantidAPI/Sample.h"
15#include "MantidIndexing/IndexInfo.h"
17
18#include <algorithm>
19#include <memory>
20#include <set>
21#include <unordered_set>
22#include <vector>
23
24namespace Mantid::DataHandling {
25
26using namespace Mantid::DataObjects;
27using namespace Mantid::Kernel;
28using namespace Mantid::API;
29
30namespace {
31
40void copyLogs(const EventWorkspace_sptr &from, EventWorkspace_sptr &to) {
41 // from the logs, get all the properties that don't overwrite any
42 // prop. already set in the sink workspace (like 'filename').
43 auto props = from->mutableRun().getLogData();
44 for (auto &prop : props) {
45 if (!to->mutableRun().hasProperty(prop->name())) {
46 to->mutableRun().addLogData(prop->clone());
47 }
48 }
49}
50} // namespace
51
54EventWorkspaceCollection::EventWorkspaceCollection() : m_WsVec(1, createEmptyEventWorkspace()) {}
55
61 // Create the output workspace
63 // Make sure to initialize.
64 // We can use dummy numbers for arguments, for event workspace it doesn't
65 // matter
66 eventWS->initialize(1, 1, 1);
67
68 // Set the units
69 eventWS->getAxis(0)->unit() = UnitFactory::Instance().create("TOF");
70 eventWS->setYUnit("Counts");
71
72 return eventWS;
73}
74
75void EventWorkspaceCollection::setNPeriods(size_t nPeriods, std::unique_ptr<const TimeSeriesProperty<int>> &periodLog) {
76
77 // Create vector where size is the number of periods and initialize workspaces
78 // in each.
79 auto temp = m_WsVec[0];
80 m_WsVec = std::vector<DataObjects::EventWorkspace_sptr>(nPeriods);
81
82 std::vector<int> periodNumbers = periodLog->valuesAsVector();
83 std::unordered_set<int> uniquePeriods(periodNumbers.begin(), periodNumbers.end());
84 const bool addBoolTimeSeries = (uniquePeriods.size() == nPeriods);
85
86 auto logCreator = ISISRunLogs(temp->run());
87 logCreator.addStatusLog(temp->mutableRun());
88
89 for (size_t i = 0; i < m_WsVec.size(); ++i) {
90 const auto periodNumber = int(i + 1);
92 m_WsVec[i]->copyExperimentInfoFrom(temp.get());
93 if (addBoolTimeSeries) {
94 logCreator.addPeriodLogs(periodNumber, m_WsVec[i]->mutableRun());
95 }
96 copyLogs(temp,
97 m_WsVec[i]); // Copy all logs from dummy workspace to period workspaces.
98 m_WsVec[i]->setInstrument(temp->getInstrument());
99 }
100}
101
103 for (auto &ws : m_WsVec) {
104 ws->getSpectrum(wi).reserve(size);
105 }
106}
107
108size_t EventWorkspaceCollection::nPeriods() const { return m_WsVec.size(); }
109
111
114 if (this->nPeriods() == 1) {
115 final = getSingleHeldWorkspace();
116 } else {
117 auto wsg = std::make_shared<API::WorkspaceGroup>();
118 for (auto &ws : m_WsVec) {
119 wsg->addWorkspace(ws);
120 }
121 final = wsg;
122 }
123 return final;
124}
125
127const API::Run &EventWorkspaceCollection::run() const { return m_WsVec[0]->run(); }
130EventList &EventWorkspaceCollection::getSpectrum(const size_t index) { return m_WsVec[0]->getSpectrum(index); }
132 return m_WsVec[0]->getSpectrum(index);
133}
135 // For each workspace, update all the spectrum numbers
136 for (auto &ws : m_WsVec) {
137 size_t counter = 0;
138 for (auto spectrum : uniqueSpectra) {
139 ws->getSpectrum(counter).setSpectrumNo(spectrum);
140 ++counter;
141 }
142 }
143}
144
145void EventWorkspaceCollection::setSpectrumNumberForAllPeriods(const size_t spectrumNumber, const specnum_t specid) {
146 for (auto &ws : m_WsVec) {
147 auto &spec = ws->getSpectrum(spectrumNumber);
148 spec.setSpectrumNo(specid);
149 }
150}
151
152void EventWorkspaceCollection::setDetectorIdsForAllPeriods(const size_t spectrumNumber, const detid_t id) {
153 for (auto &ws : m_WsVec) {
154 auto &spec = ws->getSpectrum(spectrumNumber);
155 spec.setDetectorID(id);
156 }
157}
158
159Mantid::API::Axis *EventWorkspaceCollection::getAxis(const size_t &i) const { return m_WsVec[0]->getAxis(i); }
160size_t EventWorkspaceCollection::getNumberHistograms() const { return m_WsVec[0]->getNumberHistograms(); }
161
163 const size_t periodNumber) const {
164 return m_WsVec[periodNumber]->getSpectrum(workspace_index);
165}
166
167DataObjects::EventList &EventWorkspaceCollection::getSpectrum(const size_t workspace_index, const size_t periodNumber) {
168 return m_WsVec[periodNumber]->getSpectrum(workspace_index);
169}
170
172 return m_WsVec[0]->getSpectrumToWorkspaceIndexVector(offset);
173}
175 bool dothrow) const {
176 return m_WsVec[0]->getDetectorIDToWorkspaceIndexVector(offset, dothrow);
177}
178
179Types::Core::DateAndTime EventWorkspaceCollection::getFirstPulseTime() const { return m_WsVec[0]->getFirstPulseTime(); }
180void EventWorkspaceCollection::setAllX(const HistogramData::BinEdges &x) {
181 for (auto &ws : m_WsVec) {
182 ws->setAllX(x);
183 }
184}
186 return m_WsVec[0]->getNumberEvents(); // Should be the sum across all periods?
187}
188
189void EventWorkspaceCollection::setIndexInfo(const Indexing::IndexInfo &indexInfo) {
190 const HistogramData::BinEdges edges(2);
191 std::for_each(m_WsVec.begin(), m_WsVec.end(),
192 [&indexInfo, &edges](auto &ws) { ws = create<EventWorkspace>(*ws, indexInfo, edges); });
193}
194
196 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&inst](auto &ws) { ws->setInstrument(inst); });
197}
198void EventWorkspaceCollection::setMonitorWorkspace(const std::shared_ptr<API::MatrixWorkspace> &monitorWS) {
199 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&monitorWS](auto &ws) { ws->setMonitorWorkspace(monitorWS); });
200 // TODO, do we really set the same monitor on all periods???
201}
203 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&map](auto &ws) { ws->updateSpectraUsing(map); });
204}
205
207 std::for_each(m_WsVec.begin(), m_WsVec.end(), [flag](auto &ws) { ws->mutableSample().setGeometryFlag(flag); });
208}
209
211 std::for_each(m_WsVec.begin(), m_WsVec.end(), [flag](auto &ws) { ws->mutableSample().setThickness(flag); });
212}
214 std::for_each(m_WsVec.begin(), m_WsVec.end(), [flag](auto &ws) { ws->mutableSample().setHeight(flag); });
215}
217 std::for_each(m_WsVec.begin(), m_WsVec.end(), [flag](auto &ws) { ws->mutableSample().setWidth(flag); });
218}
219
220void EventWorkspaceCollection::setTitle(const std::string &title) {
221 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&title](auto &ws) { ws->setTitle(title); });
222}
223
225 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&func](auto &ws) { func(ws); });
226}
227
230 std::for_each(m_WsVec.begin(), m_WsVec.end(), [&func](auto &ws) { ws = func(ws); });
231}
232
233//-----------------------------------------------------------------------------
237 // Since there is a mutex lock around sorting, EventWorkspaces are always
238 // safe.
239 return true;
240}
241
242} // namespace Mantid::DataHandling
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
Class to represent the axis of a workspace.
Definition: Axis.h:30
This class stores information regarding an experimental run as a series of log entries.
Definition: Run.h:38
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 setAllX(const HistogramData::BinEdges &x)
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
Defines a class to aid in creating ISIS specific run logs for periods, status etc.
Definition: ISISRunLogs.h:28
A class for holding :
Definition: EventList.h:56
This class is intended to fulfill the design specified in <https://github.com/mantidproject/documents...
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
A specialised Property class for holding a series of time-value pairs.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
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.
Definition: SpectrumInfo.h:21
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16