Skip to content

Commit

Permalink
fix!: Use ActsScalar in binning tools
Browse files Browse the repository at this point in the history
The current binning tools use floats everywhere, which is prone to
conversion warnings. This commit converts that code to use the
`ActsScalar` type alias instead.
  • Loading branch information
stephenswat committed Aug 7, 2024
1 parent fc225f6 commit 6d763b8
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 109 deletions.
2 changes: 1 addition & 1 deletion Core/include/Acts/Detector/ProtoBinning.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct BinningDescription {
binUtility += BinUtility(b.bins(), b.edges.front(), b.edges.back(),
bOption, b.binValue);
} else {
std::vector<float> edges;
std::vector<ActsScalar> edges;
std::for_each(b.edges.begin(), b.edges.end(),
[&](ActsScalar edge) { edges.push_back(edge); });
binUtility += BinUtility(edges, bOption, b.binValue);
Expand Down
6 changes: 3 additions & 3 deletions Core/include/Acts/Utilities/BinUtility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class BinUtility {
/// @param opt is the binning option : open, closed
/// @param value is the binninb value : binX, binY, binZ, etc.
/// @param tForm is the (optional) transform
BinUtility(std::size_t bins, float min, float max, BinningOption opt = open,
BinningValue value = BinningValue::binX,
BinUtility(std::size_t bins, ActsScalar min, ActsScalar max,
BinningOption opt = open, BinningValue value = BinningValue::binX,
const Transform3& tForm = Transform3::Identity())
: m_binningData(), m_transform(tForm), m_itransform(tForm.inverse()) {
m_binningData.reserve(3);
Expand All @@ -85,7 +85,7 @@ class BinUtility {
/// @param opt is the binning option : open, closed
/// @param value is the binninb value : binX, binY, binZ, etc.
/// @param tForm is the (optional) transform
BinUtility(std::vector<float>& bValues, BinningOption opt = open,
BinUtility(std::vector<ActsScalar>& bValues, BinningOption opt = open,
BinningValue value = BinningValue::binPhi,
const Transform3& tForm = Transform3::Identity())
: m_binningData(), m_transform(tForm), m_itransform(tForm.inverse()) {
Expand Down
87 changes: 45 additions & 42 deletions Core/include/Acts/Utilities/BinningData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class BinningData {
BinningType type{}; ///< binning type: equidistant, arbitrary
BinningOption option{}; ///< binning option: open, closed
BinningValue binvalue{}; ///< binning value: binX, binY, binZ, binR ...
float min{}; ///< minimum value
float max{}; ///< maximum value
float step{}; ///< binning step
ActsScalar min{}; ///< minimum value
ActsScalar max{}; ///< maximum value
ActsScalar step{}; ///< binning step
bool zdim{}; ///< zero dimensional binning : direct access

/// sub structure: describe some sub binning
Expand All @@ -59,7 +59,7 @@ class BinningData {
/// @param bValue is the binning value: binX, binY, etc.
/// @param bMin is the minimum value
/// @param bMax is the maximum value
BinningData(BinningValue bValue, float bMin, float bMax)
BinningData(BinningValue bValue, ActsScalar bMin, ActsScalar bMax)
: type(equidistant),
option(open),
binvalue(bValue),
Expand All @@ -71,7 +71,7 @@ class BinningData {
m_bins(1),
m_boundaries({{min, max}}),
m_totalBins(1),
m_totalBoundaries(std::vector<float>()),
m_totalBoundaries(std::vector<ActsScalar>()),
m_functionPtr(&searchEquidistantWithBoundary) {}

/// Constructor for equidistant binning
Expand All @@ -86,7 +86,7 @@ class BinningData {
/// @param sBinData is (optional) sub structure
/// @param sBinAdditive is the prescription for the sub structure
BinningData(BinningOption bOption, BinningValue bValue, std::size_t bBins,
float bMin, float bMax,
ActsScalar bMin, ActsScalar bMax,
std::unique_ptr<const BinningData> sBinData = nullptr,
bool sBinAdditive = false)
: type(equidistant),
Expand All @@ -99,9 +99,9 @@ class BinningData {
subBinningData(std::move(sBinData)),
subBinningAdditive(sBinAdditive),
m_bins(bBins),
m_boundaries(std::vector<float>()),
m_boundaries(std::vector<ActsScalar>()),
m_totalBins(bBins),
m_totalBoundaries(std::vector<float>()) {
m_totalBoundaries(std::vector<ActsScalar>()) {
// set to equidistant search
m_functionPtr = &searchEquidistantWithBoundary;
// fill the boundary vector for fast access to center & boundaries
Expand All @@ -120,7 +120,7 @@ class BinningData {
/// @param bBoundaries are the bin boundaries
/// @param sBinData is (optional) sub structure
BinningData(BinningOption bOption, BinningValue bValue,
const std::vector<float>& bBoundaries,
const std::vector<ActsScalar>& bBoundaries,
std::unique_ptr<const BinningData> sBinData = nullptr)
: type(arbitrary),
option(bOption),
Expand Down Expand Up @@ -227,7 +227,7 @@ class BinningData {

/// Return the boundaries - including sub boundaries
/// @return vector of floats indicating the boundary values
const std::vector<float>& boundaries() const {
const std::vector<ActsScalar>& boundaries() const {
if (subBinningData) {
return m_totalBoundaries;
}
Expand All @@ -239,7 +239,7 @@ class BinningData {
/// @param lposition assumes the correct local position expression
///
/// @return float value according to the binning setup
float value(const Vector2& lposition) const {
Vector2::Scalar value(const Vector2& lposition) const {
// ordered after occurrence
if (binvalue == BinningValue::binR || binvalue == BinningValue::binRPhi ||
binvalue == BinningValue::binX || binvalue == BinningValue::binH) {
Expand All @@ -254,7 +254,7 @@ class BinningData {
/// @param position is the global position
///
/// @return float value according to the binning setup
float value(const Vector3& position) const {
Vector3::Scalar value(const Vector3& position) const {
using VectorHelpers::eta;
using VectorHelpers::perp;
using VectorHelpers::phi;
Expand All @@ -269,7 +269,7 @@ class BinningData {
return (eta(position));
}
if (toUnderlying(binvalue) < 3) {
return static_cast<float>(position[toUnderlying(binvalue)]);
return static_cast<ActsScalar>(position[toUnderlying(binvalue)]);
}
// phi gauging
return phi(position);
Expand All @@ -280,10 +280,10 @@ class BinningData {
/// @param bin is the bin for which the center value is requested
///
/// @return float value according to the bin center
float center(std::size_t bin) const {
const std::vector<float>& bvals = boundaries();
ActsScalar center(std::size_t bin) const {
const std::vector<ActsScalar>& bvals = boundaries();
// take the center between bin boundaries
float value =
ActsScalar value =
bin < (bvals.size() - 1) ? 0.5 * (bvals[bin] + bvals[bin + 1]) : 0.;
return value;
}
Expand All @@ -293,10 +293,11 @@ class BinningData {
/// @param bin is the bin for which the width is requested
///
/// @return float value of width
float width(std::size_t bin) const {
const std::vector<float>& bvals = boundaries();
ActsScalar width(std::size_t bin) const {
const std::vector<ActsScalar>& bvals = boundaries();
// take the center between bin boundaries
float value = bin < (bvals.size() - 1) ? bvals[bin + 1] - bvals[bin] : 0.;
ActsScalar value =
bin < (bvals.size() - 1) ? bvals[bin + 1] - bvals[bin] : 0.;
return value;
}

Expand All @@ -312,7 +313,7 @@ class BinningData {
}
// all other options
// @todo remove hard-coded tolerance parameters
float val = value(position);
ActsScalar val = value(position);
return (val > min - 0.001 && val < max + 0.001);
}

Expand All @@ -328,7 +329,7 @@ class BinningData {
}
// all other options
// @todo remove hard-coded tolerance parameters
float val = value(lposition);
ActsScalar val = value(lposition);
return (val > min - 0.001 && val < max + 0.001);
}

Expand Down Expand Up @@ -361,7 +362,7 @@ class BinningData {
/// @param value is the searchvalue as float
///
/// @return bin according tot this
std::size_t search(float value) const {
std::size_t search(ActsScalar value) const {
if (zdim) {
return 0;
}
Expand All @@ -376,7 +377,7 @@ class BinningData {
/// @param value is the searchvalue as float
///
/// @return bin according tot this
std::size_t searchWithSubStructure(float value) const {
std::size_t searchWithSubStructure(ActsScalar value) const {
// find the masterbin with the correct function pointer
std::size_t masterbin = (*m_functionPtr)(value, *this);
// additive sub binning -
Expand All @@ -385,7 +386,7 @@ class BinningData {
return masterbin + subBinningData->search(value);
}
// gauge the value to the subBinData
float gvalue =
ActsScalar gvalue =
value - masterbin * (subBinningData->max - subBinningData->min);
// now go / additive or multiplicative
std::size_t subbin = subBinningData->search(gvalue);
Expand All @@ -404,9 +405,9 @@ class BinningData {
if (zdim) {
return 0;
}
float val = value(position);
ActsScalar val = value(position);
Vector3 probe = position + dir.normalized();
float nextval = value(probe);
ActsScalar nextval = value(probe);
return (nextval > val) ? 1 : -1;
}

Expand All @@ -417,22 +418,24 @@ class BinningData {
/// it is set to max
///
/// @return the center value of the bin is given
float centerValue(std::size_t bin) const {
ActsScalar centerValue(std::size_t bin) const {
if (zdim) {
return 0.5 * (min + max);
}
float bmin = m_boundaries[bin];
float bmax = bin < m_boundaries.size() ? m_boundaries[bin + 1] : max;
ActsScalar bmin = m_boundaries[bin];
ActsScalar bmax = bin < m_boundaries.size() ? m_boundaries[bin + 1] : max;
return 0.5 * (bmin + bmax);
}

private:
std::size_t m_bins{}; ///< number of bins
std::vector<float> m_boundaries; ///< vector of holding the bin boundaries
std::size_t m_totalBins{}; ///< including potential substructure
std::vector<float> m_totalBoundaries; ///< including potential substructure

std::size_t (*m_functionPtr)(float,
std::size_t m_bins{}; ///< number of bins
std::vector<ActsScalar>
m_boundaries; ///< vector of holding the bin boundaries
std::size_t m_totalBins{}; ///< including potential substructure
std::vector<ActsScalar>
m_totalBoundaries; ///< including potential substructure

std::size_t (*m_functionPtr)(ActsScalar,
const BinningData&){}; /// function pointer

/// helper method to set the sub structure
Expand All @@ -447,11 +450,11 @@ class BinningData {
// the tricky one - exchange one bin by many others
m_totalBoundaries.reserve(m_totalBins + 1);
// get the sub bin boundaries
const std::vector<float>& subBinBoundaries =
const std::vector<ActsScalar>& subBinBoundaries =
subBinningData->boundaries();
float sBinMin = subBinBoundaries[0];
ActsScalar sBinMin = subBinBoundaries[0];
// get the min value of the sub bin boundaries
std::vector<float>::const_iterator mbvalue = m_boundaries.begin();
std::vector<ActsScalar>::const_iterator mbvalue = m_boundaries.begin();
for (; mbvalue != m_boundaries.end(); ++mbvalue) {
// should define numerically stable
if (std::abs((*mbvalue) - sBinMin) < 10e-10) {
Expand All @@ -469,12 +472,12 @@ class BinningData {
m_totalBins = m_bins * subBinningData->bins();
m_totalBoundaries.reserve(m_totalBins + 1);
// get the sub bin boundaries if there are any
const std::vector<float>& subBinBoundaries =
const std::vector<ActsScalar>& subBinBoundaries =
subBinningData->boundaries();
// create the boundary vector
m_totalBoundaries.push_back(min);
for (std::size_t ib = 0; ib < m_bins; ++ib) {
float offset = ib * step;
ActsScalar offset = ib * step;
for (std::size_t isb = 1; isb < subBinBoundaries.size(); ++isb) {
m_totalBoundaries.push_back(offset + subBinBoundaries[isb]);
}
Expand All @@ -487,7 +490,7 @@ class BinningData {

// Equidistant search
// - fastest method
static std::size_t searchEquidistantWithBoundary(float value,
static std::size_t searchEquidistantWithBoundary(ActsScalar value,
const BinningData& bData) {
// vanilla

Expand All @@ -510,7 +513,7 @@ class BinningData {
}

// Search in arbitrary boundary
static std::size_t searchInVectorWithBoundary(float value,
static std::size_t searchInVectorWithBoundary(ActsScalar value,
const BinningData& bData) {
// lower boundary
if (value <= bData.m_boundaries[0]) {
Expand Down
14 changes: 7 additions & 7 deletions Core/src/Detector/ProtoDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void Acts::ProtoVolume::harmonize(bool legacy) {
auto& fVolume = cts.constituentVolumes.front();
auto& lVolume = cts.constituentVolumes.back();

std::vector<float> borders = {};
std::vector<ActsScalar> borders = {};

// The volumes should be harmonized in all other constraining values
for (auto obValue : allBinningValues()) {
Expand Down Expand Up @@ -115,7 +115,7 @@ void Acts::ProtoVolume::harmonize(bool legacy) {
} else if (layersPresent && !legacy) {
// Count the gaps
std::size_t gaps = 0;
std::vector<float> boundaries = {};
std::vector<ActsScalar> boundaries = {};
// New container vector
std::vector<ProtoVolume> updatedConstituents;
ActsScalar containerMin = extent.min(binValue);
Expand All @@ -124,14 +124,14 @@ void Acts::ProtoVolume::harmonize(bool legacy) {
gap.name = name + "-gap-" + std::to_string(gaps++);
gap.extent.set(binValue, containerMin, fVolume.extent.min(binValue));
updatedConstituents.push_back(gap);
borders.push_back(static_cast<float>(containerMin));
borders.push_back(containerMin);
}
// Fill the gaps
for (unsigned int iv = 1; iv < cts.constituentVolumes.size(); ++iv) {
auto& lv = cts.constituentVolumes[iv - 1u];
// This volume is one to save
updatedConstituents.push_back(lv);
borders.push_back(static_cast<float>(lv.extent.min(binValue)));
borders.push_back(static_cast<ActsScalar>(lv.extent.min(binValue)));
// check if a gap to the next is needed
ActsScalar low = lv.extent.max(binValue);
auto& hv = cts.constituentVolumes[iv];
Expand All @@ -141,20 +141,20 @@ void Acts::ProtoVolume::harmonize(bool legacy) {
gap.name = name + "-gap-" + std::to_string(gaps++);
gap.extent.set(binValue, low, high);
updatedConstituents.push_back(gap);
borders.push_back(static_cast<float>(low));
borders.push_back(low);
}
}
ActsScalar constituentsMax = lVolume.extent.max(binValue);
updatedConstituents.push_back(lVolume);
borders.push_back(static_cast<float>(constituentsMax));
borders.push_back(constituentsMax);
// Check the container min/max setting
ActsScalar containerMax = extent.max(binValue);
if (constituentsMax < containerMax) {
ProtoVolume gap;
gap.name = name + "-gap-" + std::to_string(gaps++);
gap.extent.set(binValue, constituentsMax, containerMax);
updatedConstituents.push_back(gap);
borders.push_back(static_cast<float>(containerMax));
borders.push_back(containerMax);
}
cts.constituentVolumes = updatedConstituents;
} else if (legacy && layersPresent) {
Expand Down
2 changes: 1 addition & 1 deletion Core/src/Geometry/CuboidVolumeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Acts::MutableTrackingVolumePtr Acts::CuboidVolumeBuilder::trackingVolume(
}

// Set bin boundaries along binning
std::vector<float> binBoundaries;
std::vector<ActsScalar> binBoundaries;
binBoundaries.push_back(volumes[0]->center().x() -
m_cfg.volumeCfg[0].length.x() * 0.5);
for (std::size_t i = 0; i < volumes.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion Core/src/Geometry/LayerArrayCreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ std::unique_ptr<const Acts::LayerArray> Acts::LayerArrayCreator::layerArray(

// arbitrary binning
case arbitrary: {
std::vector<float> boundaries;
std::vector<ActsScalar> boundaries;
// initial step
boundaries.push_back(min);
double layerValue = 0.;
Expand Down
2 changes: 1 addition & 1 deletion Core/src/Geometry/TrackingVolumeArrayCreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Acts::TrackingVolumeArrayCreator::trackingVolumeArray(

// prepare what we need :
// (1) arbitrary binning for volumes is fast enough
std::vector<float> boundaries;
std::vector<ActsScalar> boundaries;
boundaries.reserve(tVolumes.size() + 1);
// (2) the vector needed for the BinnedArray
std::vector<TrackingVolumeOrderPosition> tVolumesOrdered;
Expand Down
2 changes: 1 addition & 1 deletion Plugins/Json/src/UtilitiesJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void Acts::from_json(const nlohmann::json& j, BinningData& bd) {
bd = BinningData(bOption, bValue, bins, min, max, std::move(subBinning),
subBinningAdditive);
} else {
std::vector<float> boundaries = j["boundaries"];
std::vector<ActsScalar> boundaries = j["boundaries"];
bd = BinningData(bOption, bValue, boundaries, std::move(subBinning));
}
}
Expand Down
Loading

0 comments on commit 6d763b8

Please sign in to comment.