delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
CollectionWriter.h
Go to the documentation of this file.
1// CollectionWriter.h
2//
3// Abstract base for per-domain podio writers. Owns a reference to the
4// Frame, an EventContext (shared scratch space across writers in one
5// event), and which conversion pass is running. Subclasses implement
6// `emit()`; the protected `put` / `putParameter` helpers build the
7// canonical "<prefix>_<bank>_<readable>" collection / parameter name and
8// forward to the frame. BankPrefix.h decides the prefix from the bank and
9// the pass.
10//
11// Per-event flow:
12// EventContext ctx;
13// EventWriter (frame, ctx, Pass::Sdst).emit(); // EVT_* params
14// TruthGenWriter (frame, ctx, Pass::Sdst).emit(); // sets ctx.gen_truth
15// TrackingWriter (frame, ctx, Pass::Sdst).emit(); // sets ctx.tracking
16// TruthRecoLinkWriter(frame, ctx, Pass::Sdst).emit(); // reads both
17// VertexWriter (frame, ctx, Pass::Sdst).emit(); // reads ctx.tracking
18// CalorimeterWriter (frame, ctx, Pass::Sdst).emit(); // reads ctx.tracking
19// ParticleIdWriter (frame, ctx, Pass::Sdst).emit(); // reads ctx.tracking
20//
21// All writers are stack-allocated; no heap, no vtable dispatch in the
22// usual case. `emit()` is virtual to leave the door open for runtime
23// opt-out (e.g. --skip-calorimeter) but the dispatch overhead is just
24// one indirect call per event per domain — negligible.
25
26#pragma once
27
28// Data-only headers (no writer classes) so this header doesn't pull
29// CollectionWriter back in transitively.
30#include "delphi_edm4hep/Tracking/TrackElementData.h" // track_elements::Output
31#include "delphi_edm4hep/Tracking/TraxData.h" // trax::Output
32#include "delphi_edm4hep/Tracking/TrackingData.h" // tracking::Output
33#include "delphi_edm4hep/Tracking/VdHitData.h" // vd_hits::Output
34#include "delphi_edm4hep/Calorimeter/EmcaData.h" // emca::Output
35#include "delphi_edm4hep/Truth/TruthData.h" // truth::GenParticleResult
36#include "delphi_edm4hep/BankPrefix.h" // bank::Pass, bank::make
37
38#include <edm4hep/ReconstructedParticleCollection.h>
39#include <podio/Frame.h>
40
41#include <optional>
42#include <string>
43#include <string_view>
44#include <utility>
45#include <vector>
46
47namespace delphi_edm4hep {
48
49// Per-event shared state — passed by reference into every writer's ctor.
50// Each writer reads its inputs from here (where another writer earlier
51// in the chain set them) and stores its outputs here (where a later
52// writer will read them). std::optional members signal "not yet set".
54 // Set by TruthGenWriter; consumed by TruthRecoLinkWriter.
55 std::optional<truth::GenParticleResult> gen_truth;
56
57 // Set by TrackElementsWriter; consumed by TrackingWriter, which links each
58 // mother track to the track elements from the same PA.
59 std::optional<track_elements::Output> track_elements;
60
61 // Set by TraxWriter; consumed by the track writers, which append the
62 // extrapolation states to the track built from the same PA.
63 std::optional<trax::Output> trax;
64
65 // Set by VdHitsWriter; consumed by TrackingWriter, which links each track
66 // to the Vertex-Detector hits assigned to it.
67 std::optional<vd_hits::Output> vd_hits;
68
69 // Set by EmcaWriter; consumed by CalorimeterWriter, which attaches each
70 // shower's calorimeter hits to the cluster built from the same shower.
71 std::optional<emca::Output> emca;
72
73 // Set by TrackingWriter; consumed by VertexWriter, CalorimeterWriter and
74 // ParticleIdWriter, and by particleForPa() in pass 1.
75 std::optional<tracking::Output> tracking;
76
77 // Pass-2 only: per-fDST-PA index -> sDST_TRAC_Tracks index, or -1
78 // if no perigee-match. Built by MatchProvenanceWriter from the
79 // fDST PA-chain walk; consumed by TrackHybridWriter to find the
80 // sDST Track to clone and extend.
81 std::optional<std::vector<int>> fdst_pa_to_sdst_track;
82
83 // Pass-2 only: per-fDST-PA index -> sDST_MAIN_Particles index, or
84 // -1 if no match / unmatched. Built by MatchProvenanceWriter
85 // alongside fdst_pa_to_sdst_track; consumed through particleForPa() by
86 // the writers that link a ParticleID to its particle.
87 std::optional<std::vector<int>> fdst_pa_to_sdst_particle;
88};
89
90// Where a collection's values came from.
91//
92// Transcribed the stored DST value, read from a ZEBRA bank word through
93// PHDST. Unit conversion and basis change still count as
94// transcription: the same measurement, represented differently.
95// Derived produced by SKELANA at conversion time and read from a PSC*
96// common — a refit, a re-clustering or a recomputed tag.
97// Custom computed by this converter. Not a DELPHI quantity at all.
98//
99// Every put() states one. A collection that would mix them must be split.
101
102// Record a collection's provenance. Called by put() and putParameter().
103void noteProvenance(std::string_view name, Provenance prov);
104
105// Print the provenance summary. Call once at end of job.
106void reportProvenance();
107
108// The recorded provenance, as two parallel vectors: collection names and
109// their source. Written once per job into the metadata frame.
111 std::vector<std::string> collections;
112 std::vector<std::string> sources;
113};
115
117public:
118 CollectionWriter(podio::Frame& frame,
119 EventContext& ctx,
120 bank::Pass pass)
121 : frame_(frame), ctx_(ctx), pass_(pass) {}
122
123 virtual ~CollectionWriter() = default;
124
125 // Domain-specific implementation point.
126 virtual void emit() = 0;
127
128protected:
129 podio::Frame& frame_;
132
133 // True when this writer is running over a fullDST. Writers that decode a
134 // bank differently in the two passes branch on this rather than on the
135 // collection prefix, which varies per bank.
136 bool fromFullDst() const { return pass_ == bank::Pass::Fdst; }
137
138 // The reconstructed particle this PA belongs to, or empty if it has none.
139 //
140 // `paIdx` is the PA's position in the event's PA-chain walk
141 // (pawalk::forEachPA) -- the index every writer shares.
142 //
143 // The two passes establish the mapping differently. Pass 1 knows it
144 // directly: TrackingWriter created the particles from this same walk, so the
145 // index maps straight through. Pass 2 reads a fullDST, whose PA chain is a
146 // different set of tracks, and recovers the correspondence by matching
147 // perigees onto the pass-1 tracks -- a match that can fail.
148 //
149 // A writer that runs in both passes asks here rather than choosing a map
150 // itself, so it never has to know which pass it is in.
151 std::optional<edm4hep::ReconstructedParticle> particleForPa(int paIdx) const;
152
153 // Build a canonical collection / parameter name
154 // "<prefix>_<bank>_<readable>". Also the way to name a collection written
155 // by another writer, since the prefix follows the bank.
156 std::string makeName(std::string_view bank,
157 std::string_view readable) const {
158 return bank::make(pass_, bank, readable);
159 }
160
161 // Name of a collection written by pass 1. Pass-2 writers read pass-1 output,
162 // whose prefix follows the bank rather than the pass running now.
163 std::string sdstName(std::string_view bank,
164 std::string_view readable) const {
165 return bank::make(bank::Pass::Sdst, bank, readable);
166 }
167
168 // Move a freshly-built collection into the frame under
169 // "<source>_<bank>_<readable>". `prov` states where the values came from;
170 // see Provenance. Returns the const reference podio returns, for callers
171 // that need to cross-reference the in-frame copy.
172 template <typename Coll>
173 const Coll& put(Coll&& coll,
174 std::string_view bank,
175 std::string_view readable,
176 Provenance prov) {
177 auto name = makeName(bank, readable);
178 noteProvenance(name, prov);
179 return frame_.put(std::move(coll), std::move(name));
180 }
181
182
183 // Frame-parameter equivalent. Name is "<source>_<bank>_<key>".
184 // Type T must be one of podio's supported parameter types
185 // (int, float, double, std::string, std::vector<...> of those, ...).
186 template <typename T>
187 void putParameter(std::string_view bank,
188 std::string_view key,
189 T&& value,
190 Provenance prov) {
191 auto name = makeName(bank, key);
192 noteProvenance(name, prov);
193 frame_.putParameter(std::move(name), std::forward<T>(value));
194 }
195
196
197 // A run of parameters sharing one bank tag and one provenance, so the
198 // provenance is stated once for the group rather than at every parameter:
199 //
200 // auto stored = parameters("EVT", Provenance::Transcribed);
201 // stored("runNumber", ph::IIIRUN);
202 // stored("date", ph::IIIDAT);
204 public:
206 std::string_view bank,
207 Provenance prov)
208 : writer_(writer), bank_(bank), prov_(prov) {}
209
210 template <typename T>
211 void operator()(std::string_view key, T&& value) const {
212 writer_.putParameter(bank_, key, std::forward<T>(value), prov_);
213 }
214
215 private:
216 CollectionWriter& writer_;
217 std::string_view bank_;
218 Provenance prov_;
219 };
220
221 ParameterGroup parameters(std::string_view bank, Provenance prov) {
222 return ParameterGroup(*this, bank, prov);
223 }
224};
225
226} // namespace delphi_edm4hep
ParameterGroup(CollectionWriter &writer, std::string_view bank, Provenance prov)
void operator()(std::string_view key, T &&value) const
CollectionWriter(podio::Frame &frame, EventContext &ctx, bank::Pass pass)
ParameterGroup parameters(std::string_view bank, Provenance prov)
void putParameter(std::string_view bank, std::string_view key, T &&value, Provenance prov)
std::string makeName(std::string_view bank, std::string_view readable) const
virtual ~CollectionWriter()=default
std::optional< edm4hep::ReconstructedParticle > particleForPa(int paIdx) const
std::string sdstName(std::string_view bank, std::string_view readable) const
const Coll & put(Coll &&coll, std::string_view bank, std::string_view readable, Provenance prov)
std::string make(Pass pass, std::string_view bank, std::string_view readable_name)
void noteProvenance(std::string_view name, Provenance prov)
ProvenanceRecord provenanceRecord()
std::optional< emca::Output > emca
std::optional< vd_hits::Output > vd_hits
std::optional< trax::Output > trax
std::optional< std::vector< int > > fdst_pa_to_sdst_particle
std::optional< tracking::Output > tracking
std::optional< std::vector< int > > fdst_pa_to_sdst_track
std::optional< track_elements::Output > track_elements
std::optional< truth::GenParticleResult > gen_truth
std::vector< std::string > sources
std::vector< std::string > collections