Fork me on GitHub

source: git/external/TrackCovariance/SolGridCov.cc@ 4e8e72b

Last change on this file since 4e8e72b was ebf40fd, checked in by Franco BEDESCHI <bed@…>, 3 years ago

Major update to handle highly displaced tracks

  • Property mode set to 100644
File size: 6.4 KB
Line 
1#include <iostream>
2
3#include <TMath.h>
4#include <TVectorD.h>
5#include <TVector3.h>
6#include <TMatrixDSym.h>
7#include <TDecompChol.h>
8#include <TMatrixDSymEigen.h>
9
10#include "SolGridCov.h"
11#include "SolGeom.h"
12#include "SolTrack.h"
13
14using namespace std;
15
16SolGridCov::SolGridCov()
17{
18 // Define pt-polar angle grid
19 fNpt = 22;
20 fPta.ResizeTo(fNpt);
21 Double_t p[] = { 0.1, 0.2, 0.5, 0.7, 1., 2., 3., 4., 6., 8., 10., 15.,
22 20., 25., 30., 40., 50., 60., 80., 100., 150., 200. };
23 for (Int_t ip = 0; ip < fNpt; ip++) fPta(ip) = p[ip];
24
25 fNang = 13;
26 fAnga.ResizeTo(fNang);
27 Double_t a[] = { 10., 15., 20., 25., 30., 35., 40., 45., 50., 60., 70., 80., 90. };
28 for (Int_t ia = 0; ia < fNang; ia++) fAnga(ia) = a[ia];
29 fCov = new TMatrixDSym[fNpt * fNang];
30 for (Int_t ip = 0; ip < fNpt; ip++)
31 {
32 for (Int_t ia = 0; ia < fNang; ia++) fCov[ip * fNang + ia].ResizeTo(5, 5);
33 }
34}
35
36SolGridCov::~SolGridCov()
37{
38 delete[] fCov;
39 delete fAcc;
40}
41
42void SolGridCov::Calc(SolGeom *G)
43{
44 TVectorD pta = fPta;
45 TVectorD anga = fAnga;
46 Bool_t Res = kTRUE; Bool_t MS = kTRUE; // Resolution and multiple scattering flags
47 for (Int_t ip = 0; ip < fNpt; ip++) // Loop on pt grid
48 {
49 Int_t ipt = TMath::Nint(10 * pta(ip));
50 for (Int_t ia = 0; ia < fNang; ia++) // Loop on angle grid
51 {
52 Double_t th = TMath::Pi() * (anga(ia)) / 180.;
53 Double_t x[3], p[3];
54 x[0] = 0; x[1] = 0; x[2] = 0; // Set origin
55 p[0] = pta(ip); p[1] = 0; p[2] = pta(ip) / TMath::Tan(th);
56 //
57 SolTrack *tr = new SolTrack(x, p, G); // Initialize track
58 tr->CovCalc(Res, MS); // Calculate covariance
59 fCov[ip * fNang + ia] = tr->Cov(); // Get covariance
60 }
61 }
62
63// Now make acceptance
64fAcc = new AcceptanceClx(G);
65}
66
67
68//
69Bool_t SolGridCov::IsAccepted(Double_t pt, Double_t Theta)
70{
71 //
72 // pt in GeV, Theta in degrees
73 //
74 Bool_t Accept = kFALSE;
75 if (fAcc->HitNumber(pt, Theta) >= fNminHits)Accept = kTRUE;
76 //
77 return Accept;
78}
79//
80Bool_t SolGridCov::IsAccepted(Double_t *p)
81{
82 //
83 // pt in GeV, Theta in degrees
84 //
85 Bool_t Accept = kFALSE;
86 Double_t pt = TMath::Sqrt(p[0] * p[0] + p[1] * p[1]);
87 Double_t th = 180. * TMath::ATan2(pt, p[2])/TMath::Pi();
88 if (fAcc->HitNumber(pt,th) >= fNminHits)Accept = kTRUE;
89 //
90 return Accept;
91}
92//
93Bool_t SolGridCov::IsAccepted(TVector3 p)
94{
95 //
96 // pt in GeV, Theta in degrees
97 //
98 Bool_t Accept = kFALSE;
99 Double_t pt = p.Pt();
100 Double_t th = 180.*TMath::ACos(p.CosTheta())/TMath::Pi();
101 if (fAcc->HitNumber(pt,th) >= fNminHits)Accept = kTRUE;
102 //
103 return Accept;
104}
105//
106// Detailed acceptance check
107//
108Bool_t SolGridCov::IsAccepted(TVector3 x, TVector3 p, SolGeom* G)
109{
110 Bool_t Accept = kFALSE;
111 //
112 // Check if track origin is inside beampipe and betwen the first disks
113 //
114 Double_t Rin = G->GetRmin();
115 Double_t ZinPos = G->GetZminPos();
116 Double_t ZinNeg = G->GetZminNeg();
117 Bool_t inside = TrkUtil::IsInside(x, Rin, ZinNeg, ZinPos); // Check if in inner box
118 if (inside) Accept = IsAccepted(p);
119 else
120 {
121 SolTrack* trk = new SolTrack(x, p, G);
122 if (trk->nmHit() >= fNminHits)Accept = kTRUE;
123 delete trk;
124 }
125 //
126 return Accept;
127}
128
129//
130// Find bin in grid
131Int_t SolGridCov::GetMinIndex(Double_t xval, Int_t N, TVectorD x)
132{
133 Int_t min = -1; // default for xval below the lower limit
134 if (xval < x(0))return min;
135 if (xval>x(N - 1)){ min = N; return min; }
136 for (Int_t i = 0; i < N; i++) if (xval>x(i))min = i;
137 return min;
138}
139// Force positive definitness in normalized matrix
140TMatrixDSym SolGridCov::MakePosDef(TMatrixDSym NormMat)
141{
142 // Input: symmetric matrix with 1's on diagonal
143 // Output: positive definite matrix with 1's on diagonal
144
145 // Default return value
146 TMatrixDSym rMatN = NormMat;
147 // Check the diagonal
148 Bool_t Check = kFALSE;
149 Int_t Size = NormMat.GetNcols();
150 for (Int_t i = 0; i < Size; i++)if (TMath::Abs(NormMat(i, i) - 1.0)>1.0E-15)Check = kTRUE;
151 if (Check)
152 {
153 cout << "SolGridCov::MakePosDef: input matrix doesn ot have 1 on diagonal. Abort." << endl;
154 return rMatN;
155 }
156 // Diagonalize matrix
157 TMatrixDSymEigen Eign(NormMat);
158 TMatrixD U = Eign.GetEigenVectors();
159 TVectorD lambda = Eign.GetEigenValues();
160 // Reset negative eigenvalues to small positive value
161 TMatrixDSym D(Size); D.Zero(); Double_t eps = 1.0e-13;
162 for (Int_t i = 0; i < Size; i++)
163 {
164 D(i, i) = lambda(i);
165 if (lambda(i) <= 0) D(i, i) = eps;
166 }
167 // Rebuild matrix
168 TMatrixD Ut(TMatrixD::kTransposed, U);
169 TMatrixD rMat = (U*D)*Ut; // Now it is positive defite
170 // Restore all ones on diagonal
171 for (Int_t i1 = 0; i1 < Size; i1++)
172 {
173 Double_t rn1 = TMath::Sqrt(rMat(i1, i1));
174 for (Int_t i2 = 0; i2 <= i1; i2++)
175 {
176 Double_t rn2 = TMath::Sqrt(rMat(i2, i2));
177 rMatN(i1, i2) = 0.5*(rMat(i1, i2) + rMat(i2, i1)) / (rn1*rn2);
178 rMatN(i2, i1) = rMatN(i1, i2);
179 }
180 }
181 return rMatN;
182}
183// Interpolate covariance matrix: Bi-linear interpolation
184TMatrixDSym SolGridCov::GetCov(Double_t pt, Double_t ang)
185{
186 // pt in GeV and ang in degrees
187 Int_t minPt = GetMinIndex(pt, fNpt, fPta);
188 if (minPt == -1)minPt = 0;
189 if (minPt == fNpt - 1)minPt = fNpt - 2;
190 Double_t dpt = fPta(minPt + 1) - fPta(minPt);
191 // Put ang in 0-90 range
192 ang = TMath::Abs(ang);
193 while (ang > 90.)ang -= 90.; // Needs to be fixed
194 Int_t minAng = GetMinIndex(ang, fNang, fAnga);
195 if (minAng == -1)minAng = 0;
196 if (minAng == fNang - 1)minAng = fNang - 2;
197 Double_t dang = fAnga(minAng + 1) - fAnga(minAng);
198 //
199 Double_t tpt = (pt - fPta(minPt)) / dpt;
200 Double_t tang = (ang - fAnga(minAng)) / dang;
201 //
202 TMatrixDSym C11 = fCov[minPt * fNang + minAng];
203 TMatrixDSym C12 = fCov[minPt * fNang + minAng + 1];
204 TMatrixDSym C21 = fCov[(minPt + 1) * fNang + minAng];
205 TMatrixDSym C22 = fCov[(minPt + 1) * fNang + minAng + 1];
206 TMatrixDSym Cv = ((1-tpt) * (1-tang)) * C11 +
207 ((1-tpt) * tang ) * C12 +
208 ( tpt * (1-tang)) * C21 +
209 ( tpt * tang ) * C22;
210 // Check for positive definiteness
211 TMatrixDSym CvN = Cv;
212 TMatrixDSym DCvInv(5); DCvInv.Zero();
213 for (Int_t id = 0; id < 5; id++) DCvInv(id, id) = 1.0 / TMath::Sqrt(Cv(id, id));
214 CvN.Similarity(DCvInv); // Normalize diagonal to 1
215 TDecompChol Chl(CvN);
216 if (!Chl.Decompose())
217 {
218 std::cout << "SolGridCov::GetCov: Interpolated matrix not positive definite. Recovering ...." << std::endl;
219 TMatrixDSym rCv = MakePosDef(CvN); CvN = rCv;
220 TMatrixDSym DCv(5); DCv.Zero();
221 for (Int_t id = 0; id < 5; id++) DCv(id, id) = TMath::Sqrt(Cv(id, id));
222 Cv = CvN.Similarity(DCv); // Normalize diagonal to 1
223 }
224
225 return Cv;
226}
Note: See TracBrowser for help on using the repository browser.