Skip to content

Commit

Permalink
refactor: Rework detector handling in Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Oct 22, 2024
1 parent 7559227 commit 3f1b961
Show file tree
Hide file tree
Showing 40 changed files with 782 additions and 860 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include "ActsExamples/DD4hepDetector/DD4hepDetector.hpp"
#include "ActsExamples/Geant4/DetectorConstructionFactory.hpp"
#include "ActsExamples/Geant4/RegionCreator.hpp"

Expand All @@ -23,7 +24,7 @@ class Detector;
namespace ActsExamples {

namespace DD4hep {
struct DD4hepDetector;
class DD4hepDetector;
}

/// Construct the Geant4 detector from a DD4hep description.
Expand All @@ -32,7 +33,6 @@ class DDG4DetectorConstruction final : public G4VUserDetectorConstruction {
DDG4DetectorConstruction(
std::shared_ptr<DD4hep::DD4hepDetector> detector,
std::vector<std::shared_ptr<RegionCreator>> regionCreators = {});
~DDG4DetectorConstruction() final;

/// Convert the stored DD4hep detector to a Geant4 description.
///
Expand All @@ -50,9 +50,6 @@ class DDG4DetectorConstruction final : public G4VUserDetectorConstruction {
std::vector<std::shared_ptr<RegionCreator>> m_regionCreators;
/// The world volume
G4VPhysicalVolume* m_world = nullptr;

/// The DD4hep detector instance
dd4hep::Detector& dd4hepDetector() const;
};

class DDG4DetectorConstructionFactory final
Expand Down
27 changes: 11 additions & 16 deletions Examples/Algorithms/Geant4/src/DDG4DetectorConstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,26 @@
#include <DDG4/Geant4Converter.h>
#include <DDG4/Geant4GeometryInfo.h>
#include <DDG4/Geant4Mapping.h>
#include <Parsers/Printout.h>

class G4VPhysicalVolume;

ActsExamples::DDG4DetectorConstruction::DDG4DetectorConstruction(
namespace ActsExamples {

DDG4DetectorConstruction::DDG4DetectorConstruction(
std::shared_ptr<DD4hep::DD4hepDetector> detector,
std::vector<std::shared_ptr<RegionCreator>> regionCreators)
: G4VUserDetectorConstruction(),
m_detector(std::move(detector)),
m_regionCreators(std::move(regionCreators)) {}

ActsExamples::DDG4DetectorConstruction::~DDG4DetectorConstruction() = default;

dd4hep::Detector& ActsExamples::DDG4DetectorConstruction::dd4hepDetector()
const {
return m_detector->geometryService->detector();
}

// See DD4hep::Simulation::Geant4DetectorConstruction::Construct()
G4VPhysicalVolume* ActsExamples::DDG4DetectorConstruction::Construct() {
G4VPhysicalVolume* DDG4DetectorConstruction::Construct() {
if (m_world == nullptr) {
dd4hep::sim::Geant4Mapping& g4map = dd4hep::sim::Geant4Mapping::instance();
auto conv = dd4hep::sim::Geant4Converter(dd4hepDetector(),
auto conv = dd4hep::sim::Geant4Converter(m_detector->dd4hepDetector(),
dd4hep::PrintLevel::VERBOSE);
dd4hep::sim::Geant4GeometryInfo* geoInfo =
conv.create(dd4hepDetector().world()).detach();
conv.create(m_detector->dd4hepGeometry()).detach();
g4map.attach(geoInfo);
// All volumes are deleted in ~G4PhysicalVolumeStore()
m_world = geoInfo->world();
Expand All @@ -58,17 +52,18 @@ G4VPhysicalVolume* ActsExamples::DDG4DetectorConstruction::Construct() {
return m_world;
}

ActsExamples::DDG4DetectorConstructionFactory::DDG4DetectorConstructionFactory(
DDG4DetectorConstructionFactory::DDG4DetectorConstructionFactory(
std::shared_ptr<DD4hep::DD4hepDetector> detector,
std::vector<std::shared_ptr<RegionCreator>> regionCreators)
: m_detector(std::move(detector)),
m_regionCreators(std::move(regionCreators)) {}

ActsExamples::DDG4DetectorConstructionFactory::
~DDG4DetectorConstructionFactory() = default;
DDG4DetectorConstructionFactory::~DDG4DetectorConstructionFactory() = default;

std::unique_ptr<G4VUserDetectorConstruction>
ActsExamples::DDG4DetectorConstructionFactory::factorize() const {
DDG4DetectorConstructionFactory::factorize() const {
return std::make_unique<DDG4DetectorConstruction>(m_detector,
m_regionCreators);
}

} // namespace ActsExamples
1 change: 1 addition & 0 deletions Examples/Detectors/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(Common)
add_subdirectory(ContextualDetector)
add_subdirectory_if(DD4hepDetector ACTS_BUILD_EXAMPLES_DD4HEP)
add_subdirectory(GenericDetector)
Expand Down
14 changes: 14 additions & 0 deletions Examples/Detectors/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_library(ActsExamplesDetectorCommons SHARED src/Detector.cpp)
target_include_directories(
ActsExamplesDetectorCommons
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
target_link_libraries(
ActsExamplesDetectorCommons
PUBLIC ActsCore ActsExamplesFramework
)

install(
TARGETS ActsExamplesDetectorCommons
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

// This file is part of the Acts project.
//
// Copyright (C) 2024 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#pragma once

#include "Acts/Geometry/GeometryContext.hpp"

#include <memory>
#include <vector>

namespace Acts {
class TrackingGeometry;
class DetectorElementBase;
class IMaterialDecorator;
class Logger;
namespace Experimental {
class Detector;
} // namespace Experimental
} // namespace Acts

namespace ActsExamples {
class IContextDecorator;
} // namespace ActsExamples

namespace ActsExamples::DetectorCommons {

class Detector {
public:
using TrackingGeometryPtr = std::shared_ptr<const Acts::TrackingGeometry>;
using DetectorPtr = std::shared_ptr<Acts::Experimental::Detector>;
using ContextDecorators =
std::vector<std::shared_ptr<ActsExamples::IContextDecorator>>;

using DetectorElement = Acts::DetectorElementBase;
using DetectorStore = std::vector<std::shared_ptr<const DetectorElement>>;

explicit Detector(std::unique_ptr<const Acts::Logger> logger);
virtual ~Detector() = default;

virtual std::tuple<TrackingGeometryPtr, ContextDecorators, DetectorStore>
trackingGeometry();

virtual std::tuple<DetectorPtr, ContextDecorators, DetectorStore> detector();

virtual void drop();

protected:
TrackingGeometryPtr m_trackingGeometry;
DetectorPtr m_detector;
ContextDecorators m_contextDecorators;
DetectorStore m_detectorStore;

std::unique_ptr<const Acts::Logger> m_logger;

const Acts::Logger& logger() const { return *m_logger; }

virtual void buildTrackingGeometry() = 0;

virtual void buildDetector();
};

} // namespace ActsExamples::DetectorCommons
53 changes: 53 additions & 0 deletions Examples/Detectors/Common/src/Detector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

// This file is part of the Acts project.
//
// Copyright (C) 2024 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "ActsExamples/DetectorCommons/Detector.hpp"

#include "Acts/Utilities/Logger.hpp"

namespace ActsExamples::DetectorCommons {

Detector::Detector(std::unique_ptr<const Acts::Logger> logger)
: m_logger(std::move(logger)) {}

std::tuple<Detector::TrackingGeometryPtr, Detector::ContextDecorators,
Detector::DetectorStore>
Detector::trackingGeometry() {
if (m_trackingGeometry == nullptr) {
buildTrackingGeometry();
}
return {m_trackingGeometry, m_contextDecorators, m_detectorStore};
}

std::tuple<Detector::DetectorPtr, Detector::ContextDecorators,
Detector::DetectorStore>
Detector::detector() {
if (m_detector == nullptr) {
buildDetector();
}
return {m_detector, m_contextDecorators, m_detectorStore};
}

void Detector::drop() {
m_trackingGeometry.reset();
m_detector.reset();
m_contextDecorators.clear();
m_detectorStore.clear();
}

void Detector::buildDetector() {}

} // namespace ActsExamples::DetectorCommons
8 changes: 7 additions & 1 deletion Examples/Detectors/ContextualDetector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ add_library(
src/InternalAlignmentDecorator.cpp
src/ExternalAlignmentDecorator.cpp
)

target_include_directories(
ActsExamplesDetectorContextual
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

target_link_libraries(
ActsExamplesDetectorContextual
PUBLIC ActsCore ActsExamplesFramework ActsExamplesDetectorGeneric
PUBLIC
ActsCore
ActsExamplesFramework
ActsExamplesDetectorCommons
ActsExamplesDetectorGeneric
)

install(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,28 @@

#include "Acts/Definitions/Units.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/DetectorCommons/Detector.hpp"
#include "ActsExamples/GenericDetector/GenericDetector.hpp"

#include <cstddef>
#include <memory>
#include <utility>
#include <vector>

namespace Acts {
class TrackingGeometry;
class IMaterialDecorator;
} // namespace Acts

namespace ActsExamples {
class IContextDecorator;
namespace Generic {
class GenericDetectorElement;
} // namespace Generic
} // namespace ActsExamples

namespace ActsExamples::Contextual {
class InternallyAlignedDetectorElement;
class InternalAlignmentDecorator;

class AlignedDetector {
class AlignedDetector : public DetectorCommons::Detector {
public:
using TrackingGeometryPtr = std::shared_ptr<const Acts::TrackingGeometry>;
using ContextDecorators =
std::vector<std::shared_ptr<ActsExamples::IContextDecorator>>;
using TrackingGeometryPtr = std::shared_ptr<const Acts::TrackingGeometry>;

struct Config : public GenericDetector::Config {
using DetectorStore = std::vector<
std::vector<std::shared_ptr<Generic::GenericDetectorElement>>>;

struct Config : public Generic::GenericDetector::Config {
/// Seed for the decorator random numbers.
std::size_t seed = 1324354657;
/// Size of a valid IOV.
Expand All @@ -63,21 +55,21 @@ class AlignedDetector {

enum class Mode { Internal, External };
Mode mode = Mode::Internal;

std::shared_ptr<const Acts::IMaterialDecorator> materialDecorator;
};

std::pair<TrackingGeometryPtr, ContextDecorators> finalize(
const Config& cfg,
std::shared_ptr<const Acts::IMaterialDecorator> mdecorator);
explicit AlignedDetector(const Config& cfg);

std::vector<std::vector<std::shared_ptr<Generic::GenericDetectorElement>>>&
detectorStore() {
return m_detectorStore;
}
const DetectorStore& detectorStore() const { return m_detectorStore; }

private:
Config m_cfg;

/// The Store of the detector elements (lifetime: job)
std::vector<std::vector<std::shared_ptr<Generic::GenericDetectorElement>>>
m_detectorStore;
DetectorStore m_detectorStore;

void buildTrackingGeometry() final;
};

} // namespace ActsExamples::Contextual
Loading

0 comments on commit 3f1b961

Please sign in to comment.