DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Fragment.hpp
Go to the documentation of this file.
1
13#ifndef DAQDATAFORMATS_INCLUDE_DAQDATAFORMATS_FRAGMENT_HPP_
14#define DAQDATAFORMATS_INCLUDE_DAQDATAFORMATS_FRAGMENT_HPP_
15
19
20#include <bitset>
21#include <cstdint>
22#include <cstdlib>
23#include <cstring>
24#include <new>
25#include <numeric>
26#include <stdexcept>
27#include <utility>
28#include <vector>
29
31
36{
37public:
47
52 inline explicit Fragment(const std::vector<std::pair<void*, size_t>>& pieces);
58 inline Fragment(void* buffer, size_t size);
64 inline explicit Fragment(void* existing_fragment_buffer, BufferAdoptionMode adoption_mode);
65
66 Fragment(Fragment const&) = delete;
67 Fragment& operator=(Fragment const&) = delete;
69 {
70 m_alloc = other.m_alloc;
71 other.m_alloc = false;
72 m_data_arr = other.m_data_arr;
73 }
75 {
76 m_alloc = other.m_alloc;
77 other.m_alloc = false;
78 m_data_arr = other.m_data_arr;
79 return *this;
80 }
81
85 inline ~Fragment();
90 FragmentHeader get_header() const { return *header_(); }
97 inline void set_header_fields(const FragmentHeader& header);
98
103 const void* get_storage_location() const { return m_data_arr; }
104
105 // Header setters and getters
115 void set_trigger_number(trigger_number_t trigger_number) { header_()->trigger_number = trigger_number; }
125 void set_run_number(run_number_t run_number) { header_()->run_number = run_number; }
126
136 void set_trigger_timestamp(timestamp_t trigger_timestamp) { header_()->trigger_timestamp = trigger_timestamp; }
146 void set_window_begin(timestamp_t window_begin) { header_()->window_begin = window_begin; }
156 void set_window_end(timestamp_t window_end) { header_()->window_end = window_end; }
157
163
168 void set_element_id(SourceID element_id) { header_()->element_id = element_id; }
169
174 uint16_t get_detector_id() const noexcept { return header_()->detector_id; } // NOLINT
175
180 void set_detector_id(const uint16_t& detector_id) noexcept { header_()->detector_id = detector_id; } // NOLINT
181
186 std::bitset<32> get_status_bits() const { return header_()->status_bits; }
191 void set_status_bits(std::bitset<32> status_bits) { header_()->status_bits = status_bits.to_ulong(); }
197 bool get_status_bit(FragmentStatusBits bit) const { return get_status_bits()[static_cast<size_t>(bit)]; }
198
204 inline void set_status_bit(FragmentStatusBits bit, bool value);
205
220 void set_type(FragmentType fragment_type) { header_()->fragment_type = static_cast<fragment_type_t>(fragment_type); }
221
232
237 fragment_size_t get_size() const { return header_()->size; }
238
243 fragment_size_t get_data_size() const { return header_()->size - sizeof(FragmentHeader); }
244
249 void* get_data() const
250 {
251 // Increment header pointer by one to skip header
252 return static_cast<void*>(header_() + 1); // NOLINT
253 }
254
255private:
260 FragmentHeader* header_() const { return static_cast<FragmentHeader*>(m_data_arr); }
261 void* m_data_arr{ nullptr };
262 bool m_alloc{ false };
263};
264
265// ------
266
267Fragment::Fragment(const std::vector<std::pair<void*, size_t>>& pieces)
268{
269
270 size_t size =
271 sizeof(FragmentHeader) +
272 std::accumulate(pieces.begin(), pieces.end(), 0ULL, [](const size_t& a, const std::pair<void*, size_t>& b) {
273 return a + b.second;
274 });
275
276 if (size < sizeof(FragmentHeader)) {
277 throw std::length_error("The Fragment size is smaller than the Fragment header size.");
278 }
279
280 m_data_arr = malloc(size); // NOLINT
281 if (m_data_arr == nullptr) {
282 throw std::bad_alloc();
283 }
284 m_alloc = true;
285
286 FragmentHeader header;
287 header.size = size;
288 memcpy(m_data_arr, &header, sizeof(header));
289
290 size_t offset = sizeof(FragmentHeader);
291 for (auto& piece : pieces) {
292 if (piece.first == nullptr) {
293 throw std::invalid_argument("The Fragment buffer point to NULL.");
294 }
295 memcpy(static_cast<uint8_t*>(m_data_arr) + offset, piece.first, piece.second); // NOLINT
296 offset += piece.second;
297 }
298}
299
300Fragment::Fragment(void* buffer, size_t size)
301 : Fragment({ std::make_pair(buffer, size) })
302{
303}
304
305Fragment::Fragment(void* existing_fragment_buffer, BufferAdoptionMode adoption_mode)
306{
307 if (adoption_mode == BufferAdoptionMode::kReadOnlyMode) {
308 m_data_arr = existing_fragment_buffer;
309 } else if (adoption_mode == BufferAdoptionMode::kTakeOverBuffer) {
310 m_data_arr = existing_fragment_buffer;
311 m_alloc = true;
312 } else if (adoption_mode == BufferAdoptionMode::kCopyFromBuffer) {
313 auto header = reinterpret_cast<FragmentHeader*>(existing_fragment_buffer); // NOLINT
314 m_data_arr = malloc(header->size); // NOLINT
315 if (m_data_arr == nullptr) {
316 throw std::bad_alloc();
317 }
318 m_alloc = true;
319 memcpy(m_data_arr, existing_fragment_buffer, header->size);
320 }
321}
322
324{
325 if (m_alloc)
326 free(m_data_arr); // NOLINT
327}
328
329void
343
344void
346
347{
348 auto bits = get_status_bits();
349 bits[static_cast<size_t>(bit)] = value;
350 set_status_bits(bits);
351}
352
353} // namespace dunedaq::daqdataformats
354
355#endif // DAQDATAFORMATS_INCLUDE_DAQDATAFORMATS_FRAGMENT_HPP_
C++ Representation of a DUNE Fragment, wrapping the flat byte array that is the Fragment's "actual" f...
Definition Fragment.hpp:36
timestamp_t get_trigger_timestamp() const
Get the trigger_timestamp field from the header.
Definition Fragment.hpp:131
bool get_status_bit(FragmentStatusBits bit) const
Get the value of a designated status bit.
Definition Fragment.hpp:197
sequence_number_t get_sequence_number() const
Get the sequence_number field from the header.
Definition Fragment.hpp:226
void set_status_bit(FragmentStatusBits bit, bool value)
Set the designated status bit.
Definition Fragment.hpp:345
void set_run_number(run_number_t run_number)
Set the run_number for the Fragment.
Definition Fragment.hpp:125
fragment_type_t get_fragment_type_code() const
Get the fragment_type_t value stored in the header.
Definition Fragment.hpp:210
void set_status_bits(std::bitset< 32 > status_bits)
Overwrite the status_bits header field.
Definition Fragment.hpp:191
timestamp_t get_window_begin() const
Get the window_begin field from the header.
Definition Fragment.hpp:141
timestamp_t get_window_end() const
Get the window_end field from the header.
Definition Fragment.hpp:151
void set_window_begin(timestamp_t window_begin)
Set the window_begin for the Fragment.
Definition Fragment.hpp:146
run_number_t get_run_number() const
Get the run_number field from the header.
Definition Fragment.hpp:120
BufferAdoptionMode
Describes how the "existing Fragment buffer" Constructor should treat the given buffer.
Definition Fragment.hpp:42
@ kCopyFromBuffer
Copy the contents of the buffer into a new Fragment array.
@ kReadOnlyMode
Just use the buffer in non-owning mode.
@ kTakeOverBuffer
Take over control of the buffer.
SourceID get_element_id() const
Get the SourceID for the Fragment.
Definition Fragment.hpp:162
FragmentType get_fragment_type() const
Get the fragment_type header field.
Definition Fragment.hpp:215
void set_header_fields(const FragmentHeader &header)
Copy fields from the provided header in this Fragment's header.
Definition Fragment.hpp:330
Fragment & operator=(Fragment const &)=delete
Fragment copy assignment operator is deleted.
void set_trigger_timestamp(timestamp_t trigger_timestamp)
Set the trigger_timestamp for the Fragment.
Definition Fragment.hpp:136
~Fragment()
Fragment destructor.
Definition Fragment.hpp:323
void set_sequence_number(sequence_number_t number)
Set the sequence_number for the Fragment.
Definition Fragment.hpp:231
void set_window_end(timestamp_t window_end)
Set the window_end for the Fragment.
Definition Fragment.hpp:156
Fragment(Fragment const &)=delete
Fragment copy constructor is deleted.
FragmentHeader * header_() const
Get the FragmentHeader from the m_data_arr array.
Definition Fragment.hpp:260
void set_trigger_number(trigger_number_t trigger_number)
Set the trigger_number for the Fragment.
Definition Fragment.hpp:115
FragmentHeader get_header() const
Get a copy of the FragmentHeader struct.
Definition Fragment.hpp:90
uint16_t get_detector_id() const noexcept
Get the DetID for the Fragment.
Definition Fragment.hpp:174
void set_element_id(SourceID element_id)
Set the SourceID for the Fragment.
Definition Fragment.hpp:168
void * get_data() const
Get a pointer to the data payload in the Fragmnet.
Definition Fragment.hpp:249
Fragment(const std::vector< std::pair< void *, size_t > > &pieces)
Fragment constructor using a vector of buffer pointers.
Definition Fragment.hpp:267
Fragment & operator=(Fragment &&other)
Definition Fragment.hpp:74
fragment_size_t get_data_size() const
Get the size of the Fragment data.
Definition Fragment.hpp:243
bool m_alloc
Whether the Fragment owns the memory pointed by m_data_arr.
Definition Fragment.hpp:262
std::bitset< 32 > get_status_bits() const
Get the status_bits header field.
Definition Fragment.hpp:186
fragment_size_t get_size() const
Get the total size of the Fragment.
Definition Fragment.hpp:237
void set_type(FragmentType fragment_type)
Set the fragment_type header field.
Definition Fragment.hpp:220
const void * get_storage_location() const
Get a pointer to the Fragment's data array to read its contents directly.
Definition Fragment.hpp:103
trigger_number_t get_trigger_number() const
Get the trigger_number field from the header.
Definition Fragment.hpp:110
void set_detector_id(const uint16_t &detector_id) noexcept
Set the DetID for the Fragment.
Definition Fragment.hpp:180
void * m_data_arr
Flat memory containing a FragmentHeader and the data payload.
Definition Fragment.hpp:261
double offset
uint32_t run_number_t
Type used to represent run number.
Definition Types.hpp:20
FragmentType
This enumeration should list all defined Fragment types.
uint64_t trigger_number_t
Type used to represent trigger number.
Definition Types.hpp:24
uint16_t sequence_number_t
Type used to represent sequence within a trigger record.
Definition Types.hpp:48
uint64_t timestamp_t
Type used to represent DUNE timing system timestamps.
Definition Types.hpp:36
uint32_t fragment_type_t
Type used to represent Fragment type ID.
Definition Types.hpp:28
uint64_t fragment_size_t
Type used to represent Fragment size.
Definition Types.hpp:32
FragmentStatusBits
This enumeration should list all defined status bits, as well as a short documentation of their meani...
FELIX Initialization std::string initerror FELIX queue timed std::string queuename Unexpected chunk size
The header for a DUNE Fragment.
timestamp_t trigger_timestamp
Timestamp of the TriggerDecision.
fragment_type_t fragment_type
Type of the Fragment, indicating the format of the contained payload.
SourceID element_id
Component that generated the data in this Fragment.
uint16_t detector_id
Identifier for the subdetector that produced the raw data in the Fragment payload.
run_number_t run_number
Run number this Fragment is associated with.
sequence_number_t sequence_number
Sequence number of this Fragment within a trigger record.
uint32_t status_bits
Status bits set by the Upstream DAQ.
trigger_number_t trigger_number
Trigger Number this Fragment is associated with.
timestamp_t window_end
Window end of data in the Fragment.
timestamp_t window_begin
Window begin of data in the Fragment.
fragment_size_t size
Size of the Fragment (including header and payload)
SourceID is a generalized representation of the source of a piece of data in the DAQ....
Definition SourceID.hpp:32