Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:10:18

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2017-2019 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 // This file is part of the Acts project.
0010 //
0011 // Copyright (C) 2017-2019 CERN for the benefit of the Acts project
0012 //
0013 // This Source Code Form is subject to the terms of the Mozilla Public
0014 // License, v. 2.0. If a copy of the MPL was not distributed with this
0015 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0016 
0017 #include "Acts/Plugins/Json/JsonMaterialDecorator.hpp"
0018 
0019 namespace Acts {
0020 
0021 JsonMaterialDecorator::JsonMaterialDecorator(
0022     const MaterialMapJsonConverter::Config& rConfig,
0023     const std::string& jFileName, Acts::Logging::Level level,
0024     bool clearSurfaceMaterial, bool clearVolumeMaterial)
0025     : m_readerConfig(rConfig),
0026       m_clearSurfaceMaterial(clearSurfaceMaterial),
0027       m_clearVolumeMaterial(clearVolumeMaterial),
0028       m_logger{getDefaultLogger("JsonMaterialDecorator", level)} {
0029   // the material reader
0030   Acts::MaterialMapJsonConverter jmConverter(rConfig, level);
0031 
0032   ACTS_VERBOSE("Reading JSON material description from: " << jFileName);
0033   std::ifstream ifj(jFileName.c_str());
0034   if (!ifj.good()) {
0035     throw std::runtime_error{"Unable to open input JSON material file: " +
0036                              jFileName};
0037   }
0038   nlohmann::json jin;
0039 
0040   if (jFileName.find(".cbor") != std::string::npos) {
0041     std::vector<std::uint8_t> iCbor((std::istreambuf_iterator<char>(ifj)),
0042                                     std::istreambuf_iterator<char>());
0043     jin = nlohmann::json::from_cbor(iCbor);
0044   } else {
0045     ifj >> jin;
0046   }
0047 
0048   auto maps = jmConverter.jsonToMaterialMaps(jin);
0049   m_surfaceMaterialMap = maps.first;
0050   m_volumeMaterialMap = maps.second;
0051   ACTS_VERBOSE("JSON material description read complete");
0052 }
0053 
0054 void JsonMaterialDecorator::decorate(Surface& surface) const {
0055   ACTS_VERBOSE("Processing surface: " << surface.geometryId());
0056   // Clear the material if registered to do so
0057   if (m_clearSurfaceMaterial) {
0058     ACTS_VERBOSE("-> Clearing surface material");
0059     surface.assignSurfaceMaterial(nullptr);
0060   }
0061   // Try to find the surface in the map
0062   auto sMaterial = m_surfaceMaterialMap.find(surface.geometryId());
0063   if (sMaterial != m_surfaceMaterialMap.end()) {
0064     ACTS_VERBOSE("-> Found material for surface, assigning");
0065     surface.assignSurfaceMaterial(sMaterial->second);
0066   }
0067 }
0068 
0069 /// Decorate a TrackingVolume
0070 ///
0071 /// @param volume the non-cost volume that is decorated
0072 void JsonMaterialDecorator::decorate(TrackingVolume& volume) const {
0073   ACTS_VERBOSE("Processing volume: " << volume.geometryId());
0074   // Clear the material if registered to do so
0075   if (m_clearVolumeMaterial) {
0076     ACTS_VERBOSE("-> Clearing volume material");
0077     volume.assignVolumeMaterial(nullptr);
0078   }
0079   // Try to find the volume in the map
0080   auto vMaterial = m_volumeMaterialMap.find(volume.geometryId());
0081   if (vMaterial != m_volumeMaterialMap.end()) {
0082     ACTS_VERBOSE("-> Found material for volume, assigning");
0083     volume.assignVolumeMaterial(vMaterial->second);
0084   }
0085 }
0086 }  // namespace Acts