delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
TeBank.cpp
Go to the documentation of this file.
2
3#include "phdst/uxcom.hpp"
4
5#include <cmath>
6
7namespace ph = phdst;
8
10
11namespace {
12
13// A TER occupies 10 + m words: descriptor, three coordinates, theta, phi,
14// 1/P, m covariance entries, then ndf, chi2 and track length.
15constexpr int kTerFixedWords = 10;
16
17// Map from descriptor bit (1-indexed) to TE-basis field index (0..5).
18// Returns -1 if the bit doesn't contribute to the 6-component basis.
19//
20// The measured coordinates are the stored triple minus the one the surface
21// fixes (exx.car:5045-5055): a cylinder fixes R, leaving (R*Phi, z), while a
22// plane fixes z, leaving (x, y).
23int basisIndexForBit(int bit, bool cylindrical) {
24 switch (bit) {
25 case 6: return cylindrical ? 1 : 0; // R*Phi on a cylinder, x on a plane
26 case 7: return cylindrical ? 2 : 1; // z " y "
27 case 8: return 3;
28 case 9: return 4;
29 case 10: return 5;
30 case 11: return 5;
31 default: return -1;
32 }
33}
34
35inline int lowerTriOffset(int i, int j) {
36 return i * (i + 1) / 2 + j;
37}
38
39// Number of error-matrix words this descriptor implies: the lower triangle
40// over the fields it flags as measured.
41int covEntries(int descriptor, bool cylindrical) {
42 int n = 0;
43 for (int bit = 6; bit <= 11; ++bit) {
44 if (((descriptor >> (bit - 1)) & 0x1) &&
45 basisIndexForBit(bit, cylindrical) >= 0) {
46 ++n;
47 }
48 }
49 return n * (n + 1) / 2;
50}
51
52// Decode the record whose descriptor sits at word `w`. The caller has already
53// checked that the whole record lies inside the bank.
54Decoded decodeElement(int lte, int w, int descriptor) {
55 Decoded d;
56 d.descriptor = descriptor;
57 d.is_cylindrical = (descriptor & 0x1) != 0;
58 d.invPt = ((descriptor >> 10) & 0x1) != 0; // bit 11
59
60 // Measured fields in ascending bit order; the error matrix rows follow the
61 // same order.
62 int meas_basis_idx[6];
63 int n = 0;
64 for (int bit = 6; bit <= 11; ++bit) {
65 if ((descriptor >> (bit - 1)) & 0x1) {
66 const int idx = basisIndexForBit(bit, d.is_cylindrical);
67 if (idx >= 0) {
68 meas_basis_idx[n++] = idx;
69 d.has_meas[idx] = true;
70 }
71 }
72 }
73 d.n_measured = n;
74 d.m_cov_entries = n * (n + 1) / 2;
75
76 d.coord[0] = ph::Q(lte + w + 1);
77 d.coord[1] = ph::Q(lte + w + 2);
78 d.coord[2] = ph::Q(lte + w + 3);
79 d.theta = ph::Q(lte + w + 4);
80 d.phi = ph::Q(lte + w + 5);
81 d.invP = ph::Q(lte + w + 6);
82
83 // Lower-tri row-major over the measured fields, scattered into the sparse
84 // 6x6 TE-basis storage.
85 for (int i = 0; i < n; ++i) {
86 for (int j = 0; j <= i; ++j) {
87 d.cov[covOffset(meas_basis_idx[i], meas_basis_idx[j])] =
88 ph::Q(lte + w + 7 + lowerTriOffset(i, j));
89 }
90 }
91
92 d.ndf = ph::Q(lte + w + 7 + d.m_cov_entries);
93 d.chi2 = ph::Q(lte + w + 8 + d.m_cov_entries);
94 d.length = ph::Q(lte + w + 9 + d.m_cov_entries);
95 return d;
96}
97
98} // namespace
99
100Module decodeModule(int lte, int blen) {
101 Module mod;
102 if (lte <= 0 || blen < 1 + kTerFixedWords) return mod;
103
104 // From PXDST 2.87 the label carries the reconstruction stage as its first
105 // decimal: 42.1 is the very forward tracker, stage 1.
106 const double label_word = ph::Q(lte + 1);
107 mod.label = static_cast<int>(label_word);
108 mod.stage = static_cast<int>(std::lround(label_word * 10)) - mod.label * 10;
109
110 for (int w = 2; w <= blen; ) {
111 // Each record sizes itself, so the stride is only known once its
112 // descriptor is read and a module may mix measurement codes.
113 const int descriptor =
114 static_cast<int>(std::lround(ph::Q(lte + w)));
115 const int last =
116 w + kTerFixedWords - 1 +
117 covEntries(descriptor, (descriptor & 0x1) != 0);
118
119 // Stop rather than read into the neighbouring ZEBRA bank. `ok` then stays
120 // false, recording that the walk did not account for every word.
121 if (last > blen) break;
122
123 mod.elements.push_back(decodeElement(lte, w, descriptor));
124 w = last + 1; // the next descriptor, or blen + 1 when done
125 mod.ok = (w == blen + 1);
126 }
127 return mod;
128}
129
130} // namespace delphi_edm4hep::te_bank
Module decodeModule(int lte, int blen)
Definition TeBank.cpp:100
constexpr int covOffset(int i, int j)
Definition Helix.h:38
Definition PaWalk.h:21
std::vector< Decoded > elements
Definition TeBank.h:75