EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GaussianGridTrackDensityTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GaussianGridTrackDensityTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/data/test_case.hpp>
10 #include <boost/test/tools/output_test_stream.hpp>
11 #include <boost/test/unit_test.hpp>
12 
17 #include "Acts/Utilities/Units.hpp"
19 
20 namespace bdata = boost::unit_test::data;
21 using namespace Acts::UnitLiterals;
22 
23 namespace Acts {
24 namespace Test {
25 
27 
28 // Create a test context
30 
31 BOOST_AUTO_TEST_CASE(gaussian_grid_density_test) {
32  // Define the size of the grids
33  const int mainGridSize = 400;
34  const int trkGridSize = 15;
35 
36  double binSize = 0.1; // mm
37  double zMinMax = mainGridSize / 2 * binSize;
38 
39  // Set up grid density with zMinMax
42 
43  // Create some test tracks
44  Covariance covMat;
45  covMat << 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0,
46  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1;
47 
48  BoundVector paramVec1;
49  paramVec1 << 0.01, 0.15, 0, 0, 0, 0;
50 
51  BoundVector paramVec2;
52  paramVec2 << trkGridSize * binSize - 0.1, 0.15, 0, 0, 0, 0;
53 
54  BoundVector paramVec3;
55  paramVec3 << trkGridSize * binSize + 0.01, 0.15, 0, 0, 0, 0;
56 
57  BoundVector paramVec3_1;
58  paramVec3_1 << -(trkGridSize * binSize + 0.01), 0.15, 0, 0, 0, 0;
59 
60  BoundVector paramVec4;
61  paramVec4 << 0.01, 19.95, 0, 0, 0, 0;
62 
63  BoundVector paramVec5;
64  paramVec5 << 0.01, -19.95, 0, 0, 0, 0;
65 
66  BoundVector paramVec6;
67  paramVec6 << 0.01, -100.0, 0, 0, 0, 0;
68 
69  BoundVector paramVec7;
70  paramVec7 << 0.01, +100.0, 0, 0, 0, 0;
71 
72  // Create perigee surface
73  std::shared_ptr<PerigeeSurface> perigeeSurface =
74  Surface::makeShared<PerigeeSurface>(Vector3D(0., 0., 0.));
75 
76  BoundTrackParameters params1(perigeeSurface, paramVec1, covMat);
77  BoundTrackParameters params2(perigeeSurface, paramVec2, covMat);
78  BoundTrackParameters params3(perigeeSurface, paramVec3, covMat);
79  BoundTrackParameters params3_1(perigeeSurface, paramVec3_1, covMat);
80  BoundTrackParameters params4(perigeeSurface, paramVec4, covMat);
81  BoundTrackParameters params5(perigeeSurface, paramVec5, covMat);
82  BoundTrackParameters params6(perigeeSurface, paramVec6, covMat);
83  BoundTrackParameters params7(perigeeSurface, paramVec7, covMat);
84 
85  // The grid to be filled
87 
88  // addTrack method returns the central z bin where the track density
89  // grid was added and the track density grid itself for caching
90  std::pair<int, Acts::ActsVectorF<trkGridSize>> binAndTrackGrid;
91 
92  // Adds tracks too far away in transverse distance
93  binAndTrackGrid = grid.addTrack(params3, mainGrid);
94  binAndTrackGrid = grid.addTrack(params3_1, mainGrid);
95  // Adds tracks too far away in longitudinal distance
96  binAndTrackGrid = grid.addTrack(params6, mainGrid);
97  binAndTrackGrid = grid.addTrack(params7, mainGrid);
98 
99  // Tracks are far away from z-axis (or not in region of interest) and
100  // should not have contributed to density grid
101  BOOST_CHECK_EQUAL(mainGrid, ActsVectorF<mainGridSize>::Zero());
102 
103  // Now add track 1 and 2 to grid, seperately.
104  binAndTrackGrid = grid.addTrack(params1, mainGrid);
105  auto gridCopy = mainGrid;
106 
107  mainGrid = ActsVectorF<mainGridSize>::Zero();
108  binAndTrackGrid = grid.addTrack(params2, mainGrid);
109 
110  // Track 1 is closer to z-axis and should thus yield higher
111  // density values
112  BOOST_CHECK(gridCopy.sum() > mainGrid.sum());
113 
114  // Track 1 and 2 summed should give higher densities than
115  // only track 1 alone
116  binAndTrackGrid = grid.addTrack(params1, mainGrid);
117  BOOST_CHECK(gridCopy.sum() < mainGrid.sum());
118 
119  binAndTrackGrid = grid.addTrack(params4, mainGrid);
120 
121  // Check upper boundary
122  BOOST_CHECK_EQUAL(mainGrid(mainGridSize - int((trkGridSize - 1) / 2) - 2),
123  0.);
124  BOOST_CHECK(mainGrid(mainGridSize - int((trkGridSize - 1) / 2) - 1) > 0.);
125  BOOST_CHECK(mainGrid(mainGridSize - 1) > 0.);
126 
127  binAndTrackGrid = grid.addTrack(params5, mainGrid);
128  // Check lower boundary
129  BOOST_CHECK_EQUAL(mainGrid(int((trkGridSize - 1) / 2) + 1), 0.);
130  BOOST_CHECK(mainGrid(int((trkGridSize - 1) / 2)) > 0.);
131  BOOST_CHECK(mainGrid(0) > 0.);
132 
133  // Check if position of maximum is correct
134  auto maxRes = grid.getMaxZPosition(mainGrid);
135  int maxBin = (*maxRes / binSize) + mainGridSize / 2;
136  BOOST_CHECK_EQUAL(maxBin, mainGridSize / 2 + 1);
137 
138  // Check if error is thrown for empty grid
139  mainGrid = ActsVectorF<mainGridSize>::Zero();
140  auto maxResErr = grid.getMaxZPosition(mainGrid);
141  BOOST_CHECK(!maxResErr.ok());
142 
143  // Check if removal of tracks works as desired
144  binAndTrackGrid = grid.addTrack(params1, mainGrid);
145  binAndTrackGrid = grid.addTrack(params2, mainGrid);
146  // Copy grid for future reference
147  gridCopy = mainGrid;
148  binAndTrackGrid = grid.addTrack(params4, mainGrid);
149  // Main grid should have changed by adding track4
150  BOOST_CHECK(gridCopy != mainGrid);
151  // Remove track 4 again
152  int zBin = binAndTrackGrid.first;
153  auto trackGrid = binAndTrackGrid.second;
154  grid.removeTrackGridFromMainGrid(zBin, trackGrid, mainGrid);
155  // Check if it works
156  BOOST_CHECK_EQUAL(gridCopy, mainGrid);
157 }
158 
160 BOOST_AUTO_TEST_CASE(gaussian_grid_sum_max_densitytest) {
161  // Define the size of the grids
162  const int mainGridSize = 50;
163  const int trkGridSize = 11;
164 
165  double binSize = 0.1; // mm
166  double zMinMax = mainGridSize / 2 * binSize;
167 
168  // Set up grid density with zMinMax
170  cfg.useHighestSumZPosition = true;
172 
173  // Create some test tracks
174  Covariance covMat;
175  covMat << 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0,
176  0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2;
177 
178  const double posZ1 = -1.75;
179  const double posZ2 = 1.75;
180 
181  // Take two tracks, track 1 is closer in d0 and will thus have a slightly
182  // higher density
183  BoundVector paramVec1;
184  paramVec1 << 0.01, posZ1, 0, 0, 0, 0;
185  BoundVector paramVec2;
186  paramVec2 << 0.015, posZ2, 0, 0, 0, 0;
187 
188  // Create perigee surface
189  std::shared_ptr<PerigeeSurface> perigeeSurface =
190  Surface::makeShared<PerigeeSurface>(Vector3D(0., 0., 0.));
191 
192  BoundTrackParameters params1(perigeeSurface, paramVec1, covMat);
193  BoundTrackParameters params2(perigeeSurface, paramVec2, covMat);
194 
195  // The grid to be filled
197 
198  // addTrack method returns the central z bin where the track density
199  // grid was added and the track density grid itself for caching
200  std::pair<int, Acts::ActsVectorF<trkGridSize>> binAndTrackGrid;
201 
202  binAndTrackGrid = grid.addTrack(params1, mainGrid);
203  binAndTrackGrid = grid.addTrack(params2, mainGrid);
204 
205  // Artifically add some more density around the peak of track 2
206  int maxZbin = (posZ2 / binSize + mainGridSize / 2.);
207  mainGrid(maxZbin - 1) += 1;
208  mainGrid(maxZbin + 1) += 1;
209 
210  // Even though peak density of track 1 is slighly higher, track 2
211  // has a higher sum of track densities including the peak and the two
212  // surrounding bins and will be the output z position.
213  auto maxRes = grid.getMaxZPosition(mainGrid);
214  BOOST_CHECK(maxRes.ok());
215  BOOST_CHECK_EQUAL(*maxRes, posZ2);
216 }
217 
219 BOOST_AUTO_TEST_CASE(gaussian_grid_seed_width_test) {
220  // Define the size of the grids
221  const int mainGridSize = 50;
222  const int trkGridSize = 11;
223 
224  double binSize = 0.1; // mm
225  double zMinMax = mainGridSize / 2 * binSize;
226 
227  // Set up grid density with zMinMax
229  cfg.useHighestSumZPosition = true;
231 
232  // Create some test tracks
233  Covariance covMat;
234  covMat << 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0,
235  0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2;
236 
237  const double posZ1 = -1.75;
238  const double posZ2 = 1.75;
239 
240  // Take two tracks, track 1 is closer in d0 and will thus have a slightly
241  // higher density
242  BoundVector paramVec1;
243  paramVec1 << 0.01, posZ1, 0, 0, 0, 0;
244  BoundVector paramVec2;
245  paramVec2 << 0.015, posZ2, 0, 0, 0, 0;
246 
247  // Create perigee surface
248  std::shared_ptr<PerigeeSurface> perigeeSurface =
249  Surface::makeShared<PerigeeSurface>(Vector3D(0., 0., 0.));
250 
251  BoundTrackParameters params1(perigeeSurface, paramVec1, covMat);
252  BoundTrackParameters params2(perigeeSurface, paramVec2, covMat);
253 
254  // The grid to be filled
256 
257  // addTrack method returns the central z bin where the track density
258  // grid was added and the track density grid itself for caching
259  std::pair<int, Acts::ActsVectorF<trkGridSize>> binAndTrackGrid;
260 
261  binAndTrackGrid = grid.addTrack(params1, mainGrid);
262  binAndTrackGrid = grid.addTrack(params2, mainGrid);
263 
264  // Artifically add some more density around the peak of track 2
265  int maxZbin = (posZ2 / binSize + mainGridSize / 2.);
266  mainGrid(maxZbin - 1) += 1;
267  mainGrid(maxZbin + 1) += 1;
268 
269  // Even though peak density of track 1 is slighly higher, track 2
270  // has a higher sum of track densities including the peak and the two
271  // surrounding bins and will be the output z position.
272 
273  auto maxRes = grid.getMaxZPositionAndWidth(mainGrid);
274  BOOST_CHECK(maxRes.ok());
275  double z = (*maxRes).first;
276  double width = (*maxRes).second;
277 
278  BOOST_CHECK_EQUAL(z, posZ2);
279  // Check that width was estimated
280  BOOST_CHECK(width != 0.);
281 }
282 
283 } // namespace Test
284 } // namespace Acts