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 <vector>
18
19namespace Mantid {
20namespace Types {
21namespace Core {
22class DateAndTime;
23}
24} // namespace Types
25namespace Kernel {
26class Unit;
27} // namespace Kernel
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 void setX(const Kernel::cow_ptr<HistogramData::HistogramX> &X) override;
173 MantidVec &dataX() override;
174 const MantidVec &dataX() const override;
175 const MantidVec &readX() const override;
176 Kernel::cow_ptr<HistogramData::HistogramX> ptrX() const override;
177
178 MantidVec &dataDx() override;
179 const MantidVec &dataDx() const override;
180 const MantidVec &readDx() const override;
181
184 MantidVec &dataY() override { throw std::runtime_error("EventList: non-const access to Y data is not possible."); }
187 MantidVec &dataE() override { throw std::runtime_error("EventList: non-const access to E data is not possible."); }
188
189 // Allowed data accessors - read-only Y/E histogram VIEWS of an event list
192 const MantidVec &dataY() const override;
195 const MantidVec &dataE() const override;
196
197 MantidVec *makeDataY() const;
198 MantidVec *makeDataE() const;
199
200 std::size_t getNumberEvents() const override;
201 bool empty() const;
202
203 size_t getMemorySize() const override;
204
205 virtual size_t histogram_size() const;
206
207 void compressEvents(double tolerance, EventList *destination);
208 void compressEvents(double tolerance, EventList *destination,
209 const std::shared_ptr<std::vector<double>> histogram_bin_edges);
210 void compressFatEvents(const double tolerance, const Types::Core::DateAndTime &timeStart, const double seconds,
211 EventList *destination);
212 // get EventType declaration
213 void generateHistogram(const MantidVec &X, MantidVec &Y, MantidVec &E, bool skipError = false) const override;
214 void generateHistogram(const double step, const MantidVec &X, MantidVec &Y, MantidVec &E,
215 bool skipError = false) const;
216 void generateHistogramPulseTime(const MantidVec &X, MantidVec &Y, MantidVec &E,
217 bool skipError = false) const override;
218
219 void generateHistogramTimeAtSample(const MantidVec &X, MantidVec &Y, MantidVec &E, const double &tofFactor,
220 const double &tofOffset, bool skipError = false) const override;
221
222 void integrate(const double minX, const double maxX, const bool entireRange, double &sum, double &error) const;
223
224 double integrate(const double minX, const double maxX, const bool entireRange) const override;
225
226 void convertTof(std::function<double(double)> func, const int sorting = 0) override;
227
228 void convertTof(const double factor, const double offset = 0.) override;
229
230 void scaleTof(const double factor) override;
231
232 void addTof(const double offset) override;
233
234 void addPulsetime(const double seconds) override;
235
236 void addPulsetimes(const std::vector<double> &seconds) override;
237
238 void maskTof(const double tofMin, const double tofMax) override;
239 void maskCondition(const std::vector<bool> &mask) override;
240
241 void getTofs(std::vector<double> &tofs) const override;
242 double getTofMin() const override;
243 double getTofMax() const override;
244 Mantid::Types::Core::DateAndTime getPulseTimeMax() const override;
245 Mantid::Types::Core::DateAndTime getPulseTimeMin() const override;
246 void getPulseTimeMinMax(Mantid::Types::Core::DateAndTime &tMin, Mantid::Types::Core::DateAndTime &tM) const;
247 Mantid::Types::Core::DateAndTime getTimeAtSampleMax(const double &tofFactor, const double &tofOffset) const override;
248 Mantid::Types::Core::DateAndTime getTimeAtSampleMin(const double &tofFactor, const double &tofOffset) const override;
249
250 std::vector<double> getTofs() const override;
251
253 std::vector<double> getWeights() const override;
255 void getWeights(std::vector<double> &weights) const override;
256
258 std::vector<double> getWeightErrors() const override;
260 void getWeightErrors(std::vector<double> &weightErrors) const override;
261
262 std::vector<Types::Core::DateAndTime> getPulseTimes() const override;
263
265 std::vector<Types::Core::DateAndTime> getPulseTOFTimes() const;
266
268 std::vector<Types::Core::DateAndTime> getPulseTOFTimesAtSample(const double &factor, const double &shift) const;
269
270 void setTofs(const MantidVec &tofs) override;
271
272 void reverse();
273
274 void filterByPulseTime(Types::Core::DateAndTime start, Types::Core::DateAndTime stop, EventList &output) const;
275
276 void filterByPulseTime(Kernel::TimeROI const *timeRoi, EventList *output) const;
277
278 void filterInPlace(const Kernel::TimeROI *timeRoi);
279
281 void initializePartials(std::map<int, EventList *> partials) const;
282
283 void multiply(const double value, const double error = 0.0) override;
284 EventList &operator*=(const double value);
285
286 void multiply(const MantidVec &X, const MantidVec &Y, const MantidVec &E) override;
287
288 void divide(const double value, const double error = 0.0) override;
289 EventList &operator/=(const double value);
290
291 void divide(const MantidVec &X, const MantidVec &Y, const MantidVec &E) override;
292
293 void convertUnitsViaTof(Mantid::Kernel::Unit const *fromUnit, Mantid::Kernel::Unit const *toUnit);
294 void convertUnitsQuickly(const double &factor, const double &power);
295
297 HistogramData::Histogram getHistogram() const;
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::unique_ptr<std::vector<Types::Event::TofEvent>> events;
334
336 mutable std::unique_ptr<std::vector<WeightedEvent>> weightedEvents;
337
339 mutable std::unique_ptr<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 void generateCountsHistogram(const double step, const MantidVec &X, MantidVec &Y) const;
364
365public:
366 static std::optional<size_t> findLinearBin(const MantidVec &X, const double tof, const double divisor,
367 const double offset, const bool findExact = true);
368 static std::optional<size_t> findLogBin(const MantidVec &X, const double tof, const double divisor,
369 const double offset, const bool findExact = true);
370
371private:
372 static size_t findExactBin(const MantidVec &X, const double tof, const size_t n_bin);
373
374 void generateCountsHistogramPulseTime(const MantidVec &X, MantidVec &Y) const;
375
376 void generateCountsHistogramTimeAtSample(const MantidVec &X, MantidVec &Y, const double &tofFactor,
377 const double &tofOffset) const;
378
379 void generateErrorsHistogram(const MantidVec &Y, MantidVec &E) const;
380
381 void switchToWeightedEvents();
382 void switchToWeightedEventsNoTime();
383 // should not be called externally
384 void sortPulseTimeTOFDelta(const Types::Core::DateAndTime &start, const double seconds) const;
385
386 // helper functions are all internal to simplify the code
387 template <class T1, class T2> static void minusHelper(std::vector<T1> &events, const std::vector<T2> &more_events);
388 template <class T>
389 static void compressEventsHelper(const std::vector<T> &events, std::vector<WeightedEventNoTime> &out,
390 double tolerance);
391
392 template <class T>
393 static void createWeightedEvents(std::vector<WeightedEventNoTime> &out, const std::vector<double> &tof,
394 const std::vector<T> &weight, const std::vector<T> &error);
395
396 template <class T>
397 static void processWeightedEvents(const std::vector<T> &events, std::vector<WeightedEventNoTime> &out,
398 const std::shared_ptr<std::vector<double>> histogram_bin_edges,
399 struct FindBin findBin);
400
401 template <class T>
402 static void compressFatEventsHelper(const std::vector<T> &events, std::vector<WeightedEvent> &out,
403 const double tolerance, const Mantid::Types::Core::DateAndTime &timeStart,
404 const double seconds);
405
406 template <class T>
407 static void histogramForWeightsHelper(const std::vector<T> &events, const MantidVec &X, MantidVec &Y, MantidVec &E);
408 template <class T>
409 static void histogramForWeightsHelper(const std::vector<T> &events, const double step, const MantidVec &X,
410 MantidVec &Y, MantidVec &E);
411 template <class T>
412 static void integrateHelper(std::vector<T> &events, const double minX, const double maxX, const bool entireRange,
413 double &sum, double &error);
414 template <class T> void convertTofHelper(std::vector<T> &events, const std::function<double(double)> &func);
415
416 template <class T> void convertTofHelper(std::vector<T> &events, const double factor, const double offset);
417 template <class T> void addPulsetimeHelper(std::vector<T> &events, const double seconds);
418 template <class T> void addPulsetimesHelper(std::vector<T> &events, const std::vector<double> &seconds);
419 template <class T> static std::size_t maskTofHelper(std::vector<T> &events, const double tofMin, const double tofMax);
420 template <class T> static std::size_t maskConditionHelper(std::vector<T> &events, const std::vector<bool> &mask);
421
422 template <class T> static void getTofsHelper(const std::vector<T> &events, std::vector<double> &tofs);
423 template <class T> static void getWeightsHelper(const std::vector<T> &events, std::vector<double> &weights);
424 template <class T> static void getWeightErrorsHelper(const std::vector<T> &events, std::vector<double> &weightErrors);
425
427 template <typename UnaryOperation>
428 std::vector<Types::Core::DateAndTime> eventTimesCalculator(const UnaryOperation &timesCalc) const;
429
430 template <class T> static void setTofsHelper(std::vector<T> &events, const std::vector<double> &tofs);
431 template <class T>
432 static void filterByPulseTimeHelper(std::vector<T> &events, Types::Core::DateAndTime start,
433 Types::Core::DateAndTime stop, std::vector<T> &output);
434
435 template <class T>
436 static void filterByTimeROIHelper(std::vector<T> &events, const std::vector<Kernel::TimeInterval> &intervals,
437 EventList *output);
438
439 template <class T> void filterInPlaceHelper(Kernel::TimeROI const *timeRoi, typename std::vector<T> &events);
440
441 template <class T> static void multiplyHelper(std::vector<T> &events, const double value, const double error = 0.0);
442 template <class T>
443 static void multiplyHistogramHelper(std::vector<T> &events, const MantidVec &X, const MantidVec &Y,
444 const MantidVec &E);
445 template <class T>
446 static void divideHistogramHelper(std::vector<T> &events, const MantidVec &X, const MantidVec &Y, const MantidVec &E);
447 template <class T>
448 void convertUnitsViaTofHelper(typename std::vector<T> &events, Mantid::Kernel::Unit const *fromUnit,
449 Mantid::Kernel::Unit const *toUnit);
450 template <class T>
451 void convertUnitsQuicklyHelper(typename std::vector<T> &events, const double &factor, const double &power);
452};
453
454// Methods overloaded to get event vectors.
455DLLExport void getEventsFrom(EventList &el, std::vector<Types::Event::TofEvent> *&events);
456DLLExport void getEventsFrom(const EventList &el, std::vector<Types::Event::TofEvent> const *&events);
457DLLExport void getEventsFrom(EventList &el, std::vector<WeightedEvent> *&events);
458DLLExport void getEventsFrom(const EventList &el, std::vector<WeightedEvent> const *&events);
459DLLExport void getEventsFrom(EventList &el, std::vector<WeightedEventNoTime> *&events);
460DLLExport void getEventsFrom(const EventList &el, std::vector<WeightedEventNoTime> const *&events);
461
462} // namespace DataObjects
463} // 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:37
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: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:330
Mantid::API::EventType eventType
What type of event is in our list.
Definition EventList.h:342
EventWorkspaceMRU * mru
MRU lists of the parent EventWorkspace.
Definition EventList.h:348
EventSortType order
Last sorting order.
Definition EventList.h:345
MantidVec & dataE() override
Deprecated, use mutableE() instead.
Definition EventList.h:187
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:184
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)
std::unique_ptr< std::vector< WeightedEvent > > weightedEvents
List of WeightedEvent's.
Definition EventList.h:336
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:339
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:105
std::unique_ptr< std::vector< Types::Event::TofEvent > > events
List of TofEvent (no weights).
Definition EventList.h:333
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
TimeROI : Object that holds information about when the time measurement was active.
Definition TimeROI.h:18
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: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)