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