]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDseedV1.cxx
cosmetic changes to the code in which the number of time bins have been introduced...
[u/mrichter/AliRoot.git] / TRD / AliTRDseedV1.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ////////////////////////////////////////////////////////////////////////////
19 //                                                                        //
20 //  The TRD track seed                                                    //
21 //                                                                        //
22 //  Authors:                                                              //
23 //    Alex Bercuci <A.Bercuci@gsi.de>                                     //
24 //    Markus Fasel <M.Fasel@gsi.de>                                       //
25 //                                                                        //
26 ////////////////////////////////////////////////////////////////////////////
27
28 #include "TMath.h"
29 #include "TLinearFitter.h"
30 #include "TClonesArray.h" // tmp
31 #include <TTreeStream.h>
32
33 #include "AliLog.h"
34 #include "AliMathBase.h"
35
36 #include "AliTRDseedV1.h"
37 #include "AliTRDcluster.h"
38 #include "AliTRDtrack.h"
39 #include "AliTRDcalibDB.h"
40 #include "AliTRDchamberTimeBin.h"
41 #include "AliTRDtrackingChamber.h"
42 #include "AliTRDtrackerV1.h"
43 #include "AliTRDReconstructor.h"
44 #include "AliTRDrecoParam.h"
45 #include "AliTRDgeometry.h"
46 #include "Cal/AliTRDCalPID.h"
47
48 ClassImp(AliTRDseedV1)
49
50 //____________________________________________________________________
51 AliTRDseedV1::AliTRDseedV1(Int_t plane) 
52   :AliTRDseed()
53   ,fPlane(plane)
54   ,fOwner(kFALSE)
55   ,fMom(0.)
56   ,fSnp(0.)
57   ,fTgl(0.)
58   ,fdX(0.)
59 {
60   //
61   // Constructor
62   //
63         for(int islice=0; islice < knSlices; islice++) fdEdx[islice] = 0.;
64         for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) fProb[ispec]  = -1.;
65 }
66
67 //____________________________________________________________________
68 AliTRDseedV1::AliTRDseedV1(const AliTRDseedV1 &ref)
69   :AliTRDseed((AliTRDseed&)ref)
70   ,fPlane(ref.fPlane)
71   ,fOwner(kFALSE)
72   ,fMom(ref.fMom)
73   ,fSnp(ref.fSnp)
74   ,fTgl(ref.fTgl)
75   ,fdX(ref.fdX)
76 {
77   //
78   // Copy Constructor performing a deep copy
79   //
80
81         //AliInfo("");
82         if(ref.fOwner) SetOwner();
83         for(int islice=0; islice < knSlices; islice++) fdEdx[islice] = ref.fdEdx[islice];
84         for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) fProb[ispec] = ref.fProb[ispec];
85 }
86
87
88 //____________________________________________________________________
89 AliTRDseedV1& AliTRDseedV1::operator=(const AliTRDseedV1 &ref)
90 {
91   //
92   // Assignment Operator using the copy function
93   //
94
95         //AliInfo("");
96         if(this != &ref){
97                 ref.Copy(*this);
98         }
99         return *this;
100
101 }
102
103 //____________________________________________________________________
104 AliTRDseedV1::~AliTRDseedV1()
105 {
106   //
107   // Destructor. The RecoParam object belongs to the underlying tracker.
108   //
109
110         //AliInfo(Form("fOwner[%s]", fOwner?"YES":"NO"));
111
112         if(fOwner) 
113                 for(int itb=0; itb<knTimebins; itb++){
114                         if(!fClusters[itb]) continue; 
115                         //AliInfo(Form("deleting c %p @ %d", fClusters[itb], itb));
116                         delete fClusters[itb];
117                         fClusters[itb] = 0x0;
118                 }
119 }
120
121 //____________________________________________________________________
122 void AliTRDseedV1::Copy(TObject &ref) const
123 {
124   //
125   // Copy function
126   //
127
128         //AliInfo("");
129         AliTRDseedV1 &target = (AliTRDseedV1 &)ref; 
130         
131         target.fPlane         = fPlane;
132         target.fMom           = fMom;
133         target.fSnp           = fSnp;
134         target.fTgl           = fTgl;
135         target.fdX            = fdX;
136         
137         for(int islice=0; islice < knSlices; islice++) target.fdEdx[islice] = fdEdx[islice];
138         for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) target.fProb[ispec] = fProb[ispec];
139         
140         AliTRDseed::Copy(target);
141 }
142
143
144 //____________________________________________________________
145 void AliTRDseedV1::Init(AliTRDtrack *track)
146 {
147 // Initialize this tracklet using the track information
148 //
149 // Parameters:
150 //   track - the TRD track used to initialize the tracklet
151 // 
152 // Detailed description
153 // The function sets the starting point and direction of the
154 // tracklet according to the information from the TRD track.
155 // 
156 // Caution
157 // The TRD track has to be propagated to the beginning of the
158 // chamber where the tracklet will be constructed
159 //
160
161         Double_t y, z; 
162         track->GetProlongation(fX0, y, z);
163         fYref[0] = y;
164         fYref[1] = track->GetSnp()/(1. - track->GetSnp()*track->GetSnp());
165         fZref[0] = z;
166         fZref[1] = track->GetTgl();
167
168         //printf("Tracklet ref x[%7.3f] y[%7.3f] z[%7.3f], snp[%f] tgl[%f]\n", fX0, fYref[0], fZref[0], track->GetSnp(), track->GetTgl());
169 }
170
171
172 //____________________________________________________________________
173 void AliTRDseedV1::CookdEdx(Int_t nslices)
174 {
175 // Calculates average dE/dx for all slices and store them in the internal array fdEdx. 
176 //
177 // Parameters:
178 //  nslices : number of slices for which dE/dx should be calculated
179 // Output:
180 //  store results in the internal array fdEdx. This can be accessed with the method
181 //  AliTRDseedV1::GetdEdx()
182 //
183 // Detailed description
184 // Calculates average dE/dx for all slices. Depending on the PID methode 
185 // the number of slices can be 3 (LQ) or 8(NN). 
186 // The calculation of dQ/dl are done using the tracklet fit results (see AliTRDseedV1::GetdQdl(Int_t)) i.e.
187 //
188 // dQ/dl = qc/(dx * sqrt(1 + dy/dx^2 + dz/dx^2))
189 //
190 // The following effects are included in the calculation:
191 // 1. calibration values for t0 and vdrift (using x coordinate to calculate slice)
192 // 2. cluster sharing (optional see AliTRDrecoParam::SetClusterSharing())
193 // 3. cluster size
194 //
195
196         Int_t nclusters[knSlices];
197         for(int i=0; i<knSlices; i++){ 
198                 fdEdx[i]     = 0.;
199                 nclusters[i] = 0;
200         }
201         Float_t clength = (/*.5 * */AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick());
202
203         AliTRDcluster *cluster = 0x0;
204         for(int ic=0; ic<AliTRDtrackerV1::GetNTimeBins(); ic++){
205                 if(!(cluster = fClusters[ic])) continue;
206                 Float_t x = cluster->GetX();
207                 
208                 // Filter clusters for dE/dx calculation
209                 
210                 // 1.consider calibration effects for slice determination
211                 Int_t slice; 
212                 if(cluster->IsInChamber()) slice = Int_t(TMath::Abs(fX0 - x) * nslices / clength);
213                 else slice = x < fX0 ? 0 : nslices-1;
214                 
215                 // 2. take sharing into account
216                 Float_t w = cluster->IsShared() ? .5 : 1.;
217                 
218                 // 3. take into account large clusters TODO
219                 //w *= c->GetNPads() > 3 ? .8 : 1.;
220                 
221                 //CHECK !!!
222                 fdEdx[slice]   += w * GetdQdl(ic); //fdQdl[ic];
223                 nclusters[slice]++;
224         } // End of loop over clusters
225
226         // calculate mean charge per slice
227         for(int is=0; is<nslices; is++) if(nclusters[is]) fdEdx[is] /= nclusters[is];
228 }
229
230 //____________________________________________________________________
231 Float_t AliTRDseedV1::GetdQdl(Int_t ic) const
232 {
233         return fClusters[ic] ? TMath::Abs(fClusters[ic]->GetQ()) /fdX / TMath::Sqrt(1. + fYfit[1]*fYfit[1] + fZfit[1]*fZfit[1]) : 0.;
234 }
235
236 //____________________________________________________________________
237 Double_t* AliTRDseedV1::GetProbability()
238 {       
239 // Fill probability array for tracklet from the DB.
240 //
241 // Parameters
242 //
243 // Output
244 //   returns pointer to the probability array and 0x0 if missing DB access 
245 //
246 // Detailed description
247
248         
249         // retrive calibration db
250   AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
251   if (!calibration) {
252     AliError("No access to calibration data");
253     return 0x0;
254   }
255
256   // Retrieve the CDB container class with the parametric detector response
257   const AliTRDCalPID *pd = calibration->GetPIDObject(AliTRDReconstructor::RecoParam()->GetPIDMethod());
258   if (!pd) {
259     AliError("No access to AliTRDCalPID object");
260     return 0x0;
261   }
262         
263         // calculate tracklet length TO DO
264   Float_t length = (AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick());
265   /// TMath::Sqrt((1.0 - fSnp[iPlane]*fSnp[iPlane]) / (1.0 + fTgl[iPlane]*fTgl[iPlane]));
266   
267   //calculate dE/dx
268   CookdEdx(AliTRDReconstructor::RecoParam()->GetNdEdxSlices());
269   
270   // Sets the a priori probabilities
271   for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) {
272     fProb[ispec] = pd->GetProbability(ispec, fMom, &fdEdx[0], length, fPlane);  
273   }
274
275         return &fProb[0];
276 }
277
278 //____________________________________________________________________
279 Float_t AliTRDseedV1::GetQuality(Bool_t kZcorr) const
280 {
281   //
282   // Returns a quality measurement of the current seed
283   //
284
285         Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
286         return 
287                   .5 * TMath::Abs(18.0 - fN2)
288                 + 10.* TMath::Abs(fYfit[1] - fYref[1])
289                 + 5. * TMath::Abs(fYfit[0] - fYref[0] + zcorr)
290                 + 2. * TMath::Abs(fMeanz - fZref[0]) / fPadLength;
291 }
292
293 //____________________________________________________________________
294 void AliTRDseedV1::GetCovAt(Double_t /*x*/, Double_t *cov) const
295 {
296 // Computes covariance in the y-z plane at radial point x
297
298         Int_t ic = 0; while (!fClusters[ic]) ic++; 
299   AliTRDcalibDB *fCalib = AliTRDcalibDB::Instance();
300         Double_t exB         = fCalib->GetOmegaTau(fCalib->GetVdriftAverage(fClusters[ic]->GetDetector()), -AliTracker::GetBz()*0.1);
301
302         Double_t sy2    = fSigmaY2*fSigmaY2 + .2*(fYfit[1]-exB)*(fYfit[1]-exB);
303         Double_t sz2    = fPadLength/12.;
304
305
306         //printf("Yfit[1] %f sy20 %f SigmaY2 %f\n", fYfit[1], sy20, fSigmaY2);
307
308         cov[0] = sy2;
309         cov[1] = fTilt*(sy2-sz2);
310         cov[2] = sz2;
311 }
312
313
314 //____________________________________________________________________
315 void AliTRDseedV1::SetOwner(Bool_t own)
316 {
317         //AliInfo(Form("own [%s] fOwner[%s]", own?"YES":"NO", fOwner?"YES":"NO"));
318         
319         if(own){
320                 for(int ic=0; ic<knTimebins; ic++){
321                         if(!fClusters[ic]) continue;
322                         fClusters[ic] = new AliTRDcluster(*fClusters[ic]);
323                 }
324                 fOwner = kTRUE;
325         } else {
326                 if(fOwner){
327                         for(int ic=0; ic<knTimebins; ic++){
328                                 if(!fClusters[ic]) continue;
329                                 delete fClusters[ic];
330                                 //fClusters[ic] = tracker->GetClusters(index) TODO
331                         }
332                 }
333                 fOwner = kFALSE;
334         }
335 }
336
337 //____________________________________________________________________
338 Bool_t  AliTRDseedV1::AttachClustersIter(AliTRDtrackingChamber *chamber, Float_t quality, Bool_t kZcorr, AliTRDcluster *c)
339 {
340   //
341   // Iterative process to register clusters to the seed.
342   // In iteration 0 we try only one pad-row and if quality not
343   // sufficient we try 2 pad-rows (about 5% of tracks cross 2 pad-rows)
344   //
345         // debug level 7
346         //
347         
348         if(!AliTRDReconstructor::RecoParam()){
349                 AliError("Seed can not be used without a valid RecoParam.");
350                 return kFALSE;
351         }
352
353         AliTRDchamberTimeBin *layer = 0x0;
354         if(AliTRDReconstructor::StreamLevel()>=7 && c){
355                 TClonesArray clusters("AliTRDcluster", 24);
356                 clusters.SetOwner(kTRUE);
357                 AliTRDcluster *cc = 0x0;
358                 Int_t det=-1, ncl, ncls = 0;
359                 for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
360                         if(!(layer = chamber->GetTB(iTime))) continue;
361                         if(!(ncl = Int_t(*layer))) continue;
362                         for(int ic=0; ic<ncl; ic++){ 
363                                 cc = (*layer)[ic];
364                                 det = cc->GetDetector();
365                                 new(clusters[ncls++]) AliTRDcluster(*cc);
366                         }
367                 }
368                 AliInfo(Form("N clusters[%d] = %d", fPlane, ncls));
369                 
370                 Int_t ref = c ? 1 : 0;
371                 TTreeSRedirector &cstreamer = *AliTRDtrackerV1::DebugStreamer();
372                 cstreamer << "AttachClustersIter"
373                         << "det="        << det 
374                         << "ref="        << ref 
375                         << "clusters.="  << &clusters
376                         << "tracklet.="  << this
377                         << "cl.="        << c
378                         << "\n";        
379         }
380
381         Float_t  tquality;
382         Double_t kroady = AliTRDReconstructor::RecoParam()->GetRoad1y();
383         Double_t kroadz = fPadLength * .5 + 1.;
384         
385         // initialize configuration parameters
386         Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
387         Int_t   niter = kZcorr ? 1 : 2;
388         
389         Double_t yexp, zexp;
390         Int_t ncl = 0;
391         // start seed update
392         for (Int_t iter = 0; iter < niter; iter++) {
393                 ncl = 0;
394                 for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
395                         if(!(layer = chamber->GetTB(iTime))) continue;
396                         if(!Int_t(*layer)) continue;
397                         
398                         // define searching configuration
399                         Double_t dxlayer = layer->GetX() - fX0;
400                         if(c){
401                                 zexp = c->GetZ();
402                                 //Try 2 pad-rows in second iteration
403                                 if (iter > 0) {
404                                         zexp = fZref[0] + fZref[1] * dxlayer - zcorr;
405                                         if (zexp > c->GetZ()) zexp = c->GetZ() + fPadLength*0.5;
406                                         if (zexp < c->GetZ()) zexp = c->GetZ() - fPadLength*0.5;
407                                 }
408                         } else zexp = fZref[0] + (kZcorr ? fZref[1] * dxlayer : 0.);
409                         yexp  = fYref[0] + fYref[1] * dxlayer - zcorr;
410                         
411                         // Get and register cluster
412                         Int_t    index = layer->SearchNearestCluster(yexp, zexp, kroady, kroadz);
413                         if (index < 0) continue;
414                         AliTRDcluster *cl = (*layer)[index];
415                         
416                         fIndexes[iTime]  = layer->GetGlobalIndex(index);
417                         fClusters[iTime] = cl;
418                         fY[iTime]        = cl->GetY();
419                         fZ[iTime]        = cl->GetZ();
420                         ncl++;
421                 }
422         if(AliTRDReconstructor::StreamLevel()>=7) AliInfo(Form("iter = %d ncl [%d] = %d", iter, fPlane, ncl));
423                 
424                 if(ncl>1){      
425                         // calculate length of the time bin (calibration aware)
426                         Int_t irp = 0; Float_t x[2]; Int_t tb[2];
427                         for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
428                                 if(!fClusters[iTime]) continue;
429                                 x[irp]  = fClusters[iTime]->GetX();
430                                 tb[irp] = iTime;
431                                 irp++;
432                                 if(irp==2) break;
433                         } 
434                         fdX = (x[1] - x[0]) / (tb[0] - tb[1]);
435         
436                         // update X0 from the clusters (calibration/alignment aware)
437                         for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
438                                 if(!(layer = chamber->GetTB(iTime))) continue;
439                                 if(!layer->IsT0()) continue;
440                                 if(fClusters[iTime]){ 
441                                         fX0 = fClusters[iTime]->GetX();
442                                         break;
443                                 } else { // we have to infere the position of the anode wire from the other clusters
444                                         for (Int_t jTime = iTime+1; jTime < AliTRDtrackerV1::GetNTimeBins(); jTime++) {
445                                                 if(!fClusters[jTime]) continue;
446                                                 fX0 = fClusters[jTime]->GetX() + fdX * (jTime - iTime);
447                                         }
448                                         break;
449                                 }
450                         }       
451                         
452                         // update YZ reference point
453                         // TODO
454                         
455                         // update x reference positions (calibration/alignment aware)
456                         for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
457                                 if(!fClusters[iTime]) continue;
458                                 fX[iTime] = fClusters[iTime]->GetX() - fX0;
459                         } 
460                         
461                         AliTRDseed::Update();
462                 }
463         if(AliTRDReconstructor::StreamLevel()>=7) AliInfo(Form("iter = %d nclFit [%d] = %d", iter, fPlane, fN2));
464                 
465                 if(IsOK()){
466                         tquality = GetQuality(kZcorr);
467                         if(tquality < quality) break;
468                         else quality = tquality;
469                 }
470                 kroadz *= 2.;
471         } // Loop: iter
472         if (!IsOK()) return kFALSE;
473
474         CookLabels();
475         UpdateUsed();
476         return kTRUE;   
477 }
478
479 //____________________________________________________________________
480 Bool_t  AliTRDseedV1::AttachClusters(AliTRDtrackingChamber *chamber
481                                        ,Bool_t kZcorr)
482 {
483   //
484   // Projective algorithm to attach clusters to seeding tracklets
485   //
486   // Parameters
487   //
488   // Output
489   //
490   // Detailed description
491   // 1. Collapse x coordinate for the full detector plane
492   // 2. truncated mean on y (r-phi) direction
493   // 3. purge clusters
494   // 4. truncated mean on z direction
495   // 5. purge clusters
496   // 6. fit tracklet
497   //    
498
499         if(!AliTRDReconstructor::RecoParam()){
500                 AliError("Seed can not be used without a valid RecoParam.");
501                 return kFALSE;
502         }
503
504         const Int_t kClusterCandidates = 2 * knTimebins;
505         
506         //define roads
507         Double_t kroady = AliTRDReconstructor::RecoParam()->GetRoad1y();
508         Double_t kroadz = fPadLength * 1.5 + 1.;
509         // correction to y for the tilting angle
510         Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
511
512         // working variables
513         AliTRDcluster *clusters[kClusterCandidates];
514         Double_t cond[4], yexp[knTimebins], zexp[knTimebins],
515                 yres[kClusterCandidates], zres[kClusterCandidates];
516         Int_t ncl, *index = 0x0, tboundary[knTimebins];
517         
518         // Do cluster projection
519         AliTRDchamberTimeBin *layer = 0x0;
520         Int_t nYclusters = 0; Bool_t kEXIT = kFALSE;
521         for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
522                 if(!(layer = chamber->GetTB(iTime))) continue;
523                 if(!Int_t(*layer)) continue;
524                 
525                 fX[iTime] = layer->GetX() - fX0;
526                 zexp[iTime] = fZref[0] + fZref[1] * fX[iTime];
527                 yexp[iTime] = fYref[0] + fYref[1] * fX[iTime] - zcorr;
528                 
529                 // build condition and process clusters
530                 cond[0] = yexp[iTime] - kroady; cond[1] = yexp[iTime] + kroady;
531                 cond[2] = zexp[iTime] - kroadz; cond[3] = zexp[iTime] + kroadz;
532                 layer->GetClusters(cond, index, ncl);
533                 for(Int_t ic = 0; ic<ncl; ic++){
534                         AliTRDcluster *c = layer->GetCluster(index[ic]);
535                         clusters[nYclusters] = c;
536                         yres[nYclusters++] = c->GetY() - yexp[iTime];
537                         if(nYclusters >= kClusterCandidates) {
538                                 AliWarning(Form("Cluster candidates reached limit %d. Some may be lost.", kClusterCandidates));
539                                 kEXIT = kTRUE;
540                                 break;
541                         }
542                 }
543                 tboundary[iTime] = nYclusters;
544                 if(kEXIT) break;
545         }
546         
547         // Evaluate truncated mean on the y direction
548         Double_t mean, sigma;
549         AliMathBase::EvaluateUni(nYclusters, yres, mean, sigma, Int_t(nYclusters*.8)-2);
550         // purge cluster candidates
551         Int_t nZclusters = 0;
552         for(Int_t ic = 0; ic<nYclusters; ic++){
553                 if(yres[ic] - mean > 4. * sigma){
554                         clusters[ic] = 0x0;
555                         continue;
556                 }
557                 zres[nZclusters++] = clusters[ic]->GetZ() - zexp[clusters[ic]->GetLocalTimeBin()];
558         }
559         
560         // Evaluate truncated mean on the z direction
561         AliMathBase::EvaluateUni(nZclusters, zres, mean, sigma, Int_t(nZclusters*.8)-2);
562         // purge cluster candidates
563         for(Int_t ic = 0; ic<nZclusters; ic++){
564                 if(zres[ic] - mean > 4. * sigma){
565                         clusters[ic] = 0x0;
566                         continue;
567                 }
568         }
569
570         
571         // Select only one cluster/TimeBin
572         Int_t lastCluster = 0;
573         fN2 = 0;
574         for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
575                 ncl = tboundary[iTime] - lastCluster;
576                 if(!ncl) continue;
577                 Int_t iptr = lastCluster;
578                 if(ncl > 1){
579                         Float_t dold = 9999.;
580                         for(int ic=lastCluster; ic<tboundary[iTime]; ic++){
581                                 if(!clusters[ic]) continue;
582                                 Float_t y = yexp[iTime] - clusters[ic]->GetY();
583                                 Float_t z = zexp[iTime] - clusters[ic]->GetZ();
584                                 Float_t d = y * y + z * z;
585                                 if(d > dold) continue;
586                                 dold = d;
587                                 iptr = ic;
588                         }
589                 }
590                 fIndexes[iTime]  = chamber->GetTB(iTime)->GetGlobalIndex(iptr);
591                 fClusters[iTime] = clusters[iptr];
592                 fY[iTime]        = clusters[iptr]->GetY();
593                 fZ[iTime]        = clusters[iptr]->GetZ();
594                 lastCluster      = tboundary[iTime];
595                 fN2++;
596         }
597         
598         // number of minimum numbers of clusters expected for the tracklet
599         Int_t kClmin = Int_t(AliTRDReconstructor::RecoParam()->GetFindableClusters()*AliTRDtrackerV1::GetNTimeBins());
600   if (fN2 < kClmin){
601                 AliWarning(Form("Not enough clusters to fit the tracklet %d [%d].", fN2, kClmin));
602     fN2 = 0;
603     return kFALSE;
604   }
605
606         // update used clusters
607         fNUsed = 0;
608         for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
609                 if(!fClusters[iTime]) continue;
610                 if((fClusters[iTime]->IsUsed())) fNUsed++;
611         }
612
613   if (fN2-fNUsed < kClmin){
614                 AliWarning(Form("Too many clusters already in use %d (from %d).", fNUsed, fN2));
615     fN2 = 0;
616     return kFALSE;
617   }
618         
619         return kTRUE;
620 }
621
622 //____________________________________________________________________
623 Bool_t AliTRDseedV1::Fit()
624 {
625   //
626   // Linear fit of the tracklet
627   //
628   // Parameters :
629   //
630   // Output :
631   //  True if successful
632   //
633   // Detailed description
634   // 2. Check if tracklet crosses pad row boundary
635   // 1. Calculate residuals in the y (r-phi) direction
636   // 3. Do a Least Square Fit to the data
637   //
638
639         //Float_t  sigmaexp  = 0.05 + TMath::Abs(fYref[1] * 0.25); // Expected r.m.s in y direction
640   Float_t  ycrosscor = fPadLength * fTilt * 0.5; // Y correction for crossing
641   Float_t  anglecor = fTilt * fZref[1];  // Correction to the angle
642
643         // calculate residuals
644         Float_t yres[knTimebins]; // y (r-phi) residuals
645         Int_t zint[knTimebins],   // Histograming of the z coordinate
646               zout[2*knTimebins];//
647         
648         fN = 0;
649         for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
650     if (!fClusters[iTime]) continue;
651     if (!fClusters[iTime]->IsInChamber()) continue;
652     yres[iTime] = fY[iTime] - fYref[0] - (fYref[1] + anglecor) * fX[iTime] + fTilt * (fZ[iTime] - fZref[0]);
653                 zint[fN] = Int_t(fZ[iTime]);
654                 fN++;
655         }
656
657         // calculate pad row boundary crosses
658         Int_t kClmin = Int_t(AliTRDReconstructor::RecoParam()->GetFindableClusters()*AliTRDtrackerV1::GetNTimeBins());
659         Int_t nz = AliMathBase::Freq(fN, zint, zout, kFALSE);
660   fZProb   = zout[0];
661   if(nz <= 1) zout[3] = 0;
662   if(zout[1] + zout[3] < kClmin) {
663                 AliWarning(Form("Not enough clusters to fit the cross boundary tracklet %d [%d].", zout[1]+zout[3], kClmin));
664                 return kFALSE;
665         }
666   // Z distance bigger than pad - length
667   if (TMath::Abs(zout[0]-zout[2]) > fPadLength) zout[3]=0;
668  
669
670   Double_t sumw   = 0., 
671                 sumwx  = 0.,
672                 sumwx2 = 0.,
673                 sumwy  = 0.,
674                 sumwxy = 0.,
675                 sumwz  = 0.,
676                 sumwxz = 0.;
677         Int_t npads;
678   fMPads = 0;
679         fMeanz = 0.;
680         // we will use only the clusters which are in the detector range
681         for(int iTime=0; iTime<AliTRDtrackerV1::GetNTimeBins(); iTime++){
682     fUsable[iTime] = kFALSE;
683     if (!fClusters[iTime]) continue;
684                 npads = fClusters[iTime]->GetNPads();
685
686                 fUsable[iTime] = kTRUE;
687     fN2++;
688     fMPads += npads;
689     Float_t weight = 1.0;
690     if(npads > 5) weight = 0.2;
691     else if(npads > 4) weight = 0.5;
692     sumw   += weight; 
693     sumwx  += fX[iTime] * weight;
694     sumwx2 += fX[iTime] * fX[iTime] * weight;
695     sumwy  += weight * yres[iTime];
696     sumwxy += weight * yres[iTime] * fX[iTime];
697     sumwz  += weight * fZ[iTime];    
698     sumwxz += weight * fZ[iTime] * fX[iTime];
699         }
700   if (fN2 < kClmin){
701                 AliWarning(Form("Not enough clusters to fit the tracklet %d [%d].", fN2, kClmin));
702     fN2 = 0;
703     return kFALSE;
704   }
705   fMeanz = sumwz / sumw;
706         fNChange = 0;
707
708         // Tracklet on boundary
709   Float_t correction = 0;
710   if (fNChange > 0) {
711     if (fMeanz < fZProb) correction =  ycrosscor;
712     if (fMeanz > fZProb) correction = -ycrosscor;
713   }
714
715   Double_t det = sumw * sumwx2 - sumwx * sumwx;
716   fYfitR[0]    = (sumwx2 * sumwy  - sumwx * sumwxy) / det;
717   fYfitR[1]    = (sumw   * sumwxy - sumwx * sumwy)  / det;
718   
719   fSigmaY2 = 0;
720   for (Int_t i = 0; i < AliTRDtrackerV1::GetNTimeBins()+1; i++) {
721     if (!fUsable[i]) continue;
722     Float_t delta = yres[i] - fYfitR[0] - fYfitR[1] * fX[i];
723     fSigmaY2 += delta*delta;
724   }
725   fSigmaY2 = TMath::Sqrt(fSigmaY2 / Float_t(fN2-2));
726   
727   fZfitR[0]  = (sumwx2 * sumwz  - sumwx * sumwxz) / det;
728   fZfitR[1]  = (sumw   * sumwxz - sumwx * sumwz)  / det;
729   fZfit[0]   = (sumwx2 * sumwz  - sumwx * sumwxz) / det;
730   fZfit[1]   = (sumw   * sumwxz - sumwx * sumwz)  / det;
731   fYfitR[0] += fYref[0] + correction;
732   fYfitR[1] += fYref[1];
733   fYfit[0]   = fYfitR[0];
734   fYfit[1]   = fYfitR[1];
735
736         return kTRUE;
737 }
738
739
740 //___________________________________________________________________
741 void AliTRDseedV1::Print()
742 {
743   //
744   // Printing the seedstatus
745   //
746
747         printf("Seed status :\n");
748         printf("  fTilt      = %f\n", fTilt);
749         printf("  fPadLength = %f\n", fPadLength);
750         printf("  fX0        = %f\n", fX0);
751         for(int ic=0; ic<AliTRDtrackerV1::GetNTimeBins(); ic++) {
752           const Char_t *isUsable = fUsable[ic]?"Yes":"No";
753           printf("  %d X[%f] Y[%f] Z[%f] Indexes[%d] clusters[%p] usable[%s]\n"
754                 , ic
755                 , fX[ic]
756                 , fY[ic]
757                 , fZ[ic]
758                 , fIndexes[ic]
759                 , ((void*) fClusters[ic])
760                 , isUsable);
761         }
762
763         printf("  fYref[0] =%f fYref[1] =%f\n", fYref[0], fYref[1]);
764         printf("  fZref[0] =%f fZref[1] =%f\n", fZref[0], fZref[1]);
765         printf("  fYfit[0] =%f fYfit[1] =%f\n", fYfit[0], fYfit[1]);
766         printf("  fYfitR[0]=%f fYfitR[1]=%f\n", fYfitR[0], fYfitR[1]);
767         printf("  fZfit[0] =%f fZfit[1] =%f\n", fZfit[0], fZfit[1]);
768         printf("  fZfitR[0]=%f fZfitR[1]=%f\n", fZfitR[0], fZfitR[1]);
769         printf("  fSigmaY =%f\n", fSigmaY);
770         printf("  fSigmaY2=%f\n", fSigmaY2);            
771         printf("  fMeanz  =%f\n", fMeanz);
772         printf("  fZProb  =%f\n", fZProb);
773         printf("  fLabels[0]=%d fLabels[1]=%d\n", fLabels[0], fLabels[1]);
774         printf("  fN      =%d\n", fN);
775         printf("  fN2     =%d (>8 isOK)\n",fN2);
776         printf("  fNUsed  =%d\n", fNUsed);
777         printf("  fFreq   =%d\n", fFreq);
778         printf("  fNChange=%d\n",  fNChange);
779         printf("  fMPads  =%f\n", fMPads);
780         
781         printf("  fC      =%f\n", fC);        
782         printf("  fCC     =%f\n",fCC);      
783         printf("  fChi2   =%f\n", fChi2);  
784         printf("  fChi2Z  =%f\n", fChi2Z);
785
786 }