EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FullBilloirVertexFitterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FullBilloirVertexFitterTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 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 
20 #include "Acts/Utilities/Units.hpp"
24 
25 namespace bdata = boost::unit_test::data;
26 using namespace Acts::UnitLiterals;
27 
28 namespace Acts {
29 namespace Test {
30 
32 using Linearizer =
33  HelicalTrackLinearizer<Propagator<EigenStepper<ConstantBField>>>;
34 
35 // Create a test context
38 
41 BOOST_AUTO_TEST_CASE(billoir_vertex_fitter_empty_input_test) {
42  // Set up constant B-Field
43  ConstantBField bField(0.0, 0.0, 1_T);
44 
45  // Set up Eigenstepper
47 
48  // Set up propagator with void navigator
49  auto propagator =
50  std::make_shared<Propagator<EigenStepper<ConstantBField>>>(stepper);
51 
52  Linearizer::Config ltConfig(bField, propagator);
53  Linearizer linearizer(ltConfig);
54 
55  // Set up Billoir Vertex Fitter
56  using VertexFitter =
58  VertexFitter::Config vertexFitterCfg;
59  VertexFitter billoirFitter(vertexFitterCfg);
60  VertexFitter::State state(magFieldContext);
61 
62  // Constraint for vertex fit
63  Vertex<BoundTrackParameters> myConstraint;
64  // Some abitrary values
65  SymMatrix4D myCovMat = SymMatrix4D::Zero();
66  myCovMat(0, 0) = 30.;
67  myCovMat(1, 1) = 30.;
68  myCovMat(2, 2) = 30.;
69  myCovMat(3, 3) = 30.;
70  myConstraint.setFullCovariance(std::move(myCovMat));
71  myConstraint.setFullPosition(Vector4D(0, 0, 0, 0));
72 
73  const std::vector<const BoundTrackParameters*> emptyVector;
74 
76  myConstraint);
77 
78  Vertex<BoundTrackParameters> fittedVertex =
79  billoirFitter.fit(emptyVector, linearizer, vfOptions, state).value();
80 
81  Vector3D origin(0., 0., 0.);
82  BOOST_CHECK_EQUAL(fittedVertex.position(), origin);
83 
84  SymMatrix4D zeroMat = SymMatrix4D::Zero();
85  BOOST_CHECK_EQUAL(fittedVertex.fullCovariance(), zeroMat);
86 
87  fittedVertex =
88  billoirFitter.fit(emptyVector, linearizer, vfOptions, state).value();
89 
90  BOOST_CHECK_EQUAL(fittedVertex.position(), origin);
91  BOOST_CHECK_EQUAL(fittedVertex.fullCovariance(), zeroMat);
92 }
93 
94 // Vertex x/y position distribution
95 std::uniform_real_distribution<> vXYDist(-0.1_mm, 0.1_mm);
96 // Vertex z position distribution
97 std::uniform_real_distribution<> vZDist(-20_mm, 20_mm);
98 // Track d0 distribution
99 std::uniform_real_distribution<> d0Dist(-0.01_mm, 0.01_mm);
100 // Track z0 distribution
101 std::uniform_real_distribution<> z0Dist(-0.2_mm, 0.2_mm);
102 // Track pT distribution
103 std::uniform_real_distribution<> pTDist(0.4_GeV, 10_GeV);
104 // Track phi distribution
105 std::uniform_real_distribution<> phiDist(-M_PI, M_PI);
106 // Track theta distribution
107 std::uniform_real_distribution<> thetaDist(1.0, M_PI - 1.0);
108 // Track charge helper distribution
109 std::uniform_real_distribution<> qDist(-1, 1);
110 // Track IP resolution distribution
111 std::uniform_real_distribution<> resIPDist(0., 100_um);
112 // Track angular distribution
113 std::uniform_real_distribution<> resAngDist(0., 0.1);
114 // Track q/p resolution distribution
115 std::uniform_real_distribution<> resQoPDist(-0.1, 0.1);
116 // Number of tracks distritbution
117 std::uniform_int_distribution<> nTracksDist(3, 10);
118 
123 BOOST_AUTO_TEST_CASE(billoir_vertex_fitter_defaulttrack_test) {
124  // Set up RNG
125  int mySeed = 31415;
126  std::mt19937 gen(mySeed);
127  // Set up constant B-Field
128  ConstantBField bField(0.0, 0.0, 1_T);
129 
130  // Set up Eigenstepper
132  // Set up propagator with void navigator
133  auto propagator =
134  std::make_shared<Propagator<EigenStepper<ConstantBField>>>(stepper);
135 
136  Linearizer::Config ltConfig(bField, propagator);
137  Linearizer linearizer(ltConfig);
138 
139  // Number of events
140  const int nEvents = 10;
141  for (int eventIdx = 0; eventIdx < nEvents; ++eventIdx) {
142  // Number of tracks
143  unsigned int nTracks = nTracksDist(gen);
144 
145  // Set up Billoir Vertex Fitter
146  using VertexFitter =
148  VertexFitter::Config vertexFitterCfg;
149  VertexFitter billoirFitter(vertexFitterCfg);
150  VertexFitter::State state(magFieldContext);
151  // Constraint for vertex fit
152  Vertex<BoundTrackParameters> myConstraint;
153  // Some abitrary values
154  SymMatrix4D myCovMat = SymMatrix4D::Zero();
155  myCovMat(0, 0) = 30.;
156  myCovMat(1, 1) = 30.;
157  myCovMat(2, 2) = 30.;
158  myCovMat(3, 3) = 30.;
159  myConstraint.setFullCovariance(std::move(myCovMat));
160  myConstraint.setFullPosition(Vector4D(0, 0, 0, 0));
163 
165  geoContext, magFieldContext, myConstraint);
166  // Create position of vertex and perigee surface
167  double x = vXYDist(gen);
168  double y = vXYDist(gen);
169  double z = vZDist(gen);
170 
171  Vector3D vertexPosition(x, y, z);
172  std::shared_ptr<PerigeeSurface> perigeeSurface =
173  Surface::makeShared<PerigeeSurface>(Vector3D(0., 0., 0.));
174  // Calculate d0 and z0 corresponding to vertex position
175  double d0V = sqrt(x * x + y * y);
176  double z0V = z;
177 
178  // Start constructing nTracks tracks in the following
179  std::vector<BoundTrackParameters> tracks;
180 
181  // Construct random track emerging from vicinity of vertex position
182  // Vector to store track objects used for vertex fit
183  for (unsigned int iTrack = 0; iTrack < nTracks; iTrack++) {
184  // Construct positive or negative charge randomly
185  double q = qDist(gen) < 0 ? -1. : 1.;
186 
187  // Construct random track parameters
188  BoundVector paramVec;
189  paramVec << d0V + d0Dist(gen), z0V + z0Dist(gen), phiDist(gen),
190  thetaDist(gen), q / pTDist(gen), 0.;
191 
192  // Resolutions
193  double resD0 = resIPDist(gen);
194  double resZ0 = resIPDist(gen);
195  double resPh = resAngDist(gen);
196  double resTh = resAngDist(gen);
197  double resQp = resQoPDist(gen);
198 
199  // Fill vector of track objects with simple covariance matrix
200  Covariance covMat;
201 
202  covMat << resD0 * resD0, 0., 0., 0., 0., 0., 0., resZ0 * resZ0, 0., 0.,
203  0., 0., 0., 0., resPh * resPh, 0., 0., 0., 0., 0., 0., resTh * resTh,
204  0., 0., 0., 0., 0., 0., resQp * resQp, 0., 0., 0., 0., 0., 0., 1.;
205  tracks.push_back(
206  BoundTrackParameters(perigeeSurface, paramVec, std::move(covMat)));
207  }
208 
209  std::vector<const BoundTrackParameters*> tracksPtr;
210  for (const auto& trk : tracks) {
211  tracksPtr.push_back(&trk);
212  }
213 
214  // Do the actual fit with 4 tracks without constraint
215  Vertex<BoundTrackParameters> fittedVertex =
216  billoirFitter.fit(tracksPtr, linearizer, vfOptions, state).value();
217  if (fittedVertex.tracks().size() > 0) {
218  CHECK_CLOSE_ABS(fittedVertex.position(), vertexPosition, 1_mm);
219  }
220  // Do the fit with a constraint
221  Vertex<BoundTrackParameters> fittedVertexConstraint =
222  billoirFitter.fit(tracksPtr, linearizer, vfOptionsConstr, state)
223  .value();
224  if (fittedVertexConstraint.tracks().size() > 0) {
225  CHECK_CLOSE_ABS(fittedVertexConstraint.position(), vertexPosition, 1_mm);
226  }
227 
228  std::cout << "Fitting nTracks: " << nTracks << std::endl;
229  std::cout << "True Vertex: " << x << ", " << y << ", " << z << std::endl;
230  std::cout << "Fitted Vertex: " << fittedVertex.position() << std::endl;
231  std::cout << "Fitted constraint Vertex: "
232  << fittedVertexConstraint.position() << std::endl;
233  }
234 }
235 
236 // Dummy user-defined InputTrack type
237 struct InputTrack {
238  InputTrack(const BoundTrackParameters& params) : m_parameters(params) {}
239 
240  const BoundTrackParameters& parameters() const { return m_parameters; }
241 
242  // store e.g. link to original objects here
243 
244  private:
245  BoundTrackParameters m_parameters;
246 };
247 
252 BOOST_AUTO_TEST_CASE(billoir_vertex_fitter_usertrack_test) {
253  // Set up RNG
254  int mySeed = 31415;
255  std::mt19937 gen(mySeed);
256 
257  // Set up constant B-Field
258  ConstantBField bField(0.0, 0.0, 1_T);
259 
260  // Set up Eigenstepper
262 
263  // Set up propagator with void navigator
264  auto propagator =
265  std::make_shared<Propagator<EigenStepper<ConstantBField>>>(stepper);
266 
267  Linearizer::Config ltConfig(bField, propagator);
268  Linearizer linearizer(ltConfig);
269 
270  const int nEvents = 10;
271 
272  for (int eventIdx = 0; eventIdx < nEvents; ++eventIdx) {
273  unsigned int nTracks = nTracksDist(gen);
274 
275  // Create a custom std::function to extract BoundTrackParameters from
276  // user-defined InputTrack
277  std::function<BoundTrackParameters(InputTrack)> extractParameters =
278  [](InputTrack params) { return params.parameters(); };
279 
280  // Set up Billoir Vertex Fitter
282  VertexFitter::Config vertexFitterCfg;
283  VertexFitter billoirFitter(vertexFitterCfg, extractParameters);
284  VertexFitter::State state(magFieldContext);
285 
286  // Constraint for vertex fit
287  Vertex<InputTrack> myConstraint;
288  // Some abitrary values
289  SymMatrix4D myCovMat = SymMatrix4D::Zero();
290  myCovMat(0, 0) = 30.;
291  myCovMat(1, 1) = 30.;
292  myCovMat(2, 2) = 30.;
293  myCovMat(3, 3) = 30.;
294  myConstraint.setFullCovariance(std::move(myCovMat));
295  myConstraint.setFullPosition(Vector4D(0, 0, 0, 0));
296 
298 
300  myConstraint);
301 
302  // Create position of vertex and perigee surface
303  double x = vXYDist(gen);
304  double y = vXYDist(gen);
305  double z = vZDist(gen);
306 
307  Vector3D vertexPosition(x, y, z);
308  std::shared_ptr<PerigeeSurface> perigeeSurface =
309  Surface::makeShared<PerigeeSurface>(Vector3D(0., 0., 0.));
310 
311  // Calculate d0 and z0 corresponding to vertex position
312  double d0V = sqrt(x * x + y * y);
313  double z0V = z;
314 
315  // Start constructing nTracks tracks in the following
316  std::vector<InputTrack> tracks;
317 
318  // Construct random track emerging from vicinity of vertex position
319  // Vector to store track objects used for vertex fit
320  for (unsigned int iTrack = 0; iTrack < nTracks; iTrack++) {
321  // Construct positive or negative charge randomly
322  double q = qDist(gen) < 0 ? -1. : 1.;
323 
324  // Construct random track parameters
325  BoundVector paramVec;
326  paramVec << d0V + d0Dist(gen), z0V + z0Dist(gen), phiDist(gen),
327  thetaDist(gen), q / pTDist(gen), 0.;
328 
329  // Resolutions
330  double resD0 = resIPDist(gen);
331  double resZ0 = resIPDist(gen);
332  double resPh = resAngDist(gen);
333  double resTh = resAngDist(gen);
334  double resQp = resQoPDist(gen);
335 
336  // Fill vector of track objects with simple covariance matrix
337  Covariance covMat;
338 
339  covMat << resD0 * resD0, 0., 0., 0., 0., 0., 0., resZ0 * resZ0, 0., 0.,
340  0., 0., 0., 0., resPh * resPh, 0., 0., 0., 0., 0., 0., resTh * resTh,
341  0., 0., 0., 0., 0., 0., resQp * resQp, 0., 0., 0., 0., 0., 0., 1.;
342  tracks.push_back(InputTrack(
343  BoundTrackParameters(perigeeSurface, paramVec, std::move(covMat))));
344  }
345 
346  std::vector<const InputTrack*> tracksPtr;
347  for (const auto& trk : tracks) {
348  tracksPtr.push_back(&trk);
349  }
350 
351  // Do the actual fit with 4 tracks without constraint
352  Vertex<InputTrack> fittedVertex =
353  billoirFitter.fit(tracksPtr, linearizer, vfOptions, state).value();
354  if (fittedVertex.tracks().size() > 0) {
355  CHECK_CLOSE_ABS(fittedVertex.position(), vertexPosition, 1_mm);
356  }
357  // Do the fit with a constraint
358  Vertex<InputTrack> fittedVertexConstraint =
359  billoirFitter.fit(tracksPtr, linearizer, vfOptionsConstr, state)
360  .value();
361  if (fittedVertexConstraint.tracks().size() > 0) {
362  CHECK_CLOSE_ABS(fittedVertexConstraint.position(), vertexPosition, 1_mm);
363  }
364 
365  std::cout << "Fitting nTracks: " << nTracks << std::endl;
366  std::cout << "True Vertex: " << x << ", " << y << ", " << z << std::endl;
367  std::cout << "Fitted Vertex: " << fittedVertex.position() << std::endl;
368  std::cout << "Fitted constraint Vertex: "
369  << fittedVertexConstraint.position() << std::endl;
370  }
371 }
372 
373 } // namespace Test
374 } // namespace Acts