Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:01

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