DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
WIBEthFrameProcessor.cpp
Go to the documentation of this file.
1
8#include "fdreadoutlibs/wibeth/WIBEthFrameProcessor.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
32WIBEthFrameProcessor::WIBEthFrameProcessor(std::unique_ptr<datahandlinglibs::FrameErrorRegistry>& error_registry, bool processing_enabled)
33 : TaskRawDataProcessorModel<types::DUNEWIBEthTypeAdapter>(error_registry, processing_enabled)
34{
35}
36
37void
38WIBEthFrameProcessor::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
69WIBEthFrameProcessor::stop(const nlohmann::json& args)
70{
71 inherited::stop(args);
74 m_tp_generator->free_metric_collector();
75 }
76 // Clears the pipelines and resets with the given configs.
77 m_tp_generator->set_metric_collector_enable_state(m_tpg_metric_collect_enabled);
79 }
80}
81
82void
84{
85 size_t idx = 0;
86 for (auto output : conf->get_outputs()) {
87 try {
88 if (output->get_data_type() == "TriggerPrimitiveVector") {
90 }
91 } catch (const ers::Issue& excpt) {
92 ers::error(datahandlinglibs::ResourceQueueError(ERS_HERE, "tp", "DefaultRequestHandlerModel", excpt));
93 }
94 }
95
96 m_sourceid.id = conf->get_source_id();
98 auto geo_id = conf->get_geo_id();
99 if (geo_id != nullptr) {
100 m_det_id = geo_id->get_detector_id();
101 m_crate_id = geo_id->get_crate_id();
102 m_slot_id = geo_id->get_slot_id();
103 m_stream_id = geo_id->get_stream_id();
104 }
105 m_emulator_mode = conf->get_emulation_mode();
106
107 // Setup pre-processing pipeline
108 if (!m_emulator_mode)
109 inherited::add_preprocess_task(std::bind(&WIBEthFrameProcessor::sequence_check, this, std::placeholders::_1));
110
111 inherited::add_preprocess_task(std::bind(&WIBEthFrameProcessor::timestamp_check, this, std::placeholders::_1));
112
113 // Check it post-processing is active
114 auto dp = conf->get_module_configuration()->get_data_processor();
115 if (dp != nullptr) {
116 auto proc_conf = dp->cast<appmodel::TPCRawDataProcessor>();
117 if (proc_conf != nullptr && m_post_processing_enabled) {
118 m_tp_generator = std::make_unique<tpglibs::TPGenerator>();
119
120 // Set the minimum TP samples over threshold.
121 auto conf_sot_minima = proc_conf->get_sot_minima();
122 std::vector<uint16_t> sot_minima{conf_sot_minima->get_sot_minimum_plane0(),
123 conf_sot_minima->get_sot_minimum_plane1(),
124 conf_sot_minima->get_sot_minimum_plane2()};
125 m_tp_generator->set_sot_minima(sot_minima);
126
127 const std::vector<unsigned int> channel_mask_vec = proc_conf->get_channel_mask();
128
129 std::vector<const appmodel::ProcessingStep*> processing_steps = proc_conf->get_processing_steps();
130 for (auto step : processing_steps) {
131 m_tpg_configs.push_back(std::make_pair(step->class_name(), step->to_json(false).back()));
132 }
133
134 // Setup post-processing pipeline
135 m_channel_map = dunedaq::detchannelmaps::make_tpc_map(proc_conf->get_channel_map());
136 for (int chan = 0; chan < 64; chan++) {
137 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);
138 int16_t plane = m_channel_map->get_plane_from_offline_channel(off_channel);
139 m_channel_plane_numbers.push_back(std::make_pair(off_channel, plane));
140
141 // This processor only needs to handle some (maybe 0) of the masked channels.
142 // Only get those relevant channels for the later check.
143 if (std::find(channel_mask_vec.begin(), channel_mask_vec.end(), off_channel) != channel_mask_vec.end())
144 m_channel_mask_set.insert(off_channel);
145 }
146
147 m_metric_collect_opmon_period = proc_conf->get_metric_collect_opmon_rate();
148
149 // Let the TPG generator configure
150
152
153 // After it sees the configs, it will set the metric collector enable state
154
155 m_tpg_metric_collect_enabled = m_tp_generator->get_metric_collector_enable_state();
156
157 inherited::add_postprocess_task(std::bind(&WIBEthFrameProcessor::find_hits, this, std::placeholders::_1));
158 }
159 }
161}
162
163void
165{
167
168 info.set_num_seq_id_errors(m_seq_id_error_ctr.load());
169 info.set_min_seq_id_jump(m_seq_id_min_jump.exchange(0));
170 info.set_max_seq_id_jump(m_seq_id_max_jump.exchange(0));
171
172 info.set_num_ts_errors(m_ts_error_ctr.load());
173
174 publish(std::move(info));
175
176 m_error_registry->log_registered_errors();
177
179 auto now = std::chrono::high_resolution_clock::now();
180 int new_hits = m_tpg_hits_count.exchange(0);
181 int new_tps = m_new_tps.exchange(0);
182 int new_tps_suppressed_too_long = m_tps_suppressed_too_long.exchange(0);
183 int new_tps_send_failed = m_tps_send_failed.exchange(0);
184 double seconds = std::chrono::duration_cast<std::chrono::microseconds>(now - m_t0).count() / 1000000.;
185 TLOG_DEBUG(TLVL_BOOKKEEPING) << "Hit rate: " << std::to_string(new_hits / seconds / 1000.) << " [kHz]";
186 TLOG_DEBUG(TLVL_BOOKKEEPING) << "Total new hits: " << new_hits << " new TPs: " << new_tps;
187
189 tp_info.set_rate_tp_hits(new_hits / seconds / 1000.);
190
191 tp_info.set_num_tps_sent(new_tps);
192 tp_info.set_num_tps_suppressed_too_long(new_tps_suppressed_too_long);
193 tp_info.set_num_tps_send_failed(new_tps_send_failed);
194
195 publish(std::move(tp_info));
196 // Find the channels with the top TP rates
197 // Create a vector of pairs to store the map elements
198 std::vector<std::pair<uint, int>> channel_tp_rate_vec(m_tp_channel_rate_map.begin(), m_tp_channel_rate_map.end());
199 // Sort the vector in descending order of the value of the pairs
200 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; });
201 // Add the metrics to opmon
202 // For convenience we are selecting only the top 10 elements
203 if (channel_tp_rate_vec.size() != 0) {
204 int top_highest_values = 10;
205 if (channel_tp_rate_vec.size() < 10) {
206 top_highest_values = channel_tp_rate_vec.size();
207 }
208 //datahandlinglibs::opmon::TPChannelsInfo channels_info;
209 for (int i = 0; i < top_highest_values; i++) {
211 tpc_info.set_number_of_tps(channel_tp_rate_vec[i].second);
212 tpc_info.set_channel_id(channel_tp_rate_vec[i].first);
213 publish(std::move(tpc_info), {{"channel", std::to_string(channel_tp_rate_vec[i].first)}});
214 }
215 }
216
217 // Reset the counter in the channel rate map
218 for (auto& el : m_tp_channel_rate_map) {
219 el.second = 0;
220 }
221 m_t0 = now;
222
226 }
227 }
228
230 }
231
232 void
234 auto metrics = m_tp_generator->get_processor_metrics();
235 for (const auto& [channel, vec] : metrics) {
237 for (const auto& [name, val] : vec) {
238 if (name == "m_pedestal") {
239 tpg_proc_info.set_pedestal(val);
240 } else if (name == "m_accum") {
241 tpg_proc_info.set_accum(val);
242 }
243 }
244 publish(std::move(tpg_proc_info), {{"channel", std::to_string(channel)}});
245 }
246}
247
248std::map<int16_t, std::map<std::string, std::tuple<float, int16_t, int16_t, float, dunedaq::trgdataformats::channel_t, dunedaq::trgdataformats::channel_t>>>
249WIBEthFrameProcessor::calculate_all_metric_summaries_across_planes(const std::unordered_map<dunedaq::trgdataformats::channel_t, std::vector<std::pair<std::string, int16_t>>>& metrics) {
250 // Structure to hold all statistics: plane -> metric -> (mean, min, max, stddev, min_channel_id, max_channel_id)
251 std::map<int16_t, std::map<std::string, std::tuple<float, int16_t, int16_t, float, dunedaq::trgdataformats::channel_t, dunedaq::trgdataformats::channel_t>>> all_stats;
252
253 // Structure to accumulate statistics: plane -> metric -> (count, mean, M2, min, max, min_channel_id, max_channel_id)
254 std::map<int16_t, std::map<std::string, std::tuple<size_t, double, double, int16_t, int16_t, dunedaq::trgdataformats::channel_t, dunedaq::trgdataformats::channel_t>>> accumulators;
255
256 // Single pass through all metrics to collect data using Welford's online algorithm for variance
257 for (const auto& [channel, vec] : metrics) {
258 if (!m_channel_map) continue;
259
260 int16_t plane = m_channel_map->get_plane_from_offline_channel(channel);
261
262 for (const auto& [name, val] : vec) {
263 auto& [count, mean, M2, min, max, min_channel_id, max_channel_id] = accumulators[plane][name];
264
265 count++;
266
267 if (count == 1 || val < min) {
268 min = val;
269 min_channel_id = channel;
270 }
271 if (count == 1 || val > max) {
272 max = val;
273 max_channel_id = channel;
274 }
275
276 // Welford's online algorithm for variance calculation
277 if (count == 1) {
278 // First value: initialize mean and M2
279 mean = val;
280 M2 = 0.0;
281 } else {
282 // Update mean and M2 using Welford's algorithm
283 double delta = val - mean;
284 mean += delta / count;
285 double delta2 = val - mean;
286 M2 += delta * delta2;
287 }
288 }
289 }
290
291 // Calculate final statistics from accumulated data
292 for (const auto& [plane, metric_map] : accumulators) {
293 for (const auto& [metric_name, acc_data] : metric_map) {
294 const auto& [count, mean, M2, min, max, min_channel_id, max_channel_id] = acc_data;
295
296 if (count == 0) continue;
297
298 float stddev = 0.0f;
299
300 // Calculate standard deviation using accumulated M2
301 if (count > 1) {
302 stddev = std::sqrt(M2 / (count - 1));
303 }
304
305 all_stats[plane][metric_name] = std::make_tuple(static_cast<float>(mean), min, max, stddev, min_channel_id, max_channel_id);
306 }
307 }
308
309 return all_stats;
310}
311
312void
314 auto metrics = m_tp_generator->get_processor_metrics();
315
316 // Use optimized single-pass calculation for all metrics across all planes
317 auto all_stats = calculate_all_metric_summaries_across_planes(metrics);
318
319 // Publish all calculated statistics
320 for (const auto& [plane, metric_map] : all_stats) {
321 for (const auto& [metric_name, stats] : metric_map) {
322 const auto& [mean, min, max, stddev, min_channel_id, max_channel_id] = stats;
323
325 info.set_average(mean);
326 info.set_max(max);
327 info.set_min(min);
328 info.set_standard_dev(stddev);
329 info.set_max_channel_id(max_channel_id);
330 info.set_min_channel_id(min_channel_id);
331 publish(std::move(info), {{"plane", std::to_string(plane)}, {"metric", metric_name}});
332 }
333 }
334}
335
336
340void
342{
343 // FIXME: Make source emulator deal with this! Hard to do since source emu is templated...
344 /* If EMU data, emulate perfectly incrementing timestamp
345 if (m_emulator_mode) {
346 // uint64_t ts_next = m_previous_seq_id + 1; // NOLINT(build/unsigned)
347 auto wf = reinterpret_cast<wibframeptr>(((uint8_t*)fp)); // NOLINT
348 for (unsigned int i = 0; i < fp->get_num_frames(); ++i) { // NOLINT(build/unsigned)
349 //auto wfh = const_cast<dunedaq::fddetdataformats::WIBEthFrame*>(wf->header());
350 wf->daq_header.crate_id = m_crate_id;
351 wf->daq_header.slot_id = m_slot_id;
352 wf->daq_header.stream_id = m_stream_id;
353 wf->daq_header.seq_id = (m_previous_seq_id+i) & 0xfff;
354 wf++;
355 }
356 }
357 */
358
359 // Acquire timestamp
360 auto wfptr = reinterpret_cast<dunedaq::fddetdataformats::WIBEthFrame*>(fp); // NOLINT
362
363 // Check sequence id
364 // Calculate the next sequence id (12 bits)
365 uint16_t expected_seq_id = (m_previous_seq_id + fp->get_num_frames()) & 0xfff;
366 int16_t delta_seq_id = m_current_seq_id-expected_seq_id;
367 if ( delta_seq_id > 0x800) {
368 delta_seq_id -= 0x1000;
369 } else if ( delta_seq_id < -0x7ff) {
370 delta_seq_id += 0x1000;
371 }
372
373 if (delta_seq_id == 0) {
374 m_seq_id_error_state = false;
375 } else {
376 // uint16_t delta_seq_id = (m_current_seq_id-expected_seq_id);
378 m_seq_id_max_jump = std::max(delta_seq_id, m_seq_id_max_jump.load());
379 m_seq_id_min_jump = std::min(delta_seq_id, m_seq_id_min_jump.load());
380
381 if (m_first_seq_id_mismatch) { // log once
382 TLOG_DEBUG(TLVL_BOOKKEEPING) << "First sequence id MISMATCH! -> | previous: " << std::to_string(m_previous_seq_id) << " current: " + std::to_string(m_current_seq_id);
384 } else {
386 m_error_registry->add_error("Sequence ID jump", datahandlinglibs::FrameErrorRegistry::ErrorInterval(expected_seq_id, m_current_seq_id));
388 }
389 }
390 }
391
392 if (m_seq_id_error_ctr > 1000) {
394 TLOG() << "*** Data Integrity ERROR *** Sequence ID continuity is completely broken! "
395 << "Something is wrong with the FE source or with the configuration!";
397 }
398 }
399
401
402}
403
407void
409{
410
411 uint16_t wibeth_tick_difference = types::DUNEWIBEthTypeAdapter::expected_tick_difference;
412 uint16_t wibeth_frame_tick_difference = wibeth_tick_difference * fp->get_num_frames();
413
414 // FIXME: let source emulator deal with this!
415 /* If EMU data, emulate perfectly incrementing timestamp
416 if (m_emulator_mode) { // emulate perfectly incrementing timestamp
417 uint64_t ts_next = m_previous_ts + wibeth_frame_tick_difference; // NOLINT(build/unsigned)
418 auto wf = reinterpret_cast<wibframeptr>(((uint8_t*)fp)); // NOLINT
419 for (unsigned int i = 0; i < fp->get_num_frames(); ++i) { // NOLINT(build/unsigned)
420 //auto wfh = const_cast<dunedaq::fddetdataformats::WIBEthFrame*>(wf->header());
421 wf->daq_header.crate_id = m_crate_id;
422 wf->daq_header.slot_id = m_slot_id;
423 wf->daq_header.stream_id = m_stream_id;
424 wf->set_timestamp(ts_next);
425 ts_next += wibeth_tick_difference;
426 wf++;
427 }
428 }*/
429
430 auto wfptr = reinterpret_cast<dunedaq::fddetdataformats::WIBEthFrame*>(fp); // NOLINT
431 m_current_ts = wfptr->get_timestamp();
432
433 // Check timestamp
434 if (m_previous_ts > 0 &&
435 m_current_ts - m_previous_ts != wibeth_frame_tick_difference) [[unlikely]] {
437 if (m_first_ts_missmatch) { // log once
438 TLOG_DEBUG(TLVL_BOOKKEEPING) << "First timestamp MISMATCH! -> | previous: " << std::to_string(m_previous_ts) << " current: " + std::to_string(m_current_ts);
439 m_first_ts_missmatch = false;
440 } else {
441 if (!m_ts_error_state) {
442 m_error_registry->add_error("Timestamp jump", datahandlinglibs::FrameErrorRegistry::ErrorInterval(m_previous_ts + wibeth_frame_tick_difference, m_current_ts));
443 m_ts_error_state = true;
444 }
445 }
446 } else {
447 m_ts_error_state = false;
448 }
449
450 if (m_ts_error_ctr > 1000) {
452 TLOG() << "*** Data Integrity ERROR *** Timestamp continuity is completely broken! "
453 << "Something is wrong with the FE source or with the configuration!";
455 }
456 }
457
460}
461
465void
467{
468 size_t nhits = 0;
469 if (!fp)
470 return;
471 auto wfptr = reinterpret_cast<dunedaq::fddetdataformats::WIBEthFrame*>((uint8_t*)fp); // NOLINT
472
473 // Check that the system is properly configured from the first hit.
474 if (m_first_hit) {
475 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) {
476 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));
477 }
478
479 m_first_hit = false;
480 }
481
482 std::vector<trgdataformats::TriggerPrimitive> tps = (*m_tp_generator)(wfptr);
483 m_frame_counter.fetch_add(1, std::memory_order_relaxed);
484 if (m_tpg_metric_collect_enabled && m_frame_counter.load(std::memory_order_relaxed) % m_metric_collect_opmon_period == 0) {
485 m_tp_generator->signal_metric_collection();
486 }
487
488 for (const auto& tp : tps) {
489 // If this TP is on a masked channel, skip it.
490 if (std::binary_search(m_channel_mask_set.begin(), m_channel_mask_set.end(), tp.channel))
491 continue;
492 // Need to move into a type adapter.
494 tpa.tp = tp;
495
496 tpa.tp.detid = m_det_id; // Last missing piece.
497 m_tpa_vectors[m_channel_map->get_plane_from_offline_channel(tp.channel)].push_back(tpa);
498 m_tp_channel_rate_map[tp.channel]++;
499 }
500
501 if (m_frame_counter.load(std::memory_order_relaxed) % 100 == 0) { // FIXME: Hard-coding 100 for now. This should be defined elsewhere or configurable.
502 for (int i = 0; i < 3; i++) {
503 int new_tps = m_tpa_vectors[i].size();
504 if (new_tps == 0) {
505 continue;
506 }
507 const auto s_ts_begin = m_tpa_vectors[i].front().tp.time_start;
508 const auto channel_begin = m_tpa_vectors[i].front().tp.channel;
509 const auto s_ts_end = m_tpa_vectors[i].back().tp.time_start;
510 const auto channel_end = m_tpa_vectors[i].back().tp.channel;
511 if (!m_tp_sink[i]->try_send(std::move(m_tpa_vectors[i]), iomanager::Sender::s_no_block)) {
512 ers::warning(FailedToSendTPVector(ERS_HERE, s_ts_begin, channel_begin, s_ts_end, channel_end));
514 } else {
515 m_new_tps += new_tps;
516 nhits += new_tps;
517 }
518 }
519 }
520
521 m_tpg_hits_count += nhits;
522 return;
523}
524
525} // namespace fdreadoutlibs
526} // 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.
uint64_t get_timestamp() const
Get the starting 64-bit timestamp of the frame.
detdataformats::DAQEthHeader daq_header
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::chrono::time_point< std::chrono::high_resolution_clock > m_t0
std::shared_ptr< iomanager::SenderConcept< std::vector< trigger::TriggerPrimitiveTypeAdapter > > > m_tp_sink[3]
std::vector< std::pair< trgdataformats::channel_t, int16_t > > m_channel_plane_numbers
std::vector< trigger::TriggerPrimitiveTypeAdapter > m_tpa_vectors[3]
std::map< uint, std::atomic< int > > m_tp_channel_rate_map
std::vector< std::pair< std::string, nlohmann::json > > m_tpg_configs
dunedaq::daqdataformats::timestamp_t m_current_ts
dunedaq::daqdataformats::timestamp_t m_previous_ts
void stop(const nlohmann::json &args) override
Stop operation.
std::map< int16_t, std::map< std::string, std::tuple< float, int16_t, int16_t, float, dunedaq::trgdataformats::channel_t, dunedaq::trgdataformats::channel_t > > > calculate_all_metric_summaries_across_planes(const std::unordered_map< dunedaq::trgdataformats::channel_t, std::vector< std::pair< std::string, int16_t > > > &metrics)
std::shared_ptr< detchannelmaps::TPCChannelMap > m_channel_map
void start(const nlohmann::json &args) override
Start operation.
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 daqdataformats::SourceID::Subsystem subsystem