]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCseed.cxx
Fixed warning.
[u/mrichter/AliRoot.git] / TPC / AliTPCseed.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
18
19 //-----------------------------------------------------------------
20 //           Implementation of the TPC seed class
21 //        This class is used by the AliTPCtrackerMI class
22 //      Origin: Marian Ivanov, CERN, Marian.Ivanov@cern.ch
23 //-----------------------------------------------------------------
24 #include "TClonesArray.h"
25 #include "AliTPCseed.h"
26 #include "AliTPCReconstructor.h"
27 #include "AliTPCClusterParam.h"
28 #include "AliTPCCalPad.h"
29 #include "AliTPCCalROC.h"
30 #include "AliTPCcalibDB.h"
31 #include "AliTPCParam.h"
32 #include "AliMathBase.h"
33 #include "AliTPCTransform.h"
34 #include "AliSplineFit.h"
35 #include "AliCDBManager.h"
36 #include "AliTPCcalibDButil.h"
37
38
39 ClassImp(AliTPCseed)
40
41
42
43 AliTPCseed::AliTPCseed():
44   AliTPCtrack(),
45   fEsd(0x0),
46   fClusterOwner(kFALSE),
47   fRow(0),
48   fSector(-1),
49   fRelativeSector(-1),
50   fCurrentSigmaY2(1e10),
51   fCurrentSigmaZ2(1e10),
52   fCMeanSigmaY2p30(-1.),   //! current mean sigma Y2 - mean30%
53   fCMeanSigmaZ2p30(-1.),   //! current mean sigma Z2 - mean30%
54   fCMeanSigmaY2p30R(-1.),   //! current mean sigma Y2 - mean2%
55   fCMeanSigmaZ2p30R(-1.),   //! current mean sigma Z2 - mean2%
56   //
57   fErrorY2(1e10),
58   fErrorZ2(1e10),
59   fCurrentCluster(0x0),
60   fCurrentClusterIndex1(-1),
61   fInDead(kFALSE),
62   fIsSeeding(kFALSE),
63   fNoCluster(0),
64   fSort(0),
65   fBSigned(kFALSE),
66   fSeedType(0),
67   fSeed1(-1),
68   fSeed2(-1),
69   fMAngular(0),
70   fCircular(0),
71   fClusterMap(159),
72   fSharedMap(159)
73 {
74   //
75   for (Int_t i=0;i<160;i++) SetClusterIndex2(i,-3);
76   for (Int_t i=0;i<160;i++) fClusterPointer[i]=0;
77   for (Int_t i=0;i<3;i++)   fKinkIndexes[i]=0;
78   for (Int_t i=0;i<AliPID::kSPECIES;i++)   fTPCr[i]=0.2;
79   for (Int_t i=0;i<4;i++) {
80     fDEDX[i] = 0.;
81     fSDEDX[i] = 1e10;
82     fNCDEDX[i] = 0;
83   }
84   for (Int_t i=0;i<12;i++) fOverlapLabels[i] = -1;
85   //  for (Int_t i=0;i<160;i++) fClusterMap[i]=kFALSE;
86   //for (Int_t i=0;i<160;i++) fSharedMap[i]=kFALSE;
87   fClusterMap.ResetAllBits(kFALSE);
88   fSharedMap.ResetAllBits(kFALSE);
89
90 }
91
92 AliTPCseed::AliTPCseed(const AliTPCseed &s, Bool_t clusterOwner):
93   AliTPCtrack(s),
94   fEsd(0x0),
95   fClusterOwner(clusterOwner),
96   fRow(0),
97   fSector(-1),
98   fRelativeSector(-1),
99   fCurrentSigmaY2(-1),
100   fCurrentSigmaZ2(-1),
101   fCMeanSigmaY2p30(-1.),   //! current mean sigma Y2 - mean30%
102   fCMeanSigmaZ2p30(-1.),   //! current mean sigma Z2 - mean30%
103   fCMeanSigmaY2p30R(-1.),   //! current mean sigma Y2 - mean2%
104   fCMeanSigmaZ2p30R(-1.),   //! current mean sigma Z2 - mean2%
105   fErrorY2(1e10),
106   fErrorZ2(1e10),
107   fCurrentCluster(0x0),
108   fCurrentClusterIndex1(-1),
109   fInDead(kFALSE),
110   fIsSeeding(kFALSE),
111   fNoCluster(0),
112   fSort(0),
113   fBSigned(kFALSE),
114   fSeedType(0),
115   fSeed1(-1),
116   fSeed2(-1),
117   fMAngular(0),
118   fCircular(0),
119   fClusterMap(s.fClusterMap),
120   fSharedMap(s.fSharedMap)
121 {
122   //---------------------
123   // dummy copy constructor
124   //-------------------------
125   for (Int_t i=0;i<160;i++) {
126     fClusterPointer[i]=0;
127     if (fClusterOwner){
128       if (s.fClusterPointer[i])
129         fClusterPointer[i] = new AliTPCclusterMI(*(s.fClusterPointer[i]));
130     }else{
131       fClusterPointer[i] = s.fClusterPointer[i];
132     }
133     fTrackPoints[i] = s.fTrackPoints[i];
134   }
135   for (Int_t i=0;i<160;i++) fIndex[i] = s.fIndex[i];
136   for (Int_t i=0;i<AliPID::kSPECIES;i++)   fTPCr[i]=s.fTPCr[i];
137   for (Int_t i=0;i<4;i++) {
138     fDEDX[i] = s.fDEDX[i];
139     fSDEDX[i] = s.fSDEDX[i];
140     fNCDEDX[i] = s.fNCDEDX[i];
141   }
142   for (Int_t i=0;i<12;i++) fOverlapLabels[i] = s.fOverlapLabels[i];
143
144 }
145
146
147 AliTPCseed::AliTPCseed(const AliTPCtrack &t):
148   AliTPCtrack(t),
149   fEsd(0x0),
150   fClusterOwner(kFALSE),
151   fRow(0),
152   fSector(-1),
153   fRelativeSector(-1),
154   fCurrentSigmaY2(-1),
155   fCurrentSigmaZ2(-1),
156   fCMeanSigmaY2p30(-1.),   //! current mean sigma Y2 - mean30%
157   fCMeanSigmaZ2p30(-1.),   //! current mean sigma Z2 - mean30%
158   fCMeanSigmaY2p30R(-1.),   //! current mean sigma Y2 - mean2%
159   fCMeanSigmaZ2p30R(-1.),   //! current mean sigma Z2 - mean2%
160   fErrorY2(1e10),
161   fErrorZ2(1e10),
162   fCurrentCluster(0x0),
163   fCurrentClusterIndex1(-1),
164   fInDead(kFALSE),
165   fIsSeeding(kFALSE),
166   fNoCluster(0),
167   fSort(0),
168   fBSigned(kFALSE),
169   fSeedType(0),
170   fSeed1(-1),
171   fSeed2(-1),
172   fMAngular(0),
173   fCircular(0),
174   fClusterMap(159),
175   fSharedMap(159)
176 {
177   //
178   // Constructor from AliTPCtrack
179   //
180   fFirstPoint =0;
181   for (Int_t i=0;i<5;i++)   fTPCr[i]=0.2;
182   for (Int_t i=0;i<160;i++) {
183     fClusterPointer[i] = 0;
184     Int_t index = t.GetClusterIndex(i);
185     if (index>=-1){ 
186       SetClusterIndex2(i,index);
187     }
188     else{
189       SetClusterIndex2(i,-3); 
190     }    
191   }
192   for (Int_t i=0;i<4;i++) {
193     fDEDX[i] = 0.;
194     fSDEDX[i] = 1e10;
195     fNCDEDX[i] = 0;
196   }
197   for (Int_t i=0;i<12;i++) fOverlapLabels[i] = -1;
198   
199   //for (Int_t i=0;i<160;i++) fClusterMap[i]=kFALSE;
200   //for (Int_t i=0;i<160;i++) fSharedMap[i]=kFALSE;
201   fClusterMap.ResetAllBits(kFALSE);
202   fSharedMap.ResetAllBits(kFALSE);
203
204 }
205
206 AliTPCseed::AliTPCseed(Double_t xr, Double_t alpha, const Double_t xx[5],
207                        const Double_t cc[15], Int_t index):      
208   AliTPCtrack(xr, alpha, xx, cc, index),
209   fEsd(0x0),
210   fClusterOwner(kFALSE),
211   fRow(0),
212   fSector(-1),
213   fRelativeSector(-1),
214   fCurrentSigmaY2(-1),
215   fCurrentSigmaZ2(-1),
216   fCMeanSigmaY2p30(-1.),   //! current mean sigma Y2 - mean30%
217   fCMeanSigmaZ2p30(-1.),   //! current mean sigma Z2 - mean30%
218   fCMeanSigmaY2p30R(-1.),   //! current mean sigma Y2 - mean2%
219   fCMeanSigmaZ2p30R(-1.),   //! current mean sigma Z2 - mean2%
220   fErrorY2(1e10),
221   fErrorZ2(1e10),
222   fCurrentCluster(0x0),
223   fCurrentClusterIndex1(-1),
224   fInDead(kFALSE),
225   fIsSeeding(kFALSE),
226   fNoCluster(0),
227   fSort(0),
228   fBSigned(kFALSE),
229   fSeedType(0),
230   fSeed1(-1),
231   fSeed2(-1),
232   fMAngular(0),
233   fCircular(0),
234   fClusterMap(159),
235   fSharedMap(159)
236 {
237   //
238   // Constructor
239   //
240   fFirstPoint =0;
241   for (Int_t i=0;i<160;i++) SetClusterIndex2(i,-3);
242   for (Int_t i=0;i<160;i++) fClusterPointer[i]=0;
243   for (Int_t i=0;i<5;i++)   fTPCr[i]=0.2;
244   for (Int_t i=0;i<4;i++) {
245     fDEDX[i] = 0.;
246     fSDEDX[i] = 1e10;
247     fNCDEDX[i] = 0;
248   }
249   for (Int_t i=0;i<12;i++) fOverlapLabels[i] = -1;
250 }
251
252 AliTPCseed::~AliTPCseed(){
253   //
254   // destructor
255   fNoCluster =0;
256   if (fClusterOwner){
257     for (Int_t icluster=0; icluster<160; icluster++){
258       delete fClusterPointer[icluster];
259     }
260   }
261
262 }
263 //_________________________________________________
264 AliTPCseed & AliTPCseed::operator=(const AliTPCseed &param)
265 {
266   //
267   // assignment operator 
268   //
269   if(this!=&param){
270     AliTPCtrack::operator=(param);
271     fEsd =param.fEsd; 
272     for(Int_t i = 0;i<160;++i)fClusterPointer[i] = param.fClusterPointer[i]; // this is not allocated by AliTPCSeed
273     fClusterOwner = param.fClusterOwner;
274     // leave out fPoint, they are also not copied in the copy ctor...
275     // but deleted in the dtor... strange...
276     fRow            = param.fRow;
277     fSector         = param.fSector;
278     fRelativeSector = param.fRelativeSector;
279     fCurrentSigmaY2 = param.fCurrentSigmaY2;
280     fCurrentSigmaZ2 = param.fCurrentSigmaZ2;
281     fErrorY2        = param.fErrorY2;
282     fErrorZ2        = param.fErrorZ2;
283     fCurrentCluster = param.fCurrentCluster; // this is not allocated by AliTPCSeed
284     fCurrentClusterIndex1 = param.fCurrentClusterIndex1; 
285     fInDead         = param.fInDead;
286     fIsSeeding      = param.fIsSeeding;
287     fNoCluster      = param.fNoCluster;
288     fSort           = param.fSort;
289     fBSigned        = param.fBSigned;
290     for(Int_t i = 0;i<4;++i){
291       fDEDX[i]   = param.fDEDX[i];
292       fSDEDX[i]  = param.fSDEDX[i];
293       fNCDEDX[i] = param.fNCDEDX[i];
294     }
295     for(Int_t i = 0;i<AliPID::kSPECIES;++i)fTPCr[i] = param.fTPCr[i];
296     
297     fSeedType = param.fSeedType;
298     fSeed1    = param.fSeed1;
299     fSeed2    = param.fSeed2;
300     for(Int_t i = 0;i<12;++i)fOverlapLabels[i] = param.fOverlapLabels[i];
301     fMAngular = param.fMAngular;
302     fCircular = param.fCircular;
303     for(int i = 0;i<160;++i)fTrackPoints[i] =  param.fTrackPoints[i];
304     fClusterMap = param.fClusterMap;
305     fSharedMap = param.fSharedMap;
306   }
307   return (*this);
308 }
309 //____________________________________________________
310 AliTPCTrackerPoint * AliTPCseed::GetTrackPoint(Int_t i)
311 {
312   //
313   // 
314   return &fTrackPoints[i];
315 }
316
317
318
319 Double_t AliTPCseed::GetDensityFirst(Int_t n)
320 {
321   //
322   //
323   // return cluster for n rows bellow first point
324   Int_t nfoundable = 1;
325   Int_t nfound      = 1;
326   for (Int_t i=fLastPoint-1;i>0&&nfoundable<n; i--){
327     Int_t index = GetClusterIndex2(i);
328     if (index!=-1) nfoundable++;
329     if (index>0) nfound++;
330   }
331   if (nfoundable<n) return 0;
332   return Double_t(nfound)/Double_t(nfoundable);
333
334 }
335
336
337 void AliTPCseed::GetClusterStatistic(Int_t first, Int_t last, Int_t &found, Int_t &foundable, Int_t &shared, Bool_t plus2)
338 {
339   // get cluster stat.  on given region
340   //
341   found       = 0;
342   foundable   = 0;
343   shared      =0;
344   for (Int_t i=first;i<last; i++){
345     Int_t index = GetClusterIndex2(i);
346     if (index!=-1) foundable++;
347     if (index&0x8000) continue;
348     if (fClusterPointer[i]) {
349       found++;
350     }
351     else 
352       continue;
353
354     if (fClusterPointer[i]->IsUsed(10)) {
355       shared++;
356       continue;
357     }
358     if (!plus2) continue; //take also neighborhoud
359     //
360     if ( (i>0) && fClusterPointer[i-1]){
361       if (fClusterPointer[i-1]->IsUsed(10)) {
362         shared++;
363         continue;
364       }
365     }
366     if ( fClusterPointer[i+1]){
367       if (fClusterPointer[i+1]->IsUsed(10)) {
368         shared++;
369         continue;
370       }
371     }
372     
373   }
374   //if (shared>found){
375     //Error("AliTPCseed::GetClusterStatistic","problem\n");
376   //}
377 }
378
379
380
381
382
383 void AliTPCseed::Reset(Bool_t all)
384 {
385   //
386   //
387   SetNumberOfClusters(0);
388   fNFoundable = 0;
389   SetChi2(0);
390   ResetCovariance(10.);
391   /*
392   if (fTrackPoints){
393     for (Int_t i=0;i<8;i++){
394       delete [] fTrackPoints[i];
395     }
396     delete fTrackPoints;
397     fTrackPoints =0;
398   }
399   */
400
401   if (all){   
402     for (Int_t i=0;i<200;i++) SetClusterIndex2(i,-3);
403     for (Int_t i=0;i<160;i++) fClusterPointer[i]=0;
404   }
405
406 }
407
408
409 void AliTPCseed::Modify(Double_t factor)
410 {
411
412   //------------------------------------------------------------------
413   //This function makes a track forget its history :)  
414   //------------------------------------------------------------------
415   if (factor<=0) {
416     ResetCovariance(10.);
417     return;
418   }
419   ResetCovariance(factor);
420
421   SetNumberOfClusters(0);
422   fNFoundable =0;
423   SetChi2(0);
424   fRemoval = 0;
425   fCurrentSigmaY2 = 0.000005;
426   fCurrentSigmaZ2 = 0.000005;
427   fNoCluster     = 0;
428   //fFirstPoint = 160;
429   //fLastPoint  = 0;
430 }
431
432
433
434
435 Int_t  AliTPCseed::GetProlongation(Double_t xk, Double_t &y, Double_t & z) const
436 {
437   //-----------------------------------------------------------------
438   // This function find proloncation of a track to a reference plane x=xk.
439   // doesn't change internal state of the track
440   //-----------------------------------------------------------------
441   
442   Double_t x1=GetX(), x2=x1+(xk-x1), dx=x2-x1;
443
444   if (TMath::Abs(GetSnp()+GetC()*dx) >= AliTPCReconstructor::GetMaxSnpTrack()) {   
445     return 0;
446   }
447
448   //  Double_t y1=fP0, z1=fP1;
449   Double_t c1=GetSnp(), r1=sqrt((1.-c1)*(1.+c1));
450   Double_t c2=c1 + GetC()*dx, r2=sqrt((1.-c2)*(1.+c2));
451   
452   y = GetY();
453   z = GetZ();
454   //y += dx*(c1+c2)/(r1+r2);
455   //z += dx*(c1+c2)/(c1*r2 + c2*r1)*fP3;
456   
457   Double_t dy = dx*(c1+c2)/(r1+r2);
458   Double_t dz = 0;
459   //
460   Double_t delta = GetC()*dx*(c1+c2)/(c1*r2 + c2*r1);
461   /*
462   if (TMath::Abs(delta)>0.0001){
463     dz = fP3*TMath::ASin(delta)/fP4;
464   }else{
465     dz = dx*fP3*(c1+c2)/(c1*r2 + c2*r1);
466   }
467   */
468   //  dz =  fP3*AliTPCFastMath::FastAsin(delta)/fP4;
469   dz =  GetTgl()*TMath::ASin(delta)/GetC();
470   //
471   y+=dy;
472   z+=dz;
473   
474
475   return 1;  
476 }
477
478
479 //_____________________________________________________________________________
480 Double_t AliTPCseed::GetPredictedChi2(const AliCluster *c) const 
481 {
482   //-----------------------------------------------------------------
483   // This function calculates a predicted chi2 increment.
484   //-----------------------------------------------------------------
485   Double_t p[2]={c->GetY(), c->GetZ()};
486   Double_t cov[3]={fErrorY2, 0., fErrorZ2};
487
488   Float_t dx = ((AliTPCclusterMI*)c)->GetX()-GetX();
489   if (TMath::Abs(dx)>0){
490     Float_t ty = TMath::Tan(TMath::ASin(GetSnp()));
491     Float_t dy = dx*ty;
492     Float_t dz = dx*TMath::Sqrt(1.+ty*ty)*GetTgl();
493     p[0] = c->GetY()-dy;  
494     p[1] = c->GetZ()-dz;  
495   }
496   return AliExternalTrackParam::GetPredictedChi2(p,cov);
497 }
498
499 //_________________________________________________________________________________________
500
501
502 Int_t AliTPCseed::Compare(const TObject *o) const {
503   //-----------------------------------------------------------------
504   // This function compares tracks according to the sector - for given sector according z
505   //-----------------------------------------------------------------
506   AliTPCseed *t=(AliTPCseed*)o;
507
508   if (fSort == 0){
509     if (t->fRelativeSector>fRelativeSector) return -1;
510     if (t->fRelativeSector<fRelativeSector) return 1;
511     Double_t z2 = t->GetZ();
512     Double_t z1 = GetZ();
513     if (z2>z1) return 1;
514     if (z2<z1) return -1;
515     return 0;
516   }
517   else {
518     Float_t f2 =1;
519     f2 = 1-20*TMath::Sqrt(t->GetSigma1Pt2())/(t->OneOverPt()+0.0066);
520     if (t->fBConstrain) f2=1.2;
521
522     Float_t f1 =1;
523     f1 = 1-20*TMath::Sqrt(GetSigma1Pt2())/(OneOverPt()+0.0066);
524
525     if (fBConstrain)   f1=1.2;
526  
527     if (t->GetNumberOfClusters()*f2 <GetNumberOfClusters()*f1) return -1;
528     else return +1;
529   }
530 }
531
532
533
534
535 //_____________________________________________________________________________
536 Bool_t AliTPCseed::Update(const AliCluster *c, Double_t chisq, Int_t index)
537 {
538   //-----------------------------------------------------------------
539   // This function associates a cluster with this track.
540   //-----------------------------------------------------------------
541   Int_t n=GetNumberOfClusters();
542   Int_t idx=GetClusterIndex(n);    // save the current cluster index
543
544   AliCluster cl(*c);  cl.SetSigmaY2(fErrorY2); cl.SetSigmaZ2(fErrorZ2);
545   Float_t dx = ((AliTPCclusterMI*)c)->GetX()-GetX();
546   if (TMath::Abs(dx)>0){
547     Float_t ty = TMath::Tan(TMath::ASin(GetSnp()));
548     Float_t dy = dx*ty;
549     Float_t dz = dx*TMath::Sqrt(1.+ty*ty)*GetTgl();
550     cl.SetY(c->GetY()-dy);  
551     cl.SetZ(c->GetZ()-dz);  
552   }
553
554   if (!AliTPCtrack::Update(&cl,chisq,index)) return kFALSE;
555   
556   if (fCMeanSigmaY2p30<0){
557     fCMeanSigmaY2p30= c->GetSigmaY2();   //! current mean sigma Y2 - mean30%
558     fCMeanSigmaZ2p30= c->GetSigmaZ2();   //! current mean sigma Z2 - mean30%    
559     fCMeanSigmaY2p30R = 1;   //! current mean sigma Y2 - mean5%
560     fCMeanSigmaZ2p30R = 1;   //! current mean sigma Z2 - mean5%
561   }
562   //
563   fCMeanSigmaY2p30= 0.70*fCMeanSigmaY2p30 +0.30*c->GetSigmaY2();   
564   fCMeanSigmaZ2p30= 0.70*fCMeanSigmaZ2p30 +0.30*c->GetSigmaZ2();  
565   if (fCurrentSigmaY2>0){
566     fCMeanSigmaY2p30R = 0.7*fCMeanSigmaY2p30R  +0.3*c->GetSigmaY2()/fCurrentSigmaY2;  
567     fCMeanSigmaZ2p30R = 0.7*fCMeanSigmaZ2p30R  +0.3*c->GetSigmaZ2()/fCurrentSigmaZ2;   
568   }
569
570
571   SetClusterIndex(n,idx);          // restore the current cluster index
572   return kTRUE;
573 }
574
575
576
577 //_____________________________________________________________________________
578 Float_t AliTPCseed::CookdEdx(Double_t low, Double_t up,Int_t i1, Int_t i2, Bool_t /* onlyused */) {
579   //-----------------------------------------------------------------
580   // This funtion calculates dE/dX within the "low" and "up" cuts.
581   //-----------------------------------------------------------------
582   // CookdEdxAnalytical(Double_t low, Double_t up, Int_t type, Int_t i1, Int_t i2, Int_t returnVal)
583   AliTPCParam *param = AliTPCcalibDB::Instance()->GetParameters();
584   
585   Int_t row0 = param->GetNRowLow();
586   Int_t row1 = row0+param->GetNRowUp1();
587   Int_t row2 = row1+param->GetNRowUp2();
588   const AliTPCRecoParam * recoParam = AliTPCcalibDB::Instance()->GetTransform()->GetCurrentRecoParam();
589   Int_t useTot = 0;
590   if (recoParam) useTot = (recoParam->GetUseTotCharge())? 0:1;
591   //
592   //
593   //
594   fDEDX[0]      = CookdEdxAnalytical(low,up,useTot ,i1  ,i2,   0);
595   fDEDX[1]      = CookdEdxAnalytical(low,up,useTot ,0   ,row0, 0);
596   fDEDX[2]      = CookdEdxAnalytical(low,up,useTot ,row0,row1, 0);
597   fDEDX[3]      = CookdEdxAnalytical(low,up,useTot ,row1,row2, 0);
598   //
599   fSDEDX[0]     = CookdEdxAnalytical(low,up,useTot ,i1  ,i2,   1);
600   fSDEDX[1]     = CookdEdxAnalytical(low,up,useTot ,0   ,row0, 1);
601   fSDEDX[2]     = CookdEdxAnalytical(low,up,useTot ,row0,row1, 1);
602   fSDEDX[3]     = CookdEdxAnalytical(low,up,useTot ,row1,row2, 1);
603   //
604   fNCDEDX[0]    = TMath::Nint(CookdEdxAnalytical(low,up,useTot ,i1  ,i2,   2));
605   fNCDEDX[1]    = TMath::Nint(CookdEdxAnalytical(low,up,useTot ,0   ,row0, 2));
606   fNCDEDX[2]    = TMath::Nint(CookdEdxAnalytical(low,up,useTot ,row0,row1, 2));
607   fNCDEDX[3]    = TMath::Nint(CookdEdxAnalytical(low,up,useTot ,row1,row2, 2));
608
609   SetdEdx(fDEDX[0]);
610   return fDEDX[0];
611
612 //  return CookdEdxNorm(low,up,0,i1,i2,1,0,2);
613
614
615 //   Float_t amp[200];
616 //   Float_t angular[200];
617 //   Float_t weight[200];
618 //   Int_t index[200];
619 //   //Int_t nc = 0;
620 //   Float_t meanlog = 100.;
621   
622 //   Float_t mean[4]  = {0,0,0,0};
623 //   Float_t sigma[4] = {1000,1000,1000,1000};
624 //   Int_t nc[4]      = {0,0,0,0};
625 //   Float_t norm[4]    = {1000,1000,1000,1000};
626 //   //
627 //   //
628 //   fNShared =0;
629
630 //   Float_t gainGG = 1;
631 //   if (AliTPCcalibDB::Instance()->GetParameters()){
632 //     gainGG= AliTPCcalibDB::Instance()->GetParameters()->GetGasGain()/20000.;  //relative gas gain
633 //   }
634
635
636 //   for (Int_t of =0; of<4; of++){    
637 //     for (Int_t i=of+i1;i<i2;i+=4)
638 //       {
639 //      Int_t clindex = fIndex[i];
640 //      if (clindex<0||clindex&0x8000) continue;
641
642 //      //AliTPCTrackPoint * point = (AliTPCTrackPoint *) arr.At(i);
643 //      AliTPCTrackerPoint * point = GetTrackPoint(i);
644 //      //AliTPCTrackerPoint * pointm = GetTrackPoint(i-1);
645 //      //AliTPCTrackerPoint * pointp = 0;
646 //      //if (i<159) pointp = GetTrackPoint(i+1);
647
648 //      if (point==0) continue;
649 //      AliTPCclusterMI * cl = fClusterPointer[i];
650 //      if (cl==0) continue;    
651 //      if (onlyused && (!cl->IsUsed(10))) continue;
652 //      if (cl->IsUsed(11)) {
653 //        fNShared++;
654 //        continue;
655 //      }
656 //      Int_t   type   = cl->GetType();
657 //      //if (point->fIsShared){
658 //      //  fNShared++;
659 //      //  continue;
660 //      //}
661 //      //if (pointm) 
662 //      //  if (pointm->fIsShared) continue;
663 //      //if (pointp) 
664 //      //  if (pointp->fIsShared) continue;
665
666 //      if (type<0) continue;
667 //      //if (type>10) continue;       
668 //      //if (point->GetErrY()==0) continue;
669 //      //if (point->GetErrZ()==0) continue;
670
671 //      //Float_t ddy = (point->GetY()-cl->GetY())/point->GetErrY();
672 //      //Float_t ddz = (point->GetZ()-cl->GetZ())/point->GetErrZ();
673 //      //if ((ddy*ddy+ddz*ddz)>10) continue; 
674
675
676 //      //      if (point->GetCPoint().GetMax()<5) continue;
677 //      if (cl->GetMax()<5) continue;
678 //      Float_t angley = point->GetAngleY();
679 //      Float_t anglez = point->GetAngleZ();
680
681 //      Float_t rsigmay2 =  point->GetSigmaY();
682 //      Float_t rsigmaz2 =  point->GetSigmaZ();
683 //      /*
684 //      Float_t ns = 1.;
685 //      if (pointm){
686 //        rsigmay +=  pointm->GetTPoint().GetSigmaY();
687 //        rsigmaz +=  pointm->GetTPoint().GetSigmaZ();
688 //        ns+=1.;
689 //      }
690 //      if (pointp){
691 //        rsigmay +=  pointp->GetTPoint().GetSigmaY();
692 //        rsigmaz +=  pointp->GetTPoint().GetSigmaZ();
693 //        ns+=1.;
694 //      }
695 //      rsigmay/=ns;
696 //      rsigmaz/=ns;
697 //      */
698
699 //      Float_t rsigma = TMath::Sqrt(rsigmay2*rsigmaz2);
700
701 //      Float_t ampc   = 0;     // normalization to the number of electrons
702 //      if (i>64){
703 //        //      ampc = 1.*point->GetCPoint().GetMax();
704 //        ampc = 1.*cl->GetMax();
705 //        //ampc = 1.*point->GetCPoint().GetQ();          
706 //        //      AliTPCClusterPoint & p = point->GetCPoint();
707 //        //      Float_t dy = TMath::Abs(Int_t( TMath::Abs(p.GetY()/0.6)) - TMath::Abs(p.GetY()/0.6)+0.5);
708 //        // Float_t iz =  (250.0-TMath::Abs(p.GetZ())+0.11)/0.566;
709 //        //Float_t dz = 
710 //        //  TMath::Abs( Int_t(iz) - iz + 0.5);
711 //        //ampc *= 1.15*(1-0.3*dy);
712 //        //ampc *= 1.15*(1-0.3*dz);
713 //        //      Float_t zfactor = (AliTPCReconstructor::GetCtgRange()-0.0004*TMath::Abs(point->GetCPoint().GetZ()));
714 //        //ampc               *=zfactor; 
715 //      }
716 //      else{ 
717 //        //ampc = 1.0*point->GetCPoint().GetMax(); 
718 //        ampc = 1.0*cl->GetMax(); 
719 //        //ampc = 1.0*point->GetCPoint().GetQ(); 
720 //        //AliTPCClusterPoint & p = point->GetCPoint();
721 //        // Float_t dy = TMath::Abs(Int_t( TMath::Abs(p.GetY()/0.4)) - TMath::Abs(p.GetY()/0.4)+0.5);
722 //        //Float_t iz =  (250.0-TMath::Abs(p.GetZ())+0.11)/0.566;
723 //        //Float_t dz = 
724 //        //  TMath::Abs( Int_t(iz) - iz + 0.5);
725
726 //        //ampc *= 1.15*(1-0.3*dy);
727 //        //ampc *= 1.15*(1-0.3*dz);
728 //        //    Float_t zfactor = (1.02-0.000*TMath::Abs(point->GetCPoint().GetZ()));
729 //        //ampc               *=zfactor; 
730
731 //      }
732 //      ampc *= 2.0;     // put mean value to channel 50
733 //      //ampc *= 0.58;     // put mean value to channel 50
734 //      Float_t w      =  1.;
735 //      //      if (type>0)  w =  1./(type/2.-0.5); 
736 //      //      Float_t z = TMath::Abs(cl->GetZ());
737 //      if (i<64) {
738 //        ampc /= 0.6;
739 //        //ampc /= (1+0.0008*z);
740 //      } else
741 //        if (i>128){
742 //          ampc /=1.5;
743 //          //ampc /= (1+0.0008*z);
744 //        }else{
745 //          //ampc /= (1+0.0008*z);
746 //        }
747         
748 //      if (type<0) {  //amp at the border - lower weight
749 //        // w*= 2.;
750           
751 //        continue;
752 //      }
753 //      if (rsigma>1.5) ampc/=1.3;  // if big backround
754 //      amp[nc[of]]        = ampc;
755 //      amp[nc[of]]       /=gainGG;
756 //      angular[nc[of]]    = TMath::Sqrt(1.+angley*angley+anglez*anglez);
757 //      weight[nc[of]]     = w;
758 //      nc[of]++;
759 //       }
760     
761 //     TMath::Sort(nc[of],amp,index,kFALSE);
762 //     Float_t sumamp=0;
763 //     Float_t sumamp2=0;
764 //     Float_t sumw=0;
765 //     //meanlog = amp[index[Int_t(nc[of]*0.33)]];
766 //     meanlog = 50;
767 //     for (Int_t i=int(nc[of]*low+0.5);i<int(nc[of]*up+0.5);i++){
768 //       Float_t ampl      = amp[index[i]]/angular[index[i]];
769 //       ampl              = meanlog*TMath::Log(1.+ampl/meanlog);
770 //       //
771 //       sumw    += weight[index[i]]; 
772 //       sumamp  += weight[index[i]]*ampl;
773 //       sumamp2 += weight[index[i]]*ampl*ampl;
774 //       norm[of]    += angular[index[i]]*weight[index[i]];
775 //     }
776 //     if (sumw<1){ 
777 //       SetdEdx(0);  
778 //     }
779 //     else {
780 //       norm[of] /= sumw;
781 //       mean[of]  = sumamp/sumw;
782 //       sigma[of] = sumamp2/sumw-mean[of]*mean[of];
783 //       if (sigma[of]>0.1) 
784 //      sigma[of] = TMath::Sqrt(sigma[of]);
785 //       else
786 //      sigma[of] = 1000;
787       
788 //     mean[of] = (TMath::Exp(mean[of]/meanlog)-1)*meanlog;
789 //     //mean  *=(1-0.02*(sigma/(mean*0.17)-1.));
790 //     //mean *=(1-0.1*(norm-1.));
791 //     }
792 //   }
793
794 //   Float_t dedx =0;
795 //   fSdEdx =0;
796 //   fMAngular =0;
797 //   //  mean[0]*= (1-0.05*(sigma[0]/(0.01+mean[1]*0.18)-1));
798 //   //  mean[1]*= (1-0.05*(sigma[1]/(0.01+mean[0]*0.18)-1));
799
800   
801 //   //  dedx = (mean[0]* TMath::Sqrt((1.+nc[0]))+ mean[1]* TMath::Sqrt((1.+nc[1])) )/ 
802 //   //  (  TMath::Sqrt((1.+nc[0]))+TMath::Sqrt((1.+nc[1])));
803
804 //   Int_t norm2 = 0;
805 //   Int_t norm3 = 0;
806 //   for (Int_t i =0;i<4;i++){
807 //     if (nc[i]>2&&nc[i]<1000){
808 //       dedx      += mean[i] *nc[i];
809 //       fSdEdx    += sigma[i]*(nc[i]-2);
810 //       fMAngular += norm[i] *nc[i];    
811 //       norm2     += nc[i];
812 //       norm3     += nc[i]-2;
813 //     }
814 //     fDEDX[i]  = mean[i];             
815 //     fSDEDX[i] = sigma[i];            
816 //     fNCDEDX[i]= nc[i]; 
817 //   }
818
819 //   if (norm3>0){
820 //     dedx   /=norm2;
821 //     fSdEdx /=norm3;
822 //     fMAngular/=norm2;
823 //   }
824 //   else{
825 //     SetdEdx(0);
826 //     return 0;
827 //   }
828 //   //  Float_t dedx1 =dedx;
829 //   /*
830 //   dedx =0;
831 //   for (Int_t i =0;i<4;i++){
832 //     if (nc[i]>2&&nc[i]<1000){
833 //       mean[i]   = mean[i]*(1-0.12*(sigma[i]/(fSdEdx)-1.));
834 //       dedx      += mean[i] *nc[i];
835 //     }
836 //     fDEDX[i]  = mean[i];                
837 //   }
838 //   dedx /= norm2;
839 //   */
840
841   
842 //   SetdEdx(dedx);
843 //   return dedx;
844 }
845
846 void AliTPCseed::CookPID()
847 {
848   //
849   // cook PID information according dEdx
850   //
851   Double_t fRange = 10.;
852   Double_t fRes   = 0.1;
853   Double_t fMIP   = 47.;
854   //
855   Int_t ns=AliPID::kSPECIES;
856   Double_t sumr =0;
857   for (Int_t j=0; j<ns; j++) {
858     Double_t mass=AliPID::ParticleMass(j);
859     Double_t mom=GetP();
860     Double_t dedx=fdEdx/fMIP;
861     Double_t bethe=AliMathBase::BetheBlochAleph(mom/mass); 
862     Double_t sigma=fRes*bethe;
863     if (sigma>0.001){
864       if (TMath::Abs(dedx-bethe) > fRange*sigma) {
865         fTPCr[j]=TMath::Exp(-0.5*fRange*fRange)/sigma;
866         sumr+=fTPCr[j];
867         continue;
868       }
869       fTPCr[j]=TMath::Exp(-0.5*(dedx-bethe)*(dedx-bethe)/(sigma*sigma))/sigma;
870       sumr+=fTPCr[j];
871     }
872     else{
873       fTPCr[j]=1.;
874       sumr+=fTPCr[j];
875     }
876   }
877   for (Int_t j=0; j<ns; j++) {
878     fTPCr[j]/=sumr;           //normalize
879   }
880 }
881
882 Double_t AliTPCseed::GetYat(Double_t xk) const {
883 //-----------------------------------------------------------------
884 // This function calculates the Y-coordinate of a track at the plane x=xk.
885 //-----------------------------------------------------------------
886   if (TMath::Abs(GetSnp())>AliTPCReconstructor::GetMaxSnpTrack()) return 0.; //patch 01 jan 06
887     Double_t c1=GetSnp(), r1=TMath::Sqrt((1.-c1)*(1.+c1));
888     Double_t c2=c1+GetC()*(xk-GetX());
889     if (TMath::Abs(c2)>AliTPCReconstructor::GetMaxSnpTrack()) return 0;
890     Double_t r2=TMath::Sqrt((1.-c2)*(1.+c2));
891     return GetY() + (xk-GetX())*(c1+c2)/(r1+r2);
892 }
893
894 void AliTPCseed::SetClusterMapBit(int ibit, Bool_t state)
895 {
896   fClusterMap[ibit] = state;
897 }
898 Bool_t AliTPCseed::GetClusterMapBit(int ibit)
899 {
900   return fClusterMap[ibit];
901 }
902 void AliTPCseed::SetSharedMapBit(int ibit, Bool_t state)
903 {
904   fSharedMap[ibit] = state;
905 }
906 Bool_t AliTPCseed::GetSharedMapBit(int ibit)
907 {
908   return fSharedMap[ibit];
909 }
910
911
912
913
914
915 Float_t  AliTPCseed::CookdEdxNorm(Double_t low, Double_t up, Int_t type, Int_t i1, Int_t i2, Bool_t shapeNorm,Int_t posNorm, Int_t padNorm, Int_t returnVal){
916  
917   //
918   // calculates dedx using the cluster
919   // low    -  up specify trunc mean range  - default form 0-0.7
920   // type   -  1 - max charge  or 0- total charge in cluster 
921   //           //2- max no corr 3- total+ correction
922   // i1-i2  -  the pad-row range used for calculation
923   // shapeNorm - kTRUE  -taken from OCDB
924   //           
925   // posNorm   - usage of pos normalization 
926   // padNorm   - pad type normalization
927   // returnVal - 0 return mean
928   //           - 1 return RMS
929   //           - 2 return number of clusters
930   //           
931   // normalization parametrization taken from AliTPCClusterParam
932   //
933   AliTPCClusterParam * parcl = AliTPCcalibDB::Instance()->GetClusterParam();
934   AliTPCParam * param = AliTPCcalibDB::Instance()->GetParameters();
935   if (!parcl)  return 0;
936   if (!param) return 0;
937   Int_t row0 = param->GetNRowLow();
938   Int_t row1 = row0+param->GetNRowUp1();
939
940   Float_t amp[160];
941   Int_t   indexes[160];
942   Int_t   ncl=0;
943   //
944   //
945   Float_t gainGG      = 1;  // gas gain factor -always enabled
946   Float_t gainPad     = 1;  // gain map  - used always
947   Float_t corrShape   = 1;  // correction due angular effect, diffusion and electron attachment
948   Float_t corrPos     = 1;  // local position correction - if posNorm enabled
949   Float_t corrPadType = 1;  // pad type correction - if padNorm enabled
950   Float_t corrNorm    = 1;  // normalization factor - set Q to channel 50
951   //   
952   //
953   //
954   if (AliTPCcalibDB::Instance()->GetParameters()){
955     gainGG= AliTPCcalibDB::Instance()->GetParameters()->GetGasGain()/20000;  //relative gas gain
956   }
957
958   const Float_t ktany = TMath::Tan(TMath::DegToRad()*10);
959   const Float_t kedgey =3.;
960   //
961   //
962   for (Int_t irow=i1; irow<i2; irow++){
963     AliTPCclusterMI* cluster = GetClusterPointer(irow);
964     if (!cluster) continue;
965     if (TMath::Abs(cluster->GetY())>cluster->GetX()*ktany-kedgey) continue; // edge cluster
966     Float_t charge= (type%2)? cluster->GetMax():cluster->GetQ();
967     Int_t  ipad= 0;
968     if (irow>=row0) ipad=1;
969     if (irow>=row1) ipad=2;    
970     //
971     //
972     //
973     AliTPCCalPad * gainMap =  AliTPCcalibDB::Instance()->GetDedxGainFactor();
974     if (gainMap) {
975       //
976       // Get gainPad - pad by pad calibration
977       //
978       Float_t factor = 1;      
979       AliTPCCalROC * roc = gainMap->GetCalROC(cluster->GetDetector());
980       if (irow < row0) { // IROC
981         factor = roc->GetValue(irow, TMath::Nint(cluster->GetPad()));
982       } else {         // OROC
983         factor = roc->GetValue(irow - row0, TMath::Nint(cluster->GetPad()));
984       }
985       if (factor>0.5) gainPad=factor;
986     }
987     //
988     //do position and angular normalization
989     //
990     if (shapeNorm){
991       if (type<=1){
992         //      
993         AliTPCTrackerPoint * point = GetTrackPoint(irow);
994         Float_t              ty = TMath::Abs(point->GetAngleY());
995         Float_t              tz = TMath::Abs(point->GetAngleZ()*TMath::Sqrt(1+ty*ty));
996         
997         Float_t dr    = (250.-TMath::Abs(cluster->GetZ()))/250.;
998         corrShape  = parcl->Qnorm(ipad,type,dr,ty,tz);
999       }
1000     }
1001     
1002     if (posNorm>0){
1003       //
1004       // Do position normalization - relative distance to 
1005       // center of pad- time bin
1006       // Work in progress
1007       //      corrPos = parcl->QnormPos(ipad,type, cluster->GetPad(),
1008       //                                cluster->GetTimeBin(), cluster->GetZ(),
1009       //                                cluster->GetSigmaY2(),cluster->GetSigmaZ2(),
1010       //                                cluster->GetMax(),cluster->GetQ());
1011       // scaled response function
1012       Float_t yres0 = parcl->GetRMS0(0,ipad,0,0)/param->GetPadPitchWidth(cluster->GetDetector());
1013       Float_t zres0 = parcl->GetRMS0(1,ipad,0,0)/param->GetZWidth();
1014       //
1015       
1016       AliTPCTrackerPoint * point = GetTrackPoint(irow);
1017       Float_t              ty = TMath::Abs(point->GetAngleY());
1018       Float_t              tz = TMath::Abs(point->GetAngleZ()*TMath::Sqrt(1+ty*ty));
1019       
1020       if (type==1) corrPos = 
1021         parcl->QmaxCorrection(cluster->GetDetector(), cluster->GetRow(),cluster->GetPad(), 
1022                               cluster->GetTimeBin(),ty,tz,yres0,zres0,0.4);
1023       if (type==0) corrPos = 
1024         parcl->QtotCorrection(cluster->GetDetector(), cluster->GetRow(),cluster->GetPad(), 
1025                               cluster->GetTimeBin(),ty,tz,yres0,zres0,cluster->GetQ(),2.5,0.4);
1026       if (posNorm==3){
1027         Float_t dr    = (250.-TMath::Abs(cluster->GetZ()))/250.;
1028         Double_t signtgl = (cluster->GetZ()*point->GetAngleZ()>0)? 1:-1;
1029         Double_t p2 = TMath::Abs(TMath::Sin(TMath::ATan(ty)));
1030         Float_t corrHis = parcl->QnormHis(ipad,type,dr,p2,TMath::Abs(point->GetAngleZ())*signtgl);
1031         if (corrHis>0) corrPos*=corrHis;
1032       }
1033
1034     }
1035
1036     if (padNorm==1){
1037       //taken from OCDB
1038       if (type==0 && parcl->QpadTnorm()) corrPadType = (*parcl->QpadTnorm())[ipad];
1039       if (type==1 && parcl->QpadMnorm()) corrPadType = (*parcl->QpadMnorm())[ipad];
1040
1041     }
1042     if (padNorm==2){
1043       corrPadType  =param->GetPadPitchLength(cluster->GetDetector(),cluster->GetRow());
1044       //use hardwired - temp fix
1045       if (type==0) corrNorm=3.;
1046       if (type==1) corrNorm=1.;
1047     }
1048     //
1049     amp[ncl]=charge;
1050     amp[ncl]/=gainGG;
1051     amp[ncl]/=gainPad;
1052     amp[ncl]/=corrShape;
1053     amp[ncl]/=corrPadType;
1054     amp[ncl]/=corrPos;
1055     amp[ncl]/=corrNorm; 
1056     //
1057     ncl++;
1058   }
1059
1060   if (type>3) return ncl; 
1061   TMath::Sort(ncl,amp, indexes, kFALSE);
1062
1063   if (ncl<10) return 0;
1064   
1065   Float_t suma=0;
1066   Float_t suma2=0;  
1067   Float_t sumn=0;
1068   Int_t icl0=TMath::Nint(ncl*low);
1069   Int_t icl1=TMath::Nint(ncl*up);
1070   for (Int_t icl=icl0; icl<icl1;icl++){
1071     suma+=amp[indexes[icl]];
1072     suma2+=amp[indexes[icl]]*amp[indexes[icl]];
1073     sumn++;
1074   }
1075   Float_t mean =suma/sumn;
1076   Float_t rms  =TMath::Sqrt(TMath::Abs(suma2/sumn-mean*mean));
1077   //
1078   // do time-dependent correction for pressure and temperature variations
1079   UInt_t runNumber = 1;
1080   Float_t corrTimeGain = 1;
1081   AliTPCTransform * trans = AliTPCcalibDB::Instance()->GetTransform();
1082   const AliTPCRecoParam * recoParam = AliTPCcalibDB::Instance()->GetTransform()->GetCurrentRecoParam();
1083   if (trans && recoParam->GetUseGainCorrectionTime()>0) {
1084     runNumber = trans->GetCurrentRunNumber();
1085     //AliTPCcalibDB::Instance()->SetRun(runNumber);
1086     TObjArray * timeGainSplines = AliTPCcalibDB::Instance()->GetTimeGainSplinesRun(runNumber);
1087     if (timeGainSplines) {
1088       UInt_t time = trans->GetCurrentTimeStamp();
1089       AliSplineFit * fitMIP = (AliSplineFit *) timeGainSplines->At(0);
1090       AliSplineFit * fitFPcosmic = (AliSplineFit *) timeGainSplines->At(1);
1091       if (fitMIP) {
1092         corrTimeGain = AliTPCcalibDButil::EvalGraphConst(fitMIP, time);/*fitMIP->Eval(time);*/
1093       } else {
1094         if (fitFPcosmic) corrTimeGain = AliTPCcalibDButil::EvalGraphConst(fitFPcosmic, time);/*fitFPcosmic->Eval(time);*/ 
1095       }
1096     }
1097   }
1098   mean /= corrTimeGain;
1099   rms /= corrTimeGain;
1100   //
1101   if (returnVal==1) return rms;
1102   if (returnVal==2) return ncl;
1103   return mean;
1104 }
1105
1106 Float_t  AliTPCseed::CookdEdxAnalytical(Double_t low, Double_t up, Int_t type, Int_t i1, Int_t i2, Int_t returnVal){
1107  
1108   //
1109   // calculates dedx using the cluster
1110   // low    -  up specify trunc mean range  - default form 0-0.7
1111   // type   -  1 - max charge  or 0- total charge in cluster 
1112   //           //2- max no corr 3- total+ correction
1113   // i1-i2  -  the pad-row range used for calculation
1114   //           
1115   // posNorm   - usage of pos normalization 
1116   // returnVal - 0 return mean
1117   //           - 1 return RMS
1118   //           - 2 return number of clusters
1119   //           
1120   // normalization parametrization taken from AliTPCClusterParam
1121   //
1122   AliTPCClusterParam * parcl = AliTPCcalibDB::Instance()->GetClusterParam();
1123   AliTPCParam * param = AliTPCcalibDB::Instance()->GetParameters();
1124   if (!parcl)  return 0;
1125   if (!param) return 0;
1126   Int_t row0 = param->GetNRowLow();
1127   Int_t row1 = row0+param->GetNRowUp1();
1128
1129   Float_t amp[160];
1130   Int_t   indexes[160];
1131   Int_t   ncl=0;
1132   //
1133   //
1134   Float_t gainGG      = 1;  // gas gain factor -always enabled
1135   Float_t gainPad     = 1;  // gain map  - used always
1136   Float_t corrPos     = 1;  // local position correction - if posNorm enabled
1137
1138   //   
1139   //
1140   //
1141   if (AliTPCcalibDB::Instance()->GetParameters()){
1142     gainGG= AliTPCcalibDB::Instance()->GetParameters()->GetGasGain()/20000;  //relative gas gain
1143   }
1144
1145   const Float_t ktany = TMath::Tan(TMath::DegToRad()*10);
1146   const Float_t kedgey =3.;
1147   //
1148   //
1149   for (Int_t irow=i1; irow<i2; irow++){
1150     AliTPCclusterMI* cluster = GetClusterPointer(irow);
1151     if (!cluster) continue;
1152     if (TMath::Abs(cluster->GetY())>cluster->GetX()*ktany-kedgey) continue; // edge cluster
1153     Float_t charge= (type%2)? cluster->GetMax():cluster->GetQ();
1154     Int_t  ipad= 0;
1155     if (irow>=row0) ipad=1;
1156     if (irow>=row1) ipad=2;    
1157     //
1158     //
1159     //
1160     AliTPCCalPad * gainMap =  AliTPCcalibDB::Instance()->GetDedxGainFactor();
1161     if (gainMap) {
1162       //
1163       // Get gainPad - pad by pad calibration
1164       //
1165       Float_t factor = 1;      
1166       AliTPCCalROC * roc = gainMap->GetCalROC(cluster->GetDetector());
1167       if (irow < row0) { // IROC
1168         factor = roc->GetValue(irow, TMath::Nint(cluster->GetPad()));
1169       } else {         // OROC
1170         factor = roc->GetValue(irow - row0, TMath::Nint(cluster->GetPad()));
1171       }
1172       if (factor>0.5) gainPad=factor;
1173     }
1174     
1175     //
1176     // Do position normalization - relative distance to 
1177     // center of pad- time bin
1178     
1179     AliTPCTrackerPoint * point = GetTrackPoint(irow);
1180     Float_t              ty = TMath::Abs(point->GetAngleY());
1181     Float_t              tz = TMath::Abs(point->GetAngleZ()*TMath::Sqrt(1+ty*ty));
1182     Float_t yres0 = parcl->GetRMS0(0,ipad,0,0)/param->GetPadPitchWidth(cluster->GetDetector());
1183     Float_t zres0 = parcl->GetRMS0(1,ipad,0,0)/param->GetZWidth();
1184
1185     yres0 *=parcl->GetQnormCorr(ipad, type,0);
1186     zres0 *=parcl->GetQnormCorr(ipad, type,1);
1187     Float_t effLength=parcl->GetQnormCorr(ipad, type,4)*0.5;
1188     Float_t effDiff  =(parcl->GetQnormCorr(ipad, type,2)+parcl->GetQnormCorr(ipad, type,3))*0.5;
1189     //
1190     if (type==1) {
1191       corrPos = parcl->GetQnormCorr(ipad, type,5)*
1192         parcl->QmaxCorrection(cluster->GetDetector(), cluster->GetRow(),cluster->GetPad(), 
1193                               cluster->GetTimeBin(),ty,tz,yres0,zres0,effLength,effDiff);
1194       Float_t drm   = 0.5-TMath::Abs(cluster->GetZ()/250.);
1195       corrPos*=(1+parcl->GetQnormCorr(ipad, type+2,0)*drm);
1196       corrPos*=(1+parcl->GetQnormCorr(ipad, type+2,1)*ty*ty);
1197       corrPos*=(1+parcl->GetQnormCorr(ipad, type+2,2)*tz*tz);
1198       //
1199     }
1200     if (type==0) {
1201       corrPos = parcl->GetQnormCorr(ipad, type,5)*
1202         parcl->QtotCorrection(cluster->GetDetector(), cluster->GetRow(),cluster->GetPad(), 
1203                               cluster->GetTimeBin(),ty,tz,yres0,zres0,cluster->GetQ(),2.5,effLength,effDiff);
1204       
1205       Float_t drm   = 0.5-TMath::Abs(cluster->GetZ()/250.);
1206       corrPos*=(1+parcl->GetQnormCorr(ipad, type+2,0)*drm);
1207       corrPos*=(1+parcl->GetQnormCorr(ipad, type+2,1)*ty*ty);
1208       corrPos*=(1+parcl->GetQnormCorr(ipad, type+2,2)*tz*tz);
1209       //
1210     }
1211
1212     //
1213     amp[ncl]=charge;
1214     amp[ncl]/=gainGG;
1215     amp[ncl]/=gainPad;
1216     amp[ncl]/=corrPos;
1217     //
1218     ncl++;
1219   }
1220
1221   if (type>3) return ncl; 
1222   TMath::Sort(ncl,amp, indexes, kFALSE);
1223
1224   if (ncl<10) return 0;
1225   
1226   Float_t suma=0;
1227   Float_t suma2=0;  
1228   Float_t sumn=0;
1229   Int_t icl0=TMath::Nint(ncl*low);
1230   Int_t icl1=TMath::Nint(ncl*up);
1231   for (Int_t icl=icl0; icl<icl1;icl++){
1232     suma+=amp[indexes[icl]];
1233     suma2+=amp[indexes[icl]]*amp[indexes[icl]];
1234     sumn++;
1235   }
1236   Float_t mean =suma/sumn;
1237   Float_t rms  =TMath::Sqrt(TMath::Abs(suma2/sumn-mean*mean));
1238   //
1239   // do time-dependent correction for pressure and temperature variations
1240   UInt_t runNumber = 1;
1241   Float_t corrTimeGain = 1;
1242   AliTPCTransform * trans = AliTPCcalibDB::Instance()->GetTransform();
1243   const AliTPCRecoParam * recoParam = AliTPCcalibDB::Instance()->GetTransform()->GetCurrentRecoParam();
1244   if (trans && recoParam->GetUseGainCorrectionTime()>0) {
1245     runNumber = trans->GetCurrentRunNumber();
1246     //AliTPCcalibDB::Instance()->SetRun(runNumber);
1247     TObjArray * timeGainSplines = AliTPCcalibDB::Instance()->GetTimeGainSplinesRun(runNumber);
1248     if (timeGainSplines) {
1249       UInt_t time = trans->GetCurrentTimeStamp();
1250       AliSplineFit * fitMIP = (AliSplineFit *) timeGainSplines->At(0);
1251       AliSplineFit * fitFPcosmic = (AliSplineFit *) timeGainSplines->At(1);
1252       if (fitMIP) {
1253         corrTimeGain = fitMIP->Eval(time);
1254       } else {
1255         if (fitFPcosmic) corrTimeGain = fitFPcosmic->Eval(time); // This value describes the ratio FP-to-MIP, hardwired for the moment
1256       }
1257     }
1258   }
1259   mean /= corrTimeGain;
1260   rms /= corrTimeGain;
1261   //
1262   if (returnVal==1) return rms;
1263   if (returnVal==2) return ncl;
1264   return mean;
1265 }
1266
1267
1268
1269
1270 Float_t  AliTPCseed::CookShape(Int_t type){
1271   //
1272   //
1273   //
1274  //-----------------------------------------------------------------
1275   // This funtion calculates dE/dX within the "low" and "up" cuts.
1276   //-----------------------------------------------------------------
1277   Float_t means=0;
1278   Float_t meanc=0;
1279   for (Int_t i =0; i<160;i++)    {
1280     AliTPCTrackerPoint * point = GetTrackPoint(i);
1281     if (point==0) continue;
1282
1283     AliTPCclusterMI * cl = fClusterPointer[i];
1284     if (cl==0) continue;        
1285     
1286     Float_t rsigmay =  TMath::Sqrt(point->GetSigmaY());
1287     Float_t rsigmaz =  TMath::Sqrt(point->GetSigmaZ());
1288     Float_t rsigma =   (rsigmay+rsigmaz)*0.5;
1289     if (type==0) means+=rsigma;
1290     if (type==1) means+=rsigmay;
1291     if (type==2) means+=rsigmaz;
1292     meanc++;
1293   }
1294   Float_t mean = (meanc>0)? means/meanc:0;
1295   return mean;
1296 }
1297
1298
1299
1300 Int_t  AliTPCseed::RefitTrack(AliTPCseed *seed, AliExternalTrackParam * parin, AliExternalTrackParam * parout){
1301   //
1302   // Refit the track
1303   // return value - number of used clusters
1304   // 
1305   //
1306   const Int_t kMinNcl =10;
1307   AliTPCseed *track=new AliTPCseed(*seed);
1308   Int_t sector=-1;
1309   // reset covariance
1310   //
1311   Double_t covar[15];
1312   for (Int_t i=0;i<15;i++) covar[i]=0;
1313   covar[0]=10.*10.;
1314   covar[2]=10.*10.;
1315   covar[5]=10.*10./(64.*64.);
1316   covar[9]=10.*10./(64.*64.);
1317   covar[14]=1*1;
1318   //
1319
1320   Float_t xmin=1000, xmax=-10000;
1321   Int_t imin=158, imax=0;
1322   for (Int_t i=0;i<160;i++) {
1323     AliTPCclusterMI *c=track->GetClusterPointer(i);
1324     if (!c) continue;
1325     if (sector<0) sector = c->GetDetector();
1326     if (c->GetX()<xmin) xmin=c->GetX();
1327     if (c->GetX()>xmax) xmax=c->GetX();
1328     if (i<imin) imin=i;
1329     if (i>imax) imax=i;
1330   }
1331   if(imax-imin<kMinNcl) {
1332     delete track;
1333     return 0 ;
1334   }
1335   // Not succes to rotate
1336   if (!track->Rotate(TMath::DegToRad()*(sector%18*20.+10.)-track->GetAlpha())) {
1337     delete track;
1338     return 0;
1339   }
1340   //
1341   //
1342   // fit from inner to outer row
1343   //
1344   AliExternalTrackParam paramIn;
1345   AliExternalTrackParam paramOut;
1346   Bool_t isOK=kTRUE;
1347   Int_t ncl=0;
1348   //
1349   //
1350   //
1351   for (Int_t i=imin; i<=imax; i++){
1352     AliTPCclusterMI *c=track->GetClusterPointer(i);
1353     if (!c) continue;
1354     //    if (RejectCluster(c,track)) continue;
1355     sector = (c->GetDetector()%18);
1356     if (!track->Rotate(TMath::DegToRad()*(sector%18*20.+10.)-track->GetAlpha())) {
1357       //continue;
1358     }
1359     Double_t r[3]={c->GetX(),c->GetY(),c->GetZ()};
1360     Double_t cov[3]={0.01,0.,0.01}; //TODO: correct error parametrisation
1361     if (!track->PropagateTo(r[0])) {
1362       isOK=kFALSE;
1363     }
1364     if ( !((static_cast<AliExternalTrackParam*>(track)->Update(&r[1],cov)))) isOK=kFALSE;
1365   }
1366   if (!isOK) { delete track; return 0;}
1367   track->AddCovariance(covar);
1368   //
1369   //
1370   //
1371   for (Int_t i=imax; i>=imin; i--){
1372     AliTPCclusterMI *c=track->GetClusterPointer(i);
1373     if (!c) continue;
1374     //if (RejectCluster(c,track)) continue;
1375     sector = (c->GetDetector()%18);
1376     if (!track->Rotate(TMath::DegToRad()*(sector%18*20.+10.)-track->GetAlpha())) {
1377       //continue;
1378     }
1379     Double_t r[3]={c->GetX(),c->GetY(),c->GetZ()};
1380     Double_t cov[3]={0.01,0.,0.01}; //TODO: correct error parametrisation
1381     if (!track->PropagateTo(r[0])) {
1382       isOK=kFALSE;
1383     }
1384     if ( !((static_cast<AliExternalTrackParam*>(track)->Update(&r[1],cov)))) isOK=kFALSE;
1385   }
1386   //if (!isOK) { delete track; return 0;}
1387   paramIn = *track;
1388   track->AddCovariance(covar);
1389   //
1390   //
1391   for (Int_t i=imin; i<=imax; i++){
1392     AliTPCclusterMI *c=track->GetClusterPointer(i);
1393     if (!c) continue;
1394     sector = (c->GetDetector()%18);
1395     if (!track->Rotate(TMath::DegToRad()*(sector%18*20.+10.)-track->GetAlpha())) {
1396       //continue;
1397     }
1398     ncl++;
1399     //if (RejectCluster(c,track)) continue;
1400     Double_t r[3]={c->GetX(),c->GetY(),c->GetZ()};
1401     Double_t cov[3]={0.01,0.,0.01}; //TODO: correct error parametrisation
1402     if (!track->PropagateTo(r[0])) {
1403       isOK=kFALSE;
1404     }
1405     if ( !((static_cast<AliExternalTrackParam*>(track)->Update(&r[1],cov)))) isOK=kFALSE;
1406   }
1407   //if (!isOK) { delete track; return 0;}
1408   paramOut=*track;
1409   //
1410   //
1411   //
1412   if (parin) (*parin)=paramIn;
1413   if (parout) (*parout)=paramOut;
1414   delete track;
1415   return ncl;
1416 }
1417
1418
1419
1420 Bool_t AliTPCseed::RefitTrack(AliTPCseed* /*seed*/, Bool_t /*out*/){
1421   //
1422   //
1423   //
1424   return kFALSE;
1425 }
1426
1427
1428
1429
1430
1431
1432 void  AliTPCseed::GetError(AliTPCclusterMI* cluster, AliExternalTrackParam * param, 
1433                                   Double_t& erry, Double_t &errz)
1434 {
1435   //
1436   // Get cluster error at given position
1437   //
1438   AliTPCClusterParam *clusterParam = AliTPCcalibDB::Instance()->GetClusterParam();
1439   Double_t tany,tanz;  
1440   Double_t snp1=param->GetSnp();
1441   tany=snp1/TMath::Sqrt((1.-snp1)*(1.+snp1));
1442   //
1443   Double_t tgl1=param->GetTgl();
1444   tanz=tgl1/TMath::Sqrt((1.-snp1)*(1.+snp1));
1445   //
1446   Int_t padSize = 0;                          // short pads
1447   if (cluster->GetDetector() >= 36) {
1448     padSize = 1;                              // medium pads 
1449     if (cluster->GetRow() > 63) padSize = 2; // long pads
1450   }
1451
1452   erry  = clusterParam->GetError0Par( 0, padSize, (250.0 - TMath::Abs(cluster->GetZ())), TMath::Abs(tany) );
1453   errz  = clusterParam->GetError0Par( 1, padSize, (250.0 - TMath::Abs(cluster->GetZ())), TMath::Abs(tanz) );
1454 }
1455
1456
1457 void  AliTPCseed::GetShape(AliTPCclusterMI* cluster, AliExternalTrackParam * param, 
1458                                   Double_t& rmsy, Double_t &rmsz)
1459 {
1460   //
1461   // Get cluster error at given position
1462   //
1463   AliTPCClusterParam *clusterParam = AliTPCcalibDB::Instance()->GetClusterParam();
1464   Double_t tany,tanz;  
1465   Double_t snp1=param->GetSnp();
1466   tany=snp1/TMath::Sqrt((1.-snp1)*(1.+snp1));
1467   //
1468   Double_t tgl1=param->GetTgl();
1469   tanz=tgl1/TMath::Sqrt((1.-snp1)*(1.+snp1));
1470   //
1471   Int_t padSize = 0;                          // short pads
1472   if (cluster->GetDetector() >= 36) {
1473     padSize = 1;                              // medium pads 
1474     if (cluster->GetRow() > 63) padSize = 2; // long pads
1475   }
1476
1477   rmsy  = clusterParam->GetRMSQ( 0, padSize, (250.0 - TMath::Abs(cluster->GetZ())), TMath::Abs(tany), TMath::Abs(cluster->GetMax()) );
1478   rmsz  = clusterParam->GetRMSQ( 1, padSize, (250.0 - TMath::Abs(cluster->GetZ())), TMath::Abs(tanz) ,TMath::Abs(cluster->GetMax()));
1479 }
1480
1481
1482
1483 Double_t AliTPCseed::GetQCorrGeom(Float_t ty, Float_t tz){
1484   //Geoetrical
1485   //ty    - tangent in local y direction
1486   //tz    - tangent 
1487   //
1488   Float_t norm=TMath::Sqrt(1+ty*ty+tz*tz);
1489   return norm;
1490 }
1491
1492 Double_t AliTPCseed::GetQCorrShape(Int_t ipad, Int_t type,Float_t z, Float_t ty, Float_t tz, Float_t /*q*/, Float_t /*thr*/){
1493   //
1494   // Q normalization
1495   //
1496   // return value =  Q Normalization factor
1497   // Normalization - 1 - shape factor part for full drift          
1498   //                 1 - electron attachment for 0 drift
1499
1500   // Input parameters:
1501   //
1502   // ipad - 0 short pad
1503   //        1 medium pad
1504   //        2 long pad
1505   //
1506   // type - 0 qmax
1507   //      - 1 qtot
1508   //
1509   //z     - z position (-250,250 cm)
1510   //ty    - tangent in local y direction
1511   //tz    - tangent 
1512   //
1513
1514   AliTPCClusterParam * paramCl = AliTPCcalibDB::Instance()->GetClusterParam();
1515   AliTPCParam   * paramTPC = AliTPCcalibDB::Instance()->GetParameters();
1516  
1517   if (!paramCl) return 1;
1518   //
1519   Double_t dr =  250.-TMath::Abs(z); 
1520   Double_t sy =  paramCl->GetRMS0( 0,ipad, dr, TMath::Abs(ty));
1521   Double_t sy0=  paramCl->GetRMS0(0,ipad, 250, 0);
1522   Double_t sz =  paramCl->GetRMS0( 1,ipad, dr, TMath::Abs(tz));
1523   Double_t sz0=  paramCl->GetRMS0(1,ipad, 250, 0);
1524
1525   Double_t sfactorMax = TMath::Sqrt(sy0*sz0/(sy*sz));
1526
1527  
1528   Double_t dt = 1000000*(dr/paramTPC->GetDriftV());  //time in microsecond
1529   Double_t attProb = TMath::Exp(-paramTPC->GetAttCoef()*paramTPC->GetOxyCont()*dt);
1530   //
1531   //
1532   if (type==0) return sfactorMax*attProb;
1533
1534   return attProb;
1535
1536
1537 }
1538