delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
delphi_fdst_pass.cpp
Go to the documentation of this file.
1// delphi_fdst_pass — Pass-2 binary.
2//
3// Reads (a) the pass-1 intermediate edm4hep file and (b) a Delphi fullDST
4// (.fadana) via PHDST. Drives the event loop on the fadana side. Per
5// event, the harness looks up the matching intermediate frame by
6// (run, evt) and uses it as the output frame so sDST_* collections are
7// copied through automatically; pass-2 writers ADD fDST_* on top.
8//
9// Usage: delphi_fdst_pass <intermediate.edm4hep.root> <input.fadana>
10// <output.edm4hep.root> [-n MAX]
11
13#include "delphi_edm4hep/CollectionWriter.h" // EventContext
32
33#include <charconv>
34#include <cstdlib>
35#include <cstring>
36#include <filesystem>
37#include <iostream>
38#include <string>
39
41
42// PHDST user-hook overrides — must live in the binary TU. Forward
43// straight to the harness (same pattern as delphi_sdst_pass).
44extern "C" {
45 void user00_() noexcept { harness::on_user00(); }
46 void user01_(int* need) noexcept { harness::on_user01(need); }
47 void user02_() noexcept { harness::on_user02(); }
48 void user99_() noexcept { harness::on_user99(); }
49}
50
51static void usage(const char* argv0) {
52 std::cerr
53 << "usage: " << argv0
54 << " <intermediate.edm4hep.root[,more.root...]> <input.fadana>"
55 << " <output.edm4hep.root> [-n MAX_EVENTS]\n";
56}
57
58// Parse a strictly-positive integer for -n; error + usage + exit(1) on
59// non-numeric input, <= 0, or overflow. (std::atoi silently returned 0 on
60// "-n abc"/"-n 0", which max_events<=0 then treated as "unlimited".)
61static int parseMaxEvents(const char* s, const char* argv0) {
62 int v = 0;
63 const char* end = s + std::strlen(s);
64 const auto res = std::from_chars(s, end, v);
65 if (res.ec != std::errc{} || res.ptr != end || v <= 0) {
66 std::cerr << "error: -n expects a positive integer, got '" << s << "'\n";
67 usage(argv0);
68 std::exit(1);
69 }
70 return v;
71}
72
73int main(int argc, char** argv) {
74 if (argc < 4) { usage(argv[0]); return 1; }
75
77 // argv[1] may be a comma-separated list of intermediates. A long run's
78 // official short-DST events span several .al tape files; pass all the
79 // tapes that contain this run so every reconstructed event finds its
80 // sDST match (otherwise segments whose events live in a non-listed
81 // tape produce a 0-event file). First path is primary, rest are extra.
82 {
83 const std::string list = argv[1];
84 size_t start = 0;
85 while (start <= list.size()) {
86 const size_t comma = list.find(',', start);
87 const std::string one =
88 list.substr(start, comma == std::string::npos ? std::string::npos
89 : comma - start);
90 if (!one.empty()) {
91 if (cfg.input_edm4hep.empty()) cfg.input_edm4hep = one;
92 else cfg.input_edm4hep_extra.emplace_back(one);
93 }
94 if (comma == std::string::npos) break;
95 start = comma + 1;
96 }
97 }
98 cfg.input = argv[2];
99 cfg.output = argv[3];
100
101 for (int i = 4; i < argc; ++i) {
102 if (std::strcmp(argv[i], "-n") == 0 && i + 1 < argc) {
103 cfg.max_events = parseMaxEvents(argv[++i], argv[0]);
104 } else {
105 std::cerr << "unknown option: " << argv[i] << "\n";
106 usage(argv[0]);
107 return 1;
108 }
109 }
110
111 if (!std::filesystem::exists(cfg.input_edm4hep)) {
112 std::cerr << "intermediate not found: " << cfg.input_edm4hep << "\n";
113 return 1;
114 }
115 if (!std::filesystem::exists(cfg.input)) {
116 std::cerr << "fdst input not found: " << cfg.input << "\n";
117 return 1;
118 }
119
120 // Pass-2 writers. The harness already populated frame with the
121 // matching intermediate's sDST_* collections; these writers ADD
122 // fDST_* collections on top. EventContext threads cross-writer
123 // state (e.g. fdst_pa_to_sdst_track from MatchProvenanceWriter
124 // is consumed by later writers).
125 cfg.on_event = [](podio::Frame& frame, int /*run*/, int /*evt*/) {
127 using namespace delphi_edm4hep;
128 EventContext ctx;
129 // MatchProvenanceWriter must run FIRST: it populates
130 // ctx.fdst_pa_to_sdst_particle (and _track) which the other
131 // pass-2 writers consume for their setParticle linkage.
133 // TrackElements decodes the PA.TE* modules and must run before
134 // TrackHybrid, which links each cloned track to them.
136 trax::TraxWriter (frame, ctx, bank::Pass::Fdst).emit();
138 emca::EmcaWriter (frame, ctx, bank::Pass::Fdst).emit();
141 tdha::TdhaWriter (frame, ctx, bank::Pass::Fdst).emit();
143 // ShowerHybrid clones sDST_EMNC/HCNC_Showers into fDST_* and must run
144 // BEFORE MainHybrid: the Particle→Cluster relation lives on the
145 // (mutable) particle, so MainHybrid re-points it onto these clones
146 // while fDST_MAIN_Particles is still being built.
148 // MainHybrid must run AFTER TrackHybrid (consumes fDST_TRAC_Tracks)
149 // and ShowerHybrid (consumes fDST_EMNC/HCNC_Showers), and BEFORE the
150 // hybrid writers that consume fDST_MAIN_Particles.
152 // These PA ParticleID writers link setParticle() to fDST_MAIN_Particles
153 // (1:1 with sDST_MAIN_Particles by clone index), so they MUST run AFTER
154 // MainHybrid creates it. They previously ran earlier and linked to the
155 // pass-1 sDST_MAIN_Particles, leaving final-file PID->particle relations
156 // inconsistent with the PidHybrid-repointed clones. (MU/EL/TDID + TOF + MTPC + TRAX.)
157 tof::TofWriter (frame, ctx, bank::Pass::Fdst).emit();
158 mtpc::MtpcWriter (frame, ctx, bank::Pass::Fdst).emit();
162 // B-tagging. fulldst=true: SKELANA recalculates for ANY IFLBTG > 0 on
163 btag::BtagWriter (frame, ctx, bank::Pass::Fdst).emit();
164 };
165
166 return harness::run(cfg);
167}
void emit() override
Definition Tof.cpp:32
static int parseMaxEvents(const char *s, const char *argv0)
int main(int argc, char **argv)
void user02_() noexcept
void user99_() noexcept
void user01_(int *need) noexcept
void user00_() noexcept
static void usage(const char *argv0)
void on_user99() noexcept
void on_user02() noexcept
int run(const Config &cfg)
void on_user01(int *need) noexcept
void on_user00() noexcept
std::vector< std::filesystem::path > input_edm4hep_extra
std::filesystem::path input_edm4hep
std::filesystem::path output
std::filesystem::path input