Skip to content

Commit

Permalink
feat: add Python bindings for MagneticFieldMap (#1251)
Browse files Browse the repository at this point in the history
* Adds missing Python bindings for `MagneticFieldMapXyz` and `MagneticFieldMapRz` to allow the field map to be read from a file.
* Add example use in `full_chain_itk.py`. Previously used a constant B-field.
* Also show different seeding options, and debug setting, commented out in `full_chain_itk.py`.
  • Loading branch information
timadye authored May 23, 2022
1 parent 63f33a3 commit 6e64a4c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Examples/Detectors/MagneticField/src/FieldMapRootIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 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 "FieldMapRootIo.hpp"
#include "ActsExamples/MagneticField/FieldMapRootIo.hpp"

#include "Acts/MagneticField/BFieldMapUtils.hpp"

Expand Down
2 changes: 1 addition & 1 deletion Examples/Detectors/MagneticField/src/FieldMapTextIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 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 "FieldMapTextIo.hpp"
#include "ActsExamples/MagneticField/FieldMapTextIo.hpp"

#include "Acts/MagneticField/BFieldMapUtils.hpp"

Expand Down
5 changes: 2 additions & 3 deletions Examples/Detectors/MagneticField/src/MagneticFieldOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "Acts/MagneticField/SolenoidBField.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/Framework/Sequencer.hpp"
#include "ActsExamples/MagneticField/FieldMapRootIo.hpp"
#include "ActsExamples/MagneticField/FieldMapTextIo.hpp"
#include "ActsExamples/MagneticField/ScalableBFieldService.hpp"
#include "ActsExamples/Utilities/Options.hpp"

Expand All @@ -24,9 +26,6 @@
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>

#include "FieldMapRootIo.hpp"
#include "FieldMapTextIo.hpp"

void ActsExamples::Options::addMagneticFieldOptions(Description& desc) {
using boost::program_options::bool_switch;
using boost::program_options::value;
Expand Down
72 changes: 70 additions & 2 deletions Examples/Python/src/MagneticField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
#include "Acts/MagneticField/NullBField.hpp"
#include "Acts/MagneticField/SolenoidBField.hpp"
#include "Acts/Plugins/Python/Utilities.hpp"
#include "ActsExamples/MagneticField/FieldMapRootIo.hpp"
#include "ActsExamples/MagneticField/FieldMapTextIo.hpp"

#include <memory>

#include <boost/filesystem.hpp>
#include <pybind11/pybind11.h>

namespace py = pybind11;
Expand All @@ -43,12 +46,12 @@ void addMagneticField(Context& ctx) {
.def(py::init<Acts::Vector3>());

py::class_<ActsExamples::detail::InterpolatedMagneticField2,
Acts::InterpolatedMagneticField,
Acts::InterpolatedMagneticField, Acts::MagneticFieldProvider,
std::shared_ptr<ActsExamples::detail::InterpolatedMagneticField2>>(
mex, "InterpolatedMagneticField2");

py::class_<ActsExamples::detail::InterpolatedMagneticField3,
Acts::InterpolatedMagneticField,
Acts::InterpolatedMagneticField, Acts::MagneticFieldProvider,
std::shared_ptr<ActsExamples::detail::InterpolatedMagneticField3>>(
mex, "InterpolatedMagneticField3");

Expand Down Expand Up @@ -78,6 +81,71 @@ void addMagneticField(Context& ctx) {
.def_readwrite("nCoils", &Config::nCoils)
.def_readwrite("bMagCenter", &Config::bMagCenter);
}

mex.def(
"MagneticFieldMapXyz",
[](std::string filename, std::string tree, double lengthUnit,
double BFieldUnit, bool firstOctant) {
const boost::filesystem::path file = filename;

auto mapBins = [](std::array<size_t, 3> bins,
std::array<size_t, 3> sizes) {
return (bins[0] * (sizes[1] * sizes[2]) + bins[1] * sizes[2] +
bins[2]);
};

if (file.extension() == ".root") {
auto map = ActsExamples::makeMagneticFieldMapXyzFromRoot(
std::move(mapBins), file.native(), tree, lengthUnit, BFieldUnit,
firstOctant);
return std::make_shared<
ActsExamples::detail::InterpolatedMagneticField3>(std::move(map));
} else if (file.extension() == ".txt") {
auto map = ActsExamples::makeMagneticFieldMapXyzFromText(
std::move(mapBins), file.native(), lengthUnit, BFieldUnit,
firstOctant);
return std::make_shared<
ActsExamples::detail::InterpolatedMagneticField3>(std::move(map));
} else {
throw std::runtime_error("Unsupported magnetic field map file type");
}
},
py::arg("file"), py::arg("tree") = "bField",
py::arg("lengthUnit") = Acts::UnitConstants::mm,
py::arg("BFieldUnit") = Acts::UnitConstants::T,
py::arg("firstOctant") = false);

mex.def(
"MagneticFieldMapRz",
[](std::string filename, std::string tree, double lengthUnit,
double BFieldUnit, bool firstQuadrant) {
const boost::filesystem::path file = filename;

auto mapBins = [](std::array<size_t, 2> bins,
std::array<size_t, 2> sizes) {
return (bins[1] * sizes[0] + bins[0]);
};

if (file.extension() == ".root") {
auto map = ActsExamples::makeMagneticFieldMapRzFromRoot(
std::move(mapBins), file.native(), tree, lengthUnit, BFieldUnit,
firstQuadrant);
return std::make_shared<
ActsExamples::detail::InterpolatedMagneticField2>(std::move(map));
} else if (file.extension() == ".txt") {
auto map = ActsExamples::makeMagneticFieldMapRzFromText(
std::move(mapBins), file.native(), lengthUnit, BFieldUnit,
firstQuadrant);
return std::make_shared<
ActsExamples::detail::InterpolatedMagneticField2>(std::move(map));
} else {
throw std::runtime_error("Unsupported magnetic field map file type");
}
},
py::arg("file"), py::arg("tree") = "bField",
py::arg("lengthUnit") = Acts::UnitConstants::mm,
py::arg("BFieldUnit") = Acts::UnitConstants::T,
py::arg("firstQuadrant") = false);
}

} // namespace Acts::Python
2 changes: 1 addition & 1 deletion Examples/Scripts/Python/full_chain_itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# acts.examples.dump_args_calls(locals())
detector, trackingGeometry, decorators = itk.buildITkGeometry(geo_dir)
field = acts.ConstantBField(acts.Vector3(0.0, 0.0, 2.0 * u.T))
field = acts.examples.MagneticFieldMapXyz(str(geo_dir / "bfield/ATLAS-BField-xyz.root"))
rnd = acts.examples.RandomNumbers(seed=42)

from particle_gun import addParticleGun, MomentumConfig, EtaConfig, ParticleConfig
Expand Down

0 comments on commit 6e64a4c

Please sign in to comment.