+//---------------------------------------------------------------------------
+Int_t AliTRDtracker::Refit(AliTRDtrack& t, Int_t rf)
+{
+ // Starting from current position on track=t this function tries
+ // to extrapolate the track up to timeBin=0 and to reuse already
+ // assigned clusters. Returns the number of clusters
+ // expected to be found in sensitive layers
+ // get indices of assigned clusters for each layer
+ // Origin: Thomas KUHR (Thomas.Kuhr@cern.ch)
+
+ Int_t iCluster[90];
+ for (Int_t i = 0; i < 90; i++) iCluster[i] = 0;
+ for (Int_t i = 0; i < t.GetNumberOfClusters(); i++) {
+ Int_t index = t.GetClusterIndex(i);
+ AliTRDcluster *cl=(AliTRDcluster*) GetCluster(index);
+ if (!cl) continue;
+ Int_t detector=cl->GetDetector();
+ Int_t localTimeBin=cl->GetLocalTimeBin();
+ Int_t sector=fGeom->GetSector(detector);
+ Int_t plane=fGeom->GetPlane(detector);
+
+ Int_t trackingSector = CookSectorIndex(sector);
+
+ Int_t gtb = fTrSec[trackingSector]->CookTimeBinIndex(plane,localTimeBin);
+ if(gtb < 0) continue;
+ Int_t layer = fTrSec[trackingSector]->GetLayerNumber(gtb);
+ iCluster[layer] = index;
+ }
+ t.ResetClusters();
+
+ Int_t ns=Int_t(2*TMath::Pi()/AliTRDgeometry::GetAlpha()+0.5);
+
+ Double_t alpha=t.GetAlpha();
+ alpha = TVector2::Phi_0_2pi(alpha);
+
+ Int_t s=Int_t(alpha/AliTRDgeometry::GetAlpha())%AliTRDgeometry::kNsect;
+ Double_t radLength, rho, x, dx, y, ymax, z;
+
+ Int_t expectedNumberOfClusters = 0;
+ Bool_t lookForCluster;
+
+ alpha=AliTRDgeometry::GetAlpha(); // note: change in meaning
+
+
+ for (Int_t nr=fTrSec[0]->GetLayerNumber(t.GetX()); nr>rf; nr--) {
+
+ y = t.GetY(); z = t.GetZ();
+
+ // first propagate to the inner surface of the current time bin
+ fTrSec[s]->GetLayer(nr)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
+ x = fTrSec[s]->GetLayer(nr)->GetX()-dx/2; y = t.GetY(); z = t.GetZ();
+ if(!t.PropagateTo(x,radLength,rho)) break;
+ y = t.GetY();
+ ymax = x*TMath::Tan(0.5*alpha);
+ if (y > ymax) {
+ s = (s+1) % ns;
+ if (!t.Rotate(alpha)) break;
+ if(!t.PropagateTo(x,radLength,rho)) break;
+ } else if (y <-ymax) {
+ s = (s-1+ns) % ns;
+ if (!t.Rotate(-alpha)) break;
+ if(!t.PropagateTo(x,radLength,rho)) break;
+ }
+
+ y = t.GetY(); z = t.GetZ();
+
+ // now propagate to the middle plane of the next time bin
+ fTrSec[s]->GetLayer(nr-1)->GetPropagationParameters(y,z,dx,rho,radLength,lookForCluster);
+ x = fTrSec[s]->GetLayer(nr-1)->GetX(); y = t.GetY(); z = t.GetZ();
+ if(!t.PropagateTo(x,radLength,rho)) break;
+ y = t.GetY();
+ ymax = x*TMath::Tan(0.5*alpha);
+ if (y > ymax) {
+ s = (s+1) % ns;
+ if (!t.Rotate(alpha)) break;
+ if(!t.PropagateTo(x,radLength,rho)) break;
+ } else if (y <-ymax) {
+ s = (s-1+ns) % ns;
+ if (!t.Rotate(-alpha)) break;
+ if(!t.PropagateTo(x,radLength,rho)) break;
+ }
+
+ if(lookForCluster) expectedNumberOfClusters++;
+
+ // use assigned cluster
+ if (!iCluster[nr-1]) continue;
+ AliTRDcluster *cl=(AliTRDcluster*)GetCluster(iCluster[nr-1]);
+ Double_t h01 = GetTiltFactor(cl);
+ Double_t chi2=t.GetPredictedChi2(cl, h01);
+ t.SetSampledEdx(cl->GetQ()/dx,t.GetNumberOfClusters());
+ t.Update(cl,chi2,iCluster[nr-1],h01);
+ }
+
+ return expectedNumberOfClusters;
+}
+