Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-13 08:19:54

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/Utilities/Enumerate.hpp"
0014 #include "ActsPlugins/Root/RootMagneticFieldIo.hpp"
0015 #include "ActsTests/CommonHelpers/TemporaryDirectory.hpp"
0016 
0017 #include <fstream>
0018 
0019 #include <TFile.h>
0020 #include <TTree.h>
0021 
0022 namespace bdata = boost::unit_test::data;
0023 
0024 using namespace Acts;
0025 using namespace ActsPlugins;
0026 using namespace Acts::UnitLiterals;
0027 
0028 namespace ActsTests {
0029 
0030 BOOST_AUTO_TEST_SUITE(RootSuite)
0031 
0032 BOOST_AUTO_TEST_CASE(InterpolatedBFieldMap_rz_from_root) {
0033   TemporaryDirectory tmp{};
0034 
0035   auto fileName = tmp.path() / "rz_magfield.root";
0036   std::string fieldName = "BFieldMap";
0037   // Create the file
0038   auto rzFile = TFile::Open(fileName.c_str(), "RECREATE");
0039   auto tree = new TTree(fieldName.c_str(), "BFieldMap");
0040   double r = 0.;
0041   double z = 0.;
0042   double Br = 0.;
0043   double Bz = 0.;
0044   tree->Branch("r", &r, "r/D");
0045   tree->Branch("z", &z, "z/D");
0046   tree->Branch("Br", &Br, "Br/D");
0047   tree->Branch("Bz", &Bz, "Bz/D");
0048   for (auto rv : {0., 0.1, 0.2, 0.3, 0.4, 0.5}) {
0049     for (auto zv : {-2., -1., 0., 1., 2.}) {
0050       r = rv;
0051       z = zv;
0052       Br = r * 0.1;
0053       Bz = z * 0.1 + 2.;
0054       tree->Fill();
0055     }
0056   }
0057   tree->Write();
0058   rzFile->Close();
0059 
0060   // Now read it back in
0061   auto rzField = makeMagneticFieldMapRzFromRoot(
0062       [](std::array<std::size_t, 2> binsRZ,
0063          std::array<std::size_t, 2> nBinsRZ) {
0064         return (binsRZ.at(0) * nBinsRZ.at(1) + binsRZ.at(1));
0065       },
0066       fileName, fieldName, 1_mm, 1_T, false);
0067 
0068   // Check that the bfield is two dimenstional
0069   auto nBins = rzField.getNBins();
0070   BOOST_CHECK_EQUAL(nBins.size(), 2u);
0071 
0072   // Check number of bins in r and z
0073   BOOST_CHECK_EQUAL(nBins.at(0), 6u);
0074   BOOST_CHECK_EQUAL(nBins.at(1), 5u);
0075 
0076   // Check that the bin edges are correct
0077   auto mins = rzField.getMin();
0078   auto maxs = rzField.getMax();
0079   BOOST_CHECK_EQUAL(mins.size(), 2u);
0080   BOOST_CHECK_EQUAL(maxs.size(), 2u);
0081 }
0082 
0083 BOOST_AUTO_TEST_CASE(InterpolatedBFieldMap_xyz_from_root) {
0084   TemporaryDirectory tmp{};
0085 
0086   for (auto fOctant : {true, false}) {
0087     std::string octantSuffix = fOctant ? "octant" : "full";
0088 
0089     std::string baseFileName = "xyz_magfield" + octantSuffix + ".root";
0090     auto fileName = tmp.path() / baseFileName;
0091     std::string fieldName = "BFieldMap";
0092 
0093     // Create the file
0094     auto xyzFile = TFile::Open(fileName.c_str(), "RECREATE");
0095     auto tree = new TTree(fieldName.c_str(), "BFieldMap");
0096 
0097     double x = 0.;
0098     double y = 0.;
0099     double z = 0.;
0100     double Bx = 0.;
0101     double By = 0.;
0102     double Bz = 0.;
0103     tree->Branch("x", &x, "x/D");
0104     tree->Branch("y", &y, "y/D");
0105     tree->Branch("z", &z, "z/D");
0106     tree->Branch("Bx", &Bx, "Bx/D");
0107     tree->Branch("By", &By, "By/D");
0108     tree->Branch("Bz", &Bz, "Bz/D");
0109     std::vector<double> xvals = {0., 0.5, 1.};
0110     std::vector<double> yvals = {0., 1., 2.};
0111     std::vector<double> zvals = {0., 1., 2., 3.};
0112     if (!fOctant) {
0113       xvals = {-1., -0.5, 0., 0.5, 1.};
0114       yvals = {-2., -1., 0., 1., 2.};
0115       zvals = {-3., -2., -1., 0., 1., 2., 3.};
0116     }
0117     for (auto xv : xvals) {
0118       for (auto yv : yvals) {
0119         for (auto zv : zvals) {
0120           x = xv;
0121           y = yv;
0122           z = zv;
0123           Bx = x * 0.1;
0124           By = y * 0.1;
0125           Bz = z * 0.1 + 2.;
0126           tree->Fill();
0127         }
0128       }
0129     }
0130 
0131     tree->Write();
0132     xyzFile->Close();
0133 
0134     // Now read it back in
0135     auto xyzField = makeMagneticFieldMapXyzFromRoot(
0136         [](std::array<std::size_t, 3> binsXYZ,
0137            std::array<std::size_t, 3> nBinsXYZ) {
0138           return (binsXYZ.at(0) * nBinsXYZ.at(1) * nBinsXYZ.at(2) +
0139                   binsXYZ.at(1) * nBinsXYZ.at(2) + binsXYZ.at(2));
0140         },
0141         fileName, fieldName, 1_mm, 1_T, fOctant);
0142 
0143     // Check that the bfield is three-dimenstional
0144     auto nBins = xyzField.getNBins();
0145     BOOST_CHECK_EQUAL(nBins.size(), 3u);
0146 
0147     // Check number of bins in x, y and z
0148     BOOST_CHECK_EQUAL(nBins.at(0), 5u);
0149     BOOST_CHECK_EQUAL(nBins.at(1), 5u);
0150     BOOST_CHECK_EQUAL(nBins.at(2), 7u);
0151   }
0152 }
0153 
0154 BOOST_AUTO_TEST_SUITE_END()
0155 
0156 }  // namespace ActsTests