Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2017-2024 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/DDG4/DDG4DetectorConstruction.hpp"
0010 
0011 #include "ActsExamples/DD4hepDetector/DD4hepDetector.hpp"
0012 
0013 #include <memory>
0014 #include <vector>
0015 
0016 #include <DD4hep/DetElement.h>
0017 #include <DD4hep/Detector.h>
0018 #include <DDG4/Geant4Converter.h>
0019 #include <DDG4/Geant4GeometryInfo.h>
0020 #include <DDG4/Geant4Mapping.h>
0021 #include <Parsers/Printout.h>
0022 
0023 class G4VPhysicalVolume;
0024 
0025 ActsExamples::DDG4DetectorConstruction::DDG4DetectorConstruction(
0026     std::shared_ptr<DD4hep::DD4hepDetector> detector,
0027     std::vector<std::shared_ptr<RegionCreator>> regionCreators)
0028     : G4VUserDetectorConstruction(),
0029       m_detector(std::move(detector)),
0030       m_regionCreators(std::move(regionCreators)) {}
0031 
0032 ActsExamples::DDG4DetectorConstruction::~DDG4DetectorConstruction() = default;
0033 
0034 dd4hep::Detector& ActsExamples::DDG4DetectorConstruction::dd4hepDetector()
0035     const {
0036   return m_detector->geometryService->detector();
0037 }
0038 
0039 // See DD4hep::Simulation::Geant4DetectorConstruction::Construct()
0040 G4VPhysicalVolume* ActsExamples::DDG4DetectorConstruction::Construct() {
0041   if (m_world == nullptr) {
0042     dd4hep::sim::Geant4Mapping& g4map = dd4hep::sim::Geant4Mapping::instance();
0043     auto conv = dd4hep::sim::Geant4Converter(dd4hepDetector(),
0044                                              dd4hep::PrintLevel::VERBOSE);
0045     dd4hep::sim::Geant4GeometryInfo* geo_info =
0046         conv.create(dd4hepDetector().world()).detach();
0047     g4map.attach(geo_info);
0048     // All volumes are deleted in ~G4PhysicalVolumeStore()
0049     m_world = geo_info->world();
0050     // Create Geant4 volume manager
0051     g4map.volumeManager();
0052 
0053     // Create regions
0054     for (const auto& regionCreator : m_regionCreators) {
0055       regionCreator->Construct();
0056     }
0057   }
0058   return m_world;
0059 }
0060 
0061 ActsExamples::DDG4DetectorConstructionFactory::DDG4DetectorConstructionFactory(
0062     std::shared_ptr<DD4hep::DD4hepDetector> detector,
0063     std::vector<std::shared_ptr<RegionCreator>> regionCreators)
0064     : m_detector(std::move(detector)),
0065       m_regionCreators(std::move(regionCreators)) {}
0066 
0067 ActsExamples::DDG4DetectorConstructionFactory::
0068     ~DDG4DetectorConstructionFactory() = default;
0069 
0070 std::unique_ptr<G4VUserDetectorConstruction>
0071 ActsExamples::DDG4DetectorConstructionFactory::factorize() const {
0072   return std::make_unique<DDG4DetectorConstruction>(m_detector,
0073                                                     m_regionCreators);
0074 }