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