File indexing completed on 2025-10-21 08:07:26
0001
0002
0003
0004 #include "TrackSeeding.h"
0005
0006 #include <Acts/Definitions/Algebra.hpp>
0007 #include <Acts/Definitions/Units.hpp>
0008 #if Acts_VERSION_MAJOR >= 37
0009 #include <Acts/EventData/SpacePointProxy.hpp>
0010 #endif
0011 #include <Acts/Seeding/SeedFinderUtils.hpp>
0012 #if Acts_VERSION_MAJOR >= 37
0013 #include <Acts/EventData/Seed.hpp>
0014 #else
0015 #include <Acts/Seeding/Seed.hpp>
0016 #endif
0017 #include <Acts/Seeding/SeedConfirmationRangeConfig.hpp>
0018 #include <Acts/Seeding/SeedFilter.hpp>
0019 #include <Acts/Seeding/SeedFilterConfig.hpp>
0020 #include <Acts/Seeding/SeedFinderConfig.hpp>
0021 #include <Acts/Seeding/SeedFinderOrthogonal.hpp>
0022 #include <Acts/Seeding/SeedFinderOrthogonalConfig.hpp>
0023 #include <Acts/Surfaces/PerigeeSurface.hpp>
0024 #include <Acts/Surfaces/Surface.hpp>
0025 #include <Acts/Utilities/KDTree.hpp> // IWYU pragma: keep FIXME KDTree missing in SeedFinderOrthogonal.hpp until Acts v23.0.0
0026 #include <Acts/Utilities/Result.hpp>
0027 #include <edm4eic/Cov6f.h>
0028 #include <edm4hep/Vector2f.h>
0029 #include <Eigen/Core>
0030 #include <Eigen/Geometry>
0031 #include <array>
0032 #include <cmath>
0033 #include <gsl/pointers>
0034 #include <limits>
0035 #include <tuple>
0036
0037 namespace eicrecon {
0038
0039 void TrackSeeding::init() {
0040
0041
0042 m_seedFilterConfig.maxSeedsPerSpM = m_cfg.maxSeedsPerSpM_filter;
0043 m_seedFilterConfig.deltaRMin = m_cfg.deltaRMin;
0044 m_seedFilterConfig.seedConfirmation = m_cfg.seedConfirmation;
0045 m_seedFilterConfig.deltaInvHelixDiameter = m_cfg.deltaInvHelixDiameter;
0046 m_seedFilterConfig.impactWeightFactor = m_cfg.impactWeightFactor;
0047 m_seedFilterConfig.zOriginWeightFactor = m_cfg.zOriginWeightFactor;
0048 m_seedFilterConfig.compatSeedWeight = m_cfg.compatSeedWeight;
0049 m_seedFilterConfig.compatSeedLimit = m_cfg.compatSeedLimit;
0050 m_seedFilterConfig.seedWeightIncrement = m_cfg.seedWeightIncrement;
0051
0052 m_seedFilterConfig.centralSeedConfirmationRange = Acts::SeedConfirmationRangeConfig{
0053 .zMinSeedConf = m_cfg.zMinSeedConfCentral,
0054 .zMaxSeedConf = m_cfg.zMaxSeedConfCentral,
0055 .rMaxSeedConf = m_cfg.rMaxSeedConfCentral,
0056 .nTopForLargeR = m_cfg.nTopForLargeRCentral,
0057 .nTopForSmallR = m_cfg.nTopForSmallRCentral,
0058 .seedConfMinBottomRadius = m_cfg.seedConfMinBottomRadiusCentral,
0059 .seedConfMaxZOrigin = m_cfg.seedConfMaxZOriginCentral,
0060 .minImpactSeedConf = m_cfg.minImpactSeedConfCentral};
0061
0062 m_seedFilterConfig.forwardSeedConfirmationRange = Acts::SeedConfirmationRangeConfig{
0063 .zMinSeedConf = m_cfg.zMinSeedConfForward,
0064 .zMaxSeedConf = m_cfg.zMaxSeedConfForward,
0065 .rMaxSeedConf = m_cfg.rMaxSeedConfForward,
0066 .nTopForLargeR = m_cfg.nTopForLargeRForward,
0067 .nTopForSmallR = m_cfg.nTopForSmallRForward,
0068 .seedConfMinBottomRadius = m_cfg.seedConfMinBottomRadiusForward,
0069 .seedConfMaxZOrigin = m_cfg.seedConfMaxZOriginForward,
0070 .minImpactSeedConf = m_cfg.minImpactSeedConfForward};
0071
0072 m_seedFilterConfig = m_seedFilterConfig.toInternalUnits();
0073
0074
0075 #if Acts_VERSION_MAJOR >= 37
0076 m_seedFinderConfig.seedFilter =
0077 std::make_unique<Acts::SeedFilter<proxy_type>>(m_seedFilterConfig);
0078 #else
0079 m_seedFinderConfig.seedFilter = std::make_unique<Acts::SeedFilter<eicrecon::SpacePoint>>(
0080 Acts::SeedFilter<eicrecon::SpacePoint>(m_seedFilterConfig));
0081 #endif
0082 m_seedFinderConfig.rMax = m_cfg.rMax;
0083 m_seedFinderConfig.rMin = m_cfg.rMin;
0084 m_seedFinderConfig.deltaRMinTopSP = m_cfg.deltaRMinTopSP;
0085 m_seedFinderConfig.deltaRMaxTopSP = m_cfg.deltaRMaxTopSP;
0086 m_seedFinderConfig.deltaRMinBottomSP = m_cfg.deltaRMinBottomSP;
0087 m_seedFinderConfig.deltaRMaxBottomSP = m_cfg.deltaRMaxBottomSP;
0088 m_seedFinderConfig.collisionRegionMin = m_cfg.collisionRegionMin;
0089 m_seedFinderConfig.collisionRegionMax = m_cfg.collisionRegionMax;
0090 m_seedFinderConfig.zMin = m_cfg.zMin;
0091 m_seedFinderConfig.zMax = m_cfg.zMax;
0092 m_seedFinderConfig.maxSeedsPerSpM = m_cfg.maxSeedsPerSpM;
0093 m_seedFinderConfig.cotThetaMax = m_cfg.cotThetaMax;
0094 m_seedFinderConfig.sigmaScattering = m_cfg.sigmaScattering;
0095 m_seedFinderConfig.radLengthPerSeed = m_cfg.radLengthPerSeed;
0096 m_seedFinderConfig.minPt = m_cfg.minPt;
0097 m_seedFinderConfig.impactMax = m_cfg.impactMax;
0098 m_seedFinderConfig.rMinMiddle = m_cfg.rMinMiddle;
0099 m_seedFinderConfig.rMaxMiddle = m_cfg.rMaxMiddle;
0100 m_seedFinderConfig.deltaPhiMax = m_cfg.deltaPhiMax;
0101
0102 m_seedFinderOptions.beamPos = Acts::Vector2(m_cfg.beamPosX, m_cfg.beamPosY);
0103 m_seedFinderOptions.bFieldInZ = m_cfg.bFieldInZ;
0104
0105 m_seedFinderConfig = m_seedFinderConfig.toInternalUnits().calculateDerivedQuantities();
0106 m_seedFinderOptions =
0107 m_seedFinderOptions.toInternalUnits().calculateDerivedQuantities(m_seedFinderConfig);
0108 }
0109
0110 void TrackSeeding::process(const Input& input, const Output& output) const {
0111
0112 const auto [trk_hits] = input;
0113 auto [trackparams] = output;
0114
0115 std::vector<const eicrecon::SpacePoint*> spacePoints = getSpacePoints(*trk_hits);
0116
0117 #if Acts_VERSION_MAJOR >= 37
0118 Acts::SeedFinderOrthogonal<proxy_type> finder(m_seedFinderConfig);
0119 #else
0120 Acts::SeedFinderOrthogonal<eicrecon::SpacePoint> finder(
0121 m_seedFinderConfig);
0122 #endif
0123
0124 #if Acts_VERSION_MAJOR >= 37
0125
0126 Acts::SpacePointContainerConfig spConfig;
0127
0128
0129 Acts::SpacePointContainerOptions spOptions;
0130 spOptions.beamPos = {0., 0.};
0131
0132 ActsExamples::SpacePointContainer container(spacePoints);
0133 Acts::SpacePointContainer<decltype(container), Acts::detail::RefHolder> spContainer(
0134 spConfig, spOptions, container);
0135
0136 std::vector<Acts::Seed<proxy_type>> seeds = finder.createSeeds(m_seedFinderOptions, spContainer);
0137
0138
0139 eicrecon::SeedContainer seedsToAdd;
0140 seedsToAdd.reserve(seeds.size());
0141 for (const auto& seed : seeds) {
0142 const auto& sps = seed.sp();
0143 seedsToAdd.emplace_back(*sps[0]->externalSpacePoint(), *sps[1]->externalSpacePoint(),
0144 *sps[2]->externalSpacePoint());
0145 seedsToAdd.back().setVertexZ(seed.z());
0146 seedsToAdd.back().setQuality(seed.seedQuality());
0147 }
0148
0149 addToTrackParams(*trackparams, seedsToAdd);
0150
0151 #else
0152
0153 std::function<std::tuple<Acts::Vector3, Acts::Vector2, std::optional<Acts::ActsScalar>>(
0154 const eicrecon::SpacePoint* sp)>
0155 create_coordinates = [](const eicrecon::SpacePoint* sp) {
0156 Acts::Vector3 position(sp->x(), sp->y(), sp->z());
0157 Acts::Vector2 variance(sp->varianceR(), sp->varianceZ());
0158 return std::make_tuple(position, variance, sp->t());
0159 };
0160
0161 eicrecon::SeedContainer seeds =
0162 finder.createSeeds(m_seedFinderOptions, spacePoints, create_coordinates);
0163
0164 addToTrackParams(*trackparams, seeds);
0165
0166 #endif
0167
0168 for (auto& sp : spacePoints) {
0169 delete sp;
0170 }
0171 }
0172
0173 std::vector<const eicrecon::SpacePoint*>
0174 TrackSeeding::getSpacePoints(const edm4eic::TrackerHitCollection& trk_hits) {
0175 std::vector<const eicrecon::SpacePoint*> spacepoints;
0176
0177 for (const auto hit : trk_hits) {
0178 const eicrecon::SpacePoint* sp = new SpacePoint(hit);
0179 spacepoints.push_back(sp);
0180 }
0181
0182 return spacepoints;
0183 }
0184
0185 void TrackSeeding::addToTrackParams(edm4eic::TrackParametersCollection& trackparams,
0186 SeedContainer& seeds) const {
0187
0188 for (auto& seed : seeds) {
0189 std::vector<std::pair<float, float>> xyHitPositions;
0190 std::vector<std::pair<float, float>> rzHitPositions;
0191 for (const auto& spptr : seed.sp()) {
0192 xyHitPositions.emplace_back(spptr->x(), spptr->y());
0193 rzHitPositions.emplace_back(spptr->r(), spptr->z());
0194 }
0195
0196 auto RX0Y0 = circleFit(xyHitPositions);
0197 float R = std::get<0>(RX0Y0);
0198 float X0 = std::get<1>(RX0Y0);
0199 float Y0 = std::get<2>(RX0Y0);
0200 if (!(std::isfinite(R) && std::isfinite(std::abs(X0)) && std::isfinite(std::abs(Y0)))) {
0201
0202 continue;
0203 }
0204 if (std::hypot(X0, Y0) < std::numeric_limits<decltype(std::hypot(X0, Y0))>::epsilon() ||
0205 !std::isfinite(std::hypot(X0, Y0))) {
0206
0207
0208 continue;
0209 }
0210
0211 auto slopeZ0 = lineFit(rzHitPositions);
0212 const auto xypos = findPCA(RX0Y0);
0213
0214
0215 int charge = determineCharge(xyHitPositions, xypos, RX0Y0);
0216
0217 float theta = atan(1. / std::get<0>(slopeZ0));
0218
0219 if (theta < 0) {
0220 theta += M_PI;
0221 }
0222 float eta = -log(tan(theta / 2.));
0223 float pt = R * m_cfg.bFieldInZ;
0224 float p = pt * cosh(eta);
0225 float qOverP = charge / p;
0226
0227
0228 auto xpos = xypos.first;
0229 auto ypos = xypos.second;
0230
0231 auto vxpos = -1. * charge * (ypos - Y0);
0232 auto vypos = charge * (xpos - X0);
0233
0234 auto phi = atan2(vypos, vxpos);
0235
0236 const float z0 = seed.z();
0237 auto perigee = Acts::Surface::makeShared<Acts::PerigeeSurface>(Acts::Vector3(0, 0, 0));
0238 Acts::Vector3 global(xypos.first, xypos.second, z0);
0239
0240
0241 Acts::Vector2 localpos;
0242 Acts::Vector3 direction(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
0243
0244 auto local = perigee->globalToLocal(m_geoSvc->getActsGeometryContext(), global, direction);
0245
0246 if (!local.ok()) {
0247 continue;
0248 }
0249
0250 localpos = local.value();
0251
0252 auto trackparam = trackparams.create();
0253 trackparam.setType(-1);
0254 trackparam.setLoc({static_cast<float>(localpos(0)),
0255 static_cast<float>(localpos(1))});
0256 trackparam.setPhi(static_cast<float>(phi));
0257 trackparam.setTheta(theta);
0258 trackparam.setQOverP(qOverP);
0259 trackparam.setTime(10);
0260 edm4eic::Cov6f cov;
0261 cov(0, 0) = m_cfg.locaError / Acts::UnitConstants::mm;
0262 cov(1, 1) = m_cfg.locbError / Acts::UnitConstants::mm;
0263 cov(2, 2) = m_cfg.phiError / Acts::UnitConstants::rad;
0264 cov(3, 3) = m_cfg.thetaError / Acts::UnitConstants::rad;
0265 cov(4, 4) = m_cfg.qOverPError * Acts::UnitConstants::GeV;
0266 cov(5, 5) = m_cfg.timeError / Acts::UnitConstants::ns;
0267 trackparam.setCovariance(cov);
0268 }
0269 }
0270
0271 std::pair<float, float> TrackSeeding::findPCA(std::tuple<float, float, float>& circleParams) {
0272 const float R = std::get<0>(circleParams);
0273 const float X0 = std::get<1>(circleParams);
0274 const float Y0 = std::get<2>(circleParams);
0275
0276 const double R0 = std::hypot(X0, Y0);
0277
0278
0279 const double xmin = X0 * (1. - R / R0);
0280 const double ymin = Y0 * (1. - R / R0);
0281
0282 return std::make_pair(xmin, ymin);
0283 }
0284
0285 int TrackSeeding::determineCharge(std::vector<std::pair<float, float>>& positions,
0286 const std::pair<float, float>& PCA,
0287 std::tuple<float, float, float>& RX0Y0) {
0288
0289 const auto& firstpos = positions.at(0);
0290 auto hit_x = firstpos.first;
0291 auto hit_y = firstpos.second;
0292
0293 auto xpos = PCA.first;
0294 auto ypos = PCA.second;
0295
0296 float X0 = std::get<1>(RX0Y0);
0297 float Y0 = std::get<2>(RX0Y0);
0298
0299 Acts::Vector3 B_z(0, 0, 1);
0300 Acts::Vector3 radial(X0 - xpos, Y0 - ypos, 0);
0301 Acts::Vector3 hit(hit_x - xpos, hit_y - ypos, 0);
0302
0303 auto cross = radial.cross(hit);
0304
0305 float dot = cross.dot(B_z);
0306
0307 return copysign(1., -dot);
0308 }
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322 std::tuple<float, float, float>
0323 TrackSeeding::circleFit(std::vector<std::pair<float, float>>& positions) {
0324
0325 double meanX = 0;
0326 double meanY = 0;
0327 double weight = 0;
0328
0329 for (const auto& [x, y] : positions) {
0330 meanX += x;
0331 meanY += y;
0332 ++weight;
0333 }
0334 meanX /= weight;
0335 meanY /= weight;
0336
0337
0338
0339 double Mxx = 0;
0340 double Myy = 0;
0341 double Mxy = 0;
0342 double Mxz = 0;
0343 double Myz = 0;
0344 double Mzz = 0;
0345
0346 for (auto& [x, y] : positions) {
0347 double Xi = x - meanX;
0348 double Yi = y - meanY;
0349 double Zi = std::pow(Xi, 2) + std::pow(Yi, 2);
0350
0351 Mxy += Xi * Yi;
0352 Mxx += Xi * Xi;
0353 Myy += Yi * Yi;
0354 Mxz += Xi * Zi;
0355 Myz += Yi * Zi;
0356 Mzz += Zi * Zi;
0357 }
0358 Mxx /= weight;
0359 Myy /= weight;
0360 Mxy /= weight;
0361 Mxz /= weight;
0362 Myz /= weight;
0363 Mzz /= weight;
0364
0365
0366
0367 const double Mz = Mxx + Myy;
0368 const double Cov_xy = Mxx * Myy - Mxy * Mxy;
0369 const double Var_z = Mzz - Mz * Mz;
0370 const double A3 = 4 * Mz;
0371 const double A2 = -3 * Mz * Mz - Mzz;
0372 const double A1 = Var_z * Mz + 4 * Cov_xy * Mz - Mxz * Mxz - Myz * Myz;
0373 const double A0 = Mxz * (Mxz * Myy - Myz * Mxy) + Myz * (Myz * Mxx - Mxz * Mxy) - Var_z * Cov_xy;
0374 const double A22 = A2 + A2;
0375 const double A33 = A3 + A3 + A3;
0376
0377
0378
0379
0380 static constexpr int iter_max = 99;
0381 double x = 0;
0382 double y = A0;
0383
0384
0385 for (int iter = 0; iter < iter_max; ++iter) {
0386 const double Dy = A1 + x * (A22 + A33 * x);
0387 const double xnew = x - y / Dy;
0388 if ((xnew == x) || (!std::isfinite(xnew))) {
0389 break;
0390 }
0391
0392 const double ynew = A0 + xnew * (A1 + xnew * (A2 + xnew * A3));
0393 if (std::abs(ynew) >= std::abs(y)) {
0394 break;
0395 }
0396
0397 x = xnew;
0398 y = ynew;
0399 }
0400
0401
0402 const double DET = std::pow(x, 2) - x * Mz + Cov_xy;
0403 const double Xcenter = (Mxz * (Myy - x) - Myz * Mxy) / DET / 2;
0404 const double Ycenter = (Myz * (Mxx - x) - Mxz * Mxy) / DET / 2;
0405
0406
0407 float X0 = Xcenter + meanX;
0408 float Y0 = Ycenter + meanY;
0409 float R = std::sqrt(std::pow(Xcenter, 2) + std::pow(Ycenter, 2) + Mz);
0410 return std::make_tuple(R, X0, Y0);
0411 }
0412
0413 std::tuple<float, float> TrackSeeding::lineFit(std::vector<std::pair<float, float>>& positions) {
0414 double xsum = 0;
0415 double x2sum = 0;
0416 double ysum = 0;
0417 double xysum = 0;
0418 for (const auto& [r, z] : positions) {
0419 xsum = xsum + r;
0420 ysum = ysum + z;
0421 x2sum = x2sum + std::pow(r, 2);
0422 xysum = xysum + r * z;
0423 }
0424
0425 const auto npts = positions.size();
0426 const double denominator = (x2sum * npts - std::pow(xsum, 2));
0427 const float a = (xysum * npts - xsum * ysum) / denominator;
0428 const float b = (x2sum * ysum - xsum * xysum) / denominator;
0429 return std::make_tuple(a, b);
0430 }
0431
0432 }