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
36// Apply visibility attribute for non-MSVC builds (needed for clang/OSX).
37#if defined(_MSC_VER)
38template <size_t nd> class MDEvent : public MDLeanEvent<nd> {
39#else
40template <size_t nd> class MANTID_DATAOBJECTS_DLL MDEvent : public MDLeanEvent<nd> {
41#endif
42protected:
47 uint16_t expInfoIndex;
48
51
53 int32_t detectorId;
54
55public:
56 // Enum to flag this templated type as a full md event type.
57 enum { is_full_mdevent = true };
58
59 //---------------------------------------------------------------------------------------------
61 MDEvent() : MDLeanEvent<nd>(), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
62
63 //---------------------------------------------------------------------------------------------
69 MDEvent(const float signal, const float errorSquared)
70 : MDLeanEvent<nd>(signal, errorSquared), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
71
72 //---------------------------------------------------------------------------------------------
78 MDEvent(const double signal, const double errorSquared)
79 : MDLeanEvent<nd>(signal, errorSquared), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
80
81 //---------------------------------------------------------------------------------------------
92 MDEvent(const double signal, const double errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex,
93 const int32_t detectorId)
94 : MDLeanEvent<nd>(signal, errorSquared), expInfoIndex(expInfoIndex), goniometerIndex(goniometerIndex),
95 detectorId(detectorId) {}
96
97 //---------------------------------------------------------------------------------------------
108 MDEvent(const float signal, const float errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex,
109 const int32_t detectorId)
110 : MDLeanEvent<nd>(signal, errorSquared), expInfoIndex(expInfoIndex), goniometerIndex(goniometerIndex),
111 detectorId(detectorId) {}
112
113 //---------------------------------------------------------------------------------------------
121 MDEvent(const float signal, const float errorSquared, const coord_t *centers)
122 : MDLeanEvent<nd>(signal, errorSquared, centers), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
123 //---------------------------------------------------------------------------------------------
131 MDEvent(const double signal, const double errorSquared, const coord_t *centers)
132 : MDLeanEvent<nd>(signal, errorSquared, centers), expInfoIndex(0), goniometerIndex(0), detectorId(0) {}
133 //---------------------------------------------------------------------------------------------
147 MDEvent(const float signal, const float 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 MDEvent(const double signal, const double errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex,
153 const int32_t detectorId, const coord_t *centers)
154 : MDLeanEvent<nd>(signal, errorSquared, centers), expInfoIndex(expInfoIndex), goniometerIndex(goniometerIndex),
155 detectorId(detectorId) {}
156
157#ifdef COORDT_IS_FLOAT
158 //---------------------------------------------------------------------------------------------
172 MDEvent(const float signal, const float errorSquared, const uint16_t expInfoIndex, const uint16_t goniometerIndex,
173 const int32_t detectorId, const double *centers)
174 : MDLeanEvent<nd>(signal, errorSquared, centers), expInfoIndex(expInfoIndex), goniometerIndex(goniometerIndex),
175 detectorId(detectorId) {}
176#endif
177
178 //---------------------------------------------------------------------------------------------
180 uint16_t getExpInfoIndex() const { return expInfoIndex; }
181
184 void setExpInfoIndex(uint16_t index) { expInfoIndex = index; }
185
186 //---------------------------------------------------------------------------------------------
188 uint16_t getGoniometerIndex() const { return goniometerIndex; }
189
192 void setGoniometerIndex(uint16_t index) { goniometerIndex = index; }
193
194 //---------------------------------------------------------------------------------------------
196 int32_t getDetectorID() const { return detectorId; }
197
200 void setDetectorId(int32_t id) { detectorId = id; }
201
202 //---------------------------------------------------------------------------------------------
204 static std::string getTypeName() { return "MDEvent"; }
205
206 /* static method used to convert vector of lean events into vector of their
207 coordinates & signal and error
208 @param events -- vector of events
209 @return data -- vector of events data, namely, their signal and error
210 casted to coord_t type
211 @return ncols -- the number of colunts in the data (it is nd+4 here but
212 may be different for other data types)
213 @return totalSignal -- total signal in the vector of events
214 @return totalErr -- total error corresponting to the vector of events
215 */
216 static inline void eventsToData(const std::vector<MDEvent<nd>> &events, std::vector<coord_t> &data, size_t &ncols,
217 double &totalSignal, double &totalErrSq) {
218 ncols = (nd + 5); // nd+signal+error+run+goniom+detID
219 size_t nEvents = events.size();
220 data.resize(nEvents * ncols);
221
222 totalSignal = 0;
223 totalErrSq = 0;
224
225 size_t index(0);
226 for (const auto &event : events) {
227 float signal = event.signal;
228 float errorSquared = event.errorSquared;
229 data[index++] = static_cast<coord_t>(signal);
230 data[index++] = static_cast<coord_t>(errorSquared);
231 // Additional stuff for MDEvent
232 data[index++] = static_cast<coord_t>(event.expInfoIndex);
233 data[index++] = static_cast<coord_t>(event.goniometerIndex);
234 data[index++] = static_cast<coord_t>(event.detectorId);
235 for (size_t d = 0; d < nd; d++)
236 data[index++] = event.center[d];
237 // Track the total signal
238 totalSignal += signal_t(signal);
239 totalErrSq += signal_t(errorSquared);
240 }
241 }
242 /* static method used to convert vector of data into vector of lean events
243 @return data -- vector of events coordinates, their signal and error
244 casted to coord_t type
245 @param events -- vector of events
246 @param reserveMemory -- reserve memory for events copying. Set to false if
247 one wants to add new events to the existing one.
248 */
249 static inline void dataToEvents(const std::vector<coord_t> &data, std::vector<MDEvent<nd>> &events,
250 bool reserveMemory = true) {
251 // Number of columns = number of dimensions + 5
252 // (signal/error)+detId+gonID+runID
253 size_t numColumns = (nd + 5);
254 size_t numEvents = data.size() / numColumns;
255 if (numEvents * numColumns != data.size())
256 throw(std::invalid_argument("wrong input array of data to convert to "
257 "lean events, suspected column data for "
258 "different dimensions/(type of) events "));
259
260 if (reserveMemory) // Reserve the amount of space needed. Significant speed
261 // up (~30% thanks to this)
262 {
263 events.clear();
264 events.reserve(numEvents);
265 }
266 for (size_t i = 0; i < numEvents; i++) {
267 // Index into the data array
268 size_t ii = i * numColumns;
269
270 // Point directly into the data block for the centers.
271 coord_t const *const centers = &(data[ii + 5]);
272
273 // Create the event with signal, errorSquared, expInfoIndex,
274 // goniometerIndex, detectorID, and the centers, in this order
275 events.emplace_back(static_cast<signal_t>(data[ii]), static_cast<signal_t>(data[ii + 1]),
276 static_cast<uint16_t>(data[ii + 2]), static_cast<uint16_t>(data[ii + 3]),
277 static_cast<int32_t>(data[ii + 4]), centers);
278 }
279 }
280};
281
282} // namespace DataObjects
283} // namespace Mantid
std::map< DeltaEMode::Type, std::string > index
Templated class holding data about a neutron detection event in N-dimensions (for example,...
Definition MDEvent.h:40
void setGoniometerIndex(uint16_t index)
Sets the expInfoIndex of this event.
Definition MDEvent.h:192
static std::string getTypeName()
Definition MDEvent.h:204
static void eventsToData(const std::vector< MDEvent< nd > > &events, std::vector< coord_t > &data, size_t &ncols, double &totalSignal, double &totalErrSq)
Definition MDEvent.h:216
uint16_t getExpInfoIndex() const
Definition MDEvent.h:180
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:152
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:147
MDEvent()
Empty constructor.
Definition MDEvent.h:61
uint16_t expInfoIndex
0-based index of which run this event belongs to.
Definition MDEvent.h:47
MDEvent(const float signal, const float errorSquared, const coord_t *centers)
Constructor with signal and error and an array of centers.
Definition MDEvent.h:121
void setDetectorId(int32_t id)
Sets the detectorId of this event.
Definition MDEvent.h:200
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:108
void setExpInfoIndex(uint16_t index)
Sets the expInfoIndex of this event.
Definition MDEvent.h:184
int32_t detectorId
Detector ID of the pixel that measured this event.
Definition MDEvent.h:53
int32_t getDetectorID() const
Definition MDEvent.h:196
MDEvent(const double signal, const double errorSquared, const coord_t *centers)
Constructor with signal and error and an array of centers.
Definition MDEvent.h:131
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:92
uint16_t goniometerIndex
0-based index determines the goniometer settings when this event occurred
Definition MDEvent.h:50
MDEvent(const float signal, const float errorSquared)
Constructor with signal and error.
Definition MDEvent.h:69
uint16_t getGoniometerIndex() const
Definition MDEvent.h:188
static void dataToEvents(const std::vector< coord_t > &data, std::vector< MDEvent< nd > > &events, bool reserveMemory=true)
Definition MDEvent.h:249
MDEvent(const double signal, const double errorSquared)
Constructor with signal and error.
Definition MDEvent.h:78
Templated class holding data about a neutron detection event in N-dimensions (for example,...
Definition MDLeanEvent.h:64
std::size_t numEvents(Nexus::File &file, bool &hasTotalCounts, bool &oldNeXusFileNames, const std::string &prefix)
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