Mantid
Loading...
Searching...
No Matches
EventList.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2010 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 +
7#pragma once
8
13#include <iosfwd>
14#include <vector>
15
16namespace Mantid {
17namespace Types {
18namespace Core {
19class DateAndTime;
20}
21} // namespace Types
22namespace Kernel {
23class SplittingInterval;
24using TimeSplitterType = std::vector<SplittingInterval>;
25class Unit;
26} // namespace Kernel
27namespace DataObjects {
28class EventWorkspaceMRU;
29
38};
39
40//==========================================================================================
56class MANTID_DATAOBJECTS_DLL EventList : public Mantid::API::IEventList {
57public:
58 EventList();
59
61
62 EventList(const EventList &rhs);
63
64 EventList(const std::vector<Types::Event::TofEvent> &events);
65
66 EventList(const std::vector<WeightedEvent> &events);
67
68 EventList(const std::vector<WeightedEventNoTime> &events);
69
70 ~EventList() override;
71
72 void copyDataFrom(const ISpectrum &source) override;
73
74 void createFromHistogram(const ISpectrum *inSpec, bool GenerateZeros, bool GenerateMultipleEvents,
75 int MaxEventsPerBin);
76
77 EventList &operator=(const EventList &);
78
79 EventList &operator+=(const Types::Event::TofEvent &event);
80
81 EventList &operator+=(const std::vector<Types::Event::TofEvent> &more_events);
82
83 EventList &operator+=(const WeightedEvent &event);
84
85 EventList &operator+=(const std::vector<WeightedEvent> &more_events);
86
87 EventList &operator+=(const std::vector<WeightedEventNoTime> &more_events);
88
89 EventList &operator+=(const EventList &more_events);
90
91 EventList &operator-=(const EventList &more_events);
92
93 bool operator==(const EventList &rhs) const;
94 bool operator!=(const EventList &rhs) const;
95 bool equals(const EventList &rhs, const double tolTof, const double tolWeight, const int64_t tolPulse) const;
96
97 // --------------------------------------------------------------------------
104 inline void addEventQuickly(const Types::Event::TofEvent &event) {
105 this->events.emplace_back(event);
106 this->order = UNSORTED;
107 }
108
109 // --------------------------------------------------------------------------
114 inline void addEventQuickly(const WeightedEvent &event) {
115 this->weightedEvents.emplace_back(event);
116 this->order = UNSORTED;
117 }
118
119 // --------------------------------------------------------------------------
124 inline void addEventQuickly(const WeightedEventNoTime &event) {
125 this->weightedEventsNoTime.emplace_back(event);
126 this->order = UNSORTED;
127 }
128
129 Mantid::API::EventType getEventType() const override;
130
131 void switchTo(Mantid::API::EventType newType) override;
132
133 WeightedEvent getEvent(size_t event_number);
134
135 std::vector<Types::Event::TofEvent> &getEvents();
136 const std::vector<Types::Event::TofEvent> &getEvents() const;
137
138 std::vector<WeightedEvent> &getWeightedEvents();
139 const std::vector<WeightedEvent> &getWeightedEvents() const;
140
141 std::vector<WeightedEventNoTime> &getWeightedEventsNoTime();
142 const std::vector<WeightedEventNoTime> &getWeightedEventsNoTime() const;
143
144 void clear(const bool removeDetIDs = true) override;
145 void clearUnused();
146
147 void setMRU(EventWorkspaceMRU *newMRU);
148
149 void clearData() override;
150
151 void reserve(size_t num) override;
152
153 void sort(const EventSortType order) const;
154
155 void setSortOrder(const EventSortType order) const;
156
157 void sortTof() const;
158
159 void sortPulseTime() const;
160 void sortPulseTimeTOF() const;
161 void sortTimeAtSample(const double &tofFactor, const double &tofShift, bool forceResort = false) const;
162
163 bool isSortedByTof() const override;
164
165 EventSortType getSortType() const;
166
167 // X-vector accessors. These reset the MRU for this spectrum
168 void setX(const Kernel::cow_ptr<HistogramData::HistogramX> &X) override;
169 MantidVec &dataX() override;
170 const MantidVec &dataX() const override;
171 const MantidVec &readX() const override;
172 Kernel::cow_ptr<HistogramData::HistogramX> ptrX() const override;
173
174 MantidVec &dataDx() override;
175 const MantidVec &dataDx() const override;
176 const MantidVec &readDx() const override;
177
180 MantidVec &dataY() override { throw std::runtime_error("EventList: non-const access to Y data is not possible."); }
183 MantidVec &dataE() override { throw std::runtime_error("EventList: non-const access to E data is not possible."); }
184
185 // Allowed data accessors - read-only Y/E histogram VIEWS of an event list
188 const MantidVec &dataY() const override;
191 const MantidVec &dataE() const override;
192
193 MantidVec *makeDataY() const;
194 MantidVec *makeDataE() const;
195
196 std::size_t getNumberEvents() const override;
197 bool empty() const;
198
199 size_t getMemorySize() const override;
200
201 virtual size_t histogram_size() const;
202
203 void compressEvents(double tolerance, EventList *destination);
204 void compressFatEvents(const double tolerance, const Types::Core::DateAndTime &timeStart, const double seconds,
205 EventList *destination);
206 // get EventType declaration
207 void generateHistogram(const MantidVec &X, MantidVec &Y, MantidVec &E, bool skipError = false) const override;
208 void generateHistogramPulseTime(const MantidVec &X, MantidVec &Y, MantidVec &E,
209 bool skipError = false) const override;
210
211 void generateHistogramTimeAtSample(const MantidVec &X, MantidVec &Y, MantidVec &E, const double &tofFactor,
212 const double &tofOffset, bool skipError = false) const override;
213
214 void integrate(const double minX, const double maxX, const bool entireRange, double &sum, double &error) const;
215
216 double integrate(const double minX, const double maxX, const bool entireRange) const override;
217
218 void convertTof(std::function<double(double)> func, const int sorting = 0) override;
219
220 void convertTof(const double factor, const double offset = 0.) override;
221
222 void scaleTof(const double factor) override;
223
224 void addTof(const double offset) override;
225
226 void addPulsetime(const double seconds) override;
227
228 void addPulsetimes(const std::vector<double> &seconds) override;
229
230 void maskTof(const double tofMin, const double tofMax) override;
231 void maskCondition(const std::vector<bool> &mask) override;
232
233 void getTofs(std::vector<double> &tofs) const override;
234 double getTofMin() const override;
235 double getTofMax() const override;
236 Mantid::Types::Core::DateAndTime getPulseTimeMax() const override;
237 Mantid::Types::Core::DateAndTime getPulseTimeMin() const override;
238 void getPulseTimeMinMax(Mantid::Types::Core::DateAndTime &tMin, Mantid::Types::Core::DateAndTime &tM) const;
239 Mantid::Types::Core::DateAndTime getTimeAtSampleMax(const double &tofFactor, const double &tofOffset) const override;
240 Mantid::Types::Core::DateAndTime getTimeAtSampleMin(const double &tofFactor, const double &tofOffset) const override;
241
242 std::vector<double> getTofs() const override;
243
245 std::vector<double> getWeights() const override;
247 void getWeights(std::vector<double> &weights) const override;
248
250 std::vector<double> getWeightErrors() const override;
252 void getWeightErrors(std::vector<double> &weightErrors) const override;
253
254 std::vector<Mantid::Types::Core::DateAndTime> getPulseTimes() const override;
255
256 void setTofs(const MantidVec &tofs) override;
257
258 void reverse();
259
260 void filterByPulseTime(Types::Core::DateAndTime start, Types::Core::DateAndTime stop, EventList &output) const;
261
262 void filterByTimeAtSample(Types::Core::DateAndTime start, Types::Core::DateAndTime stop, double tofFactor,
263 double tofOffset, EventList &output) const;
264
265 void filterInPlace(Kernel::TimeSplitterType &splitter);
266
267 void splitByTime(Kernel::TimeSplitterType &splitter, std::vector<EventList *> outputs) const;
268
269 void splitByFullTime(Kernel::TimeSplitterType &splitter, std::map<int, EventList *> outputs, bool docorrection,
270 double toffactor, double tofshift) const;
271
273 std::string splitByFullTimeMatrixSplitter(const std::vector<int64_t> &vec_splitters_time,
274 const std::vector<int> &vecgroups,
275 std::map<int, EventList *> vec_outputEventList, bool docorrection,
276 double toffactor, double tofshift) const;
277
279 void splitByPulseTime(Kernel::TimeSplitterType &splitter, std::map<int, EventList *> outputs) const;
280
282 void splitByPulseTimeWithMatrix(const std::vector<int64_t> &vec_times, const std::vector<int> &vec_target,
283 std::map<int, EventList *> outputs) const;
284
285 void multiply(const double value, const double error = 0.0) override;
286 EventList &operator*=(const double value);
287
288 void multiply(const MantidVec &X, const MantidVec &Y, const MantidVec &E) override;
289
290 void divide(const double value, const double error = 0.0) override;
291 EventList &operator/=(const double value);
292
293 void divide(const MantidVec &X, const MantidVec &Y, const MantidVec &E) override;
294
295 void convertUnitsViaTof(Mantid::Kernel::Unit *fromUnit, Mantid::Kernel::Unit *toUnit);
296 void convertUnitsQuickly(const double &factor, const double &power);
297
300 HistogramData::Histogram histogram() const override;
301 HistogramData::Counts counts() const override;
302 HistogramData::CountVariances countVariances() const override;
303 HistogramData::CountStandardDeviations countStandardDeviations() const override;
304 HistogramData::Frequencies frequencies() const override;
305 HistogramData::FrequencyVariances frequencyVariances() const override;
306 HistogramData::FrequencyStandardDeviations frequencyStandardDeviations() const override;
307 const HistogramData::HistogramY &y() const override;
308 const HistogramData::HistogramE &e() const override;
309 Kernel::cow_ptr<HistogramData::HistogramY> sharedY() const override;
310 Kernel::cow_ptr<HistogramData::HistogramE> sharedE() const override;
311
312 void generateCountsHistogramPulseTime(const double &xMin, const double &xMax, MantidVec &Y,
313 const double TofMin = std::numeric_limits<double>::lowest(),
314 const double TofMax = std::numeric_limits<double>::max()) const;
315
316protected:
317 void checkAndSanitizeHistogram(HistogramData::Histogram &histogram) override;
318 void checkWorksWithPoints() const override;
319 void checkIsYAndEWritable() const override;
320
321private:
322 using ISpectrum::copyDataInto;
323 void copyDataInto(EventList &sink) const override;
324 void copyDataInto(Histogram1D &sink) const override;
325
326 const HistogramData::Histogram &histogramRef() const override { return m_histogram; }
327 HistogramData::Histogram &mutableHistogramRef() override;
328
330 HistogramData::Histogram m_histogram;
331
333 mutable std::vector<Types::Event::TofEvent> events;
334
336 mutable std::vector<WeightedEvent> weightedEvents;
337
339 mutable std::vector<WeightedEventNoTime> weightedEventsNoTime;
340
343
346
349
351 mutable std::mutex m_sortMutex;
352
353 template <class T>
354 static typename std::vector<T>::const_iterator findFirstPulseEvent(const std::vector<T> &events,
355 const double seek_pulsetime);
356
357 template <class T>
358 typename std::vector<T>::const_iterator findFirstTimeAtSampleEvent(const std::vector<T> &events,
359 const double seek_time, const double &tofFactor,
360 const double &tofOffset) const;
361
362 void generateCountsHistogram(const MantidVec &X, MantidVec &Y) const;
363
364 void generateCountsHistogramPulseTime(const MantidVec &X, MantidVec &Y) const;
365
366 void generateCountsHistogramTimeAtSample(const MantidVec &X, MantidVec &Y, const double &tofFactor,
367 const double &tofOffset) const;
368
369 void generateErrorsHistogram(const MantidVec &Y, MantidVec &E) const;
370
371 void switchToWeightedEvents();
372 void switchToWeightedEventsNoTime();
373 // should not be called externally
374 void sortPulseTimeTOFDelta(const Types::Core::DateAndTime &start, const double seconds) const;
375
376 // helper functions are all internal to simplify the code
377 template <class T1, class T2> static void minusHelper(std::vector<T1> &events, const std::vector<T2> &more_events);
378 template <class T>
379 static void compressEventsHelper(const std::vector<T> &events, std::vector<WeightedEventNoTime> &out,
380 double tolerance);
381 template <class T>
382 void compressEventsParallelHelper(const std::vector<T> &events, std::vector<WeightedEventNoTime> &out,
383 double tolerance);
384 template <class T>
385 static void compressFatEventsHelper(const std::vector<T> &events, std::vector<WeightedEvent> &out,
386 const double tolerance, const Mantid::Types::Core::DateAndTime &timeStart,
387 const double seconds);
388
389 template <class T>
390 static void histogramForWeightsHelper(const std::vector<T> &events, const MantidVec &X, MantidVec &Y, MantidVec &E);
391 template <class T>
392 static void integrateHelper(std::vector<T> &events, const double minX, const double maxX, const bool entireRange,
393 double &sum, double &error);
394 template <class T>
395 static double integrateHelper(std::vector<T> &events, const double minX, const double maxX, const bool entireRange);
396 template <class T> void convertTofHelper(std::vector<T> &events, const std::function<double(double)> &func);
397
398 template <class T> void convertTofHelper(std::vector<T> &events, const double factor, const double offset);
399 template <class T> void addPulsetimeHelper(std::vector<T> &events, const double seconds);
400 template <class T> void addPulsetimesHelper(std::vector<T> &events, const std::vector<double> &seconds);
401 template <class T> static std::size_t maskTofHelper(std::vector<T> &events, const double tofMin, const double tofMax);
402 template <class T> static std::size_t maskConditionHelper(std::vector<T> &events, const std::vector<bool> &mask);
403
404 template <class T> static void getTofsHelper(const std::vector<T> &events, std::vector<double> &tofs);
405 template <class T> static void getWeightsHelper(const std::vector<T> &events, std::vector<double> &weights);
406 template <class T> static void getWeightErrorsHelper(const std::vector<T> &events, std::vector<double> &weightErrors);
407 template <class T>
408 static void getPulseTimesHelper(const std::vector<T> &events, std::vector<Mantid::Types::Core::DateAndTime> &times);
409 template <class T> static void setTofsHelper(std::vector<T> &events, const std::vector<double> &tofs);
410 template <class T>
411 static void filterByPulseTimeHelper(std::vector<T> &events, Types::Core::DateAndTime start,
412 Types::Core::DateAndTime stop, std::vector<T> &output);
413 template <class T>
414 static void filterByTimeAtSampleHelper(std::vector<T> &events, Types::Core::DateAndTime start,
415 Types::Core::DateAndTime stop, double tofFactor, double tofOffset,
416 std::vector<T> &output);
417 template <class T> void filterInPlaceHelper(Kernel::TimeSplitterType &splitter, typename std::vector<T> &events);
418 template <class T>
419 void splitByTimeHelper(Kernel::TimeSplitterType &splitter, std::vector<EventList *> outputs,
420 typename std::vector<T> &events) const;
421 template <class T>
422 void splitByFullTimeHelper(Kernel::TimeSplitterType &splitter, std::map<int, EventList *> outputs,
423 typename std::vector<T> &events, bool docorrection, double toffactor,
424 double tofshift) const;
426 template <class T>
427 void splitByPulseTimeHelper(Kernel::TimeSplitterType &splitter, std::map<int, EventList *> outputs,
428 typename std::vector<T> &events) const;
429
431 template <class T>
432 void splitByPulseTimeWithMatrixHelper(const std::vector<int64_t> &vec_split_times,
433 const std::vector<int> &vec_split_target, std::map<int, EventList *> outputs,
434 typename std::vector<T> &events) const;
435
436 template <class T>
437 std::string splitByFullTimeVectorSplitterHelper(const std::vector<int64_t> &vectimes,
438 const std::vector<int> &vecgroups, std::map<int, EventList *> outputs,
439 typename std::vector<T> &vecEvents, bool docorrection,
440 double toffactor, double tofshift) const;
441
442 template <class T>
443 std::string
444 splitByFullTimeSparseVectorSplitterHelper(const std::vector<int64_t> &vectimes, const std::vector<int> &vecgroups,
445 std::map<int, EventList *> outputs, typename std::vector<T> &vecEvents,
446 bool docorrection, double toffactor, double tofshift) const;
447
448 template <class T> static void multiplyHelper(std::vector<T> &events, const double value, const double error = 0.0);
449 template <class T>
450 static void multiplyHistogramHelper(std::vector<T> &events, const MantidVec &X, const MantidVec &Y,
451 const MantidVec &E);
452 template <class T>
453 static void divideHistogramHelper(std::vector<T> &events, const MantidVec &X, const MantidVec &Y, const MantidVec &E);
454 template <class T>
455 void convertUnitsViaTofHelper(typename std::vector<T> &events, Mantid::Kernel::Unit *fromUnit,
456 Mantid::Kernel::Unit *toUnit);
457 template <class T>
458 void convertUnitsQuicklyHelper(typename std::vector<T> &events, const double &factor, const double &power);
459};
460
461// Methods overloaded to get event vectors.
462DLLExport void getEventsFrom(EventList &el, std::vector<Types::Event::TofEvent> *&events);
463DLLExport void getEventsFrom(const EventList &el, std::vector<Types::Event::TofEvent> const *&events);
464DLLExport void getEventsFrom(EventList &el, std::vector<WeightedEvent> *&events);
465DLLExport void getEventsFrom(const EventList &el, std::vector<WeightedEvent> const *&events);
466DLLExport void getEventsFrom(EventList &el, std::vector<WeightedEventNoTime> *&events);
467DLLExport void getEventsFrom(const EventList &el, std::vector<WeightedEventNoTime> const *&events);
468
469} // namespace DataObjects
470} // namespace Mantid
const std::vector< double > & rhs
double value
The value of the point.
Definition: FitMW.cpp:51
double error
Definition: IndexPeaks.cpp:133
double tolerance
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
IEventList : Interface to Mantid::DataObjects::EventList class, used to expose to PythonAPI.
Definition: IEventList.h:26
A "spectrum" is an object that holds the data for a particular spectrum, in particular:
Definition: ISpectrum.h:39
A class for holding :
Definition: EventList.h:56
static void filterByTimeAtSampleHelper(std::vector< T > &events, Types::Core::DateAndTime start, Types::Core::DateAndTime stop, double tofFactor, double tofOffset, std::vector< T > &output)
std::vector< WeightedEventNoTime > weightedEventsNoTime
List of WeightedEvent's.
Definition: EventList.h:339
HistogramData::Histogram m_histogram
Histogram object holding the histogram data. Currently only X.
Definition: EventList.h:330
Mantid::API::EventType eventType
What type of event is in our list.
Definition: EventList.h:342
EventList(const std::vector< Types::Event::TofEvent > &events)
std::vector< Types::Event::TofEvent > events
List of TofEvent (no weights).
Definition: EventList.h:333
EventWorkspaceMRU * mru
MRU lists of the parent EventWorkspace.
Definition: EventList.h:348
EventSortType order
Last sorting order.
Definition: EventList.h:345
EventList & operator+=(const Types::Event::TofEvent &event)
MantidVec & dataE() override
Deprecated, use mutableE() instead.
Definition: EventList.h:183
static void compressFatEventsHelper(const std::vector< T > &events, std::vector< WeightedEvent > &out, const double tolerance, const Mantid::Types::Core::DateAndTime &timeStart, const double seconds)
MantidVec & dataY() override
Deprecated, use mutableY() instead.
Definition: EventList.h:180
std::mutex m_sortMutex
Mutex that is locked while sorting an event list.
Definition: EventList.h:351
static void filterByPulseTimeHelper(std::vector< T > &events, Types::Core::DateAndTime start, Types::Core::DateAndTime stop, std::vector< T > &output)
void addEventQuickly(const WeightedEvent &event)
Append an event to the histogram, without clearing the cache, to make it faster.
Definition: EventList.h:114
EventList & operator+=(const std::vector< Types::Event::TofEvent > &more_events)
std::vector< WeightedEvent > weightedEvents
List of WeightedEvent's.
Definition: EventList.h:336
void addEventQuickly(const WeightedEventNoTime &event)
Append an event to the histogram, without clearing the cache, to make it faster.
Definition: EventList.h:124
const HistogramData::Histogram & histogramRef() const override
Definition: EventList.h:326
void addEventQuickly(const Types::Event::TofEvent &event)
Append an event to the histogram, without clearing the cache, to make it faster.
Definition: EventList.h:104
This is a container for the MRU (most-recently-used) list of generated histograms.
1D histogram implementation.
Definition: Histogram1D.h:18
Info about a single neutron detection event, including a weight and error value, but excluding the pu...
Definition: Events.h:91
Info about a single neutron detection event, including a weight and error value:
Definition: Events.h:39
The base units (abstract) class.
Definition: Unit.h:41
Implements a copy on write data template.
Definition: cow_ptr.h:41
bool MANTID_API_DLL equals(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs, double tolerance=0.0)
Performs a comparison operation on two workspaces, using the CompareWorkspaces algorithm.
MatrixWorkspace_sptr MANTID_API_DLL operator/=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Divide two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator+=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Adds two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator-=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Subtracts two workspaces.
MatrixWorkspace_sptr MANTID_API_DLL operator*=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Multiply two workspaces.
EventType
What kind of event list is being stored.
Definition: IEventList.h:18
DLLExport void getEventsFrom(EventList &el, std::vector< Types::Event::TofEvent > *&events)
EventSortType
How the event list is sorted.
Definition: EventList.h:31
std::vector< SplittingInterval > TimeSplitterType
A typedef for splitting events according their pulse time.
Definition: LogManager.h:31
Helper class which provides the Collimation Length for SANS instruments.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition: cow_ptr.h:172
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16
constexpr bool operator==(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)
constexpr bool operator!=(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)