Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:36

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2020 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/data/test_case.hpp>
0010 #include <boost/test/tools/output_test_stream.hpp>
0011 #include <boost/test/unit_test.hpp>
0012 
0013 #include "Acts/Definitions/Algebra.hpp"
0014 #include "Acts/Definitions/TrackParametrization.hpp"
0015 #include "Acts/Definitions/Units.hpp"
0016 #include "Acts/EventData/GenericBoundTrackParameters.hpp"
0017 #include "Acts/EventData/TrackParameters.hpp"
0018 #include "Acts/Geometry/GeometryContext.hpp"
0019 #include "Acts/Surfaces/PerigeeSurface.hpp"
0020 #include "Acts/Surfaces/Surface.hpp"
0021 #include "Acts/Utilities/Result.hpp"
0022 #include "Acts/Vertexing/GaussianGridTrackDensity.hpp"
0023 
0024 #include <algorithm>
0025 #include <cstddef>
0026 #include <memory>
0027 #include <optional>
0028 #include <utility>
0029 
0030 namespace bdata = boost::unit_test::data;
0031 using namespace Acts::UnitLiterals;
0032 
0033 namespace Acts::Test {
0034 
0035 using Covariance = BoundSquareMatrix;
0036 
0037 // Create a test context
0038 GeometryContext geoContext = GeometryContext();
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(mainGrid(mainGridSize - int((trkGridSize - 1) / 2) - 2),
0143                     0.);
0144   BOOST_CHECK_GT(mainGrid(mainGridSize - int((trkGridSize - 1) / 2) - 1), 0.);
0145   BOOST_CHECK_GT(mainGrid(mainGridSize - 1), 0.);
0146 
0147   binAndTrackGrid = grid.addTrack(params5, mainGrid);
0148   // Check lower boundary
0149   BOOST_CHECK_EQUAL(mainGrid(int((trkGridSize - 1) / 2) + 1), 0.);
0150   BOOST_CHECK_GT(mainGrid(int((trkGridSize - 1) / 2)), 0.);
0151   BOOST_CHECK_GT(mainGrid(0), 0.);
0152 
0153   // Check if position of maximum is correct
0154   auto maxRes = grid.getMaxZPosition(mainGrid);
0155   int maxBin = static_cast<int>((*maxRes / binSize) + mainGridSize / 2);
0156   BOOST_CHECK_EQUAL(maxBin, 0);
0157 
0158   // Check if error is thrown for empty grid
0159   mainGrid = Grid::MainGridVector::Zero(mainGridSize);
0160   auto maxResErr = grid.getMaxZPosition(mainGrid);
0161   BOOST_CHECK(!maxResErr.ok());
0162 
0163   // Check if removal of tracks works as desired
0164   binAndTrackGrid = grid.addTrack(params1, mainGrid);
0165   binAndTrackGrid = grid.addTrack(params2, mainGrid);
0166   // Copy grid for future reference
0167   gridCopy = mainGrid;
0168   binAndTrackGrid = grid.addTrack(params4, mainGrid);
0169   // Main grid should have changed by adding track4
0170   BOOST_CHECK_NE(gridCopy, mainGrid);
0171   // Remove track 4 again
0172   int zBin = binAndTrackGrid.first;
0173   auto trackGrid = binAndTrackGrid.second;
0174   grid.removeTrackGridFromMainGrid(zBin, trackGrid, mainGrid);
0175   // Check if it works
0176   BOOST_CHECK_EQUAL(gridCopy, mainGrid);
0177 }
0178 
0179 /// @brief Tests the functionality of the `useHighestSumZPosition` option
0180 BOOST_AUTO_TEST_CASE(gaussian_grid_sum_max_densitytest) {
0181   // Define the size of the grids
0182   constexpr int mainGridSize = 50;
0183   constexpr int trkGridSize = 11;
0184 
0185   using Grid = Acts::GaussianGridTrackDensity;
0186 
0187   double binSize = 0.1;  // mm
0188   double zMinMax = mainGridSize / 2 * binSize;
0189 
0190   // Set up grid density with zMinMax
0191   Grid::Config cfg(zMinMax, mainGridSize, trkGridSize);
0192   cfg.useHighestSumZPosition = true;
0193   Grid grid(cfg);
0194 
0195   // Create some test tracks
0196   Covariance covMat;
0197   covMat << 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0,
0198       0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2;
0199 
0200   const double posZ1 = -1.75;
0201   const double posZ2 = 1.75;
0202 
0203   // Take two tracks, track 1 is closer in d0 and will thus have a slightly
0204   // higher density
0205   BoundVector paramVec1;
0206   paramVec1 << 0.01, posZ1, 0, 0, 0, 0;
0207   BoundVector paramVec2;
0208   paramVec2 << 0.015, posZ2, 0, 0, 0, 0;
0209 
0210   // Create perigee surface
0211   std::shared_ptr<PerigeeSurface> perigeeSurface =
0212       Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
0213 
0214   BoundTrackParameters params1(perigeeSurface, paramVec1, covMat,
0215                                ParticleHypothesis::pion());
0216   BoundTrackParameters params2(perigeeSurface, paramVec2, covMat,
0217                                ParticleHypothesis::pion());
0218 
0219   // The grid to be filled
0220   Grid::MainGridVector mainGrid = Grid::MainGridVector::Zero(mainGridSize);
0221 
0222   // addTrack method returns the central z bin where the track density
0223   // grid was added and the track density grid itself for caching
0224   std::pair<int, Grid::TrackGridVector> binAndTrackGrid;
0225 
0226   binAndTrackGrid = grid.addTrack(params1, mainGrid);
0227   binAndTrackGrid = grid.addTrack(params2, mainGrid);
0228 
0229   // Artificially add some more density around the peak of track 2
0230   int maxZbin = static_cast<int>((posZ2 / binSize + mainGridSize / 2.));
0231   mainGrid(maxZbin - 1) += 1;
0232   mainGrid(maxZbin + 1) += 1;
0233 
0234   // Even though peak density of track 1 is slightly higher, track 2
0235   // has a higher sum of track densities including the peak and the two
0236   // surrounding bins and will be the output z position.
0237   auto maxRes = grid.getMaxZPosition(mainGrid);
0238   BOOST_CHECK(maxRes.ok());
0239   BOOST_CHECK_EQUAL(*maxRes, posZ2);
0240 }
0241 
0242 /// @brief Tests the seed width
0243 BOOST_AUTO_TEST_CASE(gaussian_grid_seed_width_test) {
0244   // Define the size of the grids
0245   constexpr int mainGridSize = 50;
0246   constexpr int trkGridSize = 11;
0247 
0248   using Grid = Acts::GaussianGridTrackDensity;
0249 
0250   double binSize = 0.1;  // mm
0251   double zMinMax = mainGridSize / 2 * binSize;
0252 
0253   // Set up grid density with zMinMax
0254   Grid::Config cfg(zMinMax, mainGridSize, trkGridSize);
0255   cfg.useHighestSumZPosition = true;
0256   Grid grid(cfg);
0257 
0258   // Create some test tracks
0259   Covariance covMat;
0260   covMat << 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0,
0261       0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2;
0262 
0263   const double posZ1 = -1.75;
0264   const double posZ2 = 1.75;
0265 
0266   // Take two tracks, track 1 is closer in d0 and will thus have a slightly
0267   // higher density
0268   BoundVector paramVec1;
0269   paramVec1 << 0.01, posZ1, 0, 0, 0, 0;
0270   BoundVector paramVec2;
0271   paramVec2 << 0.015, posZ2, 0, 0, 0, 0;
0272 
0273   // Create perigee surface
0274   std::shared_ptr<PerigeeSurface> perigeeSurface =
0275       Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
0276 
0277   BoundTrackParameters params1(perigeeSurface, paramVec1, covMat,
0278                                ParticleHypothesis::pion());
0279   BoundTrackParameters params2(perigeeSurface, paramVec2, covMat,
0280                                ParticleHypothesis::pion());
0281 
0282   // The grid to be filled
0283   Grid::MainGridVector mainGrid = Grid::MainGridVector::Zero(mainGridSize);
0284 
0285   // addTrack method returns the central z bin where the track density
0286   // grid was added and the track density grid itself for caching
0287   std::pair<int, Grid::TrackGridVector> binAndTrackGrid;
0288 
0289   binAndTrackGrid = grid.addTrack(params1, mainGrid);
0290   binAndTrackGrid = grid.addTrack(params2, mainGrid);
0291 
0292   // Artificially add some more density around the peak of track 2
0293   int maxZbin = static_cast<int>((posZ2 / binSize + mainGridSize / 2.));
0294   mainGrid(maxZbin - 1) += 1;
0295   mainGrid(maxZbin + 1) += 1;
0296 
0297   // Even though peak density of track 1 is slightly higher, track 2
0298   // has a higher sum of track densities including the peak and the two
0299   // surrounding bins and will be the output z position.
0300 
0301   auto maxRes = grid.getMaxZPositionAndWidth(mainGrid);
0302   BOOST_CHECK(maxRes.ok());
0303   double z = (*maxRes).first;
0304   double width = (*maxRes).second;
0305 
0306   BOOST_CHECK_EQUAL(z, posZ2);
0307   // Check that width was estimated
0308   BOOST_CHECK_NE(width, 0.);
0309 }
0310 
0311 }  // namespace Acts::Test