From e3e86c96fc5293569b63087365de7bfa63939b79 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Mon, 16 Sep 2024 17:42:28 +0200 Subject: [PATCH 01/13] refactor --- Core/src/MagneticField/BFieldMapUtils.cpp | 232 +++++++++------------- Core/src/Material/MaterialMapUtils.cpp | 117 ++++------- 2 files changed, 131 insertions(+), 218 deletions(-) diff --git a/Core/src/MagneticField/BFieldMapUtils.cpp b/Core/src/MagneticField/BFieldMapUtils.cpp index bffdc7520ef..ada71581d3e 100644 --- a/Core/src/MagneticField/BFieldMapUtils.cpp +++ b/Core/src/MagneticField/BFieldMapUtils.cpp @@ -27,6 +27,28 @@ using Acts::VectorHelpers::perp; using Acts::VectorHelpers::phi; +namespace { +auto getMinMaxAndBinCount(std::vector& xPos) { + // sort the values for unique() + std::ranges::sort(xPos); + + // get the number of bins over unique values + const std::size_t xBinCount = + std::distance(xPos.begin(), std::ranges::unique(xPos).end()); + + // get the minimum and maximum + auto [xMin, xMax] = std::ranges::minmax(xPos); + + // calculate maxima (add one last bin, because bin value always corresponds to + // left boundary) + const double stepX = (xMax - xMin) / (xBinCount - 1); + xMax += stepX; + + // Return all values as a tuple + return std::make_tuple(xMin, xMax, xBinCount); +} +} // anonymous namespace + Acts::InterpolatedBFieldMap< Acts::Grid, Acts::Axis>> @@ -38,33 +60,15 @@ Acts::fieldMapRZ( std::vector bField, double lengthUnit, double BFieldUnit, bool firstQuadrant) { // [1] Create Grid - // sort the values - std::ranges::sort(rPos); - std::ranges::sort(zPos); - // Get unique values - rPos.erase(std::unique(rPos.begin(), rPos.end()), rPos.end()); - zPos.erase(std::unique(zPos.begin(), zPos.end()), zPos.end()); - rPos.shrink_to_fit(); - zPos.shrink_to_fit(); - // get the number of bins - std::size_t nBinsR = rPos.size(); - std::size_t nBinsZ = zPos.size(); - - // get the minimum and maximum. We just sorted the vectors, so these are just - // the first and last elements. - double rMin = rPos[0]; - double zMin = zPos[0]; - double rMax = rPos[nBinsR - 1]; - double zMax = zPos[nBinsZ - 1]; - // calculate maxima (add one last bin, because bin value always corresponds to - // left boundary) - double stepZ = std::fabs(zMax - zMin) / (nBinsZ - 1); - double stepR = std::fabs(rMax - rMin) / (nBinsR - 1); - rMax += stepR; - zMax += stepZ; + const auto [rMin, rMax, rBinCount] = getMinMaxAndBinCount(rPos); + auto [zMin, zMax, zBinCount] = getMinMaxAndBinCount(zPos); + + const std::size_t nBinsR = rBinCount; + std::size_t nBinsZ = zBinCount; + if (firstQuadrant) { zMin = -zPos[nBinsZ - 1]; - nBinsZ = static_cast(2. * nBinsZ - 1); + nBinsZ = 2 * nBinsZ - 1; } // Create the axis for the grid @@ -76,25 +80,20 @@ Acts::fieldMapRZ( using Grid_t = decltype(grid); // [2] Set the bField values + const std::array nIndices = {{rBinCount, zBinCount}}; for (std::size_t i = 1; i <= nBinsR; ++i) { for (std::size_t j = 1; j <= nBinsZ; ++j) { - std::array nIndices = {{rPos.size(), zPos.size()}}; Grid_t::index_t indices = {{i, j}}; + // std::vectors begin with 0 and we do not want the user needing to take + // underflow or overflow bins in account this is why we need to subtract + // by one if (firstQuadrant) { - // std::vectors begin with 0 and we do not want the user needing to - // take underflow or overflow bins in account this is why we need to - // subtract by one std::size_t n = - std::abs(static_cast(j) - static_cast(zPos.size())); - Grid_t::index_t indicesFirstQuadrant = {{i - 1, n}}; + std::abs(static_cast(j) - static_cast(zBinCount)); grid.atLocalBins(indices) = - bField.at(localToGlobalBin(indicesFirstQuadrant, nIndices)) * - BFieldUnit; + bField.at(localToGlobalBin({{i - 1, n}}, nIndices)) * BFieldUnit; } else { - // std::vectors begin with 0 and we do not want the user needing to - // take underflow or overflow bins in account this is why we need to - // subtract by one grid.atLocalBins(indices) = bField.at(localToGlobalBin({{i - 1, j - 1}}, nIndices)) * BFieldUnit; @@ -103,31 +102,28 @@ Acts::fieldMapRZ( } grid.setExteriorBins(Acts::Vector2::Zero()); - // [3] Create the transformation for the position - // map (x,y,z) -> (r,z) + // [3] Create the transformation for the position map (x,y,z) -> (r,z) auto transformPos = [](const Acts::Vector3& pos) { return Acts::Vector2(perp(pos), pos.z()); }; - // [4] Create the transformation for the bfield - // map (Br,Bz) -> (Bx,By,Bz) + // [4] Create the transformation for the bField map (Br,Bz) -> (Bx,By,Bz) auto transformBField = [](const Acts::Vector2& field, const Acts::Vector3& pos) { - double r_sin_theta_2 = pos.x() * pos.x() + pos.y() * pos.y(); - double cos_phi = 0, sin_phi = 0; - if (r_sin_theta_2 > std::numeric_limits::min()) { - double inv_r_sin_theta = 1. / sqrt(r_sin_theta_2); - cos_phi = pos.x() * inv_r_sin_theta; - sin_phi = pos.y() * inv_r_sin_theta; - } else { - cos_phi = 1.; - sin_phi = 0.; + const double rSinTheta2 = pos.x() * pos.x() + pos.y() * pos.y(); + double cosPhi = 1.; + double sinPhi = 0.; + + if (rSinTheta2 > std::numeric_limits::min()) { + const double invRsinTheta = 1. / sqrt(rSinTheta2); + cosPhi = pos.x() * invRsinTheta; + sinPhi = pos.y() * invRsinTheta; } - return Acts::Vector3(field.x() * cos_phi, field.x() * sin_phi, field.y()); + + return Acts::Vector3(field.x() * cosPhi, field.x() * sinPhi, field.y()); }; - // [5] Create the mapper & BField Service - // create field mapping + // [5] Create the mapper & BField Service create field mapping return Acts::InterpolatedBFieldMap( {transformPos, transformBField, std::move(grid)}); } @@ -144,50 +140,23 @@ Acts::fieldMapXYZ( std::vector zPos, std::vector bField, double lengthUnit, double BFieldUnit, bool firstOctant) { // [1] Create Grid - // Sort the values - std::ranges::sort(xPos); - std::ranges::sort(yPos); - std::ranges::sort(zPos); - // Get unique values - xPos.erase(std::unique(xPos.begin(), xPos.end()), xPos.end()); - yPos.erase(std::unique(yPos.begin(), yPos.end()), yPos.end()); - zPos.erase(std::unique(zPos.begin(), zPos.end()), zPos.end()); - xPos.shrink_to_fit(); - yPos.shrink_to_fit(); - zPos.shrink_to_fit(); - // get the number of bins - std::size_t nBinsX = xPos.size(); - std::size_t nBinsY = yPos.size(); - std::size_t nBinsZ = zPos.size(); + auto [xMin, xMax, xBinCount] = getMinMaxAndBinCount(xPos); + auto [yMin, yMax, yBinCount] = getMinMaxAndBinCount(yPos); + auto [zMin, zMax, zBinCount] = getMinMaxAndBinCount(zPos); - // Create the axis for the grid - // get minima and maximia. We just sorted the vectors, so these are just the - // first and last elements. - double xMin = xPos[0]; - double yMin = yPos[0]; - double zMin = zPos[0]; - // get maxima - double xMax = xPos[nBinsX - 1]; - double yMax = yPos[nBinsY - 1]; - double zMax = zPos[nBinsZ - 1]; - // calculate maxima (add one last bin, because bin value always corresponds to - // left boundary) - double stepZ = std::fabs(zMax - zMin) / (nBinsZ - 1); - double stepY = std::fabs(yMax - yMin) / (nBinsY - 1); - double stepX = std::fabs(xMax - xMin) / (nBinsX - 1); - xMax += stepX; - yMax += stepY; - zMax += stepZ; + std::size_t nBinsX = xBinCount; + std::size_t nBinsY = yBinCount; + std::size_t nBinsZ = zBinCount; - // If only the first octant is given if (firstOctant) { xMin = -xPos[nBinsX - 1]; - yMin = -yPos[nBinsY - 1]; - zMin = -zPos[nBinsZ - 1]; nBinsX = 2 * nBinsX - 1; + yMin = -yPos[nBinsY - 1]; nBinsY = 2 * nBinsY - 1; + zMin = -zPos[nBinsZ - 1]; nBinsZ = 2 * nBinsZ - 1; } + Acts::Axis xAxis(xMin * lengthUnit, xMax * lengthUnit, nBinsX); Acts::Axis yAxis(yMin * lengthUnit, yMax * lengthUnit, nBinsY); Acts::Axis zAxis(zMin * lengthUnit, zMax * lengthUnit, nBinsZ); @@ -197,32 +166,26 @@ Acts::fieldMapXYZ( using Grid_t = decltype(grid); // [2] Set the bField values + const std::array nIndices = { + {xBinCount, yBinCount, zBinCount}}; for (std::size_t i = 1; i <= nBinsX; ++i) { for (std::size_t j = 1; j <= nBinsY; ++j) { for (std::size_t k = 1; k <= nBinsZ; ++k) { Grid_t::index_t indices = {{i, j, k}}; - std::array nIndices = { - {xPos.size(), yPos.size(), zPos.size()}}; + // std::vectors begin with 0 and we do not want the user needing to take + // underflow or overflow bins in account this is why we need to subtract + // by one if (firstOctant) { - // std::vectors begin with 0 and we do not want the user needing to - // take underflow or overflow bins in account this is why we need to - // subtract by one + std::size_t l = + std::abs(static_cast(i) - static_cast(xBinCount)); std::size_t m = - std::abs(static_cast(i) - (static_cast(xPos.size()))); + std::abs(static_cast(j) - static_cast(yBinCount)); std::size_t n = - std::abs(static_cast(j) - (static_cast(yPos.size()))); - std::size_t l = - std::abs(static_cast(k) - (static_cast(zPos.size()))); - Grid_t::index_t indicesFirstOctant = {{m, n, l}}; + std::abs(static_cast(k) - static_cast(zBinCount)); grid.atLocalBins(indices) = - bField.at(localToGlobalBin(indicesFirstOctant, nIndices)) * - BFieldUnit; - + bField.at(localToGlobalBin({{l, m, n}}, nIndices)) * BFieldUnit; } else { - // std::vectors begin with 0 and we do not want the user needing to - // take underflow or overflow bins in account this is why we need to - // subtract by one grid.atLocalBins(indices) = bField.at(localToGlobalBin({{i - 1, j - 1, k - 1}}, nIndices)) * BFieldUnit; @@ -232,17 +195,14 @@ Acts::fieldMapXYZ( } grid.setExteriorBins(Acts::Vector3::Zero()); - // [3] Create the transformation for the position - // map (x,y,z) -> (r,z) + // [3] Create the transformation for the position map (x,y,z) -> (r,z) auto transformPos = [](const Acts::Vector3& pos) { return pos; }; - // [4] Create the transformation for the bfield - // map (Bx,By,Bz) -> (Bx,By,Bz) + // [4] Create the transformation for the BField map (Bx,By,Bz) -> (Bx,By,Bz) auto transformBField = [](const Acts::Vector3& field, const Acts::Vector3& /*pos*/) { return field; }; - // [5] Create the mapper & BField Service - // create field mapping + // [5] Create the mapper & BField Service create field mapping return Acts::InterpolatedBFieldMap( {transformPos, transformBField, std::move(grid)}); } @@ -250,20 +210,16 @@ Acts::fieldMapXYZ( Acts::InterpolatedBFieldMap< Acts::Grid, Acts::Axis>> -Acts::solenoidFieldMap(std::pair rlim, - std::pair zlim, - std::pair nbins, +Acts::solenoidFieldMap(std::pair rLim, + std::pair zLim, + std::pair nBins, const SolenoidBField& field) { - double rMin = 0, rMax = 0, zMin = 0, zMax = 0; - std::tie(rMin, rMax) = rlim; - std::tie(zMin, zMax) = zlim; - - std::size_t nBinsR = 0, nBinsZ = 0; - std::tie(nBinsR, nBinsZ) = nbins; + auto [rMin, rMax] = rLim; + auto [zMin, zMax] = zLim; + const auto [nBinsR, nBinsZ] = nBins; double stepZ = std::abs(zMax - zMin) / (nBinsZ - 1); double stepR = std::abs(rMax - rMin) / (nBinsR - 1); - rMax += stepR; zMax += stepZ; @@ -275,32 +231,29 @@ Acts::solenoidFieldMap(std::pair rlim, Grid grid(Type, std::move(rAxis), std::move(zAxis)); using Grid_t = decltype(grid); - // Create the transformation for the position - // map (x,y,z) -> (r,z) + // Create the transformation for the position map (x,y,z) -> (r,z) auto transformPos = [](const Acts::Vector3& pos) { return Acts::Vector2(perp(pos), pos.z()); }; - // Create the transformation for the bfield - // map (Br,Bz) -> (Bx,By,Bz) - auto transformBField = [](const Acts::Vector2& bfield, + // Create the transformation for the bField map (Br,Bz) -> (Bx,By,Bz) + auto transformBField = [](const Acts::Vector2& bField, const Acts::Vector3& pos) { - double r_sin_theta_2 = pos.x() * pos.x() + pos.y() * pos.y(); - double cos_phi = 0, sin_phi = 0; - if (r_sin_theta_2 > std::numeric_limits::min()) { - double inv_r_sin_theta = 1. / sqrt(r_sin_theta_2); - cos_phi = pos.x() * inv_r_sin_theta; - sin_phi = pos.y() * inv_r_sin_theta; - } else { - cos_phi = 1.; - sin_phi = 0.; + const double rSinTheta2 = pos.x() * pos.x() + pos.y() * pos.y(); + double cosPhi = 1.; + double sinPhi = 0.; + + if (rSinTheta2 > std::numeric_limits::min()) { + const double invRsinTheta = 1. / sqrt(rSinTheta2); + cosPhi = pos.x() * invRsinTheta; + sinPhi = pos.y() * invRsinTheta; } - return Acts::Vector3(bfield.x() * cos_phi, bfield.x() * sin_phi, - bfield.y()); + + return Acts::Vector3(bField.x() * cosPhi, bField.x() * sinPhi, bField.y()); }; - // iterate over all bins, set their value to the solenoid value - // at their lower left position + // iterate over all bins, set their value to the solenoid value at their lower + // left position for (std::size_t i = 0; i <= nBinsR + 1; i++) { for (std::size_t j = 0; j <= nBinsZ + 1; j++) { Grid_t::index_t index({i, j}); @@ -317,8 +270,7 @@ Acts::solenoidFieldMap(std::pair rlim, } } - // Create the mapper & BField Service - // create field mapping + // Create the mapper & BField Service create field mapping Acts::InterpolatedBFieldMap map( {transformPos, transformBField, std::move(grid)}); return map; diff --git a/Core/src/Material/MaterialMapUtils.cpp b/Core/src/Material/MaterialMapUtils.cpp index 64fdd1f5187..0b20f70e24d 100644 --- a/Core/src/Material/MaterialMapUtils.cpp +++ b/Core/src/Material/MaterialMapUtils.cpp @@ -25,6 +25,28 @@ using Acts::VectorHelpers::perp; using Acts::VectorHelpers::phi; +namespace { +auto getMinMaxAndBinCount(std::vector& xPos) { + // sort the values for unique() + std::ranges::sort(xPos); + + // get the number of bins over unique values + const std::size_t nBinsX = + std::distance(xPos.begin(), std::ranges::unique(xPos).end()); + + // get the minimum and maximum + auto [xMin, xMax] = std::ranges::minmax(xPos); + + // calculate maxima (add one last bin, because bin value always corresponds to + // left boundary) + const double stepX = (xMax - xMin) / (nBinsX - 1); + xMax += stepX; + + // Return all values as a tuple + return std::make_tuple(xMin, xMax, nBinsX); +} +} // anonymous namespace + auto Acts::materialMapperRZ( const std::function binsRZ, std::array nBinsRZ)>& @@ -43,31 +65,8 @@ auto Acts::materialMapperRZ( } // [2] Create Grid - // sort the values - std::ranges::sort(rPos); - std::ranges::sort(zPos); - // Get unique values - rPos.erase(std::unique(rPos.begin(), rPos.end()), rPos.end()); - zPos.erase(std::unique(zPos.begin(), zPos.end()), zPos.end()); - rPos.shrink_to_fit(); - zPos.shrink_to_fit(); - // get the number of bins - std::size_t nBinsR = rPos.size(); - std::size_t nBinsZ = zPos.size(); - - // get the minimum and maximum - auto minMaxR = std::minmax_element(rPos.begin(), rPos.end()); - auto minMaxZ = std::minmax_element(zPos.begin(), zPos.end()); - double rMin = *minMaxR.first; - double zMin = *minMaxZ.first; - double rMax = *minMaxR.second; - double zMax = *minMaxZ.second; - // calculate maxima (add one last bin, because bin value always corresponds to - // left boundary) - double stepZ = std::fabs(zMax - zMin) / (nBinsZ - 1); - double stepR = std::fabs(rMax - rMin) / (nBinsR - 1); - rMax += stepR; - zMax += stepZ; + const auto [rMin, rMax, nBinsR] = getMinMaxAndBinCount(rPos); + const auto [zMin, zMax, nBinsZ] = getMinMaxAndBinCount(zPos); // Create the axis for the grid Axis rAxis(rMin * lengthUnit, rMax * lengthUnit, nBinsR); @@ -79,13 +78,13 @@ auto Acts::materialMapperRZ( using Grid_t = decltype(grid); // [3] Set the material values + const std::array nIndices = {{nBinsR, nBinsZ}}; for (std::size_t i = 1; i <= nBinsR; ++i) { for (std::size_t j = 1; j <= nBinsZ; ++j) { - std::array nIndices = {{rPos.size(), zPos.size()}}; Grid_t::index_t indices = {{i, j}}; - // std::vectors begin with 0 and we do not want the user needing to - // take underflow or overflow bins in account this is why we need to - // subtract by one + // std::vectors begin with 0 and we do not want the user needing to take + // underflow or overflow bins in account this is why we need to subtract + // by one grid.atLocalBins(indices) = materialVector.at( materialVectorToGridMapper({{i - 1, j - 1}}, nIndices)); } @@ -95,14 +94,12 @@ auto Acts::materialMapperRZ( 0., 0., 0.; grid.setExteriorBins(vec); - // [4] Create the transformation for the position - // map (x,y,z) -> (r,z) + // [4] Create the transformation for the position map (x,y,z) -> (r,z) auto transformPos = [](const Vector3& pos) { return Vector2(perp(pos), pos.z()); }; - // [5] Create the mapper & BField Service - // create material mapping + // [5] Create the mapper & BField Service create material mapping return MaterialMapper(transformPos, std::move(grid)); } @@ -125,44 +122,11 @@ auto Acts::materialMapperXYZ( } // [2] Create Grid - // Sort the values - std::ranges::sort(xPos); - std::ranges::sort(yPos); - std::ranges::sort(zPos); - // Get unique values - xPos.erase(std::unique(xPos.begin(), xPos.end()), xPos.end()); - yPos.erase(std::unique(yPos.begin(), yPos.end()), yPos.end()); - zPos.erase(std::unique(zPos.begin(), zPos.end()), zPos.end()); - xPos.shrink_to_fit(); - yPos.shrink_to_fit(); - zPos.shrink_to_fit(); - // get the number of bins - std::size_t nBinsX = xPos.size(); - std::size_t nBinsY = yPos.size(); - std::size_t nBinsZ = zPos.size(); + const auto [xMin, xMax, nBinsX] = getMinMaxAndBinCount(xPos); + const auto [yMin, yMax, nBinsY] = getMinMaxAndBinCount(yPos); + const auto [zMin, zMax, nBinsZ] = getMinMaxAndBinCount(zPos); - // get the minimum and maximum - auto minMaxX = std::minmax_element(xPos.begin(), xPos.end()); - auto minMaxY = std::minmax_element(yPos.begin(), yPos.end()); - auto minMaxZ = std::minmax_element(zPos.begin(), zPos.end()); // Create the axis for the grid - // get minima - double xMin = *minMaxX.first; - double yMin = *minMaxY.first; - double zMin = *minMaxZ.first; - // get maxima - double xMax = *minMaxX.second; - double yMax = *minMaxY.second; - double zMax = *minMaxZ.second; - // calculate maxima (add one last bin, because bin value always corresponds to - // left boundary) - double stepZ = std::fabs(zMax - zMin) / (nBinsZ - 1); - double stepY = std::fabs(yMax - yMin) / (nBinsY - 1); - double stepX = std::fabs(xMax - xMin) / (nBinsX - 1); - xMax += stepX; - yMax += stepY; - zMax += stepZ; - Axis xAxis(xMin * lengthUnit, xMax * lengthUnit, nBinsX); Axis yAxis(yMin * lengthUnit, yMax * lengthUnit, nBinsY); Axis zAxis(zMin * lengthUnit, zMax * lengthUnit, nBinsZ); @@ -172,15 +136,14 @@ auto Acts::materialMapperXYZ( using Grid_t = decltype(grid); // [3] Set the bField values + const std::array nIndices = {{nBinsX, nBinsY, nBinsZ}}; for (std::size_t i = 1; i <= nBinsX; ++i) { for (std::size_t j = 1; j <= nBinsY; ++j) { for (std::size_t k = 1; k <= nBinsZ; ++k) { Grid_t::index_t indices = {{i, j, k}}; - std::array nIndices = { - {xPos.size(), yPos.size(), zPos.size()}}; - // std::vectors begin with 0 and we do not want the user needing to - // take underflow or overflow bins in account this is why we need to - // subtract by one + // std::vectors begin with 0 and we do not want the user needing to take + // underflow or overflow bins in account this is why we need to subtract + // by one grid.atLocalBins(indices) = materialVector.at( materialVectorToGridMapper({{i - 1, j - 1, k - 1}}, nIndices)); } @@ -191,11 +154,9 @@ auto Acts::materialMapperXYZ( 0., 0., 0.; grid.setExteriorBins(vec); - // [4] Create the transformation for the position - // map (x,y,z) -> (r,z) + // [4] Create the transformation for the position map (x,y,z) -> (r,z) auto transformPos = [](const Vector3& pos) { return pos; }; - // [5] Create the mapper & BField Service - // create material mapping + // [5] Create the mapper & BField Service create material mapping return MaterialMapper(transformPos, std::move(grid)); } From 9a1f17f3936826bb622e16e5c8cbcb0f95b20dd5 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Mon, 16 Sep 2024 17:51:31 +0200 Subject: [PATCH 02/13] const ref --- Core/src/MagneticField/BFieldMapUtils.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Core/src/MagneticField/BFieldMapUtils.cpp b/Core/src/MagneticField/BFieldMapUtils.cpp index ada71581d3e..a7419ea3c53 100644 --- a/Core/src/MagneticField/BFieldMapUtils.cpp +++ b/Core/src/MagneticField/BFieldMapUtils.cpp @@ -57,8 +57,8 @@ Acts::fieldMapRZ( std::array nBinsRZ)>& localToGlobalBin, std::vector rPos, std::vector zPos, - std::vector bField, double lengthUnit, double BFieldUnit, - bool firstQuadrant) { + const std::vector& bField, double lengthUnit, + double BFieldUnit, bool firstQuadrant) { // [1] Create Grid const auto [rMin, rMax, rBinCount] = getMinMaxAndBinCount(rPos); auto [zMin, zMax, zBinCount] = getMinMaxAndBinCount(zPos); @@ -137,7 +137,7 @@ Acts::fieldMapXYZ( std::array nBinsXYZ)>& localToGlobalBin, std::vector xPos, std::vector yPos, - std::vector zPos, std::vector bField, + std::vector zPos, const std::vector& bField, double lengthUnit, double BFieldUnit, bool firstOctant) { // [1] Create Grid auto [xMin, xMax, xBinCount] = getMinMaxAndBinCount(xPos); @@ -210,9 +210,9 @@ Acts::fieldMapXYZ( Acts::InterpolatedBFieldMap< Acts::Grid, Acts::Axis>> -Acts::solenoidFieldMap(std::pair rLim, - std::pair zLim, - std::pair nBins, +Acts::solenoidFieldMap(const std::pair& rLim, + const std::pair& zLim, + const std::pair& nBins, const SolenoidBField& field) { auto [rMin, rMax] = rLim; auto [zMin, zMax] = zLim; From 8426c019c7f24f3d82bba5aa2eb807a6a958dbd1 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Mon, 16 Sep 2024 18:05:04 +0200 Subject: [PATCH 03/13] fix signature --- .../Acts/MagneticField/BFieldMapUtils.hpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Core/include/Acts/MagneticField/BFieldMapUtils.hpp b/Core/include/Acts/MagneticField/BFieldMapUtils.hpp index 896b5b22344..5a178fa2d1a 100644 --- a/Core/include/Acts/MagneticField/BFieldMapUtils.hpp +++ b/Core/include/Acts/MagneticField/BFieldMapUtils.hpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2017-2018 CERN for the benefit of the Acts project +// Copyright (C) 2017-2024 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -75,7 +75,7 @@ fieldMapRZ(const std::function binsRZ, std::array nBinsRZ)>& localToGlobalBin, std::vector rPos, std::vector zPos, - std::vector bField, + const std::vector& bField, double lengthUnit = UnitConstants::mm, double BFieldUnit = UnitConstants::T, bool firstQuadrant = false); @@ -137,7 +137,7 @@ fieldMapXYZ( std::array nBinsXYZ)>& localToGlobalBin, std::vector xPos, std::vector yPos, - std::vector zPos, std::vector bField, + std::vector zPos, const std::vector& bField, double lengthUnit = UnitConstants::mm, double BFieldUnit = UnitConstants::T, bool firstOctant = false); @@ -145,17 +145,18 @@ fieldMapXYZ( /// creates a field mapper by sampling grid points from the analytical /// solenoid field. /// -/// @param rlim pair of r bounds -/// @param zlim pair of z bounds -/// @param nbins pair of bin counts +/// @param rLim pair of r bounds +/// @param zLim pair of z bounds +/// @param nBins pair of bin counts /// @param field the solenoid field instance /// /// @return A field map instance for use in interpolation. Acts::InterpolatedBFieldMap< Acts::Grid, Acts::Axis>> -solenoidFieldMap(std::pair rlim, std::pair zlim, - std::pair nbins, +solenoidFieldMap(const std::pair& rLim, + const std::pair& zLim, + const std::pair& nBins, const SolenoidBField& field); } // namespace Acts From 31e43211a70db8ed9229ea088b2689d6def99447 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Mon, 16 Sep 2024 18:19:29 +0200 Subject: [PATCH 04/13] fix unique --- Core/src/MagneticField/BFieldMapUtils.cpp | 4 ++-- Core/src/Material/MaterialMapUtils.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Core/src/MagneticField/BFieldMapUtils.cpp b/Core/src/MagneticField/BFieldMapUtils.cpp index a7419ea3c53..bcf9ed7f808 100644 --- a/Core/src/MagneticField/BFieldMapUtils.cpp +++ b/Core/src/MagneticField/BFieldMapUtils.cpp @@ -33,8 +33,8 @@ auto getMinMaxAndBinCount(std::vector& xPos) { std::ranges::sort(xPos); // get the number of bins over unique values - const std::size_t xBinCount = - std::distance(xPos.begin(), std::ranges::unique(xPos).end()); + auto it = std::unique(xPos.begin(), xPos.end()); + const std::size_t xBinCount = std::distance(xPos.begin(), it); // get the minimum and maximum auto [xMin, xMax] = std::ranges::minmax(xPos); diff --git a/Core/src/Material/MaterialMapUtils.cpp b/Core/src/Material/MaterialMapUtils.cpp index 0b20f70e24d..1123c4d49b9 100644 --- a/Core/src/Material/MaterialMapUtils.cpp +++ b/Core/src/Material/MaterialMapUtils.cpp @@ -31,8 +31,8 @@ auto getMinMaxAndBinCount(std::vector& xPos) { std::ranges::sort(xPos); // get the number of bins over unique values - const std::size_t nBinsX = - std::distance(xPos.begin(), std::ranges::unique(xPos).end()); + auto it = std::unique(xPos.begin(), xPos.end()); + const std::size_t nBinsX = std::distance(xPos.begin(), it); // get the minimum and maximum auto [xMin, xMax] = std::ranges::minmax(xPos); From 010f873862b4961adb9da359db64853ec2620e9d Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Tue, 17 Sep 2024 00:21:10 +0200 Subject: [PATCH 05/13] sonar --- Core/src/MagneticField/BFieldMapUtils.cpp | 2 +- Core/src/Material/MaterialMapUtils.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/src/MagneticField/BFieldMapUtils.cpp b/Core/src/MagneticField/BFieldMapUtils.cpp index bcf9ed7f808..9b02021804c 100644 --- a/Core/src/MagneticField/BFieldMapUtils.cpp +++ b/Core/src/MagneticField/BFieldMapUtils.cpp @@ -41,7 +41,7 @@ auto getMinMaxAndBinCount(std::vector& xPos) { // calculate maxima (add one last bin, because bin value always corresponds to // left boundary) - const double stepX = (xMax - xMin) / (xBinCount - 1); + const double stepX = (xMax - xMin) / static_cast(xBinCount - 1); xMax += stepX; // Return all values as a tuple diff --git a/Core/src/Material/MaterialMapUtils.cpp b/Core/src/Material/MaterialMapUtils.cpp index 1123c4d49b9..63a399c1237 100644 --- a/Core/src/Material/MaterialMapUtils.cpp +++ b/Core/src/Material/MaterialMapUtils.cpp @@ -39,7 +39,7 @@ auto getMinMaxAndBinCount(std::vector& xPos) { // calculate maxima (add one last bin, because bin value always corresponds to // left boundary) - const double stepX = (xMax - xMin) / (nBinsX - 1); + const double stepX = (xMax - xMin) / static_cast(nBinsX - 1); xMax += stepX; // Return all values as a tuple From 0cc0420e31715a455433353f5a0cb879a6d4c192 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Wed, 2 Oct 2024 16:22:33 +0200 Subject: [PATCH 06/13] unify --- Core/src/Material/MaterialMapUtils.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/src/Material/MaterialMapUtils.cpp b/Core/src/Material/MaterialMapUtils.cpp index 3565f34a6a7..7222b10aa31 100644 --- a/Core/src/Material/MaterialMapUtils.cpp +++ b/Core/src/Material/MaterialMapUtils.cpp @@ -32,18 +32,18 @@ auto getMinMaxAndBinCount(std::vector& xPos) { // get the number of bins over unique values auto it = std::unique(xPos.begin(), xPos.end()); - const std::size_t nBinsX = std::distance(xPos.begin(), it); + const std::size_t xBinCount = std::distance(xPos.begin(), it); // get the minimum and maximum auto [xMin, xMax] = std::ranges::minmax(xPos); // calculate maxima (add one last bin, because bin value always corresponds to // left boundary) - const double stepX = (xMax - xMin) / static_cast(nBinsX - 1); + const double stepX = (xMax - xMin) / static_cast(xBinCount - 1); xMax += stepX; // Return all values as a tuple - return std::make_tuple(xMin, xMax, nBinsX); + return std::make_tuple(xMin, xMax, xBinCount); } } // anonymous namespace From 2e92ba9fb1215429e6be791e29e14ad56a740eef Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Sun, 24 Nov 2024 21:44:57 +0100 Subject: [PATCH 07/13] review andiwand --- Core/include/Acts/Utilities/Helpers.hpp | 39 +++++++++++++++++++++++ Core/src/MagneticField/BFieldMapUtils.cpp | 33 ++++--------------- Core/src/Material/MaterialMapUtils.cpp | 33 ++++--------------- 3 files changed, 51 insertions(+), 54 deletions(-) diff --git a/Core/include/Acts/Utilities/Helpers.hpp b/Core/include/Acts/Utilities/Helpers.hpp index c59555fd848..957f1e0a13e 100644 --- a/Core/include/Acts/Utilities/Helpers.hpp +++ b/Core/include/Acts/Utilities/Helpers.hpp @@ -221,4 +221,43 @@ struct overloaded : Ts... { template overloaded(Ts...) -> overloaded; +/// Computes the minimum, maximum, and bin count for a given vector of values. +/// +/// This function processes a vector of doubles to compute: +/// - The minimum value (@c xMin) +/// - The maximum value (@c xMax), adjusted to include an additional bin +/// - The bin count (@c xBinCount) based on the number of unique values +/// +/// The computation is performed as follows: +/// 1. Sorts the input vector using @c std::ranges::sort to prepare for uniqueness. +/// 2. Determines the number of unique values using @c std::unique and calculates the bin count. +/// 3. Calculates the minimum and maximum using @c std::ranges::minmax. +/// 4. Adjusts the maximum to include an additional bin by adding the bin step +/// size. +/// +/// @param xPos A reference to a vector of doubles. +/// @return A tuple containing: +/// - The minimum value (double) +/// - The adjusted maximum value (double) +/// - The bin count (std::size_t) +auto getMinMaxAndBinCount(std::vector& xPos) { + // sort the values for unique() + std::ranges::sort(xPos); + + // get the number of bins over unique values + auto it = std::unique(xPos.begin(), xPos.end()); + const std::size_t xBinCount = std::distance(xPos.begin(), it); + + // get the minimum and maximum + auto [xMin, xMax] = std::ranges::minmax(xPos); + + // calculate maxima (add one last bin, because bin value always corresponds to + // left boundary) + const double stepX = (xMax - xMin) / static_cast(xBinCount - 1); + xMax += stepX; + + // Return all values as a tuple + return std::make_tuple(xMin, xMax, xBinCount); +} + } // namespace Acts diff --git a/Core/src/MagneticField/BFieldMapUtils.cpp b/Core/src/MagneticField/BFieldMapUtils.cpp index 8fe5714ea5a..fc7e5c01bc0 100644 --- a/Core/src/MagneticField/BFieldMapUtils.cpp +++ b/Core/src/MagneticField/BFieldMapUtils.cpp @@ -12,6 +12,7 @@ #include "Acts/MagneticField/SolenoidBField.hpp" #include "Acts/Utilities/Axis.hpp" #include "Acts/Utilities/Grid.hpp" +#include "Acts/Utilities/Helpers.hpp" #include "Acts/Utilities/Result.hpp" #include "Acts/Utilities/VectorHelpers.hpp" #include "Acts/Utilities/detail/grid_helper.hpp" @@ -27,28 +28,6 @@ using Acts::VectorHelpers::perp; using Acts::VectorHelpers::phi; -namespace { -auto getMinMaxAndBinCount(std::vector& xPos) { - // sort the values for unique() - std::ranges::sort(xPos); - - // get the number of bins over unique values - auto it = std::unique(xPos.begin(), xPos.end()); - const std::size_t xBinCount = std::distance(xPos.begin(), it); - - // get the minimum and maximum - auto [xMin, xMax] = std::ranges::minmax(xPos); - - // calculate maxima (add one last bin, because bin value always corresponds to - // left boundary) - const double stepX = (xMax - xMin) / static_cast(xBinCount - 1); - xMax += stepX; - - // Return all values as a tuple - return std::make_tuple(xMin, xMax, xBinCount); -} -} // anonymous namespace - Acts::InterpolatedBFieldMap< Acts::Grid, Acts::Axis>> @@ -60,8 +39,8 @@ Acts::fieldMapRZ( const std::vector& bField, double lengthUnit, double BFieldUnit, bool firstQuadrant) { // [1] Create Grid - const auto [rMin, rMax, rBinCount] = getMinMaxAndBinCount(rPos); - auto [zMin, zMax, zBinCount] = getMinMaxAndBinCount(zPos); + const auto [rMin, rMax, rBinCount] = Acts::getMinMaxAndBinCount(rPos); + auto [zMin, zMax, zBinCount] = Acts::getMinMaxAndBinCount(zPos); const std::size_t nBinsR = rBinCount; std::size_t nBinsZ = zBinCount; @@ -140,9 +119,9 @@ Acts::fieldMapXYZ( std::vector zPos, const std::vector& bField, double lengthUnit, double BFieldUnit, bool firstOctant) { // [1] Create Grid - auto [xMin, xMax, xBinCount] = getMinMaxAndBinCount(xPos); - auto [yMin, yMax, yBinCount] = getMinMaxAndBinCount(yPos); - auto [zMin, zMax, zBinCount] = getMinMaxAndBinCount(zPos); + auto [xMin, xMax, xBinCount] = Acts::getMinMaxAndBinCount(xPos); + auto [yMin, yMax, yBinCount] = Acts::getMinMaxAndBinCount(yPos); + auto [zMin, zMax, zBinCount] = Acts::getMinMaxAndBinCount(zPos); std::size_t nBinsX = xBinCount; std::size_t nBinsY = yBinCount; diff --git a/Core/src/Material/MaterialMapUtils.cpp b/Core/src/Material/MaterialMapUtils.cpp index 7222b10aa31..614ebce0611 100644 --- a/Core/src/Material/MaterialMapUtils.cpp +++ b/Core/src/Material/MaterialMapUtils.cpp @@ -12,6 +12,7 @@ #include "Acts/Material/Material.hpp" #include "Acts/Utilities/Axis.hpp" #include "Acts/Utilities/Grid.hpp" +#include "Acts/Utilities/Helpers.hpp" #include #include @@ -25,28 +26,6 @@ using Acts::VectorHelpers::perp; using Acts::VectorHelpers::phi; -namespace { -auto getMinMaxAndBinCount(std::vector& xPos) { - // sort the values for unique() - std::ranges::sort(xPos); - - // get the number of bins over unique values - auto it = std::unique(xPos.begin(), xPos.end()); - const std::size_t xBinCount = std::distance(xPos.begin(), it); - - // get the minimum and maximum - auto [xMin, xMax] = std::ranges::minmax(xPos); - - // calculate maxima (add one last bin, because bin value always corresponds to - // left boundary) - const double stepX = (xMax - xMin) / static_cast(xBinCount - 1); - xMax += stepX; - - // Return all values as a tuple - return std::make_tuple(xMin, xMax, xBinCount); -} -} // anonymous namespace - auto Acts::materialMapperRZ( const std::function binsRZ, std::array nBinsRZ)>& @@ -65,8 +44,8 @@ auto Acts::materialMapperRZ( } // [2] Create Grid - const auto [rMin, rMax, nBinsR] = getMinMaxAndBinCount(rPos); - const auto [zMin, zMax, nBinsZ] = getMinMaxAndBinCount(zPos); + const auto [rMin, rMax, nBinsR] = Acts::getMinMaxAndBinCount(rPos); + const auto [zMin, zMax, nBinsZ] = Acts::getMinMaxAndBinCount(zPos); // Create the axis for the grid Axis rAxis(rMin * lengthUnit, rMax * lengthUnit, nBinsR); @@ -122,9 +101,9 @@ auto Acts::materialMapperXYZ( } // [2] Create Grid - const auto [xMin, xMax, nBinsX] = getMinMaxAndBinCount(xPos); - const auto [yMin, yMax, nBinsY] = getMinMaxAndBinCount(yPos); - const auto [zMin, zMax, nBinsZ] = getMinMaxAndBinCount(zPos); + const auto [xMin, xMax, nBinsX] = Acts::getMinMaxAndBinCount(xPos); + const auto [yMin, yMax, nBinsY] = Acts::getMinMaxAndBinCount(yPos); + const auto [zMin, zMax, nBinsZ] = Acts::getMinMaxAndBinCount(zPos); // Create the axis for the grid Axis xAxis(xMin * lengthUnit, xMax * lengthUnit, nBinsX); From 92b4b50008c06d82ec413993a8e0350d10671c93 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Mon, 25 Nov 2024 14:32:12 +0100 Subject: [PATCH 08/13] make inline --- Core/include/Acts/Utilities/Helpers.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/include/Acts/Utilities/Helpers.hpp b/Core/include/Acts/Utilities/Helpers.hpp index 957f1e0a13e..2dd64025dbc 100644 --- a/Core/include/Acts/Utilities/Helpers.hpp +++ b/Core/include/Acts/Utilities/Helpers.hpp @@ -240,7 +240,7 @@ overloaded(Ts...) -> overloaded; /// - The minimum value (double) /// - The adjusted maximum value (double) /// - The bin count (std::size_t) -auto getMinMaxAndBinCount(std::vector& xPos) { +inline auto getMinMaxAndBinCount(std::vector& xPos) { // sort the values for unique() std::ranges::sort(xPos); From 54810d6412557a49ebe274d8429b7e5cd7a04d21 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Wed, 27 Nov 2024 11:02:37 +0100 Subject: [PATCH 09/13] review andiwand --- Core/include/Acts/Utilities/Helpers.hpp | 4 ++++ Core/src/MagneticField/BFieldMapUtils.cpp | 10 +++++----- Core/src/Material/MaterialMapUtils.cpp | 10 +++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Core/include/Acts/Utilities/Helpers.hpp b/Core/include/Acts/Utilities/Helpers.hpp index 2dd64025dbc..8025efc89a5 100644 --- a/Core/include/Acts/Utilities/Helpers.hpp +++ b/Core/include/Acts/Utilities/Helpers.hpp @@ -221,6 +221,8 @@ struct overloaded : Ts... { template overloaded(Ts...) -> overloaded; +namespace detail { + /// Computes the minimum, maximum, and bin count for a given vector of values. /// /// This function processes a vector of doubles to compute: @@ -260,4 +262,6 @@ inline auto getMinMaxAndBinCount(std::vector& xPos) { return std::make_tuple(xMin, xMax, xBinCount); } +} // namespace detail + } // namespace Acts diff --git a/Core/src/MagneticField/BFieldMapUtils.cpp b/Core/src/MagneticField/BFieldMapUtils.cpp index fc7e5c01bc0..788991c7078 100644 --- a/Core/src/MagneticField/BFieldMapUtils.cpp +++ b/Core/src/MagneticField/BFieldMapUtils.cpp @@ -39,8 +39,8 @@ Acts::fieldMapRZ( const std::vector& bField, double lengthUnit, double BFieldUnit, bool firstQuadrant) { // [1] Create Grid - const auto [rMin, rMax, rBinCount] = Acts::getMinMaxAndBinCount(rPos); - auto [zMin, zMax, zBinCount] = Acts::getMinMaxAndBinCount(zPos); + const auto [rMin, rMax, rBinCount] = detail::getMinMaxAndBinCount(rPos); + auto [zMin, zMax, zBinCount] = detail::getMinMaxAndBinCount(zPos); const std::size_t nBinsR = rBinCount; std::size_t nBinsZ = zBinCount; @@ -119,9 +119,9 @@ Acts::fieldMapXYZ( std::vector zPos, const std::vector& bField, double lengthUnit, double BFieldUnit, bool firstOctant) { // [1] Create Grid - auto [xMin, xMax, xBinCount] = Acts::getMinMaxAndBinCount(xPos); - auto [yMin, yMax, yBinCount] = Acts::getMinMaxAndBinCount(yPos); - auto [zMin, zMax, zBinCount] = Acts::getMinMaxAndBinCount(zPos); + auto [xMin, xMax, xBinCount] = detail::getMinMaxAndBinCount(xPos); + auto [yMin, yMax, yBinCount] = detail::getMinMaxAndBinCount(yPos); + auto [zMin, zMax, zBinCount] = detail::getMinMaxAndBinCount(zPos); std::size_t nBinsX = xBinCount; std::size_t nBinsY = yBinCount; diff --git a/Core/src/Material/MaterialMapUtils.cpp b/Core/src/Material/MaterialMapUtils.cpp index 614ebce0611..8f4d23cd71e 100644 --- a/Core/src/Material/MaterialMapUtils.cpp +++ b/Core/src/Material/MaterialMapUtils.cpp @@ -44,8 +44,8 @@ auto Acts::materialMapperRZ( } // [2] Create Grid - const auto [rMin, rMax, nBinsR] = Acts::getMinMaxAndBinCount(rPos); - const auto [zMin, zMax, nBinsZ] = Acts::getMinMaxAndBinCount(zPos); + const auto [rMin, rMax, nBinsR] = detail::getMinMaxAndBinCount(rPos); + const auto [zMin, zMax, nBinsZ] = detail::getMinMaxAndBinCount(zPos); // Create the axis for the grid Axis rAxis(rMin * lengthUnit, rMax * lengthUnit, nBinsR); @@ -101,9 +101,9 @@ auto Acts::materialMapperXYZ( } // [2] Create Grid - const auto [xMin, xMax, nBinsX] = Acts::getMinMaxAndBinCount(xPos); - const auto [yMin, yMax, nBinsY] = Acts::getMinMaxAndBinCount(yPos); - const auto [zMin, zMax, nBinsZ] = Acts::getMinMaxAndBinCount(zPos); + const auto [xMin, xMax, nBinsX] = detail::getMinMaxAndBinCount(xPos); + const auto [yMin, yMax, nBinsY] = detail::getMinMaxAndBinCount(yPos); + const auto [zMin, zMax, nBinsZ] = detail::getMinMaxAndBinCount(zPos); // Create the axis for the grid Axis xAxis(xMin * lengthUnit, xMax * lengthUnit, nBinsX); From 52eb81d4043b5f0d2bc8bd76fcbbf6a640ba0cae Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Wed, 27 Nov 2024 11:04:41 +0100 Subject: [PATCH 10/13] remove Acts:: --- Core/src/MagneticField/BFieldMapUtils.cpp | 59 +++++++++++------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/Core/src/MagneticField/BFieldMapUtils.cpp b/Core/src/MagneticField/BFieldMapUtils.cpp index 788991c7078..0832d18e8b4 100644 --- a/Core/src/MagneticField/BFieldMapUtils.cpp +++ b/Core/src/MagneticField/BFieldMapUtils.cpp @@ -36,8 +36,8 @@ Acts::fieldMapRZ( std::array nBinsRZ)>& localToGlobalBin, std::vector rPos, std::vector zPos, - const std::vector& bField, double lengthUnit, - double BFieldUnit, bool firstQuadrant) { + const std::vector& bField, double lengthUnit, double BFieldUnit, + bool firstQuadrant) { // [1] Create Grid const auto [rMin, rMax, rBinCount] = detail::getMinMaxAndBinCount(rPos); auto [zMin, zMax, zBinCount] = detail::getMinMaxAndBinCount(zPos); @@ -51,11 +51,11 @@ Acts::fieldMapRZ( } // Create the axis for the grid - Acts::Axis rAxis(rMin * lengthUnit, rMax * lengthUnit, nBinsR); - Acts::Axis zAxis(zMin * lengthUnit, zMax * lengthUnit, nBinsZ); + Axis rAxis(rMin * lengthUnit, rMax * lengthUnit, nBinsR); + Axis zAxis(zMin * lengthUnit, zMax * lengthUnit, nBinsZ); // Create the grid - Grid grid(Type, std::move(rAxis), std::move(zAxis)); + Grid grid(Type, std::move(rAxis), std::move(zAxis)); using Grid_t = decltype(grid); // [2] Set the bField values @@ -79,16 +79,15 @@ Acts::fieldMapRZ( } } } - grid.setExteriorBins(Acts::Vector2::Zero()); + grid.setExteriorBins(Vector2::Zero()); // [3] Create the transformation for the position map (x,y,z) -> (r,z) - auto transformPos = [](const Acts::Vector3& pos) { - return Acts::Vector2(perp(pos), pos.z()); + auto transformPos = [](const Vector3& pos) { + return Vector2(perp(pos), pos.z()); }; // [4] Create the transformation for the bField map (Br,Bz) -> (Bx,By,Bz) - auto transformBField = [](const Acts::Vector2& field, - const Acts::Vector3& pos) { + auto transformBField = [](const Vector2& field, const Vector3& pos) { const double rSinTheta2 = pos.x() * pos.x() + pos.y() * pos.y(); double cosPhi = 1.; double sinPhi = 0.; @@ -99,11 +98,11 @@ Acts::fieldMapRZ( sinPhi = pos.y() * invRsinTheta; } - return Acts::Vector3(field.x() * cosPhi, field.x() * sinPhi, field.y()); + return Vector3(field.x() * cosPhi, field.x() * sinPhi, field.y()); }; // [5] Create the mapper & BField Service create field mapping - return Acts::InterpolatedBFieldMap( + return InterpolatedBFieldMap( {transformPos, transformBField, std::move(grid)}); } @@ -116,7 +115,7 @@ Acts::fieldMapXYZ( std::array nBinsXYZ)>& localToGlobalBin, std::vector xPos, std::vector yPos, - std::vector zPos, const std::vector& bField, + std::vector zPos, const std::vector& bField, double lengthUnit, double BFieldUnit, bool firstOctant) { // [1] Create Grid auto [xMin, xMax, xBinCount] = detail::getMinMaxAndBinCount(xPos); @@ -136,9 +135,9 @@ Acts::fieldMapXYZ( nBinsZ = 2 * nBinsZ - 1; } - Acts::Axis xAxis(xMin * lengthUnit, xMax * lengthUnit, nBinsX); - Acts::Axis yAxis(yMin * lengthUnit, yMax * lengthUnit, nBinsY); - Acts::Axis zAxis(zMin * lengthUnit, zMax * lengthUnit, nBinsZ); + Axis xAxis(xMin * lengthUnit, xMax * lengthUnit, nBinsX); + Axis yAxis(yMin * lengthUnit, yMax * lengthUnit, nBinsY); + Axis zAxis(zMin * lengthUnit, zMax * lengthUnit, nBinsZ); // Create the grid Grid grid(Type, std::move(xAxis), std::move(yAxis), std::move(zAxis)); @@ -172,17 +171,18 @@ Acts::fieldMapXYZ( } } } - grid.setExteriorBins(Acts::Vector3::Zero()); + grid.setExteriorBins(Vector3::Zero()); // [3] Create the transformation for the position map (x,y,z) -> (r,z) - auto transformPos = [](const Acts::Vector3& pos) { return pos; }; + auto transformPos = [](const Vector3& pos) { return pos; }; // [4] Create the transformation for the BField map (Bx,By,Bz) -> (Bx,By,Bz) - auto transformBField = [](const Acts::Vector3& field, - const Acts::Vector3& /*pos*/) { return field; }; + auto transformBField = [](const Vector3& field, const Vector3& /*pos*/) { + return field; + }; // [5] Create the mapper & BField Service create field mapping - return Acts::InterpolatedBFieldMap( + return InterpolatedBFieldMap( {transformPos, transformBField, std::move(grid)}); } @@ -203,21 +203,20 @@ Acts::solenoidFieldMap(const std::pair& rLim, zMax += stepZ; // Create the axis for the grid - Acts::Axis rAxis(rMin, rMax, nBinsR); - Acts::Axis zAxis(zMin, zMax, nBinsZ); + Axis rAxis(rMin, rMax, nBinsR); + Axis zAxis(zMin, zMax, nBinsZ); // Create the grid - Grid grid(Type, std::move(rAxis), std::move(zAxis)); + Grid grid(Type, std::move(rAxis), std::move(zAxis)); using Grid_t = decltype(grid); // Create the transformation for the position map (x,y,z) -> (r,z) - auto transformPos = [](const Acts::Vector3& pos) { - return Acts::Vector2(perp(pos), pos.z()); + auto transformPos = [](const Vector3& pos) { + return Vector2(perp(pos), pos.z()); }; // Create the transformation for the bField map (Br,Bz) -> (Bx,By,Bz) - auto transformBField = [](const Acts::Vector2& bField, - const Acts::Vector3& pos) { + auto transformBField = [](const Vector2& bField, const Vector3& pos) { const double rSinTheta2 = pos.x() * pos.x() + pos.y() * pos.y(); double cosPhi = 1.; double sinPhi = 0.; @@ -228,7 +227,7 @@ Acts::solenoidFieldMap(const std::pair& rLim, sinPhi = pos.y() * invRsinTheta; } - return Acts::Vector3(bField.x() * cosPhi, bField.x() * sinPhi, bField.y()); + return Vector3(bField.x() * cosPhi, bField.x() * sinPhi, bField.y()); }; // iterate over all bins, set their value to the solenoid value at their lower @@ -250,7 +249,7 @@ Acts::solenoidFieldMap(const std::pair& rLim, } // Create the mapper & BField Service create field mapping - Acts::InterpolatedBFieldMap map( + InterpolatedBFieldMap map( {transformPos, transformBField, std::move(grid)}); return map; } From 65a2c67194673bff112caa8242404484d0bb3b58 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Wed, 27 Nov 2024 11:13:33 +0100 Subject: [PATCH 11/13] add comment --- Core/include/Acts/Utilities/Helpers.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Core/include/Acts/Utilities/Helpers.hpp b/Core/include/Acts/Utilities/Helpers.hpp index 8025efc89a5..3197928285c 100644 --- a/Core/include/Acts/Utilities/Helpers.hpp +++ b/Core/include/Acts/Utilities/Helpers.hpp @@ -242,6 +242,8 @@ namespace detail { /// - The minimum value (double) /// - The adjusted maximum value (double) /// - The bin count (std::size_t) +/// +/// @note The vector xPos will be modified during the call. inline auto getMinMaxAndBinCount(std::vector& xPos) { // sort the values for unique() std::ranges::sort(xPos); From 5a580a56fa2890050fb7752ba9cfa484655d6115 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Wed, 27 Nov 2024 13:31:51 +0100 Subject: [PATCH 12/13] more review --- Core/src/MagneticField/BFieldMapUtils.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Core/src/MagneticField/BFieldMapUtils.cpp b/Core/src/MagneticField/BFieldMapUtils.cpp index 0832d18e8b4..8ef06af743f 100644 --- a/Core/src/MagneticField/BFieldMapUtils.cpp +++ b/Core/src/MagneticField/BFieldMapUtils.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -67,8 +68,8 @@ Acts::fieldMapRZ( // underflow or overflow bins in account this is why we need to subtract // by one if (firstQuadrant) { - std::size_t n = - std::abs(static_cast(j) - static_cast(zBinCount)); + std::size_t n = std::abs(static_cast(j) - + static_cast(zBinCount)); grid.atLocalBins(indices) = bField.at(localToGlobalBin({{i - 1, n}}, nIndices)) * BFieldUnit; @@ -93,7 +94,7 @@ Acts::fieldMapRZ( double sinPhi = 0.; if (rSinTheta2 > std::numeric_limits::min()) { - const double invRsinTheta = 1. / sqrt(rSinTheta2); + const double invRsinTheta = 1. / std::sqrt(rSinTheta2); cosPhi = pos.x() * invRsinTheta; sinPhi = pos.y() * invRsinTheta; } @@ -154,12 +155,12 @@ Acts::fieldMapXYZ( // underflow or overflow bins in account this is why we need to subtract // by one if (firstOctant) { - std::size_t l = - std::abs(static_cast(i) - static_cast(xBinCount)); - std::size_t m = - std::abs(static_cast(j) - static_cast(yBinCount)); - std::size_t n = - std::abs(static_cast(k) - static_cast(zBinCount)); + std::size_t l = std::abs(static_cast(i) - + static_cast(xBinCount)); + std::size_t m = std::abs(static_cast(j) - + static_cast(yBinCount)); + std::size_t n = std::abs(static_cast(k) - + static_cast(zBinCount)); grid.atLocalBins(indices) = bField.at(localToGlobalBin({{l, m, n}}, nIndices)) * BFieldUnit; @@ -222,7 +223,7 @@ Acts::solenoidFieldMap(const std::pair& rLim, double sinPhi = 0.; if (rSinTheta2 > std::numeric_limits::min()) { - const double invRsinTheta = 1. / sqrt(rSinTheta2); + const double invRsinTheta = 1. / std::sqrt(rSinTheta2); cosPhi = pos.x() * invRsinTheta; sinPhi = pos.y() * invRsinTheta; } From 6397bf5753cd7522f736968153bcaecc37fa92f7 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Mon, 2 Dec 2024 11:13:29 +0100 Subject: [PATCH 13/13] add lambda --- Core/src/MagneticField/BFieldMapUtils.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Core/src/MagneticField/BFieldMapUtils.cpp b/Core/src/MagneticField/BFieldMapUtils.cpp index 8ef06af743f..f5b1b410ffe 100644 --- a/Core/src/MagneticField/BFieldMapUtils.cpp +++ b/Core/src/MagneticField/BFieldMapUtils.cpp @@ -147,6 +147,12 @@ Acts::fieldMapXYZ( // [2] Set the bField values const std::array nIndices = { {xBinCount, yBinCount, zBinCount}}; + + auto calcAbsDiff = [](std::size_t val, std::size_t binCount) { + return std::abs(static_cast(val) - + static_cast(binCount)); + }; + for (std::size_t i = 1; i <= nBinsX; ++i) { for (std::size_t j = 1; j <= nBinsY; ++j) { for (std::size_t k = 1; k <= nBinsZ; ++k) { @@ -155,12 +161,9 @@ Acts::fieldMapXYZ( // underflow or overflow bins in account this is why we need to subtract // by one if (firstOctant) { - std::size_t l = std::abs(static_cast(i) - - static_cast(xBinCount)); - std::size_t m = std::abs(static_cast(j) - - static_cast(yBinCount)); - std::size_t n = std::abs(static_cast(k) - - static_cast(zBinCount)); + const std::size_t l = calcAbsDiff(i, xBinCount); + const std::size_t m = calcAbsDiff(j, yBinCount); + const std::size_t n = calcAbsDiff(k, zBinCount); grid.atLocalBins(indices) = bField.at(localToGlobalBin({{l, m, n}}, nIndices)) * BFieldUnit;