Skip to content

Commit

Permalink
refactor: modernise map utils (#3615)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
	- Introduced a new function to compute minimum and maximum values along with unique bin counts for input vectors.

- **Refactor**
	- Enhanced the handling of magnetic field data by improving type usage and logic in multiple functions.
	- Streamlined calculations for minimum and maximum values using the new utility function.
	- Simplified the material mapping functions to improve maintainability and reduce complexity.

- **Bug Fixes**
	- Adjusted error handling and control flow for grid value settings based on magnetic field data.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
AJPfleger authored Dec 2, 2024
1 parent 8895da1 commit bf997eb
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 252 deletions.
15 changes: 8 additions & 7 deletions Core/include/Acts/MagneticField/BFieldMapUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fieldMapRZ(const std::function<std::size_t(std::array<std::size_t, 2> binsRZ,
std::array<std::size_t, 2> nBinsRZ)>&
localToGlobalBin,
std::vector<double> rPos, std::vector<double> zPos,
std::vector<Acts::Vector2> bField,
const std::vector<Acts::Vector2>& bField,
double lengthUnit = UnitConstants::mm,
double BFieldUnit = UnitConstants::T, bool firstQuadrant = false);

Expand Down Expand Up @@ -137,25 +137,26 @@ fieldMapXYZ(
std::array<std::size_t, 3> nBinsXYZ)>&
localToGlobalBin,
std::vector<double> xPos, std::vector<double> yPos,
std::vector<double> zPos, std::vector<Acts::Vector3> bField,
std::vector<double> zPos, const std::vector<Acts::Vector3>& bField,
double lengthUnit = UnitConstants::mm, double BFieldUnit = UnitConstants::T,
bool firstOctant = false);

/// Function which takes an existing SolenoidBField instance and
/// 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::Vector2, Acts::Axis<Acts::AxisType::Equidistant>,
Acts::Axis<Acts::AxisType::Equidistant>>>
solenoidFieldMap(std::pair<double, double> rlim, std::pair<double, double> zlim,
std::pair<std::size_t, std::size_t> nbins,
solenoidFieldMap(const std::pair<double, double>& rLim,
const std::pair<double, double>& zLim,
const std::pair<std::size_t, std::size_t>& nBins,
const SolenoidBField& field);

} // namespace Acts
45 changes: 45 additions & 0 deletions Core/include/Acts/Utilities/Helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,49 @@ struct overloaded : Ts... {
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;

namespace detail {

/// 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)
///
/// @note The vector xPos will be modified during the call.
inline auto getMinMaxAndBinCount(std::vector<double>& 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<double>(xBinCount - 1);
xMax += stepX;

// Return all values as a tuple
return std::make_tuple(xMin, xMax, xBinCount);
}

} // namespace detail

} // namespace Acts
Loading

0 comments on commit bf997eb

Please sign in to comment.