Skip to content

Commit

Permalink
chore: Cleanup some Geant4 Examples code
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Nov 23, 2024
1 parent 306ae23 commit bac20dd
Show file tree
Hide file tree
Showing 46 changed files with 449 additions and 300 deletions.
145 changes: 145 additions & 0 deletions Core/include/Acts/TrackFinding/MeasurementAdapter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// 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/.

#pragma once

#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/TrackFinding/MeasurementSelector.hpp"

#include <cstdint>

namespace Acts {

class Surface;

// TODO not too happy with the name `MeasurementAdapter` as it is still quite
// generic even tho this concept is only useful during combinatorial track
// finding. Maybe `CombinatorialMeasurementAdapter` would be better?

template <typename MeasurementAdapter>
concept MeasurementAdapterConcept = requires(const MeasurementAdapter& a,
const Surface& s) {
typename MeasurementAdapter::State;

typename MeasurementAdapter::TrackStateContainer;
typename MeasurementAdapter::TrackStateProxy;

typename MeasurementAdapter::PreBoundSelectionRange;
typename MeasurementAdapter::BoundSelectionRange;

{ a.makeState() } -> std::same_as<typename MeasurementAdapter::State>;

{ a.clearState(s) } -> std::same_as<void>;

{ a.isActive(s) } -> std::same_as<bool>;

{
a.selectPreBound(s)
} -> std::same_as<typename MeasurementAdapter::PreBoundSelectionRange>;

requires requires(
const MeasurementAdapter::PreBoundSelectionRange& preBoundSelectionRange,
const MeasurementAdapter::TrackStateContainer& trackStateContainer,
const Surface& surface, const BoundVector& parameters,
const BoundMatrix& covariance) {
{
a.selectPreCalibrationImpl(preBoundSelectionRange, surface, parameters,
covariance, trackStateContainer)
} -> std::same_as<typename MeasurementAdapter::BoundSelectionRange>;
};
};

template <typename Derived, typename traj_t>
class MeasurementAdapterBase {
public:
using TrackStateContainer = traj_t;
using TrackStateProxy = typename traj_t::TrackStateProxy;

using BoundSelectionRange = std::pair<std::size_t, std::size_t>;

struct State {};

Derived::State makeState() const { return {}; }

void clearState(Derived::State& state, const Surface& surface) const {
(void)state;
(void)surface;
}

bool isActive(Derived::State& state, const Surface& surface) const {
(void)state;
(void)surface;

return true;
}

bool shouldCreateHoleImpl(
Derived::State& state,
const Derived::BoundSelectionRange& postCalibrationSelectionRange,
const TrackStateContainer& trackStateContainer, const Surface& surface,
const BoundVector& parameters, const BoundMatrix& covariance) const {
(void)state;
(void)postCalibrationSelectionRange;
(void)trackStateContainer;
(void)surface;
(void)parameters;
(void)covariance;

return postCalibrationSelectionRange.first ==
postCalibrationSelectionRange.second;
}

Derived::BoundSelectionRange selectPostBound(
Derived::State& state,
const Derived::PreBoundSelectionRange& preBoundSelectionRange,
const Derived::TrackStateContainer& trackStateContainer,
const Surface& surface, const BoundVector& parameters,
const BoundMatrix& covariance) const {
typename Derived::BoundSelectionRange range;

// pre calibration selection and calibration
{
range.first = trackStateContainer.size();

for (auto measurementIt = preBoundSelectionRange.first;
measurementIt != preBoundSelectionRange.second; ++measurementIt) {
const auto& measurement = *measurementIt;

if (!self().doSelectPreCalibrationImpl(state, measurement, surface,
parameters, covariance)) {
continue;
}

TrackStateProxy trackState = trackStateContainer.makeTrackState(
TrackStatePropMask::None, MultiTrajectoryTraits::kInvalid);

self().calibrateImpl(state, trackState, measurement);

if (!self().doSelectPostCalibrationImpl(state, trackState, surface,
parameters, covariance)) {
// TODO pop the last track state
continue;
}
}

range.second = trackStateContainer.size();
}

if (self().shouldCreateHoleImpl(state, range, trackStateContainer, surface,
parameters, covariance)) {
// TODO
}

return range;
}

private:
const Derived& self() const { return static_cast<const Derived&>(*this); }
};

} // namespace Acts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DDG4DetectorConstruction final : public G4VUserDetectorConstruction {
public:
DDG4DetectorConstruction(
std::shared_ptr<DD4hep::DD4hepDetector> detector,
std::vector<std::shared_ptr<RegionCreator>> regionCreators = {});
std::vector<std::shared_ptr<Geant4::RegionCreator>> regionCreators = {});
~DDG4DetectorConstruction() final;

