Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KalmanVertexTrackUpdaterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file KalmanVertexTrackUpdaterTests.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 
34 
35 #include <algorithm>
36 #include <array>
37 #include <cmath>
38 #include <iostream>
39 #include <memory>
40 #include <optional>
41 #include <random>
42 #include <tuple>
43 #include <type_traits>
44 #include <utility>
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 
86 BOOST_AUTO_TEST_CASE(Kalman_Vertex_TrackUpdater) {
87  bool debug = true;
88 
89  // Number of tests
90  unsigned int nTests = 10;
91 
92  // Set up RNG
93  int mySeed = 31415;
94  std::mt19937 gen(mySeed);
95 
96  // Set up constant B-Field
97  auto bField = std::make_shared<ConstantBField>(Vector3{0.0, 0.0, 1_T});
98 
99  // Set up Eigenstepper
101 
102  // Set up propagator with void navigator
103  auto propagator = std::make_shared<Propagator>(stepper);
104 
105  // Set up ImpactPointEstimator, used for comparisons later
107  IPEstimator::Config ip3dEstConfig(bField, propagator);
108  IPEstimator ip3dEst(ip3dEstConfig);
109  IPEstimator::State state(bField->makeCache(magFieldContext));
110 
111  // Set up HelicalTrackLinearizer, needed for linearizing the tracks
112  // Linearizer for BoundTrackParameters type test
113  Linearizer::Config ltConfig(bField, propagator);
114  Linearizer linearizer(ltConfig);
115  Linearizer::State linState(bField->makeCache(magFieldContext));
116 
117  // Create perigee surface at origin
118  std::shared_ptr<PerigeeSurface> perigeeSurface =
119  Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
120 
121  // Create random tracks around origin and a random vertex.
122  // Update tracks with the assumption that they originate from
123  // the vertex position and check if they are closer to the
124  // vertex after the update process
125  for (unsigned int i = 0; i < nTests; ++i) {
126  // Construct positive or negative charge randomly
127  double q = qDist(gen) < 0 ? -1. : 1.;
128 
129  // Construct random track parameters
131 
132  paramVec << d0Dist(gen), z0Dist(gen), phiDist(gen), thetaDist(gen),
133  q / pTDist(gen), 0.;
134 
135  if (debug) {
136  std::cout << "Creating track parameters: " << paramVec << std::endl;
137  }
138 
139  // Fill vector of track objects with simple covariance matrix
140  Covariance covMat;
141 
142  // Resolutions
143  double res_d0 = resIPDist(gen);
144  double res_z0 = resIPDist(gen);
145  double res_ph = resAngDist(gen);
146  double res_th = resAngDist(gen);
147  double res_qp = resQoPDist(gen);
148 
149  covMat << res_d0 * res_d0, 0., 0., 0., 0., 0., 0., res_z0 * res_z0, 0., 0.,
150  0., 0., 0., 0., res_ph * res_ph, 0., 0., 0., 0., 0., 0.,
151  res_th * res_th, 0., 0., 0., 0., 0., 0., res_qp * res_qp, 0., 0., 0.,
152  0., 0., 0., 1.;
153  BoundTrackParameters params(perigeeSurface, paramVec, std::move(covMat),
155 
156  std::shared_ptr<PerigeeSurface> perigee =
157  Surface::makeShared<PerigeeSurface>(Vector3::Zero());
158 
159  // Linearized state of the track
160  LinearizedTrack linTrack =
161  linearizer
162  .linearizeTrack(params, 0, *perigee, geoContext, magFieldContext,
163  linState)
164  .value();
165 
166  // Create TrackAtVertex
167  TrackAtVertex<BoundTrackParameters> trkAtVtx(0., params, &params);
168 
169  // Set linearized state of trackAtVertex
170  trkAtVtx.linearizedState = linTrack;
171 
172  // Copy parameters for later comparison of old and new version
173  auto fittedParamsCopy = trkAtVtx.fittedParams;
174 
175  // Create a vertex
176  Vector3 vtxPos(vXYDist(gen), vXYDist(gen), vZDist(gen));
177  Vertex<BoundTrackParameters> vtx(vtxPos);
178 
179  // Update trkAtVertex with assumption of originating from vtx
180  KalmanVertexTrackUpdater::update<BoundTrackParameters>(trkAtVtx, vtx);
181 
182  // The old distance
183  double oldDistance =
184  ip3dEst.calculateDistance(geoContext, fittedParamsCopy, vtxPos, state)
185  .value();
186 
187  // The new distance after update
188  double newDistance =
189  ip3dEst
190  .calculateDistance(geoContext, trkAtVtx.fittedParams, vtxPos, state)
191  .value();
192  if (debug) {
193  std::cout << "Old distance: " << oldDistance << std::endl;
194  std::cout << "New distance: " << newDistance << std::endl;
195  }
196 
197  // Parameters should have changed
198  BOOST_CHECK_NE(fittedParamsCopy, trkAtVtx.fittedParams);
199 
200  // After update, track should be closer to the vertex
201  BOOST_CHECK(newDistance < oldDistance);
202 
203  } // end for loop
204 
205 } // end test case
206 
207 } // namespace Test
208 } // namespace Acts