]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDseedV1.cxx
- fix cluster ownership problems in time bin container
[u/mrichter/AliRoot.git] / TRD / AliTRDseedV1.cxx
index f134bfc9eba9aca045e33bd22d9019d445d48a45..43792bc2278996ce31e336f708ab6c64eb71a164 100644 (file)
@@ -1,17 +1,17 @@
 /**************************************************************************
- * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
- *                                                                        *
- * Author: The ALICE Off-line Project.                                    *
- * Contributors are mentioned in the code where appropriate.              *
- *                                                                        *
- * Permission to use, copy, modify and distribute this software and its   *
- * documentation strictly for non-commercial purposes is hereby granted   *
- * without fee, provided that the above copyright notice appears in all   *
- * copies and that both the copyright notice and this permission notice   *
- * appear in the supporting documentation. The authors make no claims     *
- * about the suitability of this software for any purpose. It is          *
- * provided "as is" without express or implied warranty.                  *
- **************************************************************************/
+* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+*                                                                        *
+* Author: The ALICE Off-line Project.                                    *
+* Contributors are mentioned in the code where appropriate.              *
+*                                                                        *
+* Permission to use, copy, modify and distribute this software and its   *
+* documentation strictly for non-commercial purposes is hereby granted   *
+* without fee, provided that the above copyright notice appears in all   *
+* copies and that both the copyright notice and this permission notice   *
+* appear in the supporting documentation. The authors make no claims     *
+* about the suitability of this software for any purpose. It is          *
+* provided "as is" without express or implied warranty.                  *
+**************************************************************************/
 
 /* $Id$ */
 
 
 #include "TMath.h"
 #include "TLinearFitter.h"
+#include "TClonesArray.h" // tmp
+#include <TTreeStream.h>
 
 #include "AliLog.h"
 #include "AliMathBase.h"
 
-#include "AliTRDseedV1.h"
 #include "AliTRDcluster.h"
-#include "AliTRDtrack.h"
+#include "AliTRDseedV1.h"
+#include "AliTRDtrackV1.h"
 #include "AliTRDcalibDB.h"
-#include "AliTRDstackLayer.h"
+#include "AliTRDchamberTimeBin.h"
+#include "AliTRDtrackingChamber.h"
+#include "AliTRDtrackerV1.h"
+#include "AliTRDReconstructor.h"
 #include "AliTRDrecoParam.h"
-
-#define SEED_DEBUG
+#include "Cal/AliTRDCalPID.h"
 
 ClassImp(AliTRDseedV1)
 
 //____________________________________________________________________
-AliTRDseedV1::AliTRDseedV1(Int_t layer, AliTRDrecoParam *p
+AliTRDseedV1::AliTRDseedV1(Int_t det
   :AliTRDseed()
-  ,fPlane(layer)
-  ,fOwner(kFALSE)
-  ,fRecoParam(p)
+  ,fReconstructor(0x0)
+  ,fClusterIter(0x0)
+  ,fClusterIdx(0)
+  ,fDet(det)
+  ,fMom(0.)
+  ,fSnp(0.)
+  ,fTgl(0.)
+  ,fdX(0.)
 {
   //
   // Constructor
   //
+  //printf("AliTRDseedV1::AliTRDseedV1()\n");
+
+  for(int islice=0; islice < knSlices; islice++) fdEdx[islice] = 0.;
+  for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) fProb[ispec]  = -1.;
 }
 
 //____________________________________________________________________
 AliTRDseedV1::AliTRDseedV1(const AliTRDseedV1 &ref)
   :AliTRDseed((AliTRDseed&)ref)
-  ,fPlane(ref.fPlane)
-  ,fOwner(kFALSE)
-  ,fRecoParam(ref.fRecoParam)
+  ,fReconstructor(ref.fReconstructor)
+  ,fClusterIter(0x0)
+  ,fClusterIdx(0)
+  ,fDet(ref.fDet)
+  ,fMom(ref.fMom)
+  ,fSnp(ref.fSnp)
+  ,fTgl(ref.fTgl)
+  ,fdX(ref.fdX)
 {
   //
   // Copy Constructor performing a deep copy
   //
 
-       //AliInfo("");
-       if(ref.fOwner) SetOwner();
+  //printf("AliTRDseedV1::AliTRDseedV1(const AliTRDseedV1 &)\n");
+  SetBit(kOwner, kFALSE);
+  for(int islice=0; islice < knSlices; islice++) fdEdx[islice] = ref.fdEdx[islice];
+  for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) fProb[ispec] = ref.fProb[ispec];
 }
 
 
@@ -77,11 +97,10 @@ AliTRDseedV1& AliTRDseedV1::operator=(const AliTRDseedV1 &ref)
   // Assignment Operator using the copy function
   //
 
-       //AliInfo("");
-       if(this != &ref){
-               ref.Copy(*this);
-       }
-       return *this;
+  if(this != &ref){
+    ref.Copy(*this);
+  }
+  return *this;
 
 }
 
@@ -92,9 +111,15 @@ AliTRDseedV1::~AliTRDseedV1()
   // Destructor. The RecoParam object belongs to the underlying tracker.
   //
 
-       //AliInfo(Form("fOwner[%s]", fOwner?"YES":"NO"));
+  //printf("I-AliTRDseedV1::~AliTRDseedV1() : Owner[%s]\n", IsOwner()?"YES":"NO");
 
-       if(fOwner) delete [] fClusters;
+  if(IsOwner()) 
+    for(int itb=0; itb<knTimebins; itb++){
+      if(!fClusters[itb]) continue; 
+      //AliInfo(Form("deleting c %p @ %d", fClusters[itb], itb));
+      delete fClusters[itb];
+      fClusters[itb] = 0x0;
+    }
 }
 
 //____________________________________________________________________
@@ -104,17 +129,27 @@ void AliTRDseedV1::Copy(TObject &ref) const
   // Copy function
   //
 
-       //AliInfo("");
-       AliTRDseedV1 &target = (AliTRDseedV1 &)ref; 
-       
-       target.fPlane         = fPlane;
-       target.fRecoParam     = fRecoParam;
-       AliTRDseed::Copy(target);
+  //AliInfo("");
+  AliTRDseedV1 &target = (AliTRDseedV1 &)ref; 
+
+  target.fClusterIter   = 0x0;
+  target.fClusterIdx    = 0;
+  target.fDet           = fDet;
+  target.fMom           = fMom;
+  target.fSnp           = fSnp;
+  target.fTgl           = fTgl;
+  target.fdX            = fdX;
+  target.fReconstructor = fReconstructor;
+  
+  for(int islice=0; islice < knSlices; islice++) target.fdEdx[islice] = fdEdx[islice];
+  for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) target.fProb[ispec] = fProb[ispec];
+  
+  AliTRDseed::Copy(target);
 }
 
 
 //____________________________________________________________
-void AliTRDseedV1::Init(AliTRDtrack *track)
+Bool_t AliTRDseedV1::Init(AliTRDtrackV1 *track)
 {
 // Initialize this tracklet using the track information
 //
@@ -130,16 +165,134 @@ void AliTRDseedV1::Init(AliTRDtrack *track)
 // chamber where the tracklet will be constructed
 //
 
-       Double_t y, z; 
-       track->GetProlongation(fX0, y, z);
-       fYref[0] = y;
-       fYref[1] = track->GetSnp() < 0. ? track->GetTgl() : -track->GetTgl();
-       fZref[0] = z;
-       // TO DO 
-       // tilting pad correction !!
-       fZref[1] = 0.; // TMath::Tan(track->Theta());
+  Double_t y, z; 
+  if(!track->GetProlongation(fX0, y, z)) return kFALSE;
+  fYref[0] = y;
+  fYref[1] = track->GetSnp()/(1. - track->GetSnp()*track->GetSnp());
+  fZref[0] = z;
+  fZref[1] = track->GetTgl();
 
-       //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());
+  //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());
+  return kTRUE;
+}
+
+
+//____________________________________________________________________
+void AliTRDseedV1::CookdEdx(Int_t nslices)
+{
+// Calculates average dE/dx for all slices and store them in the internal array fdEdx. 
+//
+// Parameters:
+//  nslices : number of slices for which dE/dx should be calculated
+// Output:
+//  store results in the internal array fdEdx. This can be accessed with the method
+//  AliTRDseedV1::GetdEdx()
+//
+// Detailed description
+// Calculates average dE/dx for all slices. Depending on the PID methode 
+// the number of slices can be 3 (LQ) or 8(NN). 
+// The calculation of dQ/dl are done using the tracklet fit results (see AliTRDseedV1::GetdQdl(Int_t)) i.e.
+//
+// dQ/dl = qc/(dx * sqrt(1 + dy/dx^2 + dz/dx^2))
+//
+// The following effects are included in the calculation:
+// 1. calibration values for t0 and vdrift (using x coordinate to calculate slice)
+// 2. cluster sharing (optional see AliTRDrecoParam::SetClusterSharing())
+// 3. cluster size
+//
+
+  Int_t nclusters[knSlices];
+  for(int i=0; i<knSlices; i++){ 
+    fdEdx[i]     = 0.;
+    nclusters[i] = 0;
+  }
+  Float_t clength = (/*.5 * */AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick());
+
+  AliTRDcluster *cluster = 0x0;
+  for(int ic=0; ic<AliTRDtrackerV1::GetNTimeBins(); ic++){
+    if(!(cluster = fClusters[ic])) continue;
+    Float_t x = cluster->GetX();
+    
+    // Filter clusters for dE/dx calculation
+    
+    // 1.consider calibration effects for slice determination
+    Int_t slice; 
+    if(cluster->IsInChamber()) slice = Int_t(TMath::Abs(fX0 - x) * nslices / clength);
+    else slice = x < fX0 ? 0 : nslices-1;
+    
+    // 2. take sharing into account
+    Float_t w = cluster->IsShared() ? .5 : 1.;
+    
+    // 3. take into account large clusters TODO
+    //w *= c->GetNPads() > 3 ? .8 : 1.;
+    
+    //CHECK !!!
+    fdEdx[slice]   += w * GetdQdl(ic); //fdQdl[ic];
+    nclusters[slice]++;
+  } // End of loop over clusters
+
+  //if(fReconstructor->GetPIDMethod() == AliTRDReconstructor::kLQPID){
+  if(nslices == AliTRDReconstructor::kLQslices){
+  // calculate mean charge per slice (only LQ PID)
+    for(int is=0; is<nslices; is++){ 
+      if(nclusters[is]) fdEdx[is] /= nclusters[is];
+    }
+  }
+}
+
+
+//____________________________________________________________________
+Float_t AliTRDseedV1::GetdQdl(Int_t ic) const
+{
+  return fClusters[ic] ? TMath::Abs(fClusters[ic]->GetQ()) /fdX / TMath::Sqrt(1. + fYfit[1]*fYfit[1] + fZref[1]*fZref[1]) : 0.;
+}
+
+//____________________________________________________________________
+Double_t* AliTRDseedV1::GetProbability()
+{      
+// Fill probability array for tracklet from the DB.
+//
+// Parameters
+//
+// Output
+//   returns pointer to the probability array and 0x0 if missing DB access 
+//
+// Detailed description
+
+  
+  // retrive calibration db
+  AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
+  if (!calibration) {
+    AliError("No access to calibration data");
+    return 0x0;
+  }
+
+  if (!fReconstructor) {
+    AliError("Reconstructor not set.");
+    return 0x0;
+  }
+
+  // Retrieve the CDB container class with the parametric detector response
+  const AliTRDCalPID *pd = calibration->GetPIDObject(fReconstructor->GetPIDMethod());
+  if (!pd) {
+    AliError("No access to AliTRDCalPID object");
+    return 0x0;
+  }
+  //AliInfo(Form("Method[%d] : %s", fReconstructor->GetRecoParam() ->GetPIDMethod(), pd->IsA()->GetName()));
+
+  // calculate tracklet length TO DO
+  Float_t length = (AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick());
+  /// TMath::Sqrt((1.0 - fSnp[iPlane]*fSnp[iPlane]) / (1.0 + fTgl[iPlane]*fTgl[iPlane]));
+  
+  //calculate dE/dx
+  CookdEdx(fReconstructor->GetNdEdxSlices());
+  
+  // Sets the a priori probabilities
+  for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) {
+    fProb[ispec] = pd->GetProbability(ispec, fMom, &fdEdx[0], length, GetPlane());     
+  }
+
+  return &fProb[0];
 }
 
 //____________________________________________________________________
@@ -149,11 +302,12 @@ Float_t AliTRDseedV1::GetQuality(Bool_t kZcorr) const
   // Returns a quality measurement of the current seed
   //
 
-       Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
-       return .5 * (18.0 - fN2)
-               + 10.* TMath::Abs(fYfit[1] - fYref[1])
-               + 5.* TMath::Abs(fYfit[0] - fYref[0] + zcorr)
-               + 2. * TMath::Abs(fMeanz - fZref[0]) / fPadLength;
+  Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
+  return 
+      .5 * TMath::Abs(18.0 - fN2)
+    + 10.* TMath::Abs(fYfit[1] - fYref[1])
+    + 5. * TMath::Abs(fYfit[0] - fYref[0] + zcorr)
+    + 2. * TMath::Abs(fMeanz - fZref[0]) / fPadLength;
 }
 
 //____________________________________________________________________
@@ -161,154 +315,167 @@ void AliTRDseedV1::GetCovAt(Double_t /*x*/, Double_t *cov) const
 {
 // Computes covariance in the y-z plane at radial point x
 
-       const Float_t k0= .2; // to be checked in FindClusters
-       Double_t sy20   = k0*TMath::Tan(fYfit[1]); sy20 *= sy20;
-       
-       Double_t sy2    = fSigmaY2*fSigmaY2 + sy20;
-       Double_t sz2    = fPadLength/12.;
+  Int_t ic = 0; while (!fClusters[ic]) ic++; 
+  AliTRDcalibDB *fCalib = AliTRDcalibDB::Instance();
+  Double_t exB         = fCalib->GetOmegaTau(fCalib->GetVdriftAverage(fClusters[ic]->GetDetector()), -AliTracker::GetBz()*0.1);
+
+  Double_t sy2    = fSigmaY2*fSigmaY2 + .2*(fYfit[1]-exB)*(fYfit[1]-exB);
+  Double_t sz2    = fPadLength/12.;
 
-       //printf("Yfit[1] %f sy20 %f SigmaY2 %f\n", fYfit[1], sy20, fSigmaY2);
 
-       cov[0] = sy2;
-       cov[1] = fTilt*(sy2-sz2);
-       cov[2] = sz2;
+  //printf("Yfit[1] %f sy20 %f SigmaY2 %f\n", fYfit[1], sy20, fSigmaY2);
+
+  cov[0] = sy2;
+  cov[1] = fTilt*(sy2-sz2);
+  cov[2] = sz2;
+
+  // insert systematic uncertainties calibration and misalignment
+  Double_t sys[15];
+  fReconstructor->GetRecoParam()->GetSysCovMatrix(sys);
+  cov[0] += (sys[0]*sys[0]);
+  cov[2] += (sys[1]*sys[1]);
 }
 
+
 //____________________________________________________________________
-void AliTRDseedV1::SetOwner(Bool_t own)
+void AliTRDseedV1::SetOwner()
 {
-  //
-  // Handles the ownership of the clusters
-  //
-       if(own){
-               for(int ic=0; ic<fgTimeBins; ic++){
-                       if(!fClusters[ic]) continue;
-                       fClusters[ic] = new AliTRDcluster(*fClusters[ic]);
-               }
-               fOwner = kTRUE;
-       } else {
-               if(fOwner){
-                       for(int ic=0; ic<fgTimeBins; ic++){
-                               if(!fClusters[ic]) continue;
-                               delete fClusters[ic];
-                       }
-               }
-               fOwner = kFALSE;
-       }
+  //AliInfo(Form("own [%s] fOwner[%s]", own?"YES":"NO", fOwner?"YES":"NO"));
+  
+  if(TestBit(kOwner)) return;
+  for(int ic=0; ic<knTimebins; ic++){
+    if(!fClusters[ic]) continue;
+    fClusters[ic] = new AliTRDcluster(*fClusters[ic]);
+  }
+  SetBit(kOwner);
 }
 
 //____________________________________________________________________
-Bool_t AliTRDseedV1::AttachClustersIter(AliTRDstackLayer *layer
-                                       , Float_t quality
-                                       , Bool_t kZcorr
-                                       , AliTRDcluster *c)
+Bool_t AliTRDseedV1::AttachClustersIter(AliTRDtrackingChamber *chamber, Float_t quality, Bool_t kZcorr, AliTRDcluster *c)
 {
   //
   // Iterative process to register clusters to the seed.
   // In iteration 0 we try only one pad-row and if quality not
   // sufficient we try 2 pad-rows (about 5% of tracks cross 2 pad-rows)
   //
-       
-       if(!fRecoParam){
-               AliError("Seed can not be used without a valid RecoParam.");
-               return kFALSE;
-       }
-
-       //AliInfo(Form("TimeBins = %d TimeBinsRange = %d", fgTimeBins, fTimeBinsRange));
-
-       Float_t  tquality;
-       Double_t kroady = fRecoParam->GetRoad1y();
-       Double_t kroadz = fPadLength * .5 + 1.;
-       
-       // initialize configuration parameters
-       Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
-       Int_t   niter = kZcorr ? 1 : 2;
-       
-       Double_t yexp, zexp;
-       Int_t ncl = 0;
-       // start seed update
-       for (Int_t iter = 0; iter < niter; iter++) {
-       //AliInfo(Form("iter = %i", iter));
-               ncl = 0;
-               for (Int_t iTime = 0; iTime < fgTimeBins; iTime++) {
-                       // define searching configuration
-                       Double_t dxlayer = layer[iTime].GetX() - fX0;
-                       if(c){
-                               zexp = c->GetZ();
-                               //Try 2 pad-rows in second iteration
-                               if (iter > 0) {
-                                       zexp = fZref[0] + fZref[1] * dxlayer - zcorr;
-                                       if (zexp > c->GetZ()) zexp = c->GetZ() + fPadLength*0.5;
-                                       if (zexp < c->GetZ()) zexp = c->GetZ() - fPadLength*0.5;
-                               }
-                       } else zexp = fZref[0];
-                       yexp  = fYref[0] + fYref[1] * dxlayer - zcorr;
-                       // get  cluster
-//                     printf("xexp = %3.3f ,yexp = %3.3f, zexp = %3.3f\n",layer[iTime].GetX(),yexp,zexp);
-//                     printf("layer[%i].GetNClusters() = %i\n", iTime, layer[iTime].GetNClusters());
-                       Int_t    index = layer[iTime].SearchNearestCluster(yexp, zexp, kroady, kroadz);
-
-//                     printf("%d[%d] x[%7.3f | %7.3f] y[%7.3f] z[%7.3f]\n", iTime, layer[iTime].GetNClusters(), dxlayer, layer[iTime].GetX(), yexp, zexp);
-//                     for(Int_t iclk = 0; iclk < layer[iTime].GetNClusters(); iclk++){
-//                             AliTRDcluster *testcl = layer[iTime].GetCluster(iclk);
-//                             printf("Cluster %i: %d x = %7.3f, y = %7.3f, z = %7.3f\n", iclk, testcl->GetLocalTimeBin(), testcl->GetX(), testcl->GetY(), testcl->GetZ());
-//                     }
-//                     printf("Index = %i\n",index);
-
-                       if (index < 0) continue;
-                       
-                       // Register cluster
-                       AliTRDcluster *cl = (AliTRDcluster*) layer[iTime].GetCluster(index);
-                       
-                       //printf("Cluster %i(0x%x): x = %3.3f, y = %3.3f, z = %3.3f\n", index, cl, cl->GetX(), cl->GetY(), cl->GetZ());
-
-                       Int_t globalIndex = layer[iTime].GetGlobalIndex(index);
-                       fIndexes[iTime]  = globalIndex;
-                       fClusters[iTime] = cl;
-                       fX[iTime]        = dxlayer;
-                       fY[iTime]        = cl->GetY();
-                       fZ[iTime]        = cl->GetZ();
-       
-                       // Debugging
-                       ncl++;
-               }
-
-#ifdef SEED_DEBUG
-//             Int_t nclusters = 0;
-//             Float_t fD[iter] = 0.;
-//             for(int ic=0; ic<fgTimeBins+1; ic++){
-//                     AliTRDcluster *ci = fClusters[ic];
-//                     if(!ci) continue;
-//                     for(int jc=ic+1; jc<fgTimeBins+1; jc++){
-//                             AliTRDcluster *cj = fClusters[jc];
-//                             if(!cj) continue;
-//                             fD[iter] += TMath::Sqrt((ci->GetY()-cj->GetY())*(ci->GetY()-cj->GetY())+
-//                             (ci->GetZ()-cj->GetZ())*(ci->GetZ()-cj->GetZ()));
-//                             nclusters++;
-//                     }
-//             }
-//             if(nclusters) fD[iter] /= float(nclusters);
-#endif
-
-               AliTRDseed::Update();
-
-               if(IsOK()){
-                       tquality = GetQuality(kZcorr);
-                       if(tquality < quality) break;
-                       else quality = tquality;
-               }
-               kroadz *= 2.;
-       } // Loop: iter
-       if (!IsOK()) return kFALSE;
-
-       CookLabels();
-       UpdateUsed();
-       return kTRUE;   
+  // debug level 7
+  //
+  
+  if(!fReconstructor->GetRecoParam() ){
+    AliError("Seed can not be used without a valid RecoParam.");
+    return kFALSE;
+  }
+
+  AliTRDchamberTimeBin *layer = 0x0;
+  if(fReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker)>=7){
+    AliTRDtrackingChamber *ch = new AliTRDtrackingChamber(*chamber); 
+    (*AliTRDtrackerV1::DebugStreamer()) << "AttachClustersIter"
+      << "chamber.="   << ch
+      << "tracklet.="  << this
+      << "\n"; 
+  }
+
+  Float_t  tquality;
+  Double_t kroady = fReconstructor->GetRecoParam() ->GetRoad1y();
+  Double_t kroadz = fPadLength * .5 + 1.;
+  
+  // initialize configuration parameters
+  Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
+  Int_t   niter = kZcorr ? 1 : 2;
+  
+  Double_t yexp, zexp;
+  Int_t ncl = 0;
+  // start seed update
+  for (Int_t iter = 0; iter < niter; iter++) {
+    ncl = 0;
+    for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
+      if(!(layer = chamber->GetTB(iTime))) continue;
+      if(!Int_t(*layer)) continue;
+      
+      // define searching configuration
+      Double_t dxlayer = layer->GetX() - fX0;
+      if(c){
+        zexp = c->GetZ();
+        //Try 2 pad-rows in second iteration
+        if (iter > 0) {
+          zexp = fZref[0] + fZref[1] * dxlayer - zcorr;
+          if (zexp > c->GetZ()) zexp = c->GetZ() + fPadLength*0.5;
+          if (zexp < c->GetZ()) zexp = c->GetZ() - fPadLength*0.5;
+        }
+      } else zexp = fZref[0] + (kZcorr ? fZref[1] * dxlayer : 0.);
+      yexp  = fYref[0] + fYref[1] * dxlayer - zcorr;
+      
+      // Get and register cluster
+      Int_t    index = layer->SearchNearestCluster(yexp, zexp, kroady, kroadz);
+      if (index < 0) continue;
+      AliTRDcluster *cl = (*layer)[index];
+      
+      fIndexes[iTime]  = layer->GetGlobalIndex(index);
+      fClusters[iTime] = cl;
+      fY[iTime]        = cl->GetY();
+      fZ[iTime]        = cl->GetZ();
+      ncl++;
+    }
+    if(fReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker)>=7) AliInfo(Form("iter = %d ncl [%d] = %d", iter, fDet, ncl));
+    
+    if(ncl>1){ 
+      // calculate length of the time bin (calibration aware)
+      Int_t irp = 0; Float_t x[2]; Int_t tb[2];
+      for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
+        if(!fClusters[iTime]) continue;
+        x[irp]  = fClusters[iTime]->GetX();
+        tb[irp] = iTime;
+        irp++;
+        if(irp==2) break;
+      } 
+      fdX = (x[1] - x[0]) / (tb[0] - tb[1]);
+  
+      // update X0 from the clusters (calibration/alignment aware)
+      for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
+        if(!(layer = chamber->GetTB(iTime))) continue;
+        if(!layer->IsT0()) continue;
+        if(fClusters[iTime]){ 
+          fX0 = fClusters[iTime]->GetX();
+          break;
+        } else { // we have to infere the position of the anode wire from the other clusters
+          for (Int_t jTime = iTime+1; jTime < AliTRDtrackerV1::GetNTimeBins(); jTime++) {
+            if(!fClusters[jTime]) continue;
+            fX0 = fClusters[jTime]->GetX() + fdX * (jTime - iTime);
+          }
+          break;
+        }
+      }        
+      
+      // update YZ reference point
+      // TODO
+      
+      // update x reference positions (calibration/alignment aware)
+      for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
+        if(!fClusters[iTime]) continue;
+        fX[iTime] = fClusters[iTime]->GetX() - fX0;
+      } 
+      
+      AliTRDseed::Update();
+    }
+    if(fReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker)>=7) AliInfo(Form("iter = %d nclFit [%d] = %d", iter, fDet, fN2));
+    
+    if(IsOK()){
+      tquality = GetQuality(kZcorr);
+      if(tquality < quality) break;
+      else quality = tquality;
+    }
+    kroadz *= 2.;
+  } // Loop: iter
+  if (!IsOK()) return kFALSE;
+
+  if(fReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker)>=1) CookLabels();
+  UpdateUsed();
+  return kTRUE;        
 }
 
 //____________________________________________________________________
-Bool_t AliTRDseedV1::AttachClusters(AliTRDstackLayer *layer
-                                       ,Bool_t kZcorr)
+Bool_t AliTRDseedV1::AttachClusters(AliTRDtrackingChamber *chamber
+                                      ,Bool_t kZcorr)
 {
   //
   // Projective algorithm to attach clusters to seeding tracklets
@@ -326,131 +493,131 @@ Bool_t  AliTRDseedV1::AttachClusters(AliTRDstackLayer *layer
   // 6. fit tracklet
   //   
 
-       if(!fRecoParam){
-               AliError("Seed can not be used without a valid RecoParam.");
-               return kFALSE;
-       }
-
-       const Int_t kClusterCandidates = 2 * knTimebins;
-       
-       //define roads
-       Double_t kroady = fRecoParam->GetRoad1y();
-       Double_t kroadz = fPadLength * 1.5 + 1.;
-       // correction to y for the tilting angle
-       Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
-
-       // working variables
-       AliTRDcluster *clusters[kClusterCandidates];
-       Double_t cond[4], yexp[knTimebins], zexp[knTimebins],
-               yres[kClusterCandidates], zres[kClusterCandidates];
-       Int_t ncl, *index = 0x0, tboundary[knTimebins];
-       
-       // Do cluster projection
-       Int_t nYclusters = 0; Bool_t kEXIT = kFALSE;
-       for (Int_t iTime = 0; iTime < fgTimeBins; iTime++) {
-               fX[iTime] = layer[iTime].GetX() - fX0;
-               zexp[iTime] = fZref[0] + fZref[1] * fX[iTime];
-               yexp[iTime] = fYref[0] + fYref[1] * fX[iTime] - zcorr;
-               
-               // build condition and process clusters
-               cond[0] = yexp[iTime] - kroady; cond[1] = yexp[iTime] + kroady;
-               cond[2] = zexp[iTime] - kroadz; cond[3] = zexp[iTime] + kroadz;
-               layer[iTime].GetClusters(cond, index, ncl);
-               for(Int_t ic = 0; ic<ncl; ic++){
-                       AliTRDcluster *c = layer[iTime].GetCluster(index[ic]);
-                       clusters[nYclusters] = c;
-                       yres[nYclusters++] = c->GetY() - yexp[iTime];
-                       if(nYclusters >= kClusterCandidates) {
-                               AliWarning(Form("Cluster candidates reached limit %d. Some may be lost.", kClusterCandidates));
-                               kEXIT = kTRUE;
-                               break;
-                       }
-               }
-               tboundary[iTime] = nYclusters;
-               if(kEXIT) break;
-       }
-       
-       // Evaluate truncated mean on the y direction
-       Double_t mean, sigma;
-       AliMathBase::EvaluateUni(nYclusters, yres, mean, sigma, Int_t(nYclusters*.8)-2);
-       //purge cluster candidates
-       Int_t nZclusters = 0;
-       for(Int_t ic = 0; ic<nYclusters; ic++){
-               if(yres[ic] - mean > 4. * sigma){
-                       clusters[ic] = 0x0;
-                       continue;
-               }
-               zres[nZclusters++] = clusters[ic]->GetZ() - zexp[clusters[ic]->GetLocalTimeBin()];
-       }
-       
-       // Evaluate truncated mean on the z direction
-       AliMathBase::EvaluateUni(nZclusters, zres, mean, sigma, Int_t(nZclusters*.8)-2);
-       //purge cluster candidates
-       for(Int_t ic = 0; ic<nZclusters; ic++){
-               if(zres[ic] - mean > 4. * sigma){
-                       clusters[ic] = 0x0;
-                       continue;
-               }
-       }
-
-       
-       // Select only one cluster/TimeBin
-       Int_t lastCluster = 0;
-       fN2 = 0;
-       for (Int_t iTime = 0; iTime < fgTimeBins; iTime++) {
-               ncl = tboundary[iTime] - lastCluster;
-               if(!ncl) continue;
-               AliTRDcluster *c = 0x0;
-               if(ncl == 1){
-                       c = clusters[lastCluster];
-               } else if(ncl > 1){
-                       Float_t dold = 9999.; Int_t iptr = lastCluster;
-                       for(int ic=lastCluster; ic<tboundary[iTime]; ic++){
-                               if(!clusters[ic]) continue;
-                               Float_t y = yexp[iTime] - clusters[ic]->GetY();
-                               Float_t z = zexp[iTime] - clusters[ic]->GetZ();
-                               Float_t d = y * y + z * z;
-                               if(d > dold) continue;
-                               dold = d;
-                               iptr = ic;
-                       }
-                       c = clusters[iptr];
-               }
-               //Int_t GlobalIndex = layer[iTime].GetGlobalIndex(index);
-               //fIndexes[iTime]  = GlobalIndex;
-               fClusters[iTime] = c;
-               fY[iTime]        = c->GetY();
-               fZ[iTime]        = c->GetZ();
-               lastCluster = tboundary[iTime];
-               fN2++;
-       }
-       
-       // number of minimum numbers of clusters expected for the tracklet
-       Int_t kClmin = Int_t(fRecoParam->GetFindableClusters()*fgTimeBins);
+  if(!fReconstructor->GetRecoParam() ){
+    AliError("Seed can not be used without a valid RecoParam.");
+    return kFALSE;
+  }
+
+  const Int_t kClusterCandidates = 2 * knTimebins;
+  
+  //define roads
+  Double_t kroady = fReconstructor->GetRecoParam() ->GetRoad1y();
+  Double_t kroadz = fPadLength * 1.5 + 1.;
+  // correction to y for the tilting angle
+  Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
+
+  // working variables
+  AliTRDcluster *clusters[kClusterCandidates];
+  Double_t cond[4], yexp[knTimebins], zexp[knTimebins],
+    yres[kClusterCandidates], zres[kClusterCandidates];
+  Int_t ncl, *index = 0x0, tboundary[knTimebins];
+  
+  // Do cluster projection
+  AliTRDchamberTimeBin *layer = 0x0;
+  Int_t nYclusters = 0; Bool_t kEXIT = kFALSE;
+  for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
+    if(!(layer = chamber->GetTB(iTime))) continue;
+    if(!Int_t(*layer)) continue;
+    
+    fX[iTime] = layer->GetX() - fX0;
+    zexp[iTime] = fZref[0] + fZref[1] * fX[iTime];
+    yexp[iTime] = fYref[0] + fYref[1] * fX[iTime] - zcorr;
+    
+    // build condition and process clusters
+    cond[0] = yexp[iTime] - kroady; cond[1] = yexp[iTime] + kroady;
+    cond[2] = zexp[iTime] - kroadz; cond[3] = zexp[iTime] + kroadz;
+    layer->GetClusters(cond, index, ncl);
+    for(Int_t ic = 0; ic<ncl; ic++){
+      AliTRDcluster *c = layer->GetCluster(index[ic]);
+      clusters[nYclusters] = c;
+      yres[nYclusters++] = c->GetY() - yexp[iTime];
+      if(nYclusters >= kClusterCandidates) {
+        AliWarning(Form("Cluster candidates reached limit %d. Some may be lost.", kClusterCandidates));
+        kEXIT = kTRUE;
+        break;
+      }
+    }
+    tboundary[iTime] = nYclusters;
+    if(kEXIT) break;
+  }
+  
+  // Evaluate truncated mean on the y direction
+  Double_t mean, sigma;
+  AliMathBase::EvaluateUni(nYclusters, yres, mean, sigma, Int_t(nYclusters*.8)-2);
+  // purge cluster candidates
+  Int_t nZclusters = 0;
+  for(Int_t ic = 0; ic<nYclusters; ic++){
+    if(yres[ic] - mean > 4. * sigma){
+      clusters[ic] = 0x0;
+      continue;
+    }
+    zres[nZclusters++] = clusters[ic]->GetZ() - zexp[clusters[ic]->GetLocalTimeBin()];
+  }
+  
+  // Evaluate truncated mean on the z direction
+  AliMathBase::EvaluateUni(nZclusters, zres, mean, sigma, Int_t(nZclusters*.8)-2);
+  // purge cluster candidates
+  for(Int_t ic = 0; ic<nZclusters; ic++){
+    if(zres[ic] - mean > 4. * sigma){
+      clusters[ic] = 0x0;
+      continue;
+    }
+  }
+
+  
+  // Select only one cluster/TimeBin
+  Int_t lastCluster = 0;
+  fN2 = 0;
+  for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
+    ncl = tboundary[iTime] - lastCluster;
+    if(!ncl) continue;
+    Int_t iptr = lastCluster;
+    if(ncl > 1){
+      Float_t dold = 9999.;
+      for(int ic=lastCluster; ic<tboundary[iTime]; ic++){
+        if(!clusters[ic]) continue;
+        Float_t y = yexp[iTime] - clusters[ic]->GetY();
+        Float_t z = zexp[iTime] - clusters[ic]->GetZ();
+        Float_t d = y * y + z * z;
+        if(d > dold) continue;
+        dold = d;
+        iptr = ic;
+      }
+    }
+    fIndexes[iTime]  = chamber->GetTB(iTime)->GetGlobalIndex(iptr);
+    fClusters[iTime] = clusters[iptr];
+    fY[iTime]        = clusters[iptr]->GetY();
+    fZ[iTime]        = clusters[iptr]->GetZ();
+    lastCluster      = tboundary[iTime];
+    fN2++;
+  }
+  
+  // number of minimum numbers of clusters expected for the tracklet
+  Int_t kClmin = Int_t(fReconstructor->GetRecoParam() ->GetFindableClusters()*AliTRDtrackerV1::GetNTimeBins());
   if (fN2 < kClmin){
-               AliWarning(Form("Not enough clusters to fit the tracklet %d [%d].", fN2, kClmin));
+    AliWarning(Form("Not enough clusters to fit the tracklet %d [%d].", fN2, kClmin));
     fN2 = 0;
     return kFALSE;
   }
 
-       // update used clusters
-       fNUsed = 0;
-       for (Int_t iTime = 0; iTime < fgTimeBins; iTime++) {
-               if(!fClusters[iTime]) continue;
-               if((fClusters[iTime]->IsUsed())) fNUsed++;
-       }
+  // update used clusters
+  fNUsed = 0;
+  for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
+    if(!fClusters[iTime]) continue;
+    if((fClusters[iTime]->IsUsed())) fNUsed++;
+  }
 
   if (fN2-fNUsed < kClmin){
-               AliWarning(Form("Too many clusters already in use %d (from %d).", fNUsed, fN2));
+    AliWarning(Form("Too many clusters already in use %d (from %d).", fNUsed, fN2));
     fN2 = 0;
     return kFALSE;
   }
-       
-       return kTRUE;
+  
+  return kTRUE;
 }
 
 //____________________________________________________________________
-Bool_t AliTRDseedV1::Fit()
+Bool_t AliTRDseedV1::Fit(Bool_t tilt)
 {
   //
   // Linear fit of the tracklet
@@ -466,288 +633,166 @@ Bool_t AliTRDseedV1::Fit()
   // 3. Do a Least Square Fit to the data
   //
 
-       //Float_t  sigmaexp  = 0.05 + TMath::Abs(fYref[1] * 0.25); // Expected r.m.s in y direction
-  Float_t  ycrosscor = fPadLength * fTilt * 0.5; // Y correction for crossing
-  Float_t  anglecor = fTilt * fZref[1];  // Correction to the angle
-
-       // calculate residuals
-       Float_t yres[knTimebins]; // y (r-phi) residuals
-       Int_t zint[knTimebins],   // Histograming of the z coordinate
-             zout[2*knTimebins];//
-       
-       fN = 0;
-       for (Int_t iTime = 0; iTime < fTimeBinsRange; iTime++) {
-    if (!fClusters[iTime]) continue;
-    yres[iTime] = fY[iTime] - fYref[0] - (fYref[1] + anglecor) * fX[iTime];
-               zint[fN] = Int_t(fZ[iTime]);
-               fN++;
-       }
-
-       // calculate pad row boundary crosses
-       Int_t kClmin = Int_t(fRecoParam->GetFindableClusters()*fTimeBinsRange);
-       Int_t nz = AliMathBase::Freq(fN, zint, zout, kFALSE);
-  fZProb   = zout[0];
-  if(nz <= 1) zout[3] = 0;
-  if(zout[1] + zout[3] < kClmin) {
-               AliWarning(Form("Not enough clusters to fit the cross boundary tracklet %d [%d].", zout[1]+zout[3], kClmin));
-               return kFALSE;
-       }
-  // Z distance bigger than pad - length
-  if (TMath::Abs(zout[0]-zout[2]) > fPadLength) zout[3]=0;
-
-  Double_t sumw   = 0., 
-               sumwx  = 0.,
-               sumwx2 = 0.,
-               sumwy  = 0.,
-               sumwxy = 0.,
-               sumwz  = 0.,
-               sumwxz = 0.;
-       Int_t npads;
-  fMPads = 0;
-       fMeanz = 0.;
-       // we will use only the clusters which are in the detector range
-       for(int iTime=0; iTime<fTimeBinsRange; iTime++){
-    fUsable[iTime] = kFALSE;
-    if (!fClusters[iTime]) continue;
-               npads = fClusters[iTime]->GetNPads();
-
-               fUsable[iTime] = kTRUE;
-    fN2++;
-    fMPads += npads;
-    Float_t weight = 1.0;
-    if(npads > 5) weight = 0.2;
-    else if(npads > 4) weight = 0.5;
-    sumw   += weight; 
-    sumwx  += fX[iTime] * weight;
-    sumwx2 += fX[iTime] * fX[iTime] * weight;
-    sumwy  += weight * yres[iTime];
-    sumwxy += weight * yres[iTime] * fX[iTime];
-    sumwz  += weight * fZ[iTime];    
-    sumwxz += weight * fZ[iTime] * fX[iTime];
-       }
-  if (fN2 < kClmin){
-               AliWarning(Form("Not enough clusters to fit the tracklet %d [%d].", fN2, kClmin));
-    fN2 = 0;
-    return kFALSE;
-  }
-  fMeanz = sumwz / sumw;
-       fNChange = 0;
-
-       // Tracklet on boundary
-  Float_t correction = 0;
-  if (fNChange > 0) {
-    if (fMeanz < fZProb) correction =  ycrosscor;
-    if (fMeanz > fZProb) correction = -ycrosscor;
-  }
+  const Int_t kClmin = 8;
+  const Float_t q0 = 100.;
+  const Float_t clSigma0 = 2.E-2;    //[cm]
+  const Float_t clSlopeQ = -1.19E-2; //[1/cm]
 
-  Double_t det = sumw * sumwx2 - sumwx * sumwx;
-  fYfitR[0]    = (sumwx2 * sumwy  - sumwx * sumwxy) / det;
-  fYfitR[1]    = (sumw   * sumwxy - sumwx * sumwy)  / det;
-  
-  fSigmaY2 = 0;
-  for (Int_t i = 0; i < fTimeBinsRange+1; i++) {
-    if (!fUsable[i]) continue;
-    Float_t delta = yres[i] - fYfitR[0] - fYfitR[1] * fX[i];
-    fSigmaY2 += delta*delta;
-  }
-  fSigmaY2 = TMath::Sqrt(fSigmaY2 / Float_t(fN2-2));
-  
-  fZfitR[0]  = (sumwx2 * sumwz  - sumwx * sumwxz) / det;
-  fZfitR[1]  = (sumw   * sumwxz - sumwx * sumwz)  / det;
-  fZfit[0]   = (sumwx2 * sumwz  - sumwx * sumwxz) / det;
-  fZfit[1]   = (sumw   * sumwxz - sumwx * sumwz)  / det;
-  fYfitR[0] += fYref[0] + correction;
-  fYfitR[1] += fYref[1];
-  fYfit[0]   = fYfitR[0];
-  fYfit[1]   = fYfitR[1];
-
-       return kTRUE;
-}
+  // get track direction
+  Double_t y0   = fYref[0];
+  Double_t dydx = fYref[1]; 
+  Double_t z0   = fZref[0];
+  Double_t dzdx = fZref[1];
+  Double_t yt, zt;
 
