Skip to content

Commit

Permalink
refactor: Rework detector handling in Examples (#3498)
Browse files Browse the repository at this point in the history
- Concrete detector interface for examples
- Align interfaces of different detectors
- Remove `DD4hepGeometryService`
- Improve DDG4 construction
  - `unique_ptr` for DD4hep detector with dedicated name
  - Concrete `Geant4Mapping` instance for conversion
- Simplify CMake for optional Geant4 construction for different detectors
- Move Geant4 construction code to detector folder
  • Loading branch information
andiwand authored Dec 7, 2024
1 parent 9067679 commit 341d2b0
Show file tree
Hide file tree
Showing 121 changed files with 1,584 additions and 1,857 deletions.
4 changes: 3 additions & 1 deletion CI/physmon/physmon_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def makeSetup() -> PhysmonSetup:
level=acts.logging.INFO,
)

detector, trackingGeometry, decorators = getOpenDataDetector(matDeco)
detector = getOpenDataDetector(matDeco)
trackingGeometry = detector.trackingGeometry()
decorators = detector.contextDecorators()
setup = PhysmonSetup(
detector=detector,
trackingGeometry=trackingGeometry,
Expand Down
4 changes: 1 addition & 3 deletions Core/include/Acts/Utilities/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#pragma once

// STL include(s)
#include <cassert>
#include <ctime>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <memory>
Expand Down
38 changes: 1 addition & 37 deletions Examples/Algorithms/Geant4/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
add_library(
ActsExamplesGeant4
SHARED
src/GdmlDetectorConstruction.cpp
src/TelescopeG4DetectorConstruction.cpp
src/Geant4Simulation.cpp
src/MagneticFieldWrapper.cpp
src/MaterialPhysicsList.cpp
Expand Down Expand Up @@ -32,43 +30,9 @@ target_link_libraries(
PUBLIC
ActsCore
ActsExamplesFramework
ActsExamplesDetectorTelescope
ActsExamplesDetectorsCommon
Boost::headers
${Geant4_LIBRARIES}
)

if(ACTS_BUILD_EXAMPLES_DD4HEP)
if(${DD4hep_VERSION} VERSION_LESS 1.11)
target_include_directories(
ActsExamplesGeant4
PRIVATE ${DD4hep_INCLUDE_DIRS}
)
target_link_libraries(
ActsExamplesGeant4
PRIVATE ${DD4hep_DDCORE_LIBRARY} ${DD4hep_DDG4_LIBRARY}
)
else()
target_link_libraries(
ActsExamplesGeant4
PUBLIC ActsExamplesDetectorDD4hep DD4hep::DDCore DD4hep::DDG4
)
endif()

target_sources(ActsExamplesGeant4 PUBLIC src/DDG4DetectorConstruction.cpp)
endif()

if(ACTS_BUILD_PLUGIN_GEOMODEL)
target_sources(
ActsExamplesGeant4
PUBLIC src/GeoModelDetectorConstruction.cpp
)

find_library(GeoModel2G4_LIBRARY GeoModel2G4 REQUIRED)

target_link_libraries(
ActsExamplesGeant4
PUBLIC ActsPluginGeoModel ${GeoModel2G4_LIBRARY}
)
endif()

install(TARGETS ActsExamplesGeant4 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
#pragma once

#include <memory>

#include <G4VUserDetectorConstruction.hh>
#include <vector>

namespace ActsExamples::Geant4 {
class RegionCreator;
} // namespace ActsExamples::Geant4

/// Silly Geant4 will destroy the detector construction after the run manager is
/// destructed. This class works around it by factorizing a factory.
class DetectorConstructionFactory {
public:
virtual ~DetectorConstructionFactory() = default;
namespace ActsExamples {

virtual std::unique_ptr<G4VUserDetectorConstruction> factorize() const = 0;
struct Geant4ConstructionOptions {
std::vector<std::shared_ptr<Geant4::RegionCreator>> regionCreators;
};

} // namespace ActsExamples::Geant4
} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

#include "Acts/Material/MaterialInteraction.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/DetectorCommons/Detector.hpp"
#include "ActsExamples/EventData/PropagationSummary.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/Framework/ProcessCode.hpp"
#include "ActsExamples/Framework/RandomNumbers.hpp"
#include "ActsExamples/Geant4/Geant4ConstructionOptions.hpp"
#include "ActsExamples/Geant4/SensitiveSurfaceMapper.hpp"

#include <cstddef>
Expand Down Expand Up @@ -44,10 +46,8 @@ namespace ActsExamples {
struct Geant4Handle;

namespace Geant4 {
class DetectorConstructionFactory;
class SensitiveSurfaceMapper;
struct EventStore;
class RegionCreator;
} // namespace Geant4

/// Abstracts common Geant4 Acts algorithm behaviour.
Expand All @@ -61,10 +61,11 @@ class Geant4SimulationBase : public IAlgorithm {
/// Random number service.
std::shared_ptr<const RandomNumbers> randomNumbers;

/// Detector construction object.
/// G4RunManager will take care of deletion
std::shared_ptr<Geant4::DetectorConstructionFactory>
detectorConstructionFactory;
/// Geant4 construction options.
Geant4ConstructionOptions constructionOptions;

/// Detector instance to access Geant4 geometry construction.
std::shared_ptr<Detector> detector;

/// Optional Geant4 instance overwrite.
std::shared_ptr<Geant4Handle> geant4Handle;
Expand All @@ -81,8 +82,7 @@ class Geant4SimulationBase : public IAlgorithm {
/// Algorithm execute method, called once per event with context
///
/// @param ctx the AlgorithmContext for this event
ActsExamples::ProcessCode execute(
const ActsExamples::AlgorithmContext& ctx) const override;
ProcessCode execute(const ActsExamples::AlgorithmContext& ctx) const override;

/// Readonly access to the configuration
virtual const Config& config() const = 0;
Expand Down Expand Up @@ -166,8 +166,7 @@ class Geant4Simulation final : public Geant4SimulationBase {
/// Algorithm execute method, called once per event with context
///
/// @param ctx the AlgorithmContext for this event
ActsExamples::ProcessCode execute(
const ActsExamples::AlgorithmContext& ctx) const final;
ProcessCode execute(const ActsExamples::AlgorithmContext& ctx) const final;

/// Readonly access to the configuration
const Config& config() const final { return m_cfg; }
Expand Down Expand Up @@ -209,8 +208,7 @@ class Geant4MaterialRecording final : public Geant4SimulationBase {
/// Algorithm execute method, called once per event with context
///
/// @param ctx the AlgorithmContext for this event
ActsExamples::ProcessCode execute(
const ActsExamples::AlgorithmContext& ctx) const final;
ProcessCode execute(const ActsExamples::AlgorithmContext& ctx) const final;

/// Readonly access to the configuration
const Config& config() const final { return m_cfg; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <string>
#include <vector>

class G4Region;

namespace ActsExamples::Geant4 {

/// Geant4 Region Creator
Expand All @@ -24,6 +26,9 @@ class RegionCreator {
public:
/// Nested configuration struct for the Geant4 region creator
struct Config {
/// Region name
std::string name;

/// Process cut to be applied for gammas, in mm
double gammaCut{};

Expand All @@ -37,35 +42,25 @@ class RegionCreator {
double protonCut{};

/// Volume list to be included in this region
std::vector<std::string> volumes{};
std::vector<std::string> volumes;
};

/// Region creator constructor
///
/// @param cfg is the configuration struct
/// @param name is the region name
/// @param level is the logging level to be used
RegionCreator(const Config& cfg, std::string name,
Acts::Logging::Level level);
explicit RegionCreator(const Config& cfg);

/// Construct the region
void construct();
/// @note The lifetime of the returned region is managed by Geant4
G4Region* buildRegion(
const Acts::Logger& logger = Acts::getDummyLogger()) const;

/// Readonly access to the configuration
const Config& config() const { return m_cfg; }

private:
/// Region name
std::string m_name;

/// Config instance
Config m_cfg;

/// Private access method to the logging instance
const Acts::Logger& logger() const { return *m_logger; }

/// The looging instance
std::unique_ptr<const Acts::Logger> m_logger;
};

} // namespace ActsExamples::Geant4

This file was deleted.

Loading

0 comments on commit 341d2b0

Please sign in to comment.