dc6a7c20645987d8d654c9bc4c2ad700d1befa9b
[u/mrichter/AliRoot.git] / ITS / AliITSTrackerV1.cxx
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  
16 /*
17 $Log$
18 Revision 1.21  2002/02/05 09:12:26  hristov
19 Small mods for gcc 3.02
20
21 Revision 1.20  2001/11/21 14:47:45  barbera
22 Some unuseful print-out commented out
23
24 Revision 1.19  2001/11/21 10:49:07  barbera
25 Bug correction suggested by Rene done
26
27 Revision 1.18  2001/11/20 15:46:17  barbera
28 Point coordinated are calculated in cylindrical reference frame once and for all at the beginning of tracking V1
29
30 Revision 1.10.2.1  2001/10/24 07:26:04  hristov
31 All the changes from the head are merged with the release
32
33 Revision 1.14  2001/10/24 07:19:57  hristov
34 Some pointer correctly initialised in one of the constructors
35
36 Revision 1.13  2001/10/21 19:17:12  hristov
37 Several pointers were set to zero in the default constructors to avoid memory management problems
38
39 Revision 1.12  2001/10/19 21:32:35  nilsen
40 Minor changes to remove compliation warning on gcc 2.92.2 compiler, and
41 cleanded up a little bit of code.
42
43 */
44 //     The purpose of this class is to permorm the ITS tracking. The 
45 // constructor has the task to inizialize some private members. The method 
46 // DoTracking is written to be called by a macro. It gets the event number,
47 // the minimum and maximum order number of TPC tracks that are to be tracked
48 // trough the ITS, and the file where the recpoints are registered. The 
49 // method Recursivetracking is a recursive function that performs the 
50 // tracking trough the ITS The method Intersection found the layer, ladder 
51 // and detector whre the intersection take place and caluclate the 
52 // cohordinates of this intersection. It returns an integer that is 0 if the 
53 // intersection has been found successfully. The two mwthods Kalmanfilter 
54 // and kalmanfiltervert operate the kalmanfilter without and with the vertex
55 // imposition respectively. The authors thank Mariana Bondila to have help 
56 // them to resolve some problems.  July-2000
57
58 #include <Riostream.h>
59 #include <Riostream.h>
60 #include <TMath.h>
61 #include <TBranch.h>
62 #include <TVector.h>
63 #include <TFile.h>
64 #include <TTree.h>
65 #include <TStopwatch.h>
66
67 #include "TParticle.h"
68 #include "AliRun.h"
69 #include "AliITS.h"
70 #include "AliITSsegmentationSSD.h"
71 #include "AliITSgeomSPD.h"
72 #include "AliITSgeomSDD.h"
73 #include "AliITSgeomSSD.h"
74 #include "AliITSgeom.h"
75 #include "AliITSRecPoint.h"
76 #include "stdlib.h"
77 #include "AliKalmanTrack.h" 
78 #include "AliMagF.h"
79 #include "AliITSTrackV1.h"
80 #include "AliITSIOTrack.h"
81 #include "AliITSRad.h"   
82 #include "../TPC/AliTPCtracker.h"
83 #include "AliITSTrackerV1.h"
84 #include "AliITSVertex.h"
85
86 ClassImp(AliITSTrackerV1)
87  //______________________________________________________________________
88 AliITSTrackerV1::AliITSTrackerV1() {
89   //Default constructor
90   fITS = 0;
91   fresult = 0;
92   fPtref=0.;
93   fChi2max=0.;
94   //fepsphi=0.;
95   //fepsz=0.;
96   frecPoints = 0;
97   fvettid = 0;
98   fflagvert=0;
99   frl = 0;
100   Int_t ia;
101   for(ia=0; ia<6; ia++) {
102   fNlad[ia]=0;
103   fNdet[ia]=0;
104   fAvrad[ia]=0.;
105   fDetx[ia]=0.;
106   fDetz[ia]=0.; 
107   } // end for ia  
108   fzmin = 0;
109   fzmax = 0;
110   fphimin = 0;
111   fphimax = 0;
112   fphidet = 0;
113   fNRecPoints=0;
114   fRecCylR=0;
115   fRecCylPhi=0;
116   fRecCylZ=0;
117   fFieldFactor=0;
118 }
119 //______________________________________________________________________
120 AliITSTrackerV1::AliITSTrackerV1(AliITS* IITTSS, Int_t evnumber, Bool_t flag) {
121     //Origin   A. Badala' and G.S. Pappalardo:  
122     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
123     // Class constructor. It does some initializations.
124
125   //PH Initialisation taken from the default constructor
126     fITS      = IITTSS;
127     fresult = 0;
128     fPtref    = 0.;
129     fChi2max  =0.; 
130     frecPoints = 0;              
131     fvettid = 0;
132     fflagvert = flag;    
133     frl = 0;
134     fzmin = 0;
135     fzmax = 0;
136     fphimin = 0;
137     fphimax = 0;
138     fphidet = 0;
139   
140     Int_t imax = 200,jmax = 450;
141     frl       = new AliITSRad(imax,jmax);
142
143     //////////  gets information on geometry /////////////////////////////
144          AliITSgeom *g1 = fITS->GetITSgeom();  
145     Int_t ll=1, dd=1;
146     TVector det(9);
147
148     Int_t ia;
149     for(ia=0; ia<6; ia++) {
150         fNlad[ia]=g1->GetNladders(ia+1);
151         fNdet[ia]=g1->GetNdetectors(ia+1);
152         //cout<<fNlad[i]<<" "<<fNdet[i]<<"\n"; 
153     } // end for ia
154
155     //cout<<" mean radius = ";
156     Int_t ib;
157     for(ib=0; ib<6; ib++) {  
158         g1->GetCenterThetaPhi(ib+1,ll,dd,det);
159         Double_t r1=TMath::Sqrt(det(0)*det(0)+det(1)*det(1));
160         g1->GetCenterThetaPhi(ib+1,ll,dd+1,det);
161         Double_t r2=TMath::Sqrt(det(0)*det(0)+det(1)*det(1));
162         fAvrad[ib]=(r1+r2)/2.;
163         //cout<<fAvrad[ib]<<" ";
164     } // end for ib
165     //cout<<"\n"; getchar();
166
167     fDetx[0] = ((AliITSgeomSPD*)(g1->GetShape(1, ll, dd)))->GetDx();
168     fDetz[0] = ((AliITSgeomSPD*)(g1->GetShape(1, ll, dd)))->GetDz();
169
170     fDetx[1] = ((AliITSgeomSPD*)(g1->GetShape(2, ll, dd)))->GetDx();
171     fDetz[1] = ((AliITSgeomSPD*)(g1->GetShape(2, ll, dd)))->GetDz();
172
173     fDetx[2] = ((AliITSgeomSDD*)(g1->GetShape(3, ll, dd)))->GetDx();
174     fDetz[2] = ((AliITSgeomSDD*)(g1->GetShape(3, ll, dd)))->GetDz();
175
176     fDetx[3] = ((AliITSgeomSDD*)(g1->GetShape(4, ll, dd)))->GetDx();
177     fDetz[3] = ((AliITSgeomSDD*)(g1->GetShape(4, ll, dd)))->GetDz();
178
179     fDetx[4] = ((AliITSgeomSSD*)(g1->GetShape(5, ll, dd)))->GetDx();
180     fDetz[4] = ((AliITSgeomSSD*)(g1->GetShape(5, ll, dd)))->GetDz();
181
182     fDetx[5] = ((AliITSgeomSSD*)(g1->GetShape(6, ll, dd)))->GetDx();
183     fDetz[5] = ((AliITSgeomSSD*)(g1->GetShape(6, ll, dd)))->GetDz();
184     //cout<<"    Detx     Detz\n";
185     //for(Int_t la=0; la<6; la++) cout<<"    "<<fDetx[la]<<"     "<<
186     //                                 fDetz[la]<<endl;
187     //getchar();
188          
189     // allocate memory and define matrices fzmin, fzmax, fphimin and fphimax //
190     Double_t epsz=1.2;
191     Double_t epszdrift=0.05;
192
193     fzmin = new Double_t*[6]; fzmax = new Double_t*[6];
194     Int_t im1, im2, im2max;
195     for(im1=0; im1<6; im1++) {
196         im2max=fNdet[im1];
197         fzmin[im1] = new Double_t[im2max]; fzmax[im1] = new Double_t[im2max];
198     } // end for im1
199
200     for(im1=0; im1<6; im1++) {
201         im2max=fNdet[im1];
202         for(im2=0; im2<im2max; im2++) {
203             g1->GetCenterThetaPhi(im1+1,1,im2+1,det);
204             if(im2!=0) fzmin[im1][im2]=det(2)-fDetz[im1];
205             else   
206                 fzmin[im1][im2]=det(2)-(fDetz[im1])*epsz;
207             if(im2!=(im2max-1)) fzmax[im1][im2]=det(2)+fDetz[im1];
208             else
209                 fzmax[im1][im2]=det(2)+fDetz[im1]*epsz;
210             if(im1==2 || im1==3) {
211                 fzmin[im1][im2]-=epszdrift;
212                 fzmax[im1][im2]+=epszdrift;
213             } // end if im1==2 || im1==3
214         } // end for im2
215     } // end for im1
216
217     fphimin = new Double_t*[6]; fphimax = new Double_t*[6];
218     for(im1=0;im1<6;im1++) {
219         im2max=fNlad[im1];
220         fphimin[im1] = new Double_t[im2max]; 
221         fphimax[im1] = new Double_t[im2max];
222     } // end for im1
223   
224     fphidet = new Double_t*[6];
225     for(im1=0; im1<6; im1++) {
226         im2max=fNlad[im1];
227         fphidet[im1] = new Double_t[im2max];
228     } // end for im1
229
230     //Float_t global[3],local[3];
231          Double_t global[3],local[3];
232     Double_t pigre=TMath::Pi();
233     Double_t xmin,ymin,xmax,ymax;
234
235     for(im1=0; im1<6; im1++) {
236         im2max=fNlad[im1];
237         for(im2=0; im2<im2max; im2++) {
238             Int_t idet=2;
239             g1->GetCenterThetaPhi(im1+1,im2+1,idet,det);
240             fphidet[im1][im2] = TMath::ATan2(Double_t(det(1)),
241                                              Double_t(det(0))); 
242             if(fphidet[im1][im2]<0.) fphidet[im1][im2]+=2.*pigre;  
243             local[1]=local[2]=0.;  
244             local[0]= -(fDetx[im1]);    
245             if(im1==0) local[0]= (fDetx[im1]); //to take into account 
246                                                // different reference system
247             g1->LtoG(im1+1,im2+1,idet,local,global);
248             xmax=global[0]; ymax=global[1];
249             local[0]= (fDetx[im1]);   
250             if(im1==0) local[0]= -(fDetx[im1]);//take into account different 
251                                                // reference system
252             g1->LtoG(im1+1,im2+1,idet,local,global);
253             xmin=global[0]; ymin=global[1];
254             fphimin[im1][im2]= TMath::ATan2(ymin,xmin);
255             if(fphimin[im1][im2]<0.) fphimin[im1][im2]+=2.*pigre; 
256             fphimax[im1][im2]= TMath::ATan2(ymax,xmax);
257             if(fphimax[im1][im2]<0.) fphimax[im1][im2]+=2.*pigre;
258         } // end for im2
259     } // end for im1
260 //////////////////////////////////////////////////////////////////////////////////////////////////////////
261 /////////////// allocate memory and define vector fNRecPoints and matrices fRecCylR, fRecCylPhi, fRecCylZ /////////////
262         gAlice->GetEvent(evnumber);
263   Int_t NumOfModules = g1->GetIndexMax();
264   //fRecCylR = new Float_t *[NumOfModules];
265   fRecCylR = new Double_t *[NumOfModules];
266   //fRecCylPhi = new Float_t *[NumOfModules];
267   fRecCylPhi = new Double_t *[NumOfModules]; 
268   //fRecCylZ = new Float_t *[NumOfModules];
269   fRecCylZ = new Double_t *[NumOfModules];
270   AliITSRecPoint *recp;
271   fNRecPoints = new Int_t[NumOfModules];
272    
273                  for(Int_t module=0; module<NumOfModules; module++) {                           
274                   fITS->ResetRecPoints();                    
275         gAlice->TreeR()->GetEvent(module);                
276                   frecPoints=fITS->RecPoints();
277                   Int_t nRecPoints=fNRecPoints[module]=frecPoints->GetEntries();
278                   /*
279                   fRecCylR[module] = new Float_t[nRecPoints];
280                   fRecCylPhi[module] = new Float_t[nRecPoints];
281                   fRecCylZ[module] = new Float_t[nRecPoints];
282                   */
283                   fRecCylR[module] = new Double_t[nRecPoints];
284                   fRecCylPhi[module] = new Double_t[nRecPoints];
285                   fRecCylZ[module] = new  Double_t[nRecPoints];           
286                   Int_t ind;
287                   for(ind=0; ind<fNRecPoints[module]; ind++) {    
288                     recp=(AliITSRecPoint*)frecPoints->UncheckedAt(ind);                                          
289                         // Float_t global[3], local[3];
290                          Double_t global[3], local[3];
291                local[0]=recp->GetX();
292                local[1]=0.;
293                local[2]= recp->GetZ();                                  
294                          g1->LtoG(module,local,global);
295                          /*
296                          Float_t r = TMath::Sqrt(global[0]*global[0]+global[1]*global[1]);                     // r hit
297                          Float_t phi = TMath::ATan2(global[1],global[0]); if(phi<0.) phi+=2.*TMath::Pi();      // phi hit                       
298           Float_t z = global[2];                                                               // z hit
299                          */
300                                                                                          
301                          Double_t r = TMath::Sqrt(global[0]*global[0]+global[1]*global[1]);                     // r hit
302                          Double_t phi = TMath::ATan2(global[1],global[0]); if(phi<0.) phi+=2.*TMath::Pi();      // phi hit                      
303           Double_t z = global[2];                                                                // z hit
304                                                                                                                                                                                                                                      
305                          fRecCylR[module][ind]=r;
306                          fRecCylPhi[module][ind]=phi;
307                          fRecCylZ[module][ind]=z;                        
308                   }             
309                 }        
310          //}  
311   //}
312 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
313          
314
315     ////////// gets magnetic field factor //////////////////////////////
316
317     AliMagF * fieldPointer = gAlice->Field();
318     fFieldFactor = (Double_t)fieldPointer->Factor();
319     //cout<< " field factor = "<<fFieldFactor<<"\n"; getchar();
320 }
321 //______________________________________________________________________
322 AliITSTrackerV1::AliITSTrackerV1(const AliITSTrackerV1 &cobj) {
323     // Origin  A. Badala' and G.S. Pappalardo:
324     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
325     // copy constructor
326          
327          *fITS = *cobj.fITS;
328     *fresult = *cobj.fresult;
329     fPtref = cobj.fPtref;
330     fChi2max = cobj.fChi2max;    
331     **fvettid = **cobj.fvettid;
332     fflagvert = cobj.fflagvert;
333     Int_t imax=200,jmax=450;
334     frl = new AliITSRad(imax,jmax);      
335     *frl = *cobj.frl;
336     fFieldFactor = cobj.fFieldFactor;
337     Int_t i,im1,im2,im2max;
338     for(i=0; i<6; i++) {
339         fNlad[i] = cobj.fNlad[i];
340         fNdet[i] = cobj.fNdet[i]; 
341         fAvrad[i] = cobj.fAvrad[i];
342         fDetx[i] = cobj.fDetx[i];
343         fDetz[i] = cobj.fDetz[i];
344     } // end or i
345     fzmin = new Double_t*[6]; fzmax = new Double_t*[6];
346     for(im1=0; im1<6; im1++) {
347         im2max=fNdet[im1];
348         fzmin[im1] = new Double_t[im2max];
349         fzmax[im1] = new Double_t[im2max];
350     } // end for im1
351     fphimin = new Double_t*[6]; fphimax = new Double_t*[6];
352     for(im1=0;im1<6;im1++) {
353         im2max=fNlad[im1];
354         fphimin[im1] = new Double_t[im2max];
355         fphimax[im1] = new Double_t[im2max];
356     } // end for im1
357   
358     fphidet = new Double_t*[6];
359     for(im1=0; im1<6; im1++) {
360         im2max=fNlad[im1];
361         fphidet[im1] = new Double_t[im2max];
362     } // end for im1
363     for(im1=0; im1<6; im1++) {
364         im2max=fNdet[im1];
365         for(im2=0; im2<im2max; im2++) {
366             fzmin[im1][im2]=cobj.fzmin[im1][im2];
367             fzmax[im1][im2]=cobj.fzmax[im1][im2];
368         } // end for im2
369     } // end for im1
370     for(im1=0; im1<6; im1++) {
371         im2max=fNlad[im1];
372         for(im2=0; im2<im2max; im2++) {
373             fphimin[im1][im2]=cobj.fphimin[im1][im2];
374             fphimax[im1][im2]=cobj.fphimax[im1][im2];
375             fphidet[im1][im2]=cobj.fphidet[im1][im2];  
376         } // end for im2
377     } // end for im2
378
379
380         AliITSgeom *g1 = fITS->GetITSgeom();  
381    Int_t NumOfModules = g1->GetIndexMax();
382         /*
383   fRecCylR = new Float_t *[NumOfModules];
384   fRecCylPhi = new Float_t *[NumOfModules]; 
385   fRecCylZ = new Float_t *[NumOfModules];
386   */
387   fRecCylR = new Double_t *[NumOfModules];
388   fRecCylPhi = new Double_t *[NumOfModules]; 
389   fRecCylZ = new Double_t *[NumOfModules];  
390   fNRecPoints = new Int_t[NumOfModules];        
391                 for(Int_t module=0; module<NumOfModules; module++) {            
392                   Int_t nRecPoints=fNRecPoints[module]=cobj.fNRecPoints[module];
393                   /*
394                   fRecCylR[module] = new Float_t[nRecPoints];
395                   fRecCylPhi[module] = new Float_t[nRecPoints];
396                   fRecCylZ[module] = new Float_t[nRecPoints];
397                   */
398                   fRecCylR[module] = new Double_t[nRecPoints];
399                   fRecCylPhi[module] = new Double_t[nRecPoints];
400                   fRecCylZ[module] = new Double_t[nRecPoints];            
401                   Int_t ind;    
402                   for(ind=0; ind<nRecPoints; ind++) {       
403                          fRecCylR[module][ind]=cobj.fRecCylR[module][ind];
404                          fRecCylPhi[module][ind]=cobj.fRecCylPhi[module][ind];
405                          fRecCylZ[module][ind]=cobj.fRecCylZ[module][ind];                       
406                   }             
407                 }        
408  
409 }
410 void AliITSTrackerV1::DelMatrix(Int_t NumOfModules) { 
411   for(Int_t mod=0; mod<NumOfModules; mod++) {
412     delete fRecCylR[mod];
413          delete fRecCylPhi[mod];
414          delete fRecCylZ[mod];
415   }
416     delete fRecCylR;
417          delete fRecCylPhi;
418          delete fRecCylZ;
419 }
420 //______________________________________________________________________
421 AliITSTrackerV1::~AliITSTrackerV1(){
422     // Origin  A. Badala' and G.S. Pappalardo:
423     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it  
424     // class destructor  
425   delete frl;
426   delete fNRecPoints;
427   for(Int_t i=0; i<6; i++) {
428     delete fzmin[i];
429          delete fzmax[i];
430          delete fphimin[i];
431          delete fphimax[i];
432          delete fphidet[i];
433   }
434
435   delete fzmin;
436   delete fzmax;
437   delete fphimin;
438   delete fphimax;
439   delete fphidet;
440          
441 }
442 //______________________________________________________________________
443 AliITSTrackerV1 &AliITSTrackerV1::operator=(AliITSTrackerV1 obj) {
444     // Origin  A. Badala' and G.S. Pappalardo:
445     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it  
446     // assignement operator
447
448          *fITS = *obj.fITS;
449     *fresult = *obj.fresult;
450     fPtref = obj.fPtref;
451     fChi2max = obj.fChi2max;      
452     **fvettid = **obj.fvettid;
453     fflagvert = obj.fflagvert;
454     Int_t imax=200,jmax=450;
455     frl = new AliITSRad(imax,jmax);      
456     *frl = *obj.frl;
457     fFieldFactor = obj.fFieldFactor;
458     Int_t i;
459     for(i=0; i<6; i++) {
460         fNlad[i] = obj.fNlad[i];
461         fNdet[i] = obj.fNdet[i]; 
462         fAvrad[i] = obj.fAvrad[i];
463         fDetx[i] = obj.fDetx[i];
464         fDetz[i] = obj.fDetz[i];
465     } // end for i
466     fzmin = new Double_t*[6];
467     fzmax = new Double_t*[6];
468     Int_t im1, im2, im2max;
469     for(im1=0; im1<6; im1++) {
470         im2max=fNdet[im1];
471         fzmin[im1] = new Double_t[im2max]; fzmax[im1] = new Double_t[im2max];
472     } // end for im1
473     fphimin = new Double_t*[6]; fphimax = new Double_t*[6];
474     for(im1=0;im1<6;im1++) {
475         im2max=fNlad[im1];
476         fphimin[im1] = new Double_t[im2max];
477         fphimax[im1] = new Double_t[im2max];
478     } // end for im1
479
480     fphidet = new Double_t*[6];
481     for(im1=0; im1<6; im1++) {
482         im2max=fNlad[im1];
483         fphidet[im1] = new Double_t[im2max];
484     } // end for im1
485     for(im1=0; im1<6; im1++) {
486         im2max=fNdet[im1];
487         for(im2=0; im2<im2max; im2++) {
488             fzmin[im1][im2]=obj.fzmin[im1][im2];
489             fzmax[im1][im2]=obj.fzmax[im1][im2];
490         } // end for im2
491     } // end for im1
492     for(im1=0; im1<6; im1++) {
493         im2max=fNlad[im1];
494         for(im2=0; im2<im2max; im2++) {
495             fphimin[im1][im2]=obj.fphimin[im1][im2];
496             fphimax[im1][im2]=obj.fphimax[im1][im2];
497             fphidet[im1][im2]=obj.fphidet[im1][im2];  
498         } // end for im2
499     } // end for im1
500
501         AliITSgeom *g1 = fITS->GetITSgeom();  
502    Int_t NumOfModules = g1->GetIndexMax();
503         /*
504   fRecCylR = new Float_t *[NumOfModules];
505   fRecCylPhi = new Float_t *[NumOfModules]; 
506   fRecCylZ = new Float_t *[NumOfModules];
507   */
508   fRecCylR = new Double_t *[NumOfModules];
509   fRecCylPhi = new Double_t *[NumOfModules]; 
510   fRecCylZ = new Double_t *[NumOfModules];  
511   fNRecPoints = new Int_t[NumOfModules];  
512           for(Int_t module=0; module<NumOfModules; module++) {            
513                   Int_t nRecPoints=fNRecPoints[module]=obj.fNRecPoints[module];
514                   /*
515                   fRecCylR[module] = new Float_t[nRecPoints];
516                   fRecCylPhi[module] = new Float_t[nRecPoints];
517                   fRecCylZ[module] = new Float_t[nRecPoints];
518                   */
519                   fRecCylR[module] = new Double_t[nRecPoints];
520                   fRecCylPhi[module] = new Double_t[nRecPoints];
521                   fRecCylZ[module] = new Double_t[nRecPoints];            
522                   Int_t ind;
523                   for(ind=0; ind<nRecPoints; ind++) {     
524                          fRecCylR[module][ind]=obj.fRecCylR[module][ind];
525                          fRecCylPhi[module][ind]=obj.fRecCylPhi[module][ind];
526                          fRecCylZ[module][ind]=obj.fRecCylZ[module][ind];                        
527                   }             
528                 }        
529          
530          
531     return *this;
532 }
533 //______________________________________________________________________
534 void AliITSTrackerV1::DoTracking(Int_t evNumber,Int_t minTr,Int_t maxTr,
535                                  TFile *file, Bool_t realmass) {
536     // Origin   A. Badala' and G.S. Pappalardo:
537     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
538     // The method needs the event number, the minimum and maximum order
539     // number of TPC tracks that 
540     // are to be tracked trough the ITS, and the file where the recpoints
541     // are registered.
542     // The method can be called by a macro. It preforms the tracking for
543     // all good TPC tracks
544
545     printf("begin DoTracking - file %p\n",file);
546
547     gAlice->GetEvent(evNumber);  //modificato per gestire hbt
548  
549     AliKalmanTrack *kkprov;
550     kkprov->SetConvConst(100/0.299792458/0.2/fFieldFactor);
551
552     TFile *cf=TFile::Open("AliTPCclusters.root");  
553     AliTPCParam *digp= (AliTPCParam*)cf->Get("75x40_100x60");
554     if (!digp) { cerr<<"TPC parameters have not been found !\n"; getchar();}
555
556     cf->cd();
557     AliTPCtracker *tracker = new AliTPCtracker(digp,evNumber);  
558
559     // Load clusters
560     tracker->LoadInnerSectors();
561     tracker->LoadOuterSectors();
562
563     // Load tracks
564     TFile *tf=TFile::Open("AliTPCtracksSorted.root");  
565     if (!tf->IsOpen()) {
566         cerr<<"Can't open AliTPCtracksSorted.root !\n";
567         return ;
568     } // end if
569     TObjArray tracks(200000);
570     char tname[100];                 
571     sprintf(tname,"TreeT_TPC_%d",evNumber);
572
573     TTree *tracktree=(TTree*)tf->Get(tname);   
574     if (!tracktree) {cerr<<"Can't get a tree with TPC tracks !\n";}   
575     TBranch *tbranch=tracktree->GetBranch("tracks");
576     Int_t nentr=(Int_t)tracktree->GetEntries();
577     Int_t kk;
578
579     AliTPCtrack *ioTrackTPC=0;    
580     for (kk=0; kk<nentr; kk++) {
581         ioTrackTPC=new AliTPCtrack; 
582         tbranch->SetAddress(&ioTrackTPC);
583         tracktree->GetEvent(kk);    
584         tracker->CookLabel(ioTrackTPC,0.1);       
585         tracks.AddLast(ioTrackTPC);         
586     } // end for kk
587     delete tracker;      
588     tf->Close();
589
590     Int_t nt = tracks.GetEntriesFast();
591     cerr<<"Number of found tracks "<<nt<<endl;
592
593     TVector vec(5);
594     TTree *tr=gAlice->TreeR();
595     Int_t nent=(Int_t)tr->GetEntries();  
596     frecPoints = fITS->RecPoints();
597
598     Int_t numbpoints;
599     Int_t totalpoints=0;
600     Int_t *np = new Int_t[nent];
601     fvettid = new Int_t* [nent];
602     Int_t mod;
603
604     for (mod=0; mod<nent; mod++) {
605         fvettid[mod]=0;
606         fITS->ResetRecPoints();  
607         gAlice->TreeR()->GetEvent(mod); 
608         numbpoints = frecPoints->GetEntries();
609         totalpoints+=numbpoints;
610         np[mod] = numbpoints;
611         //cout<<" mod = "<<mod<<"   numbpoints = "<<numbpoints<<"\n";getchar();
612         fvettid[mod] = new Int_t[numbpoints];
613         Int_t ii;
614         for (ii=0;ii<numbpoints; ii++) *(fvettid[mod]+ii)=0;
615     } // end for mod
616
617     AliTPCtrack *track=0;
618
619     if(minTr < 0) {minTr = 0; maxTr = nt-1;}   
620
621     TVector vgeant(3);
622
623     TTree tracktree1("TreeT","Tree with ITS tracks");
624     AliITSIOTrack *ioTrack=0;
625     tracktree1.Branch("ITStracks","AliITSIOTrack",&ioTrack,32000,0);
626   
627    TDatabasePDG * db = new TDatabasePDG;   
628   
629     Int_t j;       
630     for (j=minTr; j<=maxTr; j++) {     
631         track=(AliTPCtrack*)tracks.UncheckedAt(j);
632         if (!track) continue;
633         
634         ///   mass definition ////////////////////////
635     Double_t mass=0.13956995;
636          Int_t pcode=211;  // a pion by default
637          if(realmass) {
638     Int_t TPClabel=TMath::Abs( track->GetLabel() );   
639          TParticle *p = (TParticle*)gAlice->Particle(TPClabel);
640           pcode=p->GetPdgCode();
641          // Int_t mothercode=p->GetFirstMother();
642          //if(mothercode>0 ) numofsecondaries++; else numofprimaries++;
643          }
644          //if(!pcode) pcode=211;         
645          if(TMath::Abs(pcode)<20443) mass=db->GetParticle(pcode)->Mass();       
646         
647         
648         ///////////////////////////////////////////////
649   /*
650         //////   propagation to the end of TPC //////////////
651         Double_t xk=77.415;
652         track->PropagateTo(xk, 28.94, 1.204e-3,mass);    //Ne    
653         xk -=0.01;
654         track->PropagateTo(xk, 44.77, 1.71,mass);        //Tedlar
655         xk -=0.04;
656         track->PropagateTo(xk, 44.86, 1.45,mass);        //kevlar
657         xk -=2.0;
658         track->PropagateTo(xk, 41.28, 0.029,mass);       //Nomex         
659         xk-=16;
660         track->PropagateTo(xk,36.2,1.98e-3,mass); //C02
661         xk -=0.01;
662         track->PropagateTo(xk, 24.01, 2.7,mass);         //Al    
663         xk -=0.01;
664         track->PropagateTo(xk, 44.77, 1.71,mass);        //Tedlar
665         xk -=0.04;
666         track->PropagateTo(xk, 44.86, 1.45,mass);        //kevlar
667         xk -=0.5;
668         track->PropagateTo(xk, 41.28, 0.029,mass);       //Nomex
669     ////////////////////////////////////////////////////////////////////
670    */
671          //   new propagation to the end of TPC
672     Double_t xk=80.;
673    // track->PropagateTo(xk,0.,0.); //Ne if it's still there   //attenzione funziona solo se modifica in TPC
674         // Double_t xk=77.415;   
675          track->PropagateTo(xk, 28.94, 1.204e-3);
676     xk-=0.005;
677     track->PropagateTo(xk, 44.77,1.71); //Tedlar         
678     xk-=0.02;
679     track->PropagateTo(xk, 44.86, 1.45);   //Kevlar
680     xk-=2.0;
681     track->PropagateTo(xk, 41.28, 0.029);//Nomex
682     xk-=0.02;
683     track->PropagateTo(xk, 44.86, 1.45);   //Kevlar
684     xk-=0.005;
685     track->PropagateTo(xk, 44.77, 1.71); //Tedlar
686
687     xk=61.;
688    // track->PropagateTo(xk,0.,0.); //C02
689          track->PropagateTo(xk,36.2,1.98e-3); //C02        //attenzione funziona solo se modifica in TPC
690
691     xk -=0.005;
692     track->PropagateTo(xk, 24.01, 2.7);    //Al    
693     xk -=0.005;
694     track->PropagateTo(xk, 44.77, 1.71);  //Tedlar
695     xk -=0.02;
696     track->PropagateTo(xk, 44.86, 1.45);    //Kevlar
697     xk -=0.5;
698     track->PropagateTo(xk, 41.28, 0.029);  //Nomex    
699     xk -=0.02;
700     track->PropagateTo(xk, 44.86, 1.45);    //Kevlar
701     xk -=0.005;
702     track->PropagateTo(xk, 44.77, 1.71);  //Tedlar
703     xk -=0.005;
704     track->PropagateTo(xk, 24.01, 2.7);    //Al 
705         ////////////////////////////////////////////////////////////////////////////////////////////////////////        
706         AliITSTrackV1 trackITS(*track);     
707         trackITS.PutMass(mass);   //new to add mass to track
708         if(fresult){ delete fresult; fresult=0;}         
709         fresult = new AliITSTrackV1(trackITS);   
710
711         AliITSTrackV1 primaryTrack(trackITS);
712         vgeant=(*fresult).GetVertex();
713
714         // Definition of dv and zv for vertex constraint        
715         Double_t sigmaDv=0.0050;  Double_t sigmaZv=0.010;       
716         //Double_t sigmaDv=0.0015;  Double_t sigmaZv=0.0015;
717         Double_t uniform= gRandom->Uniform();
718         Double_t signdv;
719         if(uniform<=0.5) signdv=-1.;
720         else
721             signdv=1.;
722
723         Double_t vr=TMath::Sqrt(vgeant(0)*vgeant(0)+ vgeant(1)*vgeant(1));
724         Double_t dv=gRandom->Gaus(signdv*vr,(Float_t)sigmaDv); 
725         Double_t zv=gRandom->Gaus(vgeant(2),(Float_t)sigmaZv);          
726         //cout<<" Dv e Zv = "<<dv<<" "<<zv<<"\n";
727         trackITS.SetDv(dv);
728         trackITS.SetZv(zv);
729         trackITS.SetsigmaDv(sigmaDv);
730         trackITS.SetsigmaZv(sigmaZv); 
731         (*fresult).SetDv(dv);
732         (*fresult).SetZv(zv);
733         (*fresult).SetsigmaDv(sigmaDv);
734         (*fresult).SetsigmaZv(sigmaZv);
735         primaryTrack.SetDv(dv);
736         primaryTrack.SetZv(zv);
737         primaryTrack.SetsigmaDv(sigmaDv);
738         primaryTrack.SetsigmaZv(sigmaZv);
739         primaryTrack.PrimaryTrack(frl);
740         TVector  d2=primaryTrack.Getd2();
741         TVector  tgl2=primaryTrack.Gettgl2();
742         TVector  dtgl=primaryTrack.Getdtgl();
743         trackITS.Setd2(d2); trackITS.Settgl2(tgl2);
744         trackITS.Setdtgl(dtgl); 
745         (*fresult).Setd2(d2); (*fresult).Settgl2(tgl2);
746         (*fresult).Setdtgl(dtgl);
747         /*
748           trackITS.SetVertex(vertex); trackITS.SetErrorVertex(ervertex);
749           (*result).SetVertex(vertex);   (*result).SetErrorVertex(ervertex);   
750         */
751         TList *list= new TList();
752
753         list->AddLast(&trackITS);
754   
755         fPtref=TMath::Abs( (trackITS).GetPt() );
756         if(fPtref>1.0) fChi2max=40.;         
757         if(fPtref<=1.0) fChi2max=20.;
758         if(fPtref<0.4 ) fChi2max=100.;
759         if(fPtref<0.2 ) fChi2max=40.;             
760         // if(fPtref<0.4 ) fChi2max=30.;                                 
761         // if(fPtref<0.2 ) fChi2max=20.;
762         //if(fPtref<0.2 ) fChi2max=10.;
763         //if(fPtref<0.1 ) fChi2max=5.;
764         //cout << "\n Pt = " << fPtref <<"\n";  //stampa
765         RecursiveTracking(list);   
766         list->Delete();
767         delete list;
768
769         Int_t itot=-1;
770         TVector vecTotLabRef(18);
771         Int_t lay, k;
772         for(lay=5; lay>=0; lay--) {
773             TVector vecLabRef(3); 
774             vecLabRef=(*fresult).GetLabTrack(lay);
775             Float_t clustZ=(*fresult).GetZclusterTrack( lay);   
776             for(k=0; k<3; k++){  
777                 Int_t lpp=(Int_t)vecLabRef(k);
778                 if(lpp>=0) {
779                     TParticle *p=(TParticle*) gAlice->Particle(lpp);
780                     Int_t pcode=p->GetPdgCode();
781                     if(pcode==11) vecLabRef(k)=p->GetFirstMother();
782                 } // end if
783                 itot++; vecTotLabRef(itot)=vecLabRef(k);
784                 if(vecLabRef(k)==0. && clustZ == -1.) vecTotLabRef(itot) =-3.;
785             } // end for k
786         } // end for lay
787         Long_t labref;
788         Int_t freq;  
789         (*fresult).Search(vecTotLabRef, labref, freq);
790
791         //if(freq < 6) labref=-labref;        // cinque - sei
792         if(freq < 5) labref=-labref;        // cinque - sei     
793         (*fresult).SetLabel(labref);
794
795         // cout<<" progressive track number = "<<j<<"\r";
796         // cout<<j<<"\r";
797         Int_t numOfCluster=(*fresult).GetNumClust();  
798         //cout<<" progressive track number = "<<j<<"\n";    // stampa
799         Long_t labITS=(*fresult).GetLabel();
800         //cout << " ITS track label = " << labITS << "\n";      // stampa           
801         Int_t lab=track->GetLabel();                
802         //cout << " TPC track label = " << lab <<"\n";      // stampa
803         //propagation to vertex
804
805         Double_t rbeam=3.;
806     if((*fresult).DoNotCross(rbeam)) continue;  //no intersection with beampipe 
807         (*fresult).Propagation(rbeam);
808         Double_t c00,c10,c11,c20,c21,c22,c30,c31,c32,c33,c40,c41,c42,c43,c44;
809         (*fresult).GetCElements(c00,
810                                 c10,c11,
811                                 c20,c21,c22,
812                                 c30,c31,c32,c33,
813                                 c40,c41,c42,c43,c44);
814                  
815         Double_t pt=TMath::Abs((*fresult).GetPt());
816         Double_t dr=(*fresult).GetD();
817         Double_t z=(*fresult).GetZ();
818         Double_t tgl=(*fresult).GetTgl();
819         Double_t c=(*fresult).GetC();
820         Double_t cy=c/2.;
821         Double_t dz=z-(tgl/cy)*TMath::ASin((*fresult).Arga(rbeam));
822         dz-=vgeant(2);
823         // cout<<" dr e dz alla fine = "<<dr<<" "<<dz<<"\n"; getchar();
824         Double_t phi=(*fresult).Getphi();
825         Double_t phivertex = phi - TMath::ASin((*fresult).ArgA(rbeam));
826         Double_t duepi=2.*TMath::Pi();   
827         if(phivertex>duepi) phivertex-=duepi;
828         if(phivertex<0.) phivertex+=duepi;
829         /////////////////////////////////////////////////////////////
830         Int_t idmodule,idpoint;
831         if(numOfCluster >=5)  {            // cinque - sei
832             //if(numOfCluster ==6)  {            // cinque - sei
833             AliITSIOTrack outTrack;
834             ioTrack=&outTrack;
835             ioTrack->SetStatePhi(phi);
836             ioTrack->SetStateZ(z);
837             ioTrack->SetStateD(dr);
838             ioTrack->SetStateTgl(tgl);
839             ioTrack->SetStateC(c);
840             Double_t radius=(*fresult).Getrtrack();
841             ioTrack->SetRadius(radius);
842             Int_t charge;
843             if(c>0.) charge=-1;  else charge=1;
844             ioTrack->SetCharge(charge);
845             ioTrack->SetCovMatrix(c00,
846                                   c10,c11,
847                                   c20,c21,c22,
848                                   c30,c31,c32,c33,
849                                   c40,c41,c42,c43,c44);
850             Double_t px=pt*TMath::Cos(phivertex);
851             Double_t py=pt*TMath::Sin(phivertex);
852             Double_t pz=pt*tgl;
853             Double_t xtrack=dr*TMath::Sin(phivertex);
854             Double_t ytrack=dr*TMath::Cos(phivertex);
855             Double_t ztrack=dz+vgeant(2);
856             ioTrack->SetPx(px);
857             ioTrack->SetPy(py);
858             ioTrack->SetPz(pz);
859             ioTrack->SetX(xtrack);
860             ioTrack->SetY(ytrack);
861             ioTrack->SetZ(ztrack);
862             ioTrack->SetLabel(labITS);
863             ioTrack->SetTPCLabel(lab);
864                  ioTrack->SetDz(dz);
865             Int_t il;           
866             for(il=0;il<6; il++){
867                 ioTrack->SetIdPoint(il,(*fresult).GetIdPoint(il));
868                 ioTrack->SetIdModule(il,(*fresult).GetIdModule(il));
869             } // end for il
870             tracktree1.Fill();
871             for (il=0;il<6;il++) {
872                 idpoint=(*fresult).GetIdPoint(il);
873                 idmodule=(*fresult).GetIdModule(il);
874                 if(idmodule>0.) *(fvettid[idmodule]+idpoint)=1;
875                                                                
876                 ioTrack->SetIdPoint(il,idpoint);
877                 ioTrack->SetIdModule(il,idmodule);
878             } // end for il         
879         } // end if on numOfCluster
880         //gObjectTable->Print();    // stampa memoria     
881     }  //  end for (int j=minTr; j<=maxTr; j++)
882     delete db;           
883     static Bool_t first=kTRUE;
884     static TFile *tfile;
885     if(first) {
886         tfile=new TFile("itstracks.root","RECREATE");
887         //cout<<"I have opened itstracks.root file "<<endl;
888     } // end if     
889     first=kFALSE;
890     tfile->cd();
891     tfile->ls();
892     char hname[30];
893     sprintf(hname,"TreeT%d",evNumber);
894     tracktree1.Write(hname);
895   
896     TTree *fAli=gAlice->TreeK();
897     TFile *fileAli=0;
898     if (fAli) fileAli =fAli->GetCurrentFile();
899     fileAli->cd();
900     ////////////////////////////////////////////////////////////////////
901
902     printf("delete vectors\n");
903     if(np) delete [] np;
904     if(fvettid) delete [] fvettid;
905     if(fresult) {delete fresult; fresult=0;}
906 }
907 //______________________________________________________________________
908 void AliITSTrackerV1::RecursiveTracking(TList *trackITSlist) {
909     //   This function perform the recursive tracking in ITS detectors
910     // reference is a pointer to the final best track
911     // Origin  A. Badala' and G.S. Pappalardo:
912     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
913     // The authors thank Mariana Bondila to have help them to resolve some
914     // problems.  July-2000
915
916     //Rlayer[0]=4.; Rlayer[1]=7.;  Rlayer[2]=14.9;
917     // Rlayer[3]=23.8;  Rlayer[4]=39.1;  Rlayer[5]=43.6; //vecchio
918
919     ////////////////////// 
920     Float_t sigmaphil[6], sigmazl[6];
921     sigmaphil[0]=1.44e-6/(fAvrad[0]*fAvrad[0]);
922     sigmaphil[1]=1.44e-6/(fAvrad[1]*fAvrad[1]);
923     sigmaphil[2]=1.444e-5/(fAvrad[2]*fAvrad[2]);
924     sigmaphil[3]=1.444e-5/(fAvrad[3]*fAvrad[3]);
925     sigmaphil[4]=4e-6/(fAvrad[4]*fAvrad[4]);
926     sigmaphil[5]=4e-6/(fAvrad[5]*fAvrad[5]);
927     sigmazl[0]=1e-2;
928     sigmazl[1]=1e-2;
929     sigmazl[2]=7.84e-4;
930     sigmazl[3]=7.84e-4;
931     sigmazl[4]=0.6889;
932     sigmazl[5]=0.6889;  
933     ///////////////////////////////////////////////////////////
934     Int_t index; 
935     AliITSgeom *g1 = fITS->GetITSgeom();
936     AliITSRecPoint *recp;        
937     for(index =0; index<trackITSlist->GetSize(); index++) {
938         AliITSTrackV1 *trackITS = (AliITSTrackV1 *) trackITSlist->At(index);
939         if((*trackITS).GetLayer()==7) fresult->SetChi2(10.223e140);
940         // cout <<" Layer inizio = "<<(*trackITS).GetLayer()<<"\n";
941         //  cout<<"fvtrack =" <<"\n";
942         //  cout << (*trackITS)(0) << " "<<(*trackITS)(1)<<" "
943         //       <<(*trackITS)(2)<<" "<<(*trackITS)(3)<<" "
944         //       <<(*trackITS)(4)<<"\n";
945         //  cout<< " rtrack = "<<(*trackITS).Getrtrack()<<"\n";
946         //  cout<< " Pt = "<<(*trackITS).GetPt()<<"\n";
947         //  getchar();    
948         Double_t chi2Now, chi2Ref;
949         Float_t numClustRef = fresult->GetNumClust();            
950         if((*trackITS).GetLayer()==1 ) {
951             chi2Now = trackITS->GetChi2();
952             Float_t numClustNow = trackITS->GetNumClust();
953             if(trackITS->GetNumClust()) 
954                 chi2Now /= (Double_t)trackITS->GetNumClust();
955             chi2Ref = fresult->GetChi2();
956             if(fresult->GetNumClust()) 
957                 chi2Ref /= (Double_t)fresult->GetNumClust();
958             //cout<<" chi2Now and chi2Ref = "<<chi2Now<<" "<<chi2Ref<<"\n";
959             if( numClustNow > numClustRef ) {*fresult = *trackITS;} 
960             if((numClustNow == numClustRef )&& 
961                (chi2Now < chi2Ref))  {
962                 *fresult = *trackITS;
963             } // end if
964             continue;   
965         } // end if
966
967         if(trackITS->Getfnoclust()>=2)  continue;      
968         Float_t numClustNow = trackITS->GetNumClust();
969         if(numClustNow) { 
970             chi2Now = trackITS->GetChi2();
971
972             if(numClustNow<numClustRef && chi2Now>fresult->GetChi2()) continue;
973             //cout<<" chi2Now =  "<<chi2Now<<"\n";   
974               
975             chi2Now/=numClustNow;
976             if(fPtref > 1.0 && chi2Now > 30.) continue; 
977             if((fPtref >= 0.6 && fPtref<=1.0) && chi2Now > 40.) continue;
978             // if((fPtref <= 0.6 && fPtref>0.2)&& chi2Now > 40.) continue;
979             // if(fPtref <= 0.2 && chi2Now > 8.) continue;
980             if((fPtref <= 0.6 && fPtref>0.2)&& chi2Now > 30.) continue;
981             if(fPtref <= 0.2 && chi2Now > 7.) continue;     
982             /////////////////////////////
983         } // end if
984
985         Int_t layerInit = (*trackITS).GetLayer();
986         Int_t layernew = layerInit - 2;// -1 for new layer, -1 for matrix index
987         TList listoftrack;
988         Int_t ladp, ladm, detp,detm,ladinters,detinters;        
989         Int_t layerfin=layerInit-1;
990         // cout<<"Prima di intersection \n";
991         Int_t  outinters=Intersection(*trackITS,layerfin,ladinters,detinters);
992         // cout<<" outinters = "<<outinters<<"\n";
993         //  cout<<" Layer ladder detector intersection ="
994         //      <<layerfin<<" "<<ladinters<<" "<<detinters<<"\n";
995         //  cout << " phiinters zinters = "<<(*trackITS)(0) 
996         //       << " "<<(*trackITS)(1)<<"\n"; getchar();
997         if(outinters==-1) continue;
998         Int_t flaghit=0;
999         if(outinters==0){
1000             TVector toucLad(9), toucDet(9);      
1001             Int_t lycur=layerfin;
1002             ladp=ladinters+1;
1003             ladm=ladinters-1;
1004             if(ladm <= 0) ladm=fNlad[layerfin-1];    
1005             if(ladp > fNlad[layerfin-1]) ladp=1;  
1006             detp=detinters+1;
1007             detm=detinters-1;
1008             Int_t idetot=1;
1009             /*
1010               toucLad(0)=ladinters; toucLad(1)=ladm; toucLad(2)=ladp;
1011               toucLad(3)=ladinters; toucLad(4)=ladm; toucLad(5)=ladp;
1012               toucLad(6)=ladinters; toucLad(7)=ladm; toucLad(8)=ladp;
1013               toucDet(0)=detinters; toucDet(1)=detinters; toucDet(2)=detinters;
1014               if(detm > 0 && detp <= fNdet[layerfin-1]) {     
1015                    idetot=9;
1016                    toucDet(3)=detm; toucDet(4)=detm; toucDet(5)=detm;      
1017                    toucDet(6)=detp; toucDet(7)=detp; toucDet(8)=detp;
1018               } // end if
1019               if(detm > 0 && detp > fNdet[layerfin-1]) {   
1020                    idetot=6;
1021                    toucDet(3)=detm; toucDet(4)=detm; toucDet(5)=detm;
1022              } // end if
1023              if(detm <= 0 && detp <= fNdet[layerfin-1]) {   
1024                   idetot=6;
1025                   toucDet(3)=detp; toucDet(4)=detp; toucDet(5)=detp;
1026              } // end if
1027             */
1028             Float_t epsphi=5.0, epsz=5.0;                  
1029             if(fPtref<0.2) {epsphi=3.; epsz=3.;}     
1030             // new definition of idetot e toucLad e toucDet to be
1031             // transformed in a method
1032             // these values could be modified
1033             Float_t pigre=TMath::Pi();
1034             Float_t rangephi=5., rangez=5.;
1035             if(layerfin==1 || layerfin ==2){
1036                 rangephi=40.*epsphi*TMath::Sqrt(sigmaphil[layerfin-1]+
1037                                                 (*trackITS).GetSigmaphi());
1038                 rangez = 40.*epsz*TMath::Sqrt(sigmazl[layerfin-1]+
1039                                               (*trackITS).GetSigmaZ());
1040             } // end if
1041             if(layerfin==3 || layerfin ==4){
1042                 //rangephi=30.*fepsphi*TMath::Sqrt(sigmaphil[layerfin-1]+
1043                 //                                 (*trackITS).GetSigmaphi());
1044                 //rangez = 40.*fepsz*TMath::Sqrt(sigmazl[layerfin-1]+
1045                 //                               (*trackITS).GetSigmaZ());
1046                 rangephi=40.*epsphi*TMath::Sqrt(sigmaphil[layerfin-1]+
1047                                                 (*trackITS).GetSigmaphi());
1048                 rangez = 50.*epsz*TMath::Sqrt(sigmazl[layerfin-1]+
1049                                               (*trackITS).GetSigmaZ());
1050             } // end if
1051             if(layerfin==5 || layerfin ==6){
1052                 rangephi=20.*epsphi*TMath::Sqrt(sigmaphil[layerfin-1]+
1053                                                 (*trackITS).GetSigmaphi());
1054                 rangez =5.*epsz*TMath::Sqrt(sigmazl[layerfin-1]+
1055                                             (*trackITS).GetSigmaZ());
1056             } // end if
1057             Float_t phinters, zinters;
1058             phinters=(*trackITS).Getphi();
1059             zinters=(*trackITS).GetZ();
1060             Float_t distz = 0.0;
1061             Float_t phicm, phicp, distphim, distphip;
1062             phicm=phinters;
1063                  if(phinters>fphimax[layerfin-1][ladm-1]) phicm=phinters-2*pigre;  //corretto il 20-11-2001
1064                  distphim=TMath::Abs(phicm-fphimax[layerfin-1][ladm-1]);  //corretto il 20-11-2001
1065             phicp=phinters;
1066                  //cout<<" fNlad[layerfin-1] e ladp = "<<fNlad[layerfin-1]<<" "<<ladp<<endl;
1067                  if(phinters>fphimin[layerfin-1][ladp-1]) phicp=phinters-2.*pigre;   //corretto il 20-11-2001
1068                  distphip=TMath::Abs(phicp-fphimin[layerfin-1][ladp-1]);      //corretto il 20-11-2001
1069             Int_t flagzmin=0;
1070             Int_t flagzmax=0;
1071             idetot=1;
1072             toucLad(0)=ladinters; toucDet(0)=detinters;
1073             if(detm>0) distz=TMath::Abs(zinters-fzmax[layerfin-1][detm-1]);
1074             if(detm>0 && rangez>=distz){
1075                 flagzmin=1; 
1076                 idetot++; toucLad(idetot-1)=ladinters; toucDet(idetot-1)=detm;
1077                 if(rangephi>=distphim){
1078                     idetot++; 
1079                     toucLad(idetot-1)=ladm;
1080                     toucDet(idetot-1)=detinters;
1081                     idetot++;
1082                     toucLad(idetot-1)=ladm;
1083                     toucDet(idetot-1)=detm;
1084                 } // end if
1085                 if(rangephi>=distphip){
1086                     idetot++;
1087                     toucLad(idetot-1)=ladp;
1088                     toucDet(idetot-1)=detinters;
1089                     idetot++;
1090                     toucLad(idetot-1)=ladp;
1091                     toucDet(idetot-1)=detm;
1092                 } // end if
1093             }  //end detm>0....          
1094             if(detp<=fNdet[layerfin-1]) 
1095                 distz=TMath::Abs(zinters-fzmin[layerfin-1][detp-1]);
1096             if(detp<=fNdet[layerfin-1] && rangez>=distz){
1097                 flagzmax=1;
1098                 idetot++; toucLad(idetot-1)=ladinters; toucDet(idetot-1)=detp;
1099                 if(rangephi>=distphim){
1100                     idetot++; toucLad(idetot-1)=ladm; toucDet(idetot-1)=detp;
1101                     if(flagzmin == 0) {
1102                         idetot++;
1103                         toucLad(idetot-1)=ladm;
1104                         toucDet(idetot-1)=detinters;
1105                     } // end if
1106                 } // end if
1107                 if(rangephi>=distphip){
1108                     idetot++;
1109                     toucLad(idetot-1)=ladp;
1110                     toucDet(idetot-1)=detp;
1111                     if(flagzmin == 0) {
1112                         idetot++;
1113                         toucLad(idetot-1)=ladp; 
1114                         toucDet(idetot-1)=detinters;
1115                     } // end if
1116                 } // end if
1117             }  //end detm<fNdet[.......
1118
1119             if(flagzmin == 0 && flagzmax==0){
1120                 if(rangephi>=distphim){
1121                     idetot++; 
1122                     toucLad(idetot-1)=ladm;
1123                     toucDet(idetot-1)=detinters;
1124                 } // end if     
1125                 if(rangephi>=distphip){
1126                     idetot++; 
1127                     toucLad(idetot-1)=ladp;
1128                     toucDet(idetot-1)=detinters;
1129                 } // end if       
1130             } // end if
1131             ////////////////////////////////////////////////////////////
1132             Int_t iriv;
1133             for (iriv=0; iriv<idetot; iriv++) {  //for on detectors
1134                 ///////////////////////////////////////////////////////
1135                 /*** Rec points sorted by module *****/
1136                 /**************************************/
1137                 Int_t indexmod;       
1138                 indexmod = g1->GetModuleIndex(lycur,(Int_t)toucLad(iriv),
1139                                               (Int_t)toucDet(iriv)); 
1140                 fITS->ResetRecPoints();   
1141                 gAlice->TreeR()->GetEvent(indexmod); 
1142                 Int_t npoints=frecPoints->GetEntries();
1143        
1144                 Int_t indnew;
1145                 for(indnew=0; indnew<npoints; indnew++){
1146                     if (*(fvettid[indexmod]+indnew)==0)
1147                         recp =(AliITSRecPoint*)frecPoints->UncheckedAt(indnew);
1148                     else
1149                         continue;
1150                     TVector cluster(3),vecclust(9);
1151                     //vecclust(6)=vecclust(7)=vecclust(8)=-1.;
1152                     Double_t sigma[2];
1153   // now vecclust is with cylindrical cohordinates
1154                vecclust(0)=(Float_t)fRecCylR[indexmod][indnew];     
1155                vecclust(1)=(Float_t)fRecCylPhi[indexmod][indnew];
1156                vecclust(2)=(Float_t)fRecCylZ[indexmod][indnew];                          
1157                     vecclust(3) = (Float_t)recp->fTracks[0]; 
1158                     vecclust(4) = (Float_t)indnew;                       
1159                     vecclust(5) = (Float_t)indexmod;    
1160                     vecclust(6) = (Float_t)recp->fTracks[0];
1161                     vecclust(7) = (Float_t)recp->fTracks[1];
1162                     vecclust(8) = (Float_t)recp->fTracks[2];
1163                     sigma[0] = (Double_t)  recp->GetSigmaX2();     
1164                     sigma[1] = (Double_t) recp->GetSigmaZ2();
1165                          
1166                          cluster(0)=fRecCylR[indexmod][indnew];
1167           cluster(1)=fRecCylPhi[indexmod][indnew];
1168                          cluster(2)=fRecCylZ[indexmod][indnew];
1169                          
1170                     // cout<<" layer = "<<play<<"\n";
1171                     // cout<<" cluster prima = "<<vecclust(0)<<" "
1172                     //     <<vecclust(1)<<" "
1173                     //     <<vecclust(2)<<"\n"; getchar();    
1174
1175                     Float_t sigmatotphi, sigmatotz;  
1176                     // Float_t epsphi=5.0, epsz=5.0;                 
1177                     //if(fPtref<0.2) {epsphi=3.; epsz=3.;}
1178                     Double_t rTrack=(*trackITS).Getrtrack();
1179                     Double_t sigmaphi=sigma[0]/(rTrack*rTrack);
1180                     sigmatotphi=epsphi*TMath::Sqrt(sigmaphi + 
1181                                                    (*trackITS).GetSigmaphi());
1182                     sigmatotz=epsz*TMath::Sqrt(sigma[1] + 
1183                                                (*trackITS).GetSigmaZ());
1184                     //cout<<"cluster e sigmatotphi e track = "<<cluster(0)
1185                     //    <<" "<<cluster(1)<<" "<<sigmatotphi<<" "
1186                     //    <<vecclust(3)<<"\n";
1187                     //if(vecclust(3)==481) getchar();
1188                     if(cluster(1)<6. && (*trackITS).Getphi()>6.) 
1189                         cluster(1)=cluster(1)+(2.*TMath::Pi());
1190                     if(cluster(1)>6. && (*trackITS).Getphi()<6.) 
1191                         cluster(1)=cluster(1)-(2.*TMath::Pi());
1192                     if(TMath::Abs(cluster(1)-(*trackITS).Getphi())>sigmatotphi)
1193                         continue;
1194                     // cout<<" supero sigmaphi \n";      
1195                     AliITSTrackV1 *newTrack = new AliITSTrackV1((*trackITS));
1196                     (*newTrack).SetLayer((*trackITS).GetLayer()-1);
1197                     if (TMath::Abs(rTrack-cluster(0))/rTrack>1e-6) 
1198                         (*newTrack).Correct(Double_t(cluster(0)));      
1199                     //cout<<" cluster(2) e(*newTrack).GetZ()="<<cluster(2)<<" "
1200                     //    << (*newTrack).GetZ()<<"\n";
1201                     if(TMath::Abs(cluster(2)-(*newTrack).GetZ()) > sigmatotz){ 
1202                         delete newTrack;
1203                         continue;
1204                     } // end if
1205                     Double_t sigmanew[2];
1206                     sigmanew[0]= sigmaphi;
1207                     sigmanew[1]=sigma[1];
1208                     Double_t m[2];
1209                     m[0]=cluster(1);
1210                     m[1]=cluster(2);
1211                     //  Double_t chi2pred=newTrack->GetPredChi2(m,sigmanew);   
1212                     // cout<<" chi2pred = "<<chi2pred<<"\n";
1213                     // if(chi2pred>fChi2max) continue; //aggiunto il 30-7-2001
1214                     if(iriv == 0) flaghit=1;
1215                     (*newTrack).AddMS(frl);  // add the multiple scattering 
1216                                              //matrix to the covariance matrix 
1217                     (*newTrack).AddEL(frl,1.,0);
1218
1219                     if(fflagvert){
1220                         KalmanFilterVert(newTrack,cluster,sigmanew);
1221                         //KalmanFilterVert(newTrack,cluster,sigmanew,chi2pred);
1222                     }else{
1223                         KalmanFilter(newTrack,cluster,sigmanew);
1224                     } // end if
1225                     (*newTrack).PutCluster(layernew, vecclust);
1226                     newTrack->AddClustInTrack();
1227                     listoftrack.AddLast(newTrack);
1228                 }   // end for indnew
1229             }  // end of for on detectors (iriv)
1230         }//end if(outinters==0)
1231
1232         if(flaghit==0 || outinters==-2) {
1233             AliITSTrackV1 *newTrack = new AliITSTrackV1(*trackITS);
1234             (*newTrack).Setfnoclust();           
1235             (*newTrack).SetLayer((*trackITS).GetLayer()-1); 
1236             (*newTrack).AddMS(frl);  // add the multiple scattering matrix
1237                                      // to the covariance matrix  
1238             (*newTrack).AddEL(frl,1.,0);
1239             listoftrack.AddLast(newTrack);
1240         } // end if
1241
1242         //gObjectTable->Print();   // stampa memoria
1243          
1244         RecursiveTracking(&listoftrack);          
1245         listoftrack.Delete();
1246     } // end of for on tracks (index)
1247
1248     //gObjectTable->Print();   // stampa memoria
1249 }
1250 //______________________________________________________________________
1251 Int_t AliITSTrackerV1::Intersection(AliITSTrackV1 &track,Int_t layer,
1252                                     Int_t &ladder,Int_t &detector) { 
1253     // Origin  A. Badala' and G.S. Pappalardo
1254     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
1255     // Found the intersection and the detector 
1256
1257     Double_t rk=fAvrad[layer-1];
1258     if(track.DoNotCross(rk)){ /*cout<< " Do not cross \n";*/ return -1;} 
1259     track.Propagation(rk);
1260     Double_t zinters=track.GetZ();
1261     Double_t phinters=track.Getphi();
1262     //cout<<"zinters = "<<zinters<<"  phinters = "<<phinters<<"\n";
1263
1264     TVector det(9);
1265     TVector listDet(2);
1266     TVector distZCenter(2);
1267
1268     Int_t iz=0; 
1269     Int_t iD;
1270     for(iD = 1; iD<= fNdet[layer-1]; iD++) {
1271         if(zinters > fzmin[layer-1][iD-1] && zinters <= fzmax[layer-1][iD-1]) {
1272             if(iz>1) {
1273                 cout<< " Errore su iz in Intersection \n";
1274                 getchar();
1275             }else {
1276                 listDet(iz)= iD; distZCenter(iz)=TMath::Abs(zinters-det(2));
1277                 iz++;
1278             } // end if
1279         } // end if
1280     } // end for iD
1281
1282     if(iz==0) {/* cout<< " No detector along Z \n";*/ return -2;}
1283     detector=Int_t (listDet(0));
1284     if(iz>1 && (distZCenter(0)>distZCenter(1)))   detector=Int_t (listDet(1));
1285
1286     TVector listLad(2);
1287     TVector distPhiCenter(2);
1288     Int_t ip=0;
1289     Double_t pigre=TMath::Pi();
1290     Int_t iLd;   
1291     for(iLd = 1; iLd<= fNlad[layer-1]; iLd++) {
1292         Double_t phimin=fphimin[layer-1][iLd-1];
1293         Double_t phimax=fphimax[layer-1][iLd-1];
1294         Double_t phidet=fphidet[layer-1][iLd-1];
1295         Double_t phiconfr=phinters;
1296         if(phimin>phimax) {  
1297             //if(phimin <5.5) {cout<<" Error in Intersection for phi \n";
1298             // getchar();}
1299             phimin-=(2.*pigre);
1300             if(phinters>(1.5*pigre)) phiconfr=phinters-(2.*pigre); 
1301             if(phidet>(1.5*pigre)) phidet-=(2.*pigre);
1302         } // end if
1303         if(phiconfr>phimin && phiconfr<= phimax) {
1304             if(ip>1) {
1305                 cout<< " Errore su ip in Intersection \n"; getchar();
1306             }else  {
1307                 listLad(ip)= iLd;
1308                 distPhiCenter(ip)=TMath::Abs(phiconfr-phidet); ip++;
1309             } // end if
1310         } // end if
1311     } // end for iLd
1312     if(ip==0) { cout<< " No detector along phi \n"; getchar();}
1313     ladder=Int_t (listLad(0));
1314     if(ip>1 && (distPhiCenter(0)>distPhiCenter(1)))  ladder=Int_t (listLad(1));
1315     return 0;
1316 }
1317 //______________________________________________________________________
1318 void AliITSTrackerV1::KalmanFilter(AliITSTrackV1 *newTrack,TVector &cluster,
1319                                    Double_t sigma[2]){ 
1320     //Origin  A. Badala' and G.S. Pappalardo:
1321     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it
1322     // Kalman filter without vertex constraint
1323     ////// Evaluation of the measurement vector ////////////////////////
1324     Double_t m[2];
1325     Double_t rk,phik,zk;
1326     rk=cluster(0);   phik=cluster(1);  zk=cluster(2);
1327     m[0]=phik;    m[1]=zk;
1328     //////////////////////// Evaluation of the error matrix V  /////////
1329     Double_t v00=sigma[0];
1330     Double_t v11=sigma[1];
1331     ////////////////////////////////////////////////////////////////////  
1332     Double_t cin00,cin10,cin20,cin30,cin40,cin11,cin21,cin31,cin41,cin22,
1333              cin32,cin42,cin33,cin43,cin44;
1334
1335     newTrack->GetCElements(cin00,
1336                            cin10,cin11,
1337                            cin20,cin21,cin22,
1338                            cin30,cin31,cin32,cin33,
1339                            cin40,cin41,cin42,cin43,cin44); //get C matrix
1340     Double_t rold00=cin00+v00;
1341     Double_t rold10=cin10;
1342     Double_t rold11=cin11+v11;
1343     ////////////////////// R matrix inversion  /////////////////////////
1344     Double_t det=rold00*rold11-rold10*rold10;
1345     Double_t r00=rold11/det;
1346     Double_t r10=-rold10/det;
1347     Double_t r11=rold00/det;
1348     ////////////////////////////////////////////////////////////////////
1349     Double_t k00=cin00*r00+cin10*r10;
1350     Double_t k01=cin00*r10+cin10*r11;
1351     Double_t k10=cin10*r00+cin11*r10;  
1352     Double_t k11=cin10*r10+cin11*r11;
1353     Double_t k20=cin20*r00+cin21*r10;  
1354     Double_t k21=cin20*r10+cin21*r11;  
1355     Double_t k30=cin30*r00+cin31*r10;  
1356     Double_t k31=cin30*r10+cin31*r11;  
1357     Double_t k40=cin40*r00+cin41*r10;
1358     Double_t k41=cin40*r10+cin41*r11;
1359     Double_t x0,x1,x2,x3,x4;
1360     newTrack->GetXElements(x0,x1,x2,x3,x4);     // get the state vector
1361     Double_t savex0=x0, savex1=x1;
1362     x0+=k00*(m[0]-savex0)+k01*(m[1]-savex1);
1363     x1+=k10*(m[0]-savex0)+k11*(m[1]-savex1);
1364     x2+=k20*(m[0]-savex0)+k21*(m[1]-savex1);
1365     x3+=k30*(m[0]-savex0)+k31*(m[1]-savex1);
1366     x4+=k40*(m[0]-savex0)+k41*(m[1]-savex1);
1367     Double_t c00,c10,c20,c30,c40,c11,c21,c31,c41,c22,c32,c42,c33,c43,c44;
1368     c00=cin00-k00*cin00-k01*cin10;
1369     c10=cin10-k00*cin10-k01*cin11;
1370     c20=cin20-k00*cin20-k01*cin21;
1371     c30=cin30-k00*cin30-k01*cin31;
1372     c40=cin40-k00*cin40-k01*cin41;
1373     c11=cin11-k10*cin10-k11*cin11;
1374     c21=cin21-k10*cin20-k11*cin21;
1375     c31=cin31-k10*cin30-k11*cin31;
1376     c41=cin41-k10*cin40-k11*cin41;
1377     c22=cin22-k20*cin20-k21*cin21;
1378     c32=cin32-k20*cin30-k21*cin31;
1379     c42=cin42-k20*cin40-k21*cin41;
1380     c33=cin33-k30*cin30-k31*cin31;
1381     c43=cin43-k30*cin40-k31*cin41;
1382     c44=cin44-k40*cin40-k41*cin41;
1383     newTrack->PutXElements(x0,x1,x2,x3,x4);  // put the new state vector
1384     newTrack->PutCElements(c00,
1385                            c10,c11,
1386                            c20,c21,c22,
1387                            c30,c31,c32,c33,
1388                            c40,c41,c42,c43,c44); // put in track the
1389                                                  // new cov matrix 
1390     Double_t vmcold00=v00-c00;
1391     Double_t vmcold10=-c10;
1392     Double_t vmcold11=v11-c11;
1393     ////////////////////// Matrix vmc inversion  ///////////////////////
1394     det=vmcold00*vmcold11-vmcold10*vmcold10;
1395     Double_t vmc00=vmcold11/det;
1396     Double_t vmc10=-vmcold10/det;
1397     Double_t vmc11=vmcold00/det;
1398     ////////////////////////////////////////////////////////////////////
1399   Double_t chi2=(m[0]-x0)*( vmc00*(m[0]-x0) + 2.*vmc10*(m[1]-x1) ) +
1400                 (m[1]-x1)*vmc11*(m[1]-x1);
1401   newTrack->SetChi2(newTrack->GetChi2()+chi2);
1402 }
1403 //----------------------------------------------------------------------
1404 //void AliITSTrackerV1::KalmanFilterVert(AliITSTrackV1 *newTrack,
1405 //                                       TVector &cluster,Double_t sigma[2]){
1406 void AliITSTrackerV1::KalmanFilterVert(AliITSTrackV1 *newTrack,
1407                                        TVector &cluster,Double_t sigma[2]
1408                                        /*, Double_t chi2pred*/){
1409     //Origin  A. Badala' and G.S. Pappalardo:
1410     // e-mail Angela.Badala@ct.infn.it, Giuseppe.S.Pappalardo@ct.infn.it 
1411     // Kalman filter with vertex constraint
1412     ///////////////////// Evaluation of the measurement vector m ////////
1413     Double_t m[4];
1414     Double_t rk,phik,zk;
1415     rk=cluster(0);   phik=cluster(1);  zk=cluster(2);
1416     m[0]=phik;    m[1]=zk;
1417     Double_t cc=(*newTrack).GetC();
1418     Double_t zv=(*newTrack).GetZv(); 
1419     Double_t dv=(*newTrack).GetDv();
1420     Double_t cy=cc/2.;
1421     Double_t tgl= (zk-zv)*cy/TMath::ASin(cy*rk);
1422     m[2]=dv;    m[3]=tgl;
1423     /////////////////////// Evaluation of the error matrix V  //////////
1424     Int_t layer=newTrack->GetLayer();
1425     Double_t v00=sigma[0];
1426     Double_t v11=sigma[1];
1427     Double_t v31=sigma[1]/rk;
1428     Double_t sigmaDv=newTrack->GetsigmaDv();
1429     Double_t v22=sigmaDv*sigmaDv  + newTrack->Getd2(layer-1);
1430     Double_t v32=newTrack->Getdtgl(layer-1);
1431     Double_t sigmaZv=newTrack->GetsigmaZv();  
1432     Double_t v33=(sigma[1]+sigmaZv*sigmaZv)/(rk*rk)+newTrack->Gettgl2(layer-1);
1433     //////////////////////////////////////////////////////////////////
1434     Double_t cin00,cin10,cin11,cin20,cin21,cin22,
1435              cin30,cin31,cin32,cin33,cin40,cin41,cin42,cin43,cin44;
1436     newTrack->GetCElements(cin00,
1437                            cin10,cin11,
1438                            cin20,cin21,cin22,
1439                            cin30,cin31,cin32,cin33,
1440                            cin40,cin41,cin42,cin43,cin44); //get C matrix
1441     Double_t r[4][4];
1442     r[0][0]=cin00+v00;
1443     r[1][0]=cin10;
1444     r[2][0]=cin20;
1445     r[3][0]=cin30;
1446     r[1][1]=cin11+v11;
1447     r[2][1]=cin21;
1448     r[3][1]=cin31+sigma[1]/rk;
1449     r[2][2]=cin22+sigmaDv*sigmaDv+newTrack->Getd2(layer-1);
1450     r[3][2]=cin32+newTrack->Getdtgl(layer-1);
1451     r[3][3]=cin33+(sigma[1]+sigmaZv*sigmaZv)/(rk*rk)+
1452                                     newTrack->Gettgl2(layer-1);
1453     r[0][1]=r[1][0]; r[0][2]=r[2][0]; r[0][3]=r[3][0]; 
1454     r[1][2]=r[2][1]; r[1][3]=r[3][1]; r[2][3]=r[3][2];
1455     /////////////////////  Matrix R inversion //////////////////////////
1456     const Int_t kn=4;
1457     Double_t big, hold;
1458     Double_t d=1.;
1459     Int_t ll[kn],mm[kn];
1460     Int_t i,j,k;
1461
1462     for(k=0; k<kn; k++) {
1463         ll[k]=k;
1464         mm[k]=k;
1465         big=r[k][k];
1466         for(j=k; j<kn ; j++) {
1467             for (i=j; i<kn; i++) {
1468                 if(TMath::Abs(big) < TMath::Abs(r[i][j]) ) { 
1469                     big=r[i][j]; 
1470                     ll[k]=i; 
1471                     mm[k]=j; 
1472                 } // end if
1473             } // end for i
1474         } // end for j
1475         //
1476         j= ll[k];
1477         if(j > k) {
1478             for(i=0; i<kn; i++) {
1479                 hold=-r[k][i]; 
1480                 r[k][i]=r[j][i]; 
1481                 r[j][i]=hold;
1482             } // end for i
1483         }// end if
1484         //
1485         i=mm[k];
1486         if(i > k ) { 
1487             for(j=0; j<kn; j++) {
1488                 hold=-r[j][k];
1489                 r[j][k]=r[j][i]; 
1490                 r[j][i]=hold; 
1491             } // end for j
1492         } // end if
1493         //
1494         if(!big) {
1495             d=0.;
1496             cout << "Singular matrix\n"; 
1497         } // end if
1498         for(i=0; i<kn; i++) {
1499             if(i == k) { continue; }    
1500             r[i][k]=r[i][k]/(-big);
1501         } // end for i
1502         //
1503         for(i=0; i<kn; i++) {
1504             hold=r[i][k];
1505             for(j=0; j<kn; j++) {
1506                 if(i == k || j == k) continue;
1507                 r[i][j]=hold*r[k][j]+r[i][j];
1508             } // end for j
1509         } // end of ri
1510         // 
1511         for(j=0; j<kn; j++) {
1512             if(j == k) continue;
1513             r[k][j]=r[k][j]/big;
1514         } // end for j
1515         //
1516         d=d*big;
1517         //
1518         r[k][k]=1./big;        
1519     } // end for k
1520     //
1521     for(k=kn-1; k>=0; k--) {
1522         i=ll[k];
1523         if(i > k) {
1524             for (j=0; j<kn; j++) {
1525                 hold=r[j][k];
1526                 r[j][k]=-r[j][i]; 
1527                 r[j][i]=hold;
1528             } // end for j
1529         } // end if i
1530         j=mm[k];
1531         if(j > k) {
1532             for (i=0; i<kn; i++) {
1533                 hold=r[k][i]; 
1534                 r[k][i]=-r[j][i]; 
1535                 r[j][i]=hold;
1536             } // end for i
1537         } // end if
1538     } // end for k
1539     ////////////////////////////////////////////////////////////////////
1540     Double_t k00=cin00*r[0][0]+cin10*r[1][0]+cin20*r[2][0]+cin30*r[3][0];
1541     Double_t k01=cin00*r[1][0]+cin10*r[1][1]+cin20*r[2][1]+cin30*r[3][1];
1542     Double_t k02=cin00*r[2][0]+cin10*r[2][1]+cin20*r[2][2]+cin30*r[3][2];
1543     Double_t k03=cin00*r[3][0]+cin10*r[3][1]+cin20*r[3][2]+cin30*r[3][3];
1544     Double_t k10=cin10*r[0][0]+cin11*r[1][0]+cin21*r[2][0]+cin31*r[3][0];  
1545     Double_t k11=cin10*r[1][0]+cin11*r[1][1]+cin21*r[2][1]+cin31*r[3][1];
1546     Double_t k12=cin10*r[2][0]+cin11*r[2][1]+cin21*r[2][2]+cin31*r[3][2];
1547     Double_t k13=cin10*r[3][0]+cin11*r[3][1]+cin21*r[3][2]+cin31*r[3][3];
1548     Double_t k20=cin20*r[0][0]+cin21*r[1][0]+cin22*r[2][0]+cin32*r[3][0];  
1549     Double_t k21=cin20*r[1][0]+cin21*r[1][1]+cin22*r[2][1]+cin32*r[3][1];  
1550     Double_t k22=cin20*r[2][0]+cin21*r[2][1]+cin22*r[2][2]+cin32*r[3][2];
1551     Double_t k23=cin20*r[3][0]+cin21*r[3][1]+cin22*r[3][2]+cin32*r[3][3];
1552     Double_t k30=cin30*r[0][0]+cin31*r[1][0]+cin32*r[2][0]+cin33*r[3][0];  
1553     Double_t k31=cin30*r[1][0]+cin31*r[1][1]+cin32*r[2][1]+cin33*r[3][1];  
1554     Double_t k32=cin30*r[2][0]+cin31*r[2][1]+cin32*r[2][2]+cin33*r[3][2];  
1555     Double_t k33=cin30*r[3][0]+cin31*r[3][1]+cin32*r[3][2]+cin33*r[3][3];
1556     Double_t k40=cin40*r[0][0]+cin41*r[1][0]+cin42*r[2][0]+cin43*r[3][0];
1557     Double_t k41=cin40*r[1][0]+cin41*r[1][1]+cin42*r[2][1]+cin43*r[3][1];
1558     Double_t k42=cin40*r[2][0]+cin41*r[2][1]+cin42*r[2][2]+cin43*r[3][2];  
1559     Double_t k43=cin40*r[3][0]+cin41*r[3][1]+cin42*r[3][2]+cin43*r[3][3];
1560
1561     Double_t x0,x1,x2,x3,x4;
1562     newTrack->GetXElements(x0,x1,x2,x3,x4);     // get the state vector
1563     Double_t savex0=x0, savex1=x1, savex2=x2, savex3=x3;
1564     x0+=k00*(m[0]-savex0)+k01*(m[1]-savex1)+k02*(m[2]-savex2)+
1565         k03*(m[3]-savex3);
1566     x1+=k10*(m[0]-savex0)+k11*(m[1]-savex1)+k12*(m[2]-savex2)+
1567         k13*(m[3]-savex3);
1568     x2+=k20*(m[0]-savex0)+k21*(m[1]-savex1)+k22*(m[2]-savex2)+
1569         k23*(m[3]-savex3);
1570     x3+=k30*(m[0]-savex0)+k31*(m[1]-savex1)+k32*(m[2]-savex2)+
1571         k33*(m[3]-savex3);
1572     x4+=k40*(m[0]-savex0)+k41*(m[1]-savex1)+k42*(m[2]-savex2)+
1573         k43*(m[3]-savex3);
1574     Double_t c00,c10,c20,c30,c40,c11,c21,c31,c41,c22,c32,c42,c33,c43,c44;
1575     c00=cin00-k00*cin00-k01*cin10-k02*cin20-k03*cin30;
1576     c10=cin10-k00*cin10-k01*cin11-k02*cin21-k03*cin31;
1577     c20=cin20-k00*cin20-k01*cin21-k02*cin22-k03*cin32;
1578     c30=cin30-k00*cin30-k01*cin31-k02*cin32-k03*cin33;
1579     c40=cin40-k00*cin40-k01*cin41-k02*cin42-k03*cin43;
1580     c11=cin11-k10*cin10-k11*cin11-k12*cin21-k13*cin31;
1581     c21=cin21-k10*cin20-k11*cin21-k12*cin22-k13*cin32;
1582     c31=cin31-k10*cin30-k11*cin31-k12*cin32-k13*cin33;
1583     c41=cin41-k10*cin40-k11*cin41-k12*cin42-k13*cin43;
1584     c22=cin22-k20*cin20-k21*cin21-k22*cin22-k23*cin32;
1585     c32=cin32-k20*cin30-k21*cin31-k22*cin32-k23*cin33;
1586     c42=cin42-k20*cin40-k21*cin41-k22*cin42-k23*cin43;
1587     c33=cin33-k30*cin30-k31*cin31-k32*cin32-k33*cin33;
1588     c43=cin43-k30*cin40-k31*cin41-k32*cin42-k33*cin43;
1589     c44=cin44-k40*cin40-k41*cin41-k42*cin42-k43*cin43;
1590
1591     newTrack->PutXElements(x0,x1,x2,x3,x4); // put the new state vector
1592     newTrack->PutCElements(c00,
1593                            c10,c11,
1594                            c20,c21,c22,
1595                            c30,c31,c32,c33,
1596                            c40,c41,c42,c43,c44); // put in track the
1597                                                  // new cov matrix
1598     Double_t vmc[4][4];
1599     vmc[0][0]=v00-c00; vmc[1][0]=-c10; vmc[2][0]=-c20; vmc[3][0]=-c30;
1600     vmc[1][1]=v11-c11; vmc[2][1]=-c21; vmc[3][1]=v31-c31;
1601     vmc[2][2]=v22-c22; vmc[3][2]=v32-c32;
1602     vmc[3][3]=v33-c33;
1603     vmc[0][1]=vmc[1][0]; vmc[0][2]=vmc[2][0]; vmc[0][3]=vmc[3][0];
1604     vmc[1][2]=vmc[2][1]; vmc[1][3]=vmc[3][1];
1605     vmc[2][3]=vmc[3][2];
1606     /////////////////////// vmc matrix inversion /////////////////////// 
1607     d=1.;
1608     for(k=0; k<kn; k++) {
1609         ll[k]=k;
1610         mm[k]=k;
1611         big=vmc[k][k];
1612         for(j=k; j<kn ; j++) {
1613             for (i=j; i<kn; i++) {
1614                 if(TMath::Abs(big) < TMath::Abs(vmc[i][j]) ) {
1615                     big=vmc[i][j]; 
1616                     ll[k]=i;
1617                     mm[k]=j;
1618                 } // end if
1619             } // end for i
1620         } // end for j
1621         //
1622         j= ll[k];
1623         if(j > k) {
1624             for(i=0; i<kn; i++) {
1625                 hold=-vmc[k][i]; 
1626                 vmc[k][i]=vmc[j][i];
1627                 vmc[j][i]=hold;
1628             } // end for i
1629         } // end if
1630         //
1631         i=mm[k];
1632         if(i > k ) { 
1633             for(j=0; j<kn; j++) { 
1634                 hold=-vmc[j][k]; 
1635                 vmc[j][k]=vmc[j][i]; 
1636                 vmc[j][i]=hold; 
1637             } // end for j
1638         } // end if
1639         //
1640         if(!big) {
1641             d=0.;
1642             cout << "Singular matrix\n"; 
1643         } // end if
1644         for(i=0; i<kn; i++) {
1645             if(i == k) continue;    
1646             vmc[i][k]=vmc[i][k]/(-big);
1647         }    // end for i
1648         //
1649         for(i=0; i<kn; i++) {
1650             hold=vmc[i][k];
1651             for(j=0; j<kn; j++) {
1652                 if(i == k || j == k) continue;
1653                 vmc[i][j]=hold*vmc[k][j]+vmc[i][j];
1654             } // end for j
1655         } // end for i
1656         //  
1657         for(j=0; j<kn; j++) {
1658             if(j == k) continue;
1659             vmc[k][j]=vmc[k][j]/big;
1660         } // end for j
1661         //
1662         d=d*big;
1663         //
1664         vmc[k][k]=1./big;        
1665     }  // end for k
1666     //  
1667     for(k=kn-1; k>=0; k--) {
1668         i=ll[k];
1669         if(i > k) {
1670             for (j=0; j<kn; j++) {
1671                 hold=vmc[j][k]; 
1672                 vmc[j][k]=-vmc[j][i];
1673                 vmc[j][i]=hold;
1674             } // end for j
1675         } // end if i>k
1676         j=mm[k];
1677         if(j > k) {
1678             for (i=0; i<kn; i++) {
1679                 hold=vmc[k][i]; 
1680                 vmc[k][i]=-vmc[j][i]; 
1681                 vmc[j][i]=hold;
1682             } // end for i
1683         } // end if j>k
1684     } // end for k
1685     ////////////////////////////////////////////////////////////////////
1686     Double_t chi2=(m[0]-x0)*( vmc[0][0]*(m[0]-x0) + 2.*vmc[1][0]*(m[1]-x1) + 
1687                               2.*vmc[2][0]*(m[2]-x2)+ 2.*vmc[3][0]*(m[3]-x3) )+
1688         (m[1]-x1)* ( vmc[1][1]*(m[1]-x1) + 2.*vmc[2][1]*(m[2]-x2)+ 
1689                      2.*vmc[3][1]*(m[3]-x3) ) +
1690         (m[2]-x2)* ( vmc[2][2]*(m[2]-x2)+ 2.*vmc[3][2]*(m[3]-x3) ) +
1691         (m[3]-x3)*vmc[3][3]*(m[3]-x3);
1692     //cout<<" chi2 kalman = "<<chi2<<"\n";  getchar(); 
1693     newTrack->SetChi2(newTrack->GetChi2()+chi2);   
1694     //   newTrack->SetChi2(newTrack->GetChi2()+chi2pred);
1695 }