Skip to content

Commit

Permalink
first conversion round
Browse files Browse the repository at this point in the history
  • Loading branch information
asalzburger committed Feb 7, 2024
1 parent d8eda2a commit f793eb6
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 27 deletions.
30 changes: 30 additions & 0 deletions Examples/Python/src/GeoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// 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 "Acts/Plugins/GeoModel/GeoModelDetectorElement.hpp"
#include "Acts/Plugins/GeoModel/GeoModelDetectorSurfaceFactory.hpp"
#include "Acts/Plugins/GeoModel/GeoModelReader.hpp"
#include "Acts/Plugins/GeoModel/GeoModelTree.hpp"
#include "Acts/Plugins/Python/Utilities.hpp"
Expand All @@ -23,11 +25,39 @@ namespace Acts::Python {
void addGeoModel(Context& ctx) {
auto m = ctx.get("main");

py::class_<Acts::GeoModelDetectorElement,
std::shared_ptr<Acts::GeoModelDetectorElement>>(
m, "GeoModelDetectorElement");

py::class_<Acts::GeoModelTree>(m, "GeoModelTree").def(py::init<>());

{
py::module m2 = m.def_submodule("GeoModelReader");
m2.def("readFromDb", &Acts::GeoModelReader::readFromDb);
}

{
auto f =
py::class_<Acts::GeoModelDetectorSurfaceFactory,
std::shared_ptr<Acts::GeoModelDetectorSurfaceFactory>>(
m, "GeoModelDetectorSurfaceFactory")
.def(py::init([](Acts::Logging::Level level) {
return std::make_shared<Acts::GeoModelDetectorSurfaceFactory>(
Acts::getDefaultLogger("GeoModelDetectorSurfaceFactory",
level));
}))
.def("construct", &Acts::GeoModelDetectorSurfaceFactory::construct);

py::class_<Acts::GeoModelDetectorSurfaceFactory::Cache>(f, "Cache")
.def(py::init<>())
.def_readwrite(
"sensitiveSurfaces",
&Acts::GeoModelDetectorSurfaceFactory::Cache::sensitiveSurfaces);

py::class_<Acts::GeoModelDetectorSurfaceFactory::Options>(f, "Options")
.def(py::init<>())
.def_readwrite("queries",
&Acts::GeoModelDetectorSurfaceFactory::Options::queries);
}
}
} // namespace Acts::Python
4 changes: 3 additions & 1 deletion Plugins/GeoModel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ include(FetchContent)
add_library(
ActsPluginGeoModel SHARED
src/GeoModelReader.cpp
src/GeoModelDetectorElement.cpp)
src/GeoModelDetectorElement.cpp
src/GeoModelDetectorSurfaceFactory.cpp
src/GeoModelSurfaceConverter.cpp)
target_include_directories(
ActsPluginGeoModel
PUBLIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,42 @@

#include <memory>

class GeoVPhysVol;
class GeoFullPhysVol;

namespace Acts {

class ISurfaceMaterial;
class Surface;
class PlanarBounds;

/// @class GeoModelDetectorElement
///
/// Detector element representative for GeoModel based
/// Detector element representative for GeoModel based
/// sensitive elements.
class GeoModelDetectorElement : public DetectorElementBase {
public:
/// Broadcast the context type
using ContextType = GeometryContext;

/// @brief Constructor with arguments
/// @param surface the surface representing this detector element
/// @param geoPhysVol the physical volume representing this detector element
/// @param toGlobal the global transformation before the volume
/// @param thickness the thickness of this detector element
GeoModelDetectorElement(std::shared_ptr<Surface> surface,
const GeoVPhysVol& geoPhysVol,
const Transform3& toGlobal, ActsScalar thickness);
// Deleted default constructor
GeoModelDetectorElement() = delete;

/// @brief Factory to create a planar detector element with connected surfcace
///
/// @param geoPhysVol reprsenting the physical volume
/// @param pBounds the planar bounds
/// @param sfTransform the surface transform
/// @param thickness the thickness of the detector element
/// @return
static std::shared_ptr<GeoModelDetectorElement> createPlanarElement(
const GeoFullPhysVol& geoPhysVol,
const std::shared_ptr<PlanarBounds> pBounds,
const Transform3& sfTransform, ActsScalar thickness);

/// Constructor
GeoModelDetectorElement(const GeoFullPhysVol& geoPhysVol,
const std::shared_ptr<PlanarBounds> pBounds,
const Transform3& sfTransform, ActsScalar thickness);

/// Return local to global transform associated with this detector element
///
Expand All @@ -55,18 +67,17 @@ class GeoModelDetectorElement : public DetectorElementBase {
ActsScalar thickness() const override;

/// @return to the Geant4 physical volume
const GeoVPhysVol& geoVPhysicalVolume() const;
const GeoFullPhysVol& physicalVolume() const;

private:
/// Corresponding Surface
/// The GeoModel full physical volume
const GeoFullPhysVol* m_geoPhysVol{nullptr};
/// The surface
std::shared_ptr<Surface> m_surface;
/// The GEant4 physical volume
const GeoVPhysVol* m_geoPhysVol{nullptr};
/// The global transformation before the volume
Transform3 m_toGlobal;
Transform3 m_surfaceTransform;
/// Thickness of this detector element
ActsScalar m_thickness{0.};
};

} // namespace Acts

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// This file is part of the Acts project.
//
// Copyright (C) 2023 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 "Acts/Utilities/Logger.hpp"

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

namespace Acts {

struct GeoModelTree;
class GeoModelDetectorElement;
class Surface;

/// A factory to convert GeoModel volume into sensitive
/// or passive surfaces which are filled into a Cache object,
/// also the create GeoModelDetectorElements which are also
/// returned.
class GeoModelDetectorSurfaceFactory {
public:
/// Collect the sensitive surface & detector element
using GeoModelSensitiveSurface =
std::tuple<std::shared_ptr<GeoModelDetectorElement>,
std::shared_ptr<Surface>>;

/// Collect the passive surfaces, bool whether it should be
/// added as an "always try, i.e. assignToAll=true" surface
using GeoModelPassiveSurface = std::tuple<std::shared_ptr<Surface>, bool>;

/// Nested cache that records the conversion status
struct Cache {
/// The created detector elements - for the detector store
std::vector<GeoModelSensitiveSurface> sensitiveSurfaces;
/// The created non-const surfaces - for further processing,
std::vector<GeoModelPassiveSurface> passiveSurfaces;
};

/// The options to steer the conversion
struct Options {
std::vector<std::string> queries = {};
};

/// The GeoModel detector element factory
///
/// @param mlogger a screen output logger
GeoModelDetectorSurfaceFactory(
std::unique_ptr<const Logger> mlogger = getDefaultLogger(
"GeoModelDetectorSurfaceFactory", Acts::Logging::INFO));

/// Construction method of the detector elements
///
/// @param cache [in,out] into which the Elements are filled
/// @param gctx the geometry context
/// @param geoModelTree the gjeo model tree
/// @param options to steer the conversion
///
/// @note this method will call the recursive construction
void construct(Cache& cache, const GeometryContext& gctx,
const GeoModelTree& geoModelTree, const Options& options);

private:
/// Logging instance
std::unique_ptr<const Logger> m_logger;

/// Private access to the logger
const Logger& logger() const { return *m_logger; }
};

} // namespace Acts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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/Plugins/GeoModel/GeoModelDetectorElement.hpp"

class GeoFullPhysVol;

namespace Acts {
/// Collect the sensitive surface & detector element
using GeoModelSensitiveSurface =
std::tuple<std::shared_ptr<GeoModelDetectorElement>,
std::shared_ptr<Surface>>;

namespace GeoModelSurfaceConverter {


/// @brief conversion to sensitive surface
///
/// @param geoPhysVol the geoPhysVol to convert
///
/// @return a detector element and a surface
GeoModelSensitiveSurface convertToSensitiveSurface(
const GeoFullPhysVol& geoPhysVol);
}
} // namespace Acts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GeoVPhysVol;
namespace Acts {

struct GeoModelTree {

std::shared_ptr<GeoModelIO::ReadGeoModel> geoReader = nullptr;
GeoVPhysVol* worldVolume = nullptr;
};
} // namespace Acts
24 changes: 17 additions & 7 deletions Plugins/GeoModel/src/GeoModelDetectorElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,31 @@

#include "Acts/Plugins/GeoModel/GeoModelDetectorElement.hpp"

#include "Acts/Surfaces/PlanarBounds.hpp"
#include "Acts/Surfaces/PlaneSurface.hpp"
#include "Acts/Surfaces/Surface.hpp"

#include <utility>

std::shared_ptr<Acts::GeoModelDetectorElement>
Acts::GeoModelDetectorElement::createPlanarElement(
const GeoFullPhysVol& geoPhysVol, const std::shared_ptr<PlanarBounds> pBounds,
const Transform3& sfTransform, ActsScalar thickness) {
return std::make_shared<GeoModelDetectorElement>(
geoPhysVol, std::move(pBounds), sfTransform, thickness);
}

Acts::GeoModelDetectorElement::GeoModelDetectorElement(
std::shared_ptr<Acts::Surface> surface, const GeoVPhysVol& geoPhysVol,
const Acts::Transform3& toGlobal, Acts::ActsScalar thickness)
: m_surface(std::move(surface)),
m_geoPhysVol(&geoPhysVol),
m_toGlobal(toGlobal),
const GeoFullPhysVol& geoPhysVol, const std::shared_ptr<PlanarBounds> pBounds,
const Transform3& sfTransform, ActsScalar thickness)
: m_geoPhysVol(&geoPhysVol),
m_surface(Surface::makeShared<PlaneSurface>(sfTransform, pBounds)),
m_surfaceTransform(sfTransform),
m_thickness(thickness) {}

const Acts::Transform3& Acts::GeoModelDetectorElement::transform(
const GeometryContext& /*gctx*/) const {
return m_toGlobal;
return m_surfaceTransform;
}

const Acts::Surface& Acts::GeoModelDetectorElement::surface() const {
Expand All @@ -37,6 +47,6 @@ Acts::ActsScalar Acts::GeoModelDetectorElement::thickness() const {
return m_thickness;
}

const GeoVPhysVol& Acts::GeoModelDetectorElement::geoVPhysicalVolume() const {
const GeoFullPhysVol& Acts::GeoModelDetectorElement::physicalVolume() const {
return *m_geoPhysVol;
}
49 changes: 49 additions & 0 deletions Plugins/GeoModel/src/GeoModelDetectorSurfaceFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 "Acts/Plugins/GeoModel/GeoModelDetectorSurfaceFactory.hpp"

#include "Acts/Plugins/GeoModel/GeoModelDetectorElement.hpp"
#include "Acts/Plugins/GeoModel/GeoModelSurfaceConverter.hpp"
#include "Acts/Plugins/GeoModel/GeoModelTree.hpp"

using namespace Acts::detail;

Acts::GeoModelDetectorSurfaceFactory::GeoModelDetectorSurfaceFactory(
std::unique_ptr<const Logger> mlogger)
: m_logger(std::move(mlogger)) {}

void Acts::GeoModelDetectorSurfaceFactory::construct(
Cache& cache, const GeometryContext& gctx, const GeoModelTree& geoModelTree,
const Options& options) {
if (geoModelTree.geoReader == nullptr) {
throw std::invalid_argument("GeoModelTree has no GeoModelReader");
}

for (const auto& q : options.queries) {
ACTS_VERBOSE("Constructing detector elements for query " << q);
auto qFPV =
geoModelTree.geoReader->getPublishedNodes<std::string, GeoFullPhysVol*>(
q);

for (auto& [name, fpv] : qFPV) {
// Convert the full physical volume to a sensitive surface
auto sensitive =
GeoModelSurfaceConverter::convertToSensitiveSurface(*fpv);
if (std::get<0>(sensitive) != nullptr) {
// Add the surface to the cache
cache.sensitiveSurfaces.push_back(sensitive);
}
}
ACTS_VERBOSE("Found " << qFPV.size()
<< " full physical volumes matching the query.");
}
ACTS_DEBUG("Constructed "
<< cache.sensitiveSurfaces.size() << " sensitive elements and "
<< cache.passiveSurfaces.size() << " passive elements");
}
5 changes: 3 additions & 2 deletions Plugins/GeoModel/src/GeoModelReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Acts::GeoModelTree Acts::GeoModelReader::readFromDb(const std::string& dbPath) {
throw std::runtime_error("GeoModelReader: Could not open the database");
}
// Setup the GeoModel reader
GeoModelIO::ReadGeoModel geoReader = GeoModelIO::ReadGeoModel(db);
GeoModelTree geoModel{geoReader.buildGeoModel()};
auto geoReader = std::make_shared<GeoModelIO::ReadGeoModel>(db);
// Read the GeoModel
GeoModelTree geoModel{geoReader, geoReader->buildGeoModel()};
return geoModel;
}
Loading

0 comments on commit f793eb6

Please sign in to comment.