Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-18 08:22:57

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/TrackParametrization.hpp"
0013 #include "Acts/Definitions/Units.hpp"
0014 #include "Acts/EventData/GenericBoundTrackParameters.hpp"
0015 #include "Acts/EventData/TrackParameters.hpp"
0016 #include "Acts/Geometry/GeometryContext.hpp"
0017 #include "Acts/Surfaces/PerigeeSurface.hpp"
0018 #include "Acts/Surfaces/Surface.hpp"
0019 #include "Acts/Utilities/Result.hpp"
0020 #include "Acts/Vertexing/GaussianGridTrackDensity.hpp"
0021 
0022 #include <algorithm>
0023 #include <cstddef>
0024 #include <memory>
0025 #include <optional>
0026 #include <utility>
0027 
0028 using namespace Acts;
0029 using namespace Acts::UnitLiterals;
0030 
0031 namespace ActsTests {
0032 
0033 using Covariance = BoundSquareMatrix;
0034 
0035 // Create a test context
0036 GeometryContext geoContext = GeometryContext();
0037 
0038 BOOST_AUTO_TEST_SUITE(VertexingSuite)
0039 
0040 BOOST_AUTO_TEST_CASE(gaussian_grid_density_test) {
0041   // Define the size of the grids
0042   constexpr std::size_t mainGridSize = 400;
0043   constexpr std::size_t trkGridSize = 15;
0044 
0045   using Grid = GaussianGridTrackDensity;
0046 
0047   double binSize = 0.1;  // mm
0048   double zMinMax = mainGridSize / 2 * binSize;
0049 
0050   // Set up grid density with zMinMax
0051   Grid::Config cfg(zMinMax, mainGridSize, trkGridSize);
0052   Grid grid(cfg);
0053 
0054   // Create some test tracks
0055   Covariance covMat;
0056   covMat << 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0,
0057       0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1;
0058 
0059   BoundVector paramVec1;
0060   paramVec1 << 0.01, 0.15, 0, 0, 0, 0;
0061 
0062   BoundVector paramVec2;
0063   paramVec2 << trkGridSize * binSize - 0.1, 0.15, 0, 0, 0, 0;
0064 
0065   BoundVector paramVec3;
0066   paramVec3 << trkGridSize * binSize + 0.01, 0.15, 0, 0, 0, 0;
0067 
0068   BoundVector paramVec3_1;
0069   paramVec3_1 << -(trkGridSize * binSize + 0.01), 0.15, 0, 0, 0, 0;
0070 
0071   BoundVector paramVec4;
0072   paramVec4 << 0.01, 19.95, 0, 0, 0, 0;
0073 
0074   BoundVector paramVec5;
0075   paramVec5 << 0.01, -19.95, 0, 0, 0, 0;
0076 
0077   BoundVector paramVec6;
0078   paramVec6 << 0.01, -100.0, 0, 0, 0, 0;
0079 
0080   BoundVector paramVec7;
0081   paramVec7 << 0.01, +100.0, 0, 0, 0, 0;
0082 
0083   // Create perigee surface
0084   std::shared_ptr<PerigeeSurface> perigeeSurface =
0085       Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
0086 
0087   BoundTrackParameters params1(perigeeSurface, paramVec1, covMat,
0088                                ParticleHypothesis::pion());
0089   BoundTrackParameters params2(perigeeSurface, paramVec2, covMat,
0090                                ParticleHypothesis::pion());
0091   BoundTrackParameters params3(perigeeSurface, paramVec3, covMat,
0092                                ParticleHypothesis::pion());
0093   BoundTrackParameters params3_1(perigeeSurface, paramVec3_1, covMat,
0094                                  ParticleHypothesis::pion());
0095   BoundTrackParameters params4(perigeeSurface, paramVec4, covMat,
0096                                ParticleHypothesis::pion());
0097   BoundTrackParameters params5(perigeeSurface, paramVec5, covMat,
0098                                ParticleHypothesis::pion());
0099   BoundTrackParameters params6(perigeeSurface, paramVec6, covMat,
0100                                ParticleHypothesis::pion());
0101   BoundTrackParameters params7(perigeeSurface, paramVec7, covMat,
0102                                ParticleHypothesis::pion());
0103 
0104   // The grid to be filled
0105   Grid::MainGridVector mainGrid = Grid::MainGridVector::Zero(mainGridSize);
0106 
0107   // addTrack method returns the central z bin where the track density
0108   // grid was added and the track density grid itself for caching
0109   std::pair<int, Grid::TrackGridVector> binAndTrackGrid;
0110 
0111   // Adds tracks too far away in transverse distance
0112   binAndTrackGrid = grid.addTrack(params3, mainGrid);
0113   binAndTrackGrid = grid.addTrack(params3_1, mainGrid);
0114   // Adds tracks too far away in longitudinal distance
0115   binAndTrackGrid = grid.addTrack(params6, mainGrid);
0116   binAndTrackGrid = grid.addTrack(params7, mainGrid);
0117 
0118   // Tracks are far away from z-axis (or not in region of interest) and
0119   // should not have contributed to density grid
0120   auto zeroGrid = Grid::MainGridVector::Zero(mainGridSize);
0121   BOOST_CHECK_EQUAL(mainGrid, zeroGrid);
0122 
0123   // Now add track 1 and 2 to grid, separately.
0124   binAndTrackGrid = grid.addTrack(params1, mainGrid);
0125   auto gridCopy = mainGrid;
0126 
0127   mainGrid = Grid::MainGridVector::Zero(mainGridSize);
0128   binAndTrackGrid = grid.addTrack(params2, mainGrid);
0129 
0130   // Track 1 is closer to z-axis and should thus yield higher
0131   // density values
0132   BOOST_CHECK_GT(gridCopy.sum(), mainGrid.sum());
0133 
0134   // Track 1 and 2 summed should give higher densities than
0135   // only track 1 alone
0136   binAndTrackGrid = grid.addTrack(params1, mainGrid);
0137   BOOST_CHECK_EQUAL(gridCopy.sum(), mainGrid.sum());
0138 
0139   binAndTrackGrid = grid.addTrack(params4, mainGrid);
0140 
0141   // Check upper boundary
0142   BOOST_CHECK_EQUAL(
0143       mainGrid(mainGridSize - static_cast<int>((trkGridSize - 1) / 2) - 2), 0.);
0144   BOOST_CHECK_GT(
0145       mainGrid(mainGridSize - static_cast<int>((trkGridSize - 1) / 2) - 1), 0.);
0146   BOOST_CHECK_GT(mainGrid(mainGridSize - 1), 0.);
0147 
0148   binAndTrackGrid = grid.addTrack(params5, mainGrid);
0149   // Check lower boundary
0150   BOOST_CHECK_EQUAL(mainGrid(static_cast<int>((trkGridSize - 1) / 2) + 1), 0.);
0151   BOOST_CHECK_GT(mainGrid(static_cast<int>((trkGridSize - 1) / 2)), 0.);
0152   BOOST_CHECK_GT(mainGrid(0), 0.);
0153 
0154   // Check if position of maximum is correct
0155   auto maxRes = grid.getMaxZPosition(mainGrid);
0156   int maxBin = static_cast<int>((*maxRes / binSize) + mainGridSize / 2);
0157   BOOST_CHECK_EQUAL(maxBin, 0);
0158 
0159   // Check if error is thrown for empty grid
0160   mainGrid = Grid::MainGridVector::Zero(mainGridSize);
0161   auto maxResErr = grid.getMaxZPosition(mainGrid);
0162   BOOST_CHECK(!maxResErr.ok());
0163 
0164   // Check if removal of tracks works as desired
0165   binAndTrackGrid = grid.addTrack(params1, mainGrid);
0166   binAndTrackGrid = grid.addTrack(params2, mainGrid);
0167   // Copy grid for future reference
0168   gridCopy = mainGrid;
0169   binAndTrackGrid = grid.addTrack(params4, mainGrid);
0170   // Main grid should have changed by adding track4
0171   BOOST_CHECK_NE(gridCopy, mainGrid);
0172   // Remove track 4 again
0173   int zBin = binAndTrackGrid.first;
0174   auto trackGrid = binAndTrackGrid.second;
0175   grid.removeTrackGridFromMainGrid(zBin, trackGrid, mainGrid);
0176   // Check if it works
0177   BOOST_CHECK_EQUAL(gridCopy, mainGrid);
0178 }
0179 
0180 /// @brief Tests the functionality of the `useHighestSumZPosition` option
0181 BOOST_AUTO_TEST_CASE(gaussian_grid_sum_max_densitytest) {
0182   // Define the size of the grids
0183   constexpr int mainGridSize = 50;
0184   constexpr int trkGridSize = 11;
0185 
0186   using Grid = Acts::GaussianGridTrackDensity;
0187 
0188   double binSize = 0.1;  // mm
0189   double zMinMax = mainGridSize / 2 * binSize;
0190 
0191   // Set up grid density with zMinMax
0192   Grid::Config cfg(zMinMax, mainGridSize, trkGridSize);
0193   cfg.useHighestSumZPosition = true;
0194   Grid grid(cfg);
0195 
0196   // Create some test tracks
0197   Covariance covMat;
0198   covMat << 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0,
0199       0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2;
0200 
0201   const double posZ1 = -1.75;
0202   const double posZ2 = 1.75;
0203 
0204   // Take two tracks, track 1 is closer in d0 and will thus have a slightly
0205   // higher density
0206   BoundVector paramVec1;
0207   paramVec1 << 0.01, posZ1, 0, 0, 0, 0;
0208   BoundVector paramVec2;
0209   paramVec2 << 0.015, posZ2, 0, 0, 0, 0;
0210 
0211   // Create perigee surface
0212   std::shared_ptr<PerigeeSurface> perigeeSurface =
0213       Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
0214 
0215   BoundTrackParameters params1(perigeeSurface, paramVec1, covMat,
0216                                ParticleHypothesis::pion());
0217   BoundTrackParameters params2(perigeeSurface, paramVec2, covMat,
0218                                ParticleHypothesis::pion());
0219 
0220   // The grid to be filled
0221   Grid::MainGridVector mainGrid = Grid::MainGridVector::Zero(mainGridSize);
0222 
0223   // addTrack method returns the central z bin where the track density
0224   // grid was added and the track density grid itself for caching
0225   std::pair<int, Grid::TrackGridVector> binAndTrackGrid;
0226 
0227   binAndTrackGrid = grid.addTrack(params1, mainGrid);
0228   binAndTrackGrid = grid.addTrack(params2, mainGrid);
0229 
0230   // Artificially add some more density around the peak of track 2
0231   int maxZbin = static_cast<int>((posZ2 / binSize + mainGridSize / 2.));
0232   mainGrid(maxZbin - 1) += 1;
0233   mainGrid(maxZbin + 1) += 1;
0234 
0235   // Even though peak density of track 1 is slightly higher, track 2
0236   // has a higher sum of track densities including the peak and the two
0237   // surrounding bins and will be the output z position.
0238   auto maxRes = grid.getMaxZPosition(mainGrid);
0239   BOOST_CHECK(maxRes.ok());
0240   BOOST_CHECK_EQUAL(*maxRes, posZ2);
0241 }
0242 
0243 /// @brief Tests the seed width
0244 BOOST_AUTO_TEST_CASE(gaussian_grid_seed_width_test) {
0245   // Define the size of the grids
0246   constexpr int mainGridSize = 50;
0247   constexpr int trkGridSize = 11;
0248 
0249   using Grid = Acts::GaussianGridTrackDensity;
0250 
0251   double binSize = 0.1;  // mm
0252   double zMinMax = mainGridSize / 2 * binSize;
0253 
0254   // Set up grid density with zMinMax
0255   Grid::Config cfg(zMinMax, mainGridSize, trkGridSize);
0256   cfg.useHighestSumZPosition = true;
0257   Grid grid(cfg);
0258 
0259   // Create some test tracks
0260   Covariance covMat;
0261   covMat << 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0,
0262       0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2;
0263 
0264   const double posZ1 = -1.75;
0265   const double posZ2 = 1.75;
0266 
0267   // Take two tracks, track 1 is closer in d0 and will thus have a slightly
0268   // higher density
0269   BoundVector paramVec1;
0270   paramVec1 << 0.01, posZ1, 0, 0, 0, 0;
0271   BoundVector paramVec2;
0272   paramVec2 << 0.015, posZ2, 0, 0, 0, 0;
0273 
0274   // Create perigee surface
0275   std::shared_ptr<PerigeeSurface> perigeeSurface =
0276       Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
0277 
0278   BoundTrackParameters params1(perigeeSurface, paramVec1, covMat,
0279                                ParticleHypothesis::pion());
0280   BoundTrackParameters params2(perigeeSurface, paramVec2, covMat,
0281                                ParticleHypothesis::pion());
0282 
0283   // The grid to be filled
0284   Grid::MainGridVector mainGrid = Grid::MainGridVector::Zero(mainGridSize);
0285 
0286   // addTrack method returns the central z bin where the track density
0287   // grid was added and the track density grid itself for caching
0288   std::pair<int, Grid::TrackGridVector> binAndTrackGrid;
0289 
0290   binAndTrackGrid = grid.addTrack(params1, mainGrid);
0291   binAndTrackGrid = grid.addTrack(params2, mainGrid);
0292 
0293   // Artificially add some more density around the peak of track 2
0294   int maxZbin = static_cast<int>((posZ2 / binSize + mainGridSize / 2.));
0295   mainGrid(maxZbin - 1) += 1;
0296   mainGrid(maxZbin + 1) += 1;
0297 
0298   // Even though peak density of track 1 is slightly higher, track 2
0299   // has a higher sum of track densities including the peak and the two
0300   // surrounding bins and will be the output z position.
0301 
0302   auto maxRes = grid.getMaxZPositionAndWidth(mainGrid);
0303   BOOST_CHECK(maxRes.ok());
0304   double z = (*maxRes).first;
0305   double width = (*maxRes).second;
0306 
0307   BOOST_CHECK_EQUAL(z, posZ2);
0308   // Check that width was estimated
0309   BOOST_CHECK_NE(width, 0.);
0310 }
0311 
0312 BOOST_AUTO_TEST_SUITE_END()
0313 
0314 }  // namespace ActsTests