-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add DD4hep field adapter (#2593)
This allows querying the DD4hep-provided magnetic field for our purposes. You can define a constant field like ```xml <field name="MagnetFields_Constant" type="ConstantField" field="magnetic"> <strength x="0" y="0" z="2.0*tesla"/> </field> ``` or for example a solenoid field like ```xml <field type="solenoid" name="GlobalSolenoid" inner_field="Field_nominal_value" outer_field="outerField_nominal_value" zmax="sol_hlength" inner_radius="sol_rmin" outer_radius="sol_rmax" /> ```
- Loading branch information
1 parent
040a896
commit ecdbde7
Showing
9 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
Plugins/DD4hep/include/Acts/Plugins/DD4hep/DD4hepFieldAdapter.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// 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/MagneticField/MagneticFieldProvider.hpp" | ||
|
||
#include <memory> | ||
|
||
namespace dd4hep { | ||
class OverlayedField; | ||
} | ||
|
||
namespace Acts { | ||
|
||
class DD4hepFieldAdapter : public Acts::MagneticFieldProvider { | ||
struct Cache {}; | ||
|
||
public: | ||
DD4hepFieldAdapter(dd4hep::OverlayedField field); | ||
|
||
MagneticFieldProvider::Cache makeCache( | ||
const Acts::MagneticFieldContext& mctx) const override; | ||
|
||
Result<Vector3> getField(const Vector3& position, | ||
MagneticFieldProvider::Cache& cache) const override; | ||
|
||
Result<Vector3> getFieldGradient( | ||
const Vector3& position, ActsMatrix<3, 3>& derivative, | ||
MagneticFieldProvider::Cache& cache) const override; | ||
|
||
private: | ||
double m_fieldConversionFactor; | ||
double m_lengthConversionFactor; | ||
std::unique_ptr<dd4hep::OverlayedField> m_field; | ||
}; | ||
|
||
} // namespace Acts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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/. | ||
|
||
#include "Acts/Plugins/DD4hep/DD4hepFieldAdapter.hpp" | ||
|
||
#include "Acts/Definitions/Algebra.hpp" | ||
#include "Acts/Definitions/Units.hpp" | ||
#include "Acts/MagneticField/MagneticFieldContext.hpp" | ||
#include "Acts/MagneticField/MagneticFieldError.hpp" | ||
#include "Acts/MagneticField/MagneticFieldProvider.hpp" | ||
|
||
#include <DD4hep/Fields.h> | ||
#include <DD4hep/Handle.h> | ||
#include <DD4hep/Objects.h> | ||
|
||
namespace Acts { | ||
|
||
DD4hepFieldAdapter::DD4hepFieldAdapter(dd4hep::OverlayedField field) | ||
: m_field{std::make_unique<dd4hep::OverlayedField>(field)} { | ||
m_fieldConversionFactor = | ||
dd4hep::_toDouble("1/tesla") * Acts::UnitConstants::T; | ||
m_lengthConversionFactor = | ||
dd4hep::_toDouble("1*mm") / Acts::UnitConstants::mm; | ||
} | ||
|
||
MagneticFieldProvider::Cache DD4hepFieldAdapter::makeCache( | ||
const Acts::MagneticFieldContext& /*mctx*/) const { | ||
return MagneticFieldProvider::Cache::make<Cache>(); | ||
} | ||
|
||
Result<Vector3> DD4hepFieldAdapter::getField( | ||
const Vector3& position, MagneticFieldProvider::Cache& /*cache*/) const { | ||
dd4hep::Position dd4hepPosition{position.x(), position.y(), position.z()}; | ||
|
||
// ACTS mm -> dd4hep mm | ||
dd4hepPosition *= m_lengthConversionFactor; | ||
|
||
const auto direction = m_field->combinedMagnetic(dd4hepPosition); | ||
|
||
Vector3 result{direction.x(), direction.y(), direction.z()}; | ||
|
||
// dd4hep tesla -> ACTS tesla | ||
result *= m_fieldConversionFactor; | ||
|
||
return Result<Vector3>::success(result); | ||
} | ||
|
||
Result<Vector3> DD4hepFieldAdapter::getFieldGradient( | ||
const Vector3& /*position*/, ActsMatrix<3, 3>& /*derivative*/, | ||
MagneticFieldProvider::Cache& /*cache*/) const { | ||
return Result<Vector3>::failure(MagneticFieldError::NotImplemented); | ||
} | ||
|
||
} // namespace Acts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters