]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDseedV1.cxx
Bug fix in PID object definition
[u/mrichter/AliRoot.git] / TRD / AliTRDseedV1.cxx
index 2cbcca783698d570513751e98a55e6a93698f906..bae8ec3ff6722c3f2ebd4142915365a8753af2ec 100644 (file)
 
 #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 "AliTRDcalibDB.h"
-#include "AliTRDstackLayer.h"
+#include "AliTRDchamberTimeBin.h"
+#include "AliTRDtrackingChamber.h"
+#include "AliTRDtrackerV1.h"
+#include "AliTRDReconstructor.h"
 #include "AliTRDrecoParam.h"
-
-#define SEED_DEBUG
+#include "AliTRDgeometry.h"
+#include "Cal/AliTRDCalPID.h"
 
 ClassImp(AliTRDseedV1)
 
 //____________________________________________________________________
-AliTRDseedV1::AliTRDseedV1(Int_t layer, AliTRDrecoParam *p
+AliTRDseedV1::AliTRDseedV1(Int_t plane
   :AliTRDseed()
-  ,fLayer(layer)
-  ,fTimeBins(0)
-  ,fOwner(kFALSE)
-  ,fRecoParam(p)
+  ,fPlane(plane)
+  ,fMom(0.)
+  ,fSnp(0.)
+  ,fTgl(0.)
+  ,fdX(0.)
 {
   //
   // Constructor
   //
-
-       //AliInfo("");
-       AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
-       fTimeBins = cal->GetNumberOfTimeBins();
-
+       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, Bool_t owner)
+AliTRDseedV1::AliTRDseedV1(const AliTRDseedV1 &ref)
   :AliTRDseed((AliTRDseed&)ref)
-  ,fLayer(ref.fLayer)
-  ,fTimeBins(ref.fTimeBins)
-  ,fOwner(kFALSE)
-  ,fRecoParam(ref.fRecoParam)
+  ,fPlane(ref.fPlane)
+  ,fMom(ref.fMom)
+  ,fSnp(ref.fSnp)
+  ,fTgl(ref.fTgl)
+  ,fdX(ref.fdX)
 {
   //
   // Copy Constructor performing a deep copy
   //
 
        //AliInfo("");
-
-       if(owner){
-               for(int ic=0; ic<fTimeBins; ic++){
-                       if(!fClusters[ic]) continue;
-                       fClusters[ic] = new AliTRDcluster(*fClusters[ic]);
-               }
-               fOwner = kTRUE;
-       }
-
+       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];
 }
 
+
 //____________________________________________________________________
 AliTRDseedV1& AliTRDseedV1::operator=(const AliTRDseedV1 &ref)
 {
@@ -107,7 +106,13 @@ AliTRDseedV1::~AliTRDseedV1()
 
        //AliInfo(Form("fOwner[%s]", fOwner?"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;
+               }
 }
 
 //____________________________________________________________________
@@ -120,12 +125,156 @@ void AliTRDseedV1::Copy(TObject &ref) const
        //AliInfo("");
        AliTRDseedV1 &target = (AliTRDseedV1 &)ref; 
        
-       target.fLayer     = fLayer;
-       target.fTimeBins  = fTimeBins;
-       target.fRecoParam = fRecoParam;
+       target.fPlane         = fPlane;
+       target.fMom           = fMom;
+       target.fSnp           = fSnp;
+       target.fTgl           = fTgl;
+       target.fdX            = fdX;
+       
+       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)
+{
+// Initialize this tracklet using the track information
+//
+// Parameters:
+//   track - the TRD track used to initialize the tracklet
+// 
+// Detailed description
+// The function sets the starting point and direction of the
+// tracklet according to the information from the TRD track.
+// 
+// Caution
+// The TRD track has to be propagated to the beginning of the
+// chamber where the tracklet will be constructed
+//
+
+       Double_t y, z; 
+       track->GetProlongation(fX0, y, z);
+       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());
+}
+
+
+//____________________________________________________________________
+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
+
+       // calculate mean charge per slice
+       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;
+  }
+
+  // Retrieve the CDB container class with the parametric detector response
+  const AliTRDCalPID *pd = calibration->GetPIDObject(AliTRDReconstructor::RecoParam()->GetPIDMethod());
+  if (!pd) {
+    AliError("No access to AliTRDCalPID object");
+    return 0x0;
+  }
+       //AliInfo(Form("Method[%d] : %s", AliTRDReconstructor::RecoParam()->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(AliTRDReconstructor::RecoParam()->GetNdEdxSlices());
+  
+  // Sets the a priori probabilities
+  for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) {
+    fProb[ispec] = pd->GetProbability(ispec, fMom, &fdEdx[0], length, fPlane); 
+  }
+
+       return &fProb[0];
+}
+
 //____________________________________________________________________
 Float_t AliTRDseedV1::GetQuality(Bool_t kZcorr) const
 {
@@ -134,31 +283,103 @@ Float_t AliTRDseedV1::GetQuality(Bool_t kZcorr) const
   //
 
        Float_t zcorr = kZcorr ? fTilt * (fZProb - fZref[0]) : 0.;
-       return .5 * (18.0 - fN2)
+       return 
+                 .5 * TMath::Abs(18.0 - fN2)
                + 10.* TMath::Abs(fYfit[1] - fYref[1])
-               + 5.* TMath::Abs(fYfit[0] - fYref[0] + zcorr)
+               + 5. * TMath::Abs(fYfit[0] - fYref[0] + zcorr)
                + 2. * TMath::Abs(fMeanz - fZref[0]) / fPadLength;
 }
 
 //____________________________________________________________________
-Bool_t AliTRDseedV1::AttachClustersIter(AliTRDstackLayer *layer
-                                       , Float_t quality
-                                       , Bool_t kZcorr
-                                       , AliTRDcluster *c)
+void AliTRDseedV1::GetCovAt(Double_t /*x*/, Double_t *cov) const
+{
+// Computes covariance in the y-z plane at radial point x
+
+       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;
+}
+
+
+//____________________________________________________________________
+void AliTRDseedV1::SetOwner(Bool_t own)
+{
+       //AliInfo(Form("own [%s] fOwner[%s]", own?"YES":"NO", fOwner?"YES":"NO"));
+       
+       if(own){
+               for(int ic=0; ic<knTimebins; ic++){
+                       if(!fClusters[ic]) continue;
+                       fClusters[ic] = new AliTRDcluster(*fClusters[ic]);
+               }
+               SetBit(1);
+       } else {
+               if(IsOwner()){
+                       for(int ic=0; ic<knTimebins; ic++){
+                               if(!fClusters[ic]) continue;
+                               delete fClusters[ic];
+                               //fClusters[ic] = tracker->GetClusters(index) TODO
+                       }
+               }
+               SetBit(1, kFALSE);
+       }
+}
+
+//____________________________________________________________________
+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)
   //
+       // debug level 7
+       //
        
-       if(!fRecoParam){
+       if(!AliTRDReconstructor::RecoParam()){
                AliError("Seed can not be used without a valid RecoParam.");
                return kFALSE;
        }
-       
+
+       AliTRDchamberTimeBin *layer = 0x0;
+       if(AliTRDReconstructor::StreamLevel()>=7 && c){
+               TClonesArray clusters("AliTRDcluster", 24);
+               clusters.SetOwner(kTRUE);
+               AliTRDcluster *cc = 0x0;
+               Int_t det=-1, ncl, ncls = 0;
+               for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
+                       if(!(layer = chamber->GetTB(iTime))) continue;
+                       if(!(ncl = Int_t(*layer))) continue;
+                       for(int ic=0; ic<ncl; ic++){ 
+                               cc = (*layer)[ic];
+                               det = cc->GetDetector();
+                               new(clusters[ncls++]) AliTRDcluster(*cc);
+                       }
+               }
+               AliInfo(Form("N clusters[%d] = %d", fPlane, ncls));
+               
+               Int_t ref = c ? 1 : 0;
+               TTreeSRedirector &cstreamer = *AliTRDtrackerV1::DebugStreamer();
+               cstreamer << "AttachClustersIter"
+                       << "det="        << det 
+                       << "ref="        << ref 
+                       << "clusters.="  << &clusters
+                       << "tracklet.="  << this
+                       << "cl.="        << c
+                       << "\n";        
+       }
+
        Float_t  tquality;
-       Double_t kroady = fRecoParam->GetRoad1y();
+       Double_t kroady = AliTRDReconstructor::RecoParam()->GetRoad1y();
        Double_t kroadz = fPadLength * .5 + 1.;
        
        // initialize configuration parameters
@@ -169,11 +390,13 @@ Bool_t    AliTRDseedV1::AttachClustersIter(AliTRDstackLayer *layer
        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 < fTimeBins; iTime++) {
+               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[iTime].GetX() - fX0;
+                       Double_t dxlayer = layer->GetX() - fX0;
                        if(c){
                                zexp = c->GetZ();
                                //Try 2 pad-rows in second iteration
@@ -182,54 +405,63 @@ Bool_t    AliTRDseedV1::AttachClustersIter(AliTRDstackLayer *layer
                                        if (zexp > c->GetZ()) zexp = c->GetZ() + fPadLength*0.5;
                                        if (zexp < c->GetZ()) zexp = c->GetZ() - fPadLength*0.5;
                                }
-                       } else zexp = fZref[0];
+                       } else zexp = fZref[0] + (kZcorr ? fZref[1] * dxlayer : 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);
-//                     for(Int_t iclk = 0; iclk < layer[iTime].GetNClusters(); iclk++){
-//                             AliTRDcluster *testcl = layer[iTime].GetCluster(iclk);
-//                             printf("Cluster %i: x = %3.3f, y = %3.3f, z = %3.3f\n",iclk,testcl->GetX(), testcl->GetY(), testcl->GetZ());
-//                     }
-//                     printf("Index = %i\n",index);
-                       if (index < 0) continue;
                        
-                       // Register cluster
-                       AliTRDcluster *cl = (AliTRDcluster*) layer[iTime].GetCluster(index);
+                       // Get and register cluster
+                       Int_t    index = layer->SearchNearestCluster(yexp, zexp, kroady, kroadz);
+                       if (index < 0) continue;
+                       AliTRDcluster *cl = (*layer)[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;
+                       fIndexes[iTime]  = layer->GetGlobalIndex(index);
                        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<fTimeBins+1; ic++){
-//                     AliTRDcluster *ci = fClusters[ic];
-//                     if(!ci) continue;
-//                     for(int jc=ic+1; jc<fTimeBins+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(AliTRDReconstructor::StreamLevel()>=7) AliInfo(Form("iter = %d ncl [%d] = %d", iter, fPlane, 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(AliTRDReconstructor::StreamLevel()>=7) AliInfo(Form("iter = %d nclFit [%d] = %d", iter, fPlane, fN2));
+               
                if(IsOK()){
                        tquality = GetQuality(kZcorr);
                        if(tquality < quality) break;
@@ -245,10 +477,8 @@ Bool_t     AliTRDseedV1::AttachClustersIter(AliTRDstackLayer *layer
 }
 
 //____________________________________________________________________
-Bool_t AliTRDseedV1::AttachClustersProj(AliTRDstackLayer *layer
-                                       , Float_t /*quality*/
-                                       , Bool_t kZcorr
-                                       , AliTRDcluster *c)
+Bool_t AliTRDseedV1::AttachClusters(AliTRDtrackingChamber *chamber
+                                       ,Bool_t kZcorr)
 {
   //
   // Projective algorithm to attach clusters to seeding tracklets
@@ -266,39 +496,42 @@ Bool_t    AliTRDseedV1::AttachClustersProj(AliTRDstackLayer *layer
   // 6. fit tracklet
   //   
 
-       if(!fRecoParam){
+       if(!AliTRDReconstructor::RecoParam()){
                AliError("Seed can not be used without a valid RecoParam.");
                return kFALSE;
        }
 
-       const Int_t knTimeBins = 35;
-       const Int_t kClusterCandidates = 2 * knTimeBins;
+       const Int_t kClusterCandidates = 2 * knTimebins;
        
        //define roads
-       Double_t kroady = fRecoParam->GetRoad1y();
+       Double_t kroady = AliTRDReconstructor::RecoParam()->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],
+       Double_t cond[4], yexp[knTimebins], zexp[knTimebins],
                yres[kClusterCandidates], zres[kClusterCandidates];
-       Int_t ncl, *index = 0x0, tboundary[knTimeBins];
+       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 < fTimeBins; iTime++) {
-               fX[iTime] = layer[iTime].GetX() - fX0;
+       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[iTime].GetClusters(cond, index, ncl);
+               layer->GetClusters(cond, index, ncl);
                for(Int_t ic = 0; ic<ncl; ic++){
-                       c = layer[iTime].GetCluster(index[ic]);
+                       AliTRDcluster *c = layer->GetCluster(index[ic]);
                        clusters[nYclusters] = c;
                        yres[nYclusters++] = c->GetY() - yexp[iTime];
                        if(nYclusters >= kClusterCandidates) {
@@ -314,7 +547,7 @@ Bool_t      AliTRDseedV1::AttachClustersProj(AliTRDstackLayer *layer
        // 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
+       // purge cluster candidates
        Int_t nZclusters = 0;
        for(Int_t ic = 0; ic<nYclusters; ic++){
                if(yres[ic] - mean > 4. * sigma){
@@ -326,7 +559,7 @@ Bool_t      AliTRDseedV1::AttachClustersProj(AliTRDstackLayer *layer
        
        // Evaluate truncated mean on the z direction
        AliMathBase::EvaluateUni(nZclusters, zres, mean, sigma, Int_t(nZclusters*.8)-2);
-       //purge cluster candidates
+       // purge cluster candidates
        for(Int_t ic = 0; ic<nZclusters; ic++){
                if(zres[ic] - mean > 4. * sigma){
                        clusters[ic] = 0x0;
@@ -338,13 +571,12 @@ Bool_t    AliTRDseedV1::AttachClustersProj(AliTRDstackLayer *layer
        // Select only one cluster/TimeBin
        Int_t lastCluster = 0;
        fN2 = 0;
-       for (Int_t iTime = 0; iTime < fTimeBins; iTime++) {
+       for (Int_t iTime = 0; iTime < AliTRDtrackerV1::GetNTimeBins(); iTime++) {
                ncl = tboundary[iTime] - lastCluster;
                if(!ncl) continue;
-               if(ncl == 1){
-                       c = clusters[lastCluster];
-               } else if(ncl > 1){
-                       Float_t dold = 9999.; Int_t iptr = lastCluster;
+               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();
@@ -354,34 +586,41 @@ Bool_t    AliTRDseedV1::AttachClustersProj(AliTRDstackLayer *layer
                                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];
+               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(fRecoParam->GetFindableClusters()*fTimeBins);
+       Int_t kClmin = Int_t(AliTRDReconstructor::RecoParam()->GetFindableClusters()*AliTRDtrackerV1::GetNTimeBins());
   if (fN2 < kClmin){
                AliWarning(Form("Not enough clusters to fit the tracklet %d [%d].", fN2, kClmin));
     fN2 = 0;
     return kFALSE;
   }
-       AliTRDseed::Update();
+
+       // 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));
+    fN2 = 0;
+    return kFALSE;
+  }
        
-//     // fit tracklet and update clusters
-//     if(!FitTracklet()) return kFALSE;
-//     UpdateUsed();
        return kTRUE;
 }
 
 //____________________________________________________________________
-Bool_t AliTRDseedV1::FitTracklet()
+Bool_t AliTRDseedV1::Fit()
 {
   //
   // Linear fit of the tracklet
@@ -397,263 +636,142 @@ Bool_t AliTRDseedV1::FitTracklet()
   // 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
+       const Int_t kClmin = 8;
+       const Int_t kNtb = AliTRDtrackerV1::GetNTimeBins();
+       AliTRDtrackerV1::AliTRDLeastSquare fitterY, fitterZ;
+
+       // convertion factor from square to gauss distribution for sigma
+       Double_t convert = 1./TMath::Sqrt(12.);
+
+       // book cluster information
+       Double_t xc[knTimebins+1], yc[knTimebins], zc[knTimebins+1], sy[knTimebins], sz[knTimebins+1];
+       Int_t zRow[knTimebins];
+       AliTRDcluster *c = 0x0;
+       Int_t nc = 0;
+       for (Int_t ic=0; ic<kNtb; ic++) {
+               zRow[ic] = -1;
+               xc[ic]  = -1.;
+               yc[ic]  = 999.;
+               zc[ic]  = 999.;
+               sy[ic]  = 0.;
+               sz[ic]  = 0.;
+               if(!(c = fClusters[ic])) continue;
+               if(!c->IsInChamber()) continue;
+               Float_t w = 1.;
+               if(c->GetNPads()>4) w = .5;
+               if(c->GetNPads()>5) w = .2;
+               zRow[nc] = c->GetPadRow();
+               xc[nc]   = fX0 - c->GetX();
+               yc[nc]   = c->GetY();
+               zc[nc]   = c->GetZ();
+               sy[ic]   = w; // all clusters have the same sigma
+               sz[ic]   = fPadLength*convert;
+               fitterZ.AddPoint(&xc[ic], zc[ic], sz[ic]);
+               nc++;
+       }
+  // to few clusters
+       if (nc < kClmin) return kFALSE; 
+       
 
-       // calculate residuals
-       const Int_t knTimeBins = 35;
-       Float_t yres[knTimeBins]; // y (r-phi) residuals
-       Int_t zint[knTimeBins],   // Histograming of the z coordinate 
-             zout[2*knTimeBins];//
+       Int_t zN[2*35];
+  Int_t nz = AliTRDtrackerV1::Freq(nc, zRow, zN, kFALSE);
+       // more than one pad row crossing
+       if(nz>2) return kFALSE; 
        
-       fN = 0;
-       for (Int_t iTime = 0; iTime < fTimeBins; iTime++) {
-    if (!fClusters[iTime]) continue;
-    yres[iTime] = fY[iTime] - fYref[0] - (fYref[1] + anglecor) * fX[iTime];
-               zint[fN++] = Int_t(fZ[iTime]);
+       // estimate reference parameter at average x
+       Double_t y0 = fYref[0];
+       Double_t dydx = fYref[1]; 
+       Double_t dzdx = fZref[1];
+       zc[nc]  = fZref[0];
+
+       // determine z offset of the fit
+       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<nc; ic++) {
+                       if(zRow[ic] == padRef) continue;
+                       
+                       // debug
+                       if(zRow[ic-1] == zRow[ic]){
+                               printf("ERROR in pad row change!!!\n");
+                       }
+               
+                       // evaluate parameters of the crossing point
+                       Float_t sx = (xc[ic-1] - xc[ic])*convert;
+                       xc[nc] = .5 * (xc[ic-1] + xc[ic]);
+                       zc[nc] = .5 * (zc[ic-1] + zc[ic]);
+                       sz[nc] = TMath::Max(dzdx * sx, .01);
+                       dzdx   = zc[ic-1] > zc[ic] ? 1. : -1.;
+                       padRef = zRow[ic];
+                       nCross = ic;
+                       nchanges++;
+               }
        }
 
-       // calculate pad row boundary crosses
-       Int_t kClmin = Int_t(fRecoParam->GetFindableClusters()*fTimeBins);
-       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));
+       // condition on nCross and reset nchanges TODO
+
+       if(nchanges==1){
+               if(dzdx * fZref[1] < 0.){
+                       AliInfo("tracklet direction does not correspond to the track direction. TODO.");
+               }
+               SetBit(2, kTRUE); // mark pad row crossing
+               fCross[0] = xc[nc]; fCross[2] = zc[nc]; fCross[3] = sz[nc]; 
+               fitterZ.AddPoint(&xc[nc], zc[nc], sz[nc]);
+               fitterZ.Eval();
+               dzdx = fZref[1]; // we don't trust Parameter[1] ??;
+               zc[nc] = fitterZ.GetFunctionParameter(0); 
+       } else if(nchanges > 1){ // debug
+               AliInfo("ERROR in n changes!!!");
                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.;
-       for(int iTime=0; iTime<fTimeBins; 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];
+
+       
+       // estimate deviation from reference direction
+       dzdx *= fTilt;
+       for (Int_t ic=0; ic<nc; ic++) {
+               yc[ic] -= y0 + xc[ic]*(dydx + dzdx) + fTilt * (zc[ic] - zc[nc]);
+               fitterY.AddPoint(&xc[ic], yc[ic], sy[ic]);
        }
-  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;
-  }
+       fitterY.Eval();
+       fYfit[0] = y0+fitterY.GetFunctionParameter(0);
+       fYfit[1] = dydx+fitterY.GetFunctionParameter(1);
+       if(nchanges) fCross[1] = fYfit[0] + fCross[0] * fYfit[1];
 
-  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 < fTimeBins+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];
+//     printf("\nnz = %d\n", nz);
+//     for(int ic=0; ic<35; ic++) printf("%d row[%d]\n", ic, zRow[ic]);        
+// 
+//     for(int ic=0; ic<nz; ic++) printf("%d n[%d]\n", ic, zN[ic]);    
 
        return kTRUE;
 }
 
-//_____________________________________________________________________________
-Float_t AliTRDseedV1::FitRiemanTilt(AliTRDseedV1 *cseed, Bool_t terror)
+//___________________________________________________________________
+void AliTRDseedV1::Draw(Option_t*)
 {
-  //
-  // Fit the Rieman tilt
-  //
-
-  // 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
-  
-  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];
-      
-      Double_t error = 2.0 * uvt[1];
-      if (terror) {
-        error *= cseed[iLayer].fSigmaY;
-      }
-      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++;
-
-    }
-
-  }
-  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];
-    }
-
-    //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;
-      }
-    }
-    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;
-    
-  }
-
-  return chi2TR;
-
 }
 
 //___________________________________________________________________
-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++) {
+       for(int ic=0; ic<AliTRDtrackerV1::GetNTimeBins(); ic++) {
           const Char_t *isUsable = fUsable[ic]?"Yes":"No";
-         printf("  %d X[%f] Y[%f] Z[%f] Indexes[%d] clusters[%#x] usable[%s]\n"
+         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])
+                , ((void*) fClusters[ic])
                 , isUsable);
         }
 
@@ -679,5 +797,5 @@ void AliTRDseedV1::Print()
        printf("  fCC     =%f\n",fCC);      
        printf("  fChi2   =%f\n", fChi2);  
        printf("  fChi2Z  =%f\n", fChi2Z);
-
 }
+