LDMX Software
eventbuilder::EventBuilder Class Reference

Public Member Functions

 EventBuilder (const std::string &name, framework::Process &proc)
 
 enableLogging ("EventBuilder")
 
void configure (framework::config::Parameters &ps) override
 Callback for the EventProcessor to configure itself from the given set of parameters.
 
void produce (framework::Event &event) override
 Process the event and put new data products into it.
 
- Public Member Functions inherited from framework::Producer
 Producer (const std::string &name, Process &process)
 Class constructor.
 
virtual void process (Event &event) final
 Processing an event for a Producer is calling produce.
 
- Public Member Functions inherited from framework::EventProcessor
 DECLARE_FACTORY (EventProcessor, EventProcessor *, const std::string &, Process &)
 declare that we have a factory for this class
 
 EventProcessor (const std::string &name, Process &process)
 Class constructor.
 
virtual ~EventProcessor ()=default
 Class destructor.
 
virtual void beforeNewRun (ldmx::RunHeader &run_header)
 Callback for Producers to add parameters to the run header before conditions are initialized.
 
virtual void onNewRun (const ldmx::RunHeader &run_header)
 Callback for the EventProcessor to take any necessary action when the run being processed changes.
 
virtual void onFileOpen (EventFile &event_file)
 Callback for the EventProcessor to take any necessary action when a new event input ROOT file is opened.
 
virtual void onFileClose (EventFile &event_file)
 Callback for the EventProcessor to take any necessary action when a event input ROOT file is closed.
 
virtual void onProcessStart ()
 Callback for the EventProcessor to take any necessary action when the processing of events starts, such as creating histograms.
 
virtual void onProcessEnd ()
 Callback for the EventProcessor to take any necessary action when the processing of events finishes, such as calculating job-summary quantities.
 
template<class T >
const T & getCondition (const std::string &condition_name)
 Access a conditions object for the current event.
 
TDirectory * getHistoDirectory ()
 Access/create a directory in the histogram file for this event processor to create histograms and analysis tuples.
 
void setStorageHint (framework::StorageControl::Hint hint)
 Mark the current event as having the given storage control hint from this module_.
 
void setStorageHint (framework::StorageControl::Hint hint, const std::string &purposeString)
 Mark the current event as having the given storage control hint from this module and the given purpose string.
 
int getLogFrequency () const
 Get the current logging frequency from the process.
 
int getRunNumber () const
 Get the run number from the process.
 
std::string getName () const
 Get the processor name.
 
void createHistograms (const std::vector< framework::config::Parameters > &histos)
 Internal function which is used to create histograms passed from the python configuration @parma histos vector of Parameters that configure histograms to create.
 

Private Member Functions

PhysicsEventData assemblePayload (const std::vector< DataFragment > &fragments)
 
void writeEventBinary (const PhysicsEventData &ev, const std::string &path="events.bin")
 

Private Attributes

bool m_verbose_parse_
 
unsigned int m_event_id_
 
FragmentBuffer m_event_buffer_
 
std::string m_input_file_
 
packing::utility::Reader m_reader_
 
std::string m_output_name_ {"BuilderOutput"}
 
long long m_coherence_window_ns_
 
long long m_reorder_window_ns_ {0}
 extra wait past the window for out-of-order fragments, 0 = none
 
int m_min_subsystems_ {2}
 
std::string m_binary_file_ {"events.bin"}
 binary event output, empty to disable
 
std::string m_performance_file_ {"event_performance.csv"}
 performance metrics CSV, empty to disable
 
std::chrono::steady_clock::time_point m_start_time_
 
std::chrono::steady_clock::time_point m_event_start_time_
 
uint64_t m_total_bytes_read_ {0}
 
uint64_t m_total_events_built_ {0}
 
unsigned long m_events_since_last_report_ {0}
 
std::chrono::steady_clock::time_point m_window_start_time_
 
uint64_t m_window_events_count_ {0}
 
uint64_t m_window_bytes_read_ {0}
 

Static Private Attributes

