Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MagneticField.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MagneticField.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
20 
21 #include <array>
22 #include <cstddef>
23 #include <filesystem>
24 #include <memory>
25 #include <stdexcept>
26 #include <string>
27 #include <tuple>
28 #include <type_traits>
29 #include <utility>
30 
31 #include <pybind11/pybind11.h>
32 #include <pybind11/stl.h>
33 
34 namespace py = pybind11;
35 using namespace pybind11::literals;
36 
37 namespace Acts::Python {
38 
40  auto [m, mex, prop] = ctx.get("main", "examples", "propagation");
41 
42  py::class_<Acts::MagneticFieldProvider,
43  std::shared_ptr<Acts::MagneticFieldProvider>>(
44  m, "MagneticFieldProvider");
45 
47  std::shared_ptr<Acts::InterpolatedMagneticField>>(
48  m, "InterpolatedMagneticField");
49 
50  m.def("solenoidFieldMap", &Acts::solenoidFieldMap, py::arg("rlim"),
51  py::arg("zlim"), py::arg("nbins"), py::arg("field"));
52 
53  py::class_<Acts::ConstantBField, Acts::MagneticFieldProvider,
54  std::shared_ptr<Acts::ConstantBField>>(m, "ConstantBField")
55  .def(py::init<Acts::Vector3>());
56 
58  Acts::InterpolatedMagneticField, Acts::MagneticFieldProvider,
59  std::shared_ptr<ActsExamples::detail::InterpolatedMagneticField2>>(
60  mex, "InterpolatedMagneticField2");
61 
63  Acts::InterpolatedMagneticField, Acts::MagneticFieldProvider,
64  std::shared_ptr<ActsExamples::detail::InterpolatedMagneticField3>>(
65  mex, "InterpolatedMagneticField3");
66 
67  py::class_<Acts::NullBField, Acts::MagneticFieldProvider,
68  std::shared_ptr<Acts::NullBField>>(m, "NullBField")
69  .def(py::init<>());
70 
71  {
73 
74  auto sol =
75  py::class_<Acts::SolenoidBField, Acts::MagneticFieldProvider,
76  std::shared_ptr<Acts::SolenoidBField>>(m, "SolenoidBField")
77  .def(py::init<Config>())
78  .def(py::init([](double radius, double length, size_t nCoils,
79  double bMagCenter) {
80  return Acts::SolenoidBField{
81  Config{radius, length, nCoils, bMagCenter}};
82  }),
83  py::arg("radius"), py::arg("length"), py::arg("nCoils"),
84  py::arg("bMagCenter"));
85 
86  py::class_<Config>(sol, "Config")
87  .def(py::init<>())
88  .def_readwrite("radius", &Config::radius)
89  .def_readwrite("length", &Config::length)
90  .def_readwrite("nCoils", &Config::nCoils)
91  .def_readwrite("bMagCenter", &Config::bMagCenter);
92  }
93 
94  mex.def(
95  "MagneticFieldMapXyz",
96  [](const std::string& filename, const std::string& tree,
97  double lengthUnit, double BFieldUnit, bool firstOctant) {
99 
100  auto mapBins = [](std::array<size_t, 3> bins,
101  std::array<size_t, 3> sizes) {
102  return (bins[0] * (sizes[1] * sizes[2]) + bins[1] * sizes[2] +
103  bins[2]);
104  };
105 
106  if (file.extension() == ".root") {
108  std::move(mapBins), file.native(), tree, lengthUnit, BFieldUnit,
109  firstOctant);
110  return std::make_shared<
111  ActsExamples::detail::InterpolatedMagneticField3>(std::move(map));
112  } else if (file.extension() == ".txt") {
114  std::move(mapBins), file.native(), lengthUnit, BFieldUnit,
115  firstOctant);
116  return std::make_shared<
117  ActsExamples::detail::InterpolatedMagneticField3>(std::move(map));
118  } else {
119  throw std::runtime_error("Unsupported magnetic field map file type");
120  }
121  },
122  py::arg("file"), py::arg("tree") = "bField",
123  py::arg("lengthUnit") = Acts::UnitConstants::mm,
124  py::arg("BFieldUnit") = Acts::UnitConstants::T,
125  py::arg("firstOctant") = false);
126 
127  mex.def(
128  "MagneticFieldMapRz",
129  [](const std::string& filename, const std::string& tree,
130  double lengthUnit, double BFieldUnit, bool firstQuadrant) {
132 
133  auto mapBins = [](std::array<size_t, 2> bins,
134  std::array<size_t, 2> sizes) {
135  return (bins[1] * sizes[0] + bins[0]);
136  };
137 
138  if (file.extension() == ".root") {
140  std::move(mapBins), file.native(), tree, lengthUnit, BFieldUnit,
141  firstQuadrant);
142  return std::make_shared<
143  ActsExamples::detail::InterpolatedMagneticField2>(std::move(map));
144  } else if (file.extension() == ".txt") {
146  std::move(mapBins), file.native(), lengthUnit, BFieldUnit,
147  firstQuadrant);
148  return std::make_shared<
149  ActsExamples::detail::InterpolatedMagneticField2>(std::move(map));
150  } else {
151  throw std::runtime_error("Unsupported magnetic field map file type");
152  }
153  },
154  py::arg("file"), py::arg("tree") = "bField",
155  py::arg("lengthUnit") = Acts::UnitConstants::mm,
156  py::arg("BFieldUnit") = Acts::UnitConstants::T,
157  py::arg("firstQuadrant") = false);
158 }
159 
160 } // namespace Acts::Python