delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
TrackHybrid.cpp
Go to the documentation of this file.
1// TrackHybridWriter — pass-2 implementation.
2
4
7
8#include <edm4hep/MutableTrack.h>
9#include <edm4hep/TrackCollection.h>
10#include <edm4hep/TrackState.h>
11
12#include <cstddef>
13#include <unordered_map>
14#include <vector>
15
17
18namespace {
19
20// Clone the scalar / vector fields of a Track. Only the AtIP state is copied:
21// the extrapolation states belong to the pass that decoded them, and pass 2
22// appends its own from the fullDST, which carries far more of them.
23// Cross-collection relations are not copied either: the fDST track gets its
24// own, pointing at the fDST track elements.
25void cloneTrackShallow(edm4hep::MutableTrack dst, const edm4hep::Track& src) {
26 dst.setType (src.getType());
27 dst.setChi2 (src.getChi2());
28 dst.setNdf (src.getNdf());
29 dst.setNholes (src.getNholes());
30 for (auto n : src.getSubdetectorHitNumbers()) dst.addToSubdetectorHitNumbers(n);
31 for (auto n : src.getSubdetectorHoleNumbers()) dst.addToSubdetectorHoleNumbers(n);
32 for (auto ts : src.getTrackStates()) {
33 if (ts.location == edm4hep::TrackState::AtIP) dst.addToTrackStates(ts);
34 }
35}
36
37} // namespace
38
40{
41 edm4hep::TrackCollection trk_out;
42
43 if (!ctx_.fdst_pa_to_sdst_track) {
44 put(std::move(trk_out), "TRAC", "Tracks", Provenance::Derived);
45 return;
46 }
47 const auto& pa_to_track = *ctx_.fdst_pa_to_sdst_track;
48 const auto& sdst_tracks =
49 frame_.get<edm4hep::TrackCollection>(sdstName("TRAC", "Tracks"));
50
51 // AABTAG's impact parameters ride on the track, and these tracks are clones
52 // that do not carry the pass-1 state, so they are attached again here from
53 // the fullDST commons. Keyed by PA address, which the walk gives per index.
54 const auto lpa_to_btag = aabtag::lpaToTrack();
55 std::unordered_map<int, int> pa_to_lpa;
56 pawalk::forEachPA([&](int lpa, int paIdx) { pa_to_lpa.emplace(paIdx, lpa); });
57
58 // sDST Track index -> the fDST PAs the perigee match resolved to it.
59 // Usually 1:1, but many-to-one is tolerated.
60 std::vector<std::vector<int>> track_to_pas(sdst_tracks.size());
61 for (int paIdx = 0; paIdx < static_cast<int>(pa_to_track.size()); ++paIdx) {
62 const int t = pa_to_track[paIdx];
63 if (t >= 0 && t < static_cast<int>(sdst_tracks.size())) {
64 track_to_pas[t].push_back(paIdx);
65 }
66 }
67
68 for (std::size_t i = 0; i < sdst_tracks.size(); ++i) {
69 auto out = trk_out.create();
70 cloneTrackShallow(out, sdst_tracks[i]);
71
72 if (!ctx_.track_elements) continue;
73 const auto& te = *ctx_.track_elements;
74 for (int paIdx : track_to_pas[i]) {
75 if (paIdx < static_cast<int>(te.pa_to_segments.size())) {
76 for (const auto& seg : te.pa_to_segments[paIdx]) out.addToTracks(seg);
77 }
78 if (paIdx < static_cast<int>(te.pa_to_plane_hits.size())) {
79 for (const auto& hit : te.pa_to_plane_hits[paIdx]) {
80 out.addToTrackerHits(hit);
81 }
82 }
83 if (ctx_.trax &&
84 paIdx < static_cast<int>(ctx_.trax->pa_to_states.size())) {
85 for (const auto& st : ctx_.trax->pa_to_states[paIdx]) {
86 out.addToTrackStates(st);
87 }
88 }
89 if (const auto pa = pa_to_lpa.find(paIdx); pa != pa_to_lpa.end()) {
90 if (const auto b = lpa_to_btag.find(pa->second); b != lpa_to_btag.end()) {
91 out.addToTrackStates(
92 aabtag::vertexState(b->second, pawalk::trueCharge(pa->second)));
93 }
94 }
95 }
96 }
97
98 put(std::move(trk_out), "TRAC", "Tracks", Provenance::Derived);
99}
100
101} // namespace delphi_edm4hep::track_hybrid
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::unordered_map< int, int > lpaToTrack()
edm4hep::TrackState vertexState(int i, int charge)
void forEachPA(F &&fn)
Definition PaWalk.h:67
int trueCharge(int lpa)
Definition PaWalk.h:50