Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KalmanVertexUpdaterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file KalmanVertexUpdaterTests.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 
33 
34 #include <algorithm>
35 #include <array>
36 #include <cmath>
37 #include <iostream>
38 #include <memory>
39 #include <optional>
40 #include <random>
41 #include <tuple>
42 #include <type_traits>
43 #include <utility>
44 #include <vector>
45 
46 namespace bdata = boost::unit_test::data;
47 using namespace Acts::UnitLiterals;
48 
49 namespace Acts {
50 namespace Test {
51 
54 using Linearizer = HelicalTrackLinearizer<Propagator>;
55 
56 // Create a test context
59 
60 // Vertex x/y position distribution
61 std::uniform_real_distribution<> vXYDist(-0.1_mm, 0.1_mm);
62 // Vertex z position distribution
63 std::uniform_real_distribution<> vZDist(-20_mm, 20_mm);
64 // Track d0 distribution
65 std::uniform_real_distribution<> d0Dist(-0.01_mm, 0.01_mm);
66 // Track z0 distribution
67 std::uniform_real_distribution<> z0Dist(-0.2_mm, 0.2_mm);
68 // Track pT distribution
69 std::uniform_real_distribution<> pTDist(0.4_GeV, 10_GeV);
70 // Track phi distribution
71 std::uniform_real_distribution<> phiDist(-M_PI, M_PI);
72 // Track theta distribution
73 std::uniform_real_distribution<> thetaDist(1.0, M_PI - 1.0);
74 // Track charge helper distribution
75 std::uniform_real_distribution<> qDist(-1, 1);
76 // Track IP resolution distribution
77 std::uniform_real_distribution<> resIPDist(0., 100_um);
78 // Track angular distribution
79 std::uniform_real_distribution<> resAngDist(0., 0.1);
80 // Track q/p resolution distribution
81 std::uniform_real_distribution<> resQoPDist(-0.01, 0.01);
82 // Number of vertices per test event distribution
83 
87 BOOST_AUTO_TEST_CASE(Kalman_Vertex_Updater) {
88  bool debug = false;
89 
90  // Number of tests
91  unsigned int nTests = 10;
92 
93  // Set up RNG
94  int mySeed = 31415;
95  std::mt19937 gen(mySeed);
96 
97  // Set up constant B-Field
98  auto bField = std::make_shared<ConstantBField>(Vector3{0.0, 0.0, 1_T});
99 
100  // Set up Eigenstepper
102 
103  // Set up propagator with void navigator
104  auto propagator = std::make_shared<Propagator>(stepper);
105 
106  // Linearizer for BoundTrackParameters type test
107  Linearizer::Config ltConfig(bField, propagator);
108  Linearizer linearizer(ltConfig);
110 
111  // Create perigee surface at origin
112  std::shared_ptr<PerigeeSurface> perigeeSurface =
113  Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
114 
115  // Creates a random tracks around origin and a random vertex.
116  // VertexUpdater adds track to vertex and updates the position
117  // which should afterwards be closer to the origin/track
118  for (unsigned int i = 0; i < nTests; ++i) {
119  if (debug) {
120  std::cout << "Test " << i + 1 << std::endl;
121  }
122  // Construct positive or negative charge randomly
123  double q = qDist(gen) < 0 ? -1. : 1.;
124 
125  // Construct random track parameters around origin
127 
128  paramVec << d0Dist(gen), z0Dist(gen), phiDist(gen), thetaDist(gen),
129  q / pTDist(gen), 0.;
130 
131  if (debug) {
132  std::cout << "Creating track parameters: " << paramVec << std::endl;
133  }
134 
135  // Fill vector of track objects with simple covariance matrix
136  Covariance covMat;
137 
138  // Resolutions
139  double res_d0 = resIPDist(gen);
140  double res_z0 = resIPDist(gen);
141  double res_ph = resAngDist(gen);
142  double res_th = resAngDist(gen);
143  double res_qp = resQoPDist(gen);
144 
145  covMat << res_d0 * res_d0, 0., 0., 0., 0., 0., 0., res_z0 * res_z0, 0., 0.,
146  0., 0., 0., 0., res_ph * res_ph, 0., 0., 0., 0., 0., 0.,
147  res_th * res_th, 0., 0., 0., 0., 0., 0., res_qp * res_qp, 0., 0., 0.,
148  0., 0., 0., 1.;
149  BoundTrackParameters params(perigeeSurface, paramVec, std::move(covMat),
151 
152  std::shared_ptr<PerigeeSurface> perigee =
153  Surface::makeShared<PerigeeSurface>(Vector3::Zero());
154 
155  // Linearized state of the track
156  LinearizedTrack linTrack =
157  linearizer
158  .linearizeTrack(params, 0, *perigee, geoContext, magFieldContext,
159  state)
160  .value();
161 
162  // Create TrackAtVertex
163  TrackAtVertex<BoundTrackParameters> trkAtVtx(0., params, &params);
164 
165  // Set linearized state of trackAtVertex
166  trkAtVtx.linearizedState = linTrack;
167 
168  // Create a vertex
169  Vector3 vtxPos(vXYDist(gen), vXYDist(gen), vZDist(gen));
170  Vertex<BoundTrackParameters> vtx(vtxPos);
171  vtx.setFullCovariance(SquareMatrix4::Identity() * 0.01);
172 
173  // Update trkAtVertex with assumption of originating from vtx
174  KalmanVertexUpdater::updateVertexWithTrack<BoundTrackParameters>(vtx,
175  trkAtVtx);
176 
177  if (debug) {
178  std::cout << "Old vertex position: " << vtxPos << std::endl;
179  std::cout << "New vertex position: " << vtx.position() << std::endl;
180  }
181 
182  double oldDistance = vtxPos.norm();
183  double newDistance = vtx.position().norm();
184 
185  if (debug) {
186  std::cout << "Old distance: " << oldDistance << std::endl;
187  std::cout << "New distance: " << newDistance << std::endl;
188  }
189 
190  // After update, vertex should be closer to the track
191  BOOST_CHECK(newDistance < oldDistance);
192 
193  // Note: KalmanVertexUpdater updates the vertex w.r.t. the
194  // newly given track, but does NOT add the track to the
195  // TrackAtVertex list. Has to be done manually after calling
196  // the update method.
197  BOOST_CHECK(vtx.tracks().empty());
198 
199  } // end for loop
200 
201 } // end test case
202 
203 } // namespace Test
204 } // namespace Acts