-//_____________________________________________________________________________
-Float_t AliTRDseedV1::FitRiemanTilt(AliTRDseedV1 *cseed, Bool_t terror)
-{
-  //
-  // Fit the Rieman tilt
-  //
+  const Int_t kNtb = AliTRDtrackerV1::GetNTimeBins();
+  AliTRDtrackerV1::AliTRDLeastSquare fitterY, fitterZ;
 
-  // Fitting with tilting pads - kz not fixed
-       AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
-       Int_t nTimeBins = cal->GetNumberOfTimeBins();
-  TLinearFitter fitterT2(4,"hyp4");  
-  fitterT2.StoreData(kTRUE);
-  Float_t xref2 = (cseed[2].fX0 + cseed[3].fX0) * 0.5; // Reference x0 for z
+  // convertion factor from square to gauss distribution for sigma
+  Double_t convert = 1./TMath::Sqrt(12.);
   
-  Int_t npointsT = 0;
-  fitterT2.ClearPoints();
-
-  for (Int_t iLayer = 0; iLayer < 6; iLayer++) {
-//             printf("\nLayer %d\n", iLayer);
-//     cseed[iLayer].Print();
-               if (!cseed[iLayer].IsOK()) continue;
-    Double_t tilt = cseed[iLayer].fTilt;
-
-    for (Int_t itime = 0; itime < nTimeBins+1; itime++) {
-//                     printf("\ttime %d\n", itime);
-      if (!cseed[iLayer].fUsable[itime]) continue;
-      // x relative to the midle chamber
-      Double_t x = cseed[iLayer].fX[itime] + cseed[iLayer].fX0 - xref2;  
-      Double_t y = cseed[iLayer].fY[itime];
-      Double_t z = cseed[iLayer].fZ[itime];
-
-      //
-      // Tilted rieman
-      //
-      Double_t uvt[6];
-      Double_t x2 = cseed[iLayer].fX[itime] + cseed[iLayer].fX0;      // Global x
-      Double_t t  = 1.0 / (x2*x2 + y*y);
-      uvt[1]  = t;
-      uvt[0]  = 2.0 * x2   * uvt[1];
-      uvt[2]  = 2.0 * tilt * uvt[1];
-      uvt[3]  = 2.0 * tilt *uvt[1] * x;              
-      uvt[4]  = 2.0 * (y + tilt * z) * uvt[1];
+  // book cluster information
+  Double_t xc[knTimebins], yc[knTimebins], zc[knTimebins], sy[knTimebins], sz[knTimebins];
+  Int_t zRow[knTimebins];
+
+
+  fN = 0;
+  AliTRDcluster *c=0x0, **jc = &fClusters[0];
+  for (Int_t ic=0; ic<kNtb; ic++, ++jc) {
+    zRow[ic] = -1;
+    xc[ic]  = -1.;
+    yc[ic]  = 999.;
+    zc[ic]  = 999.;
+    sy[ic]  = 0.;
+    sz[ic]  = 0.;
+    if(!(c = (*jc))) continue;
+    if(!c->IsInChamber()) continue;
+    Float_t w = 1.;
+    if(c->GetNPads()>4) w = .5;
+    if(c->GetNPads()>5) w = .2;
+    zRow[fN] = c->GetPadRow();
+    xc[fN]   = fX0 - c->GetX();
+    yc[fN]   = c->GetY();
+    zc[fN]   = c->GetZ();
+
+    // extrapolated y value for the track
+    yt = y0 - xc[fN]*dydx; 
+    // extrapolated z value for the track
+    zt = z0 - xc[fN]*dzdx; 
+    // tilt correction
+    if(tilt) yc[fN] -= fTilt*(zc[fN] - zt); 
+
+    // elaborate cluster error
+    Float_t qr = c->GetQ() - q0;
+    sy[fN]   = qr < 0. ? clSigma0*TMath::Exp(clSlopeQ*qr) : clSigma0;
+
+    fitterY.AddPoint(&xc[fN], yc[fN]-yt, sy[fN]);
+
+    sz[fN]   = fPadLength*convert;
+    fitterZ.AddPoint(&xc[fN], zc[fN], sz[fN]);
+    fN++;
+  }
+  // to few clusters
+  if (fN < kClmin) return kFALSE; 
+
+  // fit XY plane
+  fitterY.Eval();
+  fYfit[0] = y0+fitterY.GetFunctionParameter(0);
+  fYfit[1] = dydx-fitterY.GetFunctionParameter(1);
+
+  // check par row crossing
+  Int_t zN[2*AliTRDseed::knTimebins];
+  Int_t nz = AliTRDtrackerV1::Freq(fN, zRow, zN, kFALSE);
+  // more than one pad row crossing
+  if(nz>2) return kFALSE; 
+
+
+  // determine z offset of the fit
+  Float_t zslope = 0.;
+  Int_t nchanges = 0, nCross = 0;
+  if(nz==2){ // tracklet is crossing pad row
+    // Find the break time allowing one chage on pad-rows
+    // with maximal number of accepted clusters
+    Int_t padRef = zRow[0];
+    for (Int_t ic=1; ic<fN; ic++) {
+      if(zRow[ic] == padRef) continue;
       
-      Double_t error = 2.0 * uvt[1];
-      if (terror) {
-        error *= cseed[iLayer].fSigmaY;
+      // debug
+      if(zRow[ic-1] == zRow[ic]){
+        printf("ERROR in pad row change!!!\n");
       }
-      else {
-        error *= 0.2; //Default error
-      }
-//                     printf("\tadd point :\n");
-//                     for(int i=0; i<5; i++) printf("%f ", uvt[i]);
-//                     printf("\n");
-      fitterT2.AddPoint(uvt,uvt[4],error);
-      npointsT++;
-
+    
+      // evaluate parameters of the crossing point
+      Float_t sx = (xc[ic-1] - xc[ic])*convert;
+      fCross[0] = .5 * (xc[ic-1] + xc[ic]);
+      fCross[2] = .5 * (zc[ic-1] + zc[ic]);
+      fCross[3] = TMath::Max(dzdx * sx, .01);
+      zslope    = zc[ic-1] > zc[ic] ? 1. : -1.;
+      padRef    = zRow[ic];
+      nCross    = ic;
+      nchanges++;
     }
-
   }
-  fitterT2.Eval();
-  Double_t rpolz0 = fitterT2.GetParameter(3);
-  Double_t rpolz1 = fitterT2.GetParameter(4);      
 
-  //
-  // Linear fitter  - not possible to make boundaries
-  // non accept non possible z and dzdx combination
-  //       
-  Bool_t acceptablez = kTRUE;
-  for (Int_t iLayer = 0; iLayer < 6; iLayer++) {
-    if (cseed[iLayer].IsOK()) {
-      Double_t zT2 = rpolz0 + rpolz1 * (cseed[iLayer].fX0 - xref2);
-      if (TMath::Abs(cseed[iLayer].fZProb - zT2) > cseed[iLayer].fPadLength * 0.5 + 1.0) {
-       acceptablez = kFALSE;
-      }
-    }
-  }
-  if (!acceptablez) {
-    Double_t zmf  = cseed[2].fZref[0] + cseed[2].fZref[1] * (xref2 - cseed[2].fX0);
-    Double_t dzmf = (cseed[2].fZref[1] + cseed[3].fZref[1]) * 0.5;
-    fitterT2.FixParameter(3,zmf);
-    fitterT2.FixParameter(4,dzmf);
-    fitterT2.Eval();
-    fitterT2.ReleaseParameter(3);
-    fitterT2.ReleaseParameter(4);
-    rpolz0 = fitterT2.GetParameter(3);
-    rpolz1 = fitterT2.GetParameter(4);
-  }
-  
-  Double_t chi2TR = fitterT2.GetChisquare() / Float_t(npointsT);  
-  Double_t params[3];
-  params[0] =  fitterT2.GetParameter(0);
-  params[1] =  fitterT2.GetParameter(1);
-  params[2] =  fitterT2.GetParameter(2);           
-  Double_t curvature =  1.0 + params[1] * params[1] - params[2] * params[0];
-
-  for (Int_t iLayer = 0; iLayer < 6; iLayer++) {
-
-    Double_t  x  = cseed[iLayer].fX0;
-    Double_t  y  = 0;
-    Double_t  dy = 0;
-    Double_t  z  = 0;
-    Double_t  dz = 0;
-
-    // y
-    Double_t res2 = (x * params[0] + params[1]);
-    res2 *= res2;
-    res2  = 1.0 - params[2]*params[0] + params[1]*params[1] - res2;
-    if (res2 >= 0) {
-      res2 = TMath::Sqrt(res2);
-      y    = (1.0 - res2) / params[0];
-    }
+  // condition on nCross and reset nchanges TODO
 
-    //dy
-    Double_t x0 = -params[1] / params[0];
-    if (-params[2]*params[0] + params[1]*params[1] + 1 > 0) {
-      Double_t rm1 = params[0] / TMath::Sqrt(-params[2]*params[0] + params[1]*params[1] + 1); 
-      if (1.0/(rm1*rm1) - (x-x0) * (x-x0) > 0.0) {
-       Double_t res = (x - x0) / TMath::Sqrt(1.0 / (rm1*rm1) - (x-x0)*(x-x0));
-       if (params[0] < 0) res *= -1.0;
-       dy = res;
-      }
+  if(nchanges==1){
+    if(dzdx * zslope < 0.){
+      AliInfo("tracklet direction does not correspond to the track direction. TODO.");
     }
-    z  = rpolz0 + rpolz1 * (x - xref2);
-    dz = rpolz1;
-    cseed[iLayer].fYref[0] = y;
-    cseed[iLayer].fYref[1] = dy;
-    cseed[iLayer].fZref[0] = z;
-    cseed[iLayer].fZref[1] = dz;
-    cseed[iLayer].fC       = curvature;
-    
+    SetBit(kRowCross, kTRUE); // mark pad row crossing
+    fitterZ.AddPoint(&fCross[0], fCross[2], fCross[3]);
+    fitterZ.Eval();
+    //zc[nc] = fitterZ.GetFunctionParameter(0); 
+    fCross[1] = fYfit[0] - fCross[0] * fYfit[1];
+    fCross[0] = fX0 - fCross[0];
+  } else if(nchanges > 1){ // debug
+    AliError("N pad row crossing > 1.");
+    return kFALSE;
   }
 
-  return chi2TR;
+  UpdateUsed();
 
+  return kTRUE;
 }
 
+
 //___________________________________________________________________
-void AliTRDseedV1::Print()
+void AliTRDseedV1::Print(Option_t*) const
 {
   //
   // Printing the seedstatus
   //
 
-       AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
-       Int_t nTimeBins = cal->GetNumberOfTimeBins();
-       
-       printf("Seed status :\n");
-       printf("  fTilt      = %f\n", fTilt);
-       printf("  fPadLength = %f\n", fPadLength);
-       printf("  fX0        = %f\n", fX0);
-       for(int ic=0; ic<nTimeBins; ic++) {
-          const Char_t *isUsable = fUsable[ic]?"Yes":"No";
-         printf("  %d X[%f] Y[%f] Z[%f] Indexes[%d] clusters[%p] usable[%s]\n"
-                , ic
-                , fX[ic]
-                , fY[ic]
-                , fZ[ic]
-                , fIndexes[ic]
-                , ((void*) fClusters[ic])
-                , isUsable);
-        }
-
-       printf("  fYref[0] =%f fYref[1] =%f\n", fYref[0], fYref[1]);
-       printf("  fZref[0] =%f fZref[1] =%f\n", fZref[0], fZref[1]);
-       printf("  fYfit[0] =%f fYfit[1] =%f\n", fYfit[0], fYfit[1]);
-       printf("  fYfitR[0]=%f fYfitR[1]=%f\n", fYfitR[0], fYfitR[1]);
-       printf("  fZfit[0] =%f fZfit[1] =%f\n", fZfit[0], fZfit[1]);
-       printf("  fZfitR[0]=%f fZfitR[1]=%f\n", fZfitR[0], fZfitR[1]);
-       printf("  fSigmaY =%f\n", fSigmaY);
-       printf("  fSigmaY2=%f\n", fSigmaY2);            
-       printf("  fMeanz  =%f\n", fMeanz);
-       printf("  fZProb  =%f\n", fZProb);
-       printf("  fLabels[0]=%d fLabels[1]=%d\n", fLabels[0], fLabels[1]);
-       printf("  fN      =%d\n", fN);
-       printf("  fN2     =%d (>8 isOK)\n",fN2);
-       printf("  fNUsed  =%d\n", fNUsed);
-       printf("  fFreq   =%d\n", fFreq);
-       printf("  fNChange=%d\n",  fNChange);
-       printf("  fMPads  =%f\n", fMPads);
-       
-       printf("  fC      =%f\n", fC);        
-       printf("  fCC     =%f\n",fCC);      
-       printf("  fChi2   =%f\n", fChi2);  
-       printf("  fChi2Z  =%f\n", fChi2Z);
+  AliInfo(Form("Tracklet X0[%7.2f] Det[%d]", fX0, fDet));
+  printf("  Tilt[%+6.2f] PadLength[%5.2f]\n", fTilt, fPadLength);
+  AliTRDcluster* const* jc = &fClusters[0];
+  for(int ic=0; ic<AliTRDtrackerV1::GetNTimeBins(); ic++, jc++) {
+    if(!(*jc)) continue;
+    printf("  %2d X[%7.2f] Y[%7.2f] Z[%7.2f] Idx[%d] c[%p] usable[%s]\n", 
+      ic, (*jc)->GetX(), (*jc)->GetY(), (*jc)->GetZ(), 
+      fIndexes[ic], (void*)(*jc), fUsable[ic]?"y":"n");
+  }
 
+  printf("  fYref[0] =%f fYref[1] =%f\n", fYref[0], fYref[1]);
+  printf("  fZref[0] =%f fZref[1] =%f\n", fZref[0], fZref[1]);
+  printf("  fYfit[0] =%f fYfit[1] =%f\n", fYfit[0], fYfit[1]);
+  printf("  fZfit[0] =%f fZfit[1] =%f\n", fZfit[0], fZfit[1]);
+  printf("  fSigmaY =%f\n", fSigmaY);
+  printf("  fSigmaY2=%f\n", fSigmaY2);            
+  printf("  fMeanz  =%f\n", fMeanz);
+  printf("  fZProb  =%f\n", fZProb);
+  printf("  fLabels[0]=%d fLabels[1]=%d\n", fLabels[0], fLabels[1]);
+  printf("  fN      =%d\n", fN);
+  printf("  fN2     =%d (>4 isOK - to be redesigned)\n",fN2);
+  printf("  fNUsed  =%d\n", fNUsed);
+  printf("  fFreq   =%d\n", fFreq);
+  printf("  fNChange=%d\n",  fNChange);
+  printf("  fMPads  =%f\n", fMPads);
+  
+  printf("  fC      =%f\n", fC);        
+  printf("  fCC     =%f\n",fCC);      
+  printf("  fChi2   =%f\n", fChi2);  
+  printf("  fChi2Z  =%f\n", fChi2Z);
 }
+