DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
TriggerRecordHeader.hpp
Go to the documentation of this file.
1
9#ifndef DAQDATAFORMATS_INCLUDE_DAQDATAFORMATS_TRIGGERRECORDHEADER_HPP_
10#define DAQDATAFORMATS_INCLUDE_DAQDATAFORMATS_TRIGGERRECORDHEADER_HPP_
11
16
17#include <bitset>
18#include <cstddef>
19#include <cstring>
20#include <new>
21#include <ostream>
22#include <stdexcept>
23#include <string>
24#include <vector>
25
27
33{
34public:
39 inline explicit TriggerRecordHeader(const std::vector<ComponentRequest>& components);
40
47 inline explicit TriggerRecordHeader(void* existing_trigger_record_header_buffer, bool copy_from_buffer = false);
48
53 inline TriggerRecordHeader(TriggerRecordHeader const& other);
60
62 {
63 m_alloc = other.m_alloc;
64 other.m_alloc = false;
65 m_data_arr = other.m_data_arr;
66 }
68 {
69 m_alloc = other.m_alloc;
70 other.m_alloc = false;
71 m_data_arr = other.m_data_arr;
72 return *this;
73 }
74
79 {
80 if (m_alloc)
81 free(m_data_arr);
82 }
83
89
99 void set_trigger_number(trigger_number_t trigger_number) { header_()->trigger_number = trigger_number; }
109 void set_trigger_timestamp(timestamp_t trigger_timestamp) { header_()->trigger_timestamp = trigger_timestamp; }
110
115 uint64_t get_num_requested_components() const // NOLINT(build/unsigned)
116 {
118 }
119
129 void set_run_number(run_number_t run_number) { header_()->run_number = run_number; }
130
135 std::bitset<32> get_error_bits() const { return header_()->error_bits; }
140 void set_error_bits(std::bitset<32> bits) { header_()->error_bits = bits.to_ulong(); }
146 bool get_error_bit(TriggerRecordErrorBits bit) const { return get_error_bits()[static_cast<size_t>(bit)]; }
153 {
154 auto bits = get_error_bits();
155 bits[static_cast<size_t>(bit)] = value;
156 set_error_bits(bits);
157 }
158
168 void set_trigger_type(trigger_type_t trigger_type) { header_()->trigger_type = trigger_type; }
169
180
191
201 void set_element_id(SourceID source_id) { header_()->element_id = source_id; }
202
207 size_t get_total_size_bytes() const
208 {
210 }
216 const void* get_storage_location() const { return m_data_arr; }
217
224 inline ComponentRequest at(size_t idx) const;
225
232 inline ComponentRequest& operator[](size_t idx);
233
240 inline ComponentRequest const& get_component_for_source_id(SourceID const& source_id) const;
241
242private:
248
250 nullptr
251 };
252 bool m_alloc{ false };
253};
254
255//------
256
257TriggerRecordHeader::TriggerRecordHeader(const std::vector<ComponentRequest>& components)
258{
259 size_t size = sizeof(TriggerRecordHeaderData) + components.size() * sizeof(ComponentRequest);
260
261 m_data_arr = malloc(size); // NOLINT(build/unsigned)
262 if (m_data_arr == nullptr) {
263 throw std::bad_alloc();
264 }
265 m_alloc = true;
266
268 header.num_requested_components = components.size();
269 std::memcpy(m_data_arr, &header, sizeof(header));
270
271 size_t offset = sizeof(header);
272 for (auto const& component : components) {
273 std::memcpy(static_cast<uint8_t*>(m_data_arr) + offset, &component, sizeof(component)); // NOLINT
274 offset += sizeof(component);
275 }
276}
277
278TriggerRecordHeader::TriggerRecordHeader(void* existing_trigger_record_header_buffer, bool copy_from_buffer)
279{
280 if (!copy_from_buffer) {
281 m_data_arr = existing_trigger_record_header_buffer;
282 } else {
283 auto header = reinterpret_cast<TriggerRecordHeaderData*>(existing_trigger_record_header_buffer); // NOLINT
284 size_t size = header->num_requested_components * sizeof(ComponentRequest) + sizeof(TriggerRecordHeaderData);
285
286 m_data_arr = malloc(size);
287 if (m_data_arr == nullptr) {
288 throw std::bad_alloc();
289 }
290 m_alloc = true;
291 std::memcpy(m_data_arr, existing_trigger_record_header_buffer, size);
292 }
293}
294
298
301{
302 if (&other == this)
303 return *this;
304
305 if (m_alloc) {
306 free(m_data_arr);
307 }
308 m_data_arr = malloc(other.get_total_size_bytes());
309 if (m_data_arr == nullptr) {
310 throw std::bad_alloc();
311 }
312 m_alloc = true;
313 std::memcpy(m_data_arr, other.m_data_arr, other.get_total_size_bytes());
314 return *this;
315}
316
318TriggerRecordHeader::at(size_t idx) const
319{
320 if (idx >= header_()->num_requested_components) {
321 throw std::range_error("Supplied ComponentRequest index is larger than the maximum index.");
322 }
323 // Increment header pointer by one to skip header
324 return *(reinterpret_cast<ComponentRequest*>(header_() + 1) + idx); // NOLINT
325}
326
329{
330 if (idx >= header_()->num_requested_components) {
331 throw std::range_error("Supplied ComponentRequest index is larger than the maximum index.");
332 }
333 // Increment header pointer by one to skip header
334 return *(reinterpret_cast<ComponentRequest*>(header_() + 1) + idx); // NOLINT
335}
336
337ComponentRequest const&
339{
340 for (uint64_t idx = 0; idx < get_num_requested_components(); ++idx) {
341 ComponentRequest const& component_obj = *(reinterpret_cast<ComponentRequest*>(header_() + 1) + idx); // NOLINT
342 if (source_id == component_obj.component) {
343 return component_obj;
344 }
345 }
346 throw std::invalid_argument("Supplied SourceID (" + source_id.to_string() + ") was not found in the ComponentRequest list.");
347}
348
349} // namespace dunedaq::daqdataformats
350
351#endif // DAQDATAFORMATS_INCLUDE_DAQDATAFORMATS_TRIGGERRECORDHEADER_HPP_
C++ representation of a TriggerRecordHeader, which wraps a flat array that is the TriggerRecordHeader...
bool m_alloc
Whether the TriggerRecordHeader owns the memory pointed by m_data_arr.
void set_sequence_number(sequence_number_t number)
Set the sequence number for this TriggerRecordHeader.
bool get_error_bit(TriggerRecordErrorBits bit) const
Get the value of the given error bit.
~TriggerRecordHeader()
TriggerRecordHeader destructor.
TriggerRecordHeader(const std::vector< ComponentRequest > &components)
Construct a TriggerRecordHeader using a vector of ComponentRequest objects.
TriggerRecordHeaderData * header_() const
Get the TriggerRecordHeaderData from the m_data_arr array.
uint64_t get_num_requested_components() const
Get the number of ComponentRequest objects stored in this TriggerRecordHeader.
void set_error_bits(std::bitset< 32 > bits)
Overwrite error bits using the given bitset.
void set_element_id(SourceID source_id)
Set the SourceID for this TriggerRecordHeader.
size_t get_total_size_bytes() const
Get the total size of the TriggerRecordHeader.
void set_error_bit(TriggerRecordErrorBits bit, bool value)
Set the given error bit to the given value.
const void * get_storage_location() const
Get the location of the flat data array for output.
void set_run_number(run_number_t run_number)
Set the run number for this TriggerRecordHeader.
void set_max_sequence_number(sequence_number_t number)
Set the maxiumum sequence number for this TriggerRecordHeader.
ComponentRequest const & get_component_for_source_id(SourceID const &source_id) const
Access ComponentRequest by SourceID.
void set_trigger_type(trigger_type_t trigger_type)
Set the trigger_type header field to the given value.
trigger_number_t get_max_sequence_number() const
Get the maximum sequence number for this TriggerRecordHeader.
trigger_type_t get_trigger_type() const
Get the trigger_type field from the data struct.
void set_trigger_timestamp(timestamp_t trigger_timestamp)
Set the trigger timestamp for this TriggerRecordHeader.
ComponentRequest & operator[](size_t idx)
Operator[] to access ComponentRequests by index.
run_number_t get_run_number() const
Get the run_number stored in this TriggerRecordHeader.
std::bitset< 32 > get_error_bits() const
Get the error_bits header field as a bitset.
SourceID get_element_id() const
Get the SourceID for this TriggerRecordHeader.
ComponentRequest at(size_t idx) const
Access ComponentRequest and copy result.
TriggerRecordHeaderData get_header() const
Get a copy of the TriggerRecordHeaderData struct.
timestamp_t get_trigger_timestamp() const
Get the trigger_timestamp stored in this TriggerRecordHeader.
TriggerRecordHeader & operator=(TriggerRecordHeader &&other)
void * m_data_arr
Flat memory containing a TriggerRecordHeaderData header and an array of ComponentRequests.
void set_trigger_number(trigger_number_t trigger_number)
Set the trigger number for this TriggerRecordHeader.
trigger_number_t get_trigger_number() const
Get the trigger number for this TriggerRecordHeader.
trigger_number_t get_sequence_number() const
Get the sequence number for this TriggerRecordHeader.
TriggerRecordHeader & operator=(TriggerRecordHeader const &other)
TriggerRecordHeader copy assignment operator.
double offset
uint32_t run_number_t
Type used to represent run number.
Definition Types.hpp:20
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
TriggerRecordErrorBits
This enumeration should list all defined error bits, as well as a short documentation of their meanin...
uint64_t timestamp_t
Type used to represent DUNE timing system timestamps.
Definition Types.hpp:36
uint64_t trigger_type_t
Type used to represent Trigger Decision trigger types.
Definition Types.hpp:41
FELIX Initialization std::string initerror FELIX queue timed std::string queuename Unexpected chunk size
A request sent to a Component, including the SourceID of the component and the window offset and widt...
SourceID component
The Requested Component.
SourceID is a generalized representation of the source of a piece of data in the DAQ....
Definition SourceID.hpp:32
std::string to_string() const
Definition SourceID.hpp:83
Additional data fields associated with a TriggerRecordHeader.
sequence_number_t max_sequence_number
Maximum sequence number of TriggerRecords corresponding to this trigger.
sequence_number_t sequence_number
Sequence number of this TriggerRecord within the trigger response.
timestamp_t trigger_timestamp
Timestamp of the TriggerDecision.
uint64_t num_requested_components
Number of ComponentRequest objects stored in the TriggerRecordHeader.
trigger_type_t trigger_type
Type of the TriggerDecision.
run_number_t run_number
Run Number for the TriggerRecord.
uint32_t error_bits
Error bits for the TriggerRecord.