ATLAS Offline Software
Loading...
Searching...
No Matches
Acts/ActsMaterial/src/MaterialMapping.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "MaterialMapping.h"
6#include "GaudiKernel/IInterface.h"
8#include "Acts/Geometry/TrackingGeometry.hpp"
9#include "Acts/Material/IntersectionMaterialAssigner.hpp"
10#include "Acts/Material/BinnedSurfaceMaterialAccumulater.hpp"
11#include "Acts/Material/TrackingGeometryMaterial.hpp"
12
13ActsTrk::MaterialMapping::MaterialMapping(const std::string& name, ISvcLocator* pSvcLocator) :
14 AthReentrantAlgorithm(name, pSvcLocator)
15{}
16
19
21{
25
27
29
30 // Retrieve the material surfaces
31 std::vector<const Acts::Surface*> materialSurfaces = {};
32
33 auto surfaceSelector = [&](const Acts::Surface* surface) {
34 if (surface->surfaceMaterial() != nullptr &&
35 std::ranges::find(materialSurfaces, surface) == materialSurfaces.end()) {
36 materialSurfaces.push_back(surface);
37 }
38 };
39
40 // Visit all surfaces (second argument = if true only sensitive surfaces are visited)
41 m_trackingGeometrySvc->trackingGeometry()->visitSurfaces(surfaceSelector, false);
42
43 // The material intersection assigner
44 Acts::IntersectionMaterialAssigner::Config assingerConfig;
45 assingerConfig.surfaces = materialSurfaces;
46 auto materialAssigner = std::make_shared<Acts::IntersectionMaterialAssigner>(assingerConfig,
47 makeActsAthenaLogger(this, "MaterialAssigner"));
48
49 // The binned surface material accumulator
50 Acts::BinnedSurfaceMaterialAccumulater::Config accumulaterConfig;
51 accumulaterConfig.materialSurfaces = materialSurfaces;
52 auto materialAccumulater = std::make_shared<Acts::BinnedSurfaceMaterialAccumulater>(accumulaterConfig,
53 makeActsAthenaLogger(this, "MaterialAccumulater"));
54
56 Acts::MaterialMapper::Config mapperConfig;
57 mapperConfig.assignmentFinder = materialAssigner;
58 mapperConfig.surfaceMaterialAccumulater = materialAccumulater;
59 m_materialMapper = std::make_shared<Acts::MaterialMapper> (mapperConfig,
60 makeActsAthenaLogger(this, "MaterialMapper"));
61
62 // Create the state object
63 m_mappingState = m_materialMapper->createState();
64
65 return StatusCode::SUCCESS;
66}
67
69{
70 Acts::TrackingGeometryMaterial detectorMaterial = m_materialMapper->finalizeMaps(*m_mappingState);
71
72 const ActsTrk::GeometryContext& geoContext{m_trackingGeometrySvc->getNominalContext()};
73
74 // Loop over the available writers and write the maps
75 for (auto& materialWriter : m_materialMapWriters) {
76 materialWriter->writeMaterial(geoContext, detectorMaterial);
77 }
78
79 return StatusCode::SUCCESS;
80}
81
82StatusCode
83ActsTrk::MaterialMapping::execute (const EventContext& ctx) const
84{
85 // Read the collection to the EventStore
87 // Check if all is fine
88 if (!materialTracks.isValid()) {
89 ATH_MSG_ERROR("Failed to read " << m_materialTrackCollectionKey.key());
90 return StatusCode::FAILURE;
91 }
92
93 // Write to the collection to the EventStore
96
97 // Record the collection once per event if not already there
98 // You do it once for the unmapped material tracks ...
99 if (!unmappedMaterialTracks.isPresent()) {
100 auto coll = std::make_unique<ActsTrk::RecordedMaterialTrackCollection>();
101 ATH_CHECK(unmappedMaterialTracks.record(std::move(coll)));
102 }
103 auto* unmappedColl = unmappedMaterialTracks.ptr();
104 if (!unmappedColl) {
105 ATH_MSG_ERROR("RecordedMaterialTrackCollection ptr() is null for key "
107 return StatusCode::FAILURE;
108 }
109 // ... and then for the mapped material tracks
110 if (!mappedMaterialTracks.isPresent()) {
111 auto coll = std::make_unique<ActsTrk::RecordedMaterialTrackCollection>();
112 ATH_CHECK(mappedMaterialTracks.record(std::move(coll)));
113 }
114 auto* mappedColl = mappedMaterialTracks.ptr();
115 if (!mappedColl) {
116 ATH_MSG_ERROR("RecordedMaterialTrackCollection ptr() is null for key "
118 return StatusCode::FAILURE;
119 }
120
121 auto mappingState = const_cast<Acts::MaterialMapper::State*>(m_mappingState.get());
122
123 const ActsTrk::GeometryContext& geoContext{m_trackingGeometrySvc->getNominalContext()};
124 Acts::MagneticFieldContext magFieldContext;
125
126 // Loop over the material tracks and map them on the surfaces
127 for (const auto& materialTrack : *materialTracks) {
128 auto [mapped, unmapped] = m_materialMapper->mapMaterial(
129 *mappingState, geoContext.context(), magFieldContext, materialTrack);
130 // collect mapped and unmapped states
131 mappedColl->push_back(mapped);
132 unmappedColl->push_back(unmapped);
133 }
134
135 return StatusCode::SUCCESS;
136
137}
138
139
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
std::unique_ptr< const Acts::Logger > makeActsAthenaLogger(IMessageSvc *svc, const std::string &name, int level, std::optional< std::string > parent_name)
Acts::GeometryContext context() const
SG::WriteHandleKey< RecordedMaterialTrackCollection > m_mappedMaterialTrackCollectionKey
The mapped and unmapped material tracks.
virtual StatusCode execute(const EventContext &ctx) const override
SG::WriteHandleKey< RecordedMaterialTrackCollection > m_unmappedMaterialTrackCollectionKey
SG::ReadHandleKey< RecordedMaterialTrackCollection > m_materialTrackCollectionKey
The RecordedMaterialTrackCollection to read.
std::shared_ptr< Acts::MaterialMapper > m_materialMapper
The material mapper from the ACTS core components.
ServiceHandle< ActsTrk::ITrackingGeometrySvc > m_trackingGeometrySvc
The tracking geometry service to retrive the geometry context and material surfaces.
ToolHandleArray< IMaterialWriterTool > m_materialMapWriters
The material map writes.
MaterialMapping(const std::string &name, ISvcLocator *pSvcLocator)
An algorithm that can be simultaneously executed in multiple threads.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
bool isPresent() const
Is the referenced object present in SG?
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
pointer_type ptr()
Dereference the pointer.
std::vector< std::string > mapped
Definition hcg.cxx:54