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