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
14
15#include <iosfwd>
16#include <optional>
17#include <span>
18#include <vector>
19
20namespace Mantid {
21namespace Types {
22namespace Core {
23class DateAndTime;
24}
25} // namespace Types
26namespace Kernel {
27class Unit;
28} // namespace Kernel
29namespace DataObjects {
30class EventWorkspaceMRU;
31
41
42//==========================================================================================
58class MANTID_DATAOBJECTS_DLL EventList : public Mantid::API::IEventList {
59public:
61
63
64 EventList(const EventList &rhs);
65
66 EventList(const std::vector<Types::Event::TofEvent> &events);
67
68 EventList(const std::vector<WeightedEvent> &events);
69
70 EventList(const std::vector<WeightedEventNoTime> &events);
71
72 ~EventList() override;
73
74 void copyDataFrom(const ISpectrum &source) override;
75
76 void createFromHistogram(const ISpectrum *inSpec, bool GenerateZeros, bool GenerateMultipleEvents,
77 int MaxEventsPerBin);
78
79 EventList &operator=(const EventList &);
80
81 EventList &operator+=(const Types::Event::TofEvent &event);
82
83 EventList &operator+=(const std::vector<Types::Event::TofEvent> &more_events);
84
85 EventList &operator+=(const WeightedEvent &event);
86
87 EventList &operator+=(const std::vector<WeightedEvent> &more_events);
88
89 EventList &operator+=(const std::vector<WeightedEventNoTime> &more_events);
90
91 EventList &operator+=(const EventList &more_events);
92
93 EventList &operator-=(const EventList &more_events);
94
95 bool operator==(const EventList &rhs) const;
96 bool operator!=(const EventList &rhs) const;
97 bool equals(const EventList &rhs, const double tolTof, const double tolWeight, const int64_t tolPulse) const;
98
99 // --------------------------------------------------------------------------
106 inline void addEventQuickly(const Types::Event::TofEvent &event) {
107 this->events->emplace_back(event);
108 if (this->order != UNSORTED)
109 this->setSortOrder(UNSORTED);
110 }
111
112 // --------------------------------------------------------------------------
117 inline void addEventQuickly(const WeightedEvent &event) {
118 this->weightedEvents->emplace_back(event);
119 if (this->order != UNSORTED)
120 this->setSortOrder(UNSORTED);
121 }
122
123 // --------------------------------------------------------------------------
128 inline void addEventQuickly(const WeightedEventNoTime &event) {
129 this->weightedEventsNoTime->emplace_back(event);
130 if (this->order != UNSORTED)
131 this->setSortOrder(UNSORTED);
132 }
133
134 Mantid::API::EventType getEventType() const override;
135
136 void switchTo(Mantid::API::EventType newType) override;
137
138 WeightedEvent getEvent(size_t event_number);
139
140 std::vector<Types::Event::TofEvent> &getEvents();
141 const std::vector<Types::Event::TofEvent> &getEvents() const;
142
143 std::vector<WeightedEvent> &getWeightedEvents();
144 const std::vector<WeightedEvent> &getWeightedEvents() const;
145
146 std::vector<WeightedEventNoTime> &getWeightedEventsNoTime();
147 const std::vector<WeightedEventNoTime> &getWeightedEventsNoTime() const;
148
149 void clear(const bool removeDetIDs = true) override;
150 void clearUnused();
151
152 void setMRU(EventWorkspaceMRU *newMRU);
153
154 void clearData() override;
155
156 void reserve(size_t num) override;
157
158 void sort(const EventSortType order) const;
159
160 void setSortOrder(const EventSortType order) const;
161
162 void sortTof() const;
163
164 void sortPulseTime() const;
165 void sortPulseTimeTOF() const;
166 void sortTimeAtSample(const double &tofFactor, const double &tofShift, bool forceResort = false) const;
167
168 bool isSortedByTof() const override;
169
170 EventSortType getSortType() const;
171
172 // X-vector accessors. These reset the MRU for this spectrum
173 [[deprecated("use setSharedX() instead")]]
174 void setX(const Kernel::cow_ptr<HistogramData::HistogramX> &X) override;
175 [[deprecated("use mutableX() instead")]]
176 MantidVec &dataX() override;
177 [[deprecated("use x() instead")]]
178 const MantidVec &dataX() const override;
179 [[deprecated("use x() instead")]]
180 const MantidVec &readX() const override;
181 [[deprecated("use sharedX() instead")]]
182 Kernel::cow_ptr<HistogramData::HistogramX> ptrX() const override;
183
184 [[deprecated("use mutableDx() instead")]]
185 MantidVec &dataDx() override;
186 [[deprecated("use dx() instead")]]
187 const MantidVec &dataDx() const override;
188 [[deprecated("use dx() instead")]]
189 const MantidVec &readDx() const override;
190
193 [[deprecated("use mutableY() instead")]]
194 MantidVec &dataY() override {
195 throw std::runtime_error("EventList: non-const access to Y data is not possible.");
196 }
199 [[deprecated("use mutableE() instead")]]
200 MantidVec &dataE() override {
201 throw std::runtime_error("EventList: non-const access to E data is not possible.");
202 }
203
204 // Allowed data accessors - read-only Y/E histogram VIEWS of an event list
207 [[deprecated("use y() instead")]]
208 const MantidVec &dataY() const override;
211 [[deprecated("use e() instead")]]
212 const MantidVec &dataE() const override;
213
214 MantidVec *makeDataY() const;
215 MantidVec *makeDataE() const;
216
217 std::size_t getNumberEvents() const override;
218 bool empty() const;
219
220 size_t getMemorySize() const override;
221
222 virtual size_t histogram_size() const;
223
224 void compressEvents(double tolerance, EventList *destination);
225 void compressEvents(double tolerance, EventList *destination,
226 const std::shared_ptr<std::vector<double>> histogram_bin_edges);
227 void compressFatEvents(const double tolerance, const Types::Core::DateAndTime &timeStart, const double seconds,
228 EventList *destination);
229 // get EventType declaration
230 void generateHistogram(std::span<double const> X, MantidVec &Y, MantidVec &E, bool skipError = false) const override;
231 void generateHistogram(const double step, std::span<double const> X, MantidVec &Y, MantidVec &E,
232 bool skipError = false) const;
233 void generateHistogramPulseTime(std::span<double const> X, MantidVec &Y, MantidVec &E,
234 bool skipError = false) const override;
235
236 void generateHistogramTimeAtSample(std::span<double const> X, MantidVec &Y, MantidVec &E, const double &tofFactor,
237 const double &tofOffset, bool skipError = false) const override;
238
239 void integrate(const double minX, const double maxX, const bool entireRange, double &sum, double &error) const;
240
241 double integrate(const double minX, const double maxX, const bool entireRange) const override;
242
243 void convertTof(std::function<double(double)> func, const int sorting = 0) override;
244
245 void convertTof(const double factor, const double offset = 0.) override;
246
247 void scaleTof(const double factor) override;
248
249 void addTof(const double offset) override;
250
251 void addPulsetime(const double seconds) override;
252
253 void addPulsetimes(const std::vector<double> &seconds) override;
254
255 void maskTof(const double tofMin, const double tofMax) override;
256 void maskCondition(const std::vector<bool> &mask) override;
257
258 void getTofs(std::vector<double> &tofs) const override;
259 double getTofMin() const override;
260 double getTofMax() const override;
261 Mantid::Types::Core::DateAndTime getPulseTimeMax() const override;
262 Mantid::Types::Core::DateAndTime getPulseTimeMin() const override;
263 void getPulseTimeMinMax(Mantid::Types::Core::DateAndTime &tMin, Mantid::Types::Core::DateAndTime &tM) const;
264 Mantid::Types::Core::DateAndTime getTimeAtSampleMax(const double &tofFactor, const double &tofOffset) const override;
265 Mantid::Types::Core::DateAndTime getTimeAtSampleMin(const double &tofFactor, const double &tofOffset) const override;
266
267 std::vector<double> getTofs() const override;
268
270 std::vector<double> getWeights() const override;
272 void getWeights(std::vector<double> &weights) const override;
273
275 std::vector<double> getWeightErrors() const override;
277 void getWeightErrors(std::vector<double> &weightErrors) const override;
278
279 std::vector<Types::Core::DateAndTime> getPulseTimes() const override;
280
282 std::vector<Types::Core::DateAndTime> getPulseTOFTimes() const;
283
285 std::vector<Types::Core::DateAndTime> getPulseTOFTimesAtSample(const double &factor, const double &shift) const;
286
287 void setTofs(const MantidVec &tofs) override;
288
289 void reverse();
290
291 void filterByPulseTime(Types::Core::DateAndTime start, Types::Core::DateAndTime stop, EventList &output) const;
292
293 void filterByPulseTime(Kernel::TimeROI const *timeRoi, EventList *output) const;
294
295 void filterInPlace(const Kernel::TimeROI *timeRoi);
296
298 void initializePartials(std::map<int, EventList *> partials) const;
299
300 void multiply(const double value, const double error = 0.0) override;
301 EventList &operator*=(const double value);
302
303 void multiply(std::span<double const> X, std::span<double const> Y, std::span<double const> E) override;
304
305 void divide(const double value, const double error = 0.0) override;
306 EventList &operator/=(const double value);
307
308 void divide(std::span<double const> X, std::span<double const> Y, std::span<double const> E) override;
309
310 void convertUnitsViaTof(Mantid::Kernel::Unit const *fromUnit, Mantid::Kernel::Unit const *toUnit);
311 void convertUnitsQuickly(const double &factor, const double &power);
312
314 HistogramData::Histogram getHistogram() const;
317 HistogramData::Histogram histogram() const override;
318 HistogramData::Counts counts() const override;
319 HistogramData::CountVariances countVariances() const override;
320 HistogramData::CountStandardDeviations countStandardDeviations() const override;
321 HistogramData::Frequencies frequencies() const override;
322 HistogramData::FrequencyVariances frequencyVariances() const override;
323 HistogramData::FrequencyStandardDeviations frequencyStandardDeviations() const override;
324 const HistogramData::HistogramY &y() const override;
325 const HistogramData::HistogramE &e() const override;
326 Kernel::cow_ptr<HistogramData::HistogramY> sharedY() const override;
327 Kernel::cow_ptr<HistogramData::HistogramE> sharedE() const override;
328
329 void generateCountsHistogramPulseTime(const double &xMin, const double &xMax, MantidVec &Y,
330 const double TofMin = std::numeric_limits<double>::lowest(),
331 const double TofMax = std::numeric_limits<double>::max()) const;
332
333protected:
334 void checkAndSanitizeHistogram(HistogramData::Histogram &histogram) override;
335 void checkWorksWithPoints() const override;
336 void checkIsYAndEWritable() const override;
337
338private:
339 using ISpectrum::copyDataInto;
340 void copyDataInto(EventList &sink) const override;
341 void copyDataInto(Histogram1D &sink) const override;
342
343 const HistogramData::Histogram &histogramRef() const override { return m_histogram; }
344 HistogramData::Histogram &mutableHistogramRef() override;
345
347 HistogramData::Histogram m_histogram;
348
350 mutable std::unique_ptr<std::vector<Types::Event::TofEvent>> events;
351
353 mutable std::unique_ptr<std::vector<WeightedEvent>> weightedEvents;
354
356 mutable std::unique_ptr<std::vector<WeightedEventNoTime>> weightedEventsNoTime;
357
360
363
366
368 mutable std::mutex m_sortMutex;
369
370 template <class T>
371 static typename std::vector<T>::const_iterator findFirstPulseEvent(const std::vector<T> &events,
372 const double seek_pulsetime);
373
374 template <class T>
375 typename std::vector<T>::const_iterator findFirstTimeAtSampleEvent(const std::vector<T> &events,
376 const double seek_time, const double &tofFactor,
377 const double &tofOffset) const;
378
379 void generateCountsHistogram(std::span<double const> X, MantidVec &Y) const;
380 void generateCountsHistogram(const double step, std::span<double const> X, MantidVec &Y) const;
381
382public:
383 static std::optional<size_t> findLinearBin(std::span<double const> X, const double tof, const double divisor,
384 const double offset, const bool findExact = true);
385 static std::optional<size_t> findLogBin(std::span<double const> X, const double tof, const double divisor,
386 const double offset, const bool findExact = true);
387
388private:
389 static size_t findExactBin(std::span<double const> X, const double tof, const size_t n_bin);
390
391 void generateCountsHistogramPulseTime(std::span<double const> X, MantidVec &Y) const;
392
393 void generateCountsHistogramTimeAtSample(std::span<double const> X, MantidVec &Y, const double &tofFactor,
394 const double &tofOffset) const;
395
396 void generateErrorsHistogram(std::span<double const> Y, MantidVec &E) const;
397
398 void switchToWeightedEvents();
399 void switchToWeightedEventsNoTime();
400 // should not be called externally
401 void sortPulseTimeTOFDelta(const Types::Core::DateAndTime &start, const double seconds) const;
402
403 // helper functions are all internal to simplify the code
404 template <class T1, class T2> static void minusHelper(std::vector<T1> &events, const std::vector<T2> &more_events);
405 template <class T>
406 static void compressEventsHelper(const std::vector<T> &events, std::vector<WeightedEventNoTime> &out,
407 double tolerance);
408
409 template <class T>
410 static void createWeightedEvents(std::vector<WeightedEventNoTime> &out, const std::vector<double> &tof,
411 const std::vector<T> &weight, const std::vector<T> &error);
412
413 template <class T>
414 static void processWeightedEvents(const std::vector<T> &events, std::vector<WeightedEventNoTime> &out,
415 const std::shared_ptr<std::vector<double>> histogram_bin_edges,
416 struct FindBin findBin);
417
418 template <class T>
419 static void compressFatEventsHelper(const std::vector<T> &events, std::vector<WeightedEvent> &out,
420 const double tolerance, const Mantid::Types::Core::DateAndTime &timeStart,
421 const double seconds);
422
423 template <class T>
424 static void histogramForWeightsHelper(const std::vector<T> &events, std::span<double const> X, MantidVec &Y,
425 MantidVec &E);
426 template <class T>
427 static void histogramForWeightsHelper(const std::vector<T> &events, const double step, std::span<double const> X,
428 MantidVec &Y, MantidVec &E);
429 template <class T>
430 static void integrateHelper(std::vector<T> &events, const double minX, const double maxX, const bool entireRange,
431 double &sum, double &error);
432 template <class T> void convertTofHelper(std::vector<T> &events, const std::function<double(double)> &func);
433
434 template <class T> void convertTofHelper(std::vector<T> &events, const double factor, const double offset);
435 template <class T> void addPulsetimeHelper(std::vector<T> &events, const double seconds);
436 template <class T> void addPulsetimesHelper(std::vector<T> &events, const std::vector<double> &seconds);
437 template <class T> static std::size_t maskTofHelper(std::vector<T> &events, const double tofMin, const double tofMax);
438 template <class T> static std::size_t maskConditionHelper(std::vector<T> &events, const std::vector<bool> &mask);
439
440 template <class T> static void getTofsHelper(const std::vector<T> &events, std::vector<double> &tofs);
441 template <class T> static void getWeightsHelper(const std::vector<T> &events, std::vector<double> &weights);
442 template <class T> static void getWeightErrorsHelper(const std::vector<T> &events, std::vector<double> &weightErrors);
443
445 template <typename UnaryOperation>
446 std::vector<Types::Core::DateAndTime> eventTimesCalculator(const UnaryOperation &timesCalc) const;
447
448 template <class T> static void setTofsHelper(std::vector<T> &events, const std::vector<double> &tofs);
449 template <class T>
450 static void filterByPulseTimeHelper(std::vector<T> &events, Types::Core::DateAndTime start,
451 Types::Core::DateAndTime stop, std::vector<T> &output);
452
453 template <class T>
454 static void filterByTimeROIHelper(std::vector<T> &events, const std::vector<Kernel::TimeInterval> &intervals,
455 EventList *output);
456
457 template <class T> void filterInPlaceHelper(Kernel::TimeROI const *timeRoi, typename std::vector<T> &events);
458
459 template <class T> static void multiplyHelper(std::vector<T> &events, const double value, const double error = 0.0);
460 template <class T>
461 static void multiplyHistogramHelper(std::vector<T> &events, std::span<double const> X, std::span<double const> Y,
462 std::span<double const> E);
463 template <class T>
464 static void divideHistogramHelper(std::vector<T> &events, std::span<double const> X, std::span<double const> Y,
465 std::span<double const> E);
466 template <class T>
467 void convertUnitsViaTofHelper(typename std::vector<T> &events, Mantid::Kernel::Unit const *fromUnit,
468 Mantid::Kernel::Unit const *toUnit);
469 template <class T>
470 void convertUnitsQuicklyHelper(typename std::vector<T> &events, const double &factor, const double &power);
471};
472
473// Methods overloaded to get event vectors.
474DLLExport void getEventsFrom(EventList &el, std::vector<Types::Event::TofEvent> *&events);
475DLLExport void getEventsFrom(const EventList &el, std::vector<Types::Event::TofEvent> const *&events);
476DLLExport void getEventsFrom(EventList &el, std::vector<WeightedEvent> *&events);
477DLLExport void getEventsFrom(const EventList &el, std::vector<WeightedEvent> const *&events);
478DLLExport void getEventsFrom(EventList &el, std::vector<WeightedEventNoTime> *&events);
479DLLExport void getEventsFrom(const EventList &el, std::vector<WeightedEventNoTime> const *&events);
480
481} // namespace DataObjects
482} // namespace Mantid
const std::vector< double > & rhs
double value
The value of the point.
Definition FitMW.cpp:51
double error
double tolerance
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition System.h:33
IEventList : Interface to Mantid::DataObjects::EventList class, used to expose to PythonAPI.
Definition IEventList.h:27
A "spectrum" is an object that holds the data for a particular spectrum, in particular:
Definition ISpectrum.h:38
A class for holding :
Definition EventList.h:58
HistogramData::Histogram m_histogram
Histogram object holding the histogram data. Currently only X.
Definition EventList.h:347
Mantid::API::EventType eventType
What type of event is in our list.
Definition EventList.h:359
EventWorkspaceMRU * mru
MRU lists of the parent EventWorkspace.
Definition EventList.h:365
EventSortType order
Last sorting order.
Definition EventList.h:362
MantidVec & dataE() override
Deprecated, use mutableE() instead.
Definition EventList.h:200
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:194
std::mutex m_sortMutex
Mutex that is locked while sorting an event list.
Definition EventList.h:368
static void filterByPulseTimeHelper(std::vector< T > &events, Types::Core::DateAndTime start, Types::Core::DateAndTime stop, std::vector< T > &output)
std::unique_ptr< std::vector< WeightedEvent > > weightedEvents
List of WeightedEvent's.
Definition EventList.h:353
void addEventQuickly(const WeightedEvent &event)
Append an event to the histogram, without clearing the cache, to make it faster.
Definition EventList.h:117
std::vector< Types::Core::DateAndTime > eventTimesCalculator(const UnaryOperation &timesCalc) const
Compute a time (for instance, pulse-time plus TOF) associated to each event in the list.
void addEventQuickly(const WeightedEventNoTime &event)
Append an event to the histogram, without clearing the cache, to make it faster.
Definition EventList.h:128
std::unique_ptr< std::vector< WeightedEventNoTime > > weightedEventsNoTime
List of WeightedEvent's.
Definition EventList.h:356
const HistogramData::Histogram & histogramRef() const override
Definition EventList.h:343
void addEventQuickly(const Types::Event::TofEvent &event)
Append an event to the histogram, without clearing the cache, to make it faster.
Definition EventList.h:106
std::unique_ptr< std::vector< Types::Event::TofEvent > > events
List of TofEvent (no weights).
Definition EventList.h:350
This is a container for the MRU (most-recently-used) list of generated histograms.
1D histogram implementation.
Definition Histogram1D.h:19
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
TimeROI : Object that holds information about when the time measurement was active.
Definition TimeROI.h:18
The base units (abstract) class.
Definition Unit.h:42
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:19
DLLExport void getEventsFrom(EventList &el, std::vector< Types::Event::TofEvent > *&events)
EventSortType
How the event list is sorted.
Definition EventList.h:33
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:14
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)