Mantid
Loading...
Searching...
No Matches
MDEvent.h
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 +
7#pragma once
8
13#include <cmath>
14#include <numeric>
15
16namespace Mantid {
17namespace DataObjects {
18
36template <size_t nd> class MANTID_DATAOBJECTS_DLL MDEvent : public MDLeanEvent<nd> {
37protected:
42 uint16_t expInfoIndex;
43
46
48 int32_t detectorId;
49
50public:
51 // Enum to flag this templated type as a full md event type.
52 enum { is_full_mdevent = true };
53
54 //---------------------------------------------------------------------------------------------
56 MDEvent() : MDLeanEvent<nd>(), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
57
58 //---------------------------------------------------------------------------------------------
64 MDEvent(const float signal, const float errorSquared)
65 : MDLeanEvent<nd>(signal, errorSquared), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
66
67 //---------------------------------------------------------------------------------------------
73 MDEvent(const double signal, const double errorSquared)
74 : MDLeanEvent<nd>(signal, errorSquared), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
75
76 //---------------------------------------------------------------------------------------------
87 MDEvent(const double signal, const double errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex,
88 const int32_t detectorId)
89 : MDLeanEvent<nd>(signal, errorSquared), expInfoIndex(expInfoIndex), goniometerIndex(goniometerIndex),
90 detectorId(detectorId) {}
91
92 //---------------------------------------------------------------------------------------------
103 MDEvent(const float signal, const float errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex,
104 const int32_t detectorId)
105 : MDLeanEvent<nd>(signal, errorSquared), expInfoIndex(expInfoIndex), goniometerIndex(goniometerIndex),
106 detectorId(detectorId) {}
107
108 //---------------------------------------------------------------------------------------------
116 MDEvent(const float signal, const float errorSquared, const coord_t *centers)
117 : MDLeanEvent<nd>(signal, errorSquared, centers), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
118 //---------------------------------------------------------------------------------------------
126 MDEvent(const double signal, const double errorSquared, const coord_t *centers)
127 : MDLeanEvent<nd>(signal, errorSquared, centers), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
128 //---------------------------------------------------------------------------------------------
142 MDEvent(const float signal, const float errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex,
143 const int32_t detectorId, const coord_t *centers)
144 : MDLeanEvent<nd>(signal, errorSquared, centers), expInfoIndex(expInfoIndex), goniometerIndex(goniometerIndex),
145 detectorId(detectorId) {}
146
147 MDEvent(const double signal, const double errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex,
148 const int32_t detectorId, const coord_t *centers)
149 : MDLeanEvent<nd>(signal, errorSquared, centers), expInfoIndex(expInfoIndex), goniometerIndex(goniometerIndex),
150 detectorId(detectorId) {}
151
152#ifdef COORDT_IS_FLOAT
153 //---------------------------------------------------------------------------------------------
167 MDEvent(const float signal, const float errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex,
168 const int32_t detectorId, const double *centers)
169 : MDLeanEvent<nd>(signal, errorSquared, centers), expInfoIndex(expInfoIndex), goniometerIndex(goniometerIndex),
170 detectorId(detectorId) {}
171#endif
172
173 //---------------------------------------------------------------------------------------------
175 uint16_t getExpInfoIndex() const { return expInfoIndex; }
176
179 void setExpInfoIndex(uint16_t index) { expInfoIndex = index; }
180
181 //---------------------------------------------------------------------------------------------
183 uint16_t getGoniometerIndex() const { return goniometerIndex; }
184
187 void setGoniometerIndex(uint16_t index) { goniometerIndex = index; }
188
189 //---------------------------------------------------------------------------------------------
191 int32_t getDetectorID() const { return detectorId; }
192
195 void setDetectorId(int32_t id) { detectorId = id; }
196
197 //---------------------------------------------------------------------------------------------
199 static std::string getTypeName() { return "MDEvent"; }
200
201 /* static method used to convert vector of lean events into vector of their
202 coordinates & signal and error
203 @param events -- vector of events
204 @return data -- vector of events data, namely, their signal and error
205 casted to coord_t type
206 @return ncols -- the number of colunts in the data (it is nd+4 here but
207 may be different for other data types)
208 @return totalSignal -- total signal in the vector of events
209 @return totalErr -- total error corresponting to the vector of events
210 */
211 static inline void eventsToData(const std::vector<MDEvent<nd>> &events, std::vector<coord_t> &data, size_t &ncols,
212 double &totalSignal, double &totalErrSq) {
213 ncols = (nd + 5); // nd+signal+error+run+goniom+detID
214 size_t nEvents = events.size();
215 data.resize(nEvents * ncols);
216
217 totalSignal = 0;
218 totalErrSq = 0;
219
220 size_t index(0);
221 for (const auto &event : events) {
222 float signal = event.signal;
223 float errorSquared = event.errorSquared;
224 data[index++] = static_cast<coord_t>(signal);
225 data[index++] = static_cast<coord_t>(errorSquared);
226 // Additional stuff for MDEvent
227 data[index++] = static_cast<coord_t>(event.expInfoIndex);
228 data[index++] = static_cast<coord_t>(event.goniometerIndex);
229 data[index++] = static_cast<coord_t>(event.detectorId);
230 for (size_t d = 0; d < nd; d++)
231 data[index++] = event.center[d];
232 // Track the total signal
233 totalSignal += signal_t(signal);
234 totalErrSq += signal_t(errorSquared);
235 }
236 }
237 /* static method used to convert vector of data into vector of lean events
238 @return data -- vector of events coordinates, their signal and error
239 casted to coord_t type
240 @param events -- vector of events
241 @param reserveMemory -- reserve memory for events copying. Set to false if
242 one wants to add new events to the existing one.
243 */
244 static inline void dataToEvents(const std::vector<coord_t> &data, std::vector<MDEvent<nd>> &events,
245 bool reserveMemory = true) {
246 // Number of columns = number of dimensions + 5
247 // (signal/error)+detId+gonID+runID
248 size_t numColumns = (nd + 5);
249 size_t numEvents = data.size() / numColumns;
250 if (numEvents * numColumns != data.size())
251 throw(std::invalid_argument("wrong input array of data to convert to "
252 "lean events, suspected column data for "
253 "different dimensions/(type of) events "));
254
255 if (reserveMemory) // Reserve the amount of space needed. Significant speed
256 // up (~30% thanks to this)
257 {
258 events.clear();
259 events.reserve(numEvents);
260 }
261 for (size_t i = 0; i < numEvents; i++) {
262 // Index into the data array
263 size_t ii = i * numColumns;
264
265 // Point directly into the data block for the centers.
266 coord_t const *const centers = &(data[ii + 5]);
267
268 // Create the event with signal, errorSquared, expInfoIndex,
269 // goniometerIndex, detectorID, and the centers, in this order
270 events.emplace_back(static_cast<signal_t>(data[ii]), static_cast<signal_t>(data[ii + 1]),
271 static_cast<uint16_t>(data[ii + 2]), static_cast<uint16_t>(data[ii + 3]),
272 static_cast<int32_t>(data[ii + 4]), centers);
273 }
274 }
275};
276
277} // namespace DataObjects
278} // namespace Mantid
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
Templated class holding data about a neutron detection event in N-dimensions (for example,...
Definition: MDEvent.h:36
void setGoniometerIndex(uint16_t index)
Sets the expInfoIndex of this event.
Definition: MDEvent.h:187
static std::string getTypeName()
Definition: MDEvent.h:199
static void eventsToData(const std::vector< MDEvent< nd > > &events, std::vector< coord_t > &data, size_t &ncols, double &totalSignal, double &totalErrSq)
Definition: MDEvent.h:211
uint16_t getExpInfoIndex() const
Definition: MDEvent.h:175
MDEvent(const double signal, const double errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex, const int32_t detectorId, const coord_t *centers)
Definition: MDEvent.h:147
MDEvent(const float signal, const float errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex, const int32_t detectorId, const coord_t *centers)
Constructor with signal and error and an array of centers, and the expInfoIndex and detectorID.
Definition: MDEvent.h:142
MDEvent()
Empty constructor.
Definition: MDEvent.h:56
uint16_t expInfoIndex
0-based index of which run this event belongs to.
Definition: MDEvent.h:42
MDEvent(const float signal, const float errorSquared, const coord_t *centers)
Constructor with signal and error and an array of centers.
Definition: MDEvent.h:116
void setDetectorId(int32_t id)
Sets the detectorId of this event.
Definition: MDEvent.h:195
MDEvent(const float signal, const float errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex, const int32_t detectorId)
Constructor with signal, error, expInfoIndex and detectorId.
Definition: MDEvent.h:103
void setExpInfoIndex(uint16_t index)
Sets the expInfoIndex of this event.
Definition: MDEvent.h:179
int32_t detectorId
Detector ID of the pixel that measured this event.
Definition: MDEvent.h:48
int32_t getDetectorID() const
Definition: MDEvent.h:191
MDEvent(const double signal, const double errorSquared, const coord_t *centers)
Constructor with signal and error and an array of centers.
Definition: MDEvent.h:126
MDEvent(const double signal, const double errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex, const int32_t detectorId)
Constructor with signal, error, expInfoIndex and detectorId.
Definition: MDEvent.h:87
uint16_t goniometerIndex
0-based index determines the goniometer settings when this event occurred
Definition: MDEvent.h:45
MDEvent(const float signal, const float errorSquared)
Constructor with signal and error.
Definition: MDEvent.h:64
uint16_t getGoniometerIndex() const
Definition: MDEvent.h:183
static void dataToEvents(const std::vector< coord_t > &data, std::vector< MDEvent< nd > > &events, bool reserveMemory=true)
Definition: MDEvent.h:244
MDEvent(const double signal, const double errorSquared)
Constructor with signal and error.
Definition: MDEvent.h:73
Templated class holding data about a neutron detection event in N-dimensions (for example,...
Definition: MDLeanEvent.h:60
std::size_t numEvents(::NeXus::File &file, bool &hasTotalCounts, bool &oldNeXusFileNames, const std::string &prefix, const NexusHDF5Descriptor &descriptor)
Get the number of events in the currently opened group.
Helper class which provides the Collimation Length for SANS instruments.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition: MDTypes.h:27
double signal_t
Typedef for the signal recorded in a MDBox, etc.
Definition: MDTypes.h:36