56std::map<std::pair<int, int>, std::pair<unsigned, unsigned>> g_sdst_index;
160 guardCallback(
"user00 initialization", [] {
167 if (g_cfg.output.empty()) {
168 std::cerr <<
"harness::on_user00: output path not set\n";
171 g_writer = std::make_unique<podio::ROOTWriter>(g_cfg.output.string());
172 std::cout <<
"delphi_edm4hep::harness: opened " << g_cfg.output <<
"\n";
176 std::vector<std::filesystem::path> inters;
177 if (!g_cfg.input_edm4hep.empty()) inters.push_back(g_cfg.input_edm4hep);
178 for (
const auto& p : g_cfg.input_edm4hep_extra) inters.push_back(p);
180 if (!inters.empty()) {
181 g_sdst_readers.reserve(inters.size());
182 for (
unsigned r = 0; r < inters.size(); ++r) {
183 auto reader = std::make_unique<podio::ROOTReader>();
184 reader->openFile(inters[r].
string());
191 const unsigned N = reader->getEntries(
"events");
193 for (
unsigned i = 0; i < N; ++i) {
194 auto fd = reader->readEntry(
"events", i);
196 const podio::Frame f(std::move(fd));
197 const auto run = f.getParameter<
int>(
199 const auto evt = f.getParameter<
int>(
202 if (g_sdst_index.emplace(std::make_pair(*
run, *evt),
203 std::make_pair(r, i)).second) {
208 std::cout <<
"delphi_edm4hep::harness: intermediate " << inters[r]
209 <<
" indexed (" << added <<
" new keys / " << N
211 g_sdst_readers.push_back(std::move(reader));
213 std::cout <<
"delphi_edm4hep::harness: " << g_sdst_readers.size()
214 <<
" intermediate(s), " << g_sdst_index.size()
215 <<
" unique (run, evt) keys total\n";
218 if (g_cfg.on_init) g_cfg.on_init();
237 if (g_cfg.max_events > 0 && g_n_written >= g_cfg.max_events)
return;
246 if (ph::LDTOP <= 0) { ++g_n_no_dst;
return; }
256 const std::pair<int, int> key{ph::IIIRUN, ph::IIIEVT};
257 if (!g_processed.insert(key).second) { ++g_n_redelivered;
return; }
261 if (g_n_seen == 1 && ph::LQ(ph::LDTOP - 1) == 0)
return;
283 if (!g_sdst_readers.empty()) {
284 auto it = g_sdst_index.find(key);
285 if (it == g_sdst_index.end()) {
287 std::cerr <<
"delphi_edm4hep::harness: skip — no sDST frame for"
288 <<
" (run=" << ph::IIIRUN
289 <<
", evt=" << ph::IIIEVT <<
")\n";
293 auto fd = g_sdst_readers[it->second.first]
294 ->readEntry(
"events", it->second.second);
295 if (fd) frame = podio::Frame(std::move(fd));
297 g_current_sdst_frame = &frame;
302 if (g_cfg.on_event) {
303 g_cfg.on_event(frame, ph::IIIRUN, ph::IIIEVT);
306 g_writer->writeFrame(frame,
"events");
307 g_current_sdst_frame =
nullptr;
310 if (g_n_written <= 5 || g_n_written % 500 == 0) {
311 std::cout <<
"delphi_edm4hep::harness: event " << g_n_written
312 <<
" run=" << ph::IIIRUN
313 <<
" evt=" << ph::IIIEVT <<
"\n";
323 if (g_cfg.on_finalize) {
324 guardCallback(
"user99 finalize hook", [] { g_cfg.on_finalize(); });
329 guardCallback(
"user99 metadata frame", [] {
330 if (!g_writer)
return;
332 if (record.collections.empty())
return;
334 meta.putParameter(
"provenance_collection", record.collections);
335 meta.putParameter(
"provenance_source", record.sources);
347 meta.putParameter(
"skelana_IFLCUT", sk::IFLCUT);
348 meta.putParameter(
"skelana_IFLSTR", sk::IFLSTR);
349 g_writer->writeFrame(meta,
"metadata");
352 finishWriterNoexcept(
"user99 writer finalization");
359 if (g_n_no_dst || g_n_redelivered) {
360 std::cout <<
"delphi_edm4hep::harness: skipped " << g_n_no_dst
361 <<
" records with no DST bank, " << g_n_redelivered
362 <<
" re-delivered events\n";
370 if (g_callback_failed) {
371 std::cerr <<
"delphi_edm4hep::harness: aborted after " << g_n_written
372 <<
" written events; partial output is not publishable\n";
374 std::cout <<
"delphi_edm4hep::harness: wrote " << g_n_written
375 <<
" events to " << g_cfg.output <<
"\n";
390 g_callback_failed =
false;
391 g_callback_failure.clear();
392 g_current_sdst_frame =
nullptr;
394 g_sdst_readers.clear();
395 g_sdst_index.clear();
402 std::error_code path_error;
403 const auto pdl_status = std::filesystem::symlink_status(
"PDLINPUT", path_error);
404 if (path_error == std::errc::no_such_file_or_directory) {
406 }
else if (path_error) {
407 std::cerr <<
"harness::run: cannot inspect PDLINPUT: "
408 << path_error.message() <<
"\n";
410 }
else if (pdl_status.type() != std::filesystem::file_type::not_found) {
411 std::cerr <<
"harness::run: PDLINPUT already exists in "
412 << std::filesystem::current_path() <<
"; remove it and retry\n";
416 struct InputFileCleanup {
417 std::filesystem::path link;
418 bool remove_pdl_input =
false;
419 ~InputFileCleanup() {
420 std::error_code ignored;
421 if (remove_pdl_input) std::filesystem::remove(
"PDLINPUT", ignored);
422 if (!link.empty()) std::filesystem::remove(link, ignored);
429 if (!std::filesystem::exists(cfg.
input)) {
430 std::cerr <<
"harness::run: input not found: " << cfg.
input <<
"\n";
433 const auto abs_input = std::filesystem::absolute(cfg.
input, path_error);
435 std::cerr <<
"harness::run: cannot resolve input path " << cfg.
input
436 <<
": " << path_error.message() <<
"\n";
442 const std::filesystem::path link(
443 ".phdst_input_" + std::to_string(
static_cast<long long>(::getpid())));
444 if (std::filesystem::exists(link, path_error) || path_error) {
445 std::cerr <<
"harness::run: short input link already exists or cannot "
446 "be inspected: " << link;
447 if (path_error) std::cerr <<
": " << path_error.message();
451 std::filesystem::create_symlink(abs_input, link, path_error);
453 std::cerr <<
"harness::run: cannot create short input link " << link
454 <<
" -> " << abs_input <<
": " << path_error.message()
460 pdl.open(
"PDLINPUT");
462 std::cerr <<
"harness::run: cannot create PDLINPUT in "
463 << std::filesystem::current_path() <<
"\n";
466 cleanup.remove_pdl_input =
true;
467 pdl <<
"FILE = " << link.string() <<
"\n";
471 pdl.open(
"PDLINPUT");
473 std::cerr <<
"harness::run: cannot create PDLINPUT in "
474 << std::filesystem::current_path() <<
"\n";
477 cleanup.remove_pdl_input =
true;
482 if (!std::filesystem::exists(cfg.
input)) {
483 std::cerr <<
"harness::run: pdl file not found: " << cfg.
input <<
"\n";
486 std::filesystem::copy_file(cfg.
input,
"PDLINPUT",
487 std::filesystem::copy_options::none,
490 std::cerr <<
"harness::run: cannot copy " << cfg.
input
491 <<
" to PDLINPUT: " << path_error.message() <<
"\n";
494 cleanup.remove_pdl_input =
true;
501 std::cerr <<
"harness::run: cannot write PDLINPUT\n";
510 const char opt[] =
" ";
511 ph::phdst_(
const_cast<char*
>(opt), &n, &m, std::strlen(opt));
516 finishWriterNoexcept(
"post-PHDST writer finalization");
517 if (g_callback_failed) {
518 std::cerr <<
"harness::run: event callback failed";
519 if (!g_callback_failure.empty()) {
520 std::cerr <<
": " << g_callback_failure;
525 if (g_n_written <= 0) {
526 std::cerr <<
"harness::run: no events were written; conversion failed\n";