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