Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:09:47

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2017-2020 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/MaterialMapping/MaterialMapping.hpp"
0010 
0011 #include "Acts/Material/AccumulatedMaterialSlab.hpp"
0012 #include "Acts/Material/AccumulatedSurfaceMaterial.hpp"
0013 #include "ActsExamples/MaterialMapping/IMaterialWriter.hpp"
0014 
0015 #include <iostream>
0016 #include <stdexcept>
0017 #include <unordered_map>
0018 
0019 namespace ActsExamples {
0020 
0021 MaterialMapping::MaterialMapping(const MaterialMapping::Config& cfg,
0022                                  Acts::Logging::Level level)
0023     : IAlgorithm("MaterialMapping", level),
0024       m_cfg(cfg),
0025       m_mappingState(cfg.geoContext, cfg.magFieldContext),
0026       m_mappingStateVol(cfg.geoContext, cfg.magFieldContext) {
0027   if (!m_cfg.materialSurfaceMapper && !m_cfg.materialVolumeMapper) {
0028     throw std::invalid_argument("Missing material mapper");
0029   } else if (!m_cfg.trackingGeometry) {
0030     throw std::invalid_argument("Missing tracking geometry");
0031   }
0032 
0033   m_inputMaterialTracks.initialize(m_cfg.inputMaterialTracks);
0034   m_outputMaterialTracks.initialize(m_cfg.mappingMaterialCollection);
0035 
0036   ACTS_INFO("This algorithm requires inter-event information, "
0037             << "run in single-threaded mode!");
0038 
0039   if (m_cfg.materialSurfaceMapper) {
0040     // Generate and retrieve the central cache object
0041     m_mappingState = m_cfg.materialSurfaceMapper->createState(
0042         m_cfg.geoContext, m_cfg.magFieldContext, *m_cfg.trackingGeometry);
0043   }
0044   if (m_cfg.materialVolumeMapper) {
0045     // Generate and retrieve the central cache object
0046     m_mappingStateVol = m_cfg.materialVolumeMapper->createState(
0047         m_cfg.geoContext, m_cfg.magFieldContext, *m_cfg.trackingGeometry);
0048   }
0049 }
0050 
0051 MaterialMapping::~MaterialMapping() {
0052   Acts::DetectorMaterialMaps detectorMaterial;
0053 
0054   if (m_cfg.materialSurfaceMapper && m_cfg.materialVolumeMapper) {
0055     // Finalize all the maps using the cached state
0056     m_cfg.materialSurfaceMapper->finalizeMaps(m_mappingState);
0057     m_cfg.materialVolumeMapper->finalizeMaps(m_mappingStateVol);
0058     // Loop over the state, and collect the maps for surfaces
0059     for (auto& [key, value] : m_mappingState.surfaceMaterial) {
0060       detectorMaterial.first.insert({key, std::move(value)});
0061     }
0062     // Loop over the state, and collect the maps for volumes
0063     for (auto& [key, value] : m_mappingStateVol.volumeMaterial) {
0064       detectorMaterial.second.insert({key, std::move(value)});
0065     }
0066   } else {
0067     if (m_cfg.materialSurfaceMapper) {
0068       // Finalize all the maps using the cached state
0069       m_cfg.materialSurfaceMapper->finalizeMaps(m_mappingState);
0070       // Loop over the state, and collect the maps for surfaces
0071       for (auto& [key, value] : m_mappingState.surfaceMaterial) {
0072         detectorMaterial.first.insert({key, std::move(value)});
0073       }
0074       // Loop over the state, and collect the maps for volumes
0075       for (auto& [key, value] : m_mappingState.volumeMaterial) {
0076         detectorMaterial.second.insert({key, std::move(value)});
0077       }
0078     }
0079     if (m_cfg.materialVolumeMapper) {
0080       // Finalize all the maps using the cached state
0081       m_cfg.materialVolumeMapper->finalizeMaps(m_mappingStateVol);
0082       // Loop over the state, and collect the maps for surfaces
0083       for (auto& [key, value] : m_mappingStateVol.surfaceMaterial) {
0084         detectorMaterial.first.insert({key, std::move(value)});
0085       }
0086       // Loop over the state, and collect the maps for volumes
0087       for (auto& [key, value] : m_mappingStateVol.volumeMaterial) {
0088         detectorMaterial.second.insert({key, std::move(value)});
0089       }
0090     }
0091   }
0092   // Loop over the available writers and write the maps
0093   for (auto& imw : m_cfg.materialWriters) {
0094     imw->writeMaterial(detectorMaterial);
0095   }
0096 }
0097 
0098 ProcessCode MaterialMapping::execute(const AlgorithmContext& context) const {
0099   // Take the collection from the EventStore
0100   std::unordered_map<std::size_t, Acts::RecordedMaterialTrack>
0101       mtrackCollection = m_inputMaterialTracks(context);
0102 
0103   if (m_cfg.materialSurfaceMapper) {
0104     // To make it work with the framework needs a lock guard
0105     auto mappingState =
0106         const_cast<Acts::SurfaceMaterialMapper::State*>(&m_mappingState);
0107     for (auto& [idTrack, mTrack] : mtrackCollection) {
0108       // Map this one onto the geometry
0109       m_cfg.materialSurfaceMapper->mapMaterialTrack(*mappingState, mTrack);
0110     }
0111   }
0112   if (m_cfg.materialVolumeMapper) {
0113     // To make it work with the framework needs a lock guard
0114     auto mappingState =
0115         const_cast<Acts::VolumeMaterialMapper::State*>(&m_mappingStateVol);
0116 
0117     for (auto& [idTrack, mTrack] : mtrackCollection) {
0118       // Map this one onto the geometry
0119       m_cfg.materialVolumeMapper->mapMaterialTrack(*mappingState, mTrack);
0120     }
0121   }
0122   // Write take the collection to the EventStore
0123   m_outputMaterialTracks(context, std::move(mtrackCollection));
0124   return ProcessCode::SUCCESS;
0125 }
0126 
0127 std::vector<std::pair<double, int>> MaterialMapping::scoringParameters(
0128     uint64_t surfaceID) {
0129   std::vector<std::pair<double, int>> scoringParameters;
0130 
0131   if (m_cfg.materialSurfaceMapper) {
0132     auto surfaceAccumulatedMaterial = m_mappingState.accumulatedMaterial.find(
0133         Acts::GeometryIdentifier(surfaceID));
0134 
0135     if (surfaceAccumulatedMaterial !=
0136         m_mappingState.accumulatedMaterial.end()) {
0137       auto matrixMaterial =
0138           surfaceAccumulatedMaterial->second.accumulatedMaterial();
0139       for (const auto& vectorMaterial : matrixMaterial) {
0140         for (const auto& AccumulatedMaterial : vectorMaterial) {
0141           auto totalVariance = AccumulatedMaterial.totalVariance();
0142           scoringParameters.push_back(
0143               {totalVariance.first, totalVariance.second});
0144         }
0145       }
0146     }
0147   }
0148   return scoringParameters;
0149 }
0150 
0151 }  // namespace ActsExamples