Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Examples can run sim/digi on ODD with Gen1 and Gen2 geometry #3175

Merged
merged 13 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Core/include/Acts/Geometry/TrackingGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ class TrackingGeometry {
/// @retval pointer to the found surface otherwise.
const Surface* findSurface(GeometryIdentifier id) const;

/// Access to the GeometryIdentifier - Surface association map
const std::unordered_map<GeometryIdentifier, const Surface*>&
geoIdSurfaceMap() const;

private:
// the known world
TrackingVolumePtr m_world;
Expand Down
5 changes: 5 additions & 0 deletions Core/src/Geometry/TrackingGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ const Acts::Surface* Acts::TrackingGeometry::findSurface(
}
return srf->second;
}

const std::unordered_map<Acts::GeometryIdentifier, const Acts::Surface*>&
Acts::TrackingGeometry::geoIdSurfaceMap() const {
return m_surfacesById;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/Definitions/Units.hpp"
#include "Acts/Geometry/GeometryHierarchyMap.hpp"
#include "Acts/Geometry/TrackingGeometry.hpp"
#include "Acts/Utilities/BinUtility.hpp"
#include "Acts/Utilities/BinningType.hpp"
#include "Acts/Utilities/Logger.hpp"
Expand All @@ -26,16 +25,17 @@
#include <algorithm>
#include <cstddef>
#include <functional>
#include <map>
asalzburger marked this conversation as resolved.
Show resolved Hide resolved
#include <memory>
#include <stdexcept>
#include <string>
#include <system_error>
#include <unordered_map>
#include <utility>
#include <vector>

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

namespace ActsExamples {
Expand Down Expand Up @@ -145,8 +145,9 @@ class DigitizationConfig {
std::string outputMeasurementParticlesMap = "measurement_particles_map";
/// Output collection to map measured hits to simulated hits.
std::string outputMeasurementSimHitsMap = "measurement_simhits_map";
/// Tracking geometry required to access global-to-local transforms.
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry = nullptr;
/// Map of surface by identifier to allow local - to global
std::unordered_map<Acts::GeometryIdentifier, const Acts::Surface *>
surfaceByIdentifier;
/// Random numbers tool.
std::shared_ptr<const RandomNumbers> randomNumbers = nullptr;
/// Do we merge hits or not
Expand Down
11 changes: 6 additions & 5 deletions Examples/Algorithms/Digitization/src/DigitizationAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ ActsExamples::DigitizationAlgorithm::DigitizationAlgorithm(
throw std::invalid_argument(
"Missing hit-to-simulated-hits map output collection");
}
if (!m_cfg.trackingGeometry) {
throw std::invalid_argument("Missing tracking geometry");
if (m_cfg.surfaceByIdentifier.empty()) {
throw std::invalid_argument("Missing Surface-GeometryID association map");
}
if (!m_cfg.randomNumbers) {
throw std::invalid_argument("Missing random numbers tool");
Expand Down Expand Up @@ -162,17 +162,18 @@ ActsExamples::ProcessCode ActsExamples::DigitizationAlgorithm::execute(
Acts::GeometryIdentifier moduleGeoId = simHitsGroup.first;
const auto& moduleSimHits = simHitsGroup.second;

const Acts::Surface* surfacePtr =
m_cfg.trackingGeometry->findSurface(moduleGeoId);
auto surfaceItr = m_cfg.surfaceByIdentifier.find(moduleGeoId);

if (surfacePtr == nullptr) {
if (surfaceItr == m_cfg.surfaceByIdentifier.end()) {
// this is either an invalid geometry id or a misconfigured smearer
// setup; both cases can not be handled and should be fatal.
ACTS_ERROR("Could not find surface " << moduleGeoId
<< " for configured smearer");
return ProcessCode::ABORT;
}

const Acts::Surface* surfacePtr = surfaceItr->second;

auto digitizerItr = m_digitizers.find(moduleGeoId);
if (digitizerItr == m_digitizers.end()) {
ACTS_VERBOSE("No digitizer present for module " << moduleGeoId);
Expand Down
2 changes: 0 additions & 2 deletions Examples/Io/Json/src/JsonDigitizationConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ void ActsExamples::from_json(const nlohmann::json& j,
gdc.threshold = j["threshold"];
gdc.digital = j["digital"];
if (j.find("variances") != j.end()) {
std::cout << "READING variance map!" << std::endl;

/// Read the variances from the json file
auto jvariances = j["variances"];
for (const auto& jvar : jvariances) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ class RootMeasurementWriter final : public WriterT<MeasurementContainer> {
std::string fileMode = "RECREATE"; ///< file access mode
/// The indices for this digitization configurations
Acts::GeometryHierarchyMap<std::vector<Acts::BoundIndices>> boundIndices;
/// Tracking geometry required to access local-to-global transforms.
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry;
/// Map of the geometry identifier to the surface
std::unordered_map<Acts::GeometryIdentifier, const Acts::Surface*>
surfaceByIdentifier;
};

struct DigitizationTree {
Expand Down
12 changes: 5 additions & 7 deletions Examples/Io/Root/src/RootMeasurementWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "ActsExamples/Io/Root/RootMeasurementWriter.hpp"

#include "Acts/Geometry/TrackingGeometry.hpp"
#include "ActsExamples/EventData/AverageSimHits.hpp"
#include "ActsExamples/EventData/Index.hpp"
#include "ActsExamples/EventData/IndexSourceLink.hpp"
Expand Down Expand Up @@ -45,8 +44,8 @@ ActsExamples::RootMeasurementWriter::RootMeasurementWriter(
m_inputMeasurementSimHitsMap.initialize(m_cfg.inputMeasurementSimHitsMap);
m_inputClusters.maybeInitialize(m_cfg.inputClusters);

if (!m_cfg.trackingGeometry) {
throw std::invalid_argument("Missing tracking geometry");
if (m_cfg.surfaceByIdentifier.empty()) {
throw std::invalid_argument("Missing Surface-GeoID association map");
}
// Setup ROOT File
m_outputFile = TFile::Open(m_cfg.filePath.c_str(), m_cfg.fileMode.c_str());
Expand Down Expand Up @@ -122,12 +121,11 @@ ActsExamples::ProcessCode ActsExamples::RootMeasurementWriter::writeT(
Acts::GeometryIdentifier geoId =
m.sourceLink().template get<IndexSourceLink>().geometryId();
// find the corresponding surface
const Acts::Surface* surfacePtr =
m_cfg.trackingGeometry->findSurface(geoId);
if (!surfacePtr) {
auto surfaceItr = m_cfg.surfaceByIdentifier.find(geoId);
if (surfaceItr == m_cfg.surfaceByIdentifier.end()) {
return;
}
const Acts::Surface& surface = *surfacePtr;
const Acts::Surface& surface = *(surfaceItr->second);
// find the corresponding output tree
auto dTreeItr = m_outputTrees.find(geoId);
if (dTreeItr == m_outputTrees.end()) {
Expand Down
10 changes: 5 additions & 5 deletions Examples/Python/python/acts/examples/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def addGeant4(
----------
s: Sequencer
the sequencer module to which we add the Geant4 steps (returned from addGeant4)
trackingGeometry : tracking geometry
trackingGeometry : tracking geometry or detector
field : magnetic field
rnd : RandomNumbers, None
random number generator
Expand Down Expand Up @@ -766,7 +766,7 @@ def addGeant4(

def addDigitization(
s: acts.examples.Sequencer,
trackingGeometry: acts.TrackingGeometry,
trackingGeometry: Union[acts.TrackingGeometry, acts.Detector],
field: acts.MagneticFieldProvider,
digiConfigFile: Union[Path, str],
outputDirCsv: Optional[Union[Path, str]] = None,
Expand All @@ -782,7 +782,7 @@ def addDigitization(
----------
s: Sequencer
the sequencer module to which we add the Digitization steps (returned from addDigitization)
trackingGeometry : tracking geometry
trackingGeometry : tracking geometry or detector
field : magnetic field
digiConfigFile : Path|str, path
Configuration (.json) file for digitization or smearing description
Expand All @@ -804,7 +804,7 @@ def addDigitization(
acts.examples.readDigiConfigFromJson(
str(digiConfigFile),
),
trackingGeometry=trackingGeometry,
surfaceByIdentifier=trackingGeometry.geoIdSurfaceMap(),
randomNumbers=rnd,
inputSimHits="simhits",
outputSourceLinks="sourcelinks",
Expand Down Expand Up @@ -832,7 +832,7 @@ def addDigitization(
inputSimHits=digiAlg.config.inputSimHits,
inputMeasurementSimHitsMap=digiAlg.config.outputMeasurementSimHitsMap,
filePath=str(outputDirRoot / f"{digiAlg.config.outputMeasurements}.root"),
trackingGeometry=trackingGeometry,
surfaceByIdentifier=trackingGeometry.geoIdSurfaceMap(),
)
rmwConfig.addBoundIndicesFromDigiConfig(digiAlg.config)
s.addWriter(acts.examples.RootMeasurementWriter(rmwConfig, customLogLevel()))
Expand Down
2 changes: 1 addition & 1 deletion Examples/Python/src/Digitization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void addDigitization(Context& ctx) {
ACTS_PYTHON_MEMBER(outputClusters);
ACTS_PYTHON_MEMBER(outputMeasurementParticlesMap);
ACTS_PYTHON_MEMBER(outputMeasurementSimHitsMap);
ACTS_PYTHON_MEMBER(trackingGeometry);
ACTS_PYTHON_MEMBER(surfaceByIdentifier);
ACTS_PYTHON_MEMBER(randomNumbers);
ACTS_PYTHON_MEMBER(doMerge);
ACTS_PYTHON_MEMBER(minEnergyDeposit);
Expand Down
24 changes: 20 additions & 4 deletions Examples/Python/src/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include <array>
#include <memory>
#include <unordered_map>
#include <vector>

#include <pybind11/pybind11.h>
Expand Down Expand Up @@ -74,6 +75,14 @@ struct MaterialSurfaceSelector {
}
};

struct IdentifierSurfacesCollector {
std::unordered_map<Acts::GeometryIdentifier, const Acts::Surface*> surfaces;
/// @param surface is the test surface
void operator()(const Acts::Surface* surface) {
surfaces[surface->geometryId()] = surface;
}
};

} // namespace

namespace Acts::Python {
Expand Down Expand Up @@ -141,6 +150,7 @@ void addGeometry(Context& ctx) {
[](Acts::TrackingGeometry& self, py::function& func) {
self.visitSurfaces(func);
})
.def("geoIdSurfaceMap", &Acts::TrackingGeometry::geoIdSurfaceMap)
.def("extractMaterialSurfaces",
[](Acts::TrackingGeometry& self) {
MaterialSurfaceSelector selector;
Expand Down Expand Up @@ -217,10 +227,16 @@ void addExperimentalGeometry(Context& ctx) {
.def("volumePtrs", &Detector::volumePtrs)
.def("numberVolumes",
[](Detector& self) { return self.volumes().size(); })
.def("extractMaterialSurfaces", [](Detector& self) {
MaterialSurfaceSelector selector;
self.visitSurfaces(selector);
return selector.surfaces;
.def("extractMaterialSurfaces",
[](Detector& self) {
MaterialSurfaceSelector selector;
self.visitSurfaces(selector);
return selector.surfaces;
})
.def("geoIdSurfaceMap", [](Detector& self) {
IdentifierSurfacesCollector collector;
self.visitSurfaces(collector);
return collector.surfaces;
});

// Portal definition
Expand Down
2 changes: 1 addition & 1 deletion Examples/Python/src/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void addOutput(Context& ctx) {
ACTS_PYTHON_MEMBER(filePath);
ACTS_PYTHON_MEMBER(fileMode);
ACTS_PYTHON_MEMBER(boundIndices);
ACTS_PYTHON_MEMBER(trackingGeometry);
ACTS_PYTHON_MEMBER(surfaceByIdentifier);
ACTS_PYTHON_STRUCT_END();
}

Expand Down
2 changes: 1 addition & 1 deletion Examples/Python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def _factory(s):
/ "Examples/Algorithms/Digitization/share/default-smearing-config-generic.json"
)
),
trackingGeometry=trk_geo,
surfaceByIdentifier=trk_geo.geoIdSurfaceMap(),
randomNumbers=rng,
inputSimHits=simAlg.config.outputSimHits,
)
Expand Down
6 changes: 3 additions & 3 deletions Examples/Python/tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_root_meas_writer(tmp_path, fatras, trk_geo, assert_root_hash):
inputSimHits=simAlg.config.outputSimHits,
inputMeasurementSimHitsMap=digiAlg.config.outputMeasurementSimHitsMap,
filePath=str(out),
trackingGeometry=trk_geo,
surfaceByIdentifier=trk_geo.geoIdSurfaceMap(),
)
config.addBoundIndicesFromDigiConfig(digiAlg.config)
s.addWriter(RootMeasurementWriter(level=acts.logging.INFO, config=config))
Expand Down Expand Up @@ -280,8 +280,8 @@ def test_root_writer_interface(writer, conf_const, tmp_path, trk_geo):
for k, _ in inspect.getmembers(config):
if k.startswith("input"):
kw[k] = "collection"
if k == "trackingGeometry":
kw[k] = trk_geo
if k == "surfaceByIdentifier":
kw[k] = trk_geo.geoIdSurfaceMap()

assert conf_const(writer, **kw)

Expand Down
Loading
Loading