LDMX Software
TrackingUtils.cxx
1#include "Tracking/Sim/TrackingUtils.h"
2
4
5namespace tracking {
6namespace sim {
7namespace utils {
8
9// This method returns the sensor ID
10int getSensorID(const ldmx::SimTrackerHit& hit) {
11 bool debug = false;
12
13 ldmx::TrackerID tid(hit.getID());
14 int vol = (tid.subdet() == ldmx::SD_TRACKER_RECOIL) ? 3 : 2;
15
16 unsigned int sensor_id = 0;
17 unsigned int layer_id = 0;
18
19 // tagger numbering scheme for surfaces mapping
20 // Layers from 1 to 14 => transform to 0->13
21 if (vol == 2) {
22 sensor_id = (hit.getLayerID() + 1) % 2; // 0,1,0,1 ...
23
24 // v12
25 // layerId = (hit.getLayerID() + 1) / 2; //1,2,3,4,5,6,7
26 // v14
27 layer_id = 7 - ((hit.getLayerID() - 1) / 2);
28 }
29
30 // recoil numbering scheme for surfaces mapping
31 if (vol == 3) {
32 // For axial-stereo modules use the same numbering scheme as the tagger
33 if (hit.getLayerID() < 9) {
34 sensor_id = (hit.getLayerID() + 1) % 2;
35 layer_id = (hit.getLayerID() + 1) / 2;
36 }
37
38 // For the axial only modules
39 else {
40 sensor_id = hit.getModuleID();
41 layer_id = (hit.getLayerID() + 2) / 2; // 9->11 /2 = 5 10->12 / 2 = 6
42 }
43 }
44
45 // vol * 1000 + ly * 100 + sensor
46 unsigned int index = vol * 1000 + layer_id * 100 + sensor_id;
47
48 if (debug) {
49 std::cout << "LdmxSpacePointConverter::Check index::" << vol << "--"
50 << layer_id << "--" << sensor_id << "==>" << index << std::endl;
51 std::cout << vol << "===" << hit.getLayerID() << "===" << hit.getModuleID()
52 << std::endl;
53 }
54
55 return index;
56}
57
58// This method converts a SimHit in a LdmxSpacePoint for the Acts seeder.
59// (1) Rotate the coordinates into acts::seedFinder coordinates defined by
60// B-Field along z_ axis [Z_ldmx -> X_acts, X_ldmx->Y_acts, Y_ldmx->Z_acts]
61// (2) Saves the error information. At the moment the errors are fixed. They
62// should be obtained from the digitized hits_.
63// Vol==2 for tagger, Vol==3 for recoil
64ldmx::LdmxSpacePoint* convertSimHitToLdmxSpacePoint(
65 const ldmx::SimTrackerHit& hit, unsigned int vol, double sigma_u,
66 double sigma_v) {
67 unsigned int index = getSensorID(hit);
68
69 // Rotate position
70 float ldmxsp_x = hit.getPosition()[2];
71 float ldmxsp_y = hit.getPosition()[0];
72 float ldmxsp_z = hit.getPosition()[1];
73
74 return new ldmx::LdmxSpacePoint(ldmxsp_x, ldmxsp_y, ldmxsp_z, hit.getTime(),
75 index, hit.getEdep(), sigma_u * sigma_u,
76 sigma_v * sigma_v, hit.getID());
77}
78
79void flatCov(Acts::BoundSquareMatrix cov, std::vector<double>& v_cov) {
80 v_cov.clear();
81 v_cov.reserve(cov.rows() * (cov.rows() + 1) / 2);
82 for (int i = 0; i < cov.rows(); i++)
83 for (int j = i; j < cov.cols(); j++) v_cov.push_back(cov(i, j));
84}
85
86Acts::BoundSquareMatrix unpackCov(const std::vector<double>& v_cov) {
87 Acts::BoundSquareMatrix cov;
88 int e{0};
89 for (int i = 0; i < cov.rows(); i++)
90 for (int j = i; j < cov.cols(); j++) {
91 cov(i, j) = v_cov.at(e);
92 cov(j, i) = cov(i, j);
93 e++;
94 }
95
96 return cov;
97}
98
99// Rotate LDMX global -> ACTS frame: z_ldmx->x_acts, x_ldmx->y_acts,
100// y_ldmx->z_acts (0 0 1) * (x,y,z)_ldmx = x_acts (1 0 0) * (x,y,z)_ldmx =
101// y_acts (0 1 0) * (x,y,z)_ldmx = z_acts
102Acts::SquareMatrix3 ldmx2ActsRotation() {
103 Acts::SquareMatrix3 r;
104 r << 0., 0., 1., 1., 0., 0., 0., 1., 0.;
105 return r;
106}
107
108Acts::Vector3 ldmx2Acts(Acts::Vector3 ldmx_v) {
109 return ldmx2ActsRotation() * ldmx_v;
110}
111
112// Rotate ACTS frame -> LDMX global (inverse of ldmx2Acts, i.e. transpose):
113// x_ldmx = y_acts, y_ldmx = z_acts, z_ldmx = x_acts
114Acts::SquareMatrix3 acts2LdmxRotation() {
115 return ldmx2ActsRotation().transpose();
116}
117
118Acts::Vector3 acts2Ldmx(Acts::Vector3 acts_v) {
119 return acts2LdmxRotation() * acts_v;
120}
121
122// Transform position, momentum and charge to free parameters
123Acts::FreeVector toFreeParameters(Acts::Vector3 pos_, Acts::Vector3 mom,
124 Acts::ActsScalar q) {
125 Acts::FreeVector free_params;
126 Acts::ActsScalar p = mom.norm() * Acts::UnitConstants::MeV;
127
128 free_params[Acts::eFreePos0] = pos_(Acts::ePos0) * Acts::UnitConstants::mm;
129 free_params[Acts::eFreePos1] = pos_(Acts::ePos1) * Acts::UnitConstants::mm;
130 free_params[Acts::eFreePos2] = pos_(Acts::ePos2) * Acts::UnitConstants::mm;
131 free_params[Acts::eFreeTime] = 0.;
132 free_params[Acts::eFreeDir0] = mom(0) / mom.norm();
133 free_params[Acts::eFreeDir1] = mom(1) / mom.norm();
134 free_params[Acts::eFreeDir2] = mom(2) / mom.norm();
135 free_params[Acts::eFreeQOverP] =
136 (q != Acts::ActsScalar(0)) ? (q / p) : 0.; // 1. / p instead?
137
138 return free_params;
139}
140
141// Pack the acts track parameters into something that is serializable for the
142// event bus
143std::vector<double> convertActsToLdmxPars(Acts::BoundVector acts_par) {
144 std::vector<double> v_ldmx(
145 acts_par.data(), acts_par.data() + acts_par.rows() * acts_par.cols());
146 return v_ldmx;
147}
148
149Acts::BoundVector boundState(const ldmx::Track& trk) {
150 Acts::BoundVector param_vec;
151 param_vec << trk.getD0(), trk.getZ0(), trk.getPhi(), trk.getTheta(),
152 trk.getQoP(), trk.getT();
153 return param_vec;
154}
155
156Acts::BoundTrackParameters boundTrackParameters(
157 const ldmx::Track& trk, std::shared_ptr<Acts::PerigeeSurface> perigee) {
158 Acts::BoundVector param_vec = boundState(trk);
159 Acts::BoundSquareMatrix cov_mat = unpackCov(trk.getPerigeeCov());
160 auto part_hypo{Acts::SinglyChargedParticleHypothesis::electron()};
161 return Acts::BoundTrackParameters(perigee, param_vec, std::move(cov_mat),
162 part_hypo);
163}
164
165// Return an unbound surface
166const std::shared_ptr<Acts::PlaneSurface> unboundSurface(double xloc,
167 double yloc,
168 double zloc) {
169 // Define the target surface - be careful:
170 // x_ - downstream
171 // y_ - left (when looking along x_)
172 // z_ - up
173 // Passing identity here means that your target surface is oriented in the
174 // same way
175 Acts::RotationMatrix3 surf_rotation = Acts::RotationMatrix3::Zero();
176 // u direction along +Y
177 surf_rotation(1, 0) = 1;
178 // v direction along +Z
179 surf_rotation(2, 1) = 1;
180 // w direction along +X
181 surf_rotation(0, 2) = 1;
182
183 Acts::Vector3 pos(xloc, yloc, zloc);
184 Acts::Translation3 surf_translation(pos);
185 Acts::Transform3 surf_transform(surf_translation * surf_rotation);
186
187 // Unbounded surface
188 const std::shared_ptr<Acts::PlaneSurface> target_surface =
189 Acts::Surface::makeShared<Acts::PlaneSurface>(surf_transform);
190
191 return Acts::Surface::makeShared<Acts::PlaneSurface>(surf_transform);
192}
193
194// This method returns a source link index
195std::size_t sourceLinkHash(const Acts::SourceLink& a) {
196 return static_cast<std::size_t>(
198}
199
200// This method checks if two source links are equal by index
201bool sourceLinkEquality(const Acts::SourceLink& a, const Acts::SourceLink& b) {
202 return a.get<acts_examples::IndexSourceLink>().index() ==
203 b.get<acts_examples::IndexSourceLink>().index();
204}
205
206/*
207 * Build a TrackState from ACTS BoundTrackParameters.
208 * All output quantities (position, momentum, covariance) are in the LDMX
209 * global frame: x=horizontal, y=vertical, z=downstream.
210 *
211 * Covariance transformation steps:
212 * 1. Bound (6x6) -> Free (8x8) via ACTS bound-to-free Jacobian
213 * 2. Drop time row/col -> 7x7
214 * 3. 7D (x,y,z,d0,d1,d2,qop) -> 6D Cartesian (x,y,z,px,py,pz) in ACTS frame
215 * 4. Rotate 6x6 covariance ACTS -> LDMX via block-diagonal rotation
216 * 5. Flatten upper triangle -> 21-element vector
217 */
218ldmx::Track::TrackState makeTrackState(
219 const Acts::GeometryContext& gctx,
220 const Acts::BoundTrackParameters& bound_pars,
221 ldmx::TrackStateType ts_type) {
223 new_ts.ts_type_ = ts_type;
224
225 const double p = bound_pars.absoluteMomentum(); // GeV
226 const Acts::Vector3 acts_pos = bound_pars.position(gctx);
227 const Acts::Vector3 acts_dir = bound_pars.direction();
228
229 // Rotate position and momentum to LDMX frame
230 const Acts::SquareMatrix3 r = acts2LdmxRotation();
231 const Acts::Vector3 ldmx_pos = r * acts_pos;
232 const Acts::Vector3 ldmx_mom = r * (acts_dir * p);
233
234 new_ts.pos_ = {ldmx_pos[0], ldmx_pos[1], ldmx_pos[2]};
235 // Convert momentum from ACTS native units (GeV) to MeV
236 new_ts.mom_ = {ldmx_mom[0] / Acts::UnitConstants::MeV,
237 ldmx_mom[1] / Acts::UnitConstants::MeV,
238 ldmx_mom[2] / Acts::UnitConstants::MeV};
239
240 const auto& bound_cov = bound_pars.covariance();
241 if (!bound_cov.has_value()) {
242 std::cerr << "TrackingUtils::makeTrackState: bound covariance missing\n";
243 return new_ts;
244 }
245
246 // Step 1: Bound (6x6) -> Free (8x8) covariance
247 const Acts::BoundToFreeMatrix j_btf =
248 bound_pars.referenceSurface().boundToFreeJacobian(gctx, acts_pos,
249 acts_dir);
250 const Acts::FreeSquareMatrix free_cov =
251 j_btf * bound_cov.value() * j_btf.transpose();
252
253 // Step 2: Drop time row/col (eFreeTime = 3) -> 7x7
254 // Remaining indices: pos(0,1,2), dir(4,5,6), qop(7)
255 constexpr std::array<int, 7> k_keep = {
256 Acts::eFreePos0, Acts::eFreePos1, Acts::eFreePos2, Acts::eFreeDir0,
257 Acts::eFreeDir1, Acts::eFreeDir2, Acts::eFreeQOverP};
258 Eigen::Matrix<double, 7, 7> free_cov7;
259 for (int i = 0; i < 7; ++i)
260 for (int j = 0; j < 7; ++j)
261 free_cov7(i, j) = free_cov(k_keep[i], k_keep[j]);
262
263 // Step 3: Jacobian from 7D free-no-time -> 6D Cartesian in ACTS frame
264 // p_i = dir_i * p, dp_i/d(dir_j) = p*delta_ij, dp_i/d(qop) = -dir_i*p/qop
265 const double qop = bound_pars.parameters()[Acts::eBoundQOverP];
266 Eigen::Matrix<double, 6, 7> j_fp = Eigen::Matrix<double, 6, 7>::Zero();
267 j_fp.block<3, 3>(0, 0) = Eigen::Matrix3d::Identity(); // pos -> pos
268 j_fp.block<3, 3>(3, 3) = p * Eigen::Matrix3d::Identity(); // dir -> mom
269 j_fp(3, 6) = -acts_dir[0] * p / qop; // qop -> px
270 j_fp(4, 6) = -acts_dir[1] * p / qop; // qop -> py
271 j_fp(5, 6) = -acts_dir[2] * p / qop; // qop -> pz
272
273 const Eigen::Matrix<double, 6, 6> cov_acts =
274 j_fp * free_cov7 * j_fp.transpose();
275
276 // Step 4: Rotate covariance ACTS -> LDMX using block-diagonal R_6 = diag(R,R)
277 Eigen::Matrix<double, 6, 6> r_6 = Eigen::Matrix<double, 6, 6>::Zero();
278 r_6.block<3, 3>(0, 0) = r;
279 r_6.block<3, 3>(3, 3) = r;
280 const Eigen::Matrix<double, 6, 6> cov_ldmx = r_6 * cov_acts * r_6.transpose();
281
282 // Step 5: Flatten upper triangle -> 21 elements.
283 // Scale momentum rows/cols from GeV to MeV:
284 // pos-pos (i<3, j<3): x1 [mm^2]
285 // pos-mom (i<3, j>=3): x1000 [mm*MeV]
286 // mom-mom (i>=3, j>=3): x1e6 [MeV^2]
287 const double mev = Acts::UnitConstants::MeV;
288 new_ts.pos_mom_cov_.reserve(21);
289 for (int i = 0; i < 6; ++i) {
290 for (int j = i; j < 6; ++j) {
291 double scale = 1.0;
292 if (i >= 3) scale /= mev; // row is momentum (GeV -> MeV)
293 if (j >= 3) scale /= mev; // col is momentum (GeV -> MeV)
294 new_ts.pos_mom_cov_.push_back(cov_ldmx(i, j) * scale);
295 }
296 }
297
298 return new_ts;
299}
300
301} // namespace utils
302} // namespace sim
303} // namespace tracking
Class that defines a Tracker detector ID with a module number.
Represents a simulated tracker hit in the simulation.
int getModuleID() const
Get the module ID associated with a hit.
float getEdep() const
Get the energy deposited on the hit [MeV].
std::vector< float > getPosition() const
Get the XYZ position of the hit [mm].
int getID() const
Get the detector ID of the hit.
float getTime() const
Get the global time of the hit [ns].
int getLayerID() const
Get the geometric layer ID of the hit.
Implementation of a track object.
Definition Track.h:53
Extension of DetectorID providing access to layer and module number for tracker IDs.
Definition TrackerID.h:20
The measurement calibrator can be a function or a class/struct able to retrieve the sim hits containe...