LDMX Software
TrackingUtils.h
1#ifndef TRACKUTILS_H_
2#define TRACKUTILS_H_
3
4// TODO:: MAKE A CXX!!
5
6// Recoil back layers numbering scheme for module
7
8// +Y /\ 4 3 2 1 0
9// |
10// |
11// -Y \/ 9 8 7 6 5
12// -X <---- ----> +X
13
14// ModN (x, y, z)
15// 0 (96, 40, z2)
16// 1 (48, 40, z1)
17// 2 (0, 40, z2)
18// 3 (-48, 40, z1)
19// 4 (-96, 40, z2)
20
21// 5 (96, -40, z2)
22// 6 (48, -40, z1)
23// 7 (0, -40, z2)
24// 8 (-48, -40, z1)
25// 9 (-96, -40, z2)
26
27// ---< SimCore >---//
29#include "Tracking/Sim/LdmxSpacePoint.h"
30
31// --- Tracking ---//
32#include "Tracking/Event/Track.h"
33
34// --- < ACTS > --- //
35#include "Acts/Definitions/Algebra.hpp"
36#include "Acts/Definitions/PdgParticle.hpp"
37#include "Acts/Definitions/TrackParametrization.hpp"
38#include "Acts/Definitions/Units.hpp"
39#include "Acts/EventData/TrackParameters.hpp"
40#include "Acts/Surfaces/PerigeeSurface.hpp"
41#include "Acts/Surfaces/PlaneSurface.hpp"
42#include "Tracking/Event/Measurement.h"
43
44namespace tracking {
45namespace sim {
46namespace utils {
47
48/*
49 It looks like the recoil is subdetector ID 4 and tagger is subdetector ID 1
50https://github.com/LDMX-Software/ldmx-sw/blob/0476ccc407e068560518e0614aa83b6eda22e186/DetDescr/include/DetDescr/DetectorID.h#L11-L24
51So you could bit shift and mask to get these numbers
52https://github.com/LDMX-Software/ldmx-sw/blob/0476ccc407e068560518e0614aa83b6eda22e186/DetDescr/include/DetDescr/DetectorID.h#L38-L39
53(sim_tracker_hit.getID() >> 26)&0x3f
54or if you are in ldmx-sw, it is easier, more robust, and just as performant to
55wrap the ID in the helper class and use its accessors sd =
56TrackerID(sim_tracker_hit.getID()).subdet(); if (sd ==
57SubdetectorID::SD_TRACKER_RECOIL) {
58 // hit in recoil
59} else if (sd == SubdetectorID::SD_TRACKER_TAGGER) {
60 // hit in tagger
61} else {
62 // this should never happen since the TrackerID constructor checks for
63mal-formed IDs
64}
65*/
66
67// This method returns the sensor ID
68inline int getSensorID(const ldmx::SimTrackerHit& hit) {
69 bool debug = false;
70
71 int vol = 2;
72
73 // TODO!! FIX THIS HARDCODE!
74 if (hit.getPosition()[2] > 0) vol = 3;
75
76 unsigned int sensorId = 0;
77 unsigned int layerId = 0;
78
79 // tagger numbering scheme for surfaces mapping
80 // Layers from 1 to 14 => transform to 0->13
81 if (vol == 2) {
82 sensorId = (hit.getLayerID() + 1) % 2; // 0,1,0,1 ...
83
84 // v12
85 // layerId = (hit.getLayerID() + 1) / 2; //1,2,3,4,5,6,7
86 // v14
87 layerId = 7 - ((hit.getLayerID() - 1) / 2);
88 }
89
90 // recoil numbering scheme for surfaces mapping
91 if (vol == 3) {
92 // For axial-stereo modules use the same numbering scheme as the tagger
93 if (hit.getLayerID() < 9) {
94 sensorId = (hit.getLayerID() + 1) % 2;
95 layerId = (hit.getLayerID() + 1) / 2;
96 }
97
98 // For the axial only modules
99 else {
100 sensorId = hit.getModuleID();
101 layerId = (hit.getLayerID() + 2) / 2; // 9->11 /2 = 5 10->12 / 2 = 6
102 }
103 }
104
105 // vol * 1000 + ly * 100 + sensor
106 unsigned int index = vol * 1000 + layerId * 100 + sensorId;
107
108 if (debug) {
109 std::cout << "LdmxSpacePointConverter::Check index::" << vol << "--"
110 << layerId << "--" << sensorId << "==>" << index << std::endl;
111 std::cout << vol << "===" << hit.getLayerID() << "===" << hit.getModuleID()
112 << std::endl;
113 }
114
115 return index;
116}
117
118// This method converts a SimHit in a LdmxSpacePoint for the Acts seeder.
119// (1) Rotate the coordinates into acts::seedFinder coordinates defined by
120// B-Field along z axis [Z_ldmx -> X_acts, X_ldmx->Y_acts, Y_ldmx->Z_acts] (2)
121// Saves the error information. At the moment the errors are fixed. They should
122// be obtained from the digitized hits.
123
124// TODO::Move to shared pointers?!
125// TODO::Pass to instances?
126// Vol==2 for tagger, Vol==3 for recoil
127
128inline ldmx::LdmxSpacePoint* convertSimHitToLdmxSpacePoint(
129 const ldmx::SimTrackerHit& hit, unsigned int vol = 2, double sigma_u = 0.05,
130 double sigma_v = 1.) {
131 unsigned int index = getSensorID(hit);
132
133 // Rotate position
134 float ldmxsp_x = hit.getPosition()[2];
135 float ldmxsp_y = hit.getPosition()[0];
136 float ldmxsp_z = hit.getPosition()[1];
137
138 return new ldmx::LdmxSpacePoint(ldmxsp_x, ldmxsp_y, ldmxsp_z, hit.getTime(),
139 index, hit.getEdep(), sigma_u * sigma_u,
140 sigma_v * sigma_v, hit.getID());
141}
142
143// BoundSymMatrix doesn't exist in v36 .. use BoundSquareMatrix
144// have to change this everywhere .. I think using BoundSysMatrix was defined
145// exactly the same as BoundSquareMatrix is now in ACTs
146inline void flatCov(Acts::BoundSquareMatrix cov, std::vector<double>& v_cov) {
147 v_cov.clear();
148 v_cov.reserve(cov.rows() * (cov.rows() + 1) / 2);
149 for (int i = 0; i < cov.rows(); i++)
150 for (int j = i; j < cov.cols(); j++) v_cov.push_back(cov(i, j));
151}
152
153inline Acts::BoundSquareMatrix unpackCov(const std::vector<double>& v_cov) {
154 Acts::BoundSquareMatrix cov;
155 int e{0};
156 for (int i = 0; i < cov.rows(); i++)
157 for (int j = i; j < cov.cols(); j++) {
158 cov(i, j) = v_cov.at(e);
159 cov(j, i) = cov(i, j);
160 e++;
161 }
162
163 return cov;
164}
165
166// Rotate to ACTS frame
167// z->x, x->y, y->z
168
169//(0 0 1) x = z
170//(1 0 0) y = x
171//(0 1 0) z = y
172
173inline Acts::Vector3 Ldmx2Acts(Acts::Vector3 ldmx_v) {
174 // TODO::Move it to a static member
175 Acts::SquareMatrix3 acts_rot;
176 acts_rot << 0., 0., 1., 1., 0., 0., 0., 1, 0.;
177
178 return acts_rot * ldmx_v;
179}
180
181// Transform position, momentum and charge to free parameters
182
183inline Acts::FreeVector toFreeParameters(Acts::Vector3 pos, Acts::Vector3 mom,
184 Acts::ActsScalar q) {
185 Acts::FreeVector free_params;
186 Acts::ActsScalar p = mom.norm() * Acts::UnitConstants::MeV;
187
188 free_params[Acts::eFreePos0] = pos(Acts::ePos0) * Acts::UnitConstants::mm;
189 free_params[Acts::eFreePos1] = pos(Acts::ePos1) * Acts::UnitConstants::mm;
190 free_params[Acts::eFreePos2] = pos(Acts::ePos2) * Acts::UnitConstants::mm;
191 free_params[Acts::eFreeTime] = 0.;
192 free_params[Acts::eFreeDir0] = mom(0) / mom.norm();
193 free_params[Acts::eFreeDir1] = mom(1) / mom.norm();
194 free_params[Acts::eFreeDir2] = mom(2) / mom.norm();
195 free_params[Acts::eFreeQOverP] =
196 (q != Acts::ActsScalar(0)) ? (q / p) : 0.; // 1. / p instead?
197
198 return free_params;
199}
200
201// Pack the acts track parameters into something that is serializable for the
202// event bus
203
204inline std::vector<double> convertActsToLdmxPars(Acts::BoundVector acts_par) {
205 std::vector<double> v_ldmx(
206 acts_par.data(), acts_par.data() + acts_par.rows() * acts_par.cols());
207 return v_ldmx;
208}
209
210inline Acts::BoundVector boundState(const ldmx::Track& trk) {
211 Acts::BoundVector paramVec;
212 paramVec << trk.getD0(), trk.getZ0(), trk.getPhi(), trk.getTheta(),
213 trk.getQoP(), trk.getT();
214 return paramVec;
215}
216
217inline Acts::BoundVector boundState(const ldmx::Track::TrackState& ts) {
218 Acts::BoundVector paramVec;
219 paramVec << ts.params[0], ts.params[1], ts.params[2], ts.params[3],
220 ts.params[4], ts.params[5];
221 return paramVec;
222}
223
224inline Acts::BoundTrackParameters boundTrackParameters(
225 const ldmx::Track& trk, std::shared_ptr<Acts::PerigeeSurface> perigee) {
226 Acts::BoundVector paramVec = boundState(trk);
227 Acts::BoundSquareMatrix covMat = unpackCov(trk.getPerigeeCov());
228 auto partHypo{Acts::SinglyChargedParticleHypothesis::electron()};
229 // auto
230 // part{Acts::GenericParticleHypothesis(Acts::ParticleHypothesis(Acts::PdgParticle(trk.getPdgID())))};
231 // return Acts::BoundTrackParameters(perigee, paramVec, std::move(covMat));
232 // need to add particle hypothesis
233 return Acts::BoundTrackParameters(perigee, paramVec, std::move(covMat),
234 partHypo);
235}
236
237inline Acts::BoundTrackParameters btp(const ldmx::Track::TrackState& ts,
238 std::shared_ptr<Acts::Surface> surf,
239 int pdgid) {
240 Acts::BoundVector paramVec = boundState(ts);
241 Acts::BoundSquareMatrix covMat = unpackCov(ts.cov);
242 auto partHypo{Acts::SinglyChargedParticleHypothesis::electron()};
243 // auto
244 // part{Acts::GenericParticleHypothesis(Acts::ParticleHypothesis(Acts::PdgParticle(pdgid)))};
245 return Acts::BoundTrackParameters(surf, paramVec, std::move(covMat),
246 partHypo);
247}
248
249// Return an unbound surface
250inline const std::shared_ptr<Acts::PlaneSurface> unboundSurface(
251 double xloc, double yloc = 0., double zloc = 0.) {
252 // Define the target surface - be careful:
253 // x - downstream
254 // y - left (when looking along x)
255 // z - up
256 // Passing identity here means that your target surface is oriented in the
257 // same way
258 Acts::RotationMatrix3 surf_rotation = Acts::RotationMatrix3::Zero();
259 // u direction along +Y
260 surf_rotation(1, 0) = 1;
261 // v direction along +Z
262 surf_rotation(2, 1) = 1;
263 // w direction along +X
264 surf_rotation(0, 2) = 1;
265
266 Acts::Vector3 pos(xloc, yloc, zloc);
267 Acts::Translation3 surf_translation(pos);
268 Acts::Transform3 surf_transform(surf_translation * surf_rotation);
269
270 // Unbounded surface
271 const std::shared_ptr<Acts::PlaneSurface> target_surface =
272 Acts::Surface::makeShared<Acts::PlaneSurface>(surf_transform);
273
274 return Acts::Surface::makeShared<Acts::PlaneSurface>(surf_transform);
275}
276
277} // namespace utils
278} // namespace sim
279} // namespace tracking
280
281#endif
Class which encapsulates information from a hit in a simulated tracking detector.
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:52
The measurement calibrator can be a function or a class/struct able to retrieve the sim hits containe...