Skip to content

Commit

Permalink
feat: Add standalone digitization coordinates converter
Browse files Browse the repository at this point in the history
  • Loading branch information
ntadej committed Oct 21, 2024
1 parent c83597c commit 7d4680c
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions Examples/Algorithms/Digitization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(
SHARED
src/DigitizationAlgorithm.cpp
src/DigitizationConfig.cpp
src/DigitizationCoordinatesConverter.cpp
src/MeasurementCreation.cpp
src/DigitizationConfigurator.cpp
src/ModuleClusters.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 "ActsExamples/Digitization/DigitizationConfig.hpp"

namespace ActsExamples {

/// Utility that converts hit coordinates.
class DigitizationCoordinatesConverter final {
public:
/// Construct the converter
///
/// @param config is the configuration
DigitizationCoordinatesConverter(DigitizationConfig config);

/// Get const access to the config
const DigitizationConfig& config() const { return m_cfg; }

/// Convert the hit coordinates to the local frame.
std::tuple<double, double> globalToLocal(std::uint64_t moduleId, double x,
double y, double z) const;

/// Convert the hit coordinates to the global frame.
std::tuple<double, double, double> localToGlobal(std::uint64_t moduleId,
double x, double y) const;

private:
/// Configuration
DigitizationConfig m_cfg;
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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/Digitization/DigitizationCoordinatesConverter.hpp"

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Geometry/GeometryContext.hpp"

#include <cmath>
#include <stdexcept>
#include <utility>

ActsExamples::DigitizationCoordinatesConverter::
DigitizationCoordinatesConverter(DigitizationConfig config)
: m_cfg(std::move(config)) {
if (m_cfg.surfaceByIdentifier.empty()) {
throw std::invalid_argument("Missing Surface-GeometryID association map");
}
}

std::tuple<double, double>
ActsExamples::DigitizationCoordinatesConverter::globalToLocal(
std::uint64_t moduleId, double x, double y, double z) const {
const Acts::GeometryIdentifier moduleGeoId = moduleId;
auto surfaceItr = m_cfg.surfaceByIdentifier.find(moduleGeoId);
if (surfaceItr == m_cfg.surfaceByIdentifier.end()) {
throw std::runtime_error("Surface not found for moduleGeoId");
}

// Transform the position into the local surface frame
const Acts::Surface* surfacePtr = surfaceItr->second;
const auto& invTransform =
surfacePtr->transform(Acts::GeometryContext()).inverse();

const Acts::Vector3 pos(x, y, z);
Acts::Vector2 pos2Local = (invTransform * pos).segment<2>(0);

return {pos2Local.x(), pos2Local.y()};
}

std::tuple<double, double, double>
ActsExamples::DigitizationCoordinatesConverter::localToGlobal(
std::uint64_t moduleId, double x, double y) const {
const Acts::GeometryIdentifier moduleGeoId = moduleId;
auto surfaceItr = m_cfg.surfaceByIdentifier.find(moduleGeoId);
if (surfaceItr == m_cfg.surfaceByIdentifier.end()) {
throw std::runtime_error("Surface not found for moduleGeoId");
}

// Transform the position into the global frame
const Acts::Surface* surfacePtr = surfaceItr->second;
const auto& transform = surfacePtr->transform(Acts::GeometryContext());

const Acts::Vector3 pos(x, y, 0);
Acts::Vector3 pos2Global = (transform * pos);

return {pos2Global.x(), pos2Global.y(), pos2Global.z()};
}
14 changes: 14 additions & 0 deletions Examples/Python/src/Digitization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ActsExamples/Digitization/DigitizationAlgorithm.hpp"
#include "ActsExamples/Digitization/DigitizationConfig.hpp"
#include "ActsExamples/Digitization/DigitizationConfigurator.hpp"
#include "ActsExamples/Digitization/DigitizationCoordinatesConverter.hpp"
#include "ActsExamples/Framework/AlgorithmContext.hpp"
#include "ActsExamples/Io/Json/JsonDigitizationConfig.hpp"

Expand Down Expand Up @@ -101,6 +102,19 @@ void addDigitization(Context& ctx) {
ACTS_PYTHON_MEMBER(outputDigiComponents);
ACTS_PYTHON_STRUCT_END();
}

{
py::class_<ActsExamples::DigitizationCoordinatesConverter,
std::shared_ptr<ActsExamples::DigitizationCoordinatesConverter>>(
mex, "DigitizationCoordinatesConverter")
.def(py::init<ActsExamples::DigitizationConfig&>(), py::arg("config"))
.def_property_readonly(
"config", &ActsExamples::DigitizationCoordinatesConverter::config)
.def("globalToLocal",
&ActsExamples::DigitizationCoordinatesConverter::globalToLocal)
.def("localToGlobal",
&ActsExamples::DigitizationCoordinatesConverter::localToGlobal);
}
}

} // namespace Acts::Python

0 comments on commit 7d4680c

Please sign in to comment.