Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 09:20:48

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2026 Tom Bleher
0003 
0004 #include "B0TrackSeeding.h"
0005 
0006 #include <Acts/Definitions/Algebra.hpp>
0007 #include <Acts/Definitions/TrackParametrization.hpp>
0008 #include <Acts/Definitions/Units.hpp>
0009 #include <Acts/Seeding/EstimateTrackParamsFromSeed.hpp>
0010 #include <Acts/Utilities/Logger.hpp>
0011 #include <Acts/Utilities/MathHelpers.hpp>
0012 #include <Acts/Utilities/Result.hpp>
0013 #include <Acts/Utilities/UnitVectors.hpp>
0014 #include <Acts/Utilities/VectorHelpers.hpp>
0015 #include <edm4eic/Cov6f.h>
0016 #include <edm4eic/CovDiag3f.h>
0017 #include <edm4eic/unit_system.h>
0018 #include <edm4hep/Vector2f.h>
0019 #include <edm4hep/Vector3f.h>
0020 #include <Eigen/Core>
0021 #include <Eigen/Geometry>
0022 #include <algorithm>
0023 #include <array>
0024 #include <cmath>
0025 #include <cstdint>
0026 #include <span>
0027 #include <tuple>
0028 #include <utility>
0029 #include <vector>
0030 
0031 #include "ActsGeometryProvider.h"
0032 #include "B0TrackSeedingConfig.h"
0033 #include "TrackSeeding.h"
0034 
0035 // Acts version-specific includes
0036 #if TRACKSEEDING_HAS_SEEDING
0037 #include <Acts/Definitions/Direction.hpp>
0038 #include <Acts/EventData/SeedContainer.hpp>
0039 #include <Acts/EventData/SeedProxy.hpp>
0040 #include <Acts/EventData/SpacePointColumnProxy.hpp>
0041 #include <Acts/EventData/SpacePointColumns.hpp>
0042 #include <Acts/EventData/SpacePointContainer.hpp>
0043 #include <Acts/EventData/SpacePointProxy.hpp>
0044 #include <Acts/Seeding/BroadTripletSeedFilter.hpp>
0045 #include <Acts/Seeding/DoubletSeedFinder.hpp>
0046 #include <Acts/Seeding/TripletSeedFinder.hpp>
0047 #include <Acts/Seeding/TripletSeeder.hpp>
0048 
0049 namespace Acts {
0050 using SeedContainer2       = Acts::SeedContainer;
0051 using SpacePointContainer2 = Acts::SpacePointContainer;
0052 using ConstSeedProxy2      = Acts::ConstSeedProxy;
0053 } // namespace Acts
0054 #endif
0055 
0056 #if TRACKSEEDING_HAS_SEEDING2
0057 #include <Acts/Definitions/Direction.hpp>
0058 #include <Acts/EventData/SeedContainer2.hpp>
0059 #include <Acts/EventData/SeedProxy2.hpp>
0060 #include <Acts/EventData/SpacePointColumns.hpp>
0061 #include <Acts/EventData/SpacePointContainer2.hpp>
0062 #include <Acts/EventData/SpacePointProxy2.hpp>
0063 #include <Acts/Seeding2/BroadTripletSeedFilter.hpp>
0064 #include <Acts/Seeding2/DoubletSeedFinder.hpp>
0065 #include <Acts/Seeding2/TripletSeedFinder.hpp>
0066 #include <Acts/Seeding2/TripletSeeder.hpp>
0067 #endif
0068 
0069 namespace eicrecon {
0070 
0071 namespace {
0072 
0073   /// Opens a cut of the Acts finders, in Acts units
0074   constexpr float noLimit = 1.e5 * Acts::UnitConstants::mm;
0075 
0076   /// Adds the seed of an Acts triplet on a helix in the field at its middle hit, unless it has no
0077   /// finite non-zero q/p. Space point i is hit i, at positions[i] in Acts units.
0078   void addSeed(const B0TrackSeedingConfig& cfg, const Acts::ConstSeedProxy2& actsSeed,
0079                const edm4eic::TrackerHitCollection& hits, std::span<const Acts::Vector3> positions,
0080                const Acts::Vector3& field, edm4eic::TrackSeedCollection& seeds,
0081                edm4eic::TrackParametersCollection& parameters) {
0082     const auto triplet           = actsSeed.spacePointIndices();
0083     const Acts::Vector3& first   = positions[triplet[0]];
0084     const Acts::FreeVector helix = Acts::estimateTrackParamsFromSeed(
0085         first, 0., positions[triplet[1]], positions[triplet[2]], field);
0086     const double qOverP = helix[Acts::eFreeQOverP] * Acts::UnitConstants::GeV / edm4eic::unit::GeV;
0087     if (!helix.allFinite() || qOverP == 0.) {
0088       return;
0089     }
0090 
0091     // CKFTracking rebuilds the perigee along global z from the seed's point, so the seed is at
0092     // loc0 = loc1 = 0. Forward tracks run nearly parallel to it, so the loc1 and phi errors are
0093     // scaled up.
0094     const Acts::Vector3 direction = helix.segment<3>(Acts::eFreeDir0);
0095     const double theta            = Acts::VectorHelpers::theta(direction);
0096     const double sinTheta         = std::sin(theta);
0097     // The perigee is not defined for a track along its axis
0098     if (!(sinTheta > 0.)) {
0099       return;
0100     }
0101 
0102     edm4eic::Cov6f cov;
0103     cov(0, 0) = static_cast<float>(Acts::square(cfg.positionError));
0104     cov(1, 1) = static_cast<float>(Acts::square(cfg.positionError / sinTheta));
0105     cov(2, 2) = static_cast<float>(Acts::square(cfg.angleError / sinTheta));
0106     cov(3, 3) = static_cast<float>(Acts::square(cfg.angleError));
0107     cov(4, 4) = static_cast<float>(Acts::square(cfg.qOverPRelativeError * qOverP));
0108     cov(5, 5) = static_cast<float>(Acts::square(cfg.timeError));
0109 
0110     auto params = parameters.create();
0111     params.setType(-1); // seed
0112     params.setLoc({0.F, 0.F});
0113     params.setPhi(static_cast<float>(Acts::VectorHelpers::phi(direction)));
0114     params.setTheta(static_cast<float>(theta));
0115     params.setQOverP(static_cast<float>(qOverP));
0116     params.setTime(hits[triplet[0]].getTime());
0117     params.setCovariance(cov);
0118 
0119     const Acts::Vector3 anchor =
0120         (first - cfg.anchorDistance * direction) / Acts::UnitConstants::mm * edm4eic::unit::mm;
0121     auto seed = seeds.create();
0122     seed.setPerigee({static_cast<float>(anchor.x()), static_cast<float>(anchor.y()),
0123                      static_cast<float>(anchor.z())});
0124     seed.setQuality(actsSeed.quality());
0125     seed.setParams(params);
0126     for (const auto i : triplet) {
0127       seed.addToHits(hits[i]);
0128     }
0129   }
0130 
0131 } // namespace
0132 
0133 void B0TrackSeeding::init() {}
0134 
0135 void B0TrackSeeding::process(const Input& input, const Output& output) const {
0136   const auto [hits]        = input;
0137   auto [seeds, parameters] = output;
0138   findSeeds(*hits, *m_geoSvc->getFieldProvider(), m_geoSvc->getActsMagneticFieldContext(), *seeds,
0139             *parameters);
0140   debug("Created {} track seeds from {} hits", seeds->size(), hits->size());
0141 }
0142 
0143 void B0TrackSeeding::findSeeds(const edm4eic::TrackerHitCollection& hits,
0144                                const Acts::MagneticFieldProvider& fieldProvider,
0145                                const Acts::MagneticFieldContext& fieldContext,
0146                                edm4eic::TrackSeedCollection& seeds,
0147                                edm4eic::TrackParametersCollection& parameters) const {
0148   // The Acts seeder, with only what differs from its defaults. Every hit is tried as a middle hit;
0149   // in its frame, r' is about the distance along the beam and z' lies along the local field.
0150   // Doublets span stations. Tracks may come from decays upstream, so the origin is not cut on.
0151   Acts::DoubletSeedFinder::Config topConfig;
0152   topConfig.spacePointsSortedByRadius = false;
0153   topConfig.deltaRMin                 = m_cfg.stationGap;
0154   topConfig.deltaRMax                 = noLimit;
0155   topConfig.collisionRegionMin        = -noLimit;
0156   topConfig.collisionRegionMax        = noLimit;
0157 
0158   auto bottomConfig               = topConfig;
0159   bottomConfig.candidateDirection = Acts::Direction::Backward();
0160 
0161   Acts::TripletSeedFinder::Config tripletConfig;
0162   tripletConfig.minPt     = m_cfg.minPt;
0163   tripletConfig.impactMax = noLimit;
0164 
0165   // Seeds of a middle hit rank by the hits on other stations that confirm them. A confirming hit
0166   // only has to pass the slope cut: above about 10 GeV the curvature window of the filter
0167   // (deltaInvHelixDiameter) is wider than the curvature itself.
0168   // The impact parameter here comes from extrapolating the circle through the field-free beam line,
0169   // of order 100 mm at 41 GeV, not from where the track came from. The Acts default weight of 1 per
0170   // mm would make it compete with a confirming hit (+200), so it is lowered to a tie-break between
0171   // seeds of equal confirmation.
0172   Acts::BroadTripletSeedFilter::Config filterConfig;
0173   filterConfig.deltaRMin          = m_cfg.stationGap;
0174   filterConfig.impactWeightFactor = 1.e-4F;
0175 
0176   // Space point i is hit i, with its position in the frame of the current middle hit. All hits are
0177   // bottom and top candidates: the doublet finders keep those at least deltaRMin from the middle.
0178   const double toActs = Acts::UnitConstants::mm / edm4eic::unit::mm;
0179   std::vector<Acts::Vector3> positions;
0180   Acts::SpacePointContainer2 spacePoints(
0181       Acts::SpacePointColumns::PackedXY | Acts::SpacePointColumns::PackedZR |
0182       Acts::SpacePointColumns::VarianceZ | Acts::SpacePointColumns::VarianceR);
0183   for (const auto& hit : hits) {
0184     const auto pos = hit.getPosition();
0185     const auto cov = hit.getPositionError();
0186     positions.emplace_back(toActs * Acts::Vector3(pos.x, pos.y, pos.z));
0187     auto sp = spacePoints.createSpacePoint();
0188     sp.varianceZ() =
0189         static_cast<float>(Acts::square(toActs * m_cfg.hitErrorScale) * std::max(cov.xx, cov.yy));
0190     sp.varianceR() = sp.varianceZ();
0191   }
0192   std::array candidates{spacePoints.range({0, spacePoints.size()}).asConst()};
0193 
0194   Acts::BroadTripletSeedFilter::State filterState;
0195   Acts::BroadTripletSeedFilter::Cache filterCache;
0196   const Acts::BroadTripletSeedFilter filter(filterConfig, filterState, filterCache,
0197                                             Acts::getDummyLogger());
0198   const Acts::TripletSeeder seeder;
0199   Acts::TripletSeeder::Cache seederCache;
0200   Acts::SeedContainer2 actsSeeds;
0201 
0202   auto fieldCache = fieldProvider.makeCache(fieldContext);
0203 
0204   for (std::uint32_t m = 0; m < hits.size(); ++m) {
0205     const auto fieldResult = fieldProvider.getField(positions[m], fieldCache);
0206     // No field outside the field map
0207     if (!fieldResult.ok() || !(fieldResult.value().norm() > 0.)) {
0208       continue;
0209     }
0210     const Acts::Vector3 field = fieldResult.value();
0211     const auto bField         = static_cast<float>(field.norm());
0212 
0213     const Acts::Vector3 fieldDirection = field.normalized();
0214     const auto [u, v]                  = Acts::createCurvilinearUnitVectors(fieldDirection);
0215     for (std::uint32_t i = 0; i < hits.size(); ++i) {
0216       const auto x = static_cast<float>(u.dot(positions[i]));
0217       const auto y = static_cast<float>(v.dot(positions[i]));
0218       auto sp      = spacePoints[i];
0219       sp.xy()      = {x, y};
0220       sp.zr()      = {static_cast<float>(fieldDirection.dot(positions[i])), std::hypot(x, y)};
0221     }
0222 
0223     // The Acts finders take |B| when they are created. Unlike in TrackSeeding, it cannot be fixed
0224     // once in init(): it varies by about a factor 2 across the B0 tracker and with the beam setup.
0225     // The middle radius range only applies to hits sorted in radius.
0226     actsSeeds.clear();
0227     seeder.createSeedsFromGroups(
0228         seederCache, *Acts::DoubletSeedFinder::create({bottomConfig, bField}),
0229         *Acts::DoubletSeedFinder::create({topConfig, bField}),
0230         *Acts::TripletSeedFinder::create({tripletConfig, bField}), filter, spacePoints, candidates,
0231         spacePoints.range({m, m + 1}).asConst(), candidates, {0.F, 0.F}, actsSeeds);
0232 
0233     for (const Acts::ConstSeedProxy2 actsSeed : std::as_const(actsSeeds)) {
0234       addSeed(m_cfg, actsSeed, hits, positions, field, seeds, parameters);
0235     }
0236   }
0237 }
0238 
0239 } // namespace eicrecon