/// Convert the stored DD4hep detector to a Geant4 description.
Expand All @@ -47,7 +47,7 @@ class DDG4DetectorConstruction final : public G4VUserDetectorConstruction {
/// The Acts DD4hep detector instance
std::shared_ptr<DD4hep::DD4hepDetector> m_detector;
/// Region creators
std::vector<std::shared_ptr<RegionCreator>> m_regionCreators;
std::vector<std::shared_ptr<Geant4::RegionCreator>> m_regionCreators;
/// The world volume
G4VPhysicalVolume* m_world = nullptr;

Expand All @@ -56,11 +56,11 @@ class DDG4DetectorConstruction final : public G4VUserDetectorConstruction {
};

class DDG4DetectorConstructionFactory final
: public DetectorConstructionFactory {
: public Geant4::DetectorConstructionFactory {
public:
DDG4DetectorConstructionFactory(
std::shared_ptr<DD4hep::DD4hepDetector> detector,
std::vector<std::shared_ptr<RegionCreator>> regionCreators = {});
std::vector<std::shared_ptr<Geant4::RegionCreator>> regionCreators = {});
~DDG4DetectorConstructionFactory() final;

std::unique_ptr<G4VUserDetectorConstruction> factorize() const override;
Expand All @@ -69,7 +69,7 @@ class DDG4DetectorConstructionFactory final
/// The Acts DD4hep detector instance
std::shared_ptr<DD4hep::DD4hepDetector> m_detector;
/// Region creators
std::vector<std::shared_ptr<RegionCreator>> m_regionCreators;
std::vector<std::shared_ptr<Geant4::RegionCreator>> m_regionCreators;
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include <G4VUserDetectorConstruction.hh>

namespace ActsExamples {
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.
Expand All @@ -23,4 +23,4 @@ class DetectorConstructionFactory {
virtual std::unique_ptr<G4VUserDetectorConstruction> factorize() const = 0;
};

} // namespace ActsExamples
} // namespace ActsExamples::Geant4
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#include "G4Types.hh"

namespace ActsExamples {

class WhiteBoard;
}

namespace ActsExamples::Geant4 {

/// Common event store for all Geant4 related sub algorithms
struct EventStore {
Expand Down Expand Up @@ -80,4 +82,4 @@ struct EventStore {
std::unordered_map<BarcodeWithoutSubparticle, std::size_t> subparticleMap;
};

} // namespace ActsExamples
} // namespace ActsExamples::Geant4
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GdmlDetectorConstruction final : public G4VUserDetectorConstruction {
/// @param regionCreators are the region creators
GdmlDetectorConstruction(
std::string path,
std::vector<std::shared_ptr<RegionCreator>> regionCreators = {});
std::vector<std::shared_ptr<Geant4::RegionCreator>> regionCreators = {});

/// Read the file and parse it to construct the Geant4 description
///
Expand All @@ -38,25 +38,25 @@ class GdmlDetectorConstruction final : public G4VUserDetectorConstruction {
/// Path to the Gdml file
std::string m_path;
/// Region creators
std::vector<std::shared_ptr<RegionCreator>> m_regionCreators;
std::vector<std::shared_ptr<Geant4::RegionCreator>> m_regionCreators;
/// Cached world volume
G4VPhysicalVolume* m_world = nullptr;
};

class GdmlDetectorConstructionFactory final
: public DetectorConstructionFactory {
: public Geant4::DetectorConstructionFactory {
public:
GdmlDetectorConstructionFactory(
std::string path,
std::vector<std::shared_ptr<RegionCreator>> regionCreators = {});
std::vector<std::shared_ptr<Geant4::RegionCreator>> regionCreators = {});

std::unique_ptr<G4VUserDetectorConstruction> factorize() const override;

private:
/// Path to the Gdml file
std::string m_path;
/// Region creators
std::vector<std::shared_ptr<RegionCreator>> m_regionCreators;
std::vector<std::shared_ptr<Geant4::RegionCreator>> m_regionCreators;
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class G4VUserPhysicsList;

namespace ActsExamples {

class PhysicsListFactory;
class Geant4Manager;
namespace Geant4 {
class PhysicsListFactory;
}

/// Manages the life time of G4RunManager and G4VUserPhysicsList.
///
Expand Down Expand Up @@ -75,12 +77,14 @@ class Geant4Manager {
/// Registers a named physics list factory to the manager for easy
/// instantiation when needed.
void registerPhysicsListFactory(
std::string name, std::shared_ptr<PhysicsListFactory> physicsListFactory);
std::string name,
std::shared_ptr<Geant4::PhysicsListFactory> physicsListFactory);
std::unique_ptr<G4VUserPhysicsList> createPhysicsList(
const std::string &name) const;

/// Get the current list of physics list factories.
const std::unordered_map<std::string, std::shared_ptr<PhysicsListFactory>> &
const std::unordered_map<std::string,
std::shared_ptr<Geant4::PhysicsListFactory>> &
getPhysicsListFactories() const;

private:
Expand All @@ -89,7 +93,7 @@ class Geant4Manager {

bool m_created = false;
std::weak_ptr<Geant4Handle> m_handle;
std::unordered_map<std::string, std::shared_ptr<PhysicsListFactory>>
std::unordered_map<std::string, std::shared_ptr<Geant4::PhysicsListFactory>>
m_physicsListFactories;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ class MagneticFieldProvider;
class Volume;
} // namespace Acts

class G4VUserDetectorConstruction;

namespace ActsExamples {
struct Geant4Handle;

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

/// Abstracts common Geant4 Acts algorithm behaviour.
class Geant4SimulationBase : public IAlgorithm {
Expand All @@ -61,7 +62,8 @@ class Geant4SimulationBase : public IAlgorithm {

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

/// Optional Geant4 instance overwrite.
std::shared_ptr<Geant4Handle> geant4Handle;
Expand Down Expand Up @@ -91,11 +93,11 @@ class Geant4SimulationBase : public IAlgorithm {

G4RunManager& runManager() const;

EventStore& eventStore() const;
Geant4::EventStore& eventStore() const;

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

std::shared_ptr<EventStore> m_eventStore;
std::shared_ptr<Geant4::EventStore> m_eventStore;

int m_geant4Level{};

Expand All @@ -122,8 +124,8 @@ class Geant4Simulation final : public Geant4SimulationBase {
std::string outputParticles = "particles_simulated";

/// The ACTS sensitive surfaces in a mapper, used for hit creation
std::shared_ptr<const SensitiveSurfaceMapper> sensitiveSurfaceMapper =
nullptr;
std::shared_ptr<const Geant4::SensitiveSurfaceMapper>
sensitiveSurfaceMapper = nullptr;

/// The ACTS Magnetic field provider
std::shared_ptr<const Acts::MagneticFieldProvider> magneticField = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Acts {
class MagneticFieldProvider;
}

namespace ActsExamples {
namespace ActsExamples::Geant4 {

/// A magnetic field wrapper for the Acts magnetic field
/// to be used with Geant4.
Expand Down Expand Up @@ -58,4 +58,4 @@ class MagneticFieldWrapper : public G4MagneticField {
std::unique_ptr<const Acts::Logger> m_logger;
};

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

#include <memory>
#include <string>
#include <vector>

#include <G4VUserPhysicsList.hh>

namespace ActsExamples {
namespace ActsExamples::Geant4 {

/// @class MaterialPhysicsList
///
Expand Down Expand Up @@ -57,4 +56,4 @@ class MaterialPhysicsList final : public G4VUserPhysicsList {
std::unique_ptr<const Acts::Logger> m_logger;
};

} // namespace ActsExamples
} // namespace ActsExamples::Geant4
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class G4Step;

namespace ActsExamples {
namespace ActsExamples::Geant4 {

/// @class MaterialSteppingAction
///
Expand Down Expand Up @@ -66,4 +66,4 @@ class MaterialSteppingAction final : public G4UserSteppingAction {
std::unique_ptr<const Acts::Logger> m_logger;
};

} // namespace ActsExamples
} // namespace ActsExamples::Geant4
Loading

0 comments on commit bac20dd

Please sign in to comment.