Extended Kalman Filter. Keisuke Fujii. The ACFA-Sim-J Group. ABSTRACT. This short review stems from the appendix of Kotoyo Hoshina's Ph.D thesis and ...
Extended Kalman Filter Keisuke Fujii The ACFA-Sim-J Group
ABSTRACT This short review stems from the appendix of Kotoyo Hoshina’s Ph.D thesis and chapter 4 of Yasuhiro Nakashima’s Master’s thesis both written in Japanese. It is intended to be an introduction for those who are interested in principle of Kalman Filter and its application to track fitting in high energy collider experiments.
Contents 1 Introduction
3
2 Principle of Kalman Filter 2.1 Statement of the Problem . . . . . . . . . . . . . . 2.1.1 Example 1: Ballistic Missile . . . . . . . . . 2.1.2 Example 2: Tracking in HEP Experiments . 2.1.3 What We Need . . . . . . . . . . . . . . . . 2.2 Prediction: Extrapolation to Next Site . . . . . . . 2.3 Filtering: Optimal Estimate at Current Site . . . . 2.4 Smoothing: Reevaluation at Past Site . . . . . . . 2.5 Inverse Kalman Filter: Exclusion of Site . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
5 5 6 6 6 7 8 11 13
3 Application of Kalman Filter to Track Fitting 3.1 Basic Formulae for Kalman Filter . . . . . . . . 3.2 System Equation: f k−1 , F k−1 , and Qk−1 . . . 3.2.1 State Propagator: f k−1 (ak−1 ) . . . . . 3.2.2 Propagator Matrix: F k−1 . . . . . . . . 3.2.3 Process Noise: Qk−1 . . . . . . . . . . . 3.3 Measurement Equation: hk , H k , and V k . . . 3.3.1 Projector: hk (a) . . . . . . . . . . . . . 3.3.2 Projector Matrix: H k . . . . . . . . . . 3.3.3 Measurement Noise: V k . . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
14 14 15 15 17 20 20 21 21 22
4 Implementation in C++ 4.1 Design Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 KalLib: Base Class Library for Kalman Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.1 TVKalSystem Class (Inheriting from TObjArray; an Array of Sites) . . . . . . . . . . 4.2.2 TVKalSite Class (Inheriting from TObjArray: an Array of States: Measurement Site) 4.2.3 TVKalState Class (Inheriting from TKalMatrix: State Vector) . . . . . . . . . . . . . 4.3 Kaltracklib: Kalman-Filter-Based Track Fitting Library . . . . . . . . . . . . . . . . . . . . . 4.3.1 TKalTrack Class: Inheriting from TVKalSystem; Track . . . . . . . . . . . . . . . . . 4.3.2 TKalTrackSite Class: Inheriting from TVKalSite; Measurement Site . . . . . . . . . . 4.3.3 TKalTrackState Class: Inheriting from TVKalState; Track Parameter Vector . . . . . 4.3.4 TVTrackHit Class: Inheriting from TKalMatrix; Measurement Vector . . . . . . . . . 4.3.5 TVMeasLayer Class: Measurement Layer . . . . . . . . . . . . . . . . . . . . . . . . . 4.3.6 TVKalDetector Class: Inheriting from TObjArray; Detector Component . . . . . . . . 4.3.7 TKalDetCradle Class: Inheriting from TObjArray; Detector System . . . . . . . . . .
23 23 24 24 25 26 26 26 27 28 29 29 31 31
1
. . . . . . . . .
. . . . . . . . .
2 4.4
GeomLib: Geometry Class Library . . . . . . . . . . . . . . . . . . . . . . . 4.4.1 TVTrack Class: Inheriting from TVCurve; Track . . . . . . . . . . . 4.4.2 TVSurface Class: Inheriting from TObject; Surface . . . . . . . . . . 4.4.3 THelicalTrack Class: Inheriting from TVTrack; Helical Track . . . . 4.4.4 TStraightTrack Class: Inheriting from TVTrack; Straight Line Track 4.4.5 TCylinder Class: Inheriting from TVSurface; Cylindrical Surface . . 4.4.6 THype Class: Inheriting from TVSurface; Hyperboloidal Surface . . 4.4.7 TPlane Class: Inheriting from TVSurface; Flat Surface . . . . . . .
. . . . . . . .
5 How To Use the Kalman Filter Package 5.1 EXMeasLayer Class: Inheriting from TVMeasLayer and TCylinder . . . . . . 5.1.1 Declaration of EXMeasLayer Class: EXMeasLayer.h . . . . . . . . . . . 5.1.2 Implementation of EXMeasLayer Class: Preamble . . . . . . . . . . . . 5.1.3 Constructor and Destructor: EXMeasLayer() and ~ EXMeasLayer() . . 5.1.4 Coordinate Projector: XvToMv() . . . . . . . . . . . . . . . . . . . . . 5.1.5 Reverse Coordinate Projector: HitToMv() . . . . . . . . . . . . . . . . 5.1.6 Projector Matrix: CalcDhDa() . . . . . . . . . . . . . . . . . . . . . . 5.2 EXKalDetector Class: Inheriting from TVKalDetector . . . . . . . . . . . . . 5.2.1 Declaration of EXKalDetector Class: EXKalDetector.h . . . . . . . . 5.2.2 Implementation of EXKalDetector Class: Preamble . . . . . . . . . . 5.2.3 Constructor and Destructor: EXKalDetector() and ~ EXKalDetector() 5.3 EXHit Class: Inheriting from TVTrackHit . . . . . . . . . . . . . . . . . . . . 5.3.1 Declaration of EXHit Class: EXHit.h . . . . . . . . . . . . . . . . . . . 5.3.2 Implementation of EXHit Class: Preamble . . . . . . . . . . . . . . . . 5.3.3 Constructor and Destructor: EXHit() and ~ EXHit() . . . . . . . . . . . 5.3.4 Coordinate Projector: XvToMv() . . . . . . . . . . . . . . . . . . . . . 5.3.5 Debugging Method: DebugPrint() . . . . . . . . . . . . . . . . . . . . 5.4 EXKalTest: Main Program . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . .
31 32 33 34 35 37 38 39
. . . . . . . . . . . . . . . . . .
40 41 41 42 42 43 43 44 45 45 46 46 47 47 48 49 49 50 50
Chapter 1
Introduction The ultimate goal of a detector system at a future linear e+ e− collider experiment is to measure two 4-vectors, pµ (4-momentum), and sµ (spin polarization), of ”partons” (quarks, leptons, gauge bosons, and other fundamental particles such as Higgs bosons, if any) produced in e+ e− collisions so that we can visualize the underlying Feynman amplitudes and determine their corresponding S-matrix element. When you have quarks or gluons in the final states, however, what you actually observe is not these fundamental partons but jets of particles, which are predominantly stable hadrons. In order to reconstruct parent partons from the jets of particles, we need a high resolution particle tracker capable of efficient and accurate reconstruction of densely packed tracks of charge particles in the jets. Track reconstruction consists of two steps: track finding, which selects hits belonging to a single track from the set of hits created by all the charged particles in an event, and track fitting, which fits the selected hits to a track model and determines its track parameters at the interaction point. The purpose of this short review is to elaborate this second step, track fitting, so as to find out the optimal way to extract track parameters at the interaction point. In a uniform magnetic field, a charged particle follows a helical trajectory, which can be specified by 5 parameters: (dρ , φ0 , κ, dz , tan λ), if there is no multiple scattering or energy loss in detector materials. The simplest way to extract the track parameters is then to perform a χ2 fit assuming that the particle follows a perfect helix, ignoring multiple scattering and energy loss. In this method, however, we have a single track parameter vector for a single set of hits. In the case of a low momentum tracks, for which multiple scattering and energy loss due to ionization are significant, a relatively strong disturbance in the middle of the flight would distort the trajectory and subsequent hits would deteriorates the fit near the interaction point (see Fig. 1.1-a)). In order to solve this problem, HEP community started to use Kalman filter for track reconstruction in around 1987 [2, 3, 4]. The idea of Kalman filter first appeared in 1960 [1] to deal with a linear system under the influence of random disturbance. It is designed to effectively estimate, from multiple observations about a system, its state at a certain point. The method is effective in particular when • equation of motion is (quasi-)linear1 , • there are many discrete points of measurements, • the distance between adjacent measurement points is short enough so that the state at k-th point is 1 Since the original Kalman filter is for a linear system, its application to a non-linear system such as a charged particle trajectory in a magnetic field requires proper linearization. Strictly speaking, therefore, the Kalman filter we are going to discuss in what follows should be called an extended Kalman filter. We shall, however, call it Kalman filter in this note.
3
CHAPTER 1. INTRODUCTION
a) Normal least square fitting
IP Momentum at IP Only one track parameter can be defined
4
b) Kalman fitting
IP Momentum at IP Track parameters are defined par hit
Figure 1.1: Simple χ2 fitting versus Kalman filter based fitting. well approximated by the extrapolation of the state at (k − 1)-th point using the equation of motion, even in the presence of random disturbance between them. The most crucial difference from the simple χ2 fitting to a single helix is that we can put a different track parameter vector to a different measurement point, since we update the track parameters as we add a new measurement point. Because of this we can take into account step-by-step the evolution of the track parameters due to energy loss, multiple Coulomb scattering, and decays such as K → µν or π → µν, etc., and hence improve the fit near the interaction point and consequently the estimation of the particle’s momentum thereat (see Fig. 1.1-b)).
Chapter 2
Principle of Kalman Filter 2.1
Statement of the Problem
In general a system we deal with using Kalman filter is subject to random disturbance (process noise) during its evolution following an equation of motion (system equation). Our goal is to derive the best estimate of this system’s state at a given point from information collected at multiple observation points (measurement sites). We assume that the state of the system can be specified by a p-dimensional column vector (state vector), a ¯k , where the suffix k denotes the site at which the state is given and the bar over a means that it is the true value. The system equation that describes the evolution of the state a ¯k−1 at site (k − 1) to a ¯k at site (k) can be written in the following form: a ¯k = f k−1 (¯ ak−1 ) + wk−1 ,
(2.1)
where f k−1 (¯ ak−1 ), which is a non-linear function in general, is a state propagator corresponding to a smooth deterministic motion expected if the random disturbance due to the process noise, wk−1 , were absent. We assume that the process noise has no bias: < wk−1 >= 0 and has a covariance given by Qk−1 ≡ < wk−1 wTk−1 > .
(2.2)
At each measurement site, we measure observables about the system. The set of observed values of these observables at site (k) forms an m-dimensional column vector called measurement vector, mk . The relation between the measurement vector and the state vector thereat is a measurement equation: mk = hk (¯ ak ) + ²k ,
(2.3)
where hk (¯ ak ) gives the true measurement vector that would be observed if the random error due to the measurement noise, ²k , were absent. Notice that hk (¯ ak ) is also non-linear in general and serves as a projector that projects out components of the measurement vector. We again assume that the random measurement noise is unbiased1 : < ²k >= 0 and has a covariance given by V k ≡ (G)−1 ≡ < ²k ²Tk > .
(2.4)
1 In the case of tracking, this corresponds to assuming that the alignment is perfect and there is no systematic errors in coordinate measurements.
5
CHAPTER 2. PRINCIPLE OF KALMAN FILTER
2.1.1
6
Example 1: Ballistic Missile
The Kalman filter was originally invented to track a ballistic missile and predict its trajectory. In this case, the state vector, ak , consists of position and momentum of the missile at site (k): µ
ak =
x p
¶
(2.5) k
which evolves according to Hamiltonian equation with the process noise, wk−1 , corresponding to random turbulence between (k − 1) and (k). The radar-detected position and velocity of the missile at site (k) comprises the measurement vector, mk , which is subject to a finite measurement error, ²k , of the radar system.
2.1.2
Example 2: Tracking in HEP Experiments
Our main concern is tracking of a charged particle in a magnetic field. If the magnetic field is uniform, at least locally, near the measurement site (k), then we can approximate the track of this particle by a helix[5]: x = x0
y = y0 z = z 0
+ dρ cos φ0 + + dρ sin φ0 + + dz −
α κ α κ α κ
(cos φ0 − cos(φ0 + φ)) (sin φ0 − sin(φ0 + φ)) tan λ · φ,
(2.6)
where x0 = (x0 , y0 , z0 )Tk is a pivotal point or simply pivot that represents a measurement site (k). At a given site, or equivalently for a given pivot, the helix is then specified by 5 parameters:
dρ φ 0 ak = κ . dz tan λ k
(2.7)
This parameter vector serves as the state vector in this case. Notice that the change of pivot transforms the helix parameter vector. As we move from site (k − 1) to site (k), the state vector hence propagates from ak−1 to ak according to the pivot transformation. The pivot transformation thus determines the function f k−1 in the system equation. On the other hand, the process noise, wk−1 , in this case comes from multiple Coulomb scattering and energy loss between site (k − 1) and site (k). We shall elaborate these points later. The measurement vector mk is a set of coordinates measured by a position sensitive detector at site (k), which is subject to random detector noise expressed by ²k .
2.1.3
What We Need
Accumulation of the measurement vectors improves the estimate of the state of the system. What we need is hence machineary (recurrence formulae) to do • Prediction, which predicts the state vector at site (k 00 > k) based on the observations made at sites up to (k): {m0k ; k 0 ≤ k} 7→ akk00 >k : future • Filtering, which updates the predicted state vector at site (k) based on the observations made at sites up to (k − 1) by adding the observation at site (k): {m0k ; k 0 ≤ k} 7→ akk00 =k : present
CHAPTER 2. PRINCIPLE OF KALMAN FILTER
7
• Smoothing, which improves the filtered state vector at site (k 00 < k) by using all the observations made at sites up to (k): : past {m0k ; k 0 ≤ k} 7→ akk00 0) X −x c ³ 0 0´ = y −Y c −1 0 tan (κ < 0) x00 −Xc = κ ¡ ¢ = z0 − z00 + dz − ακ (φ00 − φ0 ) tan λ = tan λ, (
¡
α κ
(3.4)
¢
Xc ≡ x0 + ¡ dρ + ακ¢ cos φ0 Yc ≡ y0 + dρ + ακ sin φ0 .
(3.5)
Equation (3.4) together with Eq.(3.5) defines the propagator function f k−1 (ak−1 ) for the state vector.
3.2.2
Propagator Matrix: F k−1
By differentiating Eq.(3.4) with respect to a = (dρ , φ0 , κ, dz , tan λ)T we can calculate the propagator matrix F k−1 :
F k−1
µ 0¶ ∂a ≡ = ∂a
∂d0ρ ∂a ∂φ00 ∂a ∂κ0 ∂a ∂d0z ∂a ∂ tan λ0 ∂a
.
Notice that κ and tan λ do not change by the pivot transformation and hence the calculation of their partial derivatives is trivial. For the calculation of the partial derivatives of the rest we will start with φ00 since it appears also in d0ρ and d0z . ∂φ00 /∂a Noting that the coordinates of the new pivot, x00 , y00 , and z00 , are not parameters but arbitrarily chosen constants, we obtain from Eq.(3.4) the partial derivative of φ00 with respect to a variable ω to be ∂φ00 ∂ω
Ã
= cos
2
φ00
∂Yc ∂ω
−
Xc − x00
tan φ00
∂Xc ∂ω
Xc − x00
!
,
(3.6)
where (Xc , Yc ) is related to (x00 , y00 ) through Eq.(3.5) as Xc
≡ x00 + d0ρ +
Yc
≡ y00 + d0ρ +
³
³
´
α 0 κ0 ´ cos φ0 α 0 κ0 sin φ0 ,
resulting in Xc − x0 0
=
Yc − y 0 0
=
³
d0ρ +
³
d0ρ +
´
α 0 κ ´ cos φ0 α 0 κ sin φ0
(3.7)
CHAPTER 3. APPLICATION OF KALMAN FILTER TO TRACK FITTING
18
since κ0 = κ. To complete the differentiation, all we need now is to calculate ∂Xc /∂a and ∂Yc /∂a, using Eq.(3.5):
∂Xc ∂dρ ∂Xc ∂φ0 ∂Xc
∂κ ∂Xc ∂dz ∂Xc
∂ tan λ
= cos φ0 µ
= − = −
¶
d0ρ
α + sin φ0 κ
α cos φ0 κ2
∂Yc ∂dρ
= 0
µ
∂Yc ∂φ0
=
∂Yc
∂κ ∂Yc ∂dz ∂Yc
= 0
= sin φ0 ¶
d0ρ
= −
α + cos φ0 κ
α sin φ0 κ2
(3.8)
= 0
∂ tan λ
= 0.
The desired partial derivatives are then readily obtained by substituting Eqs.(3.7) and (3.8) in Eq.(3.6) ∂φ0 as needed. The resultant ∂a0 is given by
µ
∂φ00 ∂dρ
= −
∂φ00 ∂φ0
=
d0ρ
µ
dρ + µ
∂φ00 ∂κ
=
∂φ00 ∂dz ∂φ00
α + κ α κ
¶−1
sin(φ00 − φ0 )
¶µ
α α d0ρ + 2 κ κ
d0ρ + ¶−1
α κ
¶−1
cos(φ00 − φ0 )
sin(φ00 − φ0 )
(3.9)
= 0
∂ tan λ
= 0,
regardless of the sign of κ. ∂d0ρ /∂a As with φ00 , we obtain the following from Eq.(3.4) for the partial derivative of d0ρ with respect to a variable ω: ∂d0ρ ∂ω
=
∂Xc ∂φ0 cos φ00 − (Xc − x00 ) sin φ00 0 ∂ω ∂ω
+
∂Yc ∂φ0 ∂ α sin φ00 + (Yc − y00 ) cos φ00 0 − . ∂ω ∂ω ∂ω κ
CHAPTER 3. APPLICATION OF KALMAN FILTER TO TRACK FITTING
19
Using this equation together with Eqs.(3.8) and (3.9), we can carry out partial differentiation of d0ρ with respect to a and obtain
∂d0ρ ∂dρ
= cos(φ00 − φ0 ) µ
∂d0ρ ∂φ0
=
∂d0ρ ∂κ ∂d0ρ ∂dz
=
¶
α dρ + sin(φ00 − φ0 ) κ
¢ α ¡ 1 − cos(φ00 − φ0 ) 2 κ
(3.10)
= 0
∂d0ρ ∂ tan λ
= 0
regardless of the sign of κ. Other Partial Derivatives Partial derivatives of κ0 and tan λ0 are trivial: ∂κ0 ∂dρ ∂κ0
∂κ ∂ tan λ0 ∂dρ
0 ∂ tan λ
∂ tan λ
=
∂κ0 ∂κ0 ∂κ0 = = =0 ∂φ0 ∂dz ∂ tan λ
= 1 =
(3.11)
∂ tan λ0 ∂ tan λ0 ∂ tan λ0 = = =0 ∂φ0 ∂κ ∂dz
= 1.
With the help of Eq.(3.9) partial derivatives of d0z are also easily obtained to be
∂d0z ∂dρ ∂d0z ∂φ0
∂d0z ∂κ
∂d0z ∂dz ∂d0z
∂ tan λ
regardless of the sign of κ.
µ
¶−1
=
α 0 α dρ + κ κ
=
α α tan λ 1 − dρ + κ κ
=
α 0 α α tan λ φ00 − φ0 − dρ + 2 κ κ κ
Ã
Ã
= 1 α = − (φ00 − φ0 ) κ
µ
tan λ sin(φ00 − φ0 ) ¶µ
d0ρ µ
α + κ
¶−1
¶−1
!
cos(φ00
− φ0 ) !
sin(φ00 − φ0 )
(3.12)
CHAPTER 3. APPLICATION OF KALMAN FILTER TO TRACK FITTING
3.2.3
20
Process Noise: Qk−1
For completeness, we discuss here how to implement multiple Coulomb scattering as the process noise, wk−1 , between site (k − 1) and site (k). Notice that in the recurrence formulae given by Eq.(3.1) the effect of the process noise only appears as its variance, Qk−1 , in C k−1 = F k−1 C k−1 F Tk−1 + Qk−1 . k Of course, the variance of the process noise depends of the distribution of materials between site (k − 1) and site (k). For simplicity, let us first assume that the distance between the two sites is small enough so that we can approximate the multiple scattering to take place as if all the materials between site (k − 1) and site (k) are concentrated on an infinitely thin layer at the mid point (m; k − 1 < m < k) between site (k − 1) and site (k). In this thin layer limit[5], the covariance matrix Qm of the helix parameter vector at the mid point (m) takes the following simple form:
Qm
0 0 0 1 + tan2 λ 2 = σM 0 0 S 0 0 0 0
0 0 (κ tan λ)2 0 κ tan λ(1 + tan2 λ)
0 0 0 0 0
0 0 κ tan λ(1 + tan2 λ) , 0 2 2 (1 + tan λ)
(3.13)
where the κ and tan λ are those at the mid point but can be approximated by those at site (k − 1), provided that the distance between site (k − 1) and the mid point is small enough to do so, and σM S is given as usual by µ
σM S =
¶
0.0141 p 1 XL 1 + log10 XL , P (GeV)β 9
(3.14)
where P , β, and XL are the momentum, the speed in units of the speed of light, and the thickness of the scatterer in units of its radiation length. The desired covariance matrix for the process noise Qk−1 is then obtained by moving the pivot from the mid point (m) to site (k): Qk−1 = F m,k Qm F Tm,k ,
(3.15)
where F m,k is the propagator matrix for the covariance matrix2 of the state vector from the mid point (m) to site (k) as given by Eqs.(3.6), (3.10), (3.11), and (3.12) with a = am and a0 = akk−1 . When the track segment between site (k − 1) and site (k) is not short enough to apply the thin layer approximation elaborated above, we can subdivide the segment into N steps and perform the following summation: Qk−1 =
N −1 X
F ms ,k Qms F Tms ,k .
(3.16)
s=1
3.3
Measurement Equation: hk , H k , and V k
Having derived the formulae related to the system equation, let us now move on to the derivations of the formulae related to the measurement equation: mk = hk (¯ ak ) + ²k , 2
In this notation F k−1 ≡ F k−1,k
CHAPTER 3. APPLICATION OF KALMAN FILTER TO TRACK FITTING
21
where the measurement vector mk is a set of coordinates measured by a position sensitive detector at site (k), which is subject to random detector noise expressed by ²k . What we need is the projector (hk (a)), which gives the exact coordinates of the hit point at site (k) as a function of the true track parameter vector at site (k), the projector matrix (H k ), which is its derivative with respect to the track parameter vector, and the covariance matrix (V k ) for the measurement noise. In practice, locations of a charged particle are sampled at discrete points along its trajectory. We can imagine layers of (virtual) surfaces on which we sample the locations and call them measurement surfaces or layers. Each measurement layer corresponds to a measurement site, say site (k), and records the coordinates of its intersection, a hit point, with the particle’s trajectory. The coordinates of the hit point comprise a measurement vector mk .
3.3.1
Projector: hk (a)
In order to predict the measurement vector on a measurement layer as a function of the state vector, i.e., the track parameter vector, we need, first of all, an equation that gives the intersection, xk (a), of a track specified by a with the measurement surface given by Sk (x) = 0: xk = xk (a) = x(φk (a), a)
(3.17)
and another equation that translates the intersection xk (a) to the measurement vector: mk = mk (xk ) ,
(3.18)
where x = x(φ, a) is a trajectory equation, which is given by Eq.(3.2) in the case of a helical track, and φk (a) is the deflection angle at which the track hits the measurement surface corresponding to site (k). Given these two equations, we can now define the projector hk (a) by hk (a) = mk (xk (a)) = mk (x(φk (a), a)) .
3.3.2
(3.19)
Projector Matrix: H k
Although concrete forms of these equations depend on the nature of the measurement layer and hence application-specific, we can write down a general formula for the derivative of hk with respect to a: Hk ≡
∂hk = ∂a
µ
∂mk ∂x
¶µ
¶
∂x(φk (a), a) , ∂a
(3.20)
where the second factor on the right-hand side is given by ∂x(φk (a), a) ∂x ∂φk ∂x = + , ∂a ∂φk ∂a ∂a
(3.21)
in which ∂x/∂φk and ∂x/∂a are determined solely by the trajectory equation. On the other hand, ∂mk /∂x is determined by the geometry of the measurement surface Sk (x) = 0 and the layout of the coordinate system on it, which are both application-specific. Let us hence calculate ∂φk /∂a below. We start from the equation for φk : Sk (x(φk , a)) = 0.
(3.22)
Differentiating both sides by the track parameter vector, we obtain 0=
∂Sk ∂Sk = ∂a ∂x
µ
∂x ∂φk ∂x + ∂φk ∂a ∂a
¶
,
(3.23)
CHAPTER 3. APPLICATION OF KALMAN FILTER TO TRACK FITTING
22
from which we have µ
∂φk ∂Sk =− ∂a ∂x
¶µ
∂x ∂a
¶ Áµ
∂Sk ∂x
¶µ
∂x ∂φk
¶
.
(3.24)
Notice that ∂Sk /∂x is determined solely by the geometry of the measurement surface and ∂x/∂φ and ∂x/∂a by the trajectory equation alone. Whether Eq.(3.22) has an analytic solution or not depends on the geometry of the measurement surface Sk (x) = 0 and it is often difficult to write down the analytic solution in a compact form even if it exists at all, unless we are dealing with particularly simple surfaces like cylinders of planes. In such a case, we rely on Newtonian method to solve the equation. Assume that we have an approximate solution φn , then we can Taylor-expand the equation as 0 = Sk (x(φ, a)) ' Sk (x(φn , a)) +
∂Sk ∂x · · (φ − φn ) , ∂x ∂φn
(3.25)
which results in a recurrence formula: φn+1 = φn − ³
Sk (x(φn , a)) ´ ³ ´ . ∂Sk ∂x ∂ x n · ∂φ n
(3.26)
We can use this recurrence formula iteratively until φn converges and hence Sk (x(φn , a)) becomes negligibly small3 . If the measurement layer in question is near the current pivot, we can usually start from φ1 = 0 and get the iteration converged after a few iterations.
3.3.3
Measurement Noise: V k
To complete the necessary set of the formulae for Kalman filter, we need the covariance matrix for the measurement noise (V k ), which is in our case given by an error matrix that represents the resolution of the position sensitive detector at site (k). Denoting the residual measurement vector by ∆mk ≡ mk − hk (¯ ak ) , we can define the covariance matrix by D
V k ≡ ∆mk ∆mTk
E
.
(3.27)
If we have two coordinates measured on the measurement surface at site (k) and if the two measured coordinates are statistically independent, we can write the covariance matrix in the form: µ
Vk =
σ12 0
0 σ22
¶
,
(3.28)
where σ1 and σ2 are the resolutions for the two coordinates, respectively. Although these resolutions depend on the hit location and the track angle, etc. in general, we can take V k as a constant matrix in the track fitting.
3 In practice it is necessary to introduce a damping factor (1 + Λ) to the denominator of the second term of Eq.(3.26), where we start from Λ ¿ 1 and if |Sk | increases we multiply Λ by a factor greater than 1 and if |Sk | decreases we divide it by the same factor.
Chapter 4
Implementation in C++ 4.1
Design Philosophy
Since the tracking system of a collider detector usually consists of various components such as a vertex detector (VD), an intermediate tracker (IT), a central tracker (CT), etc., which have different shapes and coordinate systems, the software package for Kalman-filter-based track fitting should be able to accommodate a measurement layer with any shape and/or coordinate system. Considering possible extrapolation of a track to an outer tracking device such as a muon detector, it is also desirable that the package can handle site-tosite change of the magnetic field. In order to satisfy these requirements with minimum user-implemented code, we shall use C++ and try to exploit its object-oriented features as much as possible. For persistency of data, we shall rely on ROOT’s automatic schema evolution. Notice that the Kalman filter defines a generic procedure and has a much wider scope than track fitting. This suggests necessity for a library of generic abstract base classes (KalLib) that implement the generic algorithm of the Kalman Filter. By inheriting from the generic base classes in KalLib and implementing their pure virtual methods for track fitting purpose, we can then realize a Kalman-filter-based track fitter library (KalTrackLib). However, KalTrackLib should not depend on any particular track model or shape or coordinate system of a measurement layer according to the above guideline. We hence separate out geometry classes that provide track model (helix, straight line, ...) and surfaces (cylinder, hyperboloid, flat plane, etc.) as a geometry library (GeomLib). Each of these three class libraries, KalLib, KalTrackLib, and GeomLib are distributed as a subpackage of LEDA (Library Extension for Data Analysis) or KalTest (Kalman filter Test bench). In this way we can minimize the number of user-implemented classes to the following three: • MeasLayer: a measurement layer that multiply inherits from an abstract measurement layer class TVMeasLayer and a shape class in GeomLib. • KalDetector: an array that holds user-defined MeasLayers with any shape and/or coordinate system. Notice that this also defines material distributions in the tracker. • Hit: a coordinate vector as defined by the MeasLayer. Notice also that track model can change site to site, which allows magnetic field variation along a particle trajectory.
23
CHAPTER 4. IMPLEMENTATION IN C++
4.2
24
KalLib: Base Class Library for Kalman Filter
Let us start with implementation of the base class library (KalLib) to describe a generic Kalman filter process. Fig.4.1 is a class diagram showing the architecture of KalLib. The TVKalSystem class is an abstract
KalLib Utils
Class TAttLockable
Class TVKalSystem + AddAndFilter(next: TVKalSite&) : void + SmoothBackTo(k: Int_t): void
Class TVKalSite - fM: TKalMatrix: meas. vector - fV: TKalMatrix: meas. noise - fH: TKalMatrix: @h/@a + Filter(): Bool_t + Smooth(post: TVKalSite&): void + CalcExpectedMeasVec( a: const TVKalState&, m: TKalMatrix&) = 0: Int_t + CalcMeasVecDerivative( a: const TVKalState&, H: TKalMatrix&) = 0: Int_t
Class TVKalState - fF: TKalMatrix: @f/@a - fQ: TKalMatrix: process noise - fC: TKalMatrix: cov. matrix + Propagate(to: TVKalSite&): void + MoveTo(to: const TVKalSite&, F: TKalMatrix&, Q: TKalMatrix&) = 0: void
Class TKalMatrix
Root: Class TObjArray
Class TObjArray
Class TMatrixD
Figure 4.1: KalLib: Kalman filter base class library. class representing a system to process with Kalman Filter. It is a ROOT’s TObjArray of TVKalSite’s. A TVKalSite object corresponds to a measurement site and is a TObjArray of up to three TVKalState’s, corresponding to predicted, filtered, and smoothed state vectors. On the other hand, a TVKalState object stands for a state vector and is a p × 1 TKalMatrix, which inherits from ROOT’s TMatrixD. These abstract classes have to carry f k−1 (ak−1 ), F k−1 , Qk−1 , mk , H k , and V k as data members and corresponding pure virtual functions to evaluate them. In what follows, we will explain major data members and member functions of these abstract classes so as to demonstrate which abstract class carries which data members and member functions.
4.2.1
TVKalSystem Class (Inheriting from TObjArray; an Array of Sites)
The TVKalSystem class inherits from ROOT’s TObjArray and hence behaves as an array of measurement sites. It is the abstract base class representing any Kalman system and is the only class that can handle relation between different sites. Data Members: TVKalSite * fCurSitePtr; // a pointer to the current site. Double t fChi2; // χ2 value. Member Functions:
CHAPTER 4. IMPLEMENTATION IN C++
25
void AddAndFilter(TVKalSite & next); filters the next site (next) and add it to the base TObjArray. void SmoothBackTo(Int t k); smoothes back from the last site to site (k).
4.2.2
TVKalSite Class (Inheriting from TObjArray: an Array of States: Measurement Site)
The TVKalSite class inherits from ROOT’s TObjArray and hence behaves as an array that carries state vectors at various stages (Predicted, Filtered, and Smoothed). It also inherits from TAttLockable in the Utils package so that it can be excluded from filtering without being eliminated from TVKalSystem. It is the abstract base class representing a measurement site and stores, as data members, information on the observation made there and the estimates of the state vector thereat. Data Members: TKalMatrix fM; // measurement vector. TKalMatrix fV; // measurement error matrix. TKalMatrix fH; // projector matrix. Member Functions: Int t CalcExpectedMeasVec(const TVKalState & a, TKalMatrix & h) = 0; A pure virtual function representing a projector to calculate the expected measurement vector h for a given state vector a. It is application-specific and to be implemented in a derived class. Int t CalcMeasVecDerivative(const TVKalState & a, TKalMatrix & H) = 0; A pure virtual function representing a projector matrix H, which is the derivative of the projector with respect to the input state vector a. It is application-specific and to be implemented in a derived class. Bool t Filter(); implements the generic algorithm of Kalman Filter as expressed by Eq.(3.1). It filters this instance of TVKalSite by using a predicted state vector from the previous site, a measurement vector, and its error matrix stored in the instance itself, calculates the filtered state vector ak , and its covariance matrix C k , and adds them to the base class TObjArray as a TVKalState. void Smooth(TVKalSite & post); implements the generic algorithm of Smoothing as given by Eqs.(2.34) and (2.35). It
CHAPTER 4. IMPLEMENTATION IN C++
26
smoothes this instance of TVKalSite by using the information stored in the instance itself and that from the post site (post), calculates the smoothed state vector ank and its covariance matrix C nk , and adds them to the base class TObjArray as a TVKalState.
4.2.3
TVKalState Class (Inheriting from TKalMatrix: State Vector)
The TVKalState class is a p × 1 TKalMatrix, which in turn inherits from ROOT’s TMatrixD (a double precision matrix). It is the abstract base class representing a state vector stored in the base class TKalMatrix. Data Members: TVKalSite * fSitePtr; TKalMatrix fF; TKalMatrix fQ; TKalMatrix fC;
// // // //
pointer to corresponding site. propagator matrix. covariance matrix for the process noise. covariance matrix for the state vector.
Member Functions: TVKalState & MoveTo(const TVKalSite & to, TKalMatrix & F, TKalMatrix & Q) const = 0; A pure virtual function that transports this instance of TVKalState to the destination site (to) and returns itself as reference, together with the corresponding propagator matrix and the process noise matrix. It is application-specific and to be implemented in a derived class. void Propagate(TVKalSite & to); propagates this instance of TVKalState to the destination site (to) and calculates the covariance matrix of the predicted state vector there. It implements the generic algorithm (Eqs.(2.9) and (2.13)) to propagate a state vector and its covariance matrix by means of the MoveTo() function.
4.3
Kaltracklib: Kalman-Filter-Based Track Fitting Library
In this section, we will apply the Kalman filter technique to track fitting by implementing, in a way specific to track fitting, pure virtual member functions of the base classes such as TVKalSystem, TVKalSite, and TVKalState in KalLib in their derived classes. These derived classes comprise, together with some other supplementary classes, a class library called KalTrackLib. Fig.4.2 is a class diagram showing the architecture of KalTrackLib. The classes defined in KalTrackLib implement the common algorithm specific to track fitting but are not dependent on any particular shape or coordinate system of tracking devices in question. In what follows, we will explain essential features , major data members and member functions relevant to end users, of the classes in KalTrackLib.
4.3.1
TKalTrack Class: Inheriting from TVKalSystem; Track
A track is a set of hit points. It is, hence, natural to define a track as an instance of a class inheriting from TVKalSystem, which is an array of measurement sites. Since TVKalSystem has no pure virtual functions, there is no base class member function to override.
CHAPTER 4. IMPLEMENTATION IN C++
27
KalTrackLib
Class TKalDetCradle + Install( det: TVKalDetector&) : void + Transport( from: const TKalTrackSite&, to: const TKalTrackSite&, sv: TKalMatrix&, F: TKalMatrix&, Q: TKalMatrix&) : void
Class TVKalDetector
Utils Class TAttElement
Class TKalTrack
Class TVTrackHit
Class TVMeasLayer
- fMeasLayerPtr: TVMeasLayer*
- fIndex: Int_t: index in Cradle - fMateriialInPttr: TMaterial* - fMaterialOutPtr: TMaterial* + HitToXv( ht: const TVTrackHit&) const = 0 : TVector3 + XvToMv( ht: const TVTrackHit&, xv: const TVector3&) const = 0: TKalMatrix + CalcDhDa( ht: const TVTrackHit&, xv: const TVector3&, dxdphiada: const TKalMatrix&, H: TKalMatrix&) const = 0: void + GetEnergyLoss( isoutgoing: Bool_t, hel : const TVTrack&, df : Double_t) const: Double_t + CalcQms( isoutgoing: Bool_t, hel : const TVTrack&, df : Double_t, Qms : TKalMatrix&) const : void
+ XvToMv( xv: const TVector3&, t0: Double_t) const =0 : TKalMatrix
Class TKalTrackSite - fX0: TVector3: pivot - fHitPtr: TVTrackHit* + CalcExpectedMeasVec( a: const TVKalState&, h: TKalMatrix&): Int_t + CalcMeasVecDerivative( a: const TVKalState&, H: TKalmatrix&): Int_t
Class TKalTrackState
- fX0: TVector3: pivot + MoveTo( to: constTVKalSite&, F: TKalMatrix&, Q: TKalMatrix&) const: TKalTrackState&
KalLib
Class TVKalSystem + AddAndFilter(next: TVKalSite&) : void + SmoothBackTo(k: Int_t): void
Class TVKalSite - fM: TKalMatrix: meas. vector - fV: TKalMatrix: meas. noise - fH: TKalMatrix: @h/@a + Filter(): Bool_t + Smooth(post: TVKalSite&): void + CalcExpectedMeasVec( a: const TVKalState&, m: TKalMatrix&) = 0: Int_t + CalcMeasVecDerivative( a: const TVKalState&, H: TKalMatrix&) = 0: Int_t
Class TVKalState - fF: TKalMatrix: @f/@a - fQ: TKalMatrix: process noise - fC: TKalMatrix: cov. matrix + Propagate(to: TVKalSite&): void + MoveTo(to: const TVKalSite&, F: TKalMatrix&, Q: TKalMatrix&) = 0: void
Class TKalMatrix
Figure 4.2: KalTrackLib: Kalman-filter-based track fitting class library. Member Functions: TKalTrack(); constructor that constructs an instance of TKalTrack.
4.3.2
TKalTrackSite Class: Inheriting from TVKalSite; Measurement Site
In track fitting, a measurement site provides a hit point, whose coordinates comprise a measurement vector. The TKalTrackSite class is designed to serve as the measurement site and implements the pure virtual member functions of the base class, TVKalSite, while carrying a pointer to a measurement vector (TVTrackHit) as one of its data members. Data Members:
CHAPTER 4. IMPLEMENTATION IN C++
28
TVTrackHit * fHitPtr; // pointer to a corresponding hit object. TVector3 fX0; // pivot set to the hit point. Member Functions: TKalTrackSite(const TVTrackHit & ht); A constructor that constructs an instance from a hit object. Int t CalcExpectedMeasVec(const TVKalState & a, TKalMatrix & h); implements its base class pure virtual function that provides a projector to calculate the expected measurement vector h for a given state vector a. This function realizes the algorithm in subsection 3.3.1 and the Newtonian method in subsection 3.3.2. Int t CalcMeasVecDerivative(const TVKalState & a, TKalMatrix & H); implements its base class pure virtual function that provides a projector matrix H, which is the derivative of the projector with respect to the input state vector a. This function realizes the algorithm in subsection 3.3.2. Functions for derivatives such as ∂x/∂φk and ∂x/∂a that appear in the realization of the generic algorithm in CalcExpectedMeasVec() and CalcMeasVecDerivative() depend on a track model (trajectory equation). Their interface will be fixed by an abstract base class, TVTrack, defined later in GeomLib and be implemented in a derived class that describes a particular track model such as a helix or a straight line. The derivative of the measurement vector with respect to the position vector ∂mk /∂x is, on the other hand, determined by the shape and coordinate system of the measurement layer in question. The interface of the function to calculate it will be specified by an abstract base class, TVMeasLayer, defined later and be implemented in a derived class that describes a particular measurement layer.
4.3.3
TKalTrackState Class: Inheriting from TVKalState; Track Parameter Vector
In track fitting, a state vector carries track parameters as their components. TKalTrackState is designed to serve as the track parameter vector and implements the pure virtual member function MoveTo() of the base class, TVKalState. Data Members: TVector3
fX0;
// pivot.
Member Functions: TKalTrackState & MoveTo(const TVKalSite & to, TKalMatrix & F, TKalMatrix & Q) const; implements the base class pure virtual function that transports this instance of TKalTrackState to the destination site (to) and returns itself as reference, together with the corresponding propagator matrix and the process noise matrix.
CHAPTER 4. IMPLEMENTATION IN C++
29
The calculation of the process noise depends on the distribution of materials between the current site and the next site. As described below the structure of a tracking device is defined by a concrete class derived from an abstract base class, TVKalDetector, which is an array of measurement layers that derive from their abstract base class TVMeasLayer. The information on the local materials about the measurement layer is stored in the TVMeasLayer class. If necessary, we can compose a tracking system consisting of multiple tracking devices by installing the corresponding KalDetectors into a folder class, TKalDetCradle. The instance of TKalDetCradle then knows everything about the detector configuration. The MoveTo() function thus invokes TKalDetCradle::Transport() to move to site to in order to take into account the process noise that depends on the material distribution. Notice that the TKalDetCradle::Transport() functions invokes a pure virtual MoveTo() function of an abstract class, TVTrack, in GeomLib so that the track model can change from site to site in accordance with the local magnetic field.
4.3.4
TVTrackHit Class: Inheriting from TKalMatrix; Measurement Vector
In track fitting, a hit point plays the role of a measurement vector. It provides a set of coordinates at the intersection of a particle’s trajectory with a measurement layer. The TVTrackHit class is designed to be the abstract base class for any hit point and specifies, through a pure virtual method (XvToMv()), the interface for the coordinate projector from the 3-dimensional position vector corresponding to the hit point to coordinate axes of the measurement layer. Data Members: Double t fBfield; // magnetic field. TVMeasLayer * fMeasLayerPtr; // pointer to the corresponding measurement layer. Member Functions: TKalMatrix XvToMv(const TVector3 & xv, Double t t0) const = 0; A pure virtual function that specifies the interface for the coordinate projector from the expected hit position vector xv to the coordinate axes of the measurement layer, knowing the measured coordinates as stored in the instance and the input time stamp t0 of the hit. The coordinate projector depends on the coordinate system of the measurement layer in question and hence is to be implemented in a derived class. In order to introduce a new tracking device, one has to define a concrete hit point class inheriting from TVTrackHit and implements its pure virtual method, XvToMv().
4.3.5
TVMeasLayer Class: Measurement Layer
As mentioned earlier, a tracking detector can be regarded as a set of measurement layers. Each of these measurement layers should be equipped with at least common interfaces for the projector hk and the projector matrix H k and for various other functions needed to calculate them using the generic algorithms explained in subsections 3.3.1 and 3.3.2. These common interfaces are specified by abstract base classes, TVMeasLayer in KalTrackLib and TVSurface in GeomLib. A concrete measurement layer then multiply inherits from TVMeasLayer and a concrete surface object such as a cylinder (TCylinder) or a hyperboloid (THype) or a plane (TPlane) derived from TVSurface, thereby realizing a particular shape and a coordinate system specific to the measurement layer. The TVMeasLayer class inherits from TAttElement and behaves as an element of some container, a TVKalDetector object described below.
CHAPTER 4. IMPLEMENTATION IN C++
30
Data Members: TMaterial TMaterial Int t Bool t
* fMaterialInPtr; * fMaterialOutPtr; fIndex; fIsActive;
// pointer to inner material. // pointer to out ner material. // index in TKalDetCradle. // flag to tell layer is active or not.
Notice that a measurement layer defines a surface on which coordinate measurements are made or, if it is not active, it just spcifies a boundary between different materials. Member Functions: TKalMatrix XvToMv(const TVTrackHit & ht, const TVector3 & xv) const = 0; A pure virtual function corresponding to Eq.(3.18) that projects the 3-dimensional hit point vector xv to coordinate axes and returns the resultant measurement vector. TVector3 HitToXv(const TVTrackHit & ht) const = 0; A pure virtual function that transforms a measurement vector as stored in ht to a 3dimensional hit point vector. To be used to calculate a next pivot. void CalcDhDa(const TVTrackHit & ht, const TVector3 & xv, const TKalMatrix & dxphiada, TKalMatrix & H) const = 0; A pure virtual function that calculates the projector matrix H at the hit point xv with dxphiada = ∂x(φ(a), a)/∂a given as an input, assuming the use of the generic algorithm given in subsection 3.3.2. Double t CalcEnergyLoss(
Bool t isoutgoingt, const TVTrack & hel, Double t df) const;
calculates and returns energy loss during the evolution of the track hel by the deflection angle df, knowing the direction of flight specified by isoutgoing. void CalcQms(
Bool t isoutgoingt, const TVTrack & hel, df, Double t TKalMatrix & Qms) const;
calculates the process noise matrix Qm as given by Eq.(3.13) for the evolution of the track hel by the deflection angle df, knowing the direction of flight specified by isoutgoing. As mentioned above, a concrete measurement layer multiply inherits from this TVMeasLayer class and a concrete surface object derived from TVSurface. Since the surface objects such as TCylinder or THype or TPlane are already equipped with methods to calculate the intersection of a track and a surface, etc., all users have to implement in the concrete measurement layer class is the projector to coordinate axes as given by Eq.(3.18) and the corresponding projector matrix.
CHAPTER 4. IMPLEMENTATION IN C++
4.3.6
31
TVKalDetector Class: Inheriting from TObjArray; Detector Component
In order to describe a tracking detector such as VTX, IT, and CT, consisting of multiple measurement layers implemented as instances of a concrete class that multiply inherits from the abstract base class TVMeasLayer and a concrete surface class such as TCylinder or THype or TPlane, we define an abstract base class called TVKalDetector, which inherits from ROOT’s TObjArray and carries these concrete measurement layers as array elements. The TVKalDetector class acts only as a container and has no special member functions. The TVKalDetector class also inherits from TAttElement and behaves as an element of an instance of TKalDetCradle described below. Notice that one can Add() a measurement layer with any shape or any coordinate system to TVKalDetector, as far as the measurement layer has TVMeasLayer and TVSurface as its base classes. This way one can compose a detector system with measurement layers with various shapes and coordinate systems in a unified manner.
4.3.7
TKalDetCradle Class: Inheriting from TObjArray; Detector System
In order to describe a whole tracking system consisting of tracking detector components such as VTX, IT, and CT, we define a class called TKalDetCradle, which inherits from ROOT’s TObjArray and carries the individual measurement layers from various detector components as its array elements. The whole purpose of TKalDetCradle is to hold all the measurement layers including dummy layers corresponding to a beam pipe, support cylinders, etc. The TKalDetCradle object is the only object that knows everything about the distribution of materils in the detector system and hence its Transport() member function is used by TKalTrackState::MoveTo(). Member Functions: TKalDetCradle(); A constructor that constructs an instance of TKalDetCradle. void Install(TVKalDetector & det); installs det as a component of this instance of TKalDetCradle. void Transport(const TKalTrackSite & const TKalTrackSite & TKalMatrix & TKalMatrix & TKalMatrix &
from, to, sv, F, Q) const;
transports the input state sv from site from to site to and calculates the propagator matrix F and the process noise Q with successive thin layer multiple scattering approximation given by Eqs.(3.13) and (3.16).
4.4
GeomLib: Geometry Class Library
GeomLib is a library consisting of classes to describe track models and measurement surfaces. A class to represent a concrete track model such as a helix or a straight line should inherit from an abstract base class called TVTrack, while a class to represent a concrete surface such as a cylinder or a plane should inherit
CHAPTER 4. IMPLEMENTATION IN C++
32
from an abstract base class called TVSurface. These abstract base classes serve to set interfaces. Fig.4.3 is a class diagram showing the architecture of GeomLib. The classes defined in GeomLib implement utility
GeomLib
Class TPlane
- fXc : TVector3: a point on the plane - fNormal: TVector3: normal Class TVSurface + CalcXingPointWith(hel: const TVTrack&, xx : TVector3&, phi: Double_t, mode: Int_t, eps: Double_t = 1.e-8) const: Int_t + CalcS (xx: const TVector3&) const = 0: Double_t + CaclDSDx (xx: const TVector3&) const = 0: TMatrixD + IsOnSurface (xx: const TVector3&) const = 0: Bool_t + GetSortingPolicy() const = 0: Double_t
+ CalcS (xx: const TVector3&) const: Double_t + CaclDSDx (xx: const TVector3&) const: TMatrixD + IsOnSurface (xx: const TVector3&) const: Bool_t + GetSortingPolicy() const: Double_t Class TCylinder
- fR : Double_t: radius - fHalfLen: Double_t: half length - fXc : TVector3: center + CalcXingPointWith(hel: const TVTrack&, xx : TVector3&, phi: Double_t, mode: Int_t, eps: Double_t = 1.e-8) const: Int_t + CalcS (xx: const TVector3&) const: Double_t + CaclDSDx (xx: const TVector3&) const: TMatrixD + IsOnSurface (xx: const TVector3&) const: Bool_t + GetSortingPolicy() const: Double_t
Class TVTrack
- fDrho : Double_t - fPhi0 : Double_t - fKappa: Double_t - fDz : Double_t - fTanL : Double_t - fX0 : TVector3: pivot + MoveTo(x0to: const TVector3&, fid : Double_t&, F : TMatrixD* = 0, C : TMatrixD* = 0) = 0: void + CalcXAt (phi: Double_t) const = 0: TVector3 + CaclDxDa (phi: Double_t) const = 0: TMatrixD + CaclDxDphi(phi: Double_t) const = 0: TMatrixD + CaclDapDa(fid : Double_t, dr : Double_t, drp: Double_t, F : TMatrixD&) const = 0: void
Class THelicalTrack + MoveTo(x0to: const TVector3&, fid : Double_t&, F : TMatrixD* = 0, C : TMatrixD* = 0): void + CalcXAt (phi: Double_t) const: TVector3 + CaclDxDa (phi: Double_t) const: TMatrixD + CaclDxDphi(phi: Double_t) const: TMatrixD + CaclDapDa(fid : Double_t, dr : Double_t, drp: Double_t, F : TMatrixD&) const: void + CalcStartHelix(x1: const TVector3&, x2: const TVector3&, x3: const TVector3&, dir: Bool_t = kIterForward): void
Figure 4.3: GeomLib: geometry class library to describe track models and measurement surfaces. methods to calculate the intersection of a track and a surface, the tangential vector of a track, the gradient of a surface, etc., which are independent of KalTrackLib or KalLib. In what follows, we will explain essential features , major data members and member functions relevant to end users, of the classes in GeomLib.
4.4.1
TVTrack Class: Inheriting from TVCurve; Track
The TVTrack class is the abstract base class that sets interface for a trajectory equation as a function of a track parameter a and a parameter φ that specifies location along the trajectory1 . Data Members:
1 The trajectory parameter φ is the deflection angle from the pivot in the case of a helical track. It is the signed path length from the pivot in the case of a straight line track.
CHAPTER 4. IMPLEMENTATION IN C++ Double Double Double Double Double Double
t t t t t t
fDrho; fPhi0; fKappa; fDz; fTanL; fAlpha;
// // // // // //
33
dρ . φ0 . κ. dz . tan λ. α ≡ 1/cB (see Eq.(3.3)).
Member Functions: void MoveTo(const TVector3 Double t TMatrixD TMatrixD
& & * *
xv0,] fid, F = 0, C = 0) = 0;
A pure virtual function to move the pivot to xv0 and calculate the parameter change fid to the new pivot together with *F= ∂a0 /∂a and/or the covariance matrix at the new pivot *C, if F6= 0 and/or C6= 0. TVector3 CalcXAt(Double t phi) const = 0; A pure virtual function to calculate the position vector of a particle along the track at phi. TMatrixD CalcDxDa(Double t phi) const = 0; A pure virtual function to calculate and return ∂x(φ, a)/∂a (see subsection 3.3.2). TMatrixD CalcDxDphi(Double| t phi) const = 0; A pure virtual function to calculate and return the tangential vector to the trajectory: ∂x(φ, a)/∂φ (see subsection 3.3.2).
4.4.2
TVSurface Class: Inheriting from TObject; Surface
The TVSurface class is the abstract base class for any surface and sets interfaces for the methods to calculate its intersection with a track, the gradient at a given point on it, etc., as needed in the generic algorithm explained in subsection 3.3.2. Member Functions: Int t CalcXingPointWith(const TVTrack & hel, TVector3 & xx, Double t & phi, Int t mode, Double t eps = 1.e-8) const; implements the Newtonian method in subsection 3.3.2 to calculate the intersection xx with a track hel and the corresponding location parameter phi with the tolerance eps. The mode parameter is dummy here. It returns the number of intersections.
CHAPTER 4. IMPLEMENTATION IN C++
34
Double| t CalcS(const TVector3 & xx) const = 0; A pure virtual function to calculate the left-hand side Sk (x) of the surface equation (Eq.(3.22)) at x =xx. TMatrixD CalcDSDx(const TVector3 & xx) const = 0; A pure virtual function to calculate the gradient of Sk (x): ∂Sk /∂x at x =xx. Bool t IsOnSurface(const TVector3 & xx) const = 0; A pure virtual function to test if xx is on the surface or not. Double t GetSortingPolicy() const = 0; A pure virtual function to get the sorting policy with which to sort surfaces from inside to outside.
4.4.3
THelicalTrack Class: Inheriting from TVTrack; Helical Track
The THelicalTrack class inherits from TVTrack and implements its pure virtual functions, thereby realizing a helical track. Member Functions: void MoveTo(const TVector3 Double t TMatrixD TMatrixD
& & * *
xv0, fid, F = 0, C = 0);
moves the pivot to xv0 and calculates the deflection angle fid to the new pivot together with *F= ∂a0 /∂a and/or the covariance matrix at the new pivot *C, if F6= 0 and/or C6= 0 as explained in section 3.2. TVector3 CalcXAt(Double t phi) const; calculates the position vector of a particle along the helix at the deflection angle phi according to Eq.(3.2).
x0 + dρ cos φ0 + x x (φ, a) = y = y0 + dρ sin φ0 + z0 + d z − z TMatrixD CalcDxDphi(Double| t phi) const = 0;
α κ α κ α κ
(cos φ0 − cos(φ0 + φ)) (sin φ0 − sin(φ0 + φ)) tan λ · φ
CHAPTER 4. IMPLEMENTATION IN C++
35
calculates and returns the tangential vector to the trajectory: ∂x ∂φ ∂x(φ, a) ∂y = ∂φ ∂φ ∂z
− sin(φ0 + φ) µ ¶ α · cos(φ0 + φ) . =− κ tan λ
(4.1)
∂φ See subsection 3.3.2. TMatrixD CalcDxDa(Double t phi) const; calculates and returns the track-parameter derivative of the trajectory:
cos φ0
¢ ¡ sin φ0 + ακ sin(φ0 + φ) − dρ + α κ ∂x(φ, a) = − κα2 (cos φ0 − cos(φ0 + φ)) ∂a 0
sin φ0 ¡
dρ +
0
α κ
¢
cos φ0 −
α κ
0
cos(φ0 + φ)
− κα2 (sin φ0 − sin(φ0 + φ)) 0 0
T
0 α φ tan λ . κ2 1
− ακ φ
See subsection 3.3.2. void CalcDapDa(Double t Double t Double t TMatrixD &
fid, dr, drp, F) const;
calculates F = ∂a0 /∂a when the pivot is moved by ∆φ0 = fid and dρ changed from dr to drp. See subsection 3.2.2. void CalcStartHelix(const TVector3 & x1, const TVector3 & x2, const TVector3 & x3, Bool t dir = kIterForward) const; finds a helix passing through 3 points x1, x2, and x3 and sets the resultant helix parameters to itself, knowing the direction of flight specified by dir.
4.4.4
TStraightTrack Class: Inheriting from TVTrack; Straight Line Track
A straight line track class TStraightTrack can be defined as the α → ∞ and φ → 0 limit of the THelicalTrack class with α φ = constant. κ
CHAPTER 4. IMPLEMENTATION IN C++
36
If we define this constant to be −ψ, we have α (cos φ0 − cos(φ0 + φ)) κ α α (sin φ − sin(φ + φ)) lim lim φ 0 0 = − φ→0 φ→0 κ κ α
−
κ
cos(φ0 +φ)−cos φ0 φ sin(φ0 +φ)−sin φ0 φ
tan λφ
− sin φ0
= ψ cos φ0
,
tan λ
tan λ
which leads us to the following equation of trajectory:
x0 + dρ cos φ0 − φ sin φ0 x x (φ, a) = y = y0 + dρ sin φ0 + φ cos φ0 z0 + d z + φ tan λ z
(4.2)
with φ redefined to be ψ. Notice that φ now stands for the signed projected path length to the x-y plane measured from the pivot. Member Functions: void MoveTo(const TVector3 Double t TMatrixD TMatrixD
& & * *
xv0, fid, F = 0, C = 0);
moves the pivot to xv0 and calculates the projected path length fid to the new pivot together with *F= ∂a0 /∂a and/or the covariance matrix at the new pivot *C, if F6= 0 and/or C6= 0. TVector3 CalcXAt(Double t phi) const; calculates the position vector of a particle along the track at the projected path length phi according to Eq.(4.2). TMatrixD CalcDxDphi(Double| t phi) const = 0; calculates and returns the tangential vector to the trajectory:
− sin φ0
∂x(φ, a) = cos φ0 ∂φ tan λ TMatrixD CalcDxDa(Double t phi) const;
.
(4.3)
CHAPTER 4. IMPLEMENTATION IN C++
37
calculates and returns the track-parameter derivative of the trajectory2 :
cos φ0
− d sin φ − φ cos φ ρ 0 0 ∂x(φ, a) = 0 ∂a 0
sin φ0 dρ cos φ0 − φ sin φ0
0 void CalcDapDa(Double t Double t Double t TMatrixD &
0 0 0
0
T
0 . 0 1
φ
fid, dr, drp, F) const;
calculates F = ∂a0 /∂a when the pivot is moved by ∆φ0 = fid and dρ changed from dr to drp.
4.4.5
TCylinder Class: Inheriting from TVSurface; Cylindrical Surface
The TCylinder class inherits from TVSurface and implements its pure virtual functions, thereby realizing a cylindrical surface. Data Members: Double t Double t TVector3
fR; fHalfLen; fXc;
// radius. // half length. // center: xc = (xc , yc , zc )T .
Member Functions: Int t CalcXingPointWith(const TVTrack & hel, TVector3 & xx, Double t & phi, Int t mode, Double t eps = 1.e-8) const; overrides, for CPU time economy, the base class’s Newtonian method to calculate the intersection xx with a track hel and the corresponding location parameter phi with the tolerance eps, since an analytic solution is available. The (forward, closest, backward) crossing pint is chosen for mode = (-1,0,+1), respectively. It returns the number of intersections. Double| t CalcS(const TVector3 & xx) const; calculates the left-hand side Sk (x) of the surface equation (Eq.(3.22)) at x =xx: Sk (x) = (x − xc )2 + (y − yc )2 − fR2
(4.4)
2 We continue to use the same parameter vector with five components a = (dρ , φ0 , κ, dz , tan λ)T as with a helical track, though the trajectory does not depend on κ, in order to facilitate the continuation from or to a helix.
CHAPTER 4. IMPLEMENTATION IN C++
38
TMatrixD CalcDSDx(const TVector3 & xx) const; calculates the gradient of Sk (x): ∂Sk /∂x at x =xx: ∂Sk = ∂x
µ
∂Sk ∂x
∂Sk ∂y
∂Sk ∂z
¶
= ( 2(x − xc ) 2(y − yc ) 0 ) .
(4.5)
Bool t IsOnSurface(const TVector3 & xx) const; tests if xx is within the length of the cylinder. Double t GetSortingPolicy() const; returns the radius fR as the sorting policy with which to sort surfaces from inside to outside.
4.4.6
THype Class: Inheriting from TVSurface; Hyperboloidal Surface
The THype class inherits from TVSurface and implements its pure virtual functions, thereby realizing a single-leaf hyperboloidal surface. Data Members: Double t Double t TVector3 Double t
fR0; fHalfLen; fXc; fTanA;
// waist radius at z = 0. // half length. // center: xc = (xc , yc , zc )T . // tan A with A being the stereo angle of wires.
Member Functions: Double| t CalcS(const TVector3 & xx) const; calculates the left-hand side Sk (x) of the surface equation (Eq.(3.22)) at x =xx: Sk (x) = (x − xc )2 + (y − yc )2 − fR2 − (z − zc )2 fTanA2
(4.6)
TMatrixD CalcDSDx(const TVector3 & xx) const; calculates the gradient of Sk (x): ∂Sk /∂x at x =xx: ∂Sk = ∂x
µ
∂Sk ∂x
∂Sk ∂y
∂Sk ∂z
¶
= ( 2(x − xc ) 2(y − yc ) −2(z − zc ) fTanA2 ) .
(4.7)
Bool t IsOnSurface(const TVector3 & xx) const; tests if xx is on the surface and within the length of the hyperboloid. Double t GetSortingPolicy() const; returns the radius fR0 as the sorting policy with which to sort surfaces from inside to outside.
CHAPTER 4. IMPLEMENTATION IN C++
4.4.7
39
TPlane Class: Inheriting from TVSurface; Flat Surface
The TPlane class inherits from TVSurface and implements its pure virtual functions, thereby realizing a flat surface. Data Members: TVector3 TVector3
fXc; fNormal;
// reference point on the plane: xc = (xc , yc , zc )T . // outward normal: n = (nx , ny , nz )T .
Member Functions: Double| t CalcS(const TVector3 & xx) const; calculates the left-hand side Sk (x) of the surface equation (Eq.(3.22)) at x =xx: Sk (x) = (x − xc ) · n = (x − xc ) nx + (y − yc ) ny + (z − zc ) nz
(4.8)
TMatrixD CalcDSDx(const TVector3 & xx) const; calculates the gradient of Sk (x): ∂Sk /∂x at x =xx: ∂Sk = n = ( nx ∂x
ny
nz ) .
(4.9)
Bool t IsOnSurface(const TVector3 & xx) const; tests if xx is on the surface. Double t GetSortingPolicy() const; returns the distance from the origin as the sorting policy with which to sort surfaces from inside to outside.
Chapter 5
How To Use the Kalman Filter Package In this chapter we will show how to use the C++ class libraries elaborated in the last chapter, KalLib, KalTrackLib, and GeomLib, to apply the Kalman filter technique to track fitting with a particular tracking system, taking a simple example of a cylindrical tracking system distributed as examples/kaltest/ct in the KalTest package. As shown in Fig.5.1, the example includes the following three user-defined classes User Land (KalTest/CT) Class EXMeasLayer
EXKalTest: Main Program
Class EXHit + XvToMv( xv: const TVector3&, t0: Double_t) const : TKalMatrix
Class EXKalDetector + EXKalDetector(m: Int_t = 100)
+ HitToXv( ht: const TVTrackHit&) const = 0 : TVector3 + XvToMv( ht: const TVTrackHit&, xv: const TVector3&) const = 0: TKalMatrix + CalcDhDa( ht: const TVTrackHit&, xv: const TVector3&, dxdphiada: const TKalMatrix&, H: TKalMatrix&) const = 0: void
KalTrackLib GeomLib Class TVMeasLayer Class TVKalDetector
Class TVTrackHit
- fMeasLayerPtr: TVMeasLayer* + XvToMv( xv: const TVector3&, t0: Double_t) const =0 : TKalMatrix
+ HitToXv( ht: const TVTrackHit&) const = 0 : TVector3 + XvToMv( ht: const TVTrackHit&, xv: const TVector3&) const = 0: TKalMatrix + CalcDhDa( ht: const TVTrackHit&, xv: const TVector3&, dxdphiada: const TKalMatrix&, H: TKalMatrix&) const = 0: void
Class TCylinder
Figure 5.1: Sample code for a simple cylindrical tracker: examples/kaltest/ct. required by the package: • EXMeasLayer: a measurement layer that multiply inherits from the abstract measurement layer class TVMeasLayer and a concrete surface class TCylinder in GeomLib, • EXKalDetector: an array that holds the user-defined measurement layers with particular shape and coordinate system and defines material distributions in the tracker, and 40
CHAPTER 5. HOW TO USE THE KALMAN FILTER PACKAGE
41
• EXHit: a measurement vector as defined by EXMeasLayer, as well as a steering program to actually carry out the Kalman-filter-based track fitting EXKalTest. In what follows, we will explain essential features of these program units1 .
5.1
EXMeasLayer Class: Inheriting from TVMeasLayer and TCylinder
Let us start with a class EXMeasLayer to describe a simple cylindrical measurement layer having a radius R with its axis aligned with the z-axis. As explained in subsection 4.3.5, this suggests that EXMeasLayer is to inherits from TVMeasLayer in KalTrackLib and TCyliner in GeomLib. For simplicity, let us assume that EXMeasLayer provides Rφ and z coordinates of a hit point at r = R with fixed measurement errors σrφ and σz , respectively. This sets the coordinate system necessary to implement the pure virtual functions of TVMeasLayer to calculate the projector to coordinate axes as given by Eq.(3.18) and the corresponding projector matrix.
5.1.1
Declaration of EXMeasLayer Class: EXMeasLayer.h
The header file for EXMeasLayer is shown below: #ifndef __EXMEASLAYER__ #define __EXMEASLAYER__ #include #include #include #include #include
"TVector3.h" "TKalMatrix.h" "TCylinder.h" "TVMeasLayer.h" "KalTrackDim.h"
// // // // //
from from from from from
ROOT KalLib GeomLib KalTrackLib KalTrackLib
class TVTrackHit; // forward declaration class EXMeasLayer : public TVMeasLayer, public TCylinder { public: // Ctors and Dtor EXMeasLayer(TMaterial &min, TMaterial &mout, Double_t r0, Double_t lhalf, Bool_t isactive = kTRUE); virtual ~EXMeasLayer(); // Parrent’s pure virtuals that must be implemented virtual TKalMatrix XvToMv (const TVTrackHit &ht, const TVector3 &xv) const; virtual TKalMatrix XvToMv (const TVector3 &xv) const; virtual TVector3 HitToXv (const TVTrackHit &ht) const; 1 Notice that the sample code shown below might be slightly different from the actual code distributed with KalTest because of possible omissions of some features or details irrelevant for the purpose of this chapter.
CHAPTER 5. HOW TO USE THE KALMAN FILTER PACKAGE
virtual void
CalcDhDa
ClassDef(EXMeasLayer,1)
42
(const TVTrackHit &ht, const TVector3 &xv, const TKalMatrix &dxphiada, TKalMatrix &H) const;
// Sample measurement layer class
}; #endif
where ClassDef(...) is a ROOT macro that expands into declarations for ROOT-required data fields and methods for automatic schema evolution.
5.1.2
Implementation of EXMeasLayer Class: Preamble
The implementation of EXMeasLayer starts with #include "EXMeasLayer.h" #include "EXHit.h" ClassImp(EXMeasLayer)
where ClassImp(EXMeasLayer) is a ROOT macro that expands into implementations of ROOT-methods declared in the ClassDef(...) macro in the header file for automatic schema evolution.
5.1.3
Constructor and Destructor: EXMeasLayer() and ~ EXMeasLayer()
Then we need a constructor and the destructor:
EXMeasLayer::EXMeasLayer(TMaterial &min, // TMaterial &mout, // Double_t r0, // Double_t lhalf, // Bool_t isactive) // : TVMeasLayer(min, mout, isactive), TCylinder(r0, lhalf) { } EXMeasLayer::~EXMeasLayer() { }
where TMaterial is a ROOT class to store material information.
material inside the layer material outside the layer radius of the layer half length of the layer flag to tell the layer is active
CHAPTER 5. HOW TO USE THE KALMAN FILTER PACKAGE
5.1.4
43
Coordinate Projector: XvToMv()
Then we need to implement the coordinate projector, which projects the hit point vector xk to the coordinate axes to get the corresponding measurement vector: µ
mk (xk ) =
Rk · φk zk
¶
µ
=
³ ´¶ yk Rk · tan−1 x k , zk
where Rk is the radius of layer (k). The coordinate projector method, XvToMv(.....), calculates and returns the measurement vector mv as the return value: TKalMatrix EXMeasLayer::XvToMv(const TVector3 &xv) const { // Calculate hit coordinate information: // mv(0,0) = r * phi // (1,0) = z TKalMatrix mv(kMdim,1); mv(0,0) = GetR() * TMath::ATan2(xv.Y(), xv.X()); mv(1,0) = xv.Z(); return mv; } TKalMatrix EXMeasLayer::XvToMv(const TVTrackHit & /* ht */, const TVector3 &xv) const { return XvToMv(xv); }
where GetR() is the method of the base class TCylinder. Notice that in this example, ht, is not used.
5.1.5
Reverse Coordinate Projector: HitToMv()
We also need to implement the reverse coordinate projector, which projects back the measurement vector stored in an EXHit object to the 3-dimensional hit position vector:
xk
xk Rk · cos φk = yk = Rk · sin φk , zk zk
which can be realized for instance as follows: TVector3 EXMeasLayer::HitToXv(const TVTrackHit &vht) const { const EXHit &ht = dynamic_cast(vht); Double_t phi = ht(0,0) / GetR();
CHAPTER 5. HOW TO USE THE KALMAN FILTER PACKAGE
Double_t z Double_t x Double_t y
44
= ht(1,0); = GetR() * TMath::Cos(phi); = GetR() * TMath::Sin(phi);
return TVector3(x,y,z); }
5.1.6
Projector Matrix: CalcDhDa()
Finally we need to implement the projector matrix:
Hk =
∂(Rk ·φk ) ∂a ∂zk ∂a
=
³
Rk
∂φk ∂ xk
´³
∂ xk ∂a
´
=
− Rykk
∂zk ∂a
³
∂xk ∂a
´
+
xk Rk
³
∂yk ∂a
´ ,
∂zk ∂a
where ∂xk ∂x (φ (a) , a) = ∂a ∂a ∂φk ∂a
=
∂x ∂φk ∂x · + ∂φk ∂a ∂a
= −³
1 ∂Sk ∂x
∂x · ∂φ k
´
∂Sk ∂x · ∂x ∂a
as explained in subsection 3.3.2. The projector matrix method, CalcDhDa(.....), can then be implemented for instance as folows:
void EXMeasLayer::CalcDhDa(const TVTrackHit &, // Hit: not used here const TVector3 &xxv, // hit position vector const TKalMatrix &dxphiada, // @x(\phi(a),a)/@a TKalMatrix &H) const // projector matrix = @h/@a { // Calculate // H = (@h/@a) = (@phi/@a, @z/@a)^t // where // h(a) = (phi, z)^t: expected measurement vector // a = (drho, phi0, kappa, dz, tanl, t0) // Int_t sdim = H.GetNcols(); Int_t hdim = TMath::Max(5,sdim-1); Double_t xv = xxv.X(); Double_t yv = xxv.Y(); Double_t xxyy = xv * xv + yv * yv; // Set H = (@h/@a) = (@d/@a, @z/@a)^t
CHAPTER 5. HOW TO USE THE KALMAN FILTER PACKAGE
45
for (Int_t i=0; i kPi) dphi -= kTwoPi; h(0,0) = r * (phim + dphi); h(1,0) += 0.; return h; }
CHAPTER 5. HOW TO USE THE KALMAN FILTER PACKAGE
50
where t0 is not used in this simple example. Notice that the function of this XvToMv() method is to avoid the wrap-around problem at the 2π boundary and to choose the correct φ that matches the measurement vector stored in the EXHit object.
5.3.5
Debugging Method: DebugPrint()
The DebugPrint() function is another pure virtual function in the base class, TVTrackHit, that must be implemented to facilitate debugging: void EXHit::DebugPrint(Option_t *) const { cerr