]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPC.cxx
Introduction of the Copyright and cvs Log
[u/mrichter/AliRoot.git] / TPC / AliTPC.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 */
19
20 ///////////////////////////////////////////////////////////////////////////////
21 //                                                                           //
22 //  Time Projection Chamber                                                  //
23 //  This class contains the basic functions for the Time Projection Chamber  //
24 //  detector. Functions specific to one particular geometry are              //
25 //  contained in the derived classes                                         //
26 //                                                                           //
27 //Begin_Html
28 /*
29 <img src="picts/AliTPCClass.gif">
30 */
31 //End_Html
32 //                                                                           //
33 //                                                                          //
34 ///////////////////////////////////////////////////////////////////////////////
35
36 #include <TMath.h>
37 #include <TRandom.h>
38 #include <TVector.h>
39 #include <TMatrix.h>
40 #include <TGeometry.h>
41 #include <TNode.h>
42 #include <TTUBS.h>
43 #include <TObjectTable.h>
44 #include "TParticle.h"
45 #include "AliTPC.h"
46 #include "AliRun.h"
47 #include <iostream.h>
48 #include <fstream.h>
49 #include "AliMC.h"
50
51 //MI change
52 #include "AliTPCParam.h"
53 #include "AliTPCD.h"
54 #include "AliTPCPRF2D.h"
55 #include "AliTPCRF1D.h"
56
57
58
59 ClassImp(AliTPC) 
60
61 //_____________________________________________________________________________
62 AliTPC::AliTPC()
63 {
64   //
65   // Default constructor
66   //
67   fIshunt   = 0;
68   fClusters = 0;
69   fHits     = 0;
70   fDigits   = 0;
71   fTracks   = 0;
72   fNsectors = 0;
73   fNtracks  = 0;
74   fNclusters= 0;
75
76   fDigParam= new AliTPCD();
77   fDigits = fDigParam->GetArray();
78 }
79  
80 //_____________________________________________________________________________
81 AliTPC::AliTPC(const char *name, const char *title)
82       : AliDetector(name,title)
83 {
84   //
85   // Standard constructor
86   //
87
88   //
89   // Initialise arrays of hits and digits 
90   fHits     = new TClonesArray("AliTPChit",  176);
91   //  fDigits   = new TClonesArray("AliTPCdigit",10000);
92   //MI change
93   fDigParam= new AliTPCD;
94   fDigits = fDigParam->GetArray();
95
96   AliTPCParam  *fTPCParam = &(fDigParam->GetParam());
97
98   //
99   // Initialise counters
100   //
101   fClusters = 0;
102   fTracks   = 0;
103   fNsectors = fTPCParam->GetNSector();
104   fNtracks  = 0;
105   fNclusters= 0;
106   fDigitsIndex = new Int_t[fNsectors+1];
107   fClustersIndex = new Int_t[fNsectors+1];
108   //
109   fIshunt     =  0;
110   //
111   // Initialise color attributes
112   SetMarkerColor(kYellow);
113 }
114
115 //_____________________________________________________________________________
116 AliTPC::~AliTPC()
117 {
118   //
119   // TPC destructor
120   //
121   fIshunt   = 0;
122   delete fHits;
123   delete fDigits;
124   delete fClusters;
125   delete fTracks;
126   delete fDigParam;
127   if (fDigitsIndex)   delete [] fDigitsIndex;
128   if (fClustersIndex) delete [] fClustersIndex;
129 }
130
131 //_____________________________________________________________________________
132 void AliTPC::AddCluster(Float_t *hits, Int_t *tracks)
133 {
134   //
135   // Add a simulated cluster to the list
136   //
137   if(!fClusters) fClusters=new TClonesArray("AliTPCcluster",10000);
138   TClonesArray &lclusters = *fClusters;
139   new(lclusters[fNclusters++]) AliTPCcluster(hits,tracks);
140 }
141  
142 //_____________________________________________________________________________
143 void AliTPC::AddCluster(const AliTPCcluster &c)
144 {
145   //
146   // Add a simulated cluster copy to the list
147   //
148   if(!fClusters) fClusters=new TClonesArray("AliTPCcluster",10000);
149   TClonesArray &lclusters = *fClusters;
150   new(lclusters[fNclusters++]) AliTPCcluster(c);
151 }
152  
153 //_____________________________________________________________________________
154 void AliTPC::AddDigit(Int_t *tracks, Int_t *digits)
155 {
156   //
157   // Add a TPC digit to the list
158   //
159   //  TClonesArray &ldigits = *fDigits;
160   //MI change 
161   TClonesArray &ldigits = *fDigParam->GetArray();
162   new(ldigits[fNdigits++]) AliTPCdigit(tracks,digits);
163 }
164  
165 //_____________________________________________________________________________
166 void AliTPC::AddHit(Int_t track, Int_t *vol, Float_t *hits)
167 {
168   //
169   // Add a hit to the list
170   //
171   TClonesArray &lhits = *fHits;
172   new(lhits[fNhits++]) AliTPChit(fIshunt,track,vol,hits);
173 }
174  
175 //_____________________________________________________________________________
176 void AliTPC::AddTrack(Float_t *hits)
177 {
178   //
179   // Add a track to the list of tracks
180   //
181   TClonesArray &ltracks = *fTracks;
182   new(ltracks[fNtracks++]) AliTPCtrack(hits);
183 }
184
185 //_____________________________________________________________________________
186 void AliTPC::AddTrack(const AliTPCtrack& t)
187 {
188   //
189   // Add a track copy to the list of tracks
190   //
191   if(!fTracks) fTracks=new TClonesArray("AliTPCtrack",10000);
192   TClonesArray &ltracks = *fTracks;
193   new(ltracks[fNtracks++]) AliTPCtrack(t);
194 }
195
196 //_____________________________________________________________________________
197 void AliTPC::BuildGeometry()
198 {
199   //
200   // Build TPC ROOT TNode geometry for the event display
201   //
202   TNode *Node, *Top;
203   TTUBS *tubs;
204   Int_t i;
205   const int kColorTPC=19;
206   char name[5], title[25];
207   const Double_t kDegrad=TMath::Pi()/180;
208   const Double_t kRaddeg=180./TMath::Pi();
209
210   AliTPCParam * fTPCParam = &(fDigParam->GetParam());
211
212   Float_t InnerOpenAngle = fTPCParam->GetInnerAngle();
213   Float_t OuterOpenAngle = fTPCParam->GetOuterAngle();
214
215   Float_t InnerAngleShift = fTPCParam->GetInnerAngleShift();
216   Float_t OuterAngleShift = fTPCParam->GetOuterAngleShift();
217
218   Int_t nLo = fTPCParam->GetNInnerSector()/2;
219   Int_t nHi = fTPCParam->GetNOuterSector()/2;  
220
221   const Double_t loAng = (Double_t)TMath::Nint(InnerOpenAngle*kRaddeg);
222   const Double_t hiAng = (Double_t)TMath::Nint(OuterOpenAngle*kRaddeg);
223   const Double_t loAngSh = (Double_t)TMath::Nint(InnerAngleShift*kRaddeg);
224   const Double_t hiAngSh = (Double_t)TMath::Nint(OuterAngleShift*kRaddeg);  
225
226
227   const Double_t loCorr = 1/TMath::Cos(0.5*loAng*kDegrad);
228   const Double_t hiCorr = 1/TMath::Cos(0.5*hiAng*kDegrad);
229
230   Double_t rl,ru;
231   
232
233   //
234   // Get ALICE top node
235   //
236
237   Top=gAlice->GetGeometry()->GetNode("alice");
238
239   //  inner sectors
240
241   rl = fTPCParam->GetInSecLowEdge();
242   ru = fTPCParam->GetInSecUpEdge();
243  
244
245   for(i=0;i<nLo;i++) {
246     sprintf(name,"LS%2.2d",i);
247     name[4]='\0';
248     sprintf(title,"TPC low sector %3d",i);
249     title[24]='\0';
250     
251     tubs = new TTUBS(name,title,"void",rl*loCorr,ru*loCorr,250.,
252                      loAng*(i-0.5)+loAngSh,loAng*(i+0.5)+loAngSh);
253     tubs->SetNumberOfDivisions(1);
254     Top->cd();
255     Node = new TNode(name,title,name,0,0,0,"");
256     Node->SetLineColor(kColorTPC);
257     fNodes->Add(Node);
258   }
259
260   // Outer sectors
261
262   rl = fTPCParam->GetOuSecLowEdge();
263   ru = fTPCParam->GetOuSecUpEdge();
264
265   for(i=0;i<nHi;i++) {
266     sprintf(name,"US%2.2d",i);
267     name[4]='\0';
268     sprintf(title,"TPC upper sector %d",i);
269     title[24]='\0';
270     tubs = new TTUBS(name,title,"void",rl*hiCorr,ru*hiCorr,250,
271                      hiAng*(i-0.5)+hiAngSh,hiAng*(i+0.5)+hiAngSh);
272     tubs->SetNumberOfDivisions(1);
273     Top->cd();
274     Node = new TNode(name,title,name,0,0,0,"");
275     Node->SetLineColor(kColorTPC);
276     fNodes->Add(Node);
277   }
278 }  
279   
280   
281
282 //_____________________________________________________________________________
283 Int_t AliTPC::DistancetoPrimitive(Int_t , Int_t )
284 {
285   //
286   // Calculate distance from TPC to mouse on the display
287   // Dummy procedure
288   //
289   return 9999;
290 }
291
292 //_____________________________________________________________________________
293 static Double_t SigmaY2(Double_t r, Double_t tgl, Double_t pt)
294 {
295   //
296   // Parametrised error of the cluster reconstruction (pad direction)   
297   //
298   pt=TMath::Abs(pt)*1000.;
299   Double_t x=r/pt;
300   tgl=TMath::Abs(tgl);
301   Double_t s=a_rphi - b_rphi*r*tgl + c_rphi*x*x + d_rphi*x;
302   if (s<0.4e-3) s=0.4e-3;
303   s*=1.3; //Iouri Belikov
304   return s;
305 }
306
307 //_____________________________________________________________________________
308 static Double_t SigmaZ2(Double_t r, Double_t tgl) 
309 {
310   //
311   // Parametrised error of the cluster reconstruction (drift direction)
312   //
313   tgl=TMath::Abs(tgl);
314   Double_t s=a_z - b_z*r*tgl + c_z*tgl*tgl;
315   if (s<0.4e-3) s=0.4e-3;
316   s*=1.3; //Iouri Belikov
317   return s;
318 }
319
320 //_____________________________________________________________________________
321 inline Double_t f1(Double_t x1,Double_t y1,
322                    Double_t x2,Double_t y2,
323                    Double_t x3,Double_t y3) 
324 {
325   //-----------------------------------------------------------------
326   // Initial approximation of the track curvature
327   //
328   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
329   //-----------------------------------------------------------------
330   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
331   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
332                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
333   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
334                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
335
336   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
337
338   return -xr*yr/sqrt(xr*xr+yr*yr); 
339 }
340
341
342 //_____________________________________________________________________________
343 inline Double_t f2(Double_t x1,Double_t y1,
344                    Double_t x2,Double_t y2,
345                    Double_t x3,Double_t y3) 
346 {
347   //-----------------------------------------------------------------
348   // Initial approximation of the track curvature times center of curvature
349   //
350   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
351   //-----------------------------------------------------------------
352   Double_t d=(x2-x1)*(y3-y2)-(x3-x2)*(y2-y1);
353   Double_t a=0.5*((y3-y2)*(y2*y2-y1*y1+x2*x2-x1*x1)-
354                   (y2-y1)*(y3*y3-y2*y2+x3*x3-x2*x2));
355   Double_t b=0.5*((x2-x1)*(y3*y3-y2*y2+x3*x3-x2*x2)-
356                   (x3-x2)*(y2*y2-y1*y1+x2*x2-x1*x1));
357
358   Double_t xr=TMath::Abs(d/(d*x1-a)), yr=d/(d*y1-b);
359   
360   return -a/(d*y1-b)*xr/sqrt(xr*xr+yr*yr);
361 }
362
363 //_____________________________________________________________________________
364 inline Double_t f3(Double_t x1,Double_t y1, 
365                    Double_t x2,Double_t y2,
366                    Double_t z1,Double_t z2) 
367 {
368   //-----------------------------------------------------------------
369   // Initial approximation of the tangent of the track dip angle
370   //
371   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
372   //-----------------------------------------------------------------
373   return (z1 - z2)/sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
374 }
375
376 //_____________________________________________________________________________
377 static int FindProlongation(AliTPCtrack& t, const AliTPCSector *sec,
378                             int s, int rf=0) 
379 {
380   //-----------------------------------------------------------------
381   // This function tries to find a track prolongation.
382   //
383   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
384   //-----------------------------------------------------------------
385   const int ROWS_TO_SKIP=int(0.5*sec->GetNRows());
386   const Float_t MAX_CHI2=12.;
387   int try_again=ROWS_TO_SKIP;
388   Double_t alpha=sec->GetAlpha();
389   int ns=int(2*TMath::Pi()/alpha+0.5);
390
391   for (int nr=sec->GetRowNumber(t.GetX())-1; nr>=rf; nr--) {
392     Double_t x=sec->GetX(nr), ymax=sec->GetMaxY(nr);
393     if (!t.PropagateTo(x)) return 0;
394
395     AliTPCcluster *cl=0;
396     Double_t max_chi2=MAX_CHI2;
397     const AliTPCRow& row=sec[s][nr];
398     Double_t sy2=SigmaY2(t.GetX(),t.GetTgl(),t.GetPt());
399     Double_t sz2=SigmaZ2(t.GetX(),t.GetTgl());
400     Double_t road=5.*sqrt(t.GetSigmaY2() + sy2), y=t.GetY(), z=t.GetZ();
401
402     if (road>30) {
403       if (t>4) cerr<<t<<" FindProlongation warning: Too broad road !\n"; 
404       return 0;
405     }
406
407     if (row) {
408       for (int i=row.Find(y-road); i<row; i++) {
409         AliTPCcluster* c=(AliTPCcluster*)(row[i]);
410         if (c->fY > y+road) break;
411         if (c->IsUsed()) continue;
412         if ((c->fZ - z)*(c->fZ - z) > 25.*(t.GetSigmaZ2() + sz2)) continue;
413         Double_t chi2=t.GetPredictedChi2(c);
414         if (chi2 > max_chi2) continue;
415         max_chi2=chi2;
416         cl=c;       
417       }
418     }
419     if (cl) {
420       t.Update(cl,max_chi2);
421       Double_t ll=TMath::Sqrt((1+t.GetTgl()*t.GetTgl())/
422                (1-(t.GetC()*x-t.GetEta())*(t.GetC()*x-t.GetEta())));
423       cl->fdEdX = cl->fQ/ll;
424       try_again=ROWS_TO_SKIP;
425     } else {
426       if (try_again==0) break;
427       if (y > ymax) {
428          s = (s+1) % ns;
429          if (!t.Rotate(alpha)) return 0;
430       } else if (y <-ymax) {
431          s = (s-1+ns) % ns;
432          if (!t.Rotate(-alpha)) return 0;
433       }
434       try_again--;
435     }
436   }
437
438   return 1;
439 }
440
441
442 //_____________________________________________________________________________
443 static void MakeSeeds(TObjArray& seeds,const AliTPCSector *sec, int max_sec,
444 int i1, int i2)
445 {
446   //-----------------------------------------------------------------
447   // This function creates track seeds.
448   //
449   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
450   //-----------------------------------------------------------------
451   TMatrix C(5,5); TVector x(5);
452   double alpha=sec->GetAlpha(), shift=sec->GetAlphaShift();
453   double cs=cos(alpha), sn=sin(alpha);
454   for (int ns=0; ns<max_sec; ns++) {
455     int nl=sec[(ns-1+max_sec)%max_sec][i2];
456     int nm=sec[ns][i2];
457     int nu=sec[(ns+1)%max_sec][i2];
458     const AliTPCRow& r1=sec[ns][i1];
459     for (int is=0; is < r1; is++) {
460       double x1=sec->GetX(i1), y1=r1[is]->fY, z1=r1[is]->fZ;
461       for (int js=0; js < nl+nm+nu; js++) {
462         const AliTPCcluster *cl;
463         int ks;
464         double x2=sec->GetX(i2), y2, z2, tmp;
465
466         if (js<nl) {
467           ks=(ns-1+max_sec)%max_sec;
468           const AliTPCRow& r2=sec[(ns-1+max_sec)%max_sec][i2];
469           cl=r2[js];
470           y2=cl->fY; z2=cl->fZ;
471           tmp= x2*cs+y2*sn;
472           y2 =-x2*sn+y2*cs; x2=tmp;
473         } else 
474           if (js<nl+nm) {
475             ks=ns;
476             const AliTPCRow& r2=sec[ns][i2];
477             cl=r2[js-nl];
478             y2=cl->fY; z2=cl->fZ;
479           } else {
480             ks=(ns+1)%max_sec;
481             const AliTPCRow& r2=sec[(ns+1)%max_sec][i2];
482             cl=r2[js-nl-nm];
483             y2=cl->fY; z2=cl->fZ;
484             tmp=x2*cs-y2*sn;
485             y2 =x2*sn+y2*cs; x2=tmp;
486           }
487
488         double d=(x2-x1)*(0.-y2)-(0.-x2)*(y2-y1);
489         if (d==0.) {cerr<<"MakeSeeds warning: Straight seed !\n"; continue;}
490
491         x(0)=y1;
492         x(1)=z1;
493         x(2)=f1(x1,y1,x2,y2,0.,0.);
494         x(3)=f2(x1,y1,x2,y2,0.,0.);
495         x(4)=f3(x1,y1,x2,y2,z1,z2);
496         
497         if (TMath::Abs(x(2)*x1-x(3)) >= 0.999) continue;
498         
499         if (TMath::Abs(x(4)) > 1.2) continue;
500
501         Double_t a=asin(x(3));
502         Double_t zv=z1 - x(4)/x(2)*(a+asin(x(2)*x1-x(3)));
503         if (TMath::Abs(zv)>33.) continue; 
504
505         TMatrix X(6,6); X=0.; 
506         X(0,0)=r1[is]->fSigmaY2; X(1,1)=r1[is]->fSigmaZ2;
507         X(2,2)=cl->fSigmaY2;     X(3,3)=cl->fSigmaZ2;
508         X(4,4)=3./12.; X(5,5)=3./12.;
509         TMatrix F(5,6); F.UnitMatrix();
510         Double_t sy=sqrt(X(0,0)), sz=sqrt(X(1,1));
511         F(2,0)=(f1(x1,y1+sy,x2,y2,0.,0.)-x(2))/sy;
512         F(2,2)=(f1(x1,y1,x2,y2+sy,0.,0.)-x(2))/sy;
513         F(2,4)=(f1(x1,y1,x2,y2,0.,0.+sy)-x(2))/sy;
514         F(3,0)=(f2(x1,y1+sy,x2,y2,0.,0.)-x(3))/sy;
515         F(3,2)=(f2(x1,y1,x2,y2+sy,0.,0.)-x(3))/sy;
516         F(3,4)=(f2(x1,y1,x2,y2,0.,0.+sy)-x(3))/sy;
517         F(4,0)=(f3(x1,y1+sy,x2,y2,z1,z2)-x(4))/sy;
518         F(4,1)=(f3(x1,y1,x2,y2,z1+sz,z2)-x(4))/sz;
519         F(4,2)=(f3(x1,y1,x2,y2+sy,z1,z2)-x(4))/sy;
520         F(4,3)=(f3(x1,y1,x2,y2,z1,z2+sz)-x(4))/sz;
521         F(4,4)=0;
522         F(3,3)=0;
523         
524         TMatrix t(F,TMatrix::kMult,X);
525         C.Mult(t,TMatrix(TMatrix::kTransposed,F));
526
527         AliTPCtrack *track=new AliTPCtrack(r1[is], x, C, x1, ns*alpha+shift);
528         int rc=FindProlongation(*track,sec,ns,i2);
529         if (rc<0 || *track<(i1-i2)/2) delete track;
530         else seeds.AddLast(track); 
531       }
532     }
533   }
534 }
535
536 //_____________________________________________________________________________
537 AliTPCParam *AliTPCSector::param;
538 void AliTPC::Clusters2Tracks()
539 {
540   //-----------------------------------------------------------------
541   // This is a track finder.
542   //
543   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
544   //-----------------------------------------------------------------
545   if (!fClusters) return;
546
547   AliTPCParam *p=&fDigParam->GetParam();
548   AliTPCSector::SetParam(p);
549
550   const int nis=p->GetNInnerSector()/2;
551   AliTPCSSector *ssec=new AliTPCSSector[nis];         
552   int nrow_low=ssec->GetNRows();     
553
554   const int nos=p->GetNOuterSector()/2;
555   AliTPCLSector *lsec=new AliTPCLSector[nos];
556   int nrow_up=lsec->GetNRows();
557
558   int ncl=fClusters->GetEntriesFast();
559   while (ncl--) {
560     AliTPCcluster *c=(AliTPCcluster*)fClusters->UncheckedAt(ncl);
561     Int_t sec=c->fSector, row=c->fPadRow;
562     if (sec<nis*2) {
563       ssec[sec%nis][row].InsertCluster(c);
564     } else {
565       sec -= nis*2;
566       lsec[sec%nos][row].InsertCluster(c);
567     }
568   }
569   
570   TObjArray seeds(20000);
571
572   int nrows=nrow_low+nrow_up;
573   int gap=int(0.125*nrows), shift=int(0.5*gap);
574   MakeSeeds(seeds, lsec, nos, nrow_up-1, nrow_up-1-gap);
575   MakeSeeds(seeds, lsec, nos, nrow_up-1-shift, nrow_up-1-shift-gap);
576     
577   seeds.Sort();
578   
579   int found=0;
580   int nseed=seeds.GetEntriesFast();
581   
582   for (int s=0; s<nseed; s++) {
583     AliTPCtrack& t=*((AliTPCtrack*)seeds.UncheckedAt(s));
584     double alpha=t.GetAlpha();
585     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
586     if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
587     int ns=int(alpha/lsec->GetAlpha())%nos;
588
589     if (!FindProlongation(t,lsec,ns)) continue;
590
591     alpha=t.GetAlpha() + 0.5*ssec->GetAlpha() - ssec->GetAlphaShift();
592     if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();
593     if (alpha < 0.            ) alpha += 2.*TMath::Pi();
594     ns=int(alpha/ssec->GetAlpha())%nis; //index of the inner sector needed
595
596     alpha=ns*ssec->GetAlpha() - t.GetAlpha();
597     if (!t.Rotate(alpha)) continue;
598
599     if (!FindProlongation(t,ssec,ns)) continue;
600     
601     if (t < int(0.4*nrows)) continue;
602     
603     AddTrack(t);
604     t.UseClusters();
605     cerr<<found++<<'\r';
606   }  
607
608   delete[] ssec;
609   delete[] lsec;
610 }
611
612 //_____________________________________________________________________________
613 void AliTPC::CreateMaterials()
614 {
615   //-----------------------------------------------
616   // Create Materials for for TPC
617   //-----------------------------------------------
618
619   //-----------------------------------------------------------------
620   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
621   //-----------------------------------------------------------------
622
623   Int_t ISXFLD=gAlice->Field()->Integ();
624   Float_t SXMGMX=gAlice->Field()->Max();
625
626   Float_t amat[5]; // atomic numbers
627   Float_t zmat[5]; // z
628   Float_t wmat[5]; // proportions
629
630   Float_t density;
631
632   //  ********************* Gases *******************
633
634   //--------------------------------------------------------------
635   // pure gases
636   //--------------------------------------------------------------
637
638   // Ne
639
640
641   Float_t a_ne = 20.18;
642   Float_t z_ne = 10.;
643   
644   density = 0.0009;
645
646   AliMaterial(20,"Ne",a_ne,z_ne,density,999.,999.);
647
648   // Ar
649
650   Float_t a_ar = 39.948;
651   Float_t z_ar = 18.;
652
653   density = 0.001782;
654  
655   AliMaterial(21,"Ar",a_ar,z_ar,density,999.,999.);
656
657   Float_t a_pure[2];
658   
659   a_pure[0] = a_ne;
660   a_pure[1] = a_ar;
661   
662
663   //--------------------------------------------------------------
664   // gases - compounds
665   //--------------------------------------------------------------
666
667   Float_t amol[3];
668
669   //  CO2
670
671   amat[0]=12.011;
672   amat[1]=15.9994;
673
674   zmat[0]=6.;
675   zmat[1]=8.;
676
677   wmat[0]=1.;
678   wmat[1]=2.;
679
680   density=0.001977;
681
682   amol[0] = amat[0]*wmat[0]+amat[1]*wmat[1];
683
684   AliMixture(10,"CO2",amat,zmat,density,-2,wmat);
685
686   // CF4
687
688   amat[0]=12.011;
689   amat[1]=18.998;
690
691   zmat[0]=6.;
692   zmat[1]=9.;
693  
694   wmat[0]=1.;
695   wmat[1]=4.;
696  
697   density=0.003034;
698
699   amol[1] = amat[0]*wmat[0]+amat[1]*wmat[1];
700
701   AliMixture(11,"CF4",amat,zmat,density,-2,wmat); 
702
703   // CH4
704
705   amat[0]=12.011;
706   amat[1]=1.;
707
708   zmat[0]=6.;
709   zmat[1]=1.;
710
711   wmat[0]=1.;
712   wmat[1]=4.;
713
714   density=0.000717;
715
716   amol[2] = amat[0]*wmat[0]+amat[1]*wmat[1];
717
718   AliMixture(12,"CH4",amat,zmat,density,-2,wmat);
719
720   //----------------------------------------------------------------
721   // gases - mixtures, ID >= 20 pure gases, <= 10 ID < 20 -compounds
722   //----------------------------------------------------------------
723  
724   char namate[21];
725  
726   density = 0.;
727   Float_t am=0;
728   Int_t nc;
729
730   Float_t a,z,rho,absl,X0,buf[1];
731   Int_t nbuf;
732
733   for(nc = 0;nc<fNoComp;nc++)
734     {
735     
736       // retrive material constants
737       
738       gMC->Gfmate((*fIdmate)[fMixtComp[nc]],namate,a,z,rho,X0,absl,buf,nbuf);
739
740       amat[nc] = a;
741       zmat[nc] = z;
742
743       Int_t nnc = (fMixtComp[nc]>=20) ? fMixtComp[nc]%20 : fMixtComp[nc]%10;
744  
745       am += fMixtProp[nc]*((fMixtComp[nc]>=20) ? a_pure[nnc] : amol[nnc]); 
746       density += fMixtProp[nc]*rho;  // density of the mixture
747       
748     }
749
750   // mixture proportions by weight!
751
752   for(nc = 0;nc<fNoComp;nc++)
753     {
754
755       Int_t nnc = (fMixtComp[nc]>=20) ? fMixtComp[nc]%20 : fMixtComp[nc]%10;
756
757       wmat[nc] = fMixtProp[nc]*((fMixtComp[nc]>=20) ? a_pure[nnc] : amol[nnc])/am;
758
759     }  
760   
761   AliMixture(31,"Drift gas 1",amat,zmat,density,fNoComp,wmat);
762   AliMixture(32,"Drift gas 2",amat,zmat,density,fNoComp,wmat);
763   AliMixture(33,"Drift gas 3",amat,zmat,density,fNoComp,wmat); 
764
765   AliMedium(2, "Drift gas 1", 31, 0, ISXFLD, SXMGMX, 10., 999.,.1, .001, .001);
766   AliMedium(3, "Drift gas 2", 32, 0, ISXFLD, SXMGMX, 10., 999.,.1, .001, .001);
767   AliMedium(4, "Drift gas 3", 33, 1, ISXFLD, SXMGMX, 10., 999.,.1, .001, .001);
768
769   // Air 
770
771   AliMaterial(24, "Air", 14.61, 7.3, .001205, 30420., 67500.);
772
773   AliMedium(24, "Air", 24, 0, ISXFLD, SXMGMX, 10., .1, .1, .1, .1);
774
775   //----------------------------------------------------------------------
776   //               solid materials
777   //----------------------------------------------------------------------
778
779   // Al
780
781   AliMaterial(30, "Al", 26.98, 13., 2.7, 8.9, 37.2);
782
783   AliMedium(0, "Al",30, 0, ISXFLD, SXMGMX, 10., .1, .1, .1,   .1);
784
785   // Si
786
787   AliMaterial(31, "Si", 28.086, 14.,2.33, 9.36, 999.);
788
789   AliMedium(7, "Al",31, 0, ISXFLD, SXMGMX, 10., .1, .1, .1,   .1);
790   
791
792   // Mylar C5H4O2
793
794   amat[0]=12.011;
795   amat[1]=1.;
796   amat[2]=15.9994;
797
798   zmat[0]=6.;
799   zmat[1]=1.;
800   zmat[2]=8.;
801
802   wmat[0]=5.;
803   wmat[1]=4.;
804   wmat[2]=2.; 
805
806   density = 1.39;
807   
808   AliMixture(32, "Mylar",amat,zmat,density,-3,wmat);
809
810   AliMedium(5, "Mylar",32, 0, ISXFLD, SXMGMX, 10., .1, .1, .001, .01);
811
812
813
814
815   // Carbon (normal)
816
817   AliMaterial(33,"C normal",12.011,6.,2.265,18.8,999.);
818
819   AliMedium(6,"C normal",33,0, ISXFLD, SXMGMX, 10., .1, .1, .001, .01);
820
821   // G10 for inner and outr field cage
822   // G10 is 60% SiO2 + 40% epoxy, right now I use A and Z for SiO2
823
824   Float_t rhoFactor;
825
826   amat[0]=28.086;
827   amat[1]=15.9994;
828
829   zmat[0]=14.;
830   zmat[1]=8.;
831
832   wmat[0]=1.;
833   wmat[1]=2.;
834
835   density = 1.7;
836   
837
838   AliMixture(34,"G10 aux.",amat,zmat,density,-2,wmat);
839
840
841   gMC->Gfmate((*fIdmate)[34],namate,a,z,rho,X0,absl,buf,nbuf);
842
843   Float_t thickX0 = 0.0052; // field cage in X0 units
844   
845   Float_t thick = 2.; // in cm
846
847   X0=19.4; // G10 
848
849   rhoFactor = X0*thickX0/thick;
850   density = rho*rhoFactor;
851
852   AliMaterial(35,"G10-fc",a,z,density,999.,999.);
853
854   AliMedium(8,"G10-fc",35,0, ISXFLD, SXMGMX, 10., .1, .1, .001, .01);
855
856   thickX0 = 0.0027; // inner vessel (eta <0.9)
857   thick=0.5;
858   rhoFactor = X0*thickX0/thick;
859   density = rho*rhoFactor;
860
861   AliMaterial(36,"G10-iv",a,z,density,999.,999.);  
862
863   AliMedium(9,"G10-iv",36,0, ISXFLD, SXMGMX, 10., .1, .1, .001, .01);
864
865   //  Carbon fibre  
866   
867   gMC->Gfmate((*fIdmate)[33],namate,a,z,rho,X0,absl,buf,nbuf);
868
869   thickX0 = 0.0133; // outer vessel
870   thick=3.0;
871   rhoFactor = X0*thickX0/thick;
872   density = rho*rhoFactor;
873
874
875   AliMaterial(37,"C-ov",a,z,density,999.,999.);
876
877   AliMedium(10,"C-ov",37,0, ISXFLD, SXMGMX, 10., .1, .1, .001, .01);  
878
879   thickX0=0.015; // inner vessel (cone, eta > 0.9)
880   thick=1.5;
881   rhoFactor = X0*thickX0/thick;
882   density = rho*rhoFactor;
883
884   AliMaterial(38,"C-ivc",a,z,density,999.,999.);
885
886   AliMedium(11,"C-ivc",38,0, ISXFLD, SXMGMX, 10., .1, .1, .001, .01);
887
888   //
889
890   AliMedium(12,"CO2",10,0, ISXFLD, SXMGMX, 10., 999.,.1, .001, .001);
891     
892
893
894 }
895
896 //_____________________________________________________________________________
897 struct Bin {
898    const AliTPCdigit *dig;
899    int idx;
900    Bin() {dig=0; idx=-1;}
901 };
902
903 struct PreCluster : public AliTPCcluster {
904   const AliTPCdigit* summit; //pointer to the maximum digit of this precluster
905   int idx;                   //index in AliTPC::fClusters
906   int npeaks;                //number of peaks in this precluster
907   int ndigits;               //number of digits in this precluster
908   PreCluster();
909 };
910 PreCluster::PreCluster() : AliTPCcluster() {npeaks=ndigits=0;}
911
912 //_____________________________________________________________________________
913 static void FindPreCluster(int i,int j,int maxj,Bin *bins,PreCluster &c) 
914 {
915   //-----------------------------------------------------------------
916   // This function looks for "preclusters".
917   //
918   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
919   //-----------------------------------------------------------------
920   Bin& b=bins[i*maxj+j];
921   double q=(double)TMath::Abs(b.dig->fSignal);
922
923   if (b.idx >= 0 && b.idx != c.idx) {
924     c.idx=b.idx;
925     c.npeaks++;
926   }
927   
928   if (q > TMath::Abs(c.summit->fSignal)) c.summit=b.dig;
929   
930   c.fY += i*q;
931   c.fZ += j*q;
932   c.fSigmaY2 += i*i*q;
933   c.fSigmaZ2 += j*j*q;
934   c.fQ += q;
935   c.ndigits++;
936
937   b.dig = 0;  b.idx = c.idx;
938   
939   if (bins[(i-1)*maxj+j].dig) FindPreCluster(i-1,j,maxj,bins,c);
940   if (bins[i*maxj+(j-1)].dig) FindPreCluster(i,j-1,maxj,bins,c);
941   if (bins[(i+1)*maxj+j].dig) FindPreCluster(i+1,j,maxj,bins,c);
942   if (bins[i*maxj+(j+1)].dig) FindPreCluster(i,j+1,maxj,bins,c);
943   
944 }
945
946 //_____________________________________________________________________________
947 void AliTPC::Digits2Clusters()
948 {
949   //-----------------------------------------------------------------
950   // This is a simple cluster finder.
951   //
952   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
953   //-----------------------------------------------------------------
954   AliTPCParam *par = &(fDigParam->GetParam());
955   
956   int inp=par->GetNPads(0,                  par->GetNRowLow()-1);
957   int onp=par->GetNPads(par->GetNSector()-1,par->GetNRowUp() -1);
958   const int MAXY=(inp>onp) ? inp+2 : onp+2;
959   const int MAXTBKT=int((z_end+6*par->GetZSigma())/par->GetZWidth())+1;
960   const int MAXZ=MAXTBKT+2;
961   const int THRESHOLD=20;
962   
963   TTree *t=(TTree*)gDirectory->Get("TreeD0_Param1");
964   t->GetBranch("Digits")->SetAddress(&fDigits);
965   Int_t sectors_by_rows=(Int_t)t->GetEntries();
966   
967   int ncls=0;
968   
969   for (Int_t n=0; n<sectors_by_rows; n++) {
970     if (!t->GetEvent(n)) continue;
971     Bin *bins=new Bin[MAXY*MAXZ];
972     AliTPCdigit *dig=(AliTPCdigit*)fDigits->UncheckedAt(0);
973     int sec=dig->fSector, row=dig->fPadRow;
974     int ndigits=fDigits->GetEntriesFast();
975     
976     int npads, sign;
977     {
978        int nis=par->GetNInnerSector(), nos=par->GetNOuterSector();
979        if (sec < nis) {
980           npads = par->GetNPadsLow(row);
981           sign = (sec < nis/2) ? 1 : -1;
982        } else {
983           npads = par->GetNPadsUp(row);
984           sign = ((sec-nis) < nos/2) ? 1 : -1;
985        }
986     }
987
988     int ndig;
989     for (ndig=0; ndig<ndigits; ndig++) {
990       dig=(AliTPCdigit*)fDigits->UncheckedAt(ndig);
991       int i=dig->fPad+1, j=dig->fTime+1;
992       if (i > npads) {
993          cerr<<"AliTPC::Digits2Clusters error: pad number is out of range ! ";
994          cerr<<i<<' '<<npads<<endl; 
995          continue;
996       }
997       if (j > MAXTBKT) {
998          cerr<<"AliTPC::Digits2Clusters error: time bucket is out of range ! ";
999          cerr<<j<<' '<<MAXTBKT<<endl; 
1000          continue;
1001       }
1002       if (dig->fSignal >= THRESHOLD) bins[i*MAXZ+j].dig=dig;
1003       if (i==1 || i==npads || j==1 || j==MAXTBKT) dig->fSignal*=-1;
1004     }
1005
1006     int ncl=0;
1007     int i,j;
1008     
1009     for (i=1; i<MAXY-1; i++) {
1010       for (j=1; j<MAXZ-1; j++) {
1011         if (bins[i*MAXZ+j].dig == 0) continue;
1012         PreCluster c; c.summit=bins[i*MAXZ+j].dig; c.idx=ncls;
1013         FindPreCluster(i, j, MAXZ, bins, c);
1014         c.fY /= c.fQ;
1015         c.fZ /= c.fQ;
1016
1017         double s2 = c.fSigmaY2/c.fQ - c.fY*c.fY;
1018         c.fSigmaY2 = s2 + 1./12.;
1019         c.fSigmaY2 *= par->GetPadPitchWidth()*par->GetPadPitchWidth();
1020         if (s2 != 0.) c.fSigmaY2 *= 0.17;
1021
1022         s2 = c.fSigmaZ2/c.fQ - c.fZ*c.fZ;
1023         c.fSigmaZ2 = s2 + 1./12.;
1024         c.fSigmaZ2 *= par->GetZWidth()*par->GetZWidth();
1025         if (s2 != 0.) c.fSigmaZ2 *= 0.41;
1026
1027         c.fY = (c.fY - 0.5 - 0.5*npads)*par->GetPadPitchWidth();
1028         c.fZ = par->GetZWidth()*c.fZ; 
1029         c.fZ -= 3.*par->GetZSigma(); // PASA delay 
1030         c.fZ = sign*(z_end - c.fZ);
1031
1032         c.fSector=sec;
1033         c.fPadRow=row;
1034         c.fTracks[0]=c.summit->fTracks[0];
1035         c.fTracks[1]=c.summit->fTracks[1];
1036         c.fTracks[2]=c.summit->fTracks[2];
1037
1038         if (c.summit->fSignal<0) {
1039           c.fSigmaY2 *= 25.;
1040           c.fSigmaZ2 *= 4.;
1041         }
1042         
1043         AddCluster(c); ncls++; ncl++;
1044       }
1045     }
1046         
1047     for (ndig=0; ndig<ndigits; ndig++) {
1048       dig=(AliTPCdigit*)fDigits->UncheckedAt(ndig);
1049       int i=dig->fPad+1, j=dig->fTime+1;
1050       if (i > npads) {
1051          cerr<<"AliTPC::Digits2Clusters error: pad number is out of range ! ";
1052          cerr<<i<<' '<<npads<<endl; 
1053          continue;
1054       }
1055       if (j > MAXTBKT) {
1056          cerr<<"AliTPC::Digits2Clusters error: time bucket is out of range ! ";
1057          cerr<<j<<' '<<MAXTBKT<<endl; 
1058          continue;
1059       }
1060       if (TMath::Abs(dig->fSignal)>=par->GetZeroSup()) bins[i*MAXZ+j].dig=dig;
1061     }
1062     
1063     for (i=1; i<MAXY-1; i++) {
1064       for (j=1; j<MAXZ-1; j++) {
1065         if (bins[i*MAXZ+j].dig == 0) continue;
1066         PreCluster c; c.summit=bins[i*MAXZ+j].dig; c.idx=ncls;
1067         FindPreCluster(i, j, MAXZ, bins, c);
1068         if (c.ndigits < 2) continue; //noise cluster
1069         if (c.npeaks>1) continue;    //overlapped cluster
1070         c.fY /= c.fQ;
1071         c.fZ /= c.fQ;
1072
1073         double s2 = c.fSigmaY2/c.fQ - c.fY*c.fY;
1074         c.fSigmaY2 = s2 + 1./12.;
1075         c.fSigmaY2 *= par->GetPadPitchWidth()*par->GetPadPitchWidth();
1076         if (s2 != 0.) c.fSigmaY2 *= 0.04;
1077
1078         s2 = c.fSigmaZ2/c.fQ - c.fZ*c.fZ;
1079         c.fSigmaZ2 = s2 + 1./12.;
1080         c.fSigmaZ2 *= par->GetZWidth()*par->GetZWidth();
1081         if (s2 != 0.) c.fSigmaZ2 *= 0.10;
1082
1083         c.fY = (c.fY - 0.5 - 0.5*npads)*par->GetPadPitchWidth();
1084         c.fZ = par->GetZWidth()*c.fZ; 
1085         c.fZ -= 3.*par->GetZSigma(); // PASA delay 
1086         c.fZ = sign*(z_end - c.fZ);
1087         
1088         c.fSector=sec;
1089         c.fPadRow=row;
1090         c.fTracks[0]=c.summit->fTracks[0];
1091         c.fTracks[1]=c.summit->fTracks[1];
1092         c.fTracks[2]=c.summit->fTracks[2];
1093         
1094         if (c.summit->fSignal<0) {
1095           c.fSigmaY2 *= 25.;
1096           c.fSigmaZ2 *= 4.;
1097         }
1098
1099         if (c.npeaks==0) {AddCluster(c); ncls++; ncl++;}
1100         else {
1101           new ((*fClusters)[c.idx]) AliTPCcluster(c);
1102         }
1103       }
1104     }
1105     
1106     cerr<<"sector, row, digits, clusters: "
1107         <<sec<<' '<<row<<' '<<ndigits<<' '<<ncl<<"                  \r";
1108     
1109     fDigits->Clear();
1110   
1111     delete[] bins;  
1112   }
1113 }
1114
1115 //_____________________________________________________________________________
1116 void AliTPC::ElDiff(Float_t *xyz)
1117 {
1118   //--------------------------------------------------
1119   // calculates the diffusion of a single electron
1120   //--------------------------------------------------
1121
1122   //-----------------------------------------------------------------
1123   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1124   //-----------------------------------------------------------------
1125   AliTPCParam * fTPCParam = &(fDigParam->GetParam());
1126   Float_t driftl;
1127   
1128   Float_t z0=xyz[2];
1129
1130   driftl=z_end-TMath::Abs(xyz[2]);
1131
1132   if(driftl<0.01) driftl=0.01;
1133
1134   // check the attachment
1135
1136   driftl=TMath::Sqrt(driftl);
1137
1138   //  Float_t sig_t = driftl*diff_t;
1139   //Float_t sig_l = driftl*diff_l;
1140   Float_t sig_t = driftl*fTPCParam->GetDiffT();
1141   Float_t sig_l = driftl*fTPCParam->GetDiffL();
1142   xyz[0]=gRandom->Gaus(xyz[0],sig_t);
1143   xyz[1]=gRandom->Gaus(xyz[1],sig_t);
1144   xyz[2]=gRandom->Gaus(xyz[2],sig_l);
1145   
1146   if (TMath::Abs(xyz[2])>z_end){
1147     xyz[2]=TMath::Sign(z_end,z0);
1148   }
1149   if(xyz[2]*z0 < 0.){
1150     Float_t eps = 0.0001;
1151     xyz[2]=TMath::Sign(eps,z0);
1152   } 
1153 }
1154
1155 //_____________________________________________________________________________
1156 void AliTPC::Hits2Clusters()
1157 {
1158   //--------------------------------------------------------
1159   // TPC simple cluster generator from hits
1160   // obtained from the TPC Fast Simulator
1161   // The point errors are taken from the parametrization
1162   //--------------------------------------------------------
1163
1164   //-----------------------------------------------------------------
1165   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1166   //-----------------------------------------------------------------
1167
1168   AliTPCParam * fTPCParam = &(fDigParam->GetParam());
1169   Float_t sigma_rphi,sigma_z,cl_rphi,cl_z;
1170   //
1171   TParticle *particle; // pointer to a given particle
1172   AliTPChit *tpcHit; // pointer to a sigle TPC hit
1173   TClonesArray *Particles; //pointer to the particle list
1174   Int_t sector,nhits;
1175   Int_t ipart;
1176   Float_t xyz[5];
1177   Float_t pl,pt,tanth,rpad,ratio;
1178   Float_t cph,sph;
1179   
1180   //---------------------------------------------------------------
1181   //  Get the access to the tracks 
1182   //---------------------------------------------------------------
1183   
1184   TTree *TH = gAlice->TreeH();
1185   Stat_t ntracks = TH->GetEntries();
1186   Particles=gAlice->Particles();
1187   
1188   //------------------------------------------------------------
1189   // Loop over all sectors (72 sectors)
1190   // Sectors 0-35 are lower sectors, 0-17 z>0, 18-35 z<0
1191   // Sectors 36-71 are upper sectors, 36-53 z>0, 54-71 z<0
1192   //
1193   // First cluster for sector 0 starts at "0"
1194   //------------------------------------------------------------
1195   
1196   
1197   //fClustersIndex[0] = 0;
1198   
1199   //
1200   int Nsectors=fDigParam->GetParam().GetNSector();
1201   for(Int_t isec=0; isec<Nsectors; isec++){
1202     //MI change
1203     fTPCParam->AdjustAngles(isec,cph,sph);
1204     
1205     //------------------------------------------------------------
1206     // Loop over tracks
1207     //------------------------------------------------------------
1208     
1209     for(Int_t track=0;track<ntracks;track++){
1210       ResetHits();
1211       TH->GetEvent(track);
1212       //
1213       //  Get number of the TPC hits and a pointer
1214       //  to the particles
1215       //
1216       nhits=fHits->GetEntriesFast();
1217       //
1218       // Loop over hits
1219       //
1220       for(Int_t hit=0;hit<nhits;hit++){
1221         tpcHit=(AliTPChit*)fHits->UncheckedAt(hit);
1222         sector=tpcHit->fSector; // sector number
1223         if(sector != isec) continue; //terminate iteration
1224         ipart=tpcHit->fTrack;
1225         particle=(TParticle*)Particles->UncheckedAt(ipart);
1226         pl=particle->Pz();
1227         pt=particle->Pt();
1228         if(pt < 1.e-9) pt=1.e-9;
1229         tanth=pl/pt;
1230         tanth = TMath::Abs(tanth);
1231         rpad=TMath::Sqrt(tpcHit->fX*tpcHit->fX + tpcHit->fY*tpcHit->fY);
1232         ratio=0.001*rpad/pt; // pt must be in MeV/c - historical reason
1233         
1234         //   space-point resolutions
1235         
1236         sigma_rphi=SigmaY2(rpad,tanth,pt);
1237         sigma_z   =SigmaZ2(rpad,tanth   );
1238         
1239         //   cluster widths
1240         
1241         cl_rphi=ac_rphi-bc_rphi*rpad*tanth+cc_rphi*ratio*ratio;
1242         cl_z=ac_z-bc_z*rpad*tanth+cc_z*tanth*tanth;
1243         
1244         // temporary protection
1245         
1246         if(sigma_rphi < 0.) sigma_rphi=0.4e-3;
1247         if(sigma_z < 0.) sigma_z=0.4e-3;
1248         if(cl_rphi < 0.) cl_rphi=2.5e-3;
1249         if(cl_z < 0.) cl_z=2.5e-5;
1250         
1251         //
1252         // smearing --> rotate sectors firstly,
1253         // then the inaccuracy in a X-Y plane is only along Y (pad row)!
1254         //
1255         Float_t xprim= tpcHit->fX*cph + tpcHit->fY*sph;
1256         Float_t yprim=-tpcHit->fX*sph + tpcHit->fY*cph;
1257         xyz[0]=gRandom->Gaus(yprim,TMath::Sqrt(sigma_rphi));   // y
1258         Double_t alpha=(sector < fTPCParam->GetNInnerSector()) ?
1259         fTPCParam->GetInnerAngle() : fTPCParam->GetOuterAngle();
1260         if (TMath::Abs(xyz[0]/xprim) > TMath::Tan(0.5*alpha)) xyz[0]=yprim;
1261         xyz[1]=gRandom->Gaus(tpcHit->fZ,TMath::Sqrt(sigma_z)); // z 
1262         if (TMath::Abs(xyz[1]) > 250) xyz[1]=tpcHit->fZ;
1263         xyz[2]=tpcHit->fQ+1;// q; let it be not equal to zero (Y.Belikov)
1264         xyz[3]=sigma_rphi;                                     // fSigmaY2
1265         xyz[4]=sigma_z;                                        // fSigmaZ2
1266                 
1267         // and finally add the cluster
1268         Int_t tracks[5]={tpcHit->fTrack, -1, -1, sector, tpcHit->fPadRow};
1269         AddCluster(xyz,tracks);
1270         
1271       } // end of loop over hits
1272     }   // end of loop over tracks 
1273     
1274     //fClustersIndex[isec] = fNclusters; // update clusters index
1275     
1276   } // end of loop over sectors
1277   
1278   //fClustersIndex[fNsectors]--; // set end of the clusters buffer
1279   
1280 }
1281
1282
1283 void AliTPC::Hits2Digits()  
1284
1285
1286  //----------------------------------------------------
1287   // Loop over all sectors (72 sectors)
1288   // Sectors 0-35 are lower sectors, 0-17 z>0, 18-35 z<0
1289   // Sectors 36-71 are upper sectors, 36-53 z>0, 54-71 z<0
1290   //----
1291   int Nsectors=fDigParam->GetParam().GetNSector();
1292   for(Int_t isec=0;isec<Nsectors;isec++)  Hits2DigitsSector(isec);
1293 }
1294
1295
1296 //_____________________________________________________________________________
1297 void AliTPC::Hits2DigitsSector(Int_t isec)
1298 {
1299   //-------------------------------------------------------------------
1300   // TPC conversion from hits to digits.
1301   //------------------------------------------------------------------- 
1302
1303   //-----------------------------------------------------------------
1304   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1305   //-----------------------------------------------------------------
1306
1307   //-------------------------------------------------------
1308   //  Get the access to the track hits
1309   //-------------------------------------------------------
1310
1311   AliTPCParam * fTPCParam = &(fDigParam->GetParam());
1312   TTree *TH = gAlice->TreeH(); // pointer to the hits tree
1313   Stat_t ntracks = TH->GetEntries();
1314
1315   if( ntracks > 0){
1316
1317   //------------------------------------------- 
1318   //  Only if there are any tracks...
1319   //-------------------------------------------
1320
1321     
1322     // TObjArrays for three neighouring pad-rows
1323
1324     TObjArray **rowTriplet = new TObjArray* [3]; 
1325     
1326     // TObjArray-s for each pad-row
1327
1328     TObjArray **row;
1329       
1330     for (Int_t trip=0;trip<3;trip++){  
1331       rowTriplet[trip]=new TObjArray;
1332     }
1333
1334
1335     
1336       printf("*** Processing sector number %d ***\n",isec);
1337
1338       Int_t nrows =fTPCParam->GetNRow(isec);
1339
1340       row= new TObjArray* [nrows];
1341     
1342       MakeSector(isec,nrows,TH,ntracks,row);
1343
1344       //--------------------------------------------------------
1345       //   Digitize this sector, row by row
1346       //   row[i] is the pointer to the TObjArray of TVectors,
1347       //   each one containing electrons accepted on this
1348       //   row, assigned into tracks
1349       //--------------------------------------------------------
1350
1351       Int_t i;
1352
1353       for (i=0;i<nrows;i++){
1354
1355         // Triplets for i = 0 and i=1 are identical!
1356         // The same for i = nrows-1 and nrows!
1357
1358         if(i != 1 && i != nrows-1){
1359            MakeTriplet(i,rowTriplet,row);
1360          }
1361
1362             DigitizeRow(i,isec,rowTriplet);
1363
1364             fDigParam->Fill();
1365
1366             Int_t ndig=fDigParam->GetArray()->GetEntriesFast();
1367
1368             printf("*** Sector, row, digits %d %d %d ***\n",isec,i,ndig);
1369
1370             ResetDigits(); // reset digits for this row after storing them
1371              
1372        } // end of the sector digitization
1373      
1374        // delete the last triplet
1375
1376        for (i=0;i<3;i++) rowTriplet[i]->Delete();
1377           
1378        delete [] row; // delete the array of pointers to TObjArray-s
1379         
1380   } // ntracks >0
1381 } // end of Hits2Digits
1382 //_____________________________________________________________________________
1383 void AliTPC::MakeTriplet(Int_t row,
1384                          TObjArray **rowTriplet, TObjArray **prow)
1385 {
1386   //------------------------------------------------------------------
1387   //  Makes the "triplet" of the neighbouring pad-row for the
1388   //  digitization including the cross-talk between the pad-rows
1389   //------------------------------------------------------------------
1390
1391   //-----------------------------------------------------------------
1392   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1393   //-----------------------------------------------------------------
1394
1395   AliTPCParam * fTPCParam = &(fDigParam->GetParam());
1396   Float_t gasgain = fTPCParam->GetGasGain();
1397   Int_t nTracks[3];
1398
1399   Int_t nElements,nElectrons;
1400
1401   TVector *pv;
1402   TVector *tv;
1403
1404   //-------------------------------------------------------------------
1405   // pv is an "old" track, i.e. label + triplets of (x,y,z) 
1406   // for each electron
1407   //
1408   //-------------------------------------------------------------------
1409
1410
1411   Int_t i1,i2;
1412   Int_t nel,nt;
1413
1414   if(row == 0 || row == 1){
1415
1416   // create entire triplet for the first AND the second row
1417
1418     nTracks[0] = prow[0]->GetEntries();
1419     nTracks[1] = prow[1]->GetEntries();
1420     nTracks[2] = prow[2]->GetEntries();
1421
1422     for(i1=0;i1<3;i1++){
1423       nt = nTracks[i1]; // number of tracks for this row
1424
1425       for(i2=0;i2<nt;i2++){
1426         pv = (TVector*)prow[i1]->At(i2);
1427         TVector &v1 = *pv;
1428         nElements = pv->GetNrows(); 
1429         nElectrons = (nElements-1)/3;
1430
1431         tv = new TVector(4*nElectrons+1); // create TVector for a modified track
1432         TVector &v2 = *tv;
1433         v2(0)=v1(0); //track label
1434
1435         for(nel=0;nel<nElectrons;nel++){        
1436           Int_t idx1 = nel*3;
1437           Int_t idx2 = nel*4;
1438           // Avalanche, including fluctuations
1439           Int_t aval = (Int_t) (-gasgain*TMath::Log(gRandom->Rndm()));
1440           v2(idx2+1)= v1(idx1+1);
1441           v2(idx2+2)= v1(idx1+2);
1442           v2(idx2+3)= v1(idx1+3);
1443           v2(idx2+4)= (Float_t)aval; // in number of electrons!        
1444         } // end of loop over electrons
1445         //
1446         //  Add this track to a row 
1447         //
1448
1449         rowTriplet[i1]->Add(tv); 
1450
1451
1452       } // end of loop over tracks for this row
1453
1454       prow[i1]->Delete(); // remove "old" tracks
1455       delete prow[i1]; // delete  TObjArray for this row
1456       prow[i1]=0; // set pointer to NULL
1457
1458     } // end of loop over row triplets
1459
1460
1461   }
1462   else{
1463    
1464     rowTriplet[0]->Delete(); // remove old lower row
1465
1466     nTracks[0]=rowTriplet[1]->GetEntries(); // previous middle row
1467     nTracks[1]=rowTriplet[2]->GetEntries(); // previous upper row
1468     nTracks[2]=prow[row+1]->GetEntries(); // next row
1469     
1470
1471     //------------------------------------------- 
1472     //  shift new tracks downwards
1473     //-------------------------------------------
1474
1475     for(i1=0;i1<nTracks[0];i1++){
1476       pv=(TVector*)rowTriplet[1]->At(i1);
1477       rowTriplet[0]->Add(pv); 
1478     }       
1479
1480     rowTriplet[1]->Clear(); // leave tracks on the heap!!!
1481
1482     for(i1=0;i1<nTracks[1];i1++){
1483       pv=(TVector*)rowTriplet[2]->At(i1);
1484       rowTriplet[1]->Add(pv);
1485     }
1486
1487     rowTriplet[2]->Clear(); // leave tracks on the heap!!!
1488
1489     //---------------------------------------------
1490     //  Create new upper row
1491     //---------------------------------------------
1492
1493     
1494
1495     for(i1=0;i1<nTracks[2];i1++){
1496         pv = (TVector*)prow[row+1]->At(i1);
1497         TVector &v1 = *pv;
1498         nElements = pv->GetNrows();
1499         nElectrons = (nElements-1)/3;
1500
1501         tv = new TVector(4*nElectrons+1); // create TVector for a modified track
1502         TVector &v2 = *tv;
1503         v2(0)=v1(0); //track label
1504
1505         for(nel=0;nel<nElectrons;nel++){
1506         
1507           Int_t idx1 = nel*3;
1508           Int_t idx2 = nel*4;
1509           // Avalanche, including fluctuations
1510           Int_t aval = (Int_t) 
1511             (-gasgain*TMath::Log(gRandom->Rndm()));
1512           
1513           v2(idx2+1)= v1(idx1+1); 
1514           v2(idx2+2)= v1(idx1+2);
1515           v2(idx2+3)= v1(idx1+3);
1516           v2(idx2+4)= (Float_t)aval; // in number of electrons!        
1517         } // end of loop over electrons
1518
1519           rowTriplet[2]->Add(tv);
1520      
1521     } // end of loop over tracks
1522          
1523     prow[row+1]->Delete(); // delete tracks for this row
1524     delete prow[row+1];  // delete TObjArray for this row
1525     prow[row+1]=0; // set a pointer to NULL
1526
1527   }
1528
1529 }  // end of MakeTriplet
1530 //_____________________________________________________________________________
1531 void AliTPC::ExB(Float_t *xyz)
1532 {
1533   //------------------------------------------------
1534   //  ExB at the wires and wire number calulation
1535   //------------------------------------------------
1536   
1537   //-----------------------------------------------------------------
1538   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1539   //-----------------------------------------------------------------
1540    AliTPCParam * fTPCParam = &(fDigParam->GetParam());
1541
1542    Float_t x1=xyz[0];
1543    fTPCParam->GetWire(x1);        //calculate nearest wire position
1544    Float_t dx=xyz[0]-x1;
1545    xyz[1]+=dx*fTPCParam->GetOmegaTau();
1546
1547 } // end of ExB
1548 //_____________________________________________________________________________
1549 void AliTPC::DigitizeRow(Int_t irow,Int_t isec,TObjArray **rowTriplet)
1550 {
1551   //-----------------------------------------------------------
1552   // Single row digitization, coupling from the neighbouring
1553   // rows taken into account
1554   //-----------------------------------------------------------
1555
1556   //-----------------------------------------------------------------
1557   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1558   //-----------------------------------------------------------------
1559  
1560
1561   AliTPCParam * fTPCParam = &(fDigParam->GetParam());
1562   Float_t chipgain= fTPCParam->GetChipGain();
1563   Float_t zerosup = fTPCParam->GetZeroSup();
1564   Int_t nrows =fTPCParam->GetNRow(isec);
1565   const int MAXTBKT=
1566   int((z_end+6*fTPCParam->GetZSigma())/fTPCParam->GetZWidth())+1;
1567   
1568   Int_t nTracks[3];
1569   Int_t n_of_pads[3];
1570   Int_t IndexRange[4];
1571   Int_t i1;
1572   Int_t iFlag; 
1573
1574   //
1575   // iFlag = 0 -> inner row, iFlag = 1 -> the middle one, iFlag = 2 -> the outer one
1576   //
1577
1578   nTracks[0]=rowTriplet[0]->GetEntries(); // lower row
1579   nTracks[1]=rowTriplet[1]->GetEntries(); // middle row
1580   nTracks[2]=rowTriplet[2]->GetEntries(); // upper row
1581
1582     
1583   if(irow == 0){
1584     iFlag=0;
1585     n_of_pads[0]=fTPCParam->GetNPads(isec,0);
1586     n_of_pads[1]=fTPCParam->GetNPads(isec,1);
1587   }
1588   else if(irow == nrows-1){
1589      iFlag=2;
1590      n_of_pads[1]=fTPCParam->GetNPads(isec,irow-1);
1591      n_of_pads[2]=fTPCParam->GetNPads(isec,irow);
1592   }
1593   else {
1594     iFlag=1;
1595     for(i1=0;i1<3;i1++){
1596        n_of_pads[i1]=fTPCParam->GetNPads(isec,irow-1+i1);
1597     }
1598   }
1599  
1600   //
1601   //  Integrated signal for this row
1602   //  and a single track signal
1603   // 
1604    
1605   TMatrix *m1   = new TMatrix(0,n_of_pads[iFlag]-1,0,MAXTBKT-1); // integrated
1606   TMatrix *m2   = new TMatrix(0,n_of_pads[iFlag]-1,0,MAXTBKT-1); // single
1607
1608   //
1609
1610   TMatrix &Total  = *m1;
1611
1612   //  Array of pointers to the label-signal list
1613
1614   Int_t NofDigits = n_of_pads[iFlag]*MAXTBKT; // number of digits for this row
1615
1616   Float_t  **pList = new Float_t* [NofDigits]; 
1617
1618   Int_t lp;
1619
1620   for(lp=0;lp<NofDigits;lp++)pList[lp]=0; // set all pointers to NULL
1621
1622   //
1623   //  Straight signal and cross-talk, cross-talk is integrated over all
1624   //  tracks and added to the signal at the very end
1625   //
1626    
1627
1628   for (i1=0;i1<nTracks[iFlag];i1++){
1629
1630     m2->Zero(); // clear single track signal matrix
1631   
1632     Float_t TrackLabel = 
1633       GetSignal(rowTriplet[iFlag],i1,n_of_pads[iFlag],m2,m1,IndexRange); 
1634
1635     GetList(TrackLabel,n_of_pads[iFlag],m2,IndexRange,pList);
1636
1637   }
1638
1639   // 
1640   //  Cross talk from the neighbouring pad-rows
1641   //
1642
1643   TMatrix *m3 =  new TMatrix(0,n_of_pads[iFlag]-1,0,MAXTBKT-1); // cross-talk
1644
1645   TMatrix &Cross = *m3;
1646
1647   if(iFlag == 0){
1648
1649     // cross-talk from the outer row only (first pad row)
1650
1651     GetCrossTalk(0,rowTriplet[1],nTracks[1],n_of_pads,m3);
1652
1653   }
1654   else if(iFlag == 2){
1655
1656     // cross-talk from the inner row only (last pad row)
1657
1658     GetCrossTalk(2,rowTriplet[1],nTracks[1],n_of_pads,m3);
1659
1660   }
1661   else{
1662
1663     // cross-talk from both inner and outer rows
1664
1665     GetCrossTalk(3,rowTriplet[0],nTracks[0],n_of_pads,m3); // inner
1666     GetCrossTalk(4,rowTriplet[2],nTracks[2],n_of_pads,m3); //outer
1667   }
1668
1669   Total += Cross; // add the cross-talk
1670
1671   //
1672   //  Convert analog signal to ADC counts
1673   //
1674    
1675   Int_t tracks[3];
1676   Int_t digits[5];
1677
1678
1679   for(Int_t ip=0;ip<n_of_pads[iFlag];ip++){
1680     for(Int_t it=0;it<MAXTBKT;it++){
1681
1682       Float_t q = Total(ip,it);
1683
1684       Int_t gi =it*n_of_pads[iFlag]+ip; // global index
1685
1686       q = gRandom->Gaus(q,fTPCParam->GetNoise()); // apply noise
1687       q *= (q_el*1.e15); // convert to fC
1688       q *= chipgain; // convert to mV   
1689       q *= (adc_sat/dyn_range); // convert to ADC counts  
1690
1691       if(q <zerosup) continue; // do not fill zeros
1692       if(q > adc_sat) q = adc_sat;  // saturation
1693
1694       //
1695       //  "real" signal or electronic noise (list = -1)?
1696       //    
1697
1698       for(Int_t j1=0;j1<3;j1++){
1699         tracks[j1] = (pList[gi]) ?(Int_t)(*(pList[gi]+j1)) : -1;
1700       }
1701
1702       digits[0]=isec;
1703       digits[1]=irow;
1704       digits[2]=ip;
1705       digits[3]=it;
1706       digits[4]= (Int_t)q;
1707
1708       //  Add this digit
1709
1710       AddDigit(tracks,digits);
1711     
1712     } // end of loop over time buckets
1713   }  // end of lop over pads 
1714
1715   //
1716   //  This row has been digitized, delete nonused stuff
1717   //
1718
1719   for(lp=0;lp<NofDigits;lp++){
1720     if(pList[lp]) delete [] pList[lp];
1721   }
1722   
1723   delete [] pList;
1724
1725   delete m1;
1726   delete m2;
1727   delete m3;
1728
1729 } // end of DigitizeRow
1730 //_____________________________________________________________________________
1731 Float_t AliTPC::GetSignal(TObjArray *p1, Int_t ntr, Int_t np, TMatrix *m1, TMatrix *m2,
1732                           Int_t *IndexRange)
1733 {
1734
1735   //---------------------------------------------------------------
1736   //  Calculates 2-D signal (pad,time) for a single track,
1737   //  returns a pointer to the signal matrix and the track label 
1738   //  No digitization is performed at this level!!!
1739   //---------------------------------------------------------------
1740
1741   //-----------------------------------------------------------------
1742   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1743   //-----------------------------------------------------------------
1744
1745   TVector *tv;
1746   AliTPCParam * fTPCParam = &(fDigParam->GetParam());
1747   AliTPCPRF2D * fPRF2D = &(fDigParam->GetPRF2D());
1748   AliTPCRF1D  * fRF    = &(fDigParam->GetRF()); 
1749   const int MAXTBKT=
1750   int((z_end+6*fTPCParam->GetZSigma())/fTPCParam->GetZWidth())+1;
1751  
1752   //to make the code faster we put parameters  to the stack
1753
1754   Float_t zwidth  = fTPCParam->GetZWidth();
1755   Float_t zwidthm1  =1./zwidth;
1756
1757   tv = (TVector*)p1->At(ntr); // pointer to a track
1758   TVector &v = *tv;
1759   
1760   Float_t label = v(0);
1761
1762   Int_t CentralPad = (np-1)/2;
1763   Int_t PadNumber;
1764   Int_t nElectrons = (tv->GetNrows()-1)/4;
1765   Float_t range=((np-1)/2 + 0.5)*fTPCParam->GetPadPitchWidth(); // pad range
1766
1767   range -= 0.5; // dead zone, 5mm from the edge, according to H.G. Fischer
1768   
1769   Float_t IneffFactor = 0.5; // inefficiency in the gain close to the edge, as above
1770
1771
1772   Float_t PadSignal[7]; // signal from a single electron
1773
1774   TMatrix &signal = *m1;
1775   TMatrix &total = *m2;
1776
1777
1778   IndexRange[0]=9999; // min pad
1779   IndexRange[1]=-1; // max pad
1780   IndexRange[2]=9999; //min time
1781   IndexRange[3]=-1; // max time
1782
1783   //
1784   //  Loop over all electrons
1785   //
1786
1787   for(Int_t nel=0; nel<nElectrons; nel++){
1788    Int_t idx=nel*4;
1789    Float_t xwire = v(idx+1);
1790    Float_t y = v(idx+2);
1791    Float_t z = v(idx+3);
1792
1793
1794    Float_t absy=TMath::Abs(y);
1795    
1796    if(absy < 0.5*fTPCParam->GetPadPitchWidth()){
1797      PadNumber=CentralPad;
1798    }
1799    else if (absy < range){
1800      PadNumber=(Int_t) ((absy-0.5*fTPCParam->GetPadPitchWidth())/fTPCParam->GetPadPitchWidth()+1.);
1801      PadNumber=(Int_t) (TMath::Sign((Float_t)PadNumber, y)+CentralPad);
1802    }
1803    else continue; // electron out of pad-range , lost at the sector edge
1804     
1805    Float_t aval = (absy<range-0.5) ? v(idx+4):v(idx+4)*IneffFactor;
1806    
1807
1808    Float_t dist = y - (Float_t)(PadNumber-CentralPad)*fTPCParam->GetPadPitchWidth();
1809    for (Int_t i=0;i<7;i++){
1810      PadSignal[i]=fPRF2D->GetPRF(-dist+(i-3)*fTPCParam->GetPadPitchWidth(),xwire)*aval;
1811      PadSignal[i] *= fTPCParam->GetPadCoupling();
1812    }
1813
1814    Int_t  LeftPad = TMath::Max(0,PadNumber-3);
1815    Int_t  RightPad = TMath::Min(np-1,PadNumber+3);
1816
1817    Int_t pmin=LeftPad-PadNumber+3; // lower index of the pad_signal vector
1818    Int_t pmax=RightPad-PadNumber+3; // upper index     
1819    
1820    Float_t z_drift = z*zwidthm1;
1821    Float_t z_offset = z_drift-(Int_t)z_drift;
1822
1823    Int_t FirstBucket = (Int_t)z_drift; // numbering starts from "0"
1824
1825
1826    // loop over time bins (4 bins is enough - 3 sigma truncated Gaussian)
1827
1828    for (Int_t i2=0;i2<4;i2++){          
1829      Int_t TrueTime = FirstBucket+i2; // current time bucket
1830      Float_t dz   = (Float_t(i2)+1.-z_offset)*zwidth; 
1831      Float_t ampl = fRF->GetRF(dz); 
1832      if( (TrueTime>MAXTBKT-1) ) break; // beyond the time range
1833      
1834      IndexRange[2]=TMath::Min(IndexRange[2],TrueTime); // min time
1835      IndexRange[3]=TMath::Max(IndexRange[3],TrueTime); // max time
1836
1837      // loop over pads, from pmin to pmax
1838      for(Int_t i3=pmin;i3<=pmax;i3++){
1839        Int_t TruePad = LeftPad+i3-pmin;
1840        IndexRange[0]=TMath::Min(IndexRange[0],TruePad); // min pad
1841        IndexRange[1]=TMath::Max(IndexRange[1],TruePad); // max pad
1842        signal(TruePad,TrueTime)+=(PadSignal[i3]*ampl); // not converted to charge!!!
1843        total(TruePad,TrueTime)+=(PadSignal[i3]*ampl); // not converted to charge!!!
1844      } // end of pads loop
1845    }  // end of time loop
1846   } // end of loop over electrons
1847
1848   return label; // returns track label when finished
1849 }
1850
1851 //_____________________________________________________________________________
1852 void AliTPC::GetList(Float_t label,Int_t np,TMatrix *m,Int_t *IndexRange,
1853                      Float_t **pList)
1854 {
1855   //----------------------------------------------------------------------
1856   //  Updates the list of tracks contributing to digits for a given row
1857   //----------------------------------------------------------------------
1858
1859   //-----------------------------------------------------------------
1860   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1861   //-----------------------------------------------------------------
1862
1863   TMatrix &signal = *m;
1864
1865   // lop over nonzero digits
1866
1867   for(Int_t it=IndexRange[2];it<IndexRange[3]+1;it++){
1868     for(Int_t ip=IndexRange[0];ip<IndexRange[1]+1;ip++){
1869
1870
1871         Int_t GlobalIndex = it*np+ip; // GlobalIndex starts from 0!
1872         
1873         if(!pList[GlobalIndex]){
1874         
1875           // 
1876           // Create new list (6 elements - 3 signals and 3 labels),
1877           // but only if the signal is > 0. 
1878           //
1879
1880           if(signal(ip,it)>0.){
1881
1882           pList[GlobalIndex] = new Float_t [6];
1883
1884           // set list to -1 
1885
1886           *pList[GlobalIndex] = -1.;
1887           *(pList[GlobalIndex]+1) = -1.;
1888           *(pList[GlobalIndex]+2) = -1.;
1889           *(pList[GlobalIndex]+3) = -1.;
1890           *(pList[GlobalIndex]+4) = -1.;
1891           *(pList[GlobalIndex]+5) = -1.;
1892
1893
1894           *pList[GlobalIndex] = label;
1895           *(pList[GlobalIndex]+3) = signal(ip,it);}
1896         }
1897         else{
1898
1899           // check the signal magnitude
1900
1901           Float_t highest = *(pList[GlobalIndex]+3);
1902           Float_t middle = *(pList[GlobalIndex]+4);
1903           Float_t lowest = *(pList[GlobalIndex]+5);
1904
1905           //
1906           //  compare the new signal with already existing list
1907           //
1908
1909           if(signal(ip,it)<lowest) continue; // neglect this track
1910
1911           //
1912
1913           if (signal(ip,it)>highest){
1914             *(pList[GlobalIndex]+5) = middle;
1915             *(pList[GlobalIndex]+4) = highest;
1916             *(pList[GlobalIndex]+3) = signal(ip,it);
1917
1918             *(pList[GlobalIndex]+2) = *(pList[GlobalIndex]+1);
1919             *(pList[GlobalIndex]+1) = *pList[GlobalIndex];
1920             *pList[GlobalIndex] = label;
1921           }
1922           else if (signal(ip,it)>middle){
1923             *(pList[GlobalIndex]+5) = middle;
1924             *(pList[GlobalIndex]+4) = signal(ip,it);
1925
1926             *(pList[GlobalIndex]+2) = *(pList[GlobalIndex]+1);
1927             *(pList[GlobalIndex]+1) = label;
1928           }
1929           else{
1930             *(pList[GlobalIndex]+5) = signal(ip,it);
1931             *(pList[GlobalIndex]+2) = label;
1932           }
1933         }
1934
1935     } // end of loop over pads
1936   } // end of loop over time bins
1937
1938
1939
1940
1941 }//end of GetList
1942 //___________________________________________________________________
1943 void AliTPC::MakeSector(Int_t isec,Int_t nrows,TTree *TH,
1944                         Stat_t ntracks,TObjArray **row)
1945 {
1946
1947   //-----------------------------------------------------------------
1948   // Prepares the sector digitization, creates the vectors of
1949   // tracks for each row of this sector. The track vector
1950   // contains the track label and the position of electrons.
1951   //-----------------------------------------------------------------
1952
1953   //-----------------------------------------------------------------
1954   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
1955   //-----------------------------------------------------------------
1956
1957   AliTPCParam * fTPCParam = &(fDigParam->GetParam());
1958   Int_t i;
1959   Float_t xyz[3]; 
1960
1961   AliTPChit *tpcHit; // pointer to a sigle TPC hit    
1962  
1963   //----------------------------------------------
1964   // Create TObjArray-s, one for each row,
1965   // each TObjArray will store the TVectors
1966   // of electrons, one TVector per each track.
1967   //---------------------------------------------- 
1968     
1969   for(i=0; i<nrows; i++){
1970     row[i] = new TObjArray;
1971   }
1972   Int_t *n_of_electrons = new Int_t [nrows]; // electron counter for each row
1973   TVector **tr = new TVector* [nrows]; //pointers to the track vectors
1974
1975   //--------------------------------------------------------------------
1976   //  Loop over tracks, the "track" contains the full history
1977   //--------------------------------------------------------------------
1978
1979   Int_t previousTrack,currentTrack;
1980   previousTrack = -1; // nothing to store so far!
1981
1982   for(Int_t track=0;track<ntracks;track++){
1983
1984     ResetHits();
1985
1986     TH->GetEvent(track); // get next track
1987     Int_t nhits = fHits->GetEntriesFast(); // get number of hits for this track
1988
1989     if(nhits == 0) continue; // no hits in the TPC for this track
1990
1991     //--------------------------------------------------------------
1992     //  Loop over hits
1993     //--------------------------------------------------------------
1994
1995     for(Int_t hit=0;hit<nhits;hit++){
1996
1997       tpcHit = (AliTPChit*)fHits->UncheckedAt(hit); // get a pointer to a hit
1998       
1999       Int_t sector=tpcHit->fSector; // sector number
2000       if(sector != isec) continue; //terminate iteration
2001
2002         currentTrack = tpcHit->fTrack; // track number
2003         if(currentTrack != previousTrack){
2004                           
2005            // store already filled fTrack
2006               
2007            for(i=0;i<nrows;i++){
2008              if(previousTrack != -1){
2009                if(n_of_electrons[i]>0){
2010                  TVector &v = *tr[i];
2011                  v(0) = previousTrack;
2012                  tr[i]->ResizeTo(3*n_of_electrons[i]+1); // shrink if necessary
2013                  row[i]->Add(tr[i]);                     
2014                }
2015                else{
2016                  delete tr[i]; // delete empty TVector
2017                  tr[i]=0;
2018                }
2019              }
2020
2021              n_of_electrons[i]=0;
2022              tr[i] = new TVector(361); // TVectors for the next fTrack
2023
2024            } // end of loop over rows
2025                
2026            previousTrack=currentTrack; // update track label 
2027         }
2028            
2029         Int_t QI = (Int_t) (tpcHit->fQ); // energy loss (number of electrons)
2030
2031        //---------------------------------------------------
2032        //  Calculate the electron attachment probability
2033        //---------------------------------------------------
2034
2035         Float_t time = 1.e6*(z_end-TMath::Abs(tpcHit->fZ))/fTPCParam->GetDriftV(); 
2036         // in microseconds!     
2037         Float_t AttProb = fTPCParam->GetAttCoef()*
2038           fTPCParam->GetOxyCont()*time; //  fraction! 
2039    
2040         //-----------------------------------------------
2041         //  Loop over electrons
2042         //-----------------------------------------------
2043
2044         for(Int_t nel=0;nel<QI;nel++){
2045           // skip if electron lost due to the attachment
2046           if((gRandom->Rndm(0)) < AttProb) continue; // electron lost!
2047           xyz[0]=tpcHit->fX;
2048           xyz[1]=tpcHit->fY;
2049           xyz[2]=tpcHit->fZ;
2050           ElDiff(xyz); // Appply the diffusion
2051           Int_t row_number;
2052           fTPCParam->XYZtoCRXYZ(xyz,isec,row_number,3);
2053
2054           //transform position to local coordinates
2055           //option 3 means that we calculate x position relative to 
2056           //nearest pad row 
2057
2058           if ((row_number<0)||row_number>=fTPCParam->GetNRow(isec)) continue;
2059           ExB(xyz); // ExB effect at the sense wires
2060           n_of_electrons[row_number]++;   
2061           //----------------------------------
2062           // Expand vector if necessary
2063           //----------------------------------
2064           if(n_of_electrons[row_number]>120){
2065             Int_t range = tr[row_number]->GetNrows();
2066             if(n_of_electrons[row_number] > (range-1)/3){
2067               tr[row_number]->ResizeTo(range+150); // Add 50 electrons
2068             }
2069           }
2070           
2071           TVector &v = *tr[row_number];
2072           Int_t idx = 3*n_of_electrons[row_number]-2;
2073
2074           v(idx)=  xyz[0];   // X
2075           v(idx+1)=xyz[1];   // Y (along the pad-row)
2076           v(idx+2)=xyz[2];   // Z
2077             
2078         } // end of loop over electrons
2079         
2080       } // end of loop over hits
2081     } // end of loop over tracks
2082
2083     //
2084     //   store remaining track (the last one) if not empty
2085     //
2086
2087      for(i=0;i<nrows;i++){
2088        if(n_of_electrons[i]>0){
2089           TVector &v = *tr[i];
2090           v(0) = previousTrack;
2091           tr[i]->ResizeTo(3*n_of_electrons[i]+1); // shrink if necessary
2092           row[i]->Add(tr[i]);  
2093         }
2094         else{
2095           delete tr[i];
2096           tr[i]=0;
2097         }  
2098       }  
2099
2100           delete [] tr;
2101           delete [] n_of_electrons; 
2102
2103 } // end of MakeSector
2104 //_____________________________________________________________________________
2105 void AliTPC::GetCrossTalk (Int_t iFlag,TObjArray *p,Int_t ntracks,Int_t *npads,
2106                            TMatrix *m)
2107 {
2108
2109   //-------------------------------------------------------------
2110   // Calculates the cross-talk from one row (inner or outer)
2111   //-------------------------------------------------------------
2112
2113   //-----------------------------------------------------------------
2114   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
2115   //-----------------------------------------------------------------
2116
2117   //
2118   // iFlag=2 & 3 --> cross-talk from the inner row
2119   // iFlag=0 & 4 --> cross-talk from the outer row
2120   //
2121   AliTPCParam * fTPCParam = &(fDigParam->GetParam());
2122   AliTPCPRF2D * fPRF2D = &(fDigParam->GetPRF2D());
2123   AliTPCRF1D  * fRF    = &(fDigParam->GetRF()); 
2124   const int MAXTBKT=
2125   int((z_end+6*fTPCParam->GetZSigma())/fTPCParam->GetZWidth())+1;
2126  
2127   //to make code faster
2128
2129   Float_t zwidth  = fTPCParam->GetZWidth();
2130   Float_t zwidthm1  =1/fTPCParam->GetZWidth();
2131
2132  Int_t PadNumber;
2133  Float_t xwire;
2134
2135  Int_t nPadsSignal; // for this pads the signal is calculated
2136  Float_t range;     // sense wire range
2137  Int_t nPadsDiff;
2138
2139  Float_t IneffFactor=0.5; // gain inefficiency close to the edges
2140
2141  if(iFlag == 0){   
2142    // 1-->0
2143    nPadsSignal = npads[1];
2144    range = ((npads[1]-1)/2 + 0.5)*fTPCParam->GetPadPitchWidth();  
2145    nPadsDiff = (npads[1]-npads[0])/2;
2146  }  
2147  else if (iFlag == 2){
2148    // 1-->2
2149    nPadsSignal = npads[2];
2150    range = ((npads[1]-1)/2 + 0.5)*fTPCParam->GetPadPitchWidth();
2151    nPadsDiff = 0;
2152  }
2153  else if (iFlag == 3){
2154    // 0-->1
2155    nPadsSignal = npads[1];
2156    range = ((npads[0]-1)/2 + 0.5)*fTPCParam->GetPadPitchWidth();
2157    nPadsDiff = 0;
2158  }
2159  else{
2160    // 2-->1
2161    nPadsSignal = npads[2];
2162    range = ((npads[2]-1)/2 + 0.5)*fTPCParam->GetPadPitchWidth();
2163    nPadsDiff = (npads[2]-npads[1])/2;
2164  }
2165
2166  range-=0.5; // dead zone close to the edges
2167
2168  TVector *tv; 
2169  TMatrix &signal = *m;
2170
2171  Int_t CentralPad = (nPadsSignal-1)/2;
2172  Float_t PadSignal[7]; // signal from a single electron
2173  // Loop over tracks
2174  for(Int_t nt=0;nt<ntracks;nt++){
2175    tv=(TVector*)p->At(nt); // pointer to a track
2176    TVector &v = *tv;
2177    Int_t nElectrons = (tv->GetNrows()-1)/4;
2178    // Loop over electrons
2179    for(Int_t nel=0; nel<nElectrons; nel++){
2180      Int_t idx=nel*4;
2181      xwire=v(idx+1);
2182  
2183      if (iFlag==0) xwire+=fTPCParam->GetPadPitchLength();
2184      if (iFlag==2)  xwire-=fTPCParam->GetPadPitchLength();
2185      if (iFlag==3)  xwire-=fTPCParam->GetPadPitchLength();
2186      if (iFlag==4)  xwire+=fTPCParam->GetPadPitchLength();  
2187    
2188      //  electron acceptance for the cross-talk (only the closest wire)  
2189
2190      Float_t dxMax = fTPCParam->GetPadPitchLength()*0.5+fTPCParam->GetWWPitch();
2191      if(TMath::Abs(xwire)>dxMax) continue;
2192
2193      Float_t y = v(idx+2);
2194      Float_t z = v(idx+3);
2195      Float_t absy=TMath::Abs(y);
2196
2197      if(absy < 0.5*fTPCParam->GetPadPitchWidth()){
2198        PadNumber=CentralPad;
2199      }
2200      else if (absy < range){
2201        PadNumber=(Int_t) ((absy-0.5*fTPCParam->GetPadPitchWidth())/fTPCParam->GetPadPitchWidth() +1.);
2202        PadNumber=(Int_t) (TMath::Sign((Float_t)PadNumber, y)+CentralPad);
2203      }
2204      else continue; // electron out of sense wire range, lost at the sector edge
2205
2206      Float_t aval = (absy<range-0.5) ? v(idx+4):v(idx+4)*IneffFactor;
2207
2208      Float_t dist = y - (Float_t)(PadNumber-CentralPad)*fTPCParam->GetPadPitchWidth();
2209        
2210      for (Int_t i=0;i<7;i++){
2211        PadSignal[i]=fPRF2D->GetPRF(-dist+(i-3)*fTPCParam->GetPadPitchWidth(),xwire)*aval;
2212
2213        PadSignal[i] *= fTPCParam->GetPadCoupling();
2214      }
2215      // real pad range
2216
2217      Int_t  LeftPad = TMath::Max(0,PadNumber-3);
2218      Int_t  RightPad = TMath::Min(nPadsSignal-1,PadNumber+3);
2219
2220      Int_t pmin=LeftPad-PadNumber+3; // lower index of the pad_signal vector
2221      Int_t pmax=RightPad-PadNumber+3; // upper index  
2222
2223
2224      Float_t z_drift = z*zwidthm1;
2225      Float_t z_offset = z_drift-(Int_t)z_drift;
2226
2227      Int_t FirstBucket = (Int_t)z_drift; // numbering starts from "0"
2228
2229      for (Int_t i2=0;i2<4;i2++){     
2230        Int_t TrueTime = FirstBucket+i2; // current time bucket
2231        Float_t dz   = (Float_t(i2)+1.- z_offset)*zwidth; 
2232        Float_t ampl = fRF->GetRF(dz); 
2233        if((TrueTime>MAXTBKT-1)) break; // beyond the time range
2234
2235
2236        // loop over pads, from pmin to pmax
2237
2238        for(Int_t i3=pmin;i3<pmax+1;i3++){
2239          Int_t TruePad = LeftPad+i3-pmin;
2240
2241          if(TruePad<nPadsDiff || TruePad > nPadsSignal-nPadsDiff-1) continue;
2242
2243          TruePad -= nPadsDiff;
2244          signal(TruePad,TrueTime)+=(PadSignal[i3]*ampl); // not converted to charge!
2245
2246        } // end of loop over pads
2247      } // end of loop over time bins
2248
2249    } // end of loop over electrons 
2250
2251  } // end of loop over tracks
2252
2253 } // end of GetCrossTalk
2254
2255
2256
2257 //_____________________________________________________________________________
2258 void AliTPC::Init()
2259 {
2260   //
2261   // Initialise TPC detector after definition of geometry
2262   //
2263   Int_t i;
2264   //
2265   printf("\n");
2266   for(i=0;i<35;i++) printf("*");
2267   printf(" TPC_INIT ");
2268   for(i=0;i<35;i++) printf("*");
2269   printf("\n");
2270   //
2271   for(i=0;i<80;i++) printf("*");
2272   printf("\n");
2273 }
2274
2275 //_____________________________________________________________________________
2276 void AliTPC::MakeBranch(Option_t* option)
2277 {
2278   //
2279   // Create Tree branches for the TPC.
2280   //
2281   Int_t buffersize = 4000;
2282   char branchname[10];
2283   sprintf(branchname,"%s",GetName());
2284
2285   AliDetector::MakeBranch(option);
2286
2287   char *D = strstr(option,"D");
2288
2289   if (fDigits   && gAlice->TreeD() && D) {
2290     gAlice->TreeD()->Branch(branchname,&fDigits, buffersize);
2291     printf("Making Branch %s for digits\n",branchname);
2292   }     
2293
2294   char *R = strstr(option,"R");
2295
2296   if (fClusters && gAlice->TreeR() && R) {
2297     gAlice->TreeR()->Branch(branchname,&fClusters, buffersize);
2298     printf("Making Branch %s for Clusters\n",branchname);
2299   }     
2300 }
2301  
2302 //_____________________________________________________________________________
2303 void AliTPC::ResetDigits()
2304 {
2305   //
2306   // Reset number of digits and the digits array for this detector
2307   // reset clusters
2308   //
2309   fNdigits   = 0;
2310   //  if (fDigits)   fDigits->Clear();
2311   if (fDigParam->GetArray()!=0)  fDigParam->GetArray()->Clear();
2312   fNclusters = 0;
2313   if (fClusters) fClusters->Clear();
2314 }
2315
2316 //_____________________________________________________________________________
2317 void AliTPC::SetSecAL(Int_t sec)
2318 {
2319   //---------------------------------------------------
2320   // Activate/deactivate selection for lower sectors
2321   //---------------------------------------------------
2322
2323   //-----------------------------------------------------------------
2324   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
2325   //-----------------------------------------------------------------
2326
2327   fSecAL = sec;
2328 }
2329
2330 //_____________________________________________________________________________
2331 void AliTPC::SetSecAU(Int_t sec)
2332 {
2333   //----------------------------------------------------
2334   // Activate/deactivate selection for upper sectors
2335   //---------------------------------------------------
2336
2337   //-----------------------------------------------------------------
2338   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
2339   //-----------------------------------------------------------------
2340
2341   fSecAU = sec;
2342 }
2343
2344 //_____________________________________________________________________________
2345 void AliTPC::SetSecLows(Int_t s1,Int_t s2,Int_t s3,Int_t s4,Int_t s5, Int_t s6)
2346 {
2347   //----------------------------------------
2348   // Select active lower sectors
2349   //----------------------------------------
2350
2351   //-----------------------------------------------------------------
2352   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
2353   //-----------------------------------------------------------------
2354
2355   fSecLows[0] = s1;
2356   fSecLows[1] = s2;
2357   fSecLows[2] = s3;
2358   fSecLows[3] = s4;
2359   fSecLows[4] = s5;
2360   fSecLows[5] = s6;
2361 }
2362
2363 //_____________________________________________________________________________
2364 void AliTPC::SetSecUps(Int_t s1,Int_t s2,Int_t s3,Int_t s4,Int_t s5, Int_t s6,
2365                        Int_t s7, Int_t s8 ,Int_t s9 ,Int_t s10, 
2366                        Int_t s11 , Int_t s12)
2367 {
2368   //--------------------------------
2369   // Select active upper sectors
2370   //--------------------------------
2371
2372   //-----------------------------------------------------------------
2373   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
2374   //-----------------------------------------------------------------
2375
2376   fSecUps[0] = s1;
2377   fSecUps[1] = s2;
2378   fSecUps[2] = s3;
2379   fSecUps[3] = s4;
2380   fSecUps[4] = s5;
2381   fSecUps[5] = s6;
2382   fSecUps[6] = s7;
2383   fSecUps[7] = s8;
2384   fSecUps[8] = s9;
2385   fSecUps[9] = s10;
2386   fSecUps[10] = s11;
2387   fSecUps[11] = s12;
2388 }
2389
2390 //_____________________________________________________________________________
2391 void AliTPC::SetSens(Int_t sens)
2392 {
2393
2394   //-------------------------------------------------------------
2395   // Activates/deactivates the sensitive strips at the center of
2396   // the pad row -- this is for the space-point resolution calculations
2397   //-------------------------------------------------------------
2398
2399   //-----------------------------------------------------------------
2400   // Origin: Marek Kowalski  IFJ, Krakow, Marek.Kowalski@ifj.edu.pl
2401   //-----------------------------------------------------------------
2402
2403   fSens = sens;
2404 }
2405  
2406 void AliTPC::SetSide(Float_t side)
2407 {
2408   fSide = side;
2409  
2410 }
2411 //____________________________________________________________________________
2412 void AliTPC::SetGasMixt(Int_t nc,Int_t c1,Int_t c2,Int_t c3,Float_t p1,
2413                            Float_t p2,Float_t p3)
2414 {
2415
2416  fNoComp = nc;
2417  
2418  fMixtComp[0]=c1;
2419  fMixtComp[1]=c2;
2420  fMixtComp[2]=c3;
2421
2422  fMixtProp[0]=p1;
2423  fMixtProp[1]=p2;
2424  fMixtProp[2]=p3; 
2425  
2426  
2427 }
2428 //_____________________________________________________________________________
2429 void AliTPC::Streamer(TBuffer &R__b)
2430 {
2431   //
2432   // Stream an object of class AliTPC.
2433   //
2434    if (R__b.IsReading()) {
2435       Version_t R__v = R__b.ReadVersion(); if (R__v) { }
2436       AliDetector::Streamer(R__b);
2437       if (R__v < 2) return;
2438       R__b >> fNsectors;
2439       R__b >> fNclusters;
2440       R__b >> fNtracks;
2441       fClustersIndex = new Int_t[fNsectors+1];
2442       fDigitsIndex   = new Int_t[fNsectors+1];
2443    } else {
2444       R__b.WriteVersion(AliTPC::IsA());
2445       AliDetector::Streamer(R__b);
2446       R__b << fNsectors;
2447       R__b << fNclusters;
2448       R__b << fNtracks;
2449    }
2450 }
2451   
2452 ClassImp(AliTPCcluster)
2453  
2454 //_____________________________________________________________________________
2455 AliTPCcluster::AliTPCcluster(Float_t *hits, Int_t *lab)
2456 {
2457   //
2458   // Creates a simulated cluster for the TPC
2459   //
2460   fTracks[0]  = lab[0];
2461   fTracks[1]  = lab[1];
2462   fTracks[2]  = lab[2];
2463   fSector     = lab[3];
2464   fPadRow     = lab[4];
2465   fY          = hits[0];
2466   fZ          = hits[1];
2467   fQ          = hits[2];
2468   fSigmaY2    = hits[3];
2469   fSigmaZ2    = hits[4];
2470 }
2471  
2472 //_____________________________________________________________________________
2473 void AliTPCcluster::GetXYZ(Float_t *x, const AliTPCParam *par) const 
2474 {
2475   //
2476   // Transformation from local to global coordinate system
2477   //
2478   x[0]=par->GetPadRowRadii(fSector,fPadRow);
2479   x[1]=fY;
2480   x[2]=fZ;
2481   par->CRXYZtoXYZ(x,fSector,fPadRow,1);
2482   x[2]=fZ;
2483 }
2484  
2485 //_____________________________________________________________________________
2486 Int_t AliTPCcluster::Compare(TObject * o)
2487 {
2488   //
2489   // compare two clusters according y coordinata
2490   //
2491   AliTPCcluster *cl= (AliTPCcluster *)o;
2492   if (fY<cl->fY) return -1;
2493   if (fY==cl->fY) return 0;
2494   return 1;  
2495 }
2496
2497 Bool_t AliTPCcluster::IsSortable() const
2498 {
2499   //
2500   //make AliTPCcluster sortabale
2501   //
2502   return kTRUE; 
2503 }
2504
2505
2506
2507 ClassImp(AliTPCdigit)
2508  
2509 //_____________________________________________________________________________
2510 AliTPCdigit::AliTPCdigit(Int_t *tracks, Int_t *digits):
2511   AliDigit(tracks)
2512 {
2513   //
2514   // Creates a TPC digit object
2515   //
2516   fSector     = digits[0];
2517   fPadRow     = digits[1];
2518   fPad        = digits[2];
2519   fTime       = digits[3];
2520   fSignal     = digits[4];
2521 }
2522
2523  
2524 ClassImp(AliTPChit)
2525  
2526 //_____________________________________________________________________________
2527 AliTPChit::AliTPChit(Int_t shunt, Int_t track, Int_t *vol, Float_t *hits):
2528 AliHit(shunt,track)
2529 {
2530   //
2531   // Creates a TPC hit object
2532   //
2533   fSector     = vol[0];
2534   fPadRow     = vol[1];
2535   fX          = hits[0];
2536   fY          = hits[1];
2537   fZ          = hits[2];
2538   fQ          = hits[3];
2539 }
2540  
2541  
2542 ClassImp(AliTPCtrack)
2543  
2544 //_____________________________________________________________________________
2545 AliTPCtrack::AliTPCtrack(Float_t *hits)
2546 {
2547   //
2548   // Default creator for a TPC reconstructed track object
2549   //
2550   fX=hits[0]; // This is dummy code !
2551 }
2552
2553 AliTPCtrack::AliTPCtrack(const AliTPCcluster *c,const TVector& xx,
2554                          const TMatrix& CC, Double_t xref, Double_t alpha):
2555   x(xx),C(CC),fClusters(200)
2556 {
2557   //-----------------------------------------------------------------
2558   // This is the main track constructor.
2559   //
2560   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2561   //-----------------------------------------------------------------
2562   fX=xref;
2563   fAlpha=alpha;
2564   fChi2=0.;
2565   fClusters.AddLast((AliTPCcluster*)(c));
2566 }
2567
2568 //_____________________________________________________________________________
2569 AliTPCtrack::AliTPCtrack(const AliTPCtrack& t) : x(t.x), C(t.C),
2570   fClusters(t.fClusters.GetEntriesFast()) 
2571 {
2572   //-----------------------------------------------------------------
2573   // This is a track copy constructor.
2574   //
2575   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2576   //-----------------------------------------------------------------
2577   fX=t.fX;
2578   fChi2=t.fChi2;
2579   fAlpha=t.fAlpha;
2580   int n=t.fClusters.GetEntriesFast();
2581   for (int i=0; i<n; i++) fClusters.AddLast(t.fClusters.UncheckedAt(i));
2582 }
2583
2584 //_____________________________________________________________________________
2585 Int_t AliTPCtrack::Compare(TObject *o) {
2586   //-----------------------------------------------------------------
2587   // This function compares tracks according to their curvature.
2588   //
2589   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2590   //-----------------------------------------------------------------
2591   AliTPCtrack *t=(AliTPCtrack*)o;
2592   Double_t co=t->GetSigmaY2();
2593   Double_t c =GetSigmaY2();
2594   if (c>co) return 1;
2595   else if (c<co) return -1;
2596   return 0;
2597 }
2598
2599 //_____________________________________________________________________________
2600 int AliTPCtrack::PropagateTo(Double_t xk,Double_t x0,Double_t rho,Double_t pm)
2601 {
2602   //-----------------------------------------------------------------
2603   // This function propagates a track to a reference plane x=xk.
2604   //
2605   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2606   //-----------------------------------------------------------------
2607   if (TMath::Abs(x(2)*xk - x(3)) >= 0.999) {
2608     if (*this>4) cerr<<*this<<" AliTPCtrack warning: Propagation failed !\n";
2609     return 0;
2610   }
2611
2612   Double_t x1=fX, x2=x1+0.5*(xk-x1), dx=x2-x1, y1=x(0), z1=x(1);
2613   Double_t c1=x(2)*x1 - x(3), r1=sqrt(1.- c1*c1);
2614   Double_t c2=x(2)*x2 - x(3), r2=sqrt(1.- c2*c2);
2615   
2616   x(0) += dx*(c1+c2)/(r1+r2);
2617   x(1) += dx*(c1+c2)/(c1*r2 + c2*r1)*x(4);
2618
2619   TMatrix F(5,5); F.UnitMatrix();
2620   Double_t rr=r1+r2, cc=c1+c2, xx=x1+x2;
2621   F(0,2)= dx*(rr*xx + cc*(c1*x1/r1+c2*x2/r2))/(rr*rr);
2622   F(0,3)=-dx*(2*rr + cc*(c1/r1 + c2/r2))/(rr*rr);
2623   Double_t cr=c1*r2+c2*r1;
2624   F(1,2)= dx*x(4)*(cr*xx-cc*(r1*x2-c2*c1*x1/r1+r2*x1-c1*c2*x2/r2))/(cr*cr);
2625   F(1,3)=-dx*x(4)*(2*cr + cc*(c2*c1/r1-r1 + c1*c2/r2-r2))/(cr*cr);
2626   F(1,4)= dx*cc/cr; 
2627   TMatrix tmp(F,TMatrix::kMult,C);
2628   C.Mult(tmp,TMatrix(TMatrix::kTransposed,F));
2629   
2630   fX=x2;
2631   
2632   //Multiple scattering******************
2633   Double_t ey=x(2)*fX - x(3);
2634   Double_t ex=sqrt(1-ey*ey);
2635   Double_t ez=x(4);
2636   TMatrix Q(5,5); Q=0.;
2637   Q(2,2)=ez*ez+ey*ey;   Q(2,3)=-ex*ey;       Q(2,4)=-ex*ez;
2638   Q(3,2)=Q(2,3);        Q(3,3)= ez*ez+ex*ex; Q(3,4)=-ey*ez;
2639   Q(4,2)=Q(2,4);        Q(4,3)= Q(3,4);      Q(4,4)=1.;
2640   
2641   F=0;
2642   F(2,2)=-x(2)*ex;          F(2,3)=-x(2)*ey;
2643   F(3,2)=-ex*(x(2)*fX-ey);  F(3,3)=-(1.+ x(2)*fX*ey - ey*ey);
2644   F(4,2)=-ez*ex;            F(4,3)=-ez*ey;           F(4,4)=1.;
2645   
2646   tmp.Mult(F,Q);
2647   Q.Mult(tmp,TMatrix(TMatrix::kTransposed,F));
2648   
2649   Double_t p2=GetPt()*GetPt()*(1.+x(4)*x(4));
2650   Double_t beta2=p2/(p2 + pm*pm);
2651   Double_t d=sqrt((x1-fX)*(x1-fX)+(y1-x(0))*(y1-x(0))+(z1-x(1))*(z1-x(1)));
2652   d*=2.;
2653   Double_t theta2=14.1*14.1/(beta2*p2*1e6)*d/x0*rho;
2654   Q*=theta2;
2655   C+=Q;
2656   
2657   //Energy losses************************
2658   Double_t dE=0.153e-3/beta2*(log(5940*beta2/(1-beta2)) - beta2)*d*rho;
2659   if (x1 < x2) dE=-dE;
2660   x(2)*=(1.- sqrt(p2+pm*pm)/p2*dE);
2661   //x(3)*=(1.- sqrt(p2+pm*pm)/p2*dE);
2662   
2663   x1=fX; x2=xk; y1=x(0); z1=x(1);
2664   c1=x(2)*x1 - x(3); r1=sqrt(1.- c1*c1);
2665   c2=x(2)*x2 - x(3); r2=sqrt(1.- c2*c2);
2666   
2667   x(0) += dx*(c1+c2)/(r1+r2);
2668   x(1) += dx*(c1+c2)/(c1*r2 + c2*r1)*x(4);
2669   
2670   F.UnitMatrix();
2671   rr=r1+r2; cc=c1+c2; xx=x1+x2;
2672   F(0,2)= dx*(rr*xx + cc*(c1*x1/r1+c2*x2/r2))/(rr*rr);
2673   F(0,3)=-dx*(2*rr + cc*(c1/r1 + c2/r2))/(rr*rr);
2674   cr=c1*r2+c2*r1;
2675   F(1,2)= dx*x(4)*(cr*xx-cc*(r1*x2-c2*c1*x1/r1+r2*x1-c1*c2*x2/r2))/(cr*cr);
2676   F(1,3)=-dx*x(4)*(2*cr + cc*(c2*c1/r1-r1 + c1*c2/r2-r2))/(cr*cr);
2677   F(1,4)= dx*cc/cr; 
2678   tmp.Mult(F,C);
2679   C.Mult(tmp,TMatrix(TMatrix::kTransposed,F));
2680   
2681   fX=x2;
2682   
2683   return 1;
2684 }
2685
2686 //_____________________________________________________________________________
2687 void AliTPCtrack::PropagateToVertex(Double_t x0,Double_t rho,Double_t pm) 
2688 {
2689   //-----------------------------------------------------------------
2690   // This function propagates tracks to the "vertex".
2691   //
2692   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2693   //-----------------------------------------------------------------
2694   Double_t c=x(2)*fX - x(3);
2695   Double_t tgf=-x(3)/(x(2)*x(0) + sqrt(1-c*c));
2696   Double_t snf=tgf/sqrt(1.+ tgf*tgf);
2697   Double_t xv=(x(3)+snf)/x(2);
2698   PropagateTo(xv,x0,rho,pm);
2699 }
2700
2701 //_____________________________________________________________________________
2702 void AliTPCtrack::Update(const AliTPCcluster *c, Double_t chisq)
2703 {
2704   //-----------------------------------------------------------------
2705   // This function associates a clusters with this track.
2706   //
2707   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2708   //-----------------------------------------------------------------
2709   TMatrix H(2,5); H.UnitMatrix();
2710   TMatrix Ht(TMatrix::kTransposed,H);
2711   TVector m(2);   m(0)=c->fY; m(1)=c->fZ; 
2712   TMatrix V(2,2); V(0,0)=c->fSigmaY2; V(0,1)=0.; V(1,0)=0.; V(1,1)=c->fSigmaZ2;
2713
2714   TMatrix tmp(H,TMatrix::kMult,C);
2715   TMatrix R(tmp,TMatrix::kMult,Ht); R+=V;
2716   
2717   Double_t det=(Double_t)R(0,0)*R(1,1) - (Double_t)R(0,1)*R(1,0);
2718   R(0,1)=R(0,0); R(0,0)=R(1,1); R(1,1)=R(0,1); 
2719   R(1,0)*=-1; R(0,1)=R(1,0);
2720   R*=1./det;
2721   
2722   //R.Invert();
2723   
2724   TMatrix K(C,TMatrix::kMult,Ht); K*=R;
2725   
2726   TVector savex=x;
2727   x*=H; x-=m; x*=-1; x*=K; x+=savex;
2728   if (TMath::Abs(x(2)*fX-x(3)) >= 0.999) {
2729     if (*this>4) cerr<<*this<<" AliTPCtrack warning: Filtering failed !\n";
2730     x=savex;
2731     return;
2732   }
2733   
2734   TMatrix saveC=C;
2735   C.Mult(K,tmp); C-=saveC; C*=-1;
2736   
2737   fClusters.AddLast((AliTPCcluster*)c);
2738   fChi2 += chisq;
2739 }
2740
2741 //_____________________________________________________________________________
2742 int AliTPCtrack::Rotate(Double_t alpha)
2743 {
2744   //-----------------------------------------------------------------
2745   // This function rotates this track.
2746   //
2747   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2748   //-----------------------------------------------------------------
2749   fAlpha += alpha;
2750   
2751   Double_t x1=fX, y1=x(0);
2752   Double_t ca=cos(alpha), sa=sin(alpha);
2753   Double_t r1=x(2)*fX - x(3);
2754   
2755   fX = x1*ca + y1*sa;
2756   x(0)=-x1*sa + y1*ca;
2757   x(3)=x(3)*ca + (x(2)*y1 + sqrt(1.- r1*r1))*sa;
2758   
2759   Double_t r2=x(2)*fX - x(3);
2760   if (TMath::Abs(r2) >= 0.999) {
2761     if (*this>4) cerr<<*this<<" AliTPCtrack warning: Rotation failed !\n";
2762     return 0;
2763   }
2764   
2765   Double_t y0=x(0) + sqrt(1.- r2*r2)/x(2);
2766   if ((x(0)-y0)*x(2) >= 0.) {
2767     if (*this>4) cerr<<*this<<" AliTPCtrack warning: Rotation failed !!!\n";
2768     return 0;
2769   }
2770   
2771   TMatrix F(5,5); F.UnitMatrix();
2772   F(0,0)=ca;
2773   F(3,0)=x(2)*sa; 
2774   F(3,2)=(y1 - r1*x1/sqrt(1.- r1*r1))*sa; 
2775   F(3,3)= ca + sa*r1/sqrt(1.- r1*r1);
2776   TMatrix tmp(F,TMatrix::kMult,C); 
2777   // Double_t dy2=C(0,0);
2778   C.Mult(tmp,TMatrix(TMatrix::kTransposed,F));
2779   // C(0,0)+=dy2*sa*sa*r1*r1/(1.- r1*r1);
2780   // C(1,1)+=dy2*sa*sa*x(4)*x(4)/(1.- r1*r1);
2781   
2782   return 1;
2783 }
2784
2785 //_____________________________________________________________________________
2786 void AliTPCtrack::UseClusters() const 
2787 {
2788   //-----------------------------------------------------------------
2789   // This function marks clusters associated with this track.
2790   //
2791   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2792   //-----------------------------------------------------------------
2793   int num_of_clusters=fClusters.GetEntriesFast();
2794   for (int i=0; i<num_of_clusters; i++) {
2795     //if (i<=14) continue;
2796     AliTPCcluster *c=(AliTPCcluster*)fClusters.UncheckedAt(i);
2797     c->Use();   
2798   }
2799 }
2800
2801 //_____________________________________________________________________________
2802 Double_t AliTPCtrack::GetPredictedChi2(const AliTPCcluster *c) const 
2803 {
2804   //-----------------------------------------------------------------
2805   // This function calculates a predicted chi2 increment.
2806   //
2807   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2808   //-----------------------------------------------------------------
2809   TMatrix H(2,5); H.UnitMatrix();
2810   TVector m(2);   m(0)=c->fY; m(1)=c->fZ; 
2811   TMatrix V(2,2); V(0,0)=c->fSigmaY2; V(0,1)=0.; V(1,0)=0.; V(1,1)=c->fSigmaZ2;
2812   TVector res=x;  res*=H; res-=m; //res*=-1; 
2813   TMatrix tmp(H,TMatrix::kMult,C);
2814   TMatrix R(tmp,TMatrix::kMult,TMatrix(TMatrix::kTransposed,H)); R+=V;
2815   
2816   Double_t det=(Double_t)R(0,0)*R(1,1) - (Double_t)R(0,1)*R(1,0);
2817   if (TMath::Abs(det) < 1.e-10) {
2818     if (*this>4) cerr<<*this<<" AliTPCtrack warning: Singular matrix !\n";
2819     return 1e10;
2820   }
2821   R(0,1)=R(0,0); R(0,0)=R(1,1); R(1,1)=R(0,1); 
2822   R(1,0)*=-1; R(0,1)=R(1,0);
2823   R*=1./det;
2824   
2825   //R.Invert();
2826   
2827   TVector r=res;
2828   res*=R;
2829   return r*res;
2830 }
2831
2832 //_____________________________________________________________________________
2833 struct S { int lab; int max; };
2834 int AliTPCtrack::GetLabel(int nrows) const 
2835 {
2836   //-----------------------------------------------------------------
2837   // This function returns the track label. If label<0, this track is fake.
2838   //
2839   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2840   //-----------------------------------------------------------------
2841   int num_of_clusters=fClusters.GetEntriesFast();
2842   S *s=new S[num_of_clusters];
2843   int i;
2844   for (i=0; i<num_of_clusters; i++) s[i].lab=s[i].max=0;
2845   
2846   int lab=123456789;
2847   for (i=0; i<num_of_clusters; i++) {
2848     AliTPCcluster *c=(AliTPCcluster*)fClusters.UncheckedAt(i);
2849     lab=TMath::Abs(c->fTracks[0]);
2850     int j;
2851     for (j=0; j<num_of_clusters; j++)
2852       if (s[j].lab==lab || s[j].max==0) break;
2853     s[j].lab=lab;
2854     s[j].max++;
2855   }
2856   
2857   int max=0;
2858   for (i=0; i<num_of_clusters; i++) 
2859     if (s[i].max>max) {max=s[i].max; lab=s[i].lab;}
2860     
2861   delete[] s;
2862   
2863   for (i=0; i<num_of_clusters; i++) {
2864     AliTPCcluster *c=(AliTPCcluster*)fClusters.UncheckedAt(i);
2865     if (TMath::Abs(c->fTracks[1]) == lab ||
2866         TMath::Abs(c->fTracks[2]) == lab ) max++;
2867   }
2868   
2869   if (1.-float(max)/num_of_clusters > 0.10) return -lab;
2870   
2871   int tail=int(0.08*nrows);
2872   if (num_of_clusters < tail) return lab;
2873   
2874   max=0;
2875   for (i=1; i<=tail; i++) {
2876     AliTPCcluster *c=(AliTPCcluster*)fClusters.UncheckedAt(num_of_clusters-i);
2877     if (lab == TMath::Abs(c->fTracks[0]) ||
2878         lab == TMath::Abs(c->fTracks[1]) ||
2879         lab == TMath::Abs(c->fTracks[2])) max++;
2880   }
2881   if (max < int(0.5*tail)) return -lab;
2882   
2883   return lab;
2884 }
2885
2886 //_____________________________________________________________________________
2887 void AliTPCtrack::GetPxPyPz(Double_t& px, Double_t& py, Double_t& pz) const 
2888 {
2889   //-----------------------------------------------------------------
2890   // This function returns reconstructed track momentum in the global system.
2891   //
2892   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2893   //-----------------------------------------------------------------
2894   Double_t pt=TMath::Abs(GetPt()); // GeV/c
2895   Double_t r=x(2)*fX-x(3);
2896   Double_t y0=x(0) + sqrt(1.- r*r)/x(2);
2897   px=-pt*(x(0)-y0)*x(2);    //cos(phi);
2898   py=-pt*(x(3)-fX*x(2));   //sin(phi);
2899   pz=pt*x(4);
2900   Double_t tmp=px*TMath::Cos(fAlpha) - py*TMath::Sin(fAlpha);
2901   py=px*TMath::Sin(fAlpha) + py*TMath::Cos(fAlpha);
2902   px=tmp;  
2903 }
2904
2905 //_____________________________________________________________________________
2906 Double_t AliTPCtrack::GetdEdX(Double_t low, Double_t up) const {
2907   //-----------------------------------------------------------------
2908   // This funtion calculates dE/dX within the "low" and "up" cuts.
2909   //
2910   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2911   //-----------------------------------------------------------------
2912   int ncl=fClusters.GetEntriesFast();
2913   int n=0;
2914   Double_t *q=new Double_t[ncl];
2915   int i;
2916   for (i=0; i<ncl; i++) {
2917      AliTPCcluster *cl=(AliTPCcluster*)(fClusters.UncheckedAt(i));
2918      //     if (cl->fdEdX > 3000) continue;
2919      if (cl->fdEdX <= 0) continue;
2920      q[n++]=cl->fdEdX;
2921   }
2922
2923   //stupid sorting
2924   int swap;
2925   do {
2926     swap=0;
2927     for (i=0; i<n-1; i++) {
2928       if (q[i]<=q[i+1]) continue;
2929       Double_t tmp=q[i]; q[i]=q[i+1]; q[i+1]=tmp;
2930       swap++;
2931     }
2932   } while (swap);
2933
2934   int nl=int(low*n), nu=int(up *n);
2935   Double_t dedx=0.;
2936   for (i=nl; i<=nu; i++) dedx += q[i];
2937   dedx /= (nu-nl+1);
2938   return dedx;
2939 }
2940
2941 //_________________________________________________________________________
2942 //
2943 // Classes for internal tracking use
2944 //_________________________________________________________________________
2945 void AliTPCRow::InsertCluster(const AliTPCcluster* c) {
2946   //-----------------------------------------------------------------------
2947   // Insert a cluster into this pad row in accordence with its y-coordinate
2948   //
2949   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2950   //-----------------------------------------------------------------------
2951   if (num_of_clusters==MAX_CLUSTER_PER_ROW) {
2952     cerr<<"AliTPCRow::InsertCluster(): Too many clusters !\n"; return;
2953   }
2954   if (num_of_clusters==0) {clusters[num_of_clusters++]=c; return;}
2955   int i=Find(c->fY);
2956   memmove(clusters+i+1 ,clusters+i,(num_of_clusters-i)*sizeof(AliTPCcluster*));
2957   clusters[i]=c; num_of_clusters++;
2958 }
2959
2960 int AliTPCRow::Find(Double_t y) const {
2961   //-----------------------------------------------------------------------
2962   // Return the index of the nearest cluster 
2963   //
2964   // Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
2965   //-----------------------------------------------------------------------
2966   if (y <= clusters[0]->fY) return 0;
2967   if (y > clusters[num_of_clusters-1]->fY) return num_of_clusters;
2968   int b=0, e=num_of_clusters-1, m=(b+e)/2;
2969   for (; b<e; m=(b+e)/2) {
2970     if (y > clusters[m]->fY) b=m+1;
2971     else e=m; 
2972   }
2973   return m;
2974 }
2975