Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:15:17

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 "Acts/Plugins/Json/JsonMaterialDecorator.hpp"
0010 
0011 namespace Acts {
0012 
0013 JsonMaterialDecorator::JsonMaterialDecorator(
0014     const MaterialMapJsonConverter::Config& rConfig,
0015     const std::string& jFileName, Acts::Logging::Level level,
0016     bool clearSurfaceMaterial, bool clearVolumeMaterial)
0017     : m_readerConfig(rConfig),
0018       m_clearSurfaceMaterial(clearSurfaceMaterial),
0019       m_clearVolumeMaterial(clearVolumeMaterial),
0020       m_logger{getDefaultLogger("JsonMaterialDecorator", level)} {
0021   // the material reader
0022   Acts::MaterialMapJsonConverter jmConverter(rConfig, level);
0023 
0024   ACTS_VERBOSE("Reading JSON material description from: " << jFileName);
0025   std::ifstream ifj(jFileName.c_str());
0026   if (!ifj.good()) {
0027     throw std::runtime_error{"Unable to open input JSON material file: " +
0028                              jFileName};
0029   }
0030   nlohmann::json jin;
0031 
0032   if (jFileName.find(".cbor") != std::string::npos) {
0033     std::vector<std::uint8_t> iCbor((std::istreambuf_iterator<char>(ifj)),
0034                                     std::istreambuf_iterator<char>());
0035     jin = nlohmann::json::from_cbor(iCbor);
0036   } else {
0037     ifj >> jin;
0038   }
0039 
0040   auto maps = jmConverter.jsonToMaterialMaps(jin);
0041   m_surfaceMaterialMap = maps.first;
0042   m_volumeMaterialMap = maps.second;
0043   ACTS_VERBOSE("JSON material description read complete");
0044 }
0045 
0046 void JsonMaterialDecorator::decorate(Surface& surface) const {
0047   ACTS_VERBOSE("Processing surface: " << surface.geometryId());
0048   // Clear the material if registered to do so
0049   if (m_clearSurfaceMaterial) {
0050     ACTS_VERBOSE("-> Clearing surface material");
0051     surface.assignSurfaceMaterial(nullptr);
0052   }
0053   // Try to find the surface in the map
0054   auto sMaterial = m_surfaceMaterialMap.find(surface.geometryId());
0055   if (sMaterial != m_surfaceMaterialMap.end()) {
0056     ACTS_VERBOSE("-> Found material for surface, assigning");
0057     surface.assignSurfaceMaterial(sMaterial->second);
0058   }
0059 }
0060 
0061 /// Decorate a TrackingVolume
0062 ///
0063 /// @param volume the non-cost volume that is decorated
0064 void JsonMaterialDecorator::decorate(TrackingVolume& volume) const {
0065   ACTS_VERBOSE("Processing volume: " << volume.geometryId());
0066   // Clear the material if registered to do so
0067   if (m_clearVolumeMaterial) {
0068     ACTS_VERBOSE("-> Clearing volume material");
0069     volume.assignVolumeMaterial(nullptr);
0070   }
0071   // Try to find the volume in the map
0072   auto vMaterial = m_volumeMaterialMap.find(volume.geometryId());
0073   if (vMaterial != m_volumeMaterialMap.end()) {
0074     ACTS_VERBOSE("-> Found material for volume, assigning");
0075     volume.assignVolumeMaterial(vMaterial->second);
0076   }
0077 }
0078 }  // namespace Acts