ATLAS Offline Software
Loading...
Searching...
No Matches
GeometryRealmConvTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
9
11// ACTS
12#include "Acts/Surfaces/StrawSurface.hpp"
13#include "Acts/Surfaces/PerigeeSurface.hpp"
14#include "Acts/Surfaces/PlaneSurface.hpp"
15
16#include "Acts/Surfaces/RectangleBounds.hpp"
17#include "Acts/Surfaces/TrapezoidBounds.hpp"
18#include "Acts/Surfaces/CylinderBounds.hpp"
19#include "Acts/Surfaces/DiscBounds.hpp"
20#include "Acts/Surfaces/LineBounds.hpp"
21#include "Acts/Surfaces/RadialBounds.hpp"
22#include "Acts/Surfaces/DiamondBounds.hpp"
23
24#include "Acts/Definitions/Units.hpp"
25#include "Acts/EventData/BoundTrackParameters.hpp"
26#include "Acts/EventData/VectorTrackContainer.hpp"
27#include "Acts/EventData/TransformationHelpers.hpp"
28#include "Acts/Geometry/TrackingGeometry.hpp"
29#include "Acts/Propagator/detail/JacobianEngine.hpp"
30#include "Acts/Surfaces/detail/PlanarHelper.hpp"
31
33#include "Acts/EventData/TrackStatePropMask.hpp"
34#include "Acts/EventData/SourceLink.hpp"
35
46
48
49using namespace Acts::UnitLiterals;
50using SurfacePtr_t = ActsTrk::GeometryRealmConvTool::SurfacePtr_t;
51
52namespace ActsTrk{
55 if (parent() != toolSvc()) {
56 ATH_MSG_ERROR("The tool is initialized as a private tool but should be public");
57 return StatusCode::FAILURE;
58 }
59 ATH_CHECK(m_ctxProvider.initialize());
61 m_trackingGeometrySvc->trackingGeometry()->visitSurfaces([&](const Acts::Surface *surface) {
62 // find acts surface with the same detector element ID
63 if (!surface->isSensitive()) {
64 return;
65 }
66 const auto *actsElement = dynamic_cast<const ISurfacePlacement*>(surface->surfacePlacement());
67 if (!actsElement) {
68 return;
69 }
70 // Conversion from Acts to ATLAS surface impossible for the TRT so the TRT
71 // surfaces are not stored in this map
72 if (actsElement->detectorType() == DetectorType::Trt) {
73 return;
74 }
75 auto [it, ok] = m_actsSurfaceMap.insert(std::make_pair(actsElement->identify(), surface->getSharedPtr()));
76 if (!ok) {
77 ATH_MSG_WARNING("ATLAS ID " << actsElement->identify()
78 << " has two ACTS surfaces: "
79 << it->second->geometryId() << " and "
80 << surface->geometryId());
81 }
82 });
84 return StatusCode::SUCCESS;
85 }
86 std::shared_ptr<Trk::SurfaceBounds>
87 GeometryRealmConvTool::translateBounds(const Acts::SurfaceBounds& bounds) const {
88 switch (bounds.type()) {
89 using enum Acts::SurfaceBounds::BoundsType;
90 case eRectangle:{
91 using ParEnum_t = Acts::RectangleBounds::BoundValues;
92 const auto& cBounds = static_cast<const Acts::RectangleBounds&>(bounds);
93 return std::make_shared<Trk::RectangleBounds>(cBounds.get(ParEnum_t::eMaxX),
94 cBounds.get(ParEnum_t::eMaxY));
95 } case eTrapezoid: {
96 using ParEnum_t = Acts::TrapezoidBounds::BoundValues;
97 const auto& cBounds = static_cast<const Acts::TrapezoidBounds&>(bounds);
98 return std::make_shared<Trk::TrapezoidBounds>(cBounds.get(ParEnum_t::eHalfLengthXnegY),
99 cBounds.get(ParEnum_t::eHalfLengthXposY),
100 cBounds.get(ParEnum_t::eHalfLengthY));
101 } case eDisc: {
102 using ParEnum_t = Acts::RadialBounds::BoundValues;
103 const auto& cBounds = static_cast<const Acts::RadialBounds&>(bounds);
104 return std::make_shared<Trk::DiscBounds>(cBounds.get(ParEnum_t::eMinR),
105 cBounds.get(ParEnum_t::eMaxR),
106 cBounds.get(ParEnum_t::eAveragePhi),
107 cBounds.get(ParEnum_t::eHalfPhiSector));
108 } case eCylinder: {
109 using ParEnum_t = Acts::CylinderBounds::BoundValues;
110 const auto& cBounds = static_cast<const Acts::CylinderBounds&>(bounds);
111 return std::make_shared<Trk::CylinderBounds>(cBounds.get(ParEnum_t::eR),
112 cBounds.get(ParEnum_t::eHalfPhiSector),
113 cBounds.get(ParEnum_t::eAveragePhi),
114 cBounds.get(ParEnum_t::eHalfLengthZ));
115 } case eLine: {
116 using ParEnum_t = Acts::LineBounds::BoundValues;
117 const auto& cBounds = static_cast<const Acts::LineBounds&>(bounds);
118 return std::make_shared<Trk::CylinderBounds>(cBounds.get(ParEnum_t::eR),
119 cBounds.get(ParEnum_t::eHalfLengthZ));
120 } case eDiamond: {
121 using ParEnum_t = Acts::DiamondBounds::BoundValues;
122 const auto& cBounds = static_cast<const Acts::DiamondBounds&>(bounds);
123 return std::make_shared<Trk::DiamondBounds>(cBounds.get(ParEnum_t::eHalfLengthXnegY),
124 cBounds.get(ParEnum_t::eHalfLengthXzeroY),
125 cBounds.get(ParEnum_t::eHalfLengthXposY),
126 cBounds.get(ParEnum_t::eHalfLengthYneg),
127 cBounds.get(ParEnum_t::eHalfLengthYpos));
128 } default:
129 break;
130 }
131 THROW_EXCEPTION("The bounds "<<bounds<<" cannot be translated");
132 return nullptr;
133 }
135 const Acts::Surface& surface) const {
136 const Acts::GeometryContext tgContext = m_ctxProvider.getGeometryContext(ctx);
137 const Amg::Transform3D& trf{surface.localToGlobalTransform(tgContext)};
138 switch (surface.type()) {
139 using enum Acts::Surface::SurfaceType;
140 case Plane:
141 return SurfacePtr_t{new Trk::PlaneSurface(trf, translateBounds(surface.bounds()))};
142 case Cylinder:
143 return SurfacePtr_t{new Trk::CylinderSurface(trf,
144 std::dynamic_pointer_cast<Trk::CylinderBounds>(translateBounds(surface.bounds())))};
145 case Perigee:
146 return SurfacePtr_t{new Trk::PerigeeSurface(trf)};
147 case Disc:
148 return SurfacePtr_t{new Trk::DiscSurface(trf,
149 std::dynamic_pointer_cast<Trk::DiscBounds>(translateBounds(surface.bounds())))};
150 case Straw: {
151 auto bounds = std::dynamic_pointer_cast<Trk::CylinderBounds>(translateBounds(surface.bounds()));
152 return SurfacePtr_t{new Trk::StraightLineSurface(trf, bounds->r(), bounds->halflengthZ())};
153 } default:
154 break;
155 }
156 THROW_EXCEPTION("ActsToTrkConverterTool() - Surface cannot be translated "
157 <<surface.toString(tgContext));
158
159 return nullptr;
160 }
161
163 const Acts::Surface& actsSurface) const{
164 const auto *placement = dynamic_cast<const ISurfacePlacement*>(actsSurface.surfacePlacement());
165 if (!placement) {
166 return translateFreeSurface(ctx, actsSurface);
167 }
168 switch (placement->detectorType()) {
169 using enum DetectorType;
170 case Pixel:
171 case Sct:
172 case Hgtd:
173 case Trt: {
174 const auto* actsElement = getActsDetectorElement(actsSurface);
175 if (actsElement) {
176 return SurfacePtr_t{&actsElement->atlasSurface()};
177 }
178 break;
179 } case Mdt:
180 case Rpc:
181 case Tgc:
182 case Csc:
183 case sTgc:
184 case Mm: {
185 const MuonGM::MuonDetectorManager* detMgr{nullptr};
186 if (!SG::get(detMgr, m_muonMgrKey, ctx).isSuccess() || !detMgr) {
187 THROW_EXCEPTION("Failed to retrieve the muon detector manager");
188 }
189 return SurfacePtr_t{&detMgr->getReadoutElement(placement->identify())->surface(placement->identify())};
190 } default:
191 break;
192 }
194 return translateFreeSurface(ctx, actsSurface);
195 }
196 std::shared_ptr<const Acts::Surface> GeometryRealmConvTool::convertSurfaceToActs(const Trk::Surface& atlasSurface) const {
197 Identifier atlasID = atlasSurface.associatedDetectorElementIdentifier();
198 auto it = m_actsSurfaceMap.find(atlasID);
199 if (it != m_actsSurfaceMap.end()) {
200 return it->second;
201 }
202 const Amg::Transform3D& trf{atlasSurface.transform()};
203 switch (atlasSurface.type()){
204 using enum Trk::SurfaceType;
205 case Plane:
206 return Acts::Surface::makeShared<Acts::PlaneSurface>(trf);
207 case Perigee:
208 return Acts::Surface::makeShared<Acts::PerigeeSurface>(trf);
209 case Line:
210 return Acts::Surface::makeShared<Acts::StrawSurface>(trf);
211 // TODO - implement the missing types?
212 default: {
213 break;
214 }
215 }
216 std::stringstream surfStr{};
217 atlasSurface.dump(surfStr);
218 throw std::domain_error(std::format("Failed to translate surface {:}", surfStr.str()));
219 }
220
221 Acts::BoundTrackParameters
223 const Trk::TrackParameters& atlasParameter,
224 Trk::ParticleHypothesis hypothesis) const {
225
226 std::shared_ptr<const Acts::Surface> actsSurface{};
227 Acts::BoundVector params{};
228 const Acts::GeometryContext tgContext = m_ctxProvider.getGeometryContext(ctx);
229
230 // get the associated surface
231 try {
232 actsSurface = convertSurfaceToActs(atlasParameter.associatedSurface());
233 } catch (const std::exception &e) {
234 ATH_MSG_ERROR("Could not find ACTS detector surface for this TrackParameter:");
235 ATH_MSG_ERROR(atlasParameter);
236 throw; // Nothing we can do, so just pass exception on...
237 }
238
239 // Construct track parameters
240 const auto& atlasParam{atlasParameter.parameters()};
241 if (actsSurface->bounds().type() == Acts::SurfaceBounds::BoundsType::eAnnulus) {
242 // Annulus surfaces are constructed differently in Acts/Trk so we need to
243 // convert local coordinates
244 const Amg::Vector3D& position{atlasParameter.position()};
245 auto result = actsSurface->globalToLocal(tgContext, position, atlasParameter.momentum());
246 if (result.ok()) {
247 params << (*result)[0], (*result)[1], atlasParam[Trk::phi0],
248 atlasParam[Trk::theta],
249 atlasParameter.charge() / (atlasParameter.momentum().mag() * 1_MeV),
250 0.;
251 } else {
252 ATH_MSG_WARNING("Unable to convert annulus surface - globalToLocal failed");
253 }
254 } else {
255 params << atlasParam[Trk::locX], atlasParam[Trk::locY],
256 atlasParam[Trk::phi0], atlasParam[Trk::theta],
257 atlasParameter.charge() / (atlasParameter.momentum().mag() * 1_MeV), 0.;
258 }
259
260 std::optional<Acts::BoundMatrix> cov{};
261 if (atlasParameter.covariance()) {
262 cov = Acts::BoundMatrix::Identity();
263 cov->topLeftCorner(5, 5) = *atlasParameter.covariance();
264
265 // Convert the covariance matrix from MeV
266 // FIXME: This needs to handle the annulus case as well - currently the cov
267 // is wrong for annulus surfaces
268 for (int i = 0; i < cov->rows(); ++i) {
269 (*cov)(i, 4) = (*cov)(i, 4) / 1_MeV;
270 }
271 for (int i = 0; i < cov->cols(); ++i) {
272 (*cov)(4, i) = (*cov)(4, i) / 1_MeV;
273 }
274 }
275 return Acts::BoundTrackParameters{actsSurface, params, std::move(cov),
276 ParticleHypothesis::convert(hypothesis)};
277 }
278 std::unique_ptr<Trk::TrackParameters>
280 const Acts::BoundTrackParameters& actsParameter) const {
281
282
283 std::optional<AmgSymMatrix(5)> cov = std::nullopt;
284 if (actsParameter.covariance()) {
285 AmgSymMatrix(5) newcov(actsParameter.covariance()->topLeftCorner<5, 5>());
286 // Convert the covariance matrix to GeV
287 for (int i = 0; i < newcov.rows(); i++) {
288 newcov(i, 4) = newcov(i, 4) * 1_MeV;
289 }
290 for (int i = 0; i < newcov.cols(); i++) {
291 newcov(4, i) = newcov(4, i) * 1_MeV;
292 }
293 cov = newcov;
294 }
295
296 const Acts::Surface &actsSurface = actsParameter.referenceSurface();
297 SurfacePtr_t trkSurface = convertSurfaceToTrk(ctx, actsSurface);
298 switch (actsSurface.type()) {
299 case Acts::Surface::SurfaceType::Cone: {
300 const auto &coneSurface = static_cast<const Trk::ConeSurface&>(*trkSurface);
301 return std::make_unique<Trk::AtaCone>(
302 actsParameter.get<Acts::eBoundLoc0>(),
303 actsParameter.get<Acts::eBoundLoc1>(),
304 actsParameter.get<Acts::eBoundPhi>(),
305 actsParameter.get<Acts::eBoundTheta>(),
306 actsParameter.get<Acts::eBoundQOverP>() * 1_MeV, coneSurface, cov);
307 } case Acts::Surface::SurfaceType::Cylinder: {
308 const auto &cylSurface{static_cast<const Trk::CylinderSurface&>(*trkSurface)};
309 return std::make_unique<Trk::AtaCylinder>(
310 actsParameter.get<Acts::eBoundLoc0>(),
311 actsParameter.get<Acts::eBoundLoc1>(),
312 actsParameter.get<Acts::eBoundPhi>(),
313 actsParameter.get<Acts::eBoundTheta>(),
314 actsParameter.get<Acts::eBoundQOverP>() * 1_MeV, cylSurface, cov);
315 } case Acts::Surface::SurfaceType::Disc: {
316 if (trkSurface->type() == Trk::SurfaceType::Disc) {
317 const auto& discSurface{static_cast<const Trk::DiscSurface&>(*trkSurface)};
318 return std::make_unique<Trk::AtaDisc>(
319 actsParameter.get<Acts::eBoundLoc0>(),
320 actsParameter.get<Acts::eBoundLoc1>(),
321 actsParameter.get<Acts::eBoundPhi>(),
322 actsParameter.get<Acts::eBoundTheta>(),
323 actsParameter.get<Acts::eBoundQOverP>() * 1_MeV, discSurface, cov);
324 } else if (trkSurface->type() == Trk::SurfaceType::Plane) {
325 const Acts::GeometryContext tgContext = m_ctxProvider.getGeometryContext(ctx);
326 auto& planeSurface{static_cast<const Trk::PlaneSurface&>(*trkSurface)};
327 // need to convert to plane position on plane surface (annulus bounds)
328 auto helperSurface = Acts::Surface::makeShared<Acts::PlaneSurface>(planeSurface.transform());
329
330 auto covpc = actsParameter.covariance().value();
332 Acts::FreeVector freePars = Acts::transformBoundToFreeParameters(actsSurface, tgContext,
333 actsParameter.parameters());
334
336 Acts::BoundVector targetPars = Acts::transformFreeToBoundParameters(freePars,
337 *helperSurface, tgContext).value();
338
339
340 Acts::FreeMatrix freeTransportJacobian{Acts::FreeMatrix::Identity()};
341
342 Acts::FreeVector freeToPathDerivatives{Acts::FreeVector::Zero()};
343 freeToPathDerivatives.head<3>() = freePars.segment<3>(Acts::eFreeDir0);
344
345 auto boundToFreeJacobian = actsSurface.boundToFreeJacobian(tgContext,
346 freePars.segment<3>(Acts::eFreePos0),
347 freePars.segment<3>(Acts::eFreeDir0));
348
349 Acts::BoundMatrix boundToBoundJac =
350 Acts::detail::boundToBoundTransportJacobian(tgContext, freePars,
351 boundToFreeJacobian, freeTransportJacobian,
352 freeToPathDerivatives, *helperSurface);
353
354 Acts::BoundMatrix targetCov{boundToBoundJac * covpc * boundToBoundJac.transpose()};
355
356 return std::make_unique<Trk::AtaPlane>(
357 targetPars[Acts::eBoundLoc0], targetPars[Acts::eBoundLoc1],
358 targetPars[Acts::eBoundPhi], targetPars[Acts::eBoundTheta],
359 targetPars[Acts::eBoundQOverP] * 1_MeV, planeSurface,
360 targetCov.topLeftCorner<5, 5>());
361 } else {
362 throw std::domain_error("Acts::DiscSurface is not associated with ATLAS disc or plane surface");
363 }
364 break;
365 } case Acts::Surface::SurfaceType::Perigee: {
366 const auto& perSurface = static_cast<const Trk::PerigeeSurface&>(*trkSurface);
367 return std::make_unique<Trk::Perigee>(
368 actsParameter.get<Acts::eBoundLoc0>(),
369 actsParameter.get<Acts::eBoundLoc1>(),
370 actsParameter.get<Acts::eBoundPhi>(),
371 actsParameter.get<Acts::eBoundTheta>(),
372 actsParameter.get<Acts::eBoundQOverP>() * 1_MeV, perSurface, cov);
373 } case Acts::Surface::SurfaceType::Plane: {
374 auto &plaSurface{static_cast<const Trk::PlaneSurface&>(*trkSurface)};
375 return std::make_unique<Trk::AtaPlane>(
376 actsParameter.get<Acts::eBoundLoc0>(),
377 actsParameter.get<Acts::eBoundLoc1>(),
378 actsParameter.get<Acts::eBoundPhi>(),
379 actsParameter.get<Acts::eBoundTheta>(),
380 actsParameter.get<Acts::eBoundQOverP>() * 1_MeV, plaSurface, cov);
381 } case Acts::Surface::SurfaceType::Straw: {
382 auto& lineSurface{static_cast<const Trk::StraightLineSurface&>(*trkSurface)};
383 return std::make_unique<Trk::AtaStraightLine>(
384 actsParameter.get<Acts::eBoundLoc0>(),
385 actsParameter.get<Acts::eBoundLoc1>(),
386 actsParameter.get<Acts::eBoundPhi>(),
387 actsParameter.get<Acts::eBoundTheta>(),
388 actsParameter.get<Acts::eBoundQOverP>() * 1_MeV, lineSurface, cov);
389 } case Acts::Surface::SurfaceType::Curvilinear: {
390 const Acts::GeometryContext tgContext = m_ctxProvider.getGeometryContext(ctx);
391 return std::make_unique<Trk::CurvilinearParameters>(
392 actsParameter.position(tgContext), actsParameter.get<Acts::eBoundPhi>(),
393 actsParameter.get<Acts::eBoundTheta>(),
394 actsParameter.get<Acts::eBoundQOverP>() * 1_MeV, cov);
395 } case Acts::Surface::SurfaceType::Point:
396 case Acts::Surface::SurfaceType::Other: {
397 break;
398 }
399 }
400 throw std::domain_error("Surface type not found");
401 }
402}
const ActsDetectorElement * getActsDetectorElement(const Acts::Surface &surf)
Attempts to retrieve the ActsDetectorElement associated to the passed ActsSurface.
Scalar mag() const
mag method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define AmgSymMatrix(dim)
ActsTrk::GeometryRealmConvTool::SurfacePtr_t SurfacePtr_t
virtual std::shared_ptr< const Acts::Surface > convertSurfaceToActs(const Trk::Surface &atlasSurface) const override final
Translate the parsed Trk surface into an Acts surface.
virtual Acts::BoundTrackParameters convertTrackParametersToActs(const EventContext &ctx, const Trk::TrackParameters &atlasParameter, Trk::ParticleHypothesis hypothesis=Trk::pion) const override final
Translates the Trk track parameters into bound Acts track parameters with a particle hypothesis.
SurfacePtr_t translateFreeSurface(const EventContext &ctx, const Acts::Surface &surface) const
Translate a surface that is not associated with any detector element.
virtual std::unique_ptr< Trk::TrackParameters > convertTrackParametersToTrk(const EventContext &ctx, const Acts::BoundTrackParameters &actsParameters) const override final
Translates the bounded Acts track parameters to Trk parameters.
SG::ReadCondHandleKey< MuonGM::MuonDetectorManager > m_muonMgrKey
Detector manager to fetch the legacy Trk surfaces.
std::shared_ptr< Trk::SurfaceBounds > translateBounds(const Acts::SurfaceBounds &bounds) const
Translate the Acts surface bounds to its equivalent in the Trk realm.
ServiceHandle< ActsTrk::ITrackingGeometrySvc > m_trackingGeometrySvc
Gaudi::Property< bool > m_extractMuonSurfaces
virtual SurfacePtr_t convertSurfaceToTrk(const EventContext &ctx, const Acts::Surface &actsSurface) const override final
Translates the parsed Acts surface into a Trk::Surface via associated detector element.
ActsTrk::ContextUtility m_ctxProvider
Context provider for geometry, magnetic field and calibration contexts.
std::unordered_map< Identifier, std::shared_ptr< const Acts::Surface > > m_actsSurfaceMap
virtual StatusCode initialize() override final
Extension of the interface of the Acts::SurfacePlacementBase for ATLAS.
The MuonDetectorManager stores the transient representation of the Muon Spectrometer geometry and pro...
const MuonReadoutElement * getReadoutElement(const Identifier &id) const
Get any read out element.
Class for a conical surface in the ATLAS detector.
Definition ConeSurface.h:51
Class for a CylinderSurface in the ATLAS detector.
Class for a DiscSurface in the ATLAS detector.
Definition DiscSurface.h:54
const Amg::Vector3D & momentum() const
Access method for the momentum.
const Amg::Vector3D & position() const
Access method for the position.
double charge() const
Returns the charge.
virtual const Surface & associatedSurface() const override=0
Access to the Surface associated to the Parameters.
Class describing the Line to which the Perigee refers to.
Class for a planaer rectangular or trapezoidal surface in the ATLAS detector.
Class for a StraightLineSurface in the ATLAS detector to describe dirft tube and straw like detectors...
Abstract Base Class for tracking surfaces.
Definition Surface.h:79
virtual MsgStream & dump(MsgStream &sl) const
Output Method for MsgStream, to be overloaded by child classes.
Definition Surface.cxx:157
const Amg::Transform3D & transform() const
Returns HepGeom::Transform3D by reference.
Identifier associatedDetectorElementIdentifier() const
return Identifier of the associated Detector Element
virtual constexpr SurfaceType type() const =0
Returns the Surface type to avoid dynamic casts.
virtual const Surface & surface() const =0
Return surface associated with this detector element.
xAOD::ParticleHypothesis convert(Acts::ParticleHypothesis h)
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
DetectorType
Simple enum to Identify the Type of the ACTS sub detector.
@ Mm
Maybe not needed in the migration.
@ Tgc
Resitive Plate Chambers.
@ sTgc
Micromegas (NSW).
@ Rpc
Monitored Drift Tubes.
@ Csc
Thin gap champers.
@ Mdt
MuonSpectrometer.
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 3, 1 > Vector3D
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
SurfaceType
This enumerator simplifies the persistency & calculations,.
@ locY
local cartesian
Definition ParamDefs.h:38
@ locX
Definition ParamDefs.h:37
@ phi0
Definition ParamDefs.h:65
@ theta
Definition ParamDefs.h:66
ParticleHypothesis
Enumeration for Particle hypothesis respecting the interaction with material.
ParametersBase< TrackParametersDim, Charged > TrackParameters
#define THROW_EXCEPTION(MESSAGE)
Definition throwExcept.h:10