EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CbmRichRingFitterEllipseTau.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CbmRichRingFitterEllipseTau.cxx
1 
8 // GMij are indices for a 5x5 matrix.
9 #define GM00 0
10 #define GM01 1
11 #define GM02 2
12 #define GM03 3
13 #define GM04 4
14 
15 #define GM10 5
16 #define GM11 6
17 #define GM12 7
18 #define GM13 8
19 #define GM14 9
20 
21 #define GM20 10
22 #define GM21 11
23 #define GM22 12
24 #define GM23 13
25 #define GM24 14
26 
27 #define GM30 15
28 #define GM31 16
29 #define GM32 17
30 #define GM33 18
31 #define GM34 19
32 
33 #define GM40 20
34 #define GM41 21
35 #define GM42 22
36 #define GM43 23
37 #define GM44 24
38 
39 
40 // Aij are indices for a 6x6 matrix.
41 
42 #define GA00 0
43 #define GA01 1
44 #define GA02 2
45 #define GA03 3
46 #define GA04 4
47 #define GA05 5
48 
49 #define GA10 6
50 #define GA11 7
51 #define GA12 8
52 #define GA13 9
53 #define GA14 10
54 #define GA15 11
55 
56 #define GA20 12
57 #define GA21 13
58 #define GA22 14
59 #define GA23 15
60 #define GA24 16
61 #define GA25 17
62 
63 #define GA30 18
64 #define GA31 19
65 #define GA32 20
66 #define GA33 21
67 #define GA34 22
68 #define GA35 23
69 
70 #define GA40 24
71 #define GA41 25
72 #define GA42 26
73 #define GA43 27
74 #define GA44 28
75 #define GA45 29
76 
77 #define GA50 30
78 #define GA51 31
79 #define GA52 32
80 #define GA53 33
81 #define GA54 34
82 #define GA55 35
83 
85 
86 
87 
88 using std::endl;
89 using std::cout;
90 
92 {
93 
94 }
95 
97 {
98 
99 }
100 
102  CbmRichRingLight *ring)
103 {
104  int nofHits = ring->GetNofHits();
105 
106  if ( nofHits <= 5) {
107  ring->SetXYABP(-1., -1., -1., -1., -1.);
108  ring->SetRadius(-1.);
109  return;
110  }
111 
112  //for (int i = 0; i < nofHits; i++) {
113  // CbmRichHit* hit = (CbmRichHit*) fHitsArray->At(ring->GetHit(i));
114  // fX.push_back(hit->GetX());
115  // fY.push_back(hit->GetY());
116  //}
117 
118  InitMatrices(ring);
119  Taubin();
120  TransformEllipse(ring);
121  CalcChi2(ring);
122 }
123 
125 {
126 // TMatrixD PQ(5,5); // fPQ = P^(-1) * Q
127 
128  Inv5x5();
129  Double_t PQa[25];
130  AMultB(fP, 25, 5, fQ, 25, 5, PQa); //PQ = fP*fQ;
131  TMatrixD PQ(5,5,PQa);
132 
133 // Double_t PQa2[5][5];
134 // Double_t d[5];
135 // Double_t v[5][5];
136 // for(Int_t i = 0; i<5; i++){
137 // for (Int_t j = 0; j<5;j++){
138 // PQa2[i][j] = PQa[5*i+j];
139 // cout << PQa2[i][j] << " ";
140 // }
141 // cout << endl;
142 // }
143 // cout << endl << endl;
144 //
145 // Jacobi(PQa2, d, v);
146 // Eigsrt(d, v);
147 
148  TMatrixDEigen eig(PQ);
149  TMatrixD evc = eig.GetEigenVectors();
150 
151  Double_t AlgParF = 0.;
152  AlgParF -= evc(0,0) * fM[GA05];
153  fAlgPar[0] = evc(0,0);
154 
155  AlgParF -= evc(1,0) * fM[GA15];
156  fAlgPar[1] = evc(1,0);
157 
158  AlgParF -= evc(2,0) * fM[GA25];
159  fAlgPar[2] = evc(2,0);
160 
161  AlgParF -= evc(3,0) * fM[GA35];
162  fAlgPar[3] = evc(3,0);
163 
164  AlgParF -= evc(4,0) * fM[GA45];
165  fAlgPar[4] = evc(4,0);
166 
167  fAlgPar[5] = AlgParF;
168 }
169 
171  CbmRichRingLight* ring)
172 {
173  const unsigned int numHits = ring->GetNofHits();
174  const unsigned int numHits2 = 2* numHits;
175  const unsigned int numHits3 = 3* numHits;
176  const unsigned int numHits4 = 4* numHits;
177  const unsigned int numHits5 = 5* numHits;
178  const unsigned int numHits6 = 6* numHits;
179  const double oneOverNumHits = 1./ numHits;
180  unsigned int i6;
181  for(unsigned int i = 0; i < numHits; i++){
182  double x = ring->GetHit(i).fX;
183  double y = ring->GetHit(i).fY;
184  i6= i*6;
185  fZ[i6] = fZT[i] = x*x;
186  fZ[i6+1] = fZT[i+numHits] = x*y;
187  fZ[i6+2] = fZT[i+numHits2] = y*y;
188  fZ[i6+3] = fZT[i+numHits3] = x;
189  fZ[i6+4] = fZT[i+numHits4] = y;
190  fZ[i6+5] = fZT[i+numHits5] = 1.;
191  }
192  AMultB(fZT, numHits6, numHits, fZ, numHits6, 6, fM);
193  for (int i = 0; i < numHits6;i++) fM[i] = oneOverNumHits*fM[i];
194 
195  for (int i = 0; i < 25; i++) fP[i]=0.;
196 
197  fP[GM00] = fM[GA00] - fM[GA05] * fM[GA05];
198  fP[GM01] = fP[GM10] = fM[GA01] - fM[GA05] * fM[GA15];
199  fP[GM02] = fP[GM20] = fM[GA02] - fM[GA05] * fM[GA25];
200  fP[GM03] = fP[GM30] = fM[GA03] - fM[GA05] * fM[GA35];
201  fP[GM04] = fP[GM40] = fM[GA04] - fM[GA05] * fM[GA45];
202 
203  fP[GM11] = fM[GA11] - fM[GA15] * fM[GA15];
204  fP[GM12] = fP[GM21] = fM[GA12] - fM[GA15] * fM[GA25];
205  fP[GM13] = fP[GM31] = fM[GA13] - fM[GA15] * fM[GA35];
206  fP[GM14] = fP[GM41] = fM[GA14] - fM[GA15] * fM[GA45];
207 
208  fP[GM22] = fM[GA22] - fM[GA25] * fM[GA25];
209  fP[GM23] = fP[GM32] = fM[GA23] - fM[GA25] * fM[GA35];
210  fP[GM24] = fP[GM42] = fM[GA24] - fM[GA25] * fM[GA45];
211 
212  fP[GM33] = fM[GA33] - fM[GA35] * fM[GA35];
213  fP[GM34] = fP[GM43] = fM[GA34] - fM[GA35] * fM[GA45];
214 
215  fP[GM44] = fM[GA44] - fM[GA45] * fM[GA45];
216 
217 
218  for (int i = 0; i < 25; i++) fQ[i]=0.;
219  fQ[GM00] = 4. * fM[GA50];
220  fQ[GM01] = fQ[GM10] = 2. * fM[GA51];
221  fQ[GM03] = fQ[GM30] = 2. * fM[GA53];
222  fQ[GM11] = fM[GA50]+fM[GA52];
223  fQ[GM12] = fQ[GM21] = 2. * fM[GA51];
224  fQ[GM13] = fQ[GM31] = fM[GA54];
225  fQ[GM14] = fQ[GM41] = fM[GA53];
226  fQ[GM22] = 4. * fM[GA52];
227  fQ[GM24] = fQ[GM42] = 2. * fM[GA54];
228  fQ[GM33] = fQ[GM44] = 1.;
229 }
230 
232  CbmRichRingLight *ring)
233 {
234  double Pxx = fAlgPar[0];
235  double Pxy = fAlgPar[1];
236  double Pyy = fAlgPar[2];
237  double Px = fAlgPar[3];
238  double Py = fAlgPar[4];
239  double P = fAlgPar[5];
240  CalcChi2(Pxx, Pxy, Pyy, Px, Py, P, ring);
241  ring->SetABCDEF(Pxx, Pxy, Pyy, Px, Py, P);
242 
243  double alpha;
244  double QQx, QQy, Qxx, Qyy, Qx, Qy, Q;
245  double cosa, sina, cca, ssa, sin2a;
246  double xc, yc;
247  if (fabs(Pxx - Pyy) > 0.1e-10) {
248  alpha = atan(Pxy / (Pxx - Pyy));
249  alpha = alpha / 2.0;
250  } else
251  alpha = 1.57079633;
252 
253  cosa = cos(alpha);
254  sina = sin(alpha);
255  cca = cosa * cosa;
256  ssa = sina * sina;
257  sin2a = sin(2. * alpha);
258  Pxy = Pxy * sin2a / 2.;
259  Qx = Px * cosa + Py * sina;
260  Qy = -Px * sina + Py * cosa;
261  QQx = Qx * Qx;
262  QQy = Qy * Qy;
263  Qxx = 1. / (Pxx * cca + Pxy + Pyy * ssa);
264  Qyy = 1. / (Pxx * ssa - Pxy + Pyy * cca);
265  Q = -P + Qxx * QQx / 4. + Qyy * QQy / 4.;
266  xc = Qx * Qxx;
267  yc = Qy * Qyy;
268 
269  double axisA = TMath::Sqrt(Q * Qxx);
270  double axisB = TMath::Sqrt(Q * Qyy);
271  double centerX = -xc * cosa / 2. + yc * sina / 2.;
272  double centerY = -xc * sina / 2. - yc * cosa / 2.;
273 
274  ring->SetXYABP(centerX, centerY, axisA, axisB, alpha);
275  ring->SetRadius( (axisA + axisB) / 2.);
276 
277  if (ring->GetAaxis() < ring->GetBaxis()) {
278  double tmp = ring->GetAaxis();
279  ring->SetAaxis(ring->GetBaxis());
280  ring->SetBaxis(tmp);
281 
282  tmp = ring->GetPhi();
283  if (ring->GetPhi() <= 0)
284  ring->SetPhi(ring->GetPhi() + 1.57079633);
285  else
286  ring->SetPhi(ring->GetPhi() - 1.57079633);
287  }
288 }
289 
291 {
292  // Find all NECESSARY 2x2 dets: (30 of them)
293  const double det2_23_01 = fP[GM20] * fP[GM31] - fP[GM21] * fP[GM30];
294  const double det2_23_02 = fP[GM20] * fP[GM32] - fP[GM22] * fP[GM30];
295  const double det2_23_03 = fP[GM20] * fP[GM33] - fP[GM23] * fP[GM30];
296  const double det2_23_04 = fP[GM20] * fP[GM34] - fP[GM24] * fP[GM30];
297  const double det2_23_12 = fP[GM21] * fP[GM32] - fP[GM22] * fP[GM31];
298  const double det2_23_13 = fP[GM21] * fP[GM33] - fP[GM23] * fP[GM31];
299  const double det2_23_14 = fP[GM21] * fP[GM34] - fP[GM24] * fP[GM31];
300  const double det2_23_23 = fP[GM22] * fP[GM33] - fP[GM23] * fP[GM32];
301  const double det2_23_24 = fP[GM22] * fP[GM34] - fP[GM24] * fP[GM32];
302  const double det2_23_34 = fP[GM23] * fP[GM34] - fP[GM24] * fP[GM33];
303  const double det2_24_01 = fP[GM20] * fP[GM41] - fP[GM21] * fP[GM40];
304  const double det2_24_02 = fP[GM20] * fP[GM42] - fP[GM22] * fP[GM40];
305  const double det2_24_03 = fP[GM20] * fP[GM43] - fP[GM23] * fP[GM40];
306  const double det2_24_04 = fP[GM20] * fP[GM44] - fP[GM24] * fP[GM40];
307  const double det2_24_12 = fP[GM21] * fP[GM42] - fP[GM22] * fP[GM41];
308  const double det2_24_13 = fP[GM21] * fP[GM43] - fP[GM23] * fP[GM41];
309  const double det2_24_14 = fP[GM21] * fP[GM44] - fP[GM24] * fP[GM41];
310  const double det2_24_23 = fP[GM22] * fP[GM43] - fP[GM23] * fP[GM42];
311  const double det2_24_24 = fP[GM22] * fP[GM44] - fP[GM24] * fP[GM42];
312  const double det2_24_34 = fP[GM23] * fP[GM44] - fP[GM24] * fP[GM43];
313  const double det2_34_01 = fP[GM30] * fP[GM41] - fP[GM31] * fP[GM40];
314  const double det2_34_02 = fP[GM30] * fP[GM42] - fP[GM32] * fP[GM40];
315  const double det2_34_03 = fP[GM30] * fP[GM43] - fP[GM33] * fP[GM40];
316  const double det2_34_04 = fP[GM30] * fP[GM44] - fP[GM34] * fP[GM40];
317  const double det2_34_12 = fP[GM31] * fP[GM42] - fP[GM32] * fP[GM41];
318  const double det2_34_13 = fP[GM31] * fP[GM43] - fP[GM33] * fP[GM41];
319  const double det2_34_14 = fP[GM31] * fP[GM44] - fP[GM34] * fP[GM41];
320  const double det2_34_23 = fP[GM32] * fP[GM43] - fP[GM33] * fP[GM42];
321  const double det2_34_24 = fP[GM32] * fP[GM44] - fP[GM34] * fP[GM42];
322  const double det2_34_34 = fP[GM33] * fP[GM44] - fP[GM34] * fP[GM43];
323 
324  // Find all NECESSARY 3x3 dets: (40 of them)
325  const double det3_123_012 = fP[GM10] * det2_23_12 - fP[GM11] * det2_23_02
326  + fP[GM12] * det2_23_01;
327  const double det3_123_013 = fP[GM10] * det2_23_13 - fP[GM11] * det2_23_03
328  + fP[GM13] * det2_23_01;
329  const double det3_123_014 = fP[GM10] * det2_23_14 - fP[GM11] * det2_23_04
330  + fP[GM14] * det2_23_01;
331  const double det3_123_023 = fP[GM10] * det2_23_23 - fP[GM12] * det2_23_03
332  + fP[GM13] * det2_23_02;
333  const double det3_123_024 = fP[GM10] * det2_23_24 - fP[GM12] * det2_23_04
334  + fP[GM14] * det2_23_02;
335  const double det3_123_034 = fP[GM10] * det2_23_34 - fP[GM13] * det2_23_04
336  + fP[GM14] * det2_23_03;
337  const double det3_123_123 = fP[GM11] * det2_23_23 - fP[GM12] * det2_23_13
338  + fP[GM13] * det2_23_12;
339  const double det3_123_124 = fP[GM11] * det2_23_24 - fP[GM12] * det2_23_14
340  + fP[GM14] * det2_23_12;
341  const double det3_123_134 = fP[GM11] * det2_23_34 - fP[GM13] * det2_23_14
342  + fP[GM14] * det2_23_13;
343  const double det3_123_234 = fP[GM12] * det2_23_34 - fP[GM13] * det2_23_24
344  + fP[GM14] * det2_23_23;
345  const double det3_124_012 = fP[GM10] * det2_24_12 - fP[GM11] * det2_24_02
346  + fP[GM12] * det2_24_01;
347  const double det3_124_013 = fP[GM10] * det2_24_13 - fP[GM11] * det2_24_03
348  + fP[GM13] * det2_24_01;
349  const double det3_124_014 = fP[GM10] * det2_24_14 - fP[GM11] * det2_24_04
350  + fP[GM14] * det2_24_01;
351  const double det3_124_023 = fP[GM10] * det2_24_23 - fP[GM12] * det2_24_03
352  + fP[GM13] * det2_24_02;
353  const double det3_124_024 = fP[GM10] * det2_24_24 - fP[GM12] * det2_24_04
354  + fP[GM14] * det2_24_02;
355  const double det3_124_034 = fP[GM10] * det2_24_34 - fP[GM13] * det2_24_04
356  + fP[GM14] * det2_24_03;
357  const double det3_124_123 = fP[GM11] * det2_24_23 - fP[GM12] * det2_24_13
358  + fP[GM13] * det2_24_12;
359  const double det3_124_124 = fP[GM11] * det2_24_24 - fP[GM12] * det2_24_14
360  + fP[GM14] * det2_24_12;
361  const double det3_124_134 = fP[GM11] * det2_24_34 - fP[GM13] * det2_24_14
362  + fP[GM14] * det2_24_13;
363  const double det3_124_234 = fP[GM12] * det2_24_34 - fP[GM13] * det2_24_24
364  + fP[GM14] * det2_24_23;
365  const double det3_134_012 = fP[GM10] * det2_34_12 - fP[GM11] * det2_34_02
366  + fP[GM12] * det2_34_01;
367  const double det3_134_013 = fP[GM10] * det2_34_13 - fP[GM11] * det2_34_03
368  + fP[GM13] * det2_34_01;
369  const double det3_134_014 = fP[GM10] * det2_34_14 - fP[GM11] * det2_34_04
370  + fP[GM14] * det2_34_01;
371  const double det3_134_023 = fP[GM10] * det2_34_23 - fP[GM12] * det2_34_03
372  + fP[GM13] * det2_34_02;
373  const double det3_134_024 = fP[GM10] * det2_34_24 - fP[GM12] * det2_34_04
374  + fP[GM14] * det2_34_02;
375  const double det3_134_034 = fP[GM10] * det2_34_34 - fP[GM13] * det2_34_04
376  + fP[GM14] * det2_34_03;
377  const double det3_134_123 = fP[GM11] * det2_34_23 - fP[GM12] * det2_34_13
378  + fP[GM13] * det2_34_12;
379  const double det3_134_124 = fP[GM11] * det2_34_24 - fP[GM12] * det2_34_14
380  + fP[GM14] * det2_34_12;
381  const double det3_134_134 = fP[GM11] * det2_34_34 - fP[GM13] * det2_34_14
382  + fP[GM14] * det2_34_13;
383  const double det3_134_234 = fP[GM12] * det2_34_34 - fP[GM13] * det2_34_24
384  + fP[GM14] * det2_34_23;
385  const double det3_234_012 = fP[GM20] * det2_34_12 - fP[GM21] * det2_34_02
386  + fP[GM22] * det2_34_01;
387  const double det3_234_013 = fP[GM20] * det2_34_13 - fP[GM21] * det2_34_03
388  + fP[GM23] * det2_34_01;
389  const double det3_234_014 = fP[GM20] * det2_34_14 - fP[GM21] * det2_34_04
390  + fP[GM24] * det2_34_01;
391  const double det3_234_023 = fP[GM20] * det2_34_23 - fP[GM22] * det2_34_03
392  + fP[GM23] * det2_34_02;
393  const double det3_234_024 = fP[GM20] * det2_34_24 - fP[GM22] * det2_34_04
394  + fP[GM24] * det2_34_02;
395  const double det3_234_034 = fP[GM20] * det2_34_34 - fP[GM23] * det2_34_04
396  + fP[GM24] * det2_34_03;
397  const double det3_234_123 = fP[GM21] * det2_34_23 - fP[GM22] * det2_34_13
398  + fP[GM23] * det2_34_12;
399  const double det3_234_124 = fP[GM21] * det2_34_24 - fP[GM22] * det2_34_14
400  + fP[GM24] * det2_34_12;
401  const double det3_234_134 = fP[GM21] * det2_34_34 - fP[GM23] * det2_34_14
402  + fP[GM24] * det2_34_13;
403  const double det3_234_234 = fP[GM22] * det2_34_34 - fP[GM23] * det2_34_24
404  + fP[GM24] * det2_34_23;
405 
406  // Find all NECESSARY 4x4 dets: (25 of them)
407  const double det4_0123_0123 = fP[GM00] * det3_123_123 - fP[GM01]
408  * det3_123_023 + fP[GM02] * det3_123_013 - fP[GM03] * det3_123_012;
409  const double det4_0123_0124 = fP[GM00] * det3_123_124 - fP[GM01]
410  * det3_123_024 + fP[GM02] * det3_123_014 - fP[GM04] * det3_123_012;
411  const double det4_0123_0134 = fP[GM00] * det3_123_134 - fP[GM01]
412  * det3_123_034 + fP[GM03] * det3_123_014 - fP[GM04] * det3_123_013;
413  const double det4_0123_0234 = fP[GM00] * det3_123_234 - fP[GM02]
414  * det3_123_034 + fP[GM03] * det3_123_024 - fP[GM04] * det3_123_023;
415  const double det4_0123_1234 = fP[GM01] * det3_123_234 - fP[GM02]
416  * det3_123_134 + fP[GM03] * det3_123_124 - fP[GM04] * det3_123_123;
417  const double det4_0124_0123 = fP[GM00] * det3_124_123 - fP[GM01]
418  * det3_124_023 + fP[GM02] * det3_124_013 - fP[GM03] * det3_124_012;
419  const double det4_0124_0124 = fP[GM00] * det3_124_124 - fP[GM01]
420  * det3_124_024 + fP[GM02] * det3_124_014 - fP[GM04] * det3_124_012;
421  const double det4_0124_0134 = fP[GM00] * det3_124_134 - fP[GM01]
422  * det3_124_034 + fP[GM03] * det3_124_014 - fP[GM04] * det3_124_013;
423  const double det4_0124_0234 = fP[GM00] * det3_124_234 - fP[GM02]
424  * det3_124_034 + fP[GM03] * det3_124_024 - fP[GM04] * det3_124_023;
425  const double det4_0124_1234 = fP[GM01] * det3_124_234 - fP[GM02]
426  * det3_124_134 + fP[GM03] * det3_124_124 - fP[GM04] * det3_124_123;
427  const double det4_0134_0123 = fP[GM00] * det3_134_123 - fP[GM01]
428  * det3_134_023 + fP[GM02] * det3_134_013 - fP[GM03] * det3_134_012;
429  const double det4_0134_0124 = fP[GM00] * det3_134_124 - fP[GM01]
430  * det3_134_024 + fP[GM02] * det3_134_014 - fP[GM04] * det3_134_012;
431  const double det4_0134_0134 = fP[GM00] * det3_134_134 - fP[GM01]
432  * det3_134_034 + fP[GM03] * det3_134_014 - fP[GM04] * det3_134_013;
433  const double det4_0134_0234 = fP[GM00] * det3_134_234 - fP[GM02]
434  * det3_134_034 + fP[GM03] * det3_134_024 - fP[GM04] * det3_134_023;
435  const double det4_0134_1234 = fP[GM01] * det3_134_234 - fP[GM02]
436  * det3_134_134 + fP[GM03] * det3_134_124 - fP[GM04] * det3_134_123;
437  const double det4_0234_0123 = fP[GM00] * det3_234_123 - fP[GM01]
438  * det3_234_023 + fP[GM02] * det3_234_013 - fP[GM03] * det3_234_012;
439  const double det4_0234_0124 = fP[GM00] * det3_234_124 - fP[GM01]
440  * det3_234_024 + fP[GM02] * det3_234_014 - fP[GM04] * det3_234_012;
441  const double det4_0234_0134 = fP[GM00] * det3_234_134 - fP[GM01]
442  * det3_234_034 + fP[GM03] * det3_234_014 - fP[GM04] * det3_234_013;
443  const double det4_0234_0234 = fP[GM00] * det3_234_234 - fP[GM02]
444  * det3_234_034 + fP[GM03] * det3_234_024 - fP[GM04] * det3_234_023;
445  const double det4_0234_1234 = fP[GM01] * det3_234_234 - fP[GM02]
446  * det3_234_134 + fP[GM03] * det3_234_124 - fP[GM04] * det3_234_123;
447  const double det4_1234_0123 = fP[GM10] * det3_234_123 - fP[GM11]
448  * det3_234_023 + fP[GM12] * det3_234_013 - fP[GM13] * det3_234_012;
449  const double det4_1234_0124 = fP[GM10] * det3_234_124 - fP[GM11]
450  * det3_234_024 + fP[GM12] * det3_234_014 - fP[GM14] * det3_234_012;
451  const double det4_1234_0134 = fP[GM10] * det3_234_134 - fP[GM11]
452  * det3_234_034 + fP[GM13] * det3_234_014 - fP[GM14] * det3_234_013;
453  const double det4_1234_0234 = fP[GM10] * det3_234_234 - fP[GM12]
454  * det3_234_034 + fP[GM13] * det3_234_024 - fP[GM14] * det3_234_023;
455  const double det4_1234_1234 = fP[GM11] * det3_234_234 - fP[GM12]
456  * det3_234_134 + fP[GM13] * det3_234_124 - fP[GM14] * det3_234_123;
457 
458  // Find the 5x5 det:
459  const double det = fP[GM00] * det4_1234_1234 - fP[GM01] * det4_1234_0234
460  + fP[GM02] * det4_1234_0134 - fP[GM03] * det4_1234_0124 + fP[GM04]
461  * det4_1234_0123;
462 // if (determ)
463 // *determ = det;
464 //
465 // if (det == 0) {
466 // Error("Inv5x5", "matrix is singular");
467 // return kFALSE;
468 // }
469  const double oneOverDet = 1.0 / det;
470  const double mn1OverDet = -oneOverDet;
471 
472  fP[GM00] = det4_1234_1234 * oneOverDet;
473  fP[GM01] = det4_0234_1234 * mn1OverDet;
474  fP[GM02] = det4_0134_1234 * oneOverDet;
475  fP[GM03] = det4_0124_1234 * mn1OverDet;
476  fP[GM04] = det4_0123_1234 * oneOverDet;
477 
478  fP[GM10] = det4_1234_0234 * mn1OverDet;
479  fP[GM11] = det4_0234_0234 * oneOverDet;
480  fP[GM12] = det4_0134_0234 * mn1OverDet;
481  fP[GM13] = det4_0124_0234 * oneOverDet;
482  fP[GM14] = det4_0123_0234 * mn1OverDet;
483 
484  fP[GM20] = det4_1234_0134 * oneOverDet;
485  fP[GM21] = det4_0234_0134 * mn1OverDet;
486  fP[GM22] = det4_0134_0134 * oneOverDet;
487  fP[GM23] = det4_0124_0134 * mn1OverDet;
488  fP[GM24] = det4_0123_0134 * oneOverDet;
489 
490  fP[GM30] = det4_1234_0124 * mn1OverDet;
491  fP[GM31] = det4_0234_0124 * oneOverDet;
492  fP[GM32] = det4_0134_0124 * mn1OverDet;
493  fP[GM33] = det4_0124_0124 * oneOverDet;
494  fP[GM34] = det4_0123_0124 * mn1OverDet;
495 
496  fP[GM40] = det4_1234_0123 * oneOverDet;
497  fP[GM41] = det4_0234_0123 * mn1OverDet;
498  fP[GM42] = det4_0134_0123 * oneOverDet;
499  fP[GM43] = det4_0124_0123 * mn1OverDet;
500  fP[GM44] = det4_0123_0123 * oneOverDet;
501 }
502 
504  const double * const ap,
505  int na,
506  int ncolsa,
507  const double * const bp,
508  int nb,
509  int ncolsb,
510  double *cp)
511 {
512 // Elementary routine to calculate matrix multiplication A*B
513 
514  const double *arp0 = ap; // Pointer to A[i,0];
515  while (arp0 < ap+na) {
516  for (const double *bcp = bp; bcp < bp+ncolsb; ) { // Pointer to the j-th column of B, Start bcp = B[0,0]
517  const double *arp = arp0; // Pointer to the i-th row of A, reset to A[i,0]
518  double cij = 0;
519  while (bcp < bp+nb) { // Scan the i-th row of A and
520  cij += *arp++ * *bcp; // the j-th col of B
521  bcp += ncolsb;
522  }
523  *cp++ = cij;
524  bcp -= nb-1; // Set bcp to the (j+1)-th col
525  }
526  arp0 += ncolsa; // Set ap to the (i+1)-th row
527  }
528 }
529 
530 #define ROTATE(a,i,j,k,l) g=a[i][j];h=a[k][l];a[i][j]=g-s*(h+g*tau);a[k][l]=h+s*(g-h*tau)
531 #define MAXSWEEP 50
533  double a[5][5],
534  double d[5],
535  double v[5][5])
536 {
537  double tresh, theta, tau, t, sm, s, h, g, c;
538 
539  double b[5], z[5];
540  unsigned int ip, iq, i,j;
541  for (ip = 0; ip < 5; ip++) {
542  for (iq = 0; iq < 5; iq++)
543  v[ip][iq] = 0.;
544  v[ip][ip] = 1.;
545  }
546 
547  for (ip = 0; ip < 5; ip++) {
548  b[ip] = a[ip][ip];
549  z[ip] = 0.;
550  }
551 
552  int nrot = 0;
553 
554  for (i = 1; i <= MAXSWEEP; i++) {
555 
556  for (sm = 0., ip = 0; ip < 5; ip++)
557  for (iq = ip + 1; iq < 5; iq++)
558  sm += fabs(a[ip][iq]);
559  if (sm == 0.) {
560  return;
561  }
562 
563  tresh = (i < 4 ? 0.2 * sm / (5 * 5) : 0.);
564 
565  for (ip = 0; ip < 4; ip++)
566  for (iq = ip + 1; iq < 5; iq++) {
567 
568  g = 100. * fabs(a[ip][iq]);
569  if (i > 4 && (float) fabs(d[ip] + g) == (float) fabs(d[ip])
570  && (float) fabs(d[iq] + g) == (float) fabs(d[iq]))
571  a[ip][iq] = 0.;
572 
573  else if (fabs(a[ip][iq]) > tresh) {
574  h = d[ip] - d[iq];
575 
576  if ((float) (fabs(h) + g) == (float) fabs(h))
577  t = a[ip][iq] / h;
578  else {
579  theta = 0.5 * h / a[ip][iq];
580  t = 1. / (fabs(theta) + sqrt(1. + theta * theta));
581  if (theta < 0.)
582  t = -t;
583  }
584  c = 1. / sqrt(1 + t * t);
585  s = t * c;
586  tau = s / (1. + c);
587  h = t * a[ip][iq];
588  z[ip] -= h;
589  z[iq] += h;
590  d[ip] -= h;
591  d[iq] += h;
592  a[ip][iq] = 0.;
593  for (j = 0; j < ip; j++) {
594  ROTATE(a,j,ip,j,iq);
595  }
596  for (j = ip + 1; j < iq; j++) {
597  ROTATE(a,ip,j,iq,j);
598  }
599  for (j = iq + 1; j < 5; j++) {
600  ROTATE(a,ip,j,j,iq);
601  }
602  for (j = 0; j < 5; j++) {
603  ROTATE(v,j,ip,j,iq);
604  }
605  ++nrot;
606  }
607  }
608  for (ip = 0; ip < 5; ip++) {
609  b[ip] += z[ip];
610  d[ip] = b[ip];
611  z[ip] = 0.;
612  }
613  }//i rot
614 }
615 
617  double d[5],
618  double v[5][5])
619 {
620  double p;
621  int i,k,j;
622  for (i = 0; i < 5; i++) {
623  p = d[k = i];
624  for (j = i + 1; j < 5; j++)
625  if (d[j] >= p)
626  p = d[k = j];
627  if (k != i) {
628  d[k] = d[i];
629  d[i] = p;
630  for (j = 0; j < 5; j++) {
631  p = v[j][i];
632  v[j][i] = v[j][k];
633  v[j][k] = p;
634  }
635  }
636  }
637 }