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