Back to home page

EIC code displayed by LXR

 
 

    


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

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/Geant4Detector/Geant4Detector.hpp"
0010 
0011 #include "Acts/Geometry/CylinderVolumeHelper.hpp"
0012 #include "Acts/Geometry/KDTreeTrackingGeometryBuilder.hpp"
0013 #include "Acts/Geometry/LayerArrayCreator.hpp"
0014 #include "Acts/Geometry/LayerCreator.hpp"
0015 #include "Acts/Geometry/SurfaceArrayCreator.hpp"
0016 #include "Acts/Geometry/TrackingGeometry.hpp"
0017 #include "Acts/Geometry/TrackingVolumeArrayCreator.hpp"
0018 #include "Acts/Plugins/Geant4/Geant4DetectorElement.hpp"
0019 #include "Acts/Surfaces/Surface.hpp"
0020 
0021 #include <memory>
0022 #include <ostream>
0023 #include <stdexcept>
0024 
0025 #include "G4Transform3D.hh"
0026 #include "G4VPhysicalVolume.hh"
0027 
0028 namespace ActsExamples {
0029 
0030 Geant4Detector::Geant4Detector(const Config& cfg)
0031     : Detector(Acts::getDefaultLogger("Geant4Detector", cfg.logLevel)),
0032       m_cfg(cfg) {
0033   if (m_cfg.g4World == nullptr) {
0034     throw std::invalid_argument(
0035         "Geant4Detector: no world Geant4 volume provided");
0036   }
0037 
0038   ACTS_INFO("Building an Acts::TrackingGeometry called '"
0039             << m_cfg.name << "' from the Geant4PhysVolume '"
0040             << m_cfg.g4World->GetName() << "'");
0041 
0042   m_nominalGeometryContext = Acts::GeometryContext();
0043 
0044   auto [surfaces, elements] = buildGeant4Volumes(cfg, logger());
0045 
0046   // Surface array creator
0047   auto surfaceArrayCreator = std::make_shared<const Acts::SurfaceArrayCreator>(
0048       Acts::SurfaceArrayCreator::Config(),
0049       logger().clone("SurfaceArrayCreator"));
0050   // Layer Creator
0051   Acts::LayerCreator::Config lcConfig;
0052   lcConfig.surfaceArrayCreator = surfaceArrayCreator;
0053   auto layerCreator = std::make_shared<Acts::LayerCreator>(
0054       lcConfig, logger().clone("LayerCreator"));
0055   // Layer array creator
0056   Acts::LayerArrayCreator::Config lacConfig;
0057   auto layerArrayCreator = std::make_shared<const Acts::LayerArrayCreator>(
0058       lacConfig, logger().clone("LayerArrayCreator"));
0059   // Tracking volume array creator
0060   Acts::TrackingVolumeArrayCreator::Config tvacConfig;
0061   auto tVolumeArrayCreator =
0062       std::make_shared<const Acts::TrackingVolumeArrayCreator>(
0063           tvacConfig, logger().clone("TrackingVolumeArrayCreator"));
0064   // configure the cylinder volume helper
0065   Acts::CylinderVolumeHelper::Config cvhConfig;
0066   cvhConfig.layerArrayCreator = layerArrayCreator;
0067   cvhConfig.trackingVolumeArrayCreator = tVolumeArrayCreator;
0068   auto cylinderVolumeHelper =
0069       std::make_shared<const Acts::CylinderVolumeHelper>(
0070           cvhConfig, logger().clone("CylinderVolumeHelper"));
0071 
0072   // Configure the tracking geometry builder, copy the surfaces in
0073   Acts::KDTreeTrackingGeometryBuilder::Config kdtCfg;
0074   kdtCfg.surfaces = surfaces;
0075   kdtCfg.layerCreator = layerCreator;
0076   kdtCfg.trackingVolumeHelper = cylinderVolumeHelper;
0077   kdtCfg.protoDetector = m_cfg.protoDetector;
0078   kdtCfg.geometryIdentifierHook = m_cfg.geometryIdentifierHook;
0079 
0080   // The KDT tracking geometry builder
0081   auto kdtBuilder = Acts::KDTreeTrackingGeometryBuilder(
0082       kdtCfg, logger().clone("KDTreeTrackingGeometryBuilder"));
0083 
0084   m_trackingGeometry = kdtBuilder.trackingGeometry(m_nominalGeometryContext);
0085 }
0086 
0087 std::tuple<std::vector<std::shared_ptr<Acts::Surface>>,
0088            std::vector<std::shared_ptr<Acts::Geant4DetectorElement>>>
0089 Geant4Detector::buildGeant4Volumes(const Config& cfg,
0090                                    const Acts::Logger& logger) {
0091   // Generate the surface cache
0092   Acts::Geant4DetectorSurfaceFactory::Cache g4SurfaceCache;
0093   G4Transform3D g4ToWorld;
0094 
0095   Acts::Geant4DetectorSurfaceFactory{}.construct(
0096       g4SurfaceCache, g4ToWorld, *cfg.g4World, cfg.g4SurfaceOptions);
0097 
0098   ACTS_INFO("Found " << g4SurfaceCache.matchedG4Volumes
0099                      << " matching  Geant4 Physical volumes.");
0100   ACTS_INFO("Found " << g4SurfaceCache.sensitiveSurfaces.size()
0101                      << " converted sensitive Geant4 Physical volumes.");
0102   ACTS_INFO("Found " << g4SurfaceCache.passiveSurfaces.size()
0103                      << " converted passive Geant4 Physical volumes.");
0104   ACTS_INFO("Found " << g4SurfaceCache.convertedMaterials
0105                      << " converted Geant4 Material slabs.");
0106 
0107   std::vector<std::shared_ptr<Acts::Surface>> surfaces;
0108   std::vector<std::shared_ptr<Acts::Geant4DetectorElement>> elements;
0109 
0110   // Reserve the right amount of surfaces
0111   surfaces.reserve(g4SurfaceCache.sensitiveSurfaces.size() +
0112                    g4SurfaceCache.passiveSurfaces.size());
0113   elements.reserve(g4SurfaceCache.sensitiveSurfaces.size());
0114 
0115   // Add the sensitive surfaces
0116   for (const auto& [e, s] : g4SurfaceCache.sensitiveSurfaces) {
0117     surfaces.push_back(s);
0118     elements.push_back(e);
0119   }
0120   // Add the passive surfaces
0121   surfaces.insert(surfaces.end(), g4SurfaceCache.passiveSurfaces.begin(),
0122                   g4SurfaceCache.passiveSurfaces.end());
0123 
0124   return {std::move(surfaces), std::move(elements)};
0125 }
0126 
0127 }  // namespace ActsExamples