Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:38

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 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 https://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 <stdexcept>
0016 #include <unordered_map>
0017 
0018 namespace ActsExamples {
0019 
0020 MaterialMapping::MaterialMapping(const MaterialMapping::Config& cfg,
0021                                  Acts::Logging::Level level)
0022     : IAlgorithm("MaterialMapping", level),
0023       m_cfg(cfg),
0024       m_mappingState(cfg.geoContext, cfg.magFieldContext),
0025       m_mappingStateVol(cfg.geoContext, cfg.magFieldContext) {
0026   if (!m_cfg.materialSurfaceMapper && !m_cfg.materialVolumeMapper) {
0027     throw std::invalid_argument("Missing material mapper");
0028   } else if (!m_cfg.trackingGeometry) {
0029     throw std::invalid_argument("Missing tracking geometry");
0030   }
0031 
0032   m_inputMaterialTracks.initialize(m_cfg.inputMaterialTracks);
0033   m_outputMaterialTracks.initialize(m_cfg.mappingMaterialCollection);
0034 
0035   ACTS_INFO("This algorithm requires inter-event information, "
0036             << "run in single-threaded mode!");
0037 
0038   if (m_cfg.materialSurfaceMapper) {
0039     // Generate and retrieve the central cache object
0040     m_mappingState = m_cfg.materialSurfaceMapper->createState(
0041         m_cfg.geoContext, m_cfg.magFieldContext, *m_cfg.trackingGeometry);
0042   }
0043   if (m_cfg.materialVolumeMapper) {
0044     // Generate and retrieve the central cache object
0045     m_mappingStateVol = m_cfg.materialVolumeMapper->createState(
0046         m_cfg.geoContext, m_cfg.magFieldContext, *m_cfg.trackingGeometry);
0047   }
0048 }
0049 
0050 ProcessCode MaterialMapping::finalize() {
0051   ACTS_INFO("Finalizing material mappig output");
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   return ProcessCode::SUCCESS;
0098 }
0099 
0100 ProcessCode MaterialMapping::execute(const AlgorithmContext& context) const {
0101   // Take the collection from the EventStore
0102   std::unordered_map<std::size_t, Acts::RecordedMaterialTrack>
0103       mtrackCollection = m_inputMaterialTracks(context);
0104 
0105   if (m_cfg.materialSurfaceMapper) {
0106     // To make it work with the framework needs a lock guard
0107     auto mappingState =
0108         const_cast<Acts::SurfaceMaterialMapper::State*>(&m_mappingState);
0109     for (auto& [idTrack, mTrack] : mtrackCollection) {
0110       // Map this one onto the geometry
0111       m_cfg.materialSurfaceMapper->mapMaterialTrack(*mappingState, mTrack);
0112     }
0113   }
0114   if (m_cfg.materialVolumeMapper) {
0115     // To make it work with the framework needs a lock guard
0116     auto mappingState =
0117         const_cast<Acts::VolumeMaterialMapper::State*>(&m_mappingStateVol);
0118 
0119     for (auto& [idTrack, mTrack] : mtrackCollection) {
0120       // Map this one onto the geometry
0121       m_cfg.materialVolumeMapper->mapMaterialTrack(*mappingState, mTrack);
0122     }
0123   }
0124   // Write take the collection to the EventStore
0125   m_outputMaterialTracks(context, std::move(mtrackCollection));
0126   return ProcessCode::SUCCESS;
0127 }
0128 
0129 std::vector<std::pair<double, int>> MaterialMapping::scoringParameters(
0130     std::uint64_t surfaceID) {
0131   std::vector<std::pair<double, int>> scoringParameters;
0132 
0133   if (m_cfg.materialSurfaceMapper) {
0134     auto surfaceAccumulatedMaterial = m_mappingState.accumulatedMaterial.find(
0135         Acts::GeometryIdentifier(surfaceID));
0136 
0137     if (surfaceAccumulatedMaterial !=
0138         m_mappingState.accumulatedMaterial.end()) {
0139       auto matrixMaterial =
0140           surfaceAccumulatedMaterial->second.accumulatedMaterial();
0141       for (const auto& vectorMaterial : matrixMaterial) {
0142         for (const auto& AccumulatedMaterial : vectorMaterial) {
0143           auto totalVariance = AccumulatedMaterial.totalVariance();
0144           scoringParameters.push_back(
0145               {totalVariance.first, totalVariance.second});
0146         }
0147       }
0148     }
0149   }
0150   return scoringParameters;
0151 }
0152 
0153 }  // namespace ActsExamples