DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
TDEEthFrameProcessor.cpp
Go to the documentation of this file.
1
8#include "fdreadoutlibs/tde/TDEEthFrameProcessor.hpp" // NOLINT(build/include)
9#include "confmodel/GeoId.hpp"
14
19
21
24
25// THIS SHOULDN'T BE HERE!!!!! But it is necessary.....
27DUNE_DAQ_TYPESTRING(std::vector<dunedaq::trigger::TriggerPrimitiveTypeAdapter>, "TriggerPrimitiveVector")
28
29namespace dunedaq {
30namespace fdreadoutlibs {
31
32TDEEthFrameProcessor::TDEEthFrameProcessor(std::unique_ptr<datahandlinglibs::FrameErrorRegistry>& error_registry, bool processing_enabled)
33 : TaskRawDataProcessorModel<types::TDEEthTypeAdapter>(error_registry, processing_enabled)
34{
35}
36
37void
38TDEEthFrameProcessor::start(const nlohmann::json& args)
39{
40 // Reset software TPG resources
44 }
45
46 // Reset timestamp check
47 m_previous_ts = 0;
48 m_current_ts = 0;
51 m_ts_error_state = false;
53
58
59
60 // Reset stats
61 m_t0 = std::chrono::high_resolution_clock::now();
62 m_new_hits = 0;
63 m_new_tps = 0;
64 m_tpg_hits_count.exchange(0);
65 inherited::start(args);
66}
67
68void
69TDEEthFrameProcessor::stop(const nlohmann::json& args)
70{
71 inherited::stop(args);
73 // Clears the pipelines and resets with the given configs.
75 }
76}
77
78void
80{
81 size_t idx = 0;
82 for (auto output : conf->get_outputs()) {
83 try {
84 if (output->get_data_type() == "TriggerPrimitiveVector") {
86 }
87 } catch (const ers::Issue& excpt) {
88 ers::error(datahandlinglibs::ResourceQueueError(ERS_HERE, "tp", "DefaultRequestHandlerModel", excpt));
89 }
90 }
91
92 m_sourceid.id = conf->get_source_id();
94 auto geo_id = conf->get_geo_id();
95 if (geo_id != nullptr) {
96 m_det_id = geo_id->get_detector_id();
97 m_crate_id = geo_id->get_crate_id();
98 m_slot_id = geo_id->get_slot_id();
99 m_stream_id = geo_id->get_stream_id();
100 }
101 m_emulator_mode = conf->get_emulation_mode();
102
103 // Setup pre-processing pipeline
104 if (!m_emulator_mode)
105 inherited::add_preprocess_task(std::bind(&TDEEthFrameProcessor::sequence_check, this, std::placeholders::_1));
106
107 inherited::add_preprocess_task(std::bind(&TDEEthFrameProcessor::timestamp_check, this, std::placeholders::_1));
108
109 // Check it post-processing is active
110 auto dp = conf->get_module_configuration()->get_data_processor();
111 if (dp != nullptr) {
112 auto proc_conf = dp->cast<appmodel::TPCRawDataProcessor>();
113 if (proc_conf != nullptr && m_post_processing_enabled) {
114 m_tp_generator = std::make_unique<tpglibs::TPGenerator>();
115
116 // Set the minimum TP samples over threshold.
117 auto conf_sot_minima = proc_conf->get_sot_minima();
118 std::vector<uint16_t> sot_minima{conf_sot_minima->get_sot_minimum_plane0(),
119 conf_sot_minima->get_sot_minimum_plane1(),
120 conf_sot_minima->get_sot_minimum_plane2()};
121 m_tp_generator->set_sot_minima(sot_minima);
122
123 const std::vector<unsigned int> channel_mask_vec = proc_conf->get_channel_mask();
124
125 std::vector<const appmodel::ProcessingStep*> processing_steps = proc_conf->get_processing_steps();
126 for (auto step : processing_steps) {
127 m_tpg_configs.push_back(std::make_pair(step->class_name(), step->to_json(false).back()));
128 }
129
130 // Setup post-processing pipeline
131 m_channel_map = dunedaq::detchannelmaps::make_tpc_map(proc_conf->get_channel_map());
132 for (int chan = 0; chan < 64; chan++) {
133 trgdataformats::channel_t off_channel = m_channel_map->get_offline_channel_from_det_crate_slot_stream_chan(m_det_id, m_crate_id, m_slot_id, m_stream_id, chan);
134 int16_t plane = m_channel_map->get_plane_from_offline_channel(off_channel);
135 m_channel_plane_numbers.push_back(std::make_pair(off_channel, plane));
136
137 // This processor only needs to handle some (maybe 0) of the masked channels.
138 // Only get those relevant channels for the later check.
139 if (std::find(channel_mask_vec.begin(), channel_mask_vec.end(), off_channel) != channel_mask_vec.end())
140 m_channel_mask_set.insert(off_channel);
141 }
142
144
145 inherited::add_postprocess_task(std::bind(&TDEEthFrameProcessor::find_hits, this, std::placeholders::_1));
146 }
147 }
149}
150
151void
153{
155
156 info.set_num_seq_id_errors(m_seq_id_error_ctr.load());
157 info.set_min_seq_id_jump(m_seq_id_min_jump.exchange(0));
158 info.set_max_seq_id_jump(m_seq_id_max_jump.exchange(0));
159
160 info.set_num_ts_errors(m_ts_error_ctr.load());
161
162 publish(std::move(info));
163
164 m_error_registry->log_registered_errors();
165
167 auto now = std::chrono::high_resolution_clock::now();
168 int new_hits = m_tpg_hits_count.exchange(0);
169 int new_tps = m_new_tps.exchange(0);
170 int new_tps_suppressed_too_long = m_tps_suppressed_too_long.exchange(0);
171 int new_tps_send_failed = m_tps_send_failed.exchange(0);
172 double seconds = std::chrono::duration_cast<std::chrono::microseconds>(now - m_t0).count() / 1000000.;
173 TLOG_DEBUG(TLVL_BOOKKEEPING) << "Hit rate: " << std::to_string(new_hits / seconds / 1000.) << " [kHz]";
174 TLOG_DEBUG(TLVL_BOOKKEEPING) << "Total new hits: " << new_hits << " new TPs: " << new_tps;
175
177 tp_info.set_rate_tp_hits(new_hits / seconds / 1000.);
178
179 tp_info.set_num_tps_sent(new_tps);
180 tp_info.set_num_tps_suppressed_too_long(new_tps_suppressed_too_long);
181 tp_info.set_num_tps_send_failed(new_tps_send_failed);
182
183 publish(std::move(tp_info));
184 // Find the channels with the top TP rates
185 // Create a vector of pairs to store the map elements
186 std::vector<std::pair<uint, int>> channel_tp_rate_vec(m_tp_channel_rate_map.begin(), m_tp_channel_rate_map.end());
187 // Sort the vector in descending order of the value of the pairs
188 sort(channel_tp_rate_vec.begin(), channel_tp_rate_vec.end(), [](std::pair<uint, int>& a, std::pair<uint, int>& b) { return a.second > b.second; });
189 // Add the metrics to opmon
190 // For convenience we are selecting only the top 10 elements
191 if (channel_tp_rate_vec.size() != 0) {
192 int top_highest_values = 10;
193 if (channel_tp_rate_vec.size() < 10) {
194 top_highest_values = channel_tp_rate_vec.size();
195 }
196 //datahandlinglibs::opmon::TPChannelsInfo channels_info;
197 for (int i = 0; i < top_highest_values; i++) {
199 tpc_info.set_number_of_tps(channel_tp_rate_vec[i].second);
200 tpc_info.set_channel_id(channel_tp_rate_vec[i].first);
201 publish(std::move(tpc_info), {{"channel", std::to_string(channel_tp_rate_vec[i].first)}});
202 }
203 }
204
205 // Reset the counter in the channel rate map
206 for (auto& el : m_tp_channel_rate_map) {
207 el.second = 0;
208 }
209 m_t0 = now;
210 }
212 }
213
214
218void
220{
221 // FIXME: Make source emulator deal with this! Hard to do since source emu is templated...
222 /* If EMU data, emulate perfectly incrementing timestamp
223 if (m_emulator_mode) {
224 // uint64_t ts_next = m_previous_seq_id + 1; // NOLINT(build/unsigned)
225 auto wf = reinterpret_cast<tdeframeptr>(((uint8_t*)fp)); // NOLINT
226 for (unsigned int i = 0; i < fp->get_num_frames(); ++i) { // NOLINT(build/unsigned)
227 //auto wfh = const_cast<dunedaq::fddetdataformats::TDEEthFrame*>(wf->header());
228 wf->daq_header.crate_id = m_crate_id;
229 wf->daq_header.slot_id = m_slot_id;
230 wf->daq_header.stream_id = m_stream_id;
231 wf->daq_header.seq_id = (m_previous_seq_id+i) & 0xfff;
232 wf++;
233 }
234 }
235 */
236
237 // Acquire timestamp
238 auto wfptr = reinterpret_cast<dunedaq::fddetdataformats::TDEEthFrame*>(fp); // NOLINT
240
241 // Check sequence id
242 // Calculate the next sequence id (12 bits)
243 uint16_t expected_seq_id = (m_previous_seq_id + fp->get_num_frames()) & 0xfff;
244 int16_t delta_seq_id = m_current_seq_id-expected_seq_id;
245 if ( delta_seq_id > 0x800) {
246 delta_seq_id -= 0x1000;
247 } else if ( delta_seq_id < -0x7ff) {
248 delta_seq_id += 0x1000;
249 }
250
251 if (delta_seq_id == 0) {
252 m_seq_id_error_state = false;
253 } else {
254 // uint16_t delta_seq_id = (m_current_seq_id-expected_seq_id);
256 m_seq_id_max_jump = std::max(delta_seq_id, m_seq_id_max_jump.load());
257 m_seq_id_min_jump = std::min(delta_seq_id, m_seq_id_min_jump.load());
258
259 if (m_first_seq_id_mismatch) { // log once
260 TLOG_DEBUG(TLVL_BOOKKEEPING) << "First sequence id MISMATCH! -> | previous: " << std::to_string(m_previous_seq_id) << " current: " + std::to_string(m_current_seq_id);
262 } else {
264 m_error_registry->add_error("Sequence ID jump", datahandlinglibs::FrameErrorRegistry::ErrorInterval(expected_seq_id, m_current_seq_id));
266 }
267 }
268 }
269
270 if (m_seq_id_error_ctr > 1000) {
272 TLOG() << "*** Data Integrity ERROR *** Sequence ID continuity is completely broken! "
273 << "Something is wrong with the FE source or with the configuration!";
275 }
276 }
277
279
280}
281
285void
287{
288
289 uint16_t tdeeth_tick_difference = types::TDEEthTypeAdapter::expected_tick_difference;
290 uint16_t tdeeth_frame_tick_difference = tdeeth_tick_difference * fp->get_num_frames();
291
292 // FIXME: let source emulator deal with this!
293 /* If EMU data, emulate perfectly incrementing timestamp
294 if (inherited::m_emulator_mode) { // emulate perfectly incrementing timestamp
295 uint64_t ts_next = m_previous_ts + tdeeth_frame_tick_difference; // NOLINT(build/unsigned)
296 auto tf = reinterpret_cast<tdeframeptr>(((uint8_t*)fp)); // NOLINT
297 for (unsigned int i = 0; i < fp->get_num_frames(); ++i) { // NOLINT(build/unsigned)
298 //auto wfh = const_cast<dunedaq::fddetdataformats::TDEEthFrame*>(tf->header());
299 tf->daq_header.crate_id = m_crate_id;
300 tf->daq_header.slot_id = m_slot_id;
301 tf->daq_header.stream_id = m_stream_id;
302 tf->set_timestamp(ts_next);
303 ts_next += tdeeth_tick_difference;
304 tf++;
305 }
306 }*/
307
308 auto wfptr = reinterpret_cast<dunedaq::fddetdataformats::TDEEthFrame*>(fp); // NOLINT
309 m_current_ts = wfptr->get_timestamp();
310
311 // Check timestamp
312 if (m_previous_ts > 0 &&
313 m_current_ts - m_previous_ts != tdeeth_frame_tick_difference) [[unlikely]] {
315 if (m_first_ts_missmatch) { // log once
316 TLOG_DEBUG(TLVL_BOOKKEEPING) << "First timestamp MISMATCH! -> | previous: " << std::to_string(m_previous_ts) << " current: " + std::to_string(m_current_ts);
317 m_first_ts_missmatch = false;
318 } else {
319 if (!m_ts_error_state) {
320 m_error_registry->add_error("Timestamp jump", datahandlinglibs::FrameErrorRegistry::ErrorInterval(m_previous_ts + tdeeth_frame_tick_difference, m_current_ts));
321 m_ts_error_state = true;
322 }
323 }
324 } else {
325 m_ts_error_state = false;
326 }
327
328 if (m_ts_error_ctr > 1000) {
330 TLOG() << "*** Data Integrity ERROR *** Timestamp continuity is completely broken! "
331 << "Something is wrong with the FE source or with the configuration!";
333 }
334 }
335
338}
339
343void
345{
346 size_t nhits = 0;
347 if (!fp)
348 return;
349 auto wfptr = reinterpret_cast<dunedaq::fddetdataformats::TDEEthFrame*>((uint8_t*)fp); // NOLINT
350
351 // Check that the system is properly configured from the first hit.
352 if (m_first_hit) {
353 if (wfptr->daq_header.crate_id != m_crate_id || wfptr->daq_header.slot_id != m_slot_id || wfptr->daq_header.stream_id != m_stream_id) {
354 ers::error(LinkMisconfiguration(ERS_HERE, wfptr->daq_header.crate_id, wfptr->daq_header.slot_id, wfptr->daq_header.stream_id, m_crate_id, m_slot_id, m_stream_id));
355 }
356
357 m_first_hit = false;
358 }
359
360 std::vector<trgdataformats::TriggerPrimitive> tps = (*m_tp_generator)(wfptr);
362
363 for (const auto& tp : tps) {
364 // If this TP is on a masked channel, skip it.
365 if (std::binary_search(m_channel_mask_set.begin(), m_channel_mask_set.end(), tp.channel))
366 continue;
367 // Need to move into a type adapter.
369 tpa.tp = tp;
370
371 tpa.tp.detid = m_det_id; // Last missing piece.
372 m_tpa_vectors[m_channel_map->get_plane_from_offline_channel(tp.channel)].push_back(tpa);
373 m_tp_channel_rate_map[tp.channel]++;
374 }
375
376 if (m_frame_counter >= 100) { // FIXME: Hard-coding 100 for now. This should be defined elsewhere or configurable.
377 for (int i = 0; i < 3; i++) {
378 int new_tps = m_tpa_vectors[i].size();
379 if (new_tps == 0) {
380 continue;
381 }
382 const auto s_ts_begin = m_tpa_vectors[i].front().tp.time_start;
383 const auto channel_begin = m_tpa_vectors[i].front().tp.channel;
384 const auto s_ts_end = m_tpa_vectors[i].back().tp.time_start;
385 const auto channel_end = m_tpa_vectors[i].back().tp.channel;
386 if (!m_tp_sink[i]->try_send(std::move(m_tpa_vectors[i]), iomanager::Sender::s_no_block)) {
387 ers::warning(FailedToSendTPVector(ERS_HERE, s_ts_begin, channel_begin, s_ts_end, channel_end));
389 } else {
390 m_new_tps += new_tps;
391 nhits += new_tps;
392 }
393 }
394 m_frame_counter = 0;
395 }
396
397 m_tpg_hits_count += nhits;
398 return;
399}
400
401} // namespace fdreadoutlibs
402} // namespace dunedaq
#define ERS_HERE
#define DUNE_DAQ_TYPESTRING(Type, typestring)
Declare the datatype_to_string method for the given type.
Class for accessing raw WIB eth frames, as used in ProtoDUNE-II.
detdataformats::DAQEthHeader daq_header
uint64_t get_timestamp() const
Get the starting 64-bit timestamp of the frame.
std::vector< std::pair< std::string, nlohmann::json > > m_tpg_configs
void conf(const appmodel::DataHandlerModule *conf) override
Set the emulator mode, if active, timestamps of processed packets are overwritten with new ones.
std::unique_ptr< tpglibs::TPGenerator > m_tp_generator
std::vector< std::pair< trgdataformats::channel_t, int16_t > > m_channel_plane_numbers
std::shared_ptr< detchannelmaps::TPCChannelMap > m_channel_map
dunedaq::daqdataformats::timestamp_t m_current_ts
void stop(const nlohmann::json &args) override
Stop operation.
std::chrono::time_point< std::chrono::high_resolution_clock > m_t0
dunedaq::daqdataformats::timestamp_t m_previous_ts
std::map< uint, std::atomic< int > > m_tp_channel_rate_map
void start(const nlohmann::json &args) override
Start operation.
std::vector< trigger::TriggerPrimitiveTypeAdapter > m_tpa_vectors[3]
std::shared_ptr< iomanager::SenderConcept< std::vector< trigger::TriggerPrimitiveTypeAdapter > > > m_tp_sink[3]
static constexpr timeout_t s_no_block
Definition Sender.hpp:26
void publish(google::protobuf::Message &&, CustomOrigin &&co={}, OpMonLevel l=to_level(EntryOpMonLevel::kDefault)) const noexcept
Base class for any user define issue.
Definition Issue.hpp:69
static int64_t now()
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
#define TLOG(...)
Definition macro.hpp:22
The DUNE-DAQ namespace.
Definition DataStore.hpp:57
static std::shared_ptr< iomanager::SenderConcept< Datatype > > get_iom_sender(iomanager::ConnectionId const &id)
void warning(const Issue &issue)
Definition ers.hpp:115
void error(const Issue &issue)
Definition ers.hpp:81
Subsystem subsystem
The general subsystem of the source of the data.
Definition SourceID.hpp:69
ID_t id
Unique identifier of the source of the data.
Definition SourceID.hpp:74
static const constexpr uint64_t samples_tick_difference
static const constexpr daqdataformats::SourceID::Subsystem subsystem
static const constexpr uint64_t expected_tick_difference