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