static constexpr unsigned int WINDOW_SIZE
 

Additional Inherited Members

- Protected Member Functions inherited from framework::EventProcessor
void abortEvent ()
 Abort the event immediately.
 
- Protected Attributes inherited from framework::EventProcessor
HistogramPool histograms_
 helper object for making and filling histograms
 
NtupleManager & ntuple_ {NtupleManager::getInstance()}
 Manager for any ntuples.
 
logging::logger the_log_
 The logger for this EventProcessor.
 

Detailed Description

Definition at line 14 of file EventBuilder.h.

Constructor & Destructor Documentation

◆ EventBuilder()

eventbuilder::EventBuilder::EventBuilder ( const std::string & name,
framework::Process & proc )
inline

Definition at line 16 of file EventBuilder.h.

17 : framework::Producer(name, proc),
18 m_verbose_parse_(false),
19 m_event_id_(0) {}
Base class for a module which produces a data product.

Member Function Documentation

◆ assemblePayload()

PhysicsEventData EventBuilder::assemblePayload ( const std::vector< DataFragment > & fragments)
private

Definition at line 554 of file EventBuilder.cxx.

555 {
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}

◆ configure()

void EventBuilder::configure ( framework::config::Parameters & parameters)
overridevirtual

Callback for the EventProcessor to configure itself from the given set of parameters.

The parameters a processor has access to are the member variables of the python class in the sequence that has class_name equal to the EventProcessor class name.

For an example, look at MyProcessor.

Parameters
parametersParameters for configuration.

Reimplemented from framework::EventProcessor.

Definition at line 59 of file EventBuilder.cxx.

59 {
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}
std::string m_performance_file_
performance metrics CSV, empty to disable
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 open(const std::string &file_name)
Open a file with this reader.
Definition Reader.h:36

References framework::config::Parameters::exists(), framework::config::Parameters::get(), m_binary_file_, m_performance_file_, m_reorder_window_ns_, and packing::utility::Reader::open().

◆ produce()

void EventBuilder::produce ( framework::Event & event)
overridevirtual

Process the event and put new data products into it.

Parameters
eventThe Event to process.

Implements framework::Producer.

Definition at line 118 of file EventBuilder.cxx.

118 {
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}
void abortEvent()
Abort the event immediately.
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
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

References framework::EventProcessor::abortEvent(), packing::RogueFrameHeader::channel(), packing::LDMXRoRHeader::contributor(), packing::rawdatafile::SubsystemPacket::data(), packing::utility::Reader::eof(), ldmx::EventSummary::ERROR_DATA_DROPPED, ldmx::EventSummary::ERROR_DUPLICATE_SUBSYSTEM, ldmx::EventSummary::ERROR_PARSE_FAILURE, ldmx::EventSummary::ERROR_TRUNCATED_EVENT, packing::LDMXRoRHeader::FLAG_DATA_DROPPED, packing::LDMXRoRHeader::flags(), packing::LDMXRoRHeader::getSubsystemName(), packing::rawdatafile::SubsystemPacket::header(), m_binary_file_, m_performance_file_, m_reorder_window_ns_, packing::RogueFrameHeader::probablyYaml(), packing::LDMXRoRHeader::read(), packing::rawdatafile::SubsystemPacket::read(), packing::RogueFrameHeader::read(), packing::utility::Reader::read(), packing::utility::Reader::seek(), ldmx::EventSummary::setErrorFlags(), ldmx::EventSummary::setEventNumber(), ldmx::EventSummary::setNSystems(), ldmx::EventSummary::setPayloadSize(), ldmx::EventSummary::setSystemIds(), ldmx::EventSummary::setTimestampNs(), packing::RogueFrameHeader::size(), packing::LDMXRoRHeader::subsystem(), packing::utility::Reader::tell(), packing::LDMXRoRHeader::timestamp(), and packing::LDMXRoRHeader::valid().

◆ writeEventBinary()

void EventBuilder::writeEventBinary ( const PhysicsEventData & ev,
const std::string & path = "events.bin" )
private

Definition at line 575 of file EventBuilder.cxx.

576 {
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}

Member Data Documentation

◆ m_binary_file_

std::string eventbuilder::EventBuilder::m_binary_file_ {"events.bin"}
private

binary event output, empty to disable

Definition at line 49 of file EventBuilder.h.

49{"events.bin"};

Referenced by configure(), and produce().

◆ m_coherence_window_ns_

long long eventbuilder::EventBuilder::m_coherence_window_ns_
private
Initial value:
{
5000000}

Definition at line 41 of file EventBuilder.h.

41 {
42 5000000}; // 5 ms window for collecting fragments

◆ m_event_buffer_

FragmentBuffer eventbuilder::EventBuilder::m_event_buffer_
private

Definition at line 37 of file EventBuilder.h.

◆ m_event_id_

unsigned int eventbuilder::EventBuilder::m_event_id_
private

Definition at line 36 of file EventBuilder.h.

◆ m_event_start_time_

std::chrono::steady_clock::time_point eventbuilder::EventBuilder::m_event_start_time_
private

Definition at line 55 of file EventBuilder.h.

◆ m_events_since_last_report_

unsigned long eventbuilder::EventBuilder::m_events_since_last_report_ {0}
private

Definition at line 58 of file EventBuilder.h.

58{0};

◆ m_input_file_

std::string eventbuilder::EventBuilder::m_input_file_
private

Definition at line 38 of file EventBuilder.h.

◆ m_min_subsystems_

int eventbuilder::EventBuilder::m_min_subsystems_ {2}
private

Definition at line 47 of file EventBuilder.h.

47{2};

◆ m_output_name_

std::string eventbuilder::EventBuilder::m_output_name_ {"BuilderOutput"}
private

Definition at line 40 of file EventBuilder.h.

40{"BuilderOutput"};

◆ m_performance_file_

std::string eventbuilder::EventBuilder::m_performance_file_ {"event_performance.csv"}
private

performance metrics CSV, empty to disable

Definition at line 51 of file EventBuilder.h.

51{"event_performance.csv"};

Referenced by configure(), and produce().

◆ m_reader_

packing::utility::Reader eventbuilder::EventBuilder::m_reader_
private

Definition at line 39 of file EventBuilder.h.

◆ m_reorder_window_ns_

long long eventbuilder::EventBuilder::m_reorder_window_ns_ {0}
private

extra wait past the window for out-of-order fragments, 0 = none

Definition at line 44 of file EventBuilder.h.

44{0};

Referenced by configure(), and produce().

◆ m_start_time_

std::chrono::steady_clock::time_point eventbuilder::EventBuilder::m_start_time_
private

Definition at line 54 of file EventBuilder.h.

◆ m_total_bytes_read_

uint64_t eventbuilder::EventBuilder::m_total_bytes_read_ {0}
private

Definition at line 56 of file EventBuilder.h.

56{0};

◆ m_total_events_built_

uint64_t eventbuilder::EventBuilder::m_total_events_built_ {0}
private

Definition at line 57 of file EventBuilder.h.

57{0};

◆ m_verbose_parse_

bool eventbuilder::EventBuilder::m_verbose_parse_
private

Definition at line 35 of file EventBuilder.h.

◆ m_window_bytes_read_

uint64_t eventbuilder::EventBuilder::m_window_bytes_read_ {0}
private

Definition at line 65 of file EventBuilder.h.

65{0};

◆ m_window_events_count_

uint64_t eventbuilder::EventBuilder::m_window_events_count_ {0}
private

Definition at line 64 of file EventBuilder.h.

64{0};

◆ m_window_start_time_

std::chrono::steady_clock::time_point eventbuilder::EventBuilder::m_window_start_time_
private

Definition at line 63 of file EventBuilder.h.

◆ WINDOW_SIZE

unsigned int eventbuilder::EventBuilder::WINDOW_SIZE
staticconstexprprivate
Initial value:
=
100

Definition at line 61 of file EventBuilder.h.


The documentation for this class was generated from the following files: