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/BinnedSurfaceMaterialAccumulator.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
28 ATH_CHECK(m_materialDumper.retrieve());
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::BinnedSurfaceMaterialAccumulator::Config accumulatorConfig;
51 accumulatorConfig.materialSurfaces = std::move(materialSurfaces);
52 auto materialAccumulator = std::make_shared<Acts::BinnedSurfaceMaterialAccumulator>(accumulatorConfig,
53 makeActsAthenaLogger(this, "MaterialAccumulator"));
54
56 Acts::MaterialMapper::Config mapperConfig;
57 mapperConfig.assignmentFinder = materialAssigner;
58 mapperConfig.surfaceMaterialAccumulator = materialAccumulator;
59 m_materialMapper = std::make_shared<Acts::MaterialMapper> (mapperConfig,
60 makeActsAthenaLogger(this, "MaterialMapper"));
61
62 // Create the state object
63 const ActsTrk::GeometryContext& geoContext{m_trackingGeometrySvc->getNominalContext()};
64 m_mappingState = m_materialMapper->createState(geoContext.context());
65
66 return StatusCode::SUCCESS;
67}
68
70{
71 const ActsTrk::GeometryContext& geoContext{m_trackingGeometrySvc->getNominalContext()};
72 Acts::TrackingGeometryMaterial detectorMaterial = m_materialMapper->finalizeMaps(*m_mappingState, geoContext.context());
73
74 // Dump the maps through the material writers
75 m_materialDumper->dumpMaterial(geoContext, detectorMaterial);
76
77 return StatusCode::SUCCESS;
78}
79
80StatusCode
81ActsTrk::MaterialMapping::execute (const EventContext& ctx) const
82{
83 // Read the collection to the EventStore
85 // Check if all is fine
86 if (!materialTracks.isValid()) {
87 ATH_MSG_ERROR("Failed to read " << m_materialTrackCollectionKey.key());
88 return StatusCode::FAILURE;
89 }
90
91 // Write to the collection to the EventStore
94
95 // Record the collection once per event if not already there
96 // You do it once for the unmapped material tracks ...
97 if (!unmappedMaterialTracks.isPresent()) {
98 auto coll = std::make_unique<ActsTrk::RecordedMaterialTrackCollection>();
99 ATH_CHECK(unmappedMaterialTracks.record(std::move(coll)));
100 }
101 auto* unmappedColl = unmappedMaterialTracks.ptr();
102 if (!unmappedColl) {
103 ATH_MSG_ERROR("RecordedMaterialTrackCollection ptr() is null for key "
105 return StatusCode::FAILURE;
106 }
107 // ... and then for the mapped material tracks
108 if (!mappedMaterialTracks.isPresent()) {
109 auto coll = std::make_unique<ActsTrk::RecordedMaterialTrackCollection>();
110 ATH_CHECK(mappedMaterialTracks.record(std::move(coll)));
111 }
112 auto* mappedColl = mappedMaterialTracks.ptr();
113 if (!mappedColl) {
114 ATH_MSG_ERROR("RecordedMaterialTrackCollection ptr() is null for key "
116 return StatusCode::FAILURE;
117 }
118
119 auto mappingState = const_cast<Acts::MaterialMapper::State*>(m_mappingState.get());
120
121 const ActsTrk::GeometryContext& geoContext{m_trackingGeometrySvc->getNominalContext()};
122 Acts::MagneticFieldContext magFieldContext;
123
124 // Loop over the material tracks and map them on the surfaces
125 for (const auto& materialTrack : *materialTracks) {
126 auto [mapped, unmapped] = m_materialMapper->mapMaterial(
127 *mappingState, geoContext.context(), magFieldContext, materialTrack);
128 // collect mapped and unmapped states
129 mappedColl->push_back(mapped);
130 unmappedColl->push_back(unmapped);
131 }
132
133 return StatusCode::SUCCESS;
134
135}
136
137
#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)
Definition Logger.cxx:64
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.
ToolHandle< IMaterialDumperTool > m_materialDumper
The material dumper the produced maps are handed to.
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:56