ATLAS Offline Software
FPGAActsTrkConverter.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
3 #include "FPGAActsTrkConverter.h"
5 #include "Acts/Surfaces/PerigeeSurface.hpp"
8 
9 #include <format>
10 #include <sstream>
11 //Heavily inspired by https://gitlab.cern.ch/atlas/athena/-/blob/main/Tracking/Acts/ActsTrackReconstruction/src/RandomProtoTrackCreator.cxx
12 
14  const std::string& name,
15  const IInterface* parent): base_class(type,name,parent) { }
16 
17 
19 
20  ATH_MSG_DEBUG("Initializing FPGAActsTrkConverter...");
21 
22  // Get SCT & pixel Identifier helpers
23  ATH_CHECK(detStore()->retrieve(m_pixelId, "PixelID"));
24  ATH_CHECK(detStore()->retrieve(m_SCTId, "SCT_ID"));
25 
26  return StatusCode::SUCCESS;
27 
28 }
29 
33  std::vector<ActsTrk::ProtoTrack> & foundProtoTracks,
34  const std::vector<std::vector<FPGATrackSimHit>>& hitsInRoads,
35  const std::vector<FPGATrackSimRoad>& roads) const {
36 
37  ATH_MSG_INFO("Creating Acts proto-tracks from FPGA roads...");
38 
39  if (hitsInRoads.size() > 0) {
40  std::multimap<xAOD::DetectorIDHashType, const xAOD::PixelCluster*> pixelClusterMap;
41  for (const xAOD::PixelCluster* cluster : pixelContainer) {
42  pixelClusterMap.emplace(cluster->identifierHash(), cluster);
43  }
44 
45  std::multimap<IdentifierHash, const xAOD::StripCluster*> stripClusterMap;
46  for (const xAOD::StripCluster* cluster : stripContainer) {
47  stripClusterMap.emplace(cluster->identifierHash(), cluster);
48 
49  }
50  for(size_t roadIndex=0; roadIndex<=hitsInRoads.size()-1;roadIndex++) {
51  std::vector<ActsTrk::ATLASUncalibSourceLink> points;
52  ATH_CHECK(findPrototrackMeasurements(ctx, pixelContainer, stripContainer, pixelClusterMap, stripClusterMap, points, hitsInRoads.at(roadIndex)));
53  if (points.size()) {
54  std::unique_ptr<Acts::BoundTrackParameters> inputPerigee = makeParams(roads.at(roadIndex));
55  foundProtoTracks.emplace_back(points, std::move(inputPerigee));
56  ATH_MSG_INFO("Made a prototrack with " << points.size() << " measurements");
57  }
58  }
59  }
60 
61  return StatusCode::SUCCESS;
62 }
63 
67  std::vector<ActsTrk::ProtoTrack>& foundProtoTracks,
68  const std::vector<FPGATrackSimTrack>& tracks) const {
69 
70  ATH_MSG_INFO("Creating Acts proto-tracks from FPGA tracks...");
71  // Initialize multimaps for pixel and strip clusters
72  std::multimap<xAOD::DetectorIDHashType, const xAOD::PixelCluster*> pixelClusterMap;
73  for (const xAOD::PixelCluster* cluster : pixelContainer) {
74  pixelClusterMap.emplace(cluster->identifierHash(), cluster);
75  }
76 
77  std::multimap<IdentifierHash, const xAOD::StripCluster*> stripClusterMap;
78  for (const xAOD::StripCluster* cluster : stripContainer) {
79  stripClusterMap.emplace(cluster->identifierHash(), cluster);
80  }
81  for (const FPGATrackSimTrack& track : tracks) {
82  if (not track.passedOR()) continue;
83  std::vector<ActsTrk::ATLASUncalibSourceLink> points;
84  const std::vector <FPGATrackSimHit>& hits = track.getFPGATrackSimHits();
85  ATH_CHECK(findPrototrackMeasurements(ctx, pixelContainer, stripContainer, pixelClusterMap, stripClusterMap, points, hits));
86  if (points.size()) {
87  ATH_MSG_DEBUG("\tMaking a proto-track with " << points.size() << " clusters");
88  std::unique_ptr<Acts::BoundTrackParameters> inputPerigee = makeParams(track);
89  foundProtoTracks.emplace_back(points, std::move(inputPerigee));
90  }
91  }
92  return StatusCode::SUCCESS;
93 }
94 
98  const std::multimap<xAOD::DetectorIDHashType, const xAOD::PixelCluster*> & pixelClusterMap,
99  const std::multimap<IdentifierHash, const xAOD::StripCluster*> & stripClusterMap,
100  std::vector<ActsTrk::ATLASUncalibSourceLink>& measurements,
101  const std::vector <FPGATrackSimHit>& hits) const {
102  if (hits.empty()) {
103  ATH_MSG_ERROR("Found FPGATrack without hits");
104  return StatusCode::FAILURE;
105  }
106 
107  for (const FPGATrackSimHit& h : hits) {
108  if (h.isReal()) {
109  if (h.isPixel()) {
110  ATH_MSG_DEBUG("Looking for Pixel cluster to match");
111  auto range = pixelClusterMap.equal_range(h.getIdentifierHash());
112  for (auto it = range.first; it != range.second; ++it) {
113  ATH_CHECK(matchTrackMeasurements<xAOD::PixelCluster>(ctx, *(it->second), h, measurements, pixelContainer));
114  }
115  }
116  else if (h.isStrip()) {
117  ATH_MSG_DEBUG("Looking for Strip cluster to match");
118  auto range = stripClusterMap.equal_range(h.getIdentifierHash());
119  for (auto it = range.first; it != range.second; ++it) {
120  ATH_CHECK(matchTrackMeasurements<xAOD::StripCluster>(ctx, *(it->second), h, measurements, stripContainer));
121  }
122  }
123  else {
124  ATH_MSG_ERROR("FPGA hit not classified as pixel or strip");
125  return StatusCode::FAILURE;
126  }
127  }
128  else {
129  ATH_MSG_DEBUG("Skipping hit as non-Real");
130  }
131  }
132  return StatusCode::SUCCESS;
133 }
134 
135 std::vector<Identifier> FPGAActsTrkConverter::getRdoIdList(const FPGATrackSimHit& hit) const
136 {
137  std::vector<Identifier> ids;
138  const auto& idHashVec = hit.getIDHashVec();
139  const auto& phiIndexVec = hit.getPhiIndexVec();
140  const auto& etaIndexVec = hit.getEtaIndexVec();
141 
142  ids.reserve(idHashVec.size());
143 
144  if (hit.isPixel())
145  {
146  for (size_t i = 0; i < idHashVec.size(); ++i)
147  {
148  ids.emplace_back(m_pixelId->pixel_id(
149  m_pixelId->wafer_id(idHashVec[i]),
150  phiIndexVec[i],
151  etaIndexVec[i]
152  ));
153  }
154  }
155  else if (hit.isStrip())
156  {
157  for (size_t i = 0; i < idHashVec.size(); ++i)
158  {
159  ids.emplace_back(m_SCTId->strip_id(
160  m_SCTId->wafer_id(idHashVec[i]),
161  phiIndexVec[i]
162  ));
163  }
164  }
165  return ids;
166 }
167 
168 
169 template <typename XAOD_CLUSTER>
171  const XAOD_CLUSTER& cluster,
172  const FPGATrackSimHit & trackHit,
173  std::vector<ActsTrk::ATLASUncalibSourceLink>& measurements,
174  const DataVector<XAOD_CLUSTER>& clusterContainer) const
175 {
176  std::vector<Identifier> rdoIDs;
177  if (trackHit.getHitType() == HitType::spacepoint)
178  rdoIDs = getRdoIdList(trackHit.getOriginalHit());
179  else
180  rdoIDs = getRdoIdList(trackHit);
181 
182  const auto& rdoList = cluster.rdoList();
183  if (rdoIDs.size() != rdoList.size()) return StatusCode::SUCCESS;
184  size_t matchedCounter = 0;
185  for (const Identifier& id : rdoIDs) {
186  if (std::find(rdoList.begin(), rdoList.end(), id) != rdoList.end()) matchedCounter++;
187  }
188 
189  if (matchedCounter == rdoList.size()) {
190  measurements.emplace_back(ActsTrk::makeATLASUncalibSourceLink(&clusterContainer, cluster.index(), ctx));
191  }
192  else if (matchedCounter > 0) {
193  std::stringstream ss;
194  ss << "List of xAOD cluster rdoIDs:\n";
195  for (const Identifier& id : rdoList) ss << id << "\n";
196 
197  ATH_MSG_ERROR(std::format("Noticed rdoID mismatch: commonHits = {} | xAODClusterHits = {} | FPGAClusterHits = {}\n{}",
198  matchedCounter, rdoList.size(), rdoIDs.size(), ss.str()));
199  return StatusCode::SUCCESS; // TODO: revert to FAILURE once this is fixed
200  }
201  return StatusCode::SUCCESS;
202 }
203 
204 std::unique_ptr<Acts::BoundTrackParameters> FPGAActsTrkConverter::makeParams (const FPGATrackSimRoad &road) const{
205  using namespace Acts::UnitLiterals;
206 
207  std::shared_ptr<const Acts::Surface> actsSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(Acts::Vector3(0., 0., 0.));
208  Acts::BoundVector params;
209 
210  constexpr double GeVToMeV = 1000;
211  double d0=0.; //?
212  double z0=0.; //?
213  double phi=road.getX();
214  double eta=0.2;
215  double theta=2*std::atan(std::exp(-eta));
216  double qop = (std::abs(road.getY()) > 1E-9) ? road.getY()/GeVToMeV : 1E-12;
217  double t=0.; //?
218  ATH_MSG_DEBUG("\tphi=" <<phi << " eta=" << eta << " qop=" << qop);
219 
220  params << d0, z0, phi, theta, qop, t;
221 
222  // Covariance - TODO
223  Acts::BoundSquareMatrix cov = Acts::BoundSquareMatrix::Identity();
224  cov *= (GeVToMeV*GeVToMeV);
225 
226  // some ACTS paperwork
227  Trk::ParticleHypothesis hypothesis = Trk::pion;
229  Acts::PdgParticle absPdg = Acts::makeAbsolutePdgParticle(Acts::ePionPlus);
230  Acts::ParticleHypothesis actsHypothesis{
231  absPdg, mass, Acts::AnyCharge{1.0f}};
232 
233  return std::make_unique<Acts::BoundTrackParameters>(actsSurface, params,
234  cov, actsHypothesis);
235 
236 }
237 
238 
239 std::unique_ptr<Acts::BoundTrackParameters> FPGAActsTrkConverter::makeParams (const FPGATrackSimTrack &track) const{
240 
241  using namespace Acts::UnitLiterals;
242  std::shared_ptr<const Acts::Surface> actsSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(Acts::Vector3(0., 0., 0.));
243  Acts::BoundVector params;
244 
245  constexpr double GeVToMeV = 1000.;
246  double d0=track.getD0();
247  double z0=track.getZ0();
248  double phi=track.getPhi();
249  double eta=track.getEta();
250  double theta=track.getTheta();
251  double qopt=track.getQOverPt()*GeVToMeV;
252  double pt=track.getPt()/GeVToMeV;
253  double px=pt*std::cos(phi);
254  double py=pt*std::sin(phi);
255  double pz=pt*std::sinh(eta);
256  double p=std::sqrt(px*px+py*py+pz*pz);
257  double qop=((p > 1e-10) ? (1/p) : 1e10);
258  if (qopt < 0) qop *= -1;
259  double t=0.;
260 
261  params << d0, z0, phi, theta, qop, t;
262  ATH_MSG_DEBUG("\td0= " << d0 << " z0=" <<z0 << " phi=" <<phi << " theta=" << theta<< " qoverp=" << qop);
263 
264  // Covariance - let's be honest and say we have no clue ;-)
265  Acts::BoundSquareMatrix cov = Acts::BoundSquareMatrix::Identity();
266 
267  (cov)(0,0) *= 0.16; // d0: 0.4 **2 (conservative)
268  (cov)(1,1) *= 25; // z0: 5**2 = 25 (conservative)
269  (cov)(2,2) *= 0.0008; // phi: 0.02**2 = 0.0004, increase a bit = double
270  (cov)(3,3) *= 0.0008; // width in eta is nearly 0.2, but width in theta = 2*atan(e^-eta) will vary. Take biggest one, which is at eta of 0 when width is 0.02, so get 0.02**2 = 0.0004, increase a bit = double
271  (cov)(4,4) *= 0.36; // qop also varies with eta. Error on q/pt conservatively = 0.0003 in mev ^-1, or 0.3 in gev ^-1, double to start giving us 0.6. then square that to get 0.36
272 
273 
274 
275  // some ACTS paperwork
276  Trk::ParticleHypothesis hypothesis = Trk::pion;
278  Acts::PdgParticle absPdg = Acts::makeAbsolutePdgParticle(Acts::ePionPlus);
279  Acts::ParticleHypothesis actsHypothesis{
280  absPdg, mass, Acts::AnyCharge{1.0f}};
281 
282  return std::make_unique<Acts::BoundTrackParameters>(actsSurface, params,
283  cov, actsHypothesis);
284 
285 }
286 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
FPGATrackSimHit::getEtaIndexVec
const std::vector< int > & getEtaIndexVec() const
Definition: FPGATrackSimHit.h:198
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
FPGATrackSimHit::isStrip
bool isStrip() const
Definition: FPGATrackSimHit.h:65
test_pyathena.px
px
Definition: test_pyathena.py:18
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
vtune_athena.format
format
Definition: vtune_athena.py:14
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
FPGAActsTrkConverter::getRdoIdList
std::vector< Identifier > getRdoIdList(const FPGATrackSimHit &hit) const
Definition: FPGAActsTrkConverter.cxx:135
Base_Fragment.mass
mass
Definition: Sherpa_i/share/common/Base_Fragment.py:59
FPGATrackSimTrack
Definition: FPGATrackSimTrack.h:18
FPGAActsTrkConverter::m_SCTId
const SCT_ID * m_SCTId
Definition: FPGAActsTrkConverter.h:57
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
FPGAActsTrkConverter::FPGAActsTrkConverter
FPGAActsTrkConverter(const std::string &type, const std::string &name, const IInterface *parent)
Definition: FPGAActsTrkConverter.cxx:13
theta
Scalar theta() const
theta method
Definition: AmgMatrixBasePlugin.h:75
FPGATrackSimHit::getPhiIndexVec
const std::vector< int > & getPhiIndexVec() const
Definition: FPGATrackSimHit.h:197
skel.it
it
Definition: skel.GENtoEVGEN.py:407
plotBeamSpotVxVal.cov
cov
Definition: plotBeamSpotVxVal.py:200
test_pyathena.pt
pt
Definition: test_pyathena.py:11
FPGATrackSimHit::getIDHashVec
const std::vector< unsigned > & getIDHashVec() const
Definition: FPGATrackSimHit.h:199
HitType::spacepoint
@ spacepoint
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
DataPrepToActsConfig.stripContainer
stripContainer
Definition: DataPrepToActsConfig.py:10
FPGATrackSimRoad::getX
float getX() const
Definition: FPGATrackSimRoad.h:89
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
PixelID::wafer_id
Identifier wafer_id(int barrel_ec, int layer_disk, int phi_module, int eta_module) const
For a single crystal.
Definition: PixelID.h:360
drawFromPickle.atan
atan
Definition: drawFromPickle.py:36
FPGATrackSimHit
Definition: FPGATrackSimHit.h:41
python.SystemOfUnits.MeV
float MeV
Definition: SystemOfUnits.py:172
Trk::ParticleHypothesis
ParticleHypothesis
Definition: ParticleHypothesis.h:28
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
ActsTrk::makeATLASUncalibSourceLink
ATLASUncalibSourceLink makeATLASUncalibSourceLink(const xAOD::UncalibratedMeasurementContainer *container, std::size_t index, [[maybe_unused]] const EventContext &ctx)
Definition: ATLASSourceLink.h:30
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
FPGAActsTrkConverter.h
FPGATrackSimHit::getOriginalHit
const FPGATrackSimHit getOriginalHit() const
Definition: FPGATrackSimHit.cxx:131
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
Trk::pion
@ pion
Definition: ParticleHypothesis.h:32
FPGAActsTrkConverter::matchTrackMeasurements
StatusCode matchTrackMeasurements(const EventContext &ctx, const XAOD_CLUSTER &cluster, const FPGATrackSimHit &trackHit, std::vector< ActsTrk::ATLASUncalibSourceLink > &measurements, const DataVector< XAOD_CLUSTER > &clusterContainer) const
Definition: FPGAActsTrkConverter.cxx:170
TRT::Track::d0
@ d0
Definition: InnerDetector/InDetCalibEvent/TRT_CalibData/TRT_CalibData/TrackInfo.h:62
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
Amg::pz
@ pz
Definition: GeoPrimitives.h:40
test_pyathena.parent
parent
Definition: test_pyathena.py:15
xAOD::StripCluster_v1
Definition: StripCluster_v1.h:17
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TRT::Track::z0
@ z0
Definition: InnerDetector/InDetCalibEvent/TRT_CalibData/TRT_CalibData/TrackInfo.h:63
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
ParticleHypothesis.h
FPGATrackSimHit::isPixel
bool isPixel() const
Definition: FPGATrackSimHit.h:64
FPGAActsTrkConverter::m_pixelId
const PixelID * m_pixelId
Definition: FPGAActsTrkConverter.h:56
Trk::ParticleMasses::mass
constexpr double mass[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:56
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
Amg::py
@ py
Definition: GeoPrimitives.h:39
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
python.subdetectors.mmg.ids
ids
Definition: mmg.py:8
VP1PartSpect::E
@ E
Definition: VP1PartSpectFlags.h:21
IdentifierHash.h
FPGAActsTrkConverter::initialize
virtual StatusCode initialize() override final
Definition: FPGAActsTrkConverter.cxx:18
xAOD::ParticleHypothesis
ParticleHypothesis
Definition: TrackingPrimitives.h:193
FPGAActsTrkConverter::findPrototrackMeasurements
StatusCode findPrototrackMeasurements(const EventContext &ctx, const xAOD::PixelClusterContainer &pixelClusterContainer, const xAOD::StripClusterContainer &stripClusterContainer, const std::multimap< xAOD::DetectorIDHashType, const xAOD::PixelCluster * > &pixelClusterMap, const std::multimap< IdentifierHash, const xAOD::StripCluster * > &stripClusterMap, std::vector< ActsTrk::ATLASUncalibSourceLink > &measurements, const std::vector< FPGATrackSimHit > &hits) const
Definition: FPGAActsTrkConverter.cxx:95
FPGATrackSimRoad::getY
float getY() const
Definition: FPGATrackSimRoad.h:90
xAOD::PixelCluster_v1
Definition: PixelCluster_v1.h:17
h
FPGAActsTrkConverter::findProtoTracks
virtual StatusCode findProtoTracks(const EventContext &ctx, const xAOD::PixelClusterContainer &pixelContainer, const xAOD::StripClusterContainer &stripContainer, std::vector< ActsTrk::ProtoTrack > &foundProtoTracks, const std::vector< std::vector< FPGATrackSimHit >> &hitsInRoads, const std::vector< FPGATrackSimRoad > &roads) const override final
Definition: FPGAActsTrkConverter.cxx:30
PixelID::pixel_id
Identifier pixel_id(int barrel_ec, int layer_disk, int phi_module, int eta_module, int phi_index, int eta_index) const
For an individual pixel.
Definition: PixelID.h:428
FPGAActsTrkConverter::makeParams
std::unique_ptr< Acts::BoundTrackParameters > makeParams(const FPGATrackSimRoad &road) const
Definition: FPGAActsTrkConverter.cxx:204
xAOD::track
@ track
Definition: TrackingPrimitives.h:513
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
PowhegControl_ttFCNC_NLO.params
params
Definition: PowhegControl_ttFCNC_NLO.py:226
SCT_ID::wafer_id
Identifier wafer_id(int barrel_ec, int layer_disk, int phi_module, int eta_module, int side) const
For a single side of module.
Definition: SCT_ID.h:464
DataPrepToActsConfig.pixelContainer
pixelContainer
Definition: DataPrepToActsConfig.py:9
jobOptions.points
points
Definition: jobOptions.GenevaPy8_Zmumu.py:97
FPGATrackSimRoad
Definition: FPGATrackSimRoad.h:31
FPGATrackSimHit::getHitType
HitType getHitType() const
Definition: FPGATrackSimHit.h:57
SCT_ID::strip_id
Identifier strip_id(int barrel_ec, int layer_disk, int phi_module, int eta_module, int side, int strip) const
For an individual strip.
Definition: SCT_ID.h:535
Identifier
Definition: IdentifierFieldParser.cxx:14