File indexing completed on 2025-07-13 07:50:39
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Acts/Seeding2/detail/CandidatesForMiddleSp2.hpp"
0010
0011 #include <limits>
0012
0013 namespace Acts::Experimental {
0014
0015 void CandidatesForMiddleSp2::setMaxElements(Size nLow, Size nHigh) {
0016 m_maxSizeHigh = nHigh;
0017 m_maxSizeLow = nLow;
0018
0019
0020
0021 if (nHigh == std::numeric_limits<Size>::max() ||
0022 nLow == std::numeric_limits<Size>::max()) {
0023 return;
0024 }
0025
0026
0027 m_storage.reserve(nLow + nHigh);
0028 m_indicesHigh.reserve(nHigh);
0029 m_indicesLow.reserve(nLow);
0030 }
0031
0032 void CandidatesForMiddleSp2::pop(std::vector<Index>& indices,
0033 Size& currentSize) {
0034
0035
0036
0037
0038
0039
0040
0041 std::swap(indices[0], indices[currentSize - 1]);
0042 bubbledw(indices, 0, --currentSize);
0043 }
0044
0045 float CandidatesForMiddleSp2::weight(const std::vector<Index>& indices,
0046 const Index n) const {
0047
0048 return m_storage[indices[n]].weight;
0049 }
0050
0051 void CandidatesForMiddleSp2::clear() {
0052
0053
0054 m_nHigh = 0;
0055 m_nLow = 0;
0056
0057 m_storage.clear();
0058 m_indicesHigh.clear();
0059 m_indicesLow.clear();
0060 }
0061
0062 bool CandidatesForMiddleSp2::push(SpacePointIndex2 spB, SpacePointIndex2 spM,
0063 SpacePointIndex2 spT, float weight,
0064 float zOrigin, bool isQuality) {
0065
0066
0067 if (isQuality) {
0068 return push(m_indicesHigh, m_nHigh, m_maxSizeHigh, spB, spM, spT, weight,
0069 zOrigin, isQuality);
0070 }
0071 return push(m_indicesLow, m_nLow, m_maxSizeLow, spB, spM, spT, weight,
0072 zOrigin, isQuality);
0073 }
0074
0075 bool CandidatesForMiddleSp2::push(std::vector<Index>& indices, Index& n,
0076 const Size nMax, const Index spB,
0077 const Index spM, const Index spT,
0078 const float weight, const float zOrigin,
0079 const bool isQuality) {
0080
0081 if (nMax == 0) {
0082 return false;
0083 }
0084
0085
0086 if (n < nMax) {
0087 addToCollection(
0088 indices, n, nMax,
0089 TripletCandidate2(spB, spM, spT, weight, zOrigin, isQuality));
0090 return true;
0091 }
0092
0093
0094
0095 if (float lowestWeight = this->weight(indices, 0); weight <= lowestWeight) {
0096 return false;
0097 }
0098
0099
0100 pop(indices, n);
0101 addToCollection(indices, n, nMax,
0102 TripletCandidate2(spB, spM, spT, weight, zOrigin, isQuality));
0103 return true;
0104 }
0105
0106 void CandidatesForMiddleSp2::addToCollection(std::vector<Index>& indices,
0107 Index& n, const Size nMax,
0108 const TripletCandidate2& element) {
0109
0110 if (indices.size() == nMax) {
0111 m_storage[indices[n]] = element;
0112 } else {
0113 m_storage.push_back(element);
0114 indices.push_back(m_storage.size() - 1);
0115 }
0116
0117 bubbleup(indices, n++);
0118 }
0119
0120 void CandidatesForMiddleSp2::bubbledw(std::vector<Index>& indices, Index n,
0121 const Size actualSize) {
0122 while (n < actualSize) {
0123
0124
0125
0126 const float current = weight(indices, n);
0127 const Index leftChild = 2 * n + 1;
0128 const Index rightChild = 2 * n + 2;
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141 if (!exists(leftChild, actualSize)) {
0142 break;
0143 }
0144
0145
0146
0147
0148
0149
0150 const float weightLeftChild = weight(indices, leftChild);
0151
0152 Index selectedChild = leftChild;
0153 float selectedWeight = weightLeftChild;
0154
0155
0156 if (exists(rightChild, actualSize)) {
0157 float weightRightChild = weight(indices, rightChild);
0158 if (weightRightChild <= weightLeftChild) {
0159 selectedChild = rightChild;
0160 selectedWeight = weightRightChild;
0161 }
0162 }
0163
0164
0165
0166
0167 if (selectedWeight >= current) {
0168 break;
0169 }
0170
0171
0172 std::swap(indices[n], indices[selectedChild]);
0173 n = selectedChild;
0174 }
0175 }
0176
0177 void CandidatesForMiddleSp2::bubbleup(std::vector<Index>& indices, Index n) {
0178 while (n != 0) {
0179
0180
0181
0182 const Index parentIdx = (n - 1) / 2;
0183
0184 const float weightCurrent = weight(indices, n);
0185
0186 if (float weightParent = weight(indices, parentIdx);
0187 weightParent <= weightCurrent) {
0188 break;
0189 }
0190
0191
0192 std::swap(indices[n], indices[parentIdx]);
0193 n = parentIdx;
0194 }
0195 }
0196
0197 void CandidatesForMiddleSp2::toSortedCandidates(
0198 const SpacePointContainer2& spacePoints,
0199 std::vector<TripletCandidate2>& output) {
0200
0201
0202 output.resize(m_nHigh + m_nLow);
0203 Index outIdx = output.size() - 1;
0204
0205
0206
0207
0208 while (m_nHigh != 0 || m_nLow != 0) {
0209
0210 if (m_nHigh == 0) {
0211 Index idx = m_nLow;
0212 for (Index i = 0; i < idx; i++) {
0213 output[outIdx] = m_storage[m_indicesLow[0]];
0214 pop(m_indicesLow, m_nLow);
0215 outIdx--;
0216 }
0217 break;
0218 }
0219
0220
0221 if (m_nLow == 0) {
0222 Index idx = m_nHigh;
0223 for (Index i = 0; i < idx; i++) {
0224 output[outIdx] = m_storage[m_indicesHigh[0]];
0225 pop(m_indicesHigh, m_nHigh);
0226 outIdx--;
0227 }
0228 break;
0229 }
0230
0231
0232 if (descendingByQuality(spacePoints, m_storage[m_indicesLow[0]],
0233 m_storage[m_indicesHigh[0]])) {
0234 output[outIdx--] = m_storage[m_indicesHigh[0]];
0235 pop(m_indicesHigh, m_nHigh);
0236 } else {
0237 output[outIdx--] = m_storage[m_indicesLow[0]];
0238 pop(m_indicesLow, m_nLow);
0239 }
0240 }
0241
0242 clear();
0243 }
0244
0245 bool CandidatesForMiddleSp2::descendingByQuality(
0246 const SpacePointContainer2& spacePoints, const TripletCandidate2& i1,
0247 const TripletCandidate2& i2) {
0248 if (i1.weight != i2.weight) {
0249 return i1.weight > i2.weight;
0250 }
0251
0252
0253
0254
0255 const auto bottomL1 = spacePoints[i1.bottom];
0256 const auto middleL1 = spacePoints[i1.middle];
0257 const auto topL1 = spacePoints[i1.top];
0258
0259 const auto bottomL2 = spacePoints[i2.bottom];
0260 const auto middleL2 = spacePoints[i2.middle];
0261 const auto topL2 = spacePoints[i2.top];
0262
0263 float seed1_sum = 0.;
0264 float seed2_sum = 0.;
0265
0266 seed1_sum += bottomL1.y() * bottomL1.y() + bottomL1.z() * bottomL1.z();
0267 seed1_sum += middleL1.y() * middleL1.y() + middleL1.z() * middleL1.z();
0268 seed1_sum += topL1.y() * topL1.y() + topL1.z() * topL1.z();
0269
0270 seed2_sum += bottomL2.y() * bottomL2.y() + bottomL2.z() * bottomL2.z();
0271 seed2_sum += middleL2.y() * middleL2.y() + middleL2.z() * middleL2.z();
0272 seed2_sum += topL2.y() * topL2.y() + topL2.z() * topL2.z();
0273
0274 return seed1_sum > seed2_sum;
0275 }
0276
0277 bool CandidatesForMiddleSp2::ascendingByQuality(
0278 const SpacePointContainer2& spacePoints, const TripletCandidate2& i1,
0279 const TripletCandidate2& i2) {
0280 return !descendingByQuality(spacePoints, i1, i2);
0281 }
0282
0283 }