Skip to content

Commit

Permalink
cleanup writers; try fix covfie
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Dec 5, 2024
1 parent b42f58d commit 9b1736d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 68 deletions.
29 changes: 2 additions & 27 deletions Core/include/Acts/MagneticField/InterpolatedBFieldMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ class InterpolatedMagneticField : public MagneticFieldProvider {
/// @return @c true if position is inside the defined look-up grid,
/// otherwise @c false
virtual bool isInside(const Vector3& position) const = 0;

/// Get a field value without checking if the lookup position is within the
/// interpolation domain.
///
/// @param position The lookup position in 3D
/// @return The field value at @p position
virtual Vector3 getFieldUnchecked(const Vector3& position) const = 0;
};

/// @ingroup MagneticField
Expand Down Expand Up @@ -186,7 +179,7 @@ class InterpolatedBFieldMap : public InterpolatedMagneticField {
///
/// @pre The given @c position must lie within the range of the underlying
/// magnetic field map.
std::optional<FieldCell> getFieldCell(const Vector3& position) const {
FieldCell getFieldCell(const Vector3& position) const {
const auto& gridPosition = m_cfg.transformPos(position);
const auto& indices = m_cfg.grid.localBinsFromPosition(gridPosition);
const auto& lowerLeft = m_cfg.grid.lowerLeftBinEdge(indices);
Expand All @@ -197,10 +190,6 @@ class InterpolatedBFieldMap : public InterpolatedMagneticField {
std::array<Vector3, nCorners> neighbors;
const auto& cornerIndices = m_cfg.grid.closestPointsIndices(gridPosition);

if (!isInsideLocal(gridPosition)) {
return std::nullopt;
}

std::size_t i = 0;
for (std::size_t index : cornerIndices) {
neighbors.at(i++) = m_cfg.transformBField(m_cfg.grid.at(index), position);
Expand Down Expand Up @@ -276,16 +265,6 @@ class InterpolatedBFieldMap : public InterpolatedMagneticField {
/// @pre The given @c position must lie within the range of the underlying
/// magnetic field map.
Vector3 getField(const Vector3& position) const {
const auto gridPosition = m_cfg.transformPos(position);
if (!isInsideLocal(gridPosition)) {
return Vector3::Zero();
}

return m_cfg.transformBField(m_cfg.grid.interpolate(gridPosition),
position);
}

Vector3 getFieldUnchecked(const Vector3& position) const final {
const auto gridPosition = m_cfg.transformPos(position);
return m_cfg.transformBField(m_cfg.grid.interpolate(gridPosition),
position);
Expand All @@ -297,11 +276,7 @@ class InterpolatedBFieldMap : public InterpolatedMagneticField {
Cache& lcache = cache.as<Cache>();
const auto gridPosition = m_cfg.transformPos(position);
if (!lcache.fieldCell || !(*lcache.fieldCell).isInside(gridPosition)) {
auto fieldCell = getFieldCell(position);
if (!fieldCell.has_value()) {
return Vector3::Zero();
}
lcache.fieldCell = *fieldCell;
lcache.fieldCell = getFieldCell(position);
}
return (*lcache.fieldCell).getField(gridPosition);
}
Expand Down
24 changes: 2 additions & 22 deletions Examples/Io/Csv/src/CsvBFieldWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "Acts/MagneticField/InterpolatedBFieldMap.hpp"
#include "Acts/MagneticField/MagneticFieldContext.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "Acts/Utilities/Result.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"
#include "ActsExamples/Io/Csv/CsvInputOutput.hpp"

Expand Down Expand Up @@ -132,17 +131,7 @@ void CsvBFieldWriter::run(const Config<Coord, Grid>& config,
Acts::Vector3 pos = {x * delta[0] + min[0], y * delta[1] + min[1],
z * delta[2] + min[2]};

Acts::Vector3 bField;
if (auto fieldMap =
dynamic_cast<const Acts::InterpolatedMagneticField*>(
&field)) {
// InterpolatedMagneticField::getField() returns an error for the
// final point (upper edge), which is just outside the field volume.
// So we use getFieldUnchecked instead.
bField = fieldMap->getFieldUnchecked(pos);
} else {
bField = field.getField(pos, cache);
}
Acts::Vector3 bField = field.getField(pos, cache);

writer.append(pos[0] / Acts::UnitConstants::mm,
pos[1] / Acts::UnitConstants::mm,
Expand Down Expand Up @@ -180,16 +169,7 @@ void CsvBFieldWriter::run(const Config<Coord, Grid>& config,
// the phi coordinate is zero. Then grab the field.
Acts::Vector3 pos(min[0] + r * delta[0], 0.f, min[1] + z * delta[1]);

Acts::Vector3 bField;
if (auto fieldMap =
dynamic_cast<const Acts::InterpolatedMagneticField*>(&field)) {
// InterpolatedMagneticField::getField() returns an error for the
// final point (upper edge), which is just outside the field volume.
// So we use getFieldUnchecked instead.
bField = fieldMap->getFieldUnchecked(pos);
} else {
bField = field.getField(pos, cache);
}
Acts::Vector3 bField = field.getField(pos, cache);

writer.append(
pos[0] / Acts::UnitConstants::mm, pos[2] / Acts::UnitConstants::mm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@

#pragma once

#include "Acts/Definitions/Units.hpp"
#include "Acts/MagneticField/InterpolatedBFieldMap.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/Framework/ProcessCode.hpp"

#include <array>
#include <cstddef>
#include <ios>
#include <memory>
#include <mutex>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>

#include <TFile.h>
Expand Down
12 changes: 5 additions & 7 deletions Examples/Io/Root/src/RootBFieldWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <sstream>
#include <stdexcept>
#include <utility>
#include <vector>

#include <TFile.h>
#include <TTree.h>
Expand All @@ -30,13 +29,12 @@ namespace ActsExamples {
void RootBFieldWriter::run(const Config& config,
std::unique_ptr<const Acts::Logger> p_logger) {
// Set up (local) logging
// @todo Remove dangerous using declaration once the logger macro
// tolerates it
using namespace Acts;
ACTS_LOCAL_LOGGER(std::move(p_logger))

Acts::MagneticFieldContext bFieldContext;

auto bFieldCache = config.bField->makeCache(bFieldContext);

// Check basic configuration
if (config.treeName.empty()) {
throw std::invalid_argument("Missing tree name");
Expand Down Expand Up @@ -179,7 +177,7 @@ void RootBFieldWriter::run(const Config& config,
for (std::size_t k = 0; k < nBinsZ; k++) {
double raw_z = minZ + k * stepZ;
Acts::Vector3 position(raw_x, raw_y, raw_z);
Vector3 bField = config.bField->getFieldUnchecked(position);
Acts::Vector3 bField = config.bField->getField(position, bFieldCache);

x = raw_x / Acts::UnitConstants::mm;
y = raw_y / Acts::UnitConstants::mm;
Expand Down Expand Up @@ -264,11 +262,11 @@ void RootBFieldWriter::run(const Config& config,
double raw_r = minR + j * stepR;
Acts::Vector3 position(raw_r, 0.0, raw_z); // position at phi=0
ACTS_VERBOSE("Requesting position: " << position.transpose());
auto bField = config.bField->getFieldUnchecked(position);
auto bField = config.bField->getField(position, bFieldCache);
z = raw_z / Acts::UnitConstants::mm;
r = raw_r / Acts::UnitConstants::mm;
Bz = bField.z() / Acts::UnitConstants::T;
Br = VectorHelpers::perp(bField) / Acts::UnitConstants::T;
Br = Acts::VectorHelpers::perp(bField) / Acts::UnitConstants::T;
outputTree->Fill();
} // for R
} // for z
Expand Down
7 changes: 2 additions & 5 deletions Tests/UnitTests/Plugins/Covfie/CovfieFieldConversionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ void checkMagneticFieldEqual(const Acts::MagneticFieldProvider& fieldProvider,
auto x = point[0], y = point[1], z = point[2];

auto lookupResult = fieldProvider.getField(Acts::Vector3{x, y, z}, cache);
if (!lookupResult.ok()) {
throw std::runtime_error{"Field lookup failure"};
}
auto actsValueX = (*lookupResult)[0], actsValueY = (*lookupResult)[1],
actsValueZ = (*lookupResult)[2];
auto actsValueX = lookupResult[0], actsValueY = lookupResult[1],
actsValueZ = lookupResult[2];

auto covfieValues = view.at(x, y, z);
auto covfieValueX = covfieValues[0], covfieValueY = covfieValues[1],
Expand Down

0 comments on commit 9b1736d

Please sign in to comment.