LDMX Software
EventBuilder.cxx
1#include "EventBuilder/EventBuilder.h"
2
3#include <algorithm>
4#include <chrono>
5#include <cstdint>
6#include <cstring>
7#include <fstream>
8#include <iomanip>
9#include <iostream>
10#include <set>
11#include <stdexcept>
12#include <vector>
13
14#include "EventBuilder/Event/GenericDataBlock.h"
15#include "EventBuilder/Event/PhysicsEventData.h"
16#include "EventBuilder/Fragment.h"
18#include "Framework/Logger.h"
19#include "Packing/LDMXRoRHeader.h"
20#include "Packing/RawDataFile/SubsystemPacket.h"
21#include "Packing/RogueFrameHeader.h"
22
23using namespace eventbuilder;
24
25// Global flag to track if performance CSV header has been written
26static bool g_perf_csv_header_written = false;
27
28// Helper function to write performance metrics to CSV
29void writePerformanceMetric(const std::string& path, unsigned int event_id,
30 double build_time_ms, double cum_events_per_sec,
31 double cum_mb_per_sec, double window_events_per_sec,
32 double window_mb_per_sec, uint64_t total_events,
33 uint64_t total_bytes) {
34 if (path.empty()) return;
35 std::ofstream perf_csv(path, std::ios::app);
36 if (!perf_csv) return;
37
38 // Write header on first call
39 if (!g_perf_csv_header_written) {
40 perf_csv
41 << "event_id,event_build_time_ms,cum_events_per_sec,cum_mb_per_sec,"
42 << "window_events_per_sec,window_mb_per_sec,total_events,total_bytes_"
43 "mb\n";
44 g_perf_csv_header_written = true;
45 }
46
47 // Write data with consistent formatting
48 double total_mb = total_bytes / (1024.0 * 1024.0);
49 perf_csv << event_id << "," << std::fixed << std::setprecision(3)
50 << build_time_ms << "," << std::fixed << std::setprecision(2)
51 << cum_events_per_sec << "," << std::fixed << std::setprecision(3)
52 << cum_mb_per_sec << "," << std::fixed << std::setprecision(2)
53 << window_events_per_sec << "," << std::fixed << std::setprecision(3)
54 << window_mb_per_sec << "," << total_events << "," << std::fixed
55 << std::setprecision(2) << total_mb << "\n";
56 perf_csv.flush();
57}
58
60 if (ps.exists("verbose_parse")) {
61 m_verbose_parse_ = ps.get<bool>("verbose_parse");
62 }
63 if (ps.exists("dat_file")) {
64 m_input_file_ = ps.get<std::string>("dat_file");
65 } else {
66 const char* env_input = std::getenv("EVENTBUILDER_INPUT");
67 if (env_input) m_input_file_ = env_input;
68 }
69 if (ps.exists("output_name"))
70 m_output_name_ = ps.get<std::string>("output_name");
71 if (ps.exists("coherence_window_ns")) {
72 m_coherence_window_ns_ = ps.get<double>("coherence_window_ns");
73 }
74 if (ps.exists("reorder_window_ns")) {
75 m_reorder_window_ns_ = ps.get<double>("reorder_window_ns");
76 }
77 if (ps.exists("min_subsystems")) {
78 m_min_subsystems_ = ps.get<int>("min_subsystems");
79 }
80 if (ps.exists("binary_file")) {
81 m_binary_file_ = ps.get<std::string>("binary_file");
82 }
83 if (ps.exists("performance_file")) {
84 m_performance_file_ = ps.get<std::string>("performance_file");
85 }
86
87 ldmx_log(info) << "configure(): dat_file='" << m_input_file_
88 << "' output_name='" << m_output_name_
89 << "' verbose_parse=" << (m_verbose_parse_ ? "true" : "false")
90 << " coherence_window_ns=" << m_coherence_window_ns_
91 << " reorder_window_ns=" << m_reorder_window_ns_
92 << " min_subsystems=" << m_min_subsystems_;
93
94 if (!m_input_file_.empty()) {
95 ldmx_log(info) << "configure(): opening file '" << m_input_file_ << "'";
96 m_reader_.open(m_input_file_);
97 if (!m_reader_) {
98 ldmx_log(error) << "failed to open input file: '" << m_input_file_ << "'";
99 } else {
100 ldmx_log(info) << "configure(): file opened successfully";
101 }
102 } else {
103 ldmx_log(error) << "no input file specified";
104 }
105
106 // Initialize performance tracking
107 m_start_time_ = std::chrono::steady_clock::now();
108 m_total_bytes_read_ = 0;
109 m_total_events_built_ = 0;
110 m_events_since_last_report_ = 0;
111
112 // Initialize windowed metrics for real-time DAQ monitoring
113 m_window_start_time_ = m_start_time_;
114 m_window_events_count_ = 0;
115 m_window_bytes_read_ = 0;
116}
117
119 static int produce_call_count = 0;
120 produce_call_count++;
121 ldmx_log(debug) << "produce() called, count=" << produce_call_count;
122 ldmx_log(debug) << "verbose_parse=" << (m_verbose_parse_ ? "true" : "false");
123 if (m_verbose_parse_ || produce_call_count <= 3) {
124 ldmx_log(info) << "produce() call #" << produce_call_count;
125 }
126
127 // Start timing for this event
128 m_event_start_time_ = std::chrono::steady_clock::now();
129
130 // Track errors encountered during event assembly
131 uint32_t current_event_errors = 0;
132
133 while (m_reader_ && !m_reader_.eof()) {
134 // Try to read a RogueFrameHeader
135 packing::RogueFrameHeader frame_header;
136 frame_header.read(m_reader_);
137
138 // Store location of end-of-frame for potential recovery
139 const long long frame_end =
140 static_cast<long long>(m_reader_.tell()) + frame_header.size();
141
142 // Check if this is a data frame (channel 0) and not YAML
143 if (frame_header.channel() != 0 || frame_header.probablyYaml()) {
144 // Skip this frame
145 if (m_verbose_parse_)
146 ldmx_log(debug) << "skipping non-data frame (channel="
147 << frame_header.channel() << ")";
148 m_reader_.seek(frame_end);
149 continue;
150 }
151
152 // We have a valid data frame - try to parse it
153 DataFragment fragment{{}, {}, {.checksum_ = 0}};
154 bool parsed_ok = false;
155
156 // Try RoR format first
157 packing::LDMXRoRHeader ror_header;
158 int pos_before_ror = m_reader_.tell();
159
160 // Attempt to read as RoR header
161 try {
162 ror_header.read(m_reader_);
163 if (!ror_header.valid()) throw std::runtime_error("invalid RoR header");
164 // Successfully parsed RoR header
165 fragment.header_.subsystem_id_ =
166 static_cast<uint64_t>(ror_header.subsystem());
167 fragment.header_.contributor_id_ =
168 static_cast<uint64_t>(ror_header.contributor());
169 fragment.header_.timestamp_ = ror_header.timestamp();
170 fragment.header_.flags_ = ror_header.flags();
171
172 if (m_verbose_parse_) {
173 ldmx_log(debug) << "parsed RoR header subsys="
174 << (int)ror_header.subsystem()
175 << " contrib=" << (int)ror_header.contributor()
176 << " ts=" << ror_header.timestamp();
177 }
178
179 // Read remaining frame payload after RoR header
180 std::vector<uint8_t> payload_data;
181 long long payload_size = frame_end - m_reader_.tell();
182 if (payload_size > 0) {
183 payload_data.resize(payload_size);
184 m_reader_.read(reinterpret_cast<char*>(payload_data.data()),
185 payload_size);
186 }
187 fragment.payload_ = std::move(payload_data);
188 parsed_ok = true;
189 } catch (...) {
190 // RoR parse failed, try packing subsystem format
191 if (m_verbose_parse_)
192 ldmx_log(debug)
193 << "RoR header parse failed, trying packing subsystem format";
194 current_event_errors |= ldmx::EventSummary::ERROR_PARSE_FAILURE;
195 m_reader_.seek(pos_before_ror);
196 }
197
198 if (!parsed_ok) {
199 // Try packing subsystem format
201 try {
202 pkt.read(m_reader_);
203 fragment.header_.subsystem_id_ = static_cast<uint64_t>(pkt.id());
204 fragment.header_.contributor_id_ =
205 0; // Not available in packing subsystem format
206 fragment.header_.timestamp_ = static_cast<uint64_t>(pkt.header()[1]) *
207 1000000000ULL; // event number to ns
208
209 // Convert packet data to payload bytes
210 const auto& data = pkt.data();
211 fragment.payload_.resize(data.size() * 4);
212 std::memcpy(fragment.payload_.data(),
213 reinterpret_cast<const char*>(data.data()),
214 data.size() * 4);
215
216 if (m_verbose_parse_) {
217 ldmx_log(debug) << "parsed packing subsystem pkt subsys=" << pkt.id()
218 << " data_size=" << data.size();
219 }
220 parsed_ok = true;
221 } catch (...) {
222 // Both parse attempts failed, skip this frame
223 if (m_verbose_parse_)
224 ldmx_log(debug) << "failed to parse as either format, skipping frame";
225 current_event_errors |= ldmx::EventSummary::ERROR_PARSE_FAILURE;
226 m_reader_.seek(frame_end);
227 continue;
228 }
229 }
230
231 if (!parsed_ok) {
232 m_reader_.seek(frame_end);
233 continue;
234 }
235
236 if (m_verbose_parse_)
237 ldmx_log(debug) << "adding fragment subsys="
238 << fragment.header_.subsystem_id_
239 << " ts=" << fragment.header_.timestamp_
240 << " bytes=" << fragment.payload_.size();
241
242 // Check if this fragment is outside the coherence window of the current
243 // event batch If so, it means we should finalize the previous event before
244 // adding this one
245 long long fragment_ts = fragment.header_.timestamp_;
246 long long buffer_ref_time = m_event_buffer_.getReferenceTime();
247
248 if (buffer_ref_time > 0 &&
249 (fragment_ts < buffer_ref_time - m_coherence_window_ns_ ||
250 fragment_ts >
251 buffer_ref_time + m_coherence_window_ns_ + m_reorder_window_ns_)) {
252 // This fragment is outside the current window - try to build the previous
253 // event
254 std::vector<DataFragment> assembled_event_fragments;
255 if (m_event_buffer_.tryBuildEvent(m_coherence_window_ns_,
256 m_min_subsystems_,
257 assembled_event_fragments) &&
258 !assembled_event_fragments.empty()) {
259 // Output the complete event and return
260 ++m_event_id_;
261 // Add each subsystem's raw data as vector<uint8_t> directly
262 uint64_t event_timestamp = 0;
263 for (const auto& frag : assembled_event_fragments) {
264 std::string subsys_name = packing::LDMXRoRHeader::getSubsystemName(
265 static_cast<uint8_t>(frag.header_.subsystem_id_),
266 static_cast<uint8_t>(frag.header_.contributor_id_));
267 std::vector<uint8_t> payload_bytes(frag.payload_.begin(),
268 frag.payload_.end());
269 event.add(subsys_name, payload_bytes);
270 if (event_timestamp == 0) {
271 event_timestamp = frag.header_.timestamp_;
272 }
273 }
274 event.getEventHeader().setIntParameter(
275 "RoR Timestamp", static_cast<long long>(event_timestamp));
276 event.getEventHeader().setRealData();
277 // Still create PhysicsEventData for binary output file
278 PhysicsEventData final_event =
279 assemblePayload(assembled_event_fragments);
280 writeEventBinary(final_event, m_binary_file_);
281 ldmx_log(info) << "assembled event id=" << m_event_id_
282 << " timestamp=" << final_event.timestamp_
283 << " fragments=" << assembled_event_fragments.size()
284 << " systems=" << final_event.systems_readout_.size();
285
286 ldmx::EventSummary summary;
287 summary.setEventNumber(m_event_id_);
288 summary.setTimestampNs(static_cast<uint64_t>(final_event.timestamp_));
289 std::set<uint64_t> unique_sys, unique_sources;
290 uint64_t total_payload = 0;
291 for (const auto& f : assembled_event_fragments) {
292 unique_sys.insert(f.header_.subsystem_id_);
293 // a subsystem may have several contributors, a duplicate is the same
294 // pair twice
295 unique_sources.insert(f.header_.subsystem_id_ << 8 |
296 f.header_.contributor_id_);
297 total_payload += f.payload_.size();
298 if (f.header_.flags_ & packing::LDMXRoRHeader::FLAG_DATA_DROPPED) {
299 current_event_errors |= ldmx::EventSummary::ERROR_DATA_DROPPED;
300 }
301 }
302 // Check for duplicate subsystems
303 if (unique_sources.size() != assembled_event_fragments.size()) {
304 current_event_errors |= ldmx::EventSummary::ERROR_DUPLICATE_SUBSYSTEM;
305 }
306 summary.setNSystems(static_cast<uint32_t>(unique_sys.size()));
307 summary.setSystemIds(
308 std::vector<uint64_t>(unique_sys.begin(), unique_sys.end()));
309 summary.setPayloadSize(total_payload);
310 summary.setErrorFlags(current_event_errors);
311 event.add("EventSummary", summary);
312
313 // Update performance metrics
314 m_total_bytes_read_ += total_payload;
315 m_total_events_built_++;
316 m_events_since_last_report_++;
317 std::chrono::steady_clock::time_point now =
318 std::chrono::steady_clock::now();
319 std::chrono::milliseconds event_build_time_ms =
320 std::chrono::duration_cast<std::chrono::milliseconds>(
321 now - m_event_start_time_);
322 std::chrono::seconds total_elapsed_s =
323 std::chrono::duration_cast<std::chrono::seconds>(now -
324 m_start_time_);
325
326 double events_per_sec =
327 (total_elapsed_s.count() > 0)
328 ? static_cast<double>(m_total_events_built_) /
329 total_elapsed_s.count()
330 : 0.0;
331 double mb_per_sec = (total_elapsed_s.count() > 0)
332 ? static_cast<double>(m_total_bytes_read_) /
333 (1024.0 * 1024.0) /
334 total_elapsed_s.count()
335 : 0.0;
336
337 // Update windowed metrics
338 m_window_events_count_++;
339 m_window_bytes_read_ += total_payload;
340 std::chrono::seconds window_elapsed_s =
341 std::chrono::duration_cast<std::chrono::seconds>(
342 now - m_window_start_time_);
343
344 // Reset window if we've processed WINDOW_SIZE events
345 double window_events_per_sec = 0.0;
346 double window_mb_per_sec = 0.0;
347 if (m_window_events_count_ >= WINDOW_SIZE) {
348 double window_time_sec =
349 std::max(1.0, static_cast<double>(window_elapsed_s.count()));
350 window_events_per_sec =
351 static_cast<double>(m_window_events_count_) / window_time_sec;
352 window_mb_per_sec = static_cast<double>(m_window_bytes_read_) /
353 (1024.0 * 1024.0) / window_time_sec;
354
355 // Reset window
356 m_window_start_time_ = now;
357 m_window_events_count_ = 0;
358 m_window_bytes_read_ = 0;
359 }
360
361 // Write performance metrics to CSV
362 long long event_build_time_ms_val = event_build_time_ms.count();
363 writePerformanceMetric(m_performance_file_, m_event_id_,
364 static_cast<double>(event_build_time_ms_val),
365 events_per_sec, mb_per_sec,
366 window_events_per_sec, window_mb_per_sec,
367 m_total_events_built_, m_total_bytes_read_);
368
369 if (m_verbose_parse_ || m_events_since_last_report_ % 100 == 0) {
370 ldmx_log(info) << "Performance: "
371 << "total_events=" << m_total_events_built_ << ", "
372 << "events_per_sec=" << std::fixed
373 << std::setprecision(2) << events_per_sec << ", "
374 << "mb_per_sec=" << std::fixed << std::setprecision(3)
375 << mb_per_sec;
376 }
377
378 current_event_errors = 0; // Reset for next event
379 // The fragment that tripped the window-close belongs to the NEXT
380 // event, not the one we just emitted. Buffer it here (the reference
381 // time moves to the oldest buffered fragment) instead of dropping
382 // it -- otherwise every event after the first loses its triggering
383 // subsystem.
384 m_event_buffer_.addFragment(std::move(fragment));
385 return; // Return the event to framework
386 }
387 }
388
389 // Add fragment to buffer (may start a new event batch if buffer was empty)
390 m_event_buffer_.addFragment(std::move(fragment));
391
392 if (m_verbose_parse_)
393 ldmx_log(debug) << "frame added to buffer, searching for more frames...";
394 }
395
396 // Reached EOF - flush any remaining events in the buffer
397 if (m_verbose_parse_)
398 ldmx_log(debug) << "reached EOF, flushing remaining events";
399
400 std::vector<DataFragment> assembled_event_fragments;
401 while (m_event_buffer_.tryBuildEvent(
402 m_coherence_window_ns_, m_min_subsystems_, assembled_event_fragments)) {
403 if (assembled_event_fragments.empty()) break;
404
405 // Mark truncated events (those flushed at EOF)
406 current_event_errors |= ldmx::EventSummary::ERROR_TRUNCATED_EVENT;
407
408 ++m_event_id_;
409 // Add each subsystem's raw data as vector<uint8_t> directly
410 uint64_t event_timestamp = 0;
411 for (const auto& frag : assembled_event_fragments) {
412 std::string subsys_name = packing::LDMXRoRHeader::getSubsystemName(
413 static_cast<uint8_t>(frag.header_.subsystem_id_),
414 static_cast<uint8_t>(frag.header_.contributor_id_));
415 std::vector<uint8_t> payload_bytes(frag.payload_.begin(),
416 frag.payload_.end());
417 event.add(subsys_name, payload_bytes);
418 if (event_timestamp == 0) {
419 event_timestamp = frag.header_.timestamp_;
420 }
421 }
422 event.getEventHeader().setIntParameter(
423 "RoR Timestamp", static_cast<long long>(event_timestamp));
424 event.getEventHeader().setRealData();
425 // Still create PhysicsEventData for binary output file
426 PhysicsEventData final_event = assemblePayload(assembled_event_fragments);
427 writeEventBinary(final_event, m_binary_file_);
428 ldmx_log(info) << "assembled event id=" << m_event_id_
429 << " timestamp=" << final_event.timestamp_
430 << " fragments=" << assembled_event_fragments.size()
431 << " systems=" << final_event.systems_readout_.size();
432
433 ldmx::EventSummary summary;
434 summary.setEventNumber(m_event_id_);
435 summary.setTimestampNs(static_cast<uint64_t>(final_event.timestamp_));
436 std::set<uint64_t> unique_sys, unique_sources;
437 uint64_t total_payload = 0;
438 for (const auto& f : assembled_event_fragments) {
439 unique_sys.insert(f.header_.subsystem_id_);
440 // a subsystem may have several contributors, a duplicate is the same pair
441 // twice
442 unique_sources.insert(f.header_.subsystem_id_ << 8 |
443 f.header_.contributor_id_);
444 total_payload += f.payload_.size();
445 if (f.header_.flags_ & packing::LDMXRoRHeader::FLAG_DATA_DROPPED) {
446 current_event_errors |= ldmx::EventSummary::ERROR_DATA_DROPPED;
447 }
448 }
449 // Check for duplicate subsystems
450 if (unique_sources.size() != assembled_event_fragments.size()) {
451 current_event_errors |= ldmx::EventSummary::ERROR_DUPLICATE_SUBSYSTEM;
452 }
453 summary.setNSystems(static_cast<uint32_t>(unique_sys.size()));
454 summary.setSystemIds(
455 std::vector<uint64_t>(unique_sys.begin(), unique_sys.end()));
456 summary.setPayloadSize(total_payload);
457 summary.setErrorFlags(current_event_errors);
458 event.add("EventSummary", summary);
459
460 // Update performance metrics
461 m_total_bytes_read_ += total_payload;
462 m_total_events_built_++;
463 m_events_since_last_report_++;
464 std::chrono::steady_clock::time_point now =
465 std::chrono::steady_clock::now();
466 std::chrono::milliseconds event_build_time_ms =
467 std::chrono::duration_cast<std::chrono::milliseconds>(
468 now - m_event_start_time_);
469 std::chrono::seconds total_elapsed_s =
470 std::chrono::duration_cast<std::chrono::seconds>(now - m_start_time_);
471
472 double events_per_sec = (total_elapsed_s.count() > 0)
473 ? static_cast<double>(m_total_events_built_) /
474 total_elapsed_s.count()
475 : 0.0;
476 double mb_per_sec = (total_elapsed_s.count() > 0)
477 ? static_cast<double>(m_total_bytes_read_) /
478 (1024.0 * 1024.0) / total_elapsed_s.count()
479 : 0.0;
480
481 // Update windowed metrics
482 m_window_events_count_++;
483 m_window_bytes_read_ += total_payload;
484 std::chrono::seconds window_elapsed_s =
485 std::chrono::duration_cast<std::chrono::seconds>(now -
486 m_window_start_time_);
487
488 // Calculate window metrics (for final event, may not have full window)
489 double window_events_per_sec = 0.0;
490 double window_mb_per_sec = 0.0;
491 if (m_window_events_count_ >= WINDOW_SIZE || window_elapsed_s.count() > 0) {
492 double window_time_sec =
493 std::max(1.0, static_cast<double>(window_elapsed_s.count()));
494 window_events_per_sec =
495 static_cast<double>(m_window_events_count_) / window_time_sec;
496 window_mb_per_sec = static_cast<double>(m_window_bytes_read_) /
497 (1024.0 * 1024.0) / window_time_sec;
498 }
499
500 // Write performance metrics to CSV
501 long long event_build_time_ms_val = event_build_time_ms.count();
502 writePerformanceMetric(m_performance_file_, m_event_id_,
503 static_cast<double>(event_build_time_ms_val),
504 events_per_sec, mb_per_sec, window_events_per_sec,
505 window_mb_per_sec, m_total_events_built_,
506 m_total_bytes_read_);
507
508 if (m_verbose_parse_ || m_events_since_last_report_ % 100 == 0) {
509 ldmx_log(info) << "Performance: "
510 << "total_events=" << m_total_events_built_ << ", "
511 << "events_per_sec=" << std::fixed << std::setprecision(2)
512 << events_per_sec << ", "
513 << "mb_per_sec=" << std::fixed << std::setprecision(3)
514 << mb_per_sec;
515 }
516
517 current_event_errors = 0; // Reset for next event
518
519 assembled_event_fragments.clear();
520 return;
521 }
522
523 // No more events - print final summary statistics
524 std::chrono::steady_clock::time_point final_time =
525 std::chrono::steady_clock::now();
526 std::chrono::seconds total_time_s =
527 std::chrono::duration_cast<std::chrono::seconds>(final_time -
528 m_start_time_);
529 double final_events_per_sec =
530 (total_time_s.count() > 0)
531 ? static_cast<double>(m_total_events_built_) / total_time_s.count()
532 : 0.0;
533 double final_mb_per_sec = (total_time_s.count() > 0)
534 ? static_cast<double>(m_total_bytes_read_) /
535 (1024.0 * 1024.0) / total_time_s.count()
536 : 0.0;
537
538 ldmx_log(info) << "\n===== FINAL STATISTICS =====";
539 ldmx_log(info) << "Total events built: " << m_total_events_built_;
540 ldmx_log(info) << "Total bytes read: "
541 << m_total_bytes_read_ / (1024.0 * 1024.0) << " MB";
542 ldmx_log(info) << "Total time: " << total_time_s.count() << " seconds";
543 ldmx_log(info) << "Average throughput: "
544 << "events_per_sec=" << std::fixed << std::setprecision(2)
545 << final_events_per_sec << ", "
546 << "mb_per_sec=" << std::fixed << std::setprecision(3)
547 << final_mb_per_sec;
548 ldmx_log(info) << "=============================\n";
549 ldmx_log(info) << "Event building complete";
550
551 abortEvent();
552}
553
554PhysicsEventData EventBuilder::assemblePayload(
555 const std::vector<DataFragment>& fragments) {
556 PhysicsEventData event_data;
557 if (fragments.empty()) return event_data;
558 event_data.event_id_ = m_event_id_;
559 event_data.timestamp_ = fragments.front().header_.timestamp_;
560 for (const auto& fragment : fragments) {
562 g.subsystem_id_ = fragment.header_.subsystem_id_;
563 g.timestamp_ns_ = fragment.header_.timestamp_;
564 g.data_ = fragment.payload_;
565 if (fragment.trailer_.checksum_ != 0)
566 g.checksum_ = fragment.trailer_.checksum_;
567 else
568 g.checksum_ = crc32(fragment.payload_);
569 event_data.blocks_.push_back(std::move(g));
570 event_data.systems_readout_.push_back(fragment.header_.subsystem_id_);
571 }
572 return event_data;
573}
574
575void EventBuilder::writeEventBinary(const PhysicsEventData& ev,
576 const std::string& path) {
577 static std::mutex g_out_mutex;
578 std::lock_guard<std::mutex> lg(g_out_mutex);
579 if (path.empty()) return;
580 std::ofstream ofs(path, std::ios::binary | std::ios::app);
581 if (!ofs) return;
582 uint64_t event_id_u = static_cast<uint64_t>(ev.event_id_);
583 uint64_t ts = static_cast<uint64_t>(ev.timestamp_);
584 uint32_t nblocks = static_cast<uint32_t>(ev.blocks_.size());
585 ofs.write(reinterpret_cast<const char*>(&event_id_u), sizeof(event_id_u));
586 ofs.write(reinterpret_cast<const char*>(&ts), sizeof(ts));
587 ofs.write(reinterpret_cast<const char*>(&nblocks), sizeof(nblocks));
588 for (const auto& b : ev.blocks_) {
589 uint64_t sid = b.subsystem_id_;
590 uint64_t bts = b.timestamp_ns_;
591 uint32_t psz = static_cast<uint32_t>(b.data_.size());
592 uint32_t csum = b.checksum_;
593 ofs.write(reinterpret_cast<const char*>(&sid), sizeof(sid));
594 ofs.write(reinterpret_cast<const char*>(&bts), sizeof(bts));
595 ofs.write(reinterpret_cast<const char*>(&psz), sizeof(psz));
596 ofs.write(reinterpret_cast<const char*>(&csum), sizeof(csum));
597 if (psz)
598 ofs.write(reinterpret_cast<const char*>(b.data_.data()),
599 static_cast<std::streamsize>(psz));
600 }
601 ofs.flush();
602}
603
604// Register producer with the framework factory
#define DECLARE_PRODUCER(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Class that provides a summary of event assembly with metadata and error flags.
std::string m_performance_file_
performance metrics CSV, empty to disable
void produce(framework::Event &event) override
Process the event and put new data products into it.
void configure(framework::config::Parameters &ps) override
Callback for the EventProcessor to configure itself from the given set of parameters.
std::string m_binary_file_
binary event output, empty to disable
long long m_reorder_window_ns_
extra wait past the window for out-of-order fragments, 0 = none
void abortEvent()
Abort the event immediately.
Implements an event buffer system for storing event data.
Definition Event.h:40
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:75
bool exists(const std::string &name) const
Check to see if a parameter exists.
Definition Parameters.h:60
Provides summary information about an assembled event, including error flags for data quality assessm...
void setSystemIds(const std::vector< uint64_t > &ids)
Set the system IDs.
void setNSystems(uint32_t ns)
Set the number of systems in this event.
void setPayloadSize(uint64_t size)
Set the total payload size.
void setTimestampNs(uint64_t ts)
Set the timestamp in nanoseconds.
void setEventNumber(uint64_t num)
Set the event number.
void setErrorFlags(uint32_t flags)
Set the error flags.
@ ERROR_PARSE_FAILURE
Frame parsing error.
@ ERROR_DATA_DROPPED
A fragment is header only, its data dropped (DAQ busy)
@ ERROR_DUPLICATE_SUBSYSTEM
Duplicate subsystem in single event.
@ ERROR_TRUNCATED_EVENT
Event was truncated (EOF reached)
the header that the LDMX DAQ Firmware block includes in the output data stream at the beginning of ea...
static std::tuple< int, int > subsystem(const std::string &name)
Get the (subsystem, contributor) pair for the input subsystem name.
static constexpr uint32_t FLAG_DATA_DROPPED
header-only fragment, see flags()
uint8_t contributor() const
ID number for contributor within subsystem (configured into firmware)
static std::string getSubsystemName(uint8_t subsystem_id, uint8_t contributor_id)
Get the subsystem name from subsystem and contributor IDs.
bool valid() const
true if the header was parsed successfully (sentinel matched and the unused flag bits are zero)
uint32_t flags() const
flags set by the DAQ event formatter
uint64_t timestamp() const
get timestamp of this RoR
utility::Reader & read(utility::Reader &r)
read the next LDMX RoR header into memory
the header that the Rogue StreamWriter puts includes at the beginning of each frame.
unsigned int size() const
get the size of the frame not including this header
bool probablyYaml() const
check if this frame is probably a yaml dump
int channel() const
get the channel this data was written to
utility::Reader & read(utility::Reader &r)
read the next rogue frame header into memory
std::vector< uint32_t > & data()
Get data.
utility::Reader & read(utility::Reader &r)
read the subsystem packet from the input reader
std::vector< uint32_t > header() const
get the header words
std::streampos tell()
Tell us where the reader is.
Definition Reader.h:88
void open(const std::string &file_name)
Open a file with this reader.
Definition Reader.h:36
bool eof()
check if file is done
Definition Reader.h:217
void seek(std::streampos off, std::ios_base::seekdir dir=std::ios::beg)
Go ("seek") a specific position in the file.
Definition Reader.h:62
Reader & read(WordType *w, std::size_t count)
Read the next 'count' words into the input handle.
Definition Reader.h:114