Analysis Software
Documentation for sPHENIX simulation 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 
23 
24 #include <algorithm>
25 #include <cstddef>
26 #include <memory>
27 #include <optional>
28 #include <utility>
29 
30 namespace bdata = boost::unit_test::data;
31 using namespace Acts::UnitLiterals;
32 
33 namespace Acts {
34 namespace Test {
35 
37 
38 // Create a test context
40 
41 BOOST_AUTO_TEST_CASE(gaussian_grid_density_test) {
42  // Define the size of the grids
43  constexpr size_t mainGridSize = 400;
44  constexpr size_t trkGridSize = 15;
45 
47 
48  double binSize = 0.1; // mm
49  double zMinMax = mainGridSize / 2 * binSize;
50 
51  // Set up grid density with zMinMax
52  Grid::Config cfg(zMinMax);
53  Grid grid(cfg);
54 
55  // Create some test tracks
56  Covariance covMat;
57  covMat << 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0,
58  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1;
59 
60  BoundVector paramVec1;
61  paramVec1 << 0.01, 0.15, 0, 0, 0, 0;
62 
63  BoundVector paramVec2;
64  paramVec2 << trkGridSize * binSize - 0.1, 0.15, 0, 0, 0, 0;
65 
66  BoundVector paramVec3;
67  paramVec3 << trkGridSize * binSize + 0.01, 0.15, 0, 0, 0, 0;
68 
69  BoundVector paramVec3_1;
70  paramVec3_1 << -(trkGridSize * binSize + 0.01), 0.15, 0, 0, 0, 0;
71 
72  BoundVector paramVec4;
73  paramVec4 << 0.01, 19.95, 0, 0, 0, 0;
74 
75  BoundVector paramVec5;
76  paramVec5 << 0.01, -19.95, 0, 0, 0, 0;
77 
78  BoundVector paramVec6;
79  paramVec6 << 0.01, -100.0, 0, 0, 0, 0;
80 
81  BoundVector paramVec7;
82  paramVec7 << 0.01, +100.0, 0, 0, 0, 0;
83 
84  // Create perigee surface
85  std::shared_ptr<PerigeeSurface> perigeeSurface =
86  Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
87 
88  BoundTrackParameters params1(perigeeSurface, paramVec1, covMat,
90  BoundTrackParameters params2(perigeeSurface, paramVec2, covMat,
92  BoundTrackParameters params3(perigeeSurface, paramVec3, covMat,
94  BoundTrackParameters params3_1(perigeeSurface, paramVec3_1, covMat,
96  BoundTrackParameters params4(perigeeSurface, paramVec4, covMat,
98  BoundTrackParameters params5(perigeeSurface, paramVec5, covMat,
100  BoundTrackParameters params6(perigeeSurface, paramVec6, covMat,
102  BoundTrackParameters params7(perigeeSurface, paramVec7, covMat,
104 
105  // The grid to be filled
106  Grid::MainGridVector mainGrid = Grid::MainGridVector::Zero();
107 
108  // addTrack method returns the central z bin where the track density
109  // grid was added and the track density grid itself for caching
110  std::pair<int, Grid::TrackGridVector> binAndTrackGrid;
111 
112  // Adds tracks too far away in transverse distance
113  binAndTrackGrid = grid.addTrack(params3, mainGrid);
114  binAndTrackGrid = grid.addTrack(params3_1, mainGrid);
115  // Adds tracks too far away in longitudinal distance
116  binAndTrackGrid = grid.addTrack(params6, mainGrid);
117  binAndTrackGrid = grid.addTrack(params7, mainGrid);
118 
119  // Tracks are far away from z-axis (or not in region of interest) and
120  // should not have contributed to density grid
121  auto zeroGrid = Grid::MainGridVector::Zero();
122  BOOST_CHECK_EQUAL(mainGrid, zeroGrid);
123 
124  // Now add track 1 and 2 to grid, separately.
125  binAndTrackGrid = grid.addTrack(params1, mainGrid);
126  auto gridCopy = mainGrid;
127 
128  mainGrid = Grid::MainGridVector::Zero();
129  binAndTrackGrid = grid.addTrack(params2, mainGrid);
130 
131  // Track 1 is closer to z-axis and should thus yield higher
132  // density values
133  BOOST_CHECK(gridCopy.sum() > mainGrid.sum());
134 
135  // Track 1 and 2 summed should give higher densities than
136  // only track 1 alone
137  binAndTrackGrid = grid.addTrack(params1, mainGrid);
138  BOOST_CHECK_EQUAL(gridCopy.sum(), mainGrid.sum());
139 
140  binAndTrackGrid = grid.addTrack(params4, mainGrid);
141 
142  // Check upper boundary
143  BOOST_CHECK_EQUAL(mainGrid(mainGridSize - int((trkGridSize - 1) / 2) - 2),
144  0.);
145  BOOST_CHECK(mainGrid(mainGridSize - int((trkGridSize - 1) / 2) - 1) > 0.);
146  BOOST_CHECK(mainGrid(mainGridSize - 1) > 0.);
147 
148  binAndTrackGrid = grid.addTrack(params5, mainGrid);
149  // Check lower boundary
150  BOOST_CHECK_EQUAL(mainGrid(int((trkGridSize - 1) / 2) + 1), 0.);
151  BOOST_CHECK(mainGrid(int((trkGridSize - 1) / 2)) > 0.);
152  BOOST_CHECK(mainGrid(0) > 0.);
153 
154  // Check if position of maximum is correct
155  auto maxRes = grid.getMaxZPosition(mainGrid);
156  int maxBin = static_cast<int>((*maxRes / binSize) + mainGridSize / 2);
157  BOOST_CHECK_EQUAL(maxBin, 0);
158 
159  // Check if error is thrown for empty grid
160  mainGrid = Grid::MainGridVector::Zero();
161  auto maxResErr = grid.getMaxZPosition(mainGrid);
162  BOOST_CHECK(!maxResErr.ok());
163 
164  // Check if removal of tracks works as desired
165  binAndTrackGrid = grid.addTrack(params1, mainGrid);
166  binAndTrackGrid = grid.addTrack(params2, mainGrid);
167  // Copy grid for future reference
168  gridCopy = mainGrid;
169  binAndTrackGrid = grid.addTrack(params4, mainGrid);
170  // Main grid should have changed by adding track4
171  BOOST_CHECK(gridCopy != mainGrid);
172  // Remove track 4 again
173  int zBin = binAndTrackGrid.first;
174  auto trackGrid = binAndTrackGrid.second;
175  grid.removeTrackGridFromMainGrid(zBin, trackGrid, mainGrid);
176  // Check if it works
177  BOOST_CHECK_EQUAL(gridCopy, mainGrid);
178 }
179 
181 BOOST_AUTO_TEST_CASE(gaussian_grid_sum_max_densitytest) {
182  // Define the size of the grids
183  constexpr int mainGridSize = 50;
184  constexpr int trkGridSize = 11;
185 
187 
188  double binSize = 0.1; // mm
189  double zMinMax = mainGridSize / 2 * binSize;
190 
191  // Set up grid density with zMinMax
192  Grid::Config cfg(zMinMax);
193  cfg.useHighestSumZPosition = true;
194  Grid grid(cfg);
195 
196  // Create some test tracks
197  Covariance covMat;
198  covMat << 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0,
199  0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2;
200 
201  const double posZ1 = -1.75;
202  const double posZ2 = 1.75;
203 
204  // Take two tracks, track 1 is closer in d0 and will thus have a slightly
205  // higher density
206  BoundVector paramVec1;
207  paramVec1 << 0.01, posZ1, 0, 0, 0, 0;
208  BoundVector paramVec2;
209  paramVec2 << 0.015, posZ2, 0, 0, 0, 0;
210 
211  // Create perigee surface
212  std::shared_ptr<PerigeeSurface> perigeeSurface =
213  Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
214 
215  BoundTrackParameters params1(perigeeSurface, paramVec1, covMat,
217  BoundTrackParameters params2(perigeeSurface, paramVec2, covMat,
219 
220  // The grid to be filled
221  Grid::MainGridVector mainGrid = Grid::MainGridVector::Zero();
222 
223  // addTrack method returns the central z bin where the track density
224  // grid was added and the track density grid itself for caching
225  std::pair<int, Grid::TrackGridVector> binAndTrackGrid;
226 
227  binAndTrackGrid = grid.addTrack(params1, mainGrid);
228  binAndTrackGrid = grid.addTrack(params2, mainGrid);
229 
230  // Artificially add some more density around the peak of track 2
231  int maxZbin = static_cast<int>((posZ2 / binSize + mainGridSize / 2.));
232  mainGrid(maxZbin - 1) += 1;
233  mainGrid(maxZbin + 1) += 1;
234 
235  // Even though peak density of track 1 is slightly higher, track 2
236  // has a higher sum of track densities including the peak and the two
237  // surrounding bins and will be the output z position.
238  auto maxRes = grid.getMaxZPosition(mainGrid);
239  BOOST_CHECK(maxRes.ok());
240  BOOST_CHECK_EQUAL(*maxRes, posZ2);
241 }
242 
244 BOOST_AUTO_TEST_CASE(gaussian_grid_seed_width_test) {
245  // Define the size of the grids
246  constexpr int mainGridSize = 50;
247  constexpr int trkGridSize = 11;
248 
250 
251  double binSize = 0.1; // mm
252  double zMinMax = mainGridSize / 2 * binSize;
253 
254  // Set up grid density with zMinMax
255  Grid::Config cfg(zMinMax);
256  cfg.useHighestSumZPosition = true;
257  Grid grid(cfg);
258 
259  // Create some test tracks
260  Covariance covMat;
261  covMat << 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0,
262  0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2, 0, 0, 0, 0, 0, 0, 1e-2;
263 
264  const double posZ1 = -1.75;
265  const double posZ2 = 1.75;
266 
267  // Take two tracks, track 1 is closer in d0 and will thus have a slightly
268  // higher density
269  BoundVector paramVec1;
270  paramVec1 << 0.01, posZ1, 0, 0, 0, 0;
271  BoundVector paramVec2;
272  paramVec2 << 0.015, posZ2, 0, 0, 0, 0;
273 
274  // Create perigee surface
275  std::shared_ptr<PerigeeSurface> perigeeSurface =
276  Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
277 
278  BoundTrackParameters params1(perigeeSurface, paramVec1, covMat,
280  BoundTrackParameters params2(perigeeSurface, paramVec2, covMat,
282 
283  // The grid to be filled
284  Grid::MainGridVector mainGrid = Grid::MainGridVector::Zero();
285 
286  // addTrack method returns the central z bin where the track density
287  // grid was added and the track density grid itself for caching
288  std::pair<int, Grid::TrackGridVector> binAndTrackGrid;
289 
290  binAndTrackGrid = grid.addTrack(params1, mainGrid);
291  binAndTrackGrid = grid.addTrack(params2, mainGrid);
292 
293  // Artificially add some more density around the peak of track 2
294  int maxZbin = static_cast<int>((posZ2 / binSize + mainGridSize / 2.));
295  mainGrid(maxZbin - 1) += 1;
296  mainGrid(maxZbin + 1) += 1;
297 
298  // Even though peak density of track 1 is slightly higher, track 2
299  // has a higher sum of track densities including the peak and the two
300  // surrounding bins and will be the output z position.
301 
302  auto maxRes = grid.getMaxZPositionAndWidth(mainGrid);
303  BOOST_CHECK(maxRes.ok());
304  double z = (*maxRes).first;
305  double width = (*maxRes).second;
306 
307  BOOST_CHECK_EQUAL(z, posZ2);
308  // Check that width was estimated
309  BOOST_CHECK(width != 0.);
310 }
311 
312 } // namespace Test
313 } // namespace Acts