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