File indexing completed on 2025-01-18 09:11:39
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ActsExamples/TrackFinding/HoughTransformSeeder.hpp"
0010
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Common.hpp"
0013 #include "Acts/Definitions/TrackParametrization.hpp"
0014 #include "Acts/EventData/SourceLink.hpp"
0015 #include "Acts/Geometry/TrackingGeometry.hpp"
0016 #include "Acts/Surfaces/Surface.hpp"
0017 #include "Acts/Utilities/Enumerate.hpp"
0018 #include "Acts/Utilities/MathHelpers.hpp"
0019 #include "ActsExamples/EventData/GeometryContainers.hpp"
0020 #include "ActsExamples/EventData/Index.hpp"
0021 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0022 #include "ActsExamples/EventData/Measurement.hpp"
0023 #include "ActsExamples/EventData/ProtoTrack.hpp"
0024 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0025 #include "ActsExamples/TrackFinding/DefaultHoughFunctions.hpp"
0026 #include "ActsExamples/Utilities/GroupBy.hpp"
0027 #include "ActsExamples/Utilities/Range.hpp"
0028
0029 #include <algorithm>
0030 #include <cmath>
0031 #include <iterator>
0032 #include <ostream>
0033 #include <stdexcept>
0034 #include <variant>
0035
0036 static inline int quant(double min, double max, unsigned nSteps, double val);
0037 static inline double unquant(double min, double max, unsigned nSteps, int step);
0038 template <typename T>
0039 static inline std::string to_string(std::vector<T> v);
0040
0041 ActsExamples::HoughTransformSeeder::HoughTransformSeeder(
0042 ActsExamples::HoughTransformSeeder::Config cfg, Acts::Logging::Level lvl)
0043 : ActsExamples::IAlgorithm("HoughTransformSeeder", lvl),
0044 m_cfg(std::move(cfg)),
0045 m_logger(Acts::getDefaultLogger("HoughTransformSeeder", lvl)) {
0046
0047
0048 bool foundInput = false;
0049 for (const auto& spName : m_cfg.inputSpacePoints) {
0050 if (!(spName.empty())) {
0051 foundInput = true;
0052 }
0053
0054 auto& handle = m_inputSpacePoints.emplace_back(
0055 std::make_unique<ReadDataHandle<SimSpacePointContainer>>(
0056 this,
0057 "InputSpacePoints#" + std::to_string(m_inputSpacePoints.size())));
0058 handle->initialize(spName);
0059 }
0060 if (!(m_cfg.inputMeasurements.empty())) {
0061 foundInput = true;
0062 }
0063
0064 if (!foundInput) {
0065 throw std::invalid_argument(
0066 "HoughTransformSeeder: Missing some kind of input (measurements of "
0067 "spacepoints)");
0068 }
0069
0070 if (m_cfg.outputProtoTracks.empty()) {
0071 throw std::invalid_argument(
0072 "HoughTransformSeeder: Missing hough tracks output collection");
0073 }
0074 if (m_cfg.outputSeeds.empty()) {
0075 throw std::invalid_argument(
0076 "HoughTransformSeeder: Missing hough track seeds output collection");
0077 }
0078
0079 m_outputProtoTracks.initialize(m_cfg.outputProtoTracks);
0080 m_inputMeasurements.initialize(m_cfg.inputMeasurements);
0081
0082 if (!m_cfg.trackingGeometry) {
0083 throw std::invalid_argument(
0084 "HoughTransformSeeder: Missing tracking geometry");
0085 }
0086
0087 if (m_cfg.geometrySelection.empty()) {
0088 throw std::invalid_argument(
0089 "HoughTransformSeeder: Missing geometry selection");
0090 }
0091
0092 for (const auto& geoId : m_cfg.geometrySelection) {
0093 if ((geoId.approach() != 0u) || (geoId.boundary() != 0u) ||
0094 (geoId.sensitive() != 0u)) {
0095 throw std::invalid_argument(
0096 "HoughTransformSeeder: Invalid geometry selection: only volume and "
0097 "layer are allowed to be set");
0098 }
0099 }
0100
0101
0102
0103
0104
0105 auto isDuplicate = [](Acts::GeometryIdentifier ref,
0106 Acts::GeometryIdentifier cmp) {
0107
0108
0109 if (ref.volume() == 0) {
0110 return true;
0111 }
0112
0113 if (ref.volume() != cmp.volume()) {
0114 return false;
0115 }
0116
0117 return (ref.layer() == cmp.layer());
0118 };
0119
0120 std::ranges::sort(m_cfg.geometrySelection,
0121 std::less<Acts::GeometryIdentifier>{});
0122 auto geoSelBeg = m_cfg.geometrySelection.begin();
0123 auto geoSelEnd = m_cfg.geometrySelection.end();
0124 auto geoSelLastUnique = std::unique(geoSelBeg, geoSelEnd, isDuplicate);
0125 if (geoSelLastUnique != geoSelEnd) {
0126 ACTS_WARNING("Removed " << std::distance(geoSelLastUnique, geoSelEnd)
0127 << " geometry selection duplicates");
0128 m_cfg.geometrySelection.erase(geoSelLastUnique, geoSelEnd);
0129 }
0130 ACTS_INFO("Hough geometry selection:");
0131 for (const auto& geoId : m_cfg.geometrySelection) {
0132 ACTS_INFO(" " << geoId);
0133 }
0134
0135
0136 m_step_x = (m_cfg.xMax - m_cfg.xMin) / m_cfg.houghHistSize_x;
0137 m_step_y = (m_cfg.yMax - m_cfg.yMin) / m_cfg.houghHistSize_y;
0138 for (unsigned i = 0; i <= m_cfg.houghHistSize_x; i++) {
0139 m_bins_x.push_back(
0140 unquant(m_cfg.xMin, m_cfg.xMax, m_cfg.houghHistSize_x, i));
0141 }
0142 for (unsigned i = 0; i <= m_cfg.houghHistSize_y; i++) {
0143 m_bins_y.push_back(
0144 unquant(m_cfg.yMin, m_cfg.yMax, m_cfg.houghHistSize_y, i));
0145 }
0146
0147 m_cfg.fieldCorrector
0148 .connect<&ActsExamples::DefaultHoughFunctions::fieldCorrectionDefault>();
0149 m_cfg.layerIDFinder
0150 .connect<&ActsExamples::DefaultHoughFunctions::findLayerIDDefault>();
0151 m_cfg.sliceTester
0152 .connect<&ActsExamples::DefaultHoughFunctions::inSliceDefault>();
0153 }
0154
0155 ActsExamples::ProcessCode ActsExamples::HoughTransformSeeder::execute(
0156 const AlgorithmContext& ctx) const {
0157
0158 houghMeasurementStructs.clear();
0159
0160
0161 addSpacePoints(ctx);
0162
0163
0164 addMeasurements(ctx);
0165
0166 static thread_local ProtoTrackContainer protoTracks;
0167 protoTracks.clear();
0168
0169
0170 for (int subregion : m_cfg.subRegions) {
0171 ACTS_DEBUG("Processing subregion " << subregion);
0172 ActsExamples::HoughHist m_houghHist = createHoughHist(subregion);
0173
0174 for (unsigned y = 0; y < m_cfg.houghHistSize_y; y++) {
0175 for (unsigned x = 0; x < m_cfg.houghHistSize_x; x++) {
0176 if (passThreshold(m_houghHist, x, y)) {
0177
0178
0179
0180
0181 std::vector<std::vector<std::vector<Index>>> hitIndicesAll(
0182 m_cfg.nLayers);
0183 std::vector<std::size_t> nHitsPerLayer(m_cfg.nLayers);
0184 for (auto measurementIndex : m_houghHist.atLocalBins({y, x}).second) {
0185 HoughMeasurementStruct* meas =
0186 houghMeasurementStructs[measurementIndex].get();
0187 hitIndicesAll[meas->layer].push_back(meas->indices);
0188 nHitsPerLayer[meas->layer]++;
0189 }
0190 std::vector<std::vector<int>> combs = getComboIndices(nHitsPerLayer);
0191
0192 for (auto [icomb, hit_indices] :
0193 Acts::enumerate(combs)) {
0194
0195 ProtoTrack protoTrack;
0196 for (unsigned layer = 0; layer < m_cfg.nLayers; layer++) {
0197 if (hit_indices[layer] >= 0) {
0198 for (auto index : hitIndicesAll[layer][hit_indices[layer]]) {
0199 protoTrack.push_back(index);
0200 }
0201 }
0202 }
0203 protoTracks.push_back(protoTrack);
0204 }
0205 }
0206 }
0207 }
0208 }
0209 ACTS_DEBUG("Created " << protoTracks.size() << " proto track");
0210
0211 m_outputProtoTracks(ctx, ProtoTrackContainer{protoTracks});
0212
0213 houghMeasurementStructs.clear();
0214 return ActsExamples::ProcessCode::SUCCESS;
0215 }
0216
0217 ActsExamples::HoughHist
0218 ActsExamples::HoughTransformSeeder::createLayerHoughHist(unsigned layer,
0219 int subregion) const {
0220 ActsExamples::HoughHist houghHist(
0221 Axis(0, m_cfg.houghHistSize_y, m_cfg.houghHistSize_y),
0222 Axis(0, m_cfg.houghHistSize_x, m_cfg.houghHistSize_x));
0223 for (unsigned index = 0; index < houghMeasurementStructs.size(); index++) {
0224 HoughMeasurementStruct* meas = houghMeasurementStructs[index].get();
0225 if (meas->layer != layer) {
0226 continue;
0227 }
0228 if (!(m_cfg.sliceTester(meas->z, meas->layer, subregion)).value()) {
0229 continue;
0230 }
0231
0232
0233 for (unsigned y_ = 0; y_ < m_cfg.houghHistSize_y; y_++) {
0234 unsigned y_bin_min = y_;
0235 unsigned y_bin_max = (y_ + 1);
0236
0237
0238 auto xBins =
0239 yToXBins(y_bin_min, y_bin_max, meas->radius, meas->phi, meas->layer);
0240
0241 for (unsigned y = y_bin_min; y < y_bin_max; y++) {
0242 for (unsigned x = xBins.first; x < xBins.second; x++) {
0243 houghHist.atLocalBins({y, x}).first++;
0244 houghHist.atLocalBins({y, x}).second.insert(index);
0245 }
0246 }
0247 }
0248 }
0249
0250 return houghHist;
0251 }
0252
0253 ActsExamples::HoughHist ActsExamples::HoughTransformSeeder::createHoughHist(
0254 int subregion) const {
0255 ActsExamples::HoughHist houghHist(
0256 Axis(0, m_cfg.houghHistSize_y, m_cfg.houghHistSize_y),
0257 Axis(0, m_cfg.houghHistSize_x, m_cfg.houghHistSize_x));
0258
0259 for (unsigned i = 0; i < m_cfg.nLayers; i++) {
0260 HoughHist layerHoughHist = createLayerHoughHist(i, subregion);
0261 for (unsigned x = 0; x < m_cfg.houghHistSize_x; ++x) {
0262 for (unsigned y = 0; y < m_cfg.houghHistSize_y; ++y) {
0263 if (layerHoughHist.atLocalBins({y, x}).first > 0) {
0264 houghHist.atLocalBins({y, x}).first++;
0265 houghHist.atLocalBins({y, x}).second.insert(
0266 layerHoughHist.atLocalBins({y, x}).second.begin(),
0267 layerHoughHist.atLocalBins({y, x}).second.end());
0268 }
0269 }
0270 }
0271 }
0272
0273 return houghHist;
0274 }
0275
0276 bool ActsExamples::HoughTransformSeeder::passThreshold(
0277 HoughHist const& houghHist, unsigned x, unsigned y) const {
0278
0279 unsigned width = m_cfg.threshold.size() / 2;
0280 if (x < width || m_cfg.houghHistSize_x - x < width) {
0281 return false;
0282 }
0283 for (unsigned i = 0; i < m_cfg.threshold.size(); i++) {
0284 if (houghHist.atLocalBins({y, x - width + i}).first < m_cfg.threshold[i]) {
0285 return false;
0286 }
0287 }
0288
0289
0290 if (m_cfg.localMaxWindowSize != 0) {
0291 for (int j = -m_cfg.localMaxWindowSize; j <= m_cfg.localMaxWindowSize;
0292 j++) {
0293 for (int i = -m_cfg.localMaxWindowSize; i <= m_cfg.localMaxWindowSize;
0294 i++) {
0295 if (i == 0 && j == 0) {
0296 continue;
0297 }
0298 if (y + j < m_cfg.houghHistSize_y && x + i < m_cfg.houghHistSize_x) {
0299 if (houghHist.atLocalBins({y + j, x + i}).first >
0300 houghHist.atLocalBins({y, x}).first) {
0301 return false;
0302 }
0303 if (houghHist.atLocalBins({y + j, x + i}).first ==
0304 houghHist.atLocalBins({y, x}).first) {
0305 if (houghHist.atLocalBins({y + j, x + i}).second.size() >
0306 houghHist.atLocalBins({y, x}).second.size()) {
0307 return false;
0308 }
0309 if (houghHist.atLocalBins({y + j, x + i}).second.size() ==
0310 houghHist.atLocalBins({y, x}).second.size() &&
0311 j <= 0 && i <= 0) {
0312 return false;
0313 }
0314 }
0315 }
0316 }
0317 }
0318 }
0319
0320 return true;
0321 }
0322
0323
0324
0325
0326
0327
0328 static inline int quant(double min, double max, unsigned nSteps, double val) {
0329 return static_cast<int>((val - min) / (max - min) * nSteps);
0330 }
0331
0332
0333 static inline double unquant(double min, double max, unsigned nSteps,
0334 int step) {
0335 return min + (max - min) * step / nSteps;
0336 }
0337
0338 template <typename T>
0339 static inline std::string to_string(std::vector<T> v) {
0340 std::ostringstream oss;
0341 oss << "[";
0342 if (!v.empty()) {
0343 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(oss, ", "));
0344 oss << v.back();
0345 }
0346 oss << "]";
0347 return oss.str();
0348 }
0349
0350 double ActsExamples::HoughTransformSeeder::yToX(double y, double r,
0351 double phi) const {
0352 double d0 = 0;
0353 double x =
0354 asin(r * ActsExamples::HoughTransformSeeder::m_cfg.kA * y - d0 / r) + phi;
0355
0356 if (m_cfg.fieldCorrector.connected()) {
0357 x += (m_cfg.fieldCorrector(0, y, r)).value();
0358 }
0359
0360 return x;
0361 }
0362
0363
0364
0365
0366 std::pair<unsigned, unsigned> ActsExamples::HoughTransformSeeder::yToXBins(
0367 std::size_t yBin_min, std::size_t yBin_max, double r, double phi,
0368 unsigned layer) const {
0369 double x_min = yToX(m_bins_y[yBin_min], r, phi);
0370 double x_max = yToX(m_bins_y[yBin_max], r, phi);
0371 if (x_min > x_max) {
0372 std::swap(x_min, x_max);
0373 }
0374 if (x_max < m_cfg.xMin || x_min > m_cfg.xMax) {
0375 return {0, 0};
0376 }
0377
0378
0379 int x_bin_min = quant(m_cfg.xMin, m_cfg.xMax, m_cfg.houghHistSize_x, x_min);
0380 int x_bin_max = quant(m_cfg.xMin, m_cfg.xMax, m_cfg.houghHistSize_x, x_max) +
0381 1;
0382
0383
0384 unsigned extend = getExtension(yBin_min, layer);
0385 x_bin_min -= extend;
0386 x_bin_max += extend;
0387
0388
0389 if (x_bin_min < 0) {
0390 x_bin_min = 0;
0391 }
0392 if (x_bin_max > static_cast<int>(m_cfg.houghHistSize_x)) {
0393 x_bin_max = m_cfg.houghHistSize_x;
0394 }
0395
0396 return {x_bin_min, x_bin_max};
0397 }
0398
0399
0400
0401 unsigned ActsExamples::HoughTransformSeeder::getExtension(
0402 unsigned y, unsigned layer) const {
0403 if (m_cfg.hitExtend_x.size() == m_cfg.nLayers) {
0404 return m_cfg.hitExtend_x[layer];
0405 }
0406
0407 if (m_cfg.hitExtend_x.size() == m_cfg.nLayers * 2) {
0408
0409
0410
0411 if (y < m_cfg.houghHistSize_y / 4 || y > 3 * m_cfg.houghHistSize_y / 4) {
0412 return m_cfg.hitExtend_x[layer];
0413 }
0414
0415 return m_cfg.hitExtend_x[m_cfg.nLayers + layer];
0416 }
0417 return 0;
0418 }
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435 std::vector<std::vector<int>>
0436 ActsExamples::HoughTransformSeeder::getComboIndices(
0437 std::vector<std::size_t>& sizes) const {
0438 std::size_t nCombs = 1;
0439 std::vector<std::size_t> nCombs_prior(sizes.size());
0440 std::vector<int> temp(sizes.size(), 0);
0441
0442 for (std::size_t i = 0; i < sizes.size(); i++) {
0443 if (sizes[i] > 0) {
0444 nCombs_prior[i] = nCombs;
0445 nCombs *= sizes[i];
0446 } else {
0447 temp[i] = -1;
0448 }
0449 }
0450
0451 std::vector<std::vector<int>> combos(nCombs, temp);
0452
0453 for (std::size_t icomb = 0; icomb < nCombs; icomb++) {
0454 std::size_t index = icomb;
0455 for (std::size_t isize = sizes.size() - 1; isize < sizes.size(); isize--) {
0456 if (sizes[isize] == 0) {
0457 continue;
0458 }
0459 combos[icomb][isize] = static_cast<int>(index / nCombs_prior[isize]);
0460 index = index % nCombs_prior[isize];
0461 }
0462 }
0463
0464 return combos;
0465 }
0466
0467 void ActsExamples::HoughTransformSeeder::addSpacePoints(
0468 const AlgorithmContext& ctx) const {
0469
0470
0471 for (const auto& isp : m_inputSpacePoints) {
0472 const auto& spContainer = (*isp)(ctx);
0473 ACTS_DEBUG("Inserting " << spContainer.size() << " space points from "
0474 << isp->key());
0475 for (auto& sp : spContainer) {
0476 double r = Acts::fastHypot(sp.x(), sp.y());
0477 double z = sp.z();
0478 float phi = std::atan2(sp.y(), sp.x());
0479 ResultUnsigned hitlayer = m_cfg.layerIDFinder(r).value();
0480 if (!(hitlayer.ok())) {
0481 continue;
0482 }
0483 std::vector<Index> indices;
0484 for (const auto& slink : sp.sourceLinks()) {
0485 const auto& islink = slink.get<IndexSourceLink>();
0486 indices.push_back(islink.index());
0487 }
0488
0489 auto meas =
0490 std::shared_ptr<HoughMeasurementStruct>(new HoughMeasurementStruct(
0491 hitlayer.value(), phi, r, z, indices, HoughHitType::SP));
0492 houghMeasurementStructs.push_back(meas);
0493 }
0494 }
0495 }
0496
0497 void ActsExamples::HoughTransformSeeder::addMeasurements(
0498 const AlgorithmContext& ctx) const {
0499 const auto& measurements = m_inputMeasurements(ctx);
0500
0501 ACTS_DEBUG("Inserting " << measurements.size() << " space points from "
0502 << m_cfg.inputMeasurements);
0503
0504 for (Acts::GeometryIdentifier geoId : m_cfg.geometrySelection) {
0505
0506 auto range =
0507 selectLowestNonZeroGeometryObject(measurements.orderedIndices(), geoId);
0508
0509
0510 auto groupedByModule = makeGroupBy(range, detail::GeometryIdGetter());
0511
0512 for (const auto& [moduleGeoId, moduleSourceLinks] : groupedByModule) {
0513
0514 const Acts::Surface* surface =
0515 m_cfg.trackingGeometry->findSurface(moduleGeoId);
0516 if (surface == nullptr) {
0517 ACTS_ERROR("Could not find surface " << moduleGeoId);
0518 return;
0519 }
0520
0521 for (auto& sourceLink : moduleSourceLinks) {
0522
0523
0524
0525
0526
0527
0528 const ConstVariableBoundMeasurementProxy measurement =
0529 measurements.getMeasurement(sourceLink.index());
0530
0531 assert(measurement.contains(Acts::eBoundLoc0) &&
0532 "Measurement does not contain the required bound loc0");
0533 assert(measurement.contains(Acts::eBoundLoc1) &&
0534 "Measurement does not contain the required bound loc1");
0535
0536 auto boundLoc0 = measurement.indexOf(Acts::eBoundLoc0);
0537 auto boundLoc1 = measurement.indexOf(Acts::eBoundLoc1);
0538
0539 Acts::Vector2 localPos{measurement.parameters()[boundLoc0],
0540 measurement.parameters()[boundLoc1]};
0541
0542
0543 Acts::Vector3 globalFakeMom(1, 1, 1);
0544 Acts::Vector3 globalPos =
0545 surface->localToGlobal(ctx.geoContext, localPos, globalFakeMom);
0546 double r = globalPos.head<2>().norm();
0547 double phi = std::atan2(globalPos[Acts::ePos1], globalPos[Acts::ePos0]);
0548 double z = globalPos[Acts::ePos2];
0549 ResultUnsigned hitlayer = m_cfg.layerIDFinder(r);
0550 if (hitlayer.ok()) {
0551 std::vector<Index> index;
0552 index.push_back(sourceLink.index());
0553 auto houghMeas = std::shared_ptr<HoughMeasurementStruct>(
0554 new HoughMeasurementStruct(hitlayer.value(), phi, r, z, index,
0555 HoughHitType::MEASUREMENT));
0556 houghMeasurementStructs.push_back(houghMeas);
0557 }
0558 }
0559 }
0560 }
0561 }