delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
MatchProvenance.cpp
Go to the documentation of this file.
1// MatchProvenanceWriter — pass-2 implementation.
2//
3// Reads sDST_TRAC_Tracks + sDST_MAIN_Particles from frame_ (loaded
4// from the intermediate by the harness). Walks the fDST PA chain,
5// computes a perigee match for each charged PA, builds:
6// - ctx_.fdst_pa_to_sdst_track (paIdx -> sDST Track idx; -1 = no match)
7// - sdst_track_matched (sDST Track idx -> bool)
8// then emits the per-Particle provenance flag.
9
11
15
16#include <edm4hep/ReconstructedParticleCollection.h>
17#include <edm4hep/Track.h>
18#include <edm4hep/TrackCollection.h>
19#include <podio/UserDataCollection.h>
20
21#include <cmath>
22#include <cstdint>
23#include <vector>
24
25namespace ph = phdst;
26
28
29namespace {
30
31// Build sDST Track-index -> sDST Particle-index lookup by walking the
32// Particles collection's `tracks` relation once. O(N²) but N ~ 30.
33// Returns a vector of size sdst_tracks.size(); -1 = no owning Particle
34// (shouldn't happen in practice — every Track has one Particle).
35std::vector<int> buildTrackToParticleMap(
36 const edm4hep::TrackCollection& sdst_tracks,
37 const edm4hep::ReconstructedParticleCollection& sdst_particles)
38{
39 std::vector<int> map(sdst_tracks.size(), -1);
40 for (std::size_t i = 0; i < sdst_particles.size(); ++i) {
41 const auto p = sdst_particles[i];
42 if (p.tracks_size() == 0) continue;
43 const auto p_trk = p.getTracks(0);
44 for (std::size_t j = 0; j < sdst_tracks.size(); ++j) {
45 if (sdst_tracks[j] == p_trk) {
46 map[j] = static_cast<int>(i);
47 break;
48 }
49 }
50 }
51 return map;
52}
53
54} // namespace
55
57{
58 // The sDST_TRAC_Tracks and sDST_MAIN_Particles collections come
59 // from the intermediate frame the harness loaded. If they're
60 // missing (e.g. pass-1 produced no charged tracks), emit empty
61 // provenance and bail.
62 const auto& sdst_tracks =
63 frame_.get<edm4hep::TrackCollection>(sdstName("TRAC", "Tracks"));
64 const auto& sdst_particles =
65 frame_.get<edm4hep::ReconstructedParticleCollection>(sdstName("MAIN", "Particles"));
66
67 podio::UserDataCollection<std::int32_t> prov;
68 prov.resize(sdst_particles.size());
69 for (std::size_t i = 0; i < sdst_particles.size(); ++i) {
70 // -1 for neutral, 0 for charged-not-yet-matched. Charged that
71 // get matched below are flipped to +1.
72 prov[i] = (sdst_particles[i].getCharge() == 0.f) ? -1 : 0;
73 }
74
75 // Pre-compute the Track -> Particle index map so the fDST loop can
76 // mark provenance in O(1) per match.
77 const auto track_to_particle =
78 buildTrackToParticleMap(sdst_tracks, sdst_particles);
79
80 // Walk fDST PAs, compute perigee match for each charged PA. Build
81 // both PA -> Track and PA -> Particle maps in one pass.
82 std::vector<int> fdst_pa_to_track;
83 std::vector<int> fdst_pa_to_particle;
84 auto extend_maps_to = [&](int paIdx) {
85 if (paIdx >= static_cast<int>(fdst_pa_to_track.size())) {
86 fdst_pa_to_track.resize (static_cast<std::size_t>(paIdx + 1), -1);
87 fdst_pa_to_particle.resize(static_cast<std::size_t>(paIdx + 1), -1);
88 }
89 };
90 pawalk::forEachPA([&](int lpa, int paIdx) {
91 extend_maps_to(paIdx);
92
93 const int lmain = pawalk::lphpa("MAIN", lpa);
94 if (lmain <= 0) return;
95 const int charge_code =
96 static_cast<int>(std::lround(ph::Q(lmain + 8)));
97 if (charge_code == 0) return; // neutral — no perigee match
98
99 const int ltrac = pawalk::lphpa("TRAC", lpa);
100 if (ltrac <= 0) return;
101
102 // DELPHI perigee (cm, rad, 1/cm), converted exactly as the sDST tracks
103 // it is matched against.
104 const auto h = Helix::fromPerigee(ph::Q(ltrac + 2), ph::Q(ltrac + 3),
105 ph::Q(ltrac + 4), ph::Q(ltrac + 5),
106 ph::Q(ltrac + 6));
107 const double D0_mm = h.params().D0;
108 const double Z0_mm = h.params().Z0;
109 const double omega_pm = h.params().omega;
110
111 // Charge sign — same encoding as Tracking.cpp (code 3 = undefined
112 // -> 0, no perigee match for ambiguous charge).
113 int charge_signed = 0;
114 if (charge_code == 1) charge_signed = +1;
115 else if (charge_code == 2) charge_signed = -1;
116 if (charge_signed == 0) return;
117
118 const int matched = perigee_match::findMatch(
119 sdst_tracks, D0_mm, Z0_mm, omega_pm, charge_signed);
120 fdst_pa_to_track[paIdx] = matched;
121
122 if (matched >= 0 && matched < static_cast<int>(track_to_particle.size())) {
123 const int particle_idx = track_to_particle[matched];
124 if (particle_idx >= 0 && particle_idx < static_cast<int>(prov.size())) {
125 prov[particle_idx] = +1;
126 fdst_pa_to_particle[paIdx] = particle_idx;
127 }
128 }
129 });
130
131 // Expose the per-PA -> Track / -> Particle maps for downstream
132 // pass-2 writers (TOF / MTPC / TE-merge).
133 ctx_.fdst_pa_to_sdst_track = std::move(fdst_pa_to_track);
134 ctx_.fdst_pa_to_sdst_particle = std::move(fdst_pa_to_particle);
135
136 // Bank prefix is MAIN, not TRAC: this collection is parallel to
137 // fDST_MAIN_Particles (charged + neutral), NOT to fDST_TRAC_Tracks
138 // (charged only). The -1 = neutral value is a per-particle concept.
139 put(std::move(prov), "MAIN", "Particles_MatchProvenance", Provenance::Custom);
140}
141
142} // namespace delphi_edm4hep::matchprov
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)
static Helix fromPerigee(float d0, float z0, float theta, float phi, float invR, const std::array< float, 15 > *W=nullptr)
Definition Helix.cpp:109
void forEachPA(F &&fn)
Definition PaWalk.h:67
int lphpa(const char *iddp, int lpa, int nump=0)
Definition PaWalk.h:34
int findMatch(const edm4hep::TrackCollection &sdst_tracks, double fdst_D0_mm, double fdst_Z0_mm, double fdst_omega_per_mm, int fdst_charge_signed, double tolerance_cm2=1.0e-4)
Definition PaWalk.h:21