DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
TriggerRecordHeader.hpp
Go to the documentation of this file.
1
11
12#ifndef DAQDATAFORMATS_INCLUDE_DAQDATAFORMATS_TRIGGERRECORDHEADER_HPP_
13#define DAQDATAFORMATS_INCLUDE_DAQDATAFORMATS_TRIGGERRECORDHEADER_HPP_
14
19
20#include <bitset>
21#include <cstddef>
22#include <cstring>
23#include <format>
24#include <new>
25#include <ostream>
26#include <stdexcept>
27#include <string>
28#include <vector>
29
31
37{
38public:
43 explicit TriggerRecordHeader(const std::vector<ComponentRequest>& components);
44
51 explicit TriggerRecordHeader(void* existing_trigger_record_header_buffer, bool copy_from_buffer = false);
52
54
56 void set_trigger_number(trigger_number_t trigger_number) { header_()->trigger_number = trigger_number; }
57
59 void set_trigger_timestamp(timestamp_t trigger_timestamp) { header_()->trigger_timestamp = trigger_timestamp; }
60
61 uint64_t get_num_requested_components() const // NOLINT(build/unsigned)
62 {
64 }
65
67 void set_run_number(run_number_t run_number) { header_()->run_number = run_number; }
68
69 std::bitset<32> get_status_bits() const { return header_()->status_bits; }
70 void set_status_bits(std::bitset<32> bits) { header_()->status_bits = bits.to_ulong(); }
71
77 bool get_status_bit(TriggerRecordStatusBits bit) const { return get_status_bits()[static_cast<size_t>(bit)]; }
84 {
85 auto bits = get_status_bits();
86 bits[static_cast<size_t>(bit)] = value;
87 set_status_bits(bits);
88 }
89
91 void set_trigger_type(trigger_type_t trigger_type) { header_()->trigger_type = trigger_type; }
92
95
98
108 void set_element_id(SourceID source_id) { header_()->element_id = source_id; }
109
111 size_t get_total_size_bytes() const
112 {
114 }
115
117 const void* get_storage_location() const { return m_data_arr; }
118
125 const ComponentRequest& at(size_t idx) const;
126
133 ComponentRequest const& get_component_for_source_id(SourceID const& source_id) const;
134
137
139 {
140 m_alloc = other.m_alloc;
141 other.m_alloc = false;
142 m_data_arr = other.m_data_arr;
143 }
145 {
146 m_alloc = other.m_alloc;
147 other.m_alloc = false;
148 m_data_arr = other.m_data_arr;
149 return *this;
150 }
151
153 {
154 if (m_alloc)
155 free(m_data_arr); // NOLINT
156 }
157
158private:
164
166 nullptr
167 };
168 bool m_alloc{ false };
169};
170
171//------
172
173inline TriggerRecordHeader::TriggerRecordHeader(const std::vector<ComponentRequest>& components)
174{
175 size_t size = sizeof(TriggerRecordHeaderData) + components.size() * sizeof(ComponentRequest);
176
177 m_data_arr = malloc(size); // NOLINT
178 if (m_data_arr == nullptr) {
179 throw std::bad_alloc();
180 }
181 m_alloc = true;
182
184 header.num_requested_components = components.size();
185 std::memcpy(m_data_arr, &header, sizeof(header));
186 std::memcpy(static_cast<uint8_t*>(m_data_arr) + sizeof(header), // NOLINT
187 components.data(),
188 sizeof(ComponentRequest) * components.size());
189}
190
191inline TriggerRecordHeader::TriggerRecordHeader(void* existing_trigger_record_header_buffer, bool copy_from_buffer)
192{
193 if (!copy_from_buffer) {
194 m_data_arr = existing_trigger_record_header_buffer;
195 } else {
196 auto header = reinterpret_cast<TriggerRecordHeaderData*>(existing_trigger_record_header_buffer); // NOLINT
197 size_t size = header->num_requested_components * sizeof(ComponentRequest) + sizeof(TriggerRecordHeaderData);
198
199 m_data_arr = malloc(size); // NOLINT
200 if (m_data_arr == nullptr) {
201 throw std::bad_alloc();
202 }
203 m_alloc = true;
204 std::memcpy(m_data_arr, existing_trigger_record_header_buffer, size);
205 }
206}
207
212
215{
216 if (&other == this)
217 return *this;
218
219 if (m_alloc) {
220 free(m_data_arr); // NOLINT
221 }
222 m_data_arr = malloc(other.get_total_size_bytes()); // NOLINT
223 if (m_data_arr == nullptr) {
224 throw std::bad_alloc();
225 }
226 m_alloc = true;
227 std::memcpy(m_data_arr, other.m_data_arr, other.get_total_size_bytes());
228 return *this;
229}
230
231inline const ComponentRequest&
232TriggerRecordHeader::at(size_t idx) const
233{
234 if (idx >= header_()->num_requested_components) {
235 throw std::range_error(std::format(
236 "Supplied ComponentRequest index {} out of range (size: {})", idx, header_()->num_requested_components));
237 }
238
239 // Increment header pointer by one to skip header
240 return *(reinterpret_cast<ComponentRequest*>(header_() + 1) + idx); // NOLINT
241}
242
243inline ComponentRequest const&
245{
246 for (uint64_t idx = 0; idx < get_num_requested_components(); ++idx) { // NOLINT(build/unsigned)
247 ComponentRequest const& component_obj = *(reinterpret_cast<ComponentRequest*>(header_() + 1) + idx); // NOLINT
248 if (source_id == component_obj.component) {
249 return component_obj;
250 }
251 }
252 throw std::invalid_argument("Supplied SourceID (" + source_id.to_string() +
253 ") was not found in the ComponentRequest list.");
254}
255
256} // namespace dunedaq::daqdataformats
257
258#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_status_bit(TriggerRecordStatusBits bit, bool value)
Set the given status bit to the given value.
void set_sequence_number(sequence_number_t number)
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.
void set_element_id(SourceID source_id)
Set the SourceID for this TriggerRecordHeader.
const ComponentRequest & at(size_t idx) const
Access ComponentRequest by index.
size_t get_total_size_bytes() const
Get the total size of the TriggerRecordHeader, including header and all component requests.
const void * get_storage_location() const
Get read-only access to the underlying flat data array.
void set_max_sequence_number(sequence_number_t number)
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)
void set_trigger_timestamp(timestamp_t trigger_timestamp)
SourceID get_element_id() const
Get the SourceID for 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)
bool get_status_bit(TriggerRecordStatusBits bit) const
Get the value of the given status bit.
TriggerRecordHeader & operator=(TriggerRecordHeader const &other)
uint64_t trigger_number_t
Definition Types.hpp:18
TriggerRecordStatusBits
This enumeration lists all defined status bits, as well as a short documentation of their meaning whe...
uint16_t sequence_number_t
Type used to represent sequence within a trigger record.
Definition Types.hpp:38
uint64_t timestamp_t
Type used to represent DUNE timing system timestamps.
Definition Types.hpp:26
uint64_t trigger_type_t
Type used to represent Trigger Decision trigger types.
Definition Types.hpp:35
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 ID of 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:69
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.