Skip to content

Commit

Permalink
refactor!: Remove overloads of B field retrieval without cache (#819)
Browse files Browse the repository at this point in the history
This PR removes the overloads of `getField` and `getFieldGradient` from `Acts::MagneticFieldProvider`. The individual field provider can still implement such overloads, but aren't forced to anymore by the provider. This only requires a few changes, but one is that it effectively renders `BFieldAccessExample` void of functionality, other than being a literal example of how to use the provider.

BREAKING CHANGE: `Acts::MagneticFieldProvider` loses the following pure virtual overloads:
```cpp
virtual Vector3 getField(const Vector3& position) const = 0; 
virtual Vector3 getFieldGradient(const Vector3& position, ActsMatrix<3, 3>& derivative) const = 0;
```
Clients of generic magnetic field providers need to be adapted.
  • Loading branch information
paulgessinger committed Jun 2, 2021
1 parent bff3735 commit 815bb72
Show file tree
Hide file tree
Showing 30 changed files with 77 additions and 234 deletions.
36 changes: 2 additions & 34 deletions Core/include/Acts/MagneticField/ConstantBField.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,8 @@ class ConstantBField final : public MagneticFieldProvider {
/// @param [in] B magnetic field vector in global coordinate system
explicit ConstantBField(Vector3 B) : m_BField(std::move(B)) {}

/// @brief construct constant magnetic field from components
///
/// @param [in] Bx magnetic field component in global x-direction
/// @param [in] By magnetic field component in global y-direction
/// @param [in] Bz magnetic field component in global z-direction
ConstantBField(double Bx = 0., double By = 0., double Bz = 0.)
: m_BField(Bx, By, Bz) {}

/// @copydoc MagneticFieldProvider::getField(const Vector3&)
///
/// @note The @p position is ignored and only kept as argument to provide
/// a consistent interface with other magnetic field services.
Vector3 getField(const Vector3& /*position*/) const override {
return m_BField;
}
/// @brief Get the B field at a position
Vector3 getField() const { return m_BField; }

/// @copydoc MagneticFieldProvider::getField(const
/// Vector3&,MagneticFieldProvider::Cache&)
Expand All @@ -56,18 +43,6 @@ class ConstantBField final : public MagneticFieldProvider {
return m_BField;
}

/// @copydoc MagneticFieldProvider::getFieldGradient(const
/// Vector3&,ActsMatrix<3,3>&)
///
/// @note The @p position is ignored and only kept as argument to provide
/// a consistent interface with other magnetic field services.
/// @note currently the derivative is not calculated
/// @todo return derivative
Vector3 getFieldGradient(const Vector3& /*position*/,
ActsMatrix<3, 3>& /*derivative*/) const override {
return m_BField;
}

/// @copydoc MagneticFieldProvider::getFieldGradient(const
/// Vector3&,ActsMatrix<3,3>&,MagneticFieldProvider::Cache&)
///
Expand Down Expand Up @@ -95,13 +70,6 @@ class ConstantBField final : public MagneticFieldProvider {
/// @note The method will always return true for the constant B-Field
bool isInside(const Vector3& /*position*/) const { return true; }

/// @brief update magnetic field vector from components
///
/// @param [in] Bx magnetic field component in global x-direction
/// @param [in] By magnetic field component in global y-direction
/// @param [in] Bz magnetic field component in global z-direction
void setField(double Bx, double By, double Bz) { m_BField << Bx, By, Bz; }

/// @brief update magnetic field vector
///
/// @param [in] B magnetic field vector in global coordinate system
Expand Down
16 changes: 4 additions & 12 deletions Core/include/Acts/MagneticField/InterpolatedBFieldMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ class InterpolatedBFieldMap final : public MagneticFieldProvider {
return MagneticFieldProvider::Cache::make<Cache>(mctx);
}

/// @copydoc MagneticFieldProvider::getField(const Vector3&)
Vector3 getField(const Vector3& position) const override {
/// @brief Get the B field at a position
///
/// @param position The position to query at
Vector3 getField(const Vector3& position) const {
return m_config.mapper.getField(position);
}

Expand All @@ -316,16 +318,6 @@ class InterpolatedBFieldMap final : public MagneticFieldProvider {
return (*cache.fieldCell).getField(position);
}

/// @copydoc MagneticFieldProvider::getFieldGradient(const
/// Vector3&,ActsMatrix<3,3>&)
///
/// @note currently the derivative is not calculated
/// @todo return derivative
Vector3 getFieldGradient(const Vector3& position,
ActsMatrix<3, 3>& /*derivative*/) const override {
return m_config.mapper.getField(position);
}

/// @copydoc MagneticFieldProvider::getFieldGradient(const
/// Vector3&,ActsMatrix<3,3>&,MagneticFieldProvider::Cache&)
///
Expand Down
17 changes: 0 additions & 17 deletions Core/include/Acts/MagneticField/MagneticFieldProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,6 @@ class MagneticFieldProvider {
/// @return magnetic field vector at given position
virtual Vector3 getField(const Vector3& position, Cache& cache) const = 0;

/// @brief retrieve magnetic field value
///
/// @param [in] position global 3D position
/// @param [in,out] cache Cache object. Contains field cell used for
/// interpolation
///
/// @return magnetic field vector at given position
virtual Vector3 getField(const Vector3& position) const = 0;

/// @brief retrieve magnetic field value & its gradient
///
/// @param [in] position global 3D position
/// @param [out] derivative gradient of magnetic field vector as (3x3) matrix
/// @return magnetic field vector
virtual Vector3 getFieldGradient(const Vector3& position,
ActsMatrix<3, 3>& derivative) const = 0;

/// @brief retrieve magnetic field value & its gradient
///
/// @param [in] position global 3D position
Expand Down
20 changes: 0 additions & 20 deletions Core/include/Acts/MagneticField/NullBField.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ class NullBField final : public MagneticFieldProvider {
/// @brief Default constructor
NullBField() = default;

/// @copydoc MagneticFieldProvider::getField(const Vector3&)
///
/// @note The @p position is ignored and only kept as argument to provide
/// a consistent interface with other magnetic field services.
Vector3 getField(const Vector3& /*position*/) const override {
return m_BField;
}

/// @copydoc MagneticFieldProvider::getField(const
/// Vector3&,MagneticFieldProvider::Cache&)
///
Expand All @@ -43,18 +35,6 @@ class NullBField final : public MagneticFieldProvider {
return m_BField;
}

/// @copydoc MagneticFieldProvider::getFieldGradient(const
/// Vector3&,ActsMatrix<3,3>&)
///
/// @note The @p position is ignored and only kept as argument to provide
/// a consistent interface with other magnetic field services.
/// @note currently the derivative is not calculated
/// @todo return derivative
Vector3 getFieldGradient(const Vector3& /*position*/,
ActsMatrix<3, 3>& /*derivative*/) const override {
return m_BField;
}

/// @copydoc MagneticFieldProvider::getFieldGradient(const
/// Vector3&,ActsMatrix<3,3>&,MagneticFieldProvider::Cache&)
///
Expand Down
8 changes: 5 additions & 3 deletions Core/include/Acts/MagneticField/SharedBField.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class SharedBField final : public MagneticFieldProvider {
/// @tparam bField is the shared BField to be stored
SharedBField(std::shared_ptr<const BField> bField) : m_bField(bField) {}

/// @copydoc MagneticFieldProvider::getField(const Vector3&)
Vector3 getField(const Vector3& position) const override {
/// @brief Get the B field at a position
///
/// @param position The position to query at
Vector3 getField(const Vector3& position) const {
return m_bField->getField(position);
}

Expand All @@ -48,7 +50,7 @@ class SharedBField final : public MagneticFieldProvider {
/// @copydoc MagneticFieldProvider::getFieldGradient(const
/// Vector3&,ActsMatrix<3,3>&)
Vector3 getFieldGradient(const Vector3& position,
ActsMatrix<3, 3>& derivative) const override {
ActsMatrix<3, 3>& derivative) const {
return m_bField->getFieldGradient(position, derivative);
}

Expand Down
14 changes: 4 additions & 10 deletions Core/include/Acts/MagneticField/SolenoidBField.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,16 @@ class SolenoidBField final : public MagneticFieldProvider {
MagneticFieldProvider::Cache makeCache(
const MagneticFieldContext& mctx) const override;

/// @copydoc MagneticFieldProvider::getField(const Vector3&)
Vector3 getField(const Vector3& position) const override;
/// @brief Get the B field at a position
///
/// @param position The position to query at
Vector3 getField(const Vector3& position) const;

/// @copydoc MagneticFieldProvider::getField(const
/// Vector3&,MagneticFieldProvider::Cache&)
Vector3 getField(const Vector3& position,
MagneticFieldProvider::Cache& /*cache*/) const override;

/// @copydoc MagneticFieldProvider::getFieldGradient(const
/// Vector3&,ActsMatrix<3,3>&)
///
/// @note currently the derivative is not calculated
/// @todo return derivative
Vector3 getFieldGradient(const Vector3& position,
ActsMatrix<3, 3>& /*derivative*/) const override;

/// @copydoc MagneticFieldProvider::getFieldGradient(const
/// Vector3&,ActsMatrix<3,3>&,MagneticFieldProvider::Cache&)
///
Expand Down
5 changes: 0 additions & 5 deletions Core/src/MagneticField/SolenoidBField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ Acts::Vector2 Acts::SolenoidBField::getField(const Vector2& position) const {
return multiCoilField(position, m_scale);
}

Acts::Vector3 Acts::SolenoidBField::getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& /*derivative*/) const {
return getField(position);
}

Acts::Vector3 Acts::SolenoidBField::getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& /*derivative*/,
MagneticFieldProvider::Cache& /*cache*/) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ ActsExamples::ProcessCode ActsExamples::TrackParamsEstimationAlgorithm::execute(
trackParameters.reserve(seeds.size());
tracks.reserve(seeds.size());

auto bCache = m_cfg.magneticField->makeCache(ctx.magFieldContext);

// Loop over all found seeds to estimate track parameters
for (size_t iseed = 0; iseed < seeds.size(); ++iseed) {
const auto& seed = seeds[iseed];
Expand All @@ -179,7 +181,7 @@ ActsExamples::ProcessCode ActsExamples::TrackParamsEstimationAlgorithm::execute(

// Get the magnetic field at the bottom space point
Acts::Vector3 field = m_cfg.magneticField->getField(
{bottomSP->x(), bottomSP->y(), bottomSP->z()});
{bottomSP->x(), bottomSP->y(), bottomSP->z()}, bCache);
// Estimate the track parameters from seed
auto optParams = Acts::estimateTrackParamsFromSeed(
ctx.geoContext, seed.sp().begin(), seed.sp().end(), *surface, field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ class ScalableBField final : public Acts::MagneticFieldProvider {
Acts::ActsScalar Bz = 0)
: m_BField(Bx, By, Bz) {}

/// @brief retrieve magnetic field value
///
/// @param [in] position global position
/// @return magnetic field vector
///
/// @note The @p position is ignored and only kept as argument to provide
/// a consistent interface with other magnetic field services.
Acts::Vector3 getField(const Acts::Vector3& /*position*/) const override {
return m_BField;
}

/// @brief retrieve magnetic field value
///
/// @param [in] position global position
Expand All @@ -70,23 +59,6 @@ class ScalableBField final : public Acts::MagneticFieldProvider {
return m_BField * cache.scalor;
}

/// @brief retrieve magnetic field value & its gradient
///
/// @param [in] position global position
/// @param [out] derivative gradient of magnetic field vector as (3x3)
/// matrix
/// @return magnetic field vector
///
/// @note The @p position is ignored and only kept as argument to provide
/// a consistent interface with other magnetic field services.
/// @note currently the derivative is not calculated
/// @todo return derivative
Acts::Vector3 getFieldGradient(
const Acts::Vector3& /*position*/,
Acts::ActsMatrix<3, 3>& /*derivative*/) const override {
return m_BField;
}

/// @brief retrieve magnetic field value & its gradient
///
/// @param [in] position global position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class RootBFieldWriter {
using namespace Acts;
ACTS_LOCAL_LOGGER(std::move(p_logger))

Acts::MagneticFieldContext bFieldContext;

// Check basic configuration
if (cfg.treeName.empty()) {
throw std::invalid_argument("Missing tree name");
Expand Down Expand Up @@ -285,6 +287,7 @@ class RootBFieldWriter {
stepZ = fabs(minZ - maxZ) / nBinsZ;
double stepPhi = (2 * M_PI) / nBinsPhi;

auto bCache = cfg.bField->makeCache(bFieldContext);
for (size_t i = 0; i < nBinsPhi; i++) {
double phi = minPhi + i * stepPhi;
for (size_t k = 0; k < nBinsZ; k++) {
Expand All @@ -293,7 +296,7 @@ class RootBFieldWriter {
double raw_r = minR + j * stepR;
Acts::Vector3 position(raw_r * cos(phi), raw_r * sin(phi), raw_z);
if (cfg.bField->isInside(position)) {
auto bField = cfg.bField->getField(position);
auto bField = cfg.bField->getField(position, bCache);
z = raw_z / Acts::UnitConstants::mm;
r = raw_r / Acts::UnitConstants::mm;
Bz = bField.z() / Acts::UnitConstants::T;
Expand Down
22 changes: 4 additions & 18 deletions Examples/Run/MagneticField/BFieldAccessExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void accessStepWise(const Acts::MagneticFieldProvider& bField,
double theta_step, size_t phi_steps, double phi_0,
double phi_step, size_t access_steps, double access_step) {
std::cout << "[>>>] Start: step-wise access pattern ... " << std::endl;
size_t mismatched = 0;
// initialize the field cache
auto bCache = bField.makeCache(bFieldContext);
// boost display
Expand All @@ -59,31 +58,24 @@ void accessStepWise(const Acts::MagneticFieldProvider& bField,
// now step through the magnetic field
for (size_t istep = 0; istep < access_steps; ++istep) {
Acts::Vector3 position = currentStep * dir;
// access the field directly
auto field_direct = bField.getField(position);
// access the field with the cell
auto field_from_cache = bField.getField(position, bCache);
// check
if (!field_direct.isApprox(field_from_cache)) {
++mismatched;
}
(void)field_from_cache; // we don't use this explicitly
// increase the step
currentStep += access_step;
// show the progress bar
++show_progress;
}
}
}
std::cout << "[<<<] End result : " << mismatched << "/" << totalSteps
<< " mismatches" << std::endl;
std::cout << "[<<<] End result: total steps:" << totalSteps << std::endl;
}
}

void accessRandom(const Acts::MagneticFieldProvider& bField,
const Acts::MagneticFieldContext& bFieldContext,
size_t totalSteps, double radius) {
std::cout << "[>>>] Start: random access pattern ... " << std::endl;
size_t mismatched = 0;
RandomEngine rng;
UniformDist xDist(-radius, radius);
UniformDist yDist(-radius, radius);
Expand All @@ -97,19 +89,13 @@ void accessRandom(const Acts::MagneticFieldProvider& bField,
// loop over the events - @todo move to parallel for
for (size_t istep = 0; istep < totalSteps; ++istep) {
Acts::Vector3 position(xDist(rng), yDist(rng), zDist(rng));
// access the field directly
auto field_direct = bField.getField(position);
// access the field with the cell
auto field_from_cache = bField.getField(position, bCache);
// check
if (!field_direct.isApprox(field_from_cache)) {
++mismatched;
}
(void)field_from_cache; // we don't use this explicitly
// show the progress bar
++show_progress;
}
std::cout << "[<<<] End result : " << mismatched << "/" << totalSteps
<< " mismatches" << std::endl;
std::cout << "[<<<] End result: total steps: " << totalSteps << std::endl;
}

/// @brief main executable
Expand Down
6 changes: 3 additions & 3 deletions Examples/Run/MagneticField/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ target_link_libraries(
Boost::program_options)

add_executable(
ActsExampleMagneticFieldAcess
ActsExampleMagneticFieldAccess
BFieldAccessExample.cpp)
target_link_libraries(
ActsExampleMagneticFieldAcess
ActsExampleMagneticFieldAccess
PRIVATE
ActsCore
ActsExamplesFramework ActsExamplesCommon
ActsExamplesMagneticField ActsExamplesIoRoot Boost::program_options)

install(
TARGETS ActsExampleMagneticField ActsExampleMagneticFieldAcess
TARGETS ActsExampleMagneticField ActsExampleMagneticFieldAccess
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
3 changes: 2 additions & 1 deletion Tests/Benchmarks/AtlasStepperBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ int main(int argc, char* argv[]) {
using Propagator_type = Propagator<Stepper_type>;
using Covariance = BoundSymMatrix;

auto bField = std::make_shared<BField_type>(0, 0, BzInT * UnitConstants::T);
auto bField =
std::make_shared<BField_type>(Vector3{0, 0, BzInT * UnitConstants::T});
Stepper_type atlas_stepper(std::move(bField));
Propagator_type propagator(std::move(atlas_stepper));

Expand Down
3 changes: 2 additions & 1 deletion Tests/Benchmarks/EigenStepperBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ int main(int argc, char* argv[]) {
using Propagator_type = Propagator<Stepper_type>;
using Covariance = BoundSymMatrix;

auto bField = std::make_shared<BField_type>(0, 0, BzInT * UnitConstants::T);
auto bField =
std::make_shared<BField_type>(Vector3{0, 0, BzInT * UnitConstants::T});
Stepper_type atlas_stepper(std::move(bField));
Propagator_type propagator(std::move(atlas_stepper));

Expand Down

0 comments on commit 815bb72

Please sign in to comment.