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
30namespace dunedaq {
31
32namespace daqdataformats {
33
38{
39public:
49
54 inline explicit Fragment(const std::vector<std::pair<void*, size_t>>& pieces);
60 inline Fragment(void* buffer, size_t size);
66 inline explicit Fragment(void* existing_fragment_buffer, BufferAdoptionMode adoption_mode);
67
68 Fragment(Fragment const&) = delete;
69 Fragment& operator=(Fragment const&) = delete;
71 {
72 m_alloc = other.m_alloc;
73 other.m_alloc = false;
74 m_data_arr = other.m_data_arr;
75 }
77 {
78 m_alloc = other.m_alloc;
79 other.m_alloc = false;
80 m_data_arr = other.m_data_arr;
81 return *this;
82 }
83
87 inline ~Fragment();
92 FragmentHeader get_header() const { return *header_(); }
99 inline void set_header_fields(const FragmentHeader& header);
100
105 const void* get_storage_location() const { return m_data_arr; }
106
107 // Header setters and getters
117 void set_trigger_number(trigger_number_t trigger_number) { header_()->trigger_number = trigger_number; }
127 void set_run_number(run_number_t run_number) { header_()->run_number = run_number; }
128
138 void set_trigger_timestamp(timestamp_t trigger_timestamp) { header_()->trigger_timestamp = trigger_timestamp; }
148 void set_window_begin(timestamp_t window_begin) { header_()->window_begin = window_begin; }
158 void set_window_end(timestamp_t window_end) { header_()->window_end = window_end; }
159
165
170 void set_element_id(SourceID element_id) { header_()->element_id = element_id; }
171
177 uint16_t get_detector_id() const noexcept { return header_()->detector_id; }
178
184 void set_detector_id(const uint16_t& detector_id) noexcept { header_()->detector_id = detector_id; }
185
186
191 std::bitset<32> get_error_bits() const { return header_()->error_bits; }
196 void set_error_bits(std::bitset<32> error_bits) { header_()->error_bits = error_bits.to_ulong(); }
202 bool get_error_bit(FragmentErrorBits bit) const { return get_error_bits()[static_cast<size_t>(bit)]; }
203
209 inline void set_error_bit(FragmentErrorBits bit, bool value);
210
225 void set_type(FragmentType fragment_type) { header_()->fragment_type = static_cast<fragment_type_t>(fragment_type); }
226
237
242 fragment_size_t get_size() const { return header_()->size; }
243
249
254 void* get_data() const
255 {
256 // Increment header pointer by one to skip header
257 return static_cast<void*>(header_() + 1); // NOLINT
258 }
259
260private:
265 FragmentHeader* header_() const { return static_cast<FragmentHeader*>(m_data_arr); }
266 void* m_data_arr{ nullptr };
267 bool m_alloc{ false };
268};
269
270// ------
271
272Fragment::Fragment(const std::vector<std::pair<void*, size_t>>& pieces)
273{
274
275 size_t size = sizeof(FragmentHeader) +
276 std::accumulate(pieces.begin(), pieces.end(), 0ULL, [](const size_t& a, const std::pair<void*, size_t>& b) { return a + b.second; });
277
278 if (size < sizeof(FragmentHeader)) {
279 throw std::length_error("The Fragment size is smaller than the Fragment header size.");
280 }
281
282 m_data_arr = malloc(size); // NOLINT(build/unsigned)
283 if (m_data_arr == nullptr) {
284 throw std::bad_alloc();
285 }
286 m_alloc = true;
287
288 FragmentHeader header;
289 header.size = size;
290 memcpy(m_data_arr, &header, sizeof(header));
291
292 size_t offset = sizeof(FragmentHeader);
293 for (auto& piece : pieces) {
294 if (piece.first == nullptr) {
295 throw std::invalid_argument("The Fragment buffer point to NULL.");
296 }
297 memcpy(static_cast<uint8_t*>(m_data_arr) + offset, piece.first, piece.second); // NOLINT(build/unsigned)
298 offset += piece.second;
299 }
300}
301
302Fragment::Fragment(void* buffer, size_t size)
303 : Fragment({ std::make_pair(buffer, size) })
304{}
305
306Fragment::Fragment(void* existing_fragment_buffer, BufferAdoptionMode adoption_mode)
307{
308 if (adoption_mode == BufferAdoptionMode::kReadOnlyMode) {
309 m_data_arr = existing_fragment_buffer;
310 } else if (adoption_mode == BufferAdoptionMode::kTakeOverBuffer) {
311 m_data_arr = existing_fragment_buffer;
312 m_alloc = true;
313 } else if (adoption_mode == BufferAdoptionMode::kCopyFromBuffer) {
314 auto header = reinterpret_cast<FragmentHeader*>(existing_fragment_buffer); // NOLINT
315 m_data_arr = malloc(header->size);
316 if (m_data_arr == nullptr) {
317 throw std::bad_alloc();
318 }
319 m_alloc = true;
320 memcpy(m_data_arr, existing_fragment_buffer, header->size);
321 }
322}
323
325{
326 if (m_alloc)
327 free(m_data_arr);
328}
329
330void
344
345void
347
348{
349 auto bits = get_error_bits();
350 bits[static_cast<size_t>(bit)] = value;
351 set_error_bits(bits);
352}
353
354} // namespace daqdataformats
355} // namespace dunedaq
356
357#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:38
timestamp_t get_trigger_timestamp() const
Get the trigger_timestamp field from the header.
Definition Fragment.hpp:133
bool get_error_bit(FragmentErrorBits bit) const
Get the value of a designated error bit.
Definition Fragment.hpp:202
sequence_number_t get_sequence_number() const
Get the sequence_number field from the header.
Definition Fragment.hpp:231
void set_run_number(run_number_t run_number)
Set the run_number for the Fragment.
Definition Fragment.hpp:127
fragment_type_t get_fragment_type_code() const
Get the fragment_type_t value stored in the header.
Definition Fragment.hpp:215
timestamp_t get_window_begin() const
Get the window_begin field from the header.
Definition Fragment.hpp:143
timestamp_t get_window_end() const
Get the window_end field from the header.
Definition Fragment.hpp:153
void set_window_begin(timestamp_t window_begin)
Set the window_begin for the Fragment.
Definition Fragment.hpp:148
run_number_t get_run_number() const
Get the run_number field from the header.
Definition Fragment.hpp:122
BufferAdoptionMode
Describes how the "existing Fragment buffer" Constructor should treat the given buffer.
Definition Fragment.hpp:44
@ 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:164
FragmentType get_fragment_type() const
Get the fragment_type header field.
Definition Fragment.hpp:220
void set_header_fields(const FragmentHeader &header)
Copy fields from the provided header in this Fragment's header.
Definition Fragment.hpp:331
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:138
~Fragment()
Fragment destructor.
Definition Fragment.hpp:324
std::bitset< 32 > get_error_bits() const
Get the error_bits header field.
Definition Fragment.hpp:191
void set_sequence_number(sequence_number_t number)
Set the sequence_number for the Fragment.
Definition Fragment.hpp:236
void set_window_end(timestamp_t window_end)
Set the window_end for the Fragment.
Definition Fragment.hpp:158
Fragment(Fragment const &)=delete
Fragment copy constructor is deleted.
FragmentHeader * header_() const
Get the FragmentHeader from the m_data_arr array.
Definition Fragment.hpp:265
void set_trigger_number(trigger_number_t trigger_number)
Set the trigger_number for the Fragment.
Definition Fragment.hpp:117
FragmentHeader get_header() const
Get a copy of the FragmentHeader struct.
Definition Fragment.hpp:92
uint16_t get_detector_id() const noexcept
Get the DetID for the Fragment.
Definition Fragment.hpp:177
void set_element_id(SourceID element_id)
Set the SourceID for the Fragment.
Definition Fragment.hpp:170
void * get_data() const
Get a pointer to the data payload in the Fragmnet.
Definition Fragment.hpp:254
Fragment(const std::vector< std::pair< void *, size_t > > &pieces)
Fragment constructor using a vector of buffer pointers.
Definition Fragment.hpp:272
Fragment & operator=(Fragment &&other)
Definition Fragment.hpp:76
void set_error_bits(std::bitset< 32 > error_bits)
Overwrite the error_bits header field.
Definition Fragment.hpp:196
fragment_size_t get_data_size() const
Get the size of the Fragment data.
Definition Fragment.hpp:248
bool m_alloc
Whether the Fragment owns the memory pointed by m_data_arr.
Definition Fragment.hpp:267
fragment_size_t get_size() const
Get the total size of the Fragment.
Definition Fragment.hpp:242
void set_type(FragmentType fragment_type)
Set the fragment_type header field.
Definition Fragment.hpp:225
const void * get_storage_location() const
Get a pointer to the Fragment's data array to read its contents directly.
Definition Fragment.hpp:105
trigger_number_t get_trigger_number() const
Get the trigger_number field from the header.
Definition Fragment.hpp:112
void set_detector_id(const uint16_t &detector_id) noexcept
Set the DetID for the Fragment.
Definition Fragment.hpp:184
void * m_data_arr
Flat memory containing a FragmentHeader and the data payload.
Definition Fragment.hpp:266
void set_error_bit(FragmentErrorBits bit, bool value)
Set the designated error bit.
Definition Fragment.hpp:346
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:45
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
FragmentErrorBits
This enumeration should list all defined error bits, as well as a short documentation of their meanin...
Including Qt Headers.
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.
uint32_t error_bits
Error bits set by the Upstream DAQ.
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.
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