Fork me on GitHub

source: git/external/TrackCovariance/ObsTrk.cc@ 5c716fa

Last change on this file since 5c716fa was ff9fb2d9, checked in by Pavel Demin <pavel.demin@…>, 5 years ago

add TrackCovariance

  • Property mode set to 100644
File size: 3.9 KB
Line 
1#include <iostream>
2
3#include <TMath.h>
4#include <TVector3.h>
5#include <TMatrixD.h>
6#include <TMatrixDSym.h>
7#include <TDecompChol.h>
8#include <TRandom.h>
9
10#include "SolGridCov.h"
11#include "ObsTrk.h"
12
13using namespace std;
14
15// x(3) track origin, p(3) track momentum at origin, Q charge, B magnetic field in Tesla
16ObsTrk::ObsTrk(TVector3 x, TVector3 p, Double_t Q, Double_t B, SolGridCov *GC)
17{
18 fGC = GC;
19 fGenX = x;
20 fGenP = p;
21 fGenQ = Q;
22 fB = B;
23 fGenPar.ResizeTo(5);
24 fObsPar.ResizeTo(5);
25 fCov.ResizeTo(5, 5);
26 fGenPar = XPtoPar(x, p, Q);
27 fObsPar = GenToObsPar(fGenPar, fGC);
28 fObsX = ParToX(fObsPar);
29 fObsP = ParToP(fObsPar);
30 fObsQ = ParToQ(fObsPar);
31}
32
33ObsTrk::~ObsTrk()
34{
35}
36
37TVectorD ObsTrk::XPtoPar(TVector3 x, TVector3 p, Double_t Q)
38{
39 TVectorD Par(5);
40 // Transverse parameters
41 Double_t a = -Q * fB * 0.2998; // Units are Tesla, GeV and m
42 Double_t pt = p.Pt();
43 Double_t C = a / (2 * pt); // Half curvature
44
45 Double_t r2 = x.Perp2();
46 Double_t cross = x(0) * p(1) - x(1) * p(0);
47 Double_t T = TMath::Sqrt(pt * pt - 2 * a * cross + a * a * r2);
48 Double_t phi0 = TMath::ATan2((p(1) - a * x(0)) / T, (p(0) + a * x(1)) / T); // Phi0
49 Double_t D; // Impact parameter D
50 if (pt < 10.0) D = (T - pt) / a;
51 else D = (-2 * cross + a * r2) / (T + pt);
52
53 Par(0) = D; // Store D
54 Par(1) = phi0; // Store phi0
55 Par(2) = C; // Store C
56 // Longitudinal parameters
57 Double_t B = C * TMath::Sqrt(TMath::Max(r2 - D * D,0.0) / (1 + 2 * C * D));
58 Double_t st = TMath::ASin(B) / C;
59 Double_t ct = p(2) / pt;
60 Double_t z0 = x(2) - ct * st;
61
62 Par(3) = z0; // Store z0
63 Par(4) = ct; // Store cot(theta)
64
65 return Par;
66}
67
68TVector3 ObsTrk::ParToX(TVectorD Par)
69{
70 Double_t D = Par(0);
71 Double_t phi0 = Par(1);
72 Double_t z0 = Par(3);
73
74 TVector3 Xval;
75 Xval(0) = -D * TMath::Sin(phi0);
76 Xval(1) = D * TMath::Cos(phi0);
77 Xval(2) = z0;
78
79 return Xval;
80}
81
82TVector3 ObsTrk::ParToP(TVectorD Par)
83{
84 Double_t C = Par(2);
85 Double_t phi0 = Par(1);
86 Double_t ct = Par(4);
87 //
88 TVector3 Pval;
89 Double_t pt = fB * 0.2998 / TMath::Abs(2 * C);
90 Pval(0) = pt * TMath::Cos(phi0);
91 Pval(1) = pt * TMath::Sin(phi0);
92 Pval(2) = pt * ct;
93 //
94 return Pval;
95}
96
97Double_t ObsTrk::ParToQ(TVectorD Par)
98{
99 return TMath::Sign(1.0, -Par(2));
100}
101
102TVectorD ObsTrk::GenToObsPar(TVectorD gPar, SolGridCov *GC)
103{
104 TVector3 p = ParToP(gPar);
105 Double_t pt = p.Pt();
106 Double_t tanTh = 1.0 / TMath::Abs(gPar(4));
107 Double_t angd = TMath::ATan(tanTh) * 180. / TMath::Pi();
108 // Check ranges
109 Double_t minPt = GC->GetMinPt ();
110 if (pt < minPt) cout << "Warning ObsTrk::GenToObsPar: pt " << pt << " is below grid range of " << minPt << endl;
111 Double_t maxPt = GC->GetMaxPt();
112 if (pt > maxPt) cout << "Warning ObsTrk::GenToObsPar: pt " << pt << " is above grid range of " << maxPt << endl;
113 Double_t minAn = GC->GetMinAng();
114 if (angd < minAn) cout << "Warning ObsTrk::GenToObsPar: angle " << angd
115 << " is below grid range of " << minAn << endl;
116 Double_t maxAn = GC->GetMaxAng();
117 if (angd > maxAn) cout << "Warning ObsTrk::GenToObsPar: angle " << angd
118 << " is above grid range of " << maxAn << endl;
119 TMatrixDSym Cov = GC->GetCov(pt, angd);
120 fCov = Cov;
121 // Now do Choleski decomposition and random number extraction, with appropriate stabilization
122 TMatrixDSym CvN = Cov;
123 TMatrixDSym DCv(5); DCv.Zero();
124 TMatrixDSym DCvInv(5); DCvInv.Zero();
125 for (Int_t id = 0; id < 5; id++)
126 {
127 Double_t dVal = TMath::Sqrt(Cov(id, id));
128 DCv (id, id) = dVal;
129 DCvInv(id, id) = 1.0 / dVal;
130 }
131 CvN.Similarity(DCvInv); // Normalize diagonal to 1
132 TDecompChol Chl(CvN);
133 Bool_t OK = Chl.Decompose(); // Choleski decomposition of normalized matrix
134 TMatrixD U = Chl.GetU(); // Get Upper triangular matrix
135 TMatrixD Ut(TMatrixD::kTransposed, U); // Transposed of U (lower triangular)
136 TVectorD r(5);
137 for (Int_t i = 0; i < 5; i++) r(i) = gRandom->Gaus(0.0, 1.0); // Array of normal random numbers
138 TVectorD oPar = gPar + DCv * (Ut * r); // Observed parameter vector
139
140 return oPar;
141}
Note: See TracBrowser for help on using the repository browser.