delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
TrackElements.cpp
Go to the documentation of this file.
1// TrackElementsWriter — implementation.
2
4
8
9#include <edm4hep/TrackCollection.h>
10#include <edm4hep/TrackerHitPlaneCollection.h>
11#include <podio/UserDataCollection.h>
12
13#include <array>
14#include <cmath>
15#include <cstdint>
16#include <limits>
17#include <string>
18
19namespace ph = phdst;
20
22
23namespace {
24
25constexpr float kCm2Mm = 10.f;
26
27// Marks a track-state component the module did not measure. Zero cannot be
28// used: it is a legal measured value.
29constexpr float kNotMeasured = std::numeric_limits<float>::quiet_NaN();
30
31// TE-basis field indices used by te_bank::Decoded::has_meas. The first two
32// coordinates are (x, y) on a plane and (R*Phi, z) on a cylinder.
33constexpr int kBasisX = 0;
34constexpr int kBasisY = 1;
35constexpr int kBasisRPhi = 1;
36constexpr int kBasisZ = 2;
37constexpr int kBasisTheta = 3;
38constexpr int kBasisPhi = 4;
39constexpr int kBasisInvP = 5;
40
41// TEVF measures two coordinates and no direction, so it is a plane hit; every
42// other module measures at least phi and becomes a segment.
43struct TeModule { const char* name; bool plane; };
44constexpr std::array<TeModule, 8> kTeModules{{
45 {"TEID", false}, {"TETP", false}, {"TEOD", false}, {"TEFA", false},
46 {"TEFB", false}, {"TERF", false}, {"TEST", false}, {"TEVF", true },
47}};
48
49} // namespace
50
52{
53 const std::size_t n = kTeModules.size();
54 edm4hep::TrackCollection segments;
55 edm4hep::TrackerHitPlaneCollection planes;
56 podio::UserDataCollection<float> segment_length;
57 podio::UserDataCollection<float> plane_length;
58
59 Output out;
60
61 // Field strength as the event recorded it, so both passes agree. Zero when
62 // absent, which yields zero curvature rather than a wrong one.
63 const double B = frame_.getParameter<float>(
64 makeName("EVT", "BField")).value_or(0.f);
65
66 pawalk::forEachPA([&](int lpa, int paIdx) {
67 if (paIdx >= static_cast<int>(out.pa_to_segments.size())) {
68 out.pa_to_segments.resize(paIdx + 1);
69 out.pa_to_plane_hits.resize(paIdx + 1);
70 }
71 for (std::size_t m = 0; m < n; ++m) {
72 const int lte = pawalk::lphpa(kTeModules[m].name, lpa);
73 if (lte <= 0) continue;
74 const auto mod = te_bank::decodeModule(lte, pawalk::iphreq());
75
76 for (const auto& te : mod.elements) {
77 // The label carries the detector and the reconstruction stage, so one
78 // integer identifies which TE produced this object.
79 const std::int32_t kind = mod.label * 10 + mod.stage;
80
81 if (kTeModules[m].plane) {
82 auto hit = planes.create();
83 hit.setType(kind);
84 hit.setQuality(te.descriptor);
85 hit.setPosition({ te.coord[0] * kCm2Mm,
86 te.coord[1] * kCm2Mm,
87 te.coord[2] * kCm2Mm });
88 out.pa_to_plane_hits[paIdx].push_back(hit);
89 plane_length.push_back(te.length * kCm2Mm);
90 continue;
91 }
92
93 const auto helix = Helix::fromTrackElement(
94 te.coord[0], te.coord[1], te.coord[2], te.theta, te.phi,
95 te.invP, te.invPt, te.is_cylindrical, te.cov, B);
96
97 auto state = helix.toTrackState(edm4hep::TrackState::AtOther);
98 // The reference point is on the fitted segment, so the helix passes
99 // through it: D0 = Z0 = 0 wherever that coordinate was measured. An
100 // unmeasured coordinate is a placeholder (e.g. z on an inner-detector
101 // jet-chamber element), so the offset it would define is not measured
102 // either. A cylinder fixes R and a plane fixes z.
103 const bool transverse = te.is_cylindrical
104 ? te.has_meas[kBasisRPhi]
105 : te.has_meas[kBasisX] && te.has_meas[kBasisY];
106 const bool longitudinal = te.is_cylindrical ? te.has_meas[kBasisZ] : true;
107 state.D0 = transverse ? 0.f : kNotMeasured;
108 state.Z0 = longitudinal ? 0.f : kNotMeasured;
109 if (!te.has_meas[kBasisPhi]) state.phi = kNotMeasured;
110 if (!te.has_meas[kBasisTheta]) state.tanLambda = kNotMeasured;
111 if (!te.has_meas[kBasisInvP]) state.omega = kNotMeasured;
112
113 auto trk = segments.create();
114 trk.setType(kind);
115 trk.setChi2(te.chi2);
116 trk.setNdf(static_cast<std::int32_t>(std::lround(te.ndf)));
117 trk.addToTrackStates(state);
118 out.pa_to_segments[paIdx].push_back(trk);
119 segment_length.push_back(te.length * kCm2Mm);
120 }
121 }
122 });
123
124 // Emitted whether or not any module is on the file, so the collection set
125 // does not vary between samples.
126 put(std::move(segments), "TE", "Segments", Provenance::Transcribed);
127 put(std::move(segment_length), "TE", "Segments_Length", Provenance::Transcribed);
128 put(std::move(planes), "TEVF", "TrackElementPlane",
130 put(std::move(plane_length), "TEVF", "TrackElementPlane_Length",
132
133 ctx_.track_elements = std::move(out);
134}
135
136} // namespace delphi_edm4hep::track_elements
std::string makeName(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 fromTrackElement(double c1, double c2, double c3, double theta, double phi, double invP, bool invPt, bool cylindrical, const CovMatrix6 &teCov, double B)
Definition Helix.cpp:158
void forEachPA(F &&fn)
Definition PaWalk.h:67
int lphpa(const char *iddp, int lpa, int nump=0)
Definition PaWalk.h:34
int iphreq(int nump=1)
Definition PaWalk.h:42
Module decodeModule(int lte, int blen)
Definition TeBank.cpp:100
Definition PaWalk.h:21
std::vector< std::vector< edm4hep::TrackerHitPlane > > pa_to_plane_hits
std::vector< std::vector< edm4hep::Track > > pa_to_segments