Merge branch 'master' into TPCdev
[u/mrichter/AliRoot.git] / STEER / STEER / AliTrackResidualsFast.cxx
CommitLineData
46ae650f 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
3010c308 16/* $Id$ */
17
46ae650f 18//-----------------------------------------------------------------
19// Implementation of the derived class for track residuals
20// based on linear chi2 minimization (in approximation of
21// small alignment angles and translations)
22//
23//-----------------------------------------------------------------
24
3010c308 25#include <TMath.h>
46ae650f 26#include <TGeoMatrix.h>
27
cc345ce3 28#include "AliLog.h"
46ae650f 29#include "AliAlignObj.h"
30#include "AliTrackPointArray.h"
31#include "AliTrackResidualsFast.h"
32
4c84f475 33#include <TMatrixDSym.h>
34#include <TMatrixDSymEigen.h>
35
46ae650f 36ClassImp(AliTrackResidualsFast)
37
38//______________________________________________________________________________
75e3794b 39AliTrackResidualsFast::AliTrackResidualsFast():
40 AliTrackResiduals(),
41 fSumR(0)
46ae650f 42{
43 // Default constructor
cc345ce3 44 for (Int_t i = 0; i < 27; i++) fSum[i] = 0;
46ae650f 45}
46
47//______________________________________________________________________________
48AliTrackResidualsFast::AliTrackResidualsFast(Int_t ntracks):
75e3794b 49 AliTrackResiduals(ntracks),
50 fSumR(0)
46ae650f 51{
52 // Constructor
cc345ce3 53 for (Int_t i = 0; i < 27; i++) fSum[i] = 0;
46ae650f 54}
55
56//______________________________________________________________________________
57AliTrackResidualsFast::AliTrackResidualsFast(const AliTrackResidualsFast &res):
75e3794b 58 AliTrackResiduals(res),
59 fSumR(res.fSumR)
46ae650f 60{
61 // Copy constructor
c6130b57 62 memcpy(fSum,res.fSum,27*sizeof(Double_t));
46ae650f 63}
64
65//______________________________________________________________________________
66AliTrackResidualsFast &AliTrackResidualsFast::operator= (const AliTrackResidualsFast& res)
67{
c6130b57 68 //
46ae650f 69 // Assignment operator
c6130b57 70 //
71 if(this != &res) {
72 AliTrackResiduals::operator=(res);
73 memcpy(fSum,res.fSum,27*sizeof(Double_t));
74 fSumR = res.fSumR;
75 }
46ae650f 76
77 return *this;
78}
79
80//______________________________________________________________________________
81Bool_t AliTrackResidualsFast::Minimize()
82{
83 // Implementation of fast linear Chi2
84 // based minimization of track residuals sum
cc101660 85
86 // if(fBFixed[0]||fBFixed[1]||fBFixed[2]||fBFixed[3]||fBFixed[4]||fBFixed[5])
87 // AliError("Cannot yet fix parameters in this minimizer");
29317b68 88
89
cc345ce3 90 for (Int_t i = 0; i < 27; i++) fSum[i] = 0;
46ae650f 91 fSumR = 0;
92
93 AliTrackPoint p1,p2;
94
95 for (Int_t itrack = 0; itrack < fLast; itrack++) {
96 if (!fVolArray[itrack] || !fTrackArray[itrack]) continue;
97 for (Int_t ipoint = 0; ipoint < fVolArray[itrack]->GetNPoints(); ipoint++) {
98 fVolArray[itrack]->GetPoint(p1,ipoint);
99 fTrackArray[itrack]->GetPoint(p2,ipoint);
100 AddPoints(p1,p2);
101 }
102 }
103
104 return Update();
105
106 // debug info
107// Float_t chi2 = 0;
108// for (Int_t itrack = 0; itrack < fLast; itrack++) {
109// if (!fVolArray[itrack] || !fTrackArray[itrack]) continue;
110// for (Int_t ipoint = 0; ipoint < fVolArray[itrack]->GetNPoints(); ipoint++) {
111// fVolArray[itrack]->GetPoint(p1,ipoint);
112// fAlignObj->Transform(p1);
113// fTrackArray[itrack]->GetPoint(p2,ipoint);
114// Float_t residual = p2.GetResidual(p1,kFALSE);
115// chi2 += residual;
116// }
117// }
118// printf("Final chi2 = %f\n",chi2);
119}
120
121//______________________________________________________________________________
122void AliTrackResidualsFast::AddPoints(AliTrackPoint &p, AliTrackPoint &pprime)
123{
124 // Update the sums used for
125 // the linear chi2 minimization
126 Float_t xyz[3],xyzp[3];
cc345ce3 127 Float_t cov[6],covp[6];
128 p.GetXYZ(xyz,cov); pprime.GetXYZ(xyzp,covp);
129 TMatrixDSym mcov(3);
130 mcov(0,0) = cov[0]; mcov(0,1) = cov[1]; mcov(0,2) = cov[2];
131 mcov(1,0) = cov[1]; mcov(1,1) = cov[3]; mcov(1,2) = cov[4];
132 mcov(2,0) = cov[2]; mcov(2,1) = cov[4]; mcov(2,2) = cov[5];
133 TMatrixDSym mcovp(3);
134 mcovp(0,0) = covp[0]; mcovp(0,1) = covp[1]; mcovp(0,2) = covp[2];
135 mcovp(1,0) = covp[1]; mcovp(1,1) = covp[3]; mcovp(1,2) = covp[4];
136 mcovp(2,0) = covp[2]; mcovp(2,1) = covp[4]; mcovp(2,2) = covp[5];
137 TMatrixDSym msum = mcov + mcovp;
4c84f475 138
139
cc345ce3 140 msum.Invert();
4c84f475 141
142
cc345ce3 143 if (!msum.IsValid()) return;
144
145 TMatrixD sums(3,1);
146 sums(0,0) = (xyzp[0]-xyz[0]);
147 sums(1,0) = (xyzp[1]-xyz[1]);
148 sums(2,0) = (xyzp[2]-xyz[2]);
149 TMatrixD sumst = sums.T(); sums.T();
150
151 TMatrixD mf(3,6);
152 mf(0,0) = 1; mf(1,0) = 0; mf(2,0) = 0;
153 mf(0,1) = 0; mf(1,1) = 1; mf(2,1) = 0;
154 mf(0,2) = 0; mf(1,2) = 0; mf(2,2) = 1;
4c84f475 155 mf(0,3) = 0; mf(1,3) = -xyz[2]; mf(2,3) = xyz[1];
cc345ce3 156 mf(0,4) = xyz[2]; mf(1,4) = 0; mf(2,4) =-xyz[0];
157 mf(0,5) =-xyz[1]; mf(1,5) = xyz[0]; mf(2,5) = 0;
cc101660 158
159 for(Int_t j=0;j<6;j++){
160 if(fBFixed[j]==kTRUE){
161 mf(0,j)=0.;mf(1,j)=0.;mf(2,j)=0.;
162 }
163 }
164
cc345ce3 165 TMatrixD mft = mf.T(); mf.T();
166 TMatrixD sums2 = mft * msum * sums;
167
168 TMatrixD smatrix = mft * msum * mf;
169
170 fSum[0] += smatrix(0,0);
171 fSum[1] += smatrix(0,1);
172 fSum[2] += smatrix(0,2);
173 fSum[3] += smatrix(0,3);
174 fSum[4] += smatrix(0,4);
175 fSum[5] += smatrix(0,5);
176 fSum[6] += smatrix(1,1);
177 fSum[7] += smatrix(1,2);
178 fSum[8] += smatrix(1,3);
179 fSum[9] += smatrix(1,4);
180 fSum[10]+= smatrix(1,5);
181 fSum[11]+= smatrix(2,2);
182 fSum[12]+= smatrix(2,3);
183 fSum[13]+= smatrix(2,4);
184 fSum[14]+= smatrix(2,5);
185 fSum[15]+= smatrix(3,3);
186 fSum[16]+= smatrix(3,4);
187 fSum[17]+= smatrix(3,5);
188 fSum[18]+= smatrix(4,4);
189 fSum[19]+= smatrix(4,5);
190 fSum[20]+= smatrix(5,5);
191 fSum[21] += sums2(0,0);
192 fSum[22] += sums2(1,0);
193 fSum[23] += sums2(2,0);
194 fSum[24] += sums2(3,0);
195 fSum[25] += sums2(4,0);
196 fSum[26] += sums2(5,0);
197
198 TMatrixD tmp = sumst * msum * sums;
199 fSumR += tmp(0,0);
46ae650f 200
201 fNdf += 3;
202}
203
204//______________________________________________________________________________
205Bool_t AliTrackResidualsFast::Update()
206{
207 // Find the alignment parameters
208 // by using the already accumulated
209 // sums
210 TMatrixDSym smatrix(6);
211 TMatrixD sums(1,6);
212
3bbfb291 213 smatrix(0,0) = fSum[0];
214 smatrix(0,1) = smatrix(1,0) = fSum[1];
215 smatrix(0,2) = smatrix(2,0) = fSum[2];
216 smatrix(0,3) = smatrix(3,0) = fSum[3];
217 smatrix(0,4) = smatrix(4,0) = fSum[4];
218 smatrix(0,5) = smatrix(5,0) = fSum[5];
219 smatrix(1,1) = fSum[6];
220 smatrix(1,2) = smatrix(2,1) = fSum[7];
221 smatrix(1,3) = smatrix(3,1) = fSum[8];
222 smatrix(1,4) = smatrix(4,1) = fSum[9];
223 smatrix(1,5) = smatrix(5,1) = fSum[10];
224 smatrix(2,2) = fSum[11];
225 smatrix(2,3) = smatrix(3,2) = fSum[12];
226 smatrix(2,4) = smatrix(4,2) = fSum[13];
227 smatrix(2,5) = smatrix(5,2) = fSum[14];
228 smatrix(3,3) = fSum[15];
229 smatrix(3,4) = smatrix(4,3) = fSum[16];
230 smatrix(3,5) = smatrix(5,3) = fSum[17];
231 smatrix(4,4) = fSum[18];
232 smatrix(4,5) = smatrix(5,4) = fSum[19];
cc345ce3 233 smatrix(5,5) = fSum[20];
234
235 sums(0,0) = fSum[21]; sums(0,1) = fSum[22]; sums(0,2) = fSum[23];
236 sums(0,3) = fSum[24]; sums(0,4) = fSum[25]; sums(0,5) = fSum[26];
46ae650f 237
cc101660 238
239 Int_t fixedparamat[6]={0,0,0,0,0,0};
240 const Int_t unfixedparam=GetNFreeParam();
961fb9b8 241 Int_t position[6]={0,0,0,0,0,0};
242 Int_t last=0;//position is of size 6 but only unfiexedparam indeces will be used
cc101660 243
244 if(fBFixed[0]==kTRUE){
245 fixedparamat[0]=1;
246 }
247 else {
248 position[0]=0;
249 last++;
250 }
251
252 for(Int_t j=1;j<6;j++){
253 if(fBFixed[j]==kTRUE){
254 fixedparamat[j]=fixedparamat[j-1]+1;
255 }
256 else {
257 fixedparamat[j]=fixedparamat[j-1];
258 position[last]=j;
259 last++;
260 }
261 }
46ae650f 262
cc101660 263 TMatrixDSym smatrixRedu(unfixedparam);
264 for(Int_t i=0;i<unfixedparam;i++){
265 for(Int_t j=0;j<unfixedparam;j++){
266 smatrixRedu(i,j)=smatrix(position[i],position[j]);
267 }
268 }
269
ffc90c05 270 // smatrixRedu.Print();
cc101660 271 smatrixRedu.Invert();
272
273 if (!smatrixRedu.IsValid()) {
4c84f475 274 printf("Minimization Failed! \n");
275 return kFALSE;
276 }
277
cc101660 278 TMatrixDSym smatrixUp(6);
279 for(Int_t i=0;i<6;i++){
280 for(Int_t j=0;j<6;j++){
281 if(fBFixed[i]==kTRUE||fBFixed[j]==kTRUE)smatrixUp(i,j)=0.;
282 else smatrixUp(i,j)=smatrixRedu(i-fixedparamat[i],j-fixedparamat[j]);
283 }
284 }
285
4c84f475 286 Double_t covmatrarray[21];
287
cc101660 288 for(Int_t i=0;i<6;i++){
4c84f475 289 for(Int_t j=0;j<=i;j++){
cc101660 290 if(fBFixed[i]==kFALSE&&fBFixed[j]==kFALSE){
291 if(TMath::Abs(smatrixUp(i,j)/TMath::Sqrt(TMath::Abs(smatrixUp(i,i)*smatrixUp(j,j))))>1.01)printf("Too large Correlation number!\n");
292 }
293 covmatrarray[i*(i+1)/2+j]=smatrixUp(i,j);
4c84f475 294 }
cc101660 295 }
4c84f475 296
cc101660 297 TMatrixD res = sums*smatrixUp;
46ae650f 298 fAlignObj->SetPars(res(0,0),res(0,1),res(0,2),
299 TMath::RadToDeg()*res(0,3),
300 TMath::RadToDeg()*res(0,4),
301 TMath::RadToDeg()*res(0,5));
4c84f475 302
303 fAlignObj->SetCorrMatrix(covmatrarray);
46ae650f 304 TMatrixD tmp = res*sums.T();
305 fChi2 = fSumR - tmp(0,0);
cc101660 306 fNdf -= unfixedparam;
4c84f475 307
46ae650f 308 return kTRUE;
309}
4c84f475 310
311
312