]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONTrackReconstructorK.cxx
- AliMUONRecoParam.cxx:
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackReconstructorK.cxx
index 5cf67eb3e342c03000845df6550980de65e44257..d876a331c53c95659b9c7c2f64dbab42eaa55d50 100644 (file)
@@ -15,8 +15,8 @@
 
 /* $Id$ */
 
+//-----------------------------------------------------------------------------
 /// \class AliMUONTrackReconstructorK
-/////////////////////////////////////
 ///
 /// MUON track reconstructor using the kalman method
 ///
 /// It contains as methods, among others:
 /// - MakeTracks to build the tracks
 ///
-////////////////////////////////////
+//-----------------------------------------------------------------------------
 
 #include "AliMUONTrackReconstructorK.h"
+
 #include "AliMUONConstants.h"
-#include "AliMUONHitForRec.h"
+#include "AliMUONVCluster.h"
+#include "AliMUONVClusterServer.h"
+#include "AliMUONVClusterStore.h"
 #include "AliMUONTrack.h"
 #include "AliMUONTrackParam.h"
 #include "AliMUONTrackExtrap.h"
 
+#include "AliMpArea.h"
+
 #include "AliLog.h"
 
 #include <Riostream.h>
 ClassImp(AliMUONTrackReconstructorK) // Class implementation in ROOT context
 /// \endcond
 
-//************* Defaults parameters for reconstruction
-const Bool_t AliMUONTrackReconstructorK::fgkRunSmoother = kTRUE;
-
-
-//__________________________________________________________________________
-AliMUONTrackReconstructorK::AliMUONTrackReconstructorK()
-  : AliMUONVTrackReconstructor()
+  //__________________________________________________________________________
+AliMUONTrackReconstructorK::AliMUONTrackReconstructorK(const AliMUONRecoParam* recoParam, AliMUONVClusterServer* clusterServer)
+  : AliMUONVTrackReconstructor(recoParam, clusterServer)
 {
-  /// Constructor for class AliMUONTrackReconstructorK
-  AliInfo("*** Tracking with Kalman Filter ***");
+  /// Constructor
 }
 
-//__________________________________________________________________________
+  //__________________________________________________________________________
 AliMUONTrackReconstructorK::~AliMUONTrackReconstructorK()
 {
 /// Destructor
 } 
 
   //__________________________________________________________________________
-void AliMUONTrackReconstructorK::MakeTrackCandidates()
+void AliMUONTrackReconstructorK::MakeTrackCandidates(AliMUONVClusterStore& clusterStore)
 {
-  /// To make track candidates:
+  /// To make track candidates (assuming linear propagation if the flag fgkMakeTrackCandidatesFast is set to kTRUE):
   /// Start with segments station(1..) 4 or 5 then follow track in station 5 or 4.
-  /// Good candidates are made of at least three hitForRec's.
+  /// Good candidates are made of at least three clusters if both station are requested (two otherwise).
   /// Keep only best candidates or all of them according to the flag fgkTrackAllTracks.
+  
   TClonesArray *segments;
   AliMUONTrack *track;
   Int_t iCandidate = 0;
+  Bool_t clusterFound;
 
   AliDebug(1,"Enter MakeTrackCandidates");
 
+  // Unless we're doing combined tracking, we'll clusterize all stations at once
+  Int_t firstChamber(0);
+  Int_t lastChamber(9);
+  
+  if (GetRecoParam()->CombineClusterTrackReco()) {
+    // ... Here's the exception : ask the clustering to reconstruct
+    // clusters *only* in station 4 and 5 for combined tracking
+    firstChamber = 6;
+  }
+  
+  for (Int_t i = firstChamber; i <= lastChamber; ++i ) 
+  {
+    if (fClusterServer && GetRecoParam()->UseChamber(i)) fClusterServer->Clusterize(i, clusterStore, AliMpArea());
+  }
+  
   // Loop over stations(1..) 5 and 4 and make track candidates
   for (Int_t istat=4; istat>=3; istat--) {
     
     // Make segments in the station
-    segments = MakeSegmentsInStation(istat);
+    segments = MakeSegmentsBetweenChambers(clusterStore, 2*istat, 2*istat+1);
     
     // Loop over segments
     for (Int_t iseg=0; iseg<segments->GetEntriesFast(); iseg++) {
       AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
       
       // Transform segments to tracks and put them at the end of fRecTracksPtr
-      track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack((AliMUONObjectPair*)((*segments)[iseg]));
+      track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack((AliMUONObjectPair*)((*segments)[iseg]),GetRecoParam()->GetBendingVertexDispersion());
       fNRecTracks++;
       
-      // Recompute track parameters and covariances on station(1..) 5 using Kalman filter
-      // (to make sure all tracks are treated in the same way)
-      if (istat == 4) RetraceTrack(*track,kFALSE);
-      
-      // Printout for debuging
-      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
-        cout<<endl<<"Track parameter covariances at first hit:"<<endl;
-        ((AliMUONTrackParam*) (track->GetTrackParamAtHit()->First()))->GetCovariances().Print();
+      // Look for compatible cluster(s) in the other station
+      if (GetRecoParam()->MakeTrackCandidatesFast())
+       clusterFound = FollowLinearTrackInStation(*track, clusterStore, 7-istat);
+      else {
+        // First recompute track parameters and covariances on station(1..) 5 using Kalman filter
+        // (to make sure all tracks are treated in the same way)
+        if (istat == 4) RetraceTrack(*track, kTRUE);
+       clusterFound = FollowTrackInStation(*track, clusterStore, 7-istat);
       }
       
-      // Look for compatible hitForRec(s) in the other station
-      // Remove track if no hit found
-      if (!FollowTrackInStation(*track,7-istat)) {
-        fRecTracksPtr->Remove(track);
-       fNRecTracks--;
+      // Remove track if no cluster found on a requested station
+      if (!clusterFound) {
+       if (GetRecoParam()->RequestStation(7-istat)) {
+          fRecTracksPtr->Remove(track);
+         fNRecTracks--;
+       } else if (istat == 3 && !GetRecoParam()->MakeTrackCandidatesFast()) {
+         // update track parameters and covariances using Kalman filter
+         RetraceTrack(*track, kTRUE);
+       }
       }
       
     }
@@ -112,26 +133,125 @@ void AliMUONTrackReconstructorK::MakeTrackCandidates()
     delete segments;
   }
   
-  fRecTracksPtr->Compress(); // this is essential before checking tracks
   
-  // Remove bad tracks
-  Int_t currentNRecTracks = fNRecTracks;
-  for (Int_t iRecTrack = 0; iRecTrack < currentNRecTracks; iRecTrack++) {
+  // Retrace tracks using Kalman filter and remove bad ones
+  if (GetRecoParam()->MakeTrackCandidatesFast()) {
+    fRecTracksPtr->Compress(); // this is essential before checking tracks
     
+    Int_t currentNRecTracks = fNRecTracks;
+    for (Int_t iRecTrack = 0; iRecTrack < currentNRecTracks; iRecTrack++) {
+      track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
+      
+      // Recompute track parameters and covariances using Kalman filter
+      RetraceTrack(*track,kTRUE);
+      
+      // Remove the track if the normalized chi2 is too high
+      if (track->GetNormalizedChi2() > GetRecoParam()->GetSigmaCutForTracking() *
+                                      GetRecoParam()->GetSigmaCutForTracking()) {
+        fRecTracksPtr->Remove(track);
+        fNRecTracks--;
+      }
+      
+    }
+    
+  }
+  
+  fRecTracksPtr->Compress(); // this is essential before checking tracks
+  
+  // Keep all different tracks or only the best ones as required
+  if (GetRecoParam()->TrackAllTracks()) RemoveIdenticalTracks();
+  else RemoveDoubleTracks();
+  
+  AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
+  
+}
+
+  //__________________________________________________________________________
+void AliMUONTrackReconstructorK::MakeMoreTrackCandidates(AliMUONVClusterStore& clusterStore)
+{
+  /// To make extra track candidates (assuming linear propagation if the flag fgkMakeTrackCandidatesFast is set to kTRUE):
+  /// clustering is supposed to be already done
+  /// Start with segments made of 1 cluster in each of the stations 4 and 5 then follow track in remaining chambers.
+  /// Good candidates are made of at least three clusters if both station are requested (two otherwise).
+  /// Keep only best candidates or all of them according to the flag fgkTrackAllTracks.
+  
+  TClonesArray *segments;
+  AliMUONTrack *track;
+  Int_t iCandidate = 0, iCurrentTrack, nCurrentTracks;
+  Int_t initialNRecTracks = fNRecTracks;
+  Bool_t clusterFound;
+  
+  AliDebug(1,"Enter MakeMoreTrackCandidates");
+  
+  // Double loop over chambers in stations(1..) 4 and 5 to make track candidates
+  for (Int_t ich1 = 6; ich1 <= 7; ich1++) {
+    for (Int_t ich2 = 8; ich2 <= 9; ich2++) {
+      
+      // Make segments between ch1 and ch2
+      segments = MakeSegmentsBetweenChambers(clusterStore, ich1, ich2);
+      
+      // Loop over segments
+      for (Int_t iseg=0; iseg<segments->GetEntriesFast(); iseg++) {
+       AliDebug(1,Form("Making primary candidate(1..) %d",++iCandidate));
+       
+       // Transform segments to tracks and put them at the end of fRecTracksPtr
+       iCurrentTrack = fRecTracksPtr->GetLast()+1;
+       track = new ((*fRecTracksPtr)[iCurrentTrack]) AliMUONTrack((AliMUONObjectPair*)((*segments)[iseg]),GetRecoParam()->GetBendingVertexDispersion());
+       fNRecTracks++;
+       
+       // Look for compatible cluster(s) in the second chamber of station 5
+       if (GetRecoParam()->MakeTrackCandidatesFast())
+         clusterFound = FollowLinearTrackInChamber(*track, clusterStore, 17-ich2);
+       else clusterFound = FollowTrackInChamber(*track, clusterStore, 17-ich2);
+       
+       // skip the original track in case it has been removed
+       if (GetRecoParam()->TrackAllTracks() && clusterFound) iCurrentTrack++;
+       
+       // loop over every new tracks
+       nCurrentTracks = fRecTracksPtr->GetLast()+1;
+       while (iCurrentTrack < nCurrentTracks) {
+         track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iCurrentTrack);
+         
+         // Look for compatible cluster(s) in the second chamber of station 4
+         if (GetRecoParam()->MakeTrackCandidatesFast())
+           FollowLinearTrackInChamber(*track, clusterStore, 13-ich1);
+         else FollowTrackInChamber(*track, clusterStore, 13-ich1);
+         
+         iCurrentTrack++;
+       }
+       
+      }
+      
+      // delete the array of segments
+      delete segments;
+    }
+  }
+  
+  fRecTracksPtr->Compress(); // this is essential before checking tracks
+  
+  // Retrace tracks using Kalman filter
+  for (Int_t iRecTrack = initialNRecTracks; iRecTrack < fNRecTracks; iRecTrack++) {
     track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
     
+    // Recompute track parameters and covariances using Kalman filter
+    RetraceTrack(*track,kTRUE);
+    
     // Remove the track if the normalized chi2 is too high
-    if (track->GetNormalizedChi2() > fgkSigmaToCutForTracking * fgkSigmaToCutForTracking) {
+    // (only if the tracking has been done without magnetic field)
+    if (GetRecoParam()->MakeTrackCandidatesFast() &&
+       track->GetNormalizedChi2() > GetRecoParam()->GetSigmaCutForTracking() *
+                                    GetRecoParam()->GetSigmaCutForTracking()) {
       fRecTracksPtr->Remove(track);
       fNRecTracks--;
     }
     
   }
   
-  fRecTracksPtr->Compress(); // this is essential before checking tracks
+  // this is essential before checking tracks
+  if (GetRecoParam()->MakeTrackCandidatesFast()) fRecTracksPtr->Compress();
   
   // Keep all different tracks or only the best ones as required
-  if (fgkTrackAllTracks) RemoveIdenticalTracks();
+  if (GetRecoParam()->TrackAllTracks()) RemoveIdenticalTracks();
   else RemoveDoubleTracks();
   
   AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
@@ -141,124 +261,152 @@ void AliMUONTrackReconstructorK::MakeTrackCandidates()
   //__________________________________________________________________________
 void AliMUONTrackReconstructorK::RetraceTrack(AliMUONTrack &trackCandidate, Bool_t resetSeed)
 {
-  /// Re-run the kalman filter from the most downstream hit to the most uptream one
+  /// Re-run the kalman filter from the most downstream cluster to the most uptream one
   AliDebug(1,"Enter RetraceTrack");
   
-  AliMUONTrackParam* startingTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtHit()->Last();
+  AliMUONTrackParam* lastTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Last();
   
-  // Reset the "seed" (= track parameters and their covariances at last hit) if required
+  // Reset the "seed" (= track parameters and their covariances at last cluster) if required
   if (resetSeed) {
-    // => Shift track parameters at the position of the last hit
-    AliMUONHitForRec* hitForRecAtHit = startingTrackParam->GetHitForRecPtr();
-    startingTrackParam->SetNonBendingCoor(hitForRecAtHit->GetNonBendingCoor());
-    startingTrackParam->SetBendingCoor(hitForRecAtHit->GetBendingCoor());
-    
-    // => Re-compute and reset track parameters covariances at last hit (as if the other hits did not exist)
-    const TMatrixD& kParamCov = startingTrackParam->GetCovariances();
-    TMatrixD newParamCov(5,5);
-    newParamCov.Zero();
+    
+    // parameters at last cluster
+    AliMUONVCluster* cluster2 = lastTrackParam->GetClusterPtr();
+    Double_t x2 = cluster2->GetX();
+    Double_t y2 = cluster2->GetY();
+    Double_t z2 = cluster2->GetZ();
+    
+    // parameters at last but one cluster
+    AliMUONTrackParam* previousTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Before(lastTrackParam);
+    AliMUONVCluster* cluster1 = previousTrackParam->GetClusterPtr();
+    // make sure it is on the previous chamber (can have 2 clusters in the same chamber after "ComplementTrack")
+    if (cluster2->GetChamberId() == cluster1->GetChamberId()) {
+      previousTrackParam = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Before(previousTrackParam);
+      cluster1 = previousTrackParam->GetClusterPtr();
+    }
+    Double_t x1 = cluster1->GetX();
+    Double_t y1 = cluster1->GetY();
+    Double_t z1 = cluster1->GetZ();
+    
+    // reset track parameters
+    Double_t dZ = z1 - z2;
+    lastTrackParam->SetNonBendingCoor(x2);
+    lastTrackParam->SetBendingCoor(y2);
+    lastTrackParam->SetZ(z2);
+    lastTrackParam->SetNonBendingSlope((x1 - x2) / dZ);
+    lastTrackParam->SetBendingSlope((y1 - y2) / dZ);
+    Double_t bendingImpact = y2 - z2 * lastTrackParam->GetBendingSlope();
+    Double_t inverseBendingMomentum = 1. / AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(bendingImpact);
+    lastTrackParam->SetInverseBendingMomentum(inverseBendingMomentum);
+    
+    // => Reset track parameter covariances at last cluster (as if the other clusters did not exist)
+    TMatrixD lastParamCov(5,5);
+    lastParamCov.Zero();
     // Non bending plane
-    newParamCov(0,0) = hitForRecAtHit->GetNonBendingReso2();
-    newParamCov(1,1) = 100.*kParamCov(1,1);
+    lastParamCov(0,0) = cluster2->GetErrX2();
+    lastParamCov(1,1) = 100. * ( cluster1->GetErrX2() + cluster2->GetErrX2() ) / dZ / dZ;
     // Bending plane
-    newParamCov(2,2) = hitForRecAtHit->GetBendingReso2();
-    newParamCov(3,3) = 100.*kParamCov(3,3);
-    // Inverse bending momentum
-    newParamCov(4,4) = 0.5*startingTrackParam->GetInverseBendingMomentum() * 0.5*startingTrackParam->GetInverseBendingMomentum();
-    startingTrackParam->SetCovariances(newParamCov);
+    lastParamCov(2,2) = cluster2->GetErrY2();
+    lastParamCov(3,3) = 100. * ( cluster1->GetErrY2() + cluster2->GetErrY2() ) / dZ / dZ;
+    // Inverse bending momentum (vertex resolution + bending slope resolution + 10% error on dipole parameters+field)
+    lastParamCov(4,4) = ((GetRecoParam()->GetBendingVertexDispersion() *
+                         GetRecoParam()->GetBendingVertexDispersion() +
+                         (z1 * z1 * cluster2->GetErrY2() + z2 * z2 * cluster1->GetErrY2()) / dZ / dZ) /
+                        bendingImpact / bendingImpact + 0.1 * 0.1) * inverseBendingMomentum * inverseBendingMomentum;
+    lastTrackParam->SetCovariances(lastParamCov);
     
     // Reset the track chi2
-    startingTrackParam->SetTrackChi2(0.);
+    lastTrackParam->SetTrackChi2(0.);
   
   }
   
   // Redo the tracking
-  RetracePartialTrack(trackCandidate, startingTrackParam);
+  RetracePartialTrack(trackCandidate, lastTrackParam);
   
 }
 
   //__________________________________________________________________________
 void AliMUONTrackReconstructorK::RetracePartialTrack(AliMUONTrack &trackCandidate, const AliMUONTrackParam* startingTrackParam)
 {
-  /// Re-run the kalman filter from the hit attached to startingTrackParam to the most uptream hit
+  /// Re-run the kalman filter from the cluster attached to startingTrackParam to the most uptream cluster
   AliDebug(1,"Enter RetracePartialTrack");
   
   // Printout for debuging
   if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-    cout << "RetracePartialTrack: track chi2 before re-tracking: " << trackCandidate.GetFitFMin() << endl;
+    cout << "RetracePartialTrack: track chi2 before re-tracking: " << trackCandidate.GetGlobalChi2() << endl;
   }
   
   // Reset the track chi2
-  trackCandidate.SetFitFMin(startingTrackParam->GetTrackChi2());
+  trackCandidate.SetGlobalChi2(startingTrackParam->GetTrackChi2());
   
-  // loop over attached hits until the first one and recompute track parameters and covariances using kalman filter
-  Int_t expectedChamber = startingTrackParam->GetHitForRecPtr()->GetChamberNumber() - 1;
+  // loop over attached clusters until the first one and recompute track parameters and covariances using kalman filter
+  Int_t expectedChamber = startingTrackParam->GetClusterPtr()->GetChamberId() - 1;
   Int_t currentChamber;
-  Double_t addChi2TrackAtHit;
-  AliMUONTrackParam* trackParamAtHit = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtHit()->Before(startingTrackParam); 
-  while (trackParamAtHit) {
+  Double_t addChi2TrackAtCluster;
+  AliMUONTrackParam* trackParamAtCluster = (AliMUONTrackParam*) trackCandidate.GetTrackParamAtCluster()->Before(startingTrackParam); 
+  while (trackParamAtCluster) {
     
     // reset track parameters and their covariances
-    trackParamAtHit->SetParameters(startingTrackParam->GetParameters());
-    trackParamAtHit->SetZ(startingTrackParam->GetZ());
-    trackParamAtHit->SetCovariances(startingTrackParam->GetCovariances());
+    trackParamAtCluster->SetParameters(startingTrackParam->GetParameters());
+    trackParamAtCluster->SetZ(startingTrackParam->GetZ());
+    trackParamAtCluster->SetCovariances(startingTrackParam->GetCovariances());
     
     // add MCS effect
-    AliMUONTrackExtrap::AddMCSEffect(trackParamAtHit,AliMUONConstants::ChamberThicknessInX0(),1.);
+    AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(),1.);
     
     // reset propagator for smoother
-    if (fgkRunSmoother) trackParamAtHit->ResetPropagator();
+    if (GetRecoParam()->UseSmoother()) trackParamAtCluster->ResetPropagator();
     
-    // add MCS in missing chambers if any (at most 2 chambers can be missing according to tracking criteria)
-    currentChamber = trackParamAtHit->GetHitForRecPtr()->GetChamberNumber();
+    // add MCS in missing chambers if any
+    currentChamber = trackParamAtCluster->GetClusterPtr()->GetChamberId();
     while (currentChamber < expectedChamber) {
       // extrapolation to the missing chamber (update the propagator)
-      AliMUONTrackExtrap::ExtrapToZCov(trackParamAtHit, AliMUONConstants::DefaultChamberZ(expectedChamber), fgkRunSmoother);
+      AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, AliMUONConstants::DefaultChamberZ(expectedChamber),
+                                      GetRecoParam()->UseSmoother());
       // add MCS effect
-      AliMUONTrackExtrap::AddMCSEffect(trackParamAtHit,AliMUONConstants::ChamberThicknessInX0(),1.);
+      AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(),1.);
       expectedChamber--;
     }
     
-    // extrapolation to the plane of the hitForRec attached to the current trackParamAtHit (update the propagator)
-    AliMUONTrackExtrap::ExtrapToZCov(trackParamAtHit, trackParamAtHit->GetHitForRecPtr()->GetZ(), fgkRunSmoother);
+    // extrapolation to the plane of the cluster attached to the current trackParamAtCluster (update the propagator)
+    AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, trackParamAtCluster->GetClusterPtr()->GetZ(),
+                                    GetRecoParam()->UseSmoother());
     
-    if (fgkRunSmoother) {
+    if (GetRecoParam()->UseSmoother()) {
       // save extrapolated parameters for smoother
-      trackParamAtHit->SetExtrapParameters(trackParamAtHit->GetParameters());
+      trackParamAtCluster->SetExtrapParameters(trackParamAtCluster->GetParameters());
       
       // save extrapolated covariance matrix for smoother
-      trackParamAtHit->SetExtrapCovariances(trackParamAtHit->GetCovariances());
+      trackParamAtCluster->SetExtrapCovariances(trackParamAtCluster->GetCovariances());
     }
     
-    // Compute new track parameters including "hitForRecCh2" using kalman filter
-    addChi2TrackAtHit = RunKalmanFilter(*trackParamAtHit);
+    // Compute new track parameters using kalman filter
+    addChi2TrackAtCluster = RunKalmanFilter(*trackParamAtCluster);
     
     // Update the track chi2
-    trackCandidate.SetFitFMin(trackCandidate.GetFitFMin() + addChi2TrackAtHit);
-    trackParamAtHit->SetTrackChi2(trackCandidate.GetFitFMin());
+    trackCandidate.SetGlobalChi2(trackCandidate.GetGlobalChi2() + addChi2TrackAtCluster);
+    trackParamAtCluster->SetTrackChi2(trackCandidate.GetGlobalChi2());
     
-    // prepare next step, add MCS effects in parameter covariances
-    expectedChamber--;
-    startingTrackParam = trackParamAtHit;
-    trackParamAtHit = (AliMUONTrackParam*) (trackCandidate.GetTrackParamAtHit()->Before(startingTrackParam)); 
+    // prepare next step
+    expectedChamber = currentChamber - 1;
+    startingTrackParam = trackParamAtCluster;
+    trackParamAtCluster = (AliMUONTrackParam*) (trackCandidate.GetTrackParamAtCluster()->Before(startingTrackParam)); 
   }
   
   // Printout for debuging
   if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-    cout << "RetracePartialTrack: track chi2 after re-tracking: " << trackCandidate.GetFitFMin() << endl;
+    cout << "RetracePartialTrack: track chi2 after re-tracking: " << trackCandidate.GetGlobalChi2() << endl;
   }
   
 }
 
   //__________________________________________________________________________
-void AliMUONTrackReconstructorK::FollowTracks()
+void AliMUONTrackReconstructorK::FollowTracks(AliMUONVClusterStore& clusterStore)
 {
   /// Follow tracks in stations(1..) 3, 2 and 1
   AliDebug(1,"Enter FollowTracks");
   
   AliMUONTrack *track;
   Int_t currentNRecTracks;
-  Bool_t hitFound;
   
   for (Int_t station = 2; station >= 0; station--) {
     
@@ -272,60 +420,217 @@ void AliMUONTrackReconstructorK::FollowTracks()
       
       track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
       
+      // Look for compatible cluster(s) in station(0..) "station"
+      if (!FollowTrackInStation(*track, clusterStore, station)) {
+       
+       // Try to recover track if required
+       if (GetRecoParam()->RecoverTracks()) {
+         
+         // work on a copy of the track if this station is not required
+         // to keep the case where no cluster is reconstructed as a possible candidate
+         if (!GetRecoParam()->RequestStation(station)) {
+           track = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(*track);
+           fNRecTracks++;
+         }
+         
+         // try to recover
+         if (!RecoverTrack(*track, clusterStore, station)) {
+           // remove track if no cluster found
+           fRecTracksPtr->Remove(track);
+           fNRecTracks--;
+         }
+         
+       } else if (GetRecoParam()->RequestStation(station)) {
+         // remove track if no cluster found
+         fRecTracksPtr->Remove(track);
+         fNRecTracks--;
+       } 
+       
+      }
+      
+    }
+    
+    fRecTracksPtr->Compress(); // this is essential before checking tracks
+    
+    // Keep only the best tracks if required
+    if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
+    
+  }
+  
+}
+
+  //__________________________________________________________________________
+Bool_t AliMUONTrackReconstructorK::FollowTrackInChamber(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextChamber)
+{
+  /// Follow trackCandidate in chamber(0..) nextChamber and search for compatible cluster(s)
+  /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
+  /// kTRUE:  duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
+  ///         fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
+  ///         Remove the obsolete "trackCandidate" at the end.
+  /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
+  /// return kTRUE if new cluster(s) have been found (otherwise return kFALSE)
+  AliDebug(1,Form("Enter FollowTrackInChamber(1..) %d", nextChamber+1));
+  
+  Double_t bendingMomentum;
+  Double_t chi2OfCluster;
+  Double_t maxChi2OfCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
+                                  GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
+  Double_t addChi2TrackAtCluster;
+  Double_t bestAddChi2TrackAtCluster = 1.e10;
+  Bool_t foundOneCluster = kFALSE;
+  AliMUONTrack *newTrack = 0x0;
+  AliMUONVCluster *cluster;
+  AliMUONTrackParam extrapTrackParamAtCh;
+  AliMUONTrackParam extrapTrackParamAtCluster;
+  AliMUONTrackParam bestTrackParamAtCluster;
+  
+  // Get track parameters according to the propagation direction
+  if (nextChamber > 7) extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
+  else extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
+  
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
+    cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
+    extrapTrackParamAtCh.GetParameters().Print();
+    extrapTrackParamAtCh.GetCovariances().Print();
+  }
+  
+  // Add MCS effect
+  AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
+  
+  // reset propagator for smoother
+  if (GetRecoParam()->UseSmoother()) extrapTrackParamAtCh.ResetPropagator();
+  
+  // Add MCS in the missing chamber(s) if any
+  Int_t currentChamber = extrapTrackParamAtCh.GetClusterPtr()->GetChamberId();
+  while (currentChamber > nextChamber + 1) {
+    // extrapolation to the missing chamber
+    currentChamber--;
+    AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(currentChamber),
+                                    GetRecoParam()->UseSmoother());
+    // add MCS effect
+    AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
+  }
+  
+  //Extrapolate trackCandidate to chamber
+  AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(nextChamber),
+                                  GetRecoParam()->UseSmoother());
+  
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
+    cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(nextChamber)<<":"<<endl;
+    extrapTrackParamAtCh.GetParameters().Print();
+    extrapTrackParamAtCh.GetCovariances().Print();
+  }
+  
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+    cout << "FollowTrackInChamber: look for clusters in chamber(1..): " << nextChamber+1 << endl;
+  }
+  
+  // Ask the clustering to reconstruct new clusters around the track position in the current chamber
+  // except for station 4 and 5 that are already entirely clusterized
+  if (GetRecoParam()->CombineClusterTrackReco()) {
+    if (nextChamber < 6) AskForNewClustersInChamber(extrapTrackParamAtCh, clusterStore, nextChamber);
+  }
+  
+  // Create iterators to loop over clusters in both chambers
+  TIter next(clusterStore.CreateChamberIterator(nextChamber,nextChamber));
+  
+  // look for candidates in chamber
+  while ( ( cluster = static_cast<AliMUONVCluster*>(next()) ) ) {
+    
+    // try to add the current cluster fast
+    if (!TryOneClusterFast(extrapTrackParamAtCh, cluster)) continue;
+    
+    // try to add the current cluster accuratly
+    chi2OfCluster = TryOneCluster(extrapTrackParamAtCh, cluster, extrapTrackParamAtCluster,
+                                 GetRecoParam()->UseSmoother());
+    
+    // if good chi2 then consider to add cluster
+    if (chi2OfCluster < maxChi2OfCluster) {
+      foundOneCluster = kTRUE;
+      
       // Printout for debuging
-      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
-        cout<<endl<<"Track parameter covariances at first hit:"<<endl;
-        ((AliMUONTrackParam*) (track->GetTrackParamAtHit()->First()))->GetCovariances().Print();
+      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+       cout << "FollowTrackInChamber: found one cluster in chamber(1..): " << nextChamber+1
+       << " (Chi2 = " << chi2OfCluster << ")" << endl;
+       cluster->Print();
       }
       
-      // Look for compatible hitForRec in station(0..) "station"
-      hitFound = FollowTrackInStation(*track,station);
+      if (GetRecoParam()->UseSmoother()) {
+       // save extrapolated parameters for smoother
+       extrapTrackParamAtCluster.SetExtrapParameters(extrapTrackParamAtCluster.GetParameters());
+       
+       // save extrapolated covariance matrix for smoother
+       extrapTrackParamAtCluster.SetExtrapCovariances(extrapTrackParamAtCluster.GetCovariances());
+      }
       
-      // Try to recover track if required
-      if (!hitFound && fgkRecoverTracks) hitFound = RecoverTrack(*track,station);
+      // Compute new track parameters including new cluster using kalman filter
+      addChi2TrackAtCluster = RunKalmanFilter(extrapTrackParamAtCluster);
       
-      // remove track if no hit found
-      if (!hitFound) {
-       fRecTracksPtr->Remove(track);
-       fNRecTracks--;
+      // skip track with absolute bending momentum out of limits
+      bendingMomentum = TMath::Abs(1. / extrapTrackParamAtCluster.GetInverseBendingMomentum());
+      if (bendingMomentum < GetRecoParam()->GetMinBendingMomentum() ||
+         bendingMomentum > GetRecoParam()->GetMaxBendingMomentum()) continue;
+      
+      if (GetRecoParam()->TrackAllTracks()) {
+       // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
+       newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
+       UpdateTrack(*newTrack,extrapTrackParamAtCluster,addChi2TrackAtCluster);
+       fNRecTracks++;
+       
+       // Printout for debuging
+       if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+         cout << "FollowTrackInChamber: added one cluster in chamber(1..): " << nextChamber+1 << endl;
+         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
+       }
+       
+      } else if (addChi2TrackAtCluster < bestAddChi2TrackAtCluster) {
+       // keep track of the best cluster
+       bestAddChi2TrackAtCluster = addChi2TrackAtCluster;
+       bestTrackParamAtCluster = extrapTrackParamAtCluster;
       }
       
     }
     
-    fRecTracksPtr->Compress(); // this is essential before checking tracks
-    
-    // Remove bad tracks
-    Int_t currentNRecTracks = fNRecTracks;
-    for (Int_t iRecTrack = 0; iRecTrack < currentNRecTracks; iRecTrack++) {
-      track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iRecTrack);
+  }
+  
+  // fill out the best track if required else clean up the fRecTracksPtr array
+  if (!GetRecoParam()->TrackAllTracks()) {
+    if (foundOneCluster) {
+      UpdateTrack(trackCandidate,bestTrackParamAtCluster,bestAddChi2TrackAtCluster);
       
-      // Remove the track if the normalized chi2 is too high
-      if (track->GetNormalizedChi2() > fgkSigmaToCutForTracking * fgkSigmaToCutForTracking) {
-       fRecTracksPtr->Remove(track);
-       fNRecTracks--;
+      // Printout for debuging
+      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+        cout << "FollowTrackInChamber: added the best cluster in chamber(1..): " << bestTrackParamAtCluster.GetClusterPtr()->GetChamberId()+1 << endl;
+        if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
       }
       
-    }
+    } else return kFALSE;
     
-    fRecTracksPtr->Compress(); // this is essential before checking tracks
+  } else if (foundOneCluster) {
     
-    // Keep only the best tracks if required
-    if (!fgkTrackAllTracks) RemoveDoubleTracks();
+    // remove obsolete track
+    fRecTracksPtr->Remove(&trackCandidate);
+    fNRecTracks--;
     
-  }
+  } else return kFALSE;
+  
+  return kTRUE;
   
 }
 
   //__________________________________________________________________________
-Bool_t AliMUONTrackReconstructorK::FollowTrackInStation(AliMUONTrack &trackCandidate, Int_t nextStation)
+Bool_t AliMUONTrackReconstructorK::FollowTrackInStation(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextStation)
 {
-  /// Follow trackCandidate in station(0..) nextStation and search for compatible HitForRec(s)
+  /// Follow trackCandidate in station(0..) nextStation and search for compatible cluster(s)
   /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
   /// kTRUE:  duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
   ///         fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
   ///         Remove the obsolete "trackCandidate" at the end.
-  /// kFALSE: add only the best hit(s) to the "trackCandidate". Try to add a couple of hits in priority.
-  /// return kTRUE if new hits have been found (otherwise return kFALSE)
+  /// kFALSE: add only the best cluster(s) to the "trackCandidate". Try to add a couple of clusters in priority.
+  /// return kTRUE if new cluster(s) have been found (otherwise return kFALSE)
   AliDebug(1,Form("Enter FollowTrackInStation(1..) %d", nextStation+1));
   
   // Order the chamber according to the propagation direction (tracking starts with chamber 2):
@@ -340,163 +645,218 @@ Bool_t AliMUONTrackReconstructorK::FollowTrackInStation(AliMUONTrack &trackCandi
     ch2 = 2*nextStation+1;
   }
   
-  Double_t zCh2 = AliMUONConstants::DefaultChamberZ(ch2);
-  Double_t chi2OfHitForRec;
-  Double_t maxChi2OfHitForRec = 2. * fgkSigmaToCutForTracking * fgkSigmaToCutForTracking; // 2 because 2 quantities in chi2
-  Double_t addChi2TrackAtHit1;
-  Double_t addChi2TrackAtHit2;
-  Double_t bestAddChi2TrackAtHit1 = 1.e10;
-  Double_t bestAddChi2TrackAtHit2 = 1.e10;
-  Bool_t foundOneHit = kFALSE;
-  Bool_t foundTwoHits = kFALSE;
+  Double_t bendingMomentum;
+  Double_t chi2OfCluster;
+  Double_t maxChi2OfCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
+                                  GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
+  Double_t addChi2TrackAtCluster1;
+  Double_t addChi2TrackAtCluster2;
+  Double_t bestAddChi2TrackAtCluster1 = 1.e10;
+  Double_t bestAddChi2TrackAtCluster2 = 1.e10;
+  Bool_t foundOneCluster = kFALSE;
+  Bool_t foundTwoClusters = kFALSE;
   AliMUONTrack *newTrack = 0x0;
-  AliMUONHitForRec *hitForRecCh1, *hitForRecCh2;
+  AliMUONVCluster *clusterCh1, *clusterCh2;
   AliMUONTrackParam extrapTrackParam;
-  AliMUONTrackParam extrapTrackParamAtHit1;
-  AliMUONTrackParam extrapTrackParamAtHit2;
-  AliMUONTrackParam bestTrackParamAtHit1;
-  AliMUONTrackParam bestTrackParamAtHit2;
-  Bool_t *hitForRecCh1Used = new Bool_t[fNHitsForRecPerChamber[ch1]];
-  for (Int_t hit1 = 0; hit1 < fNHitsForRecPerChamber[ch1]; hit1++) hitForRecCh1Used[hit1] = kFALSE;
+  AliMUONTrackParam extrapTrackParamAtCh;
+  AliMUONTrackParam extrapTrackParamAtCluster1;
+  AliMUONTrackParam extrapTrackParamAtCluster2;
+  AliMUONTrackParam bestTrackParamAtCluster1;
+  AliMUONTrackParam bestTrackParamAtCluster2;
+  
+  Int_t nClusters = clusterStore.GetSize();
+  Bool_t *clusterCh1Used = new Bool_t[nClusters];
+  for (Int_t i = 0; i < nClusters; i++) clusterCh1Used[i] = kFALSE;
+  Int_t iCluster1;
+  
+  // Get track parameters according to the propagation direction
   
-  // Get track parameters and extrapolate them to chamber "ch2" to save computing time in the next steps
-  AliMUONTrackParam extrapTrackParamAtCh2(*(AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->First());
+  if (nextStation==4) extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
+  else extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
+  
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
+    cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
+    extrapTrackParamAtCh.GetParameters().Print();
+    extrapTrackParamAtCh.GetCovariances().Print();
+  }
   
   // Add MCS effect
-  AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh2,AliMUONConstants::ChamberThicknessInX0(),1.);
+  AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
   
   // reset propagator for smoother
-  if (fgkRunSmoother) extrapTrackParamAtCh2.ResetPropagator();
+  if (GetRecoParam()->UseSmoother()) extrapTrackParamAtCh.ResetPropagator();
   
-  // Add MCS in the missing chamber if any (only 1 chamber can be missing according to tracking criteria)
-  if (ch1 < ch2 && extrapTrackParamAtCh2.GetHitForRecPtr()->GetChamberNumber() > ch2 + 1) {
+  // Add MCS in the missing chamber(s) if any
+  Int_t currentChamber = extrapTrackParamAtCh.GetClusterPtr()->GetChamberId();
+  while (ch1 < ch2 && currentChamber > ch2 + 1) {
     // extrapolation to the missing chamber
-    AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh2, AliMUONConstants::DefaultChamberZ(ch2 + 1), fgkRunSmoother);
+    currentChamber--;
+    AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(currentChamber),
+                                    GetRecoParam()->UseSmoother());
     // add MCS effect
-    AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh2,AliMUONConstants::ChamberThicknessInX0(),1.);
+    AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
   }
   
   //Extrapolate trackCandidate to chamber "ch2"
-  AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh2, zCh2, fgkRunSmoother);
+  AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch2),
+                                  GetRecoParam()->UseSmoother());
   
   // Printout for debuging
   if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
-    cout<<endl<<"Track parameter covariances at first hit extrapolated to z = "<<zCh2<<":"<<endl;
-    extrapTrackParamAtCh2.GetCovariances().Print();
+    cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch2)<<":"<<endl;
+    extrapTrackParamAtCh.GetParameters().Print();
+    extrapTrackParamAtCh.GetCovariances().Print();
   }
   
   // Printout for debuging
   if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-    cout << "FollowTrackInStation: look for hits in chamber(1..): " << ch2+1 << endl;
+    cout << "FollowTrackInStation: look for clusters in chamber(1..): " << ch2+1 << endl;
   }
   
-  // look for candidates in chamber 2 
-  for (Int_t hit2 = 0; hit2 < fNHitsForRecPerChamber[ch2]; hit2++) {
+  // Ask the clustering to reconstruct new clusters around the track position in the current station
+  // except for station 4 and 5 that are already entirely clusterized
+  if (GetRecoParam()->CombineClusterTrackReco()) {
+    if (nextStation < 3) AskForNewClustersInStation(extrapTrackParamAtCh, clusterStore, nextStation);
+  }
+  
+  // Create iterators to loop over clusters in both chambers
+  TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
+  TIter nextInCh2(clusterStore.CreateChamberIterator(ch2,ch2));
+  
+  // look for candidates in chamber 2
+  while ( ( clusterCh2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
     
-    hitForRecCh2 = (AliMUONHitForRec*) fHitsForRecPtr->UncheckedAt(fIndexOfFirstHitForRecPerChamber[ch2]+hit2);
+    // try to add the current cluster fast
+    if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh2)) continue;
     
-    // try to add the current hit
-    chi2OfHitForRec = TryOneHitForRec(extrapTrackParamAtCh2, hitForRecCh2, extrapTrackParamAtHit2, fgkRunSmoother);
+    // try to add the current cluster accuratly
+    chi2OfCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh2, extrapTrackParamAtCluster2,
+                                 GetRecoParam()->UseSmoother());
     
-    // if good chi2 then try to attach a hitForRec in the other chamber too
-    if (chi2OfHitForRec < maxChi2OfHitForRec) {
-      Bool_t foundSecondHit = kFALSE;
+    // if good chi2 then try to attach a cluster in the other chamber too
+    if (chi2OfCluster < maxChi2OfCluster) {
+      Bool_t foundSecondCluster = kFALSE;
       
       // Printout for debuging
       if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-        cout << "FollowTrackInStation: found one hit in chamber(1..): " << ch2+1
-            << " (Chi2 = " << chi2OfHitForRec << ")" << endl;
-        cout << "                      look for second hits in chamber(1..): " << ch1+1 << " ..." << endl;
+        cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch2+1
+            << " (Chi2 = " << chi2OfCluster << ")" << endl;
+       clusterCh2->Print();
+        cout << "                      look for second clusters in chamber(1..): " << ch1+1 << " ..." << endl;
       }
       
-      if (fgkRunSmoother) {
+      if (GetRecoParam()->UseSmoother()) {
         // save extrapolated parameters for smoother
-        extrapTrackParamAtHit2.SetExtrapParameters(extrapTrackParamAtHit2.GetParameters());
+        extrapTrackParamAtCluster2.SetExtrapParameters(extrapTrackParamAtCluster2.GetParameters());
         
         // save extrapolated covariance matrix for smoother
-        extrapTrackParamAtHit2.SetExtrapCovariances(extrapTrackParamAtHit2.GetCovariances());
+        extrapTrackParamAtCluster2.SetExtrapCovariances(extrapTrackParamAtCluster2.GetCovariances());
       }
       
-      // Compute new track parameters including "hitForRecCh2" using kalman filter
-      addChi2TrackAtHit2 = RunKalmanFilter(extrapTrackParamAtHit2);
+      // Compute new track parameters including "clusterCh2" using kalman filter
+      addChi2TrackAtCluster2 = RunKalmanFilter(extrapTrackParamAtCluster2);
       
+      // skip track with absolute bending momentum out of limits
+      bendingMomentum = TMath::Abs(1. / extrapTrackParamAtCluster2.GetInverseBendingMomentum());
+      if (bendingMomentum < GetRecoParam()->GetMinBendingMomentum() ||
+         bendingMomentum > GetRecoParam()->GetMaxBendingMomentum()) continue;
+       
       // copy new track parameters for next step
-      extrapTrackParam = extrapTrackParamAtHit2;
+      extrapTrackParam = extrapTrackParamAtCluster2;
       
       // add MCS effect
       AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
       
       // reset propagator for smoother
-      if (fgkRunSmoother) extrapTrackParam.ResetPropagator();
+      if (GetRecoParam()->UseSmoother()) extrapTrackParam.ResetPropagator();
       
-      for (Int_t hit1 = 0; hit1 < fNHitsForRecPerChamber[ch1]; hit1++) {
-        
-       hitForRecCh1 = (AliMUONHitForRec*) fHitsForRecPtr->UncheckedAt(fIndexOfFirstHitForRecPerChamber[ch1]+hit1);
+      //Extrapolate track parameters to chamber "ch1"
+      AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(ch1),
+                                      GetRecoParam()->UseSmoother());
+      
+      // reset cluster iterator of chamber 1
+      nextInCh1.Reset();
+      iCluster1 = -1;
+      
+      // look for second candidates in chamber 1
+      while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
+       iCluster1++;
        
-        // try to add the current hit
-       chi2OfHitForRec = TryOneHitForRec(extrapTrackParam, hitForRecCh1, extrapTrackParamAtHit1, fgkRunSmoother);
-        
-       // if good chi2 then consider to add the 2 hitForRec to the "trackCandidate"
-       if (chi2OfHitForRec < maxChi2OfHitForRec) {
-         foundSecondHit = kTRUE;
-         foundTwoHits = kTRUE;
+       // try to add the current cluster fast
+       if (!TryOneClusterFast(extrapTrackParam, clusterCh1)) continue;
+       
+       // try to add the current cluster accuratly
+       chi2OfCluster = TryOneCluster(extrapTrackParam, clusterCh1, extrapTrackParamAtCluster1,
+                                     GetRecoParam()->UseSmoother());
+       
+       // if good chi2 then consider to add the 2 clusters to the "trackCandidate"
+       if (chi2OfCluster < maxChi2OfCluster) {
+         foundSecondCluster = kTRUE;
+         foundTwoClusters = kTRUE;
           
          // Printout for debuging
          if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-           cout << "FollowTrackInStation: found one hit in chamber(1..): " << ch1+1
-                << " (Chi2 = " << chi2OfHitForRec << ")" << endl;
+           cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch1+1
+                << " (Chi2 = " << chi2OfCluster << ")" << endl;
+           clusterCh1->Print();
          }
-         
-          if (fgkRunSmoother) {
+          
+          if (GetRecoParam()->UseSmoother()) {
             // save extrapolated parameters for smoother
-            extrapTrackParamAtHit1.SetExtrapParameters(extrapTrackParamAtHit1.GetParameters());
+            extrapTrackParamAtCluster1.SetExtrapParameters(extrapTrackParamAtCluster1.GetParameters());
             
             // save extrapolated covariance matrix for smoother
-            extrapTrackParamAtHit1.SetExtrapCovariances(extrapTrackParamAtHit1.GetCovariances());
+            extrapTrackParamAtCluster1.SetExtrapCovariances(extrapTrackParamAtCluster1.GetCovariances());
           }
           
-          // Compute new track parameters including "hitForRecCh1" using kalman filter
-          addChi2TrackAtHit1 = RunKalmanFilter(extrapTrackParamAtHit1);
+          // Compute new track parameters including "clusterCh1" using kalman filter
+          addChi2TrackAtCluster1 = RunKalmanFilter(extrapTrackParamAtCluster1);
           
-         if (fgkTrackAllTracks) {
-           // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new hitForRec's
+         // skip track with absolute bending momentum out of limits
+         bendingMomentum = TMath::Abs(1. / extrapTrackParamAtCluster1.GetInverseBendingMomentum());
+         if (bendingMomentum < GetRecoParam()->GetMinBendingMomentum() ||
+             bendingMomentum > GetRecoParam()->GetMaxBendingMomentum()) continue;
+         
+         if (GetRecoParam()->TrackAllTracks()) {
+           // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new clusters
             newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
-           UpdateTrack(*newTrack,extrapTrackParamAtHit1,extrapTrackParamAtHit2,addChi2TrackAtHit1,addChi2TrackAtHit2);
+           UpdateTrack(*newTrack,extrapTrackParamAtCluster1,extrapTrackParamAtCluster2,addChi2TrackAtCluster1,addChi2TrackAtCluster2);
            fNRecTracks++;
            
            // if we are arrived on station(1..) 5, recompute track parameters and covariances starting from this station
            // (going in the right direction)
            if (nextStation == 4) RetraceTrack(*newTrack,kTRUE);
            
-           // Tag hitForRecCh1 as used
-           hitForRecCh1Used[hit1] = kTRUE;
+           // Tag clusterCh1 as used
+           clusterCh1Used[iCluster1] = kTRUE;
            
            // Printout for debuging
            if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-             cout << "FollowTrackInStation: added two hits in station(1..): " << nextStation+1 << endl;
+             cout << "FollowTrackInStation: added two clusters in station(1..): " << nextStation+1 << endl;
              if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
            }
            
-          } else if (addChi2TrackAtHit1+addChi2TrackAtHit2 < bestAddChi2TrackAtHit1+bestAddChi2TrackAtHit2) {
-           // keep track of the best couple of hits
-           bestAddChi2TrackAtHit1 = addChi2TrackAtHit1;
-           bestAddChi2TrackAtHit2 = addChi2TrackAtHit2;
-           bestTrackParamAtHit1 = extrapTrackParamAtHit1;
-           bestTrackParamAtHit2 = extrapTrackParamAtHit2;
+          } else if (addChi2TrackAtCluster1+addChi2TrackAtCluster2 < bestAddChi2TrackAtCluster1+bestAddChi2TrackAtCluster2) {
+           // keep track of the best couple of clusters
+           bestAddChi2TrackAtCluster1 = addChi2TrackAtCluster1;
+           bestAddChi2TrackAtCluster2 = addChi2TrackAtCluster2;
+           bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
+           bestTrackParamAtCluster2 = extrapTrackParamAtCluster2;
           }
          
        }
        
       }
       
-      // if no hitForRecCh1 found then consider to add hitForRecCh2 only
-      if (!foundSecondHit) {
-       foundOneHit = kTRUE;
+      // if no clusterCh1 found then consider to add clusterCh2 only
+      if (!foundSecondCluster) {
+       foundOneCluster = kTRUE;
         
-       if (fgkTrackAllTracks) {
-         // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new hitForRec's
+       if (GetRecoParam()->TrackAllTracks()) {
+         // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
           newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
-         UpdateTrack(*newTrack,extrapTrackParamAtHit2,addChi2TrackAtHit2);
+         UpdateTrack(*newTrack,extrapTrackParamAtCluster2,addChi2TrackAtCluster2);
          fNRecTracks++;
          
          // if we are arrived on station(1..) 5, recompute track parameters and covariances starting from this station
@@ -505,15 +865,14 @@ Bool_t AliMUONTrackReconstructorK::FollowTrackInStation(AliMUONTrack &trackCandi
          
          // Printout for debuging
          if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-           cout << "FollowTrackInStation: added one hit in chamber(1..): " << ch2+1 << endl;
+           cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch2+1 << endl;
            if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
          }
          
-       } else if (!foundTwoHits && addChi2TrackAtHit2 < bestAddChi2TrackAtHit1) {
-         // keep track of the best single hitForRec except if a couple
-          // of hits has already been found (i.e. bestHitForRec2!=0x0)
-         bestAddChi2TrackAtHit1 = addChi2TrackAtHit2;
-         bestTrackParamAtHit1 = extrapTrackParamAtHit2;
+       } else if (!foundTwoClusters && addChi2TrackAtCluster2 < bestAddChi2TrackAtCluster1) {
+         // keep track of the best single cluster except if a couple of clusters has already been found
+         bestAddChi2TrackAtCluster1 = addChi2TrackAtCluster2;
+         bestTrackParamAtCluster1 = extrapTrackParamAtCluster2;
         }
        
       }
@@ -523,52 +882,77 @@ Bool_t AliMUONTrackReconstructorK::FollowTrackInStation(AliMUONTrack &trackCandi
   }
   
   // look for candidates in chamber 1 not already attached to a track
-  // if we want to keep all possible tracks or if no good couple of hitForRec has been found
-  if (fgkTrackAllTracks || !foundTwoHits) {
+  // if we want to keep all possible tracks or if no good couple of clusters has been found
+  if (GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
+    
+    // add MCS effect for next step
+    AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
+    
+    //Extrapolate trackCandidate to chamber "ch1"
+    AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch1),
+                                    GetRecoParam()->UseSmoother());
+    
+    // Printout for debuging
+    if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
+      cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch1)<<":"<<endl;
+      extrapTrackParamAtCh.GetParameters().Print();
+      extrapTrackParamAtCh.GetCovariances().Print();
+    }
     
     // Printout for debuging
     if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-      cout << "FollowTrackInStation: look for single hits in chamber(1..): " << ch1+1 << endl;
+      cout << "FollowTrackInStation: look for single clusters in chamber(1..): " << ch1+1 << endl;
     }
     
-    // add MCS effect for next step
-    AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh2,AliMUONConstants::ChamberThicknessInX0(),1.);
+    // reset cluster iterator of chamber 1
+    nextInCh1.Reset();
+    iCluster1 = -1;
+    
+    // look for second candidates in chamber 1
+    while ( ( clusterCh1 = static_cast<AliMUONVCluster*>(nextInCh1()) ) ) {
+      iCluster1++;
       
-    for (Int_t hit1 = 0; hit1 < fNHitsForRecPerChamber[ch1]; hit1++) {
+      if (clusterCh1Used[iCluster1]) continue; // Skip clusters already used
       
-      hitForRecCh1 = (AliMUONHitForRec*) fHitsForRecPtr->UncheckedAt(fIndexOfFirstHitForRecPerChamber[ch1]+hit1);
+      // try to add the current cluster fast
+      if (!TryOneClusterFast(extrapTrackParamAtCh, clusterCh1)) continue;
       
-      if (hitForRecCh1Used[hit1]) continue; // Skip hitForRec already used
+      // try to add the current cluster accuratly
+      chi2OfCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh1, extrapTrackParamAtCluster1,
+                                   GetRecoParam()->UseSmoother());
       
-      // try to add the current hit
-      chi2OfHitForRec = TryOneHitForRec(extrapTrackParamAtCh2, hitForRecCh1, extrapTrackParamAtHit1, fgkRunSmoother);
-    
-      // if good chi2 then consider to add hitForRecCh1
-      // We do not try to attach a hitForRec in the other chamber too since it has already been done above
-      if (chi2OfHitForRec < maxChi2OfHitForRec) {
-       foundOneHit = kTRUE;
+      // if good chi2 then consider to add clusterCh1
+      // We do not try to attach a cluster in the other chamber too since it has already been done above
+      if (chi2OfCluster < maxChi2OfCluster) {
+       foundOneCluster = kTRUE;
        
        // Printout for debuging
        if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-         cout << "FollowTrackInStation: found one hit in chamber(1..): " << ch1+1
-              << " (Chi2 = " << chi2OfHitForRec << ")" << endl;
+         cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch1+1
+              << " (Chi2 = " << chi2OfCluster << ")" << endl;
+         clusterCh1->Print();
        }
-       
-       if (fgkRunSmoother) {
+        
+       if (GetRecoParam()->UseSmoother()) {
           // save extrapolated parameters for smoother
-          extrapTrackParamAtHit1.SetExtrapParameters(extrapTrackParamAtHit1.GetParameters());
+          extrapTrackParamAtCluster1.SetExtrapParameters(extrapTrackParamAtCluster1.GetParameters());
           
           // save extrapolated covariance matrix for smoother
-          extrapTrackParamAtHit1.SetExtrapCovariances(extrapTrackParamAtHit1.GetCovariances());
+          extrapTrackParamAtCluster1.SetExtrapCovariances(extrapTrackParamAtCluster1.GetCovariances());
         }
         
-        // Compute new track parameters including "hitForRecCh1" using kalman filter
-        addChi2TrackAtHit1 = RunKalmanFilter(extrapTrackParamAtHit1);
+        // Compute new track parameters including "clusterCh1" using kalman filter
+        addChi2TrackAtCluster1 = RunKalmanFilter(extrapTrackParamAtCluster1);
         
-       if (fgkTrackAllTracks) {
-         // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new hitForRec's
+       // skip track with absolute bending momentum out of limits
+       bendingMomentum = TMath::Abs(1. / extrapTrackParamAtCluster1.GetInverseBendingMomentum());
+       if (bendingMomentum < GetRecoParam()->GetMinBendingMomentum() ||
+           bendingMomentum > GetRecoParam()->GetMaxBendingMomentum()) continue;
+       
+       if (GetRecoParam()->TrackAllTracks()) {
+         // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new cluster
          newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
-         UpdateTrack(*newTrack,extrapTrackParamAtHit1,addChi2TrackAtHit1);
+         UpdateTrack(*newTrack,extrapTrackParamAtCluster1,addChi2TrackAtCluster1);
          fNRecTracks++;
          
          // if we are arrived on station(1..) 5, recompute track parameters and covariances starting from this station
@@ -577,15 +961,14 @@ Bool_t AliMUONTrackReconstructorK::FollowTrackInStation(AliMUONTrack &trackCandi
          
          // Printout for debuging
          if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-           cout << "FollowTrackInStation: added one hit in chamber(1..): " << ch1+1 << endl;
+           cout << "FollowTrackInStation: added one cluster in chamber(1..): " << ch1+1 << endl;
            if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
          }
          
-       } else if (addChi2TrackAtHit1 < bestAddChi2TrackAtHit1) {
-         // keep track of the best single hitForRec except if a couple
-         // of hits has already been found (i.e. bestHitForRec1!=0x0)
-         bestAddChi2TrackAtHit1 = addChi2TrackAtHit1;
-         bestTrackParamAtHit1 = extrapTrackParamAtHit1;
+       } else if (addChi2TrackAtCluster1 < bestAddChi2TrackAtCluster1) {
+         // keep track of the best single cluster except if a couple of clusters has already been found
+         bestAddChi2TrackAtCluster1 = addChi2TrackAtCluster1;
+         bestTrackParamAtCluster1 = extrapTrackParamAtCluster1;
        }
        
       }
@@ -595,66 +978,73 @@ Bool_t AliMUONTrackReconstructorK::FollowTrackInStation(AliMUONTrack &trackCandi
   }
   
   // fill out the best track if required else clean up the fRecTracksPtr array
-  if (!fgkTrackAllTracks) {
-    if (foundTwoHits) {
-      UpdateTrack(trackCandidate,bestTrackParamAtHit1,bestTrackParamAtHit2,bestAddChi2TrackAtHit1,bestAddChi2TrackAtHit2);
+  if (!GetRecoParam()->TrackAllTracks()) {
+    if (foundTwoClusters) {
+      UpdateTrack(trackCandidate,bestTrackParamAtCluster1,bestTrackParamAtCluster2,bestAddChi2TrackAtCluster1,bestAddChi2TrackAtCluster2);
       
       // if we are arrived on station(1..) 5, recompute track parameters and covariances starting from this station
       // (going in the right direction)
       if (nextStation == 4) RetraceTrack(trackCandidate,kTRUE);
-
+      
       // Printout for debuging
       if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-        cout << "FollowTrackInStation: added the two best hits in station(1..): " << nextStation+1 << endl;
+        cout << "FollowTrackInStation: added the two best clusters in station(1..): " << nextStation+1 << endl;
         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
       }
       
-    } else if (foundOneHit) {
-      UpdateTrack(trackCandidate,bestTrackParamAtHit1,bestAddChi2TrackAtHit1);
+    } else if (foundOneCluster) {
+      UpdateTrack(trackCandidate,bestTrackParamAtCluster1,bestAddChi2TrackAtCluster1);
       
       // if we are arrived on station(1..) 5, recompute track parameters and covariances starting from this station
       // (going in the right direction)
       if (nextStation == 4) RetraceTrack(trackCandidate,kTRUE);
-
+      
       // Printout for debuging
       if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-        cout << "FollowTrackInStation: added the best hit in chamber(1..): " << bestTrackParamAtHit1.GetHitForRecPtr()->GetChamberNumber()+1 << endl;
+        cout << "FollowTrackInStation: added the best cluster in chamber(1..): " << bestTrackParamAtCluster1.GetClusterPtr()->GetChamberId()+1 << endl;
         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
       }
       
-    } else return kFALSE;
+    } else {
+      delete [] clusterCh1Used;
+      return kFALSE;
+    }
     
-  } else if (foundOneHit || foundTwoHits) {
+  } else if (foundOneCluster || foundTwoClusters) {
     
     // remove obsolete track
     fRecTracksPtr->Remove(&trackCandidate);
     fNRecTracks--;
     
-  } else return kFALSE;
+  } else {
+    delete [] clusterCh1Used;
+    return kFALSE;
+  }
   
+  delete [] clusterCh1Used;
   return kTRUE;
   
 }
 
   //__________________________________________________________________________
-Double_t AliMUONTrackReconstructorK::RunKalmanFilter(AliMUONTrackParam &trackParamAtHit)
+Double_t AliMUONTrackReconstructorK::RunKalmanFilter(AliMUONTrackParam &trackParamAtCluster)
 {
-  /// Compute new track parameters and their covariances including new hit using kalman filter
+  /// Compute new track parameters and their covariances including new cluster using kalman filter
   /// return the additional track chi2
   AliDebug(1,"Enter RunKalmanFilter");
   
   // Get actual track parameters (p)
-  TMatrixD param(trackParamAtHit.GetParameters());
+  TMatrixD param(trackParamAtCluster.GetParameters());
   
-  // Get new hit parameters (m)
-  AliMUONHitForRec *hitForRecAtHit = trackParamAtHit.GetHitForRecPtr();
-  TMatrixD hit(5,1);
-  hit.Zero();
-  hit(0,0) = hitForRecAtHit->GetNonBendingCoor();
-  hit(2,0) = hitForRecAtHit->GetBendingCoor();
+  // Get new cluster parameters (m)
+  AliMUONVCluster *cluster = trackParamAtCluster.GetClusterPtr();
+  TMatrixD clusterParam(5,1);
+  clusterParam.Zero();
+  clusterParam(0,0) = cluster->GetX();
+  clusterParam(2,0) = cluster->GetY();
   
   // Compute the actual parameter weight (W)
-  TMatrixD paramWeight(trackParamAtHit.GetCovariances());
+  TMatrixD paramWeight(trackParamAtCluster.GetCovariances());
   if (paramWeight.Determinant() != 0) {
     paramWeight.Invert();
   } else {
@@ -662,14 +1052,14 @@ Double_t AliMUONTrackReconstructorK::RunKalmanFilter(AliMUONTrackParam &trackPar
     return 1.e10;
   }
   
-  // Compute the new hit weight (U)
-  TMatrixD hitWeight(5,5);
-  hitWeight.Zero();
-  hitWeight(0,0) = 1. / hitForRecAtHit->GetNonBendingReso2();
-  hitWeight(2,2) = 1. / hitForRecAtHit->GetBendingReso2();
+  // Compute the new cluster weight (U)
+  TMatrixD clusterWeight(5,5);
+  clusterWeight.Zero();
+  clusterWeight(0,0) = 1. / cluster->GetErrX2();
+  clusterWeight(2,2) = 1. / cluster->GetErrY2();
 
   // Compute the new parameters covariance matrix ( (W+U)^-1 )
-  TMatrixD newParamCov(paramWeight,TMatrixD::kPlus,hitWeight);
+  TMatrixD newParamCov(paramWeight,TMatrixD::kPlus,clusterWeight);
   if (newParamCov.Determinant() != 0) {
     newParamCov.Invert();
   } else {
@@ -678,16 +1068,16 @@ Double_t AliMUONTrackReconstructorK::RunKalmanFilter(AliMUONTrackParam &trackPar
   }
   
   // Save the new parameters covariance matrix
-  trackParamAtHit.SetCovariances(newParamCov);
+  trackParamAtCluster.SetCovariances(newParamCov);
   
   // Compute the new parameters (p' = ((W+U)^-1)U(m-p) + p)
-  TMatrixD tmp(hit,TMatrixD::kMinus,param);
-  TMatrixD tmp2(hitWeight,TMatrixD::kMult,tmp); // U(m-p)
+  TMatrixD tmp(clusterParam,TMatrixD::kMinus,param);
+  TMatrixD tmp2(clusterWeight,TMatrixD::kMult,tmp); // U(m-p)
   TMatrixD newParam(newParamCov,TMatrixD::kMult,tmp2); // ((W+U)^-1)U(m-p)
   newParam += param; // ((W+U)^-1)U(m-p) + p
   
   // Save the new parameters
-  trackParamAtHit.SetParameters(newParam);
+  trackParamAtCluster.SetParameters(newParam);
   
   // Compute the additional chi2 (= ((p'-p)^-1)W(p'-p) + ((p'-m)^-1)U(p'-m))
   tmp = newParam; // p'
@@ -695,8 +1085,8 @@ Double_t AliMUONTrackReconstructorK::RunKalmanFilter(AliMUONTrackParam &trackPar
   TMatrixD tmp3(paramWeight,TMatrixD::kMult,tmp); // W(p'-p)
   TMatrixD addChi2Track(tmp,TMatrixD::kTransposeMult,tmp3); // ((p'-p)^-1)W(p'-p)
   tmp = newParam; // p'
-  tmp -= hit; // (p'-m)
-  TMatrixD tmp4(hitWeight,TMatrixD::kMult,tmp); // U(p'-m)
+  tmp -= clusterParam; // (p'-m)
+  TMatrixD tmp4(clusterWeight,TMatrixD::kMult,tmp); // U(p'-m)
   addChi2Track += TMatrixD(tmp,TMatrixD::kTransposeMult,tmp4); // ((p'-p)^-1)W(p'-p) + ((p'-m)^-1)U(p'-m)
   
   return addChi2Track(0,0);
@@ -704,110 +1094,125 @@ Double_t AliMUONTrackReconstructorK::RunKalmanFilter(AliMUONTrackParam &trackPar
 }
 
   //__________________________________________________________________________
-void AliMUONTrackReconstructorK::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtHit, Double_t addChi2)
+void AliMUONTrackReconstructorK::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster, Double_t addChi2)
 {
-  /// Add 1 hit to the track candidate
+  /// Add 1 cluster to the track candidate
   /// Update chi2 of the track 
   
-  // Update the track chi2 into TrackParamAtHit
-  trackParamAtHit.SetTrackChi2(track.GetFitFMin() + addChi2);
+  // Flag cluster as being (not) removable
+  if (GetRecoParam()->RequestStation(trackParamAtCluster.GetClusterPtr()->GetChamberId()/2))
+    trackParamAtCluster.SetRemovable(kFALSE);
+  else trackParamAtCluster.SetRemovable(kTRUE);
+  trackParamAtCluster.SetLocalChi2(0.); // --> Local chi2 not used
+  
+  // Update the track chi2 into trackParamAtCluster
+  trackParamAtCluster.SetTrackChi2(track.GetGlobalChi2() + addChi2);
   
   // Update the chi2 of the new track
-  track.SetFitFMin(trackParamAtHit.GetTrackChi2());
+  track.SetGlobalChi2(trackParamAtCluster.GetTrackChi2());
   
-  // Update array of TrackParamAtHit
-  track.AddTrackParamAtHit(&trackParamAtHit,trackParamAtHit.GetHitForRecPtr());
-  track.GetTrackParamAtHit()->Sort();
+  // Update array of TrackParamAtCluster
+  track.AddTrackParamAtCluster(trackParamAtCluster,*(trackParamAtCluster.GetClusterPtr()));
   
 }
 
   //__________________________________________________________________________
-void AliMUONTrackReconstructorK::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtHit1, AliMUONTrackParam &trackParamAtHit2,
-                                            Double_t addChi2AtHit1, Double_t addChi2AtHit2)
+void AliMUONTrackReconstructorK::UpdateTrack(AliMUONTrack &track, AliMUONTrackParam &trackParamAtCluster1, AliMUONTrackParam &trackParamAtCluster2,
+                                            Double_t addChi2AtCluster1, Double_t addChi2AtCluster2)
 {
-  /// Add 2 hits to the track candidate
+  /// Add 2 clusters to the track candidate (order is important)
   /// Update track and local chi2
   
-  // Update local chi2 at first hit
-  AliMUONHitForRec* hit1 = trackParamAtHit1.GetHitForRecPtr();
-  Double_t deltaX = trackParamAtHit1.GetNonBendingCoor() - hit1->GetNonBendingCoor();
-  Double_t deltaY = trackParamAtHit1.GetBendingCoor() - hit1->GetBendingCoor();
-  Double_t localChi2AtHit1 = deltaX*deltaX / hit1->GetNonBendingReso2() +
-                            deltaY*deltaY / hit1->GetBendingReso2();
-  trackParamAtHit1.SetLocalChi2(localChi2AtHit1);
+  // Update local chi2 at first cluster
+  AliMUONVCluster* cluster1 = trackParamAtCluster1.GetClusterPtr();
+  Double_t deltaX = trackParamAtCluster1.GetNonBendingCoor() - cluster1->GetX();
+  Double_t deltaY = trackParamAtCluster1.GetBendingCoor() - cluster1->GetY();
+  Double_t localChi2AtCluster1 = deltaX*deltaX / cluster1->GetErrX2() +
+                                deltaY*deltaY / cluster1->GetErrY2();
+  trackParamAtCluster1.SetLocalChi2(localChi2AtCluster1);
   
-  // Flag first hit as being removable
-  trackParamAtHit1.SetRemovable(kTRUE);
+  // Flag first cluster as being removable
+  trackParamAtCluster1.SetRemovable(kTRUE);
   
-  // Update local chi2 at second hit
-  AliMUONHitForRec* hit2 = trackParamAtHit2.GetHitForRecPtr();
-  AliMUONTrackParam extrapTrackParamAtHit2(trackParamAtHit1);
-  AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParamAtHit2, trackParamAtHit2.GetZ());
-  deltaX = extrapTrackParamAtHit2.GetNonBendingCoor() - hit2->GetNonBendingCoor();
-  deltaY = extrapTrackParamAtHit2.GetBendingCoor() - hit2->GetBendingCoor();
-  Double_t localChi2AtHit2 = deltaX*deltaX / hit2->GetNonBendingReso2() +
-                            deltaY*deltaY / hit2->GetBendingReso2();
-  trackParamAtHit2.SetLocalChi2(localChi2AtHit2);
+  // Update local chi2 at second cluster
+  AliMUONVCluster* cluster2 = trackParamAtCluster2.GetClusterPtr();
+  AliMUONTrackParam extrapTrackParamAtCluster2(trackParamAtCluster1);
+  AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParamAtCluster2, trackParamAtCluster2.GetZ());
+  deltaX = extrapTrackParamAtCluster2.GetNonBendingCoor() - cluster2->GetX();
+  deltaY = extrapTrackParamAtCluster2.GetBendingCoor() - cluster2->GetY();
+  Double_t localChi2AtCluster2 = deltaX*deltaX / cluster2->GetErrX2() +
+                                deltaY*deltaY / cluster2->GetErrY2();
+  trackParamAtCluster2.SetLocalChi2(localChi2AtCluster2);
   
-  // Flag second hit as being removable
-  trackParamAtHit2.SetRemovable(kTRUE);
+  // Flag second cluster as being removable
+  trackParamAtCluster2.SetRemovable(kTRUE);
   
-  // Update the track chi2 into TrackParamAtHit1
-  trackParamAtHit1.SetTrackChi2(track.GetFitFMin() + addChi2AtHit1);
+  // Update the track chi2 into trackParamAtCluster2
+  trackParamAtCluster2.SetTrackChi2(track.GetGlobalChi2() + addChi2AtCluster2);
   
-  // Update the track chi2 into TrackParamAtHit2
-  trackParamAtHit2.SetTrackChi2(trackParamAtHit1.GetTrackChi2() + addChi2AtHit2);
+  // Update the track chi2 into trackParamAtCluster1
+  trackParamAtCluster1.SetTrackChi2(trackParamAtCluster2.GetTrackChi2() + addChi2AtCluster1);
   
   // Update the chi2 of the new track
-  track.SetFitFMin(trackParamAtHit2.GetTrackChi2());
+  track.SetGlobalChi2(trackParamAtCluster1.GetTrackChi2());
   
-  // Update array of TrackParamAtHit
-  track.AddTrackParamAtHit(&trackParamAtHit1,trackParamAtHit1.GetHitForRecPtr());
-  track.AddTrackParamAtHit(&trackParamAtHit2,trackParamAtHit2.GetHitForRecPtr());
-  track.GetTrackParamAtHit()->Sort();
+  // Update array of trackParamAtCluster
+  track.AddTrackParamAtCluster(trackParamAtCluster1,*cluster1);
+  track.AddTrackParamAtCluster(trackParamAtCluster2,*cluster2);
   
 }
 
   //__________________________________________________________________________
-Bool_t AliMUONTrackReconstructorK::RecoverTrack(AliMUONTrack &trackCandidate, Int_t nextStation)
+Bool_t AliMUONTrackReconstructorK::RecoverTrack(AliMUONTrack &trackCandidate, AliMUONVClusterStore& clusterStore, Int_t nextStation)
 {
   /// Try to recover the track candidate in the next station
-  /// by removing the worst of the two hits attached in the current station
+  /// by removing the worst of the two clusters attached in the current station
   /// Return kTRUE if recovering succeeds
   AliDebug(1,"Enter RecoverTrack");
   
-  // Do not try to recover track until we have attached hit(s) on station(1..) 3
+  // Do not try to recover track until we have attached cluster(s) on station(1..) 3
   if (nextStation > 1) return kFALSE;
   
-  Int_t worstHitNumber = -1;
-  Double_t localChi2, worstChi2 = 0.;
+  Int_t worstClusterNumber = -1;
+  Double_t localChi2, worstLocalChi2 = -1.;
   
-  // Look for the hit to remove
-  for (Int_t hitNumber = 0; hitNumber < 2; hitNumber++) {
-    AliMUONTrackParam *trackParamAtHit = (AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt(hitNumber);
+  // Look for the cluster to remove
+  for (Int_t clusterNumber = 0; clusterNumber < 2; clusterNumber++) {
+    AliMUONTrackParam *trackParamAtCluster = (AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(clusterNumber);
+    
+    // check if current cluster is in the previous station
+    if (trackParamAtCluster->GetClusterPtr()->GetChamberId()/2 != nextStation+1) break;
+    
+    // check if current cluster is removable
+    if (!trackParamAtCluster->IsRemovable()) return kFALSE;
     
-    // check if current hit is removable
-    if (!trackParamAtHit->IsRemovable()) return kFALSE;
+    // reset the current cluster as being not removable if it is on a required station
+    if (GetRecoParam()->RequestStation(nextStation+1)) trackParamAtCluster->SetRemovable(kFALSE);
     
-    // Pick up hit with the worst chi2
-    localChi2 = trackParamAtHit->GetLocalChi2();
-    if (localChi2 > worstChi2) {
-      worstChi2 = localChi2;
-      worstHitNumber = hitNumber;
+    // Pick up cluster with the worst chi2
+    localChi2 = trackParamAtCluster->GetLocalChi2();
+    if (localChi2 > worstLocalChi2) {
+      worstLocalChi2 = localChi2;
+      worstClusterNumber = clusterNumber;
     }
   }
   
-  // Reset best hit as being NOT removable
-  ((AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt((worstHitNumber+1)%2))->SetRemovable(kFALSE);
+  // check if worst cluster found
+  if (worstClusterNumber < 0) return kFALSE;
   
-  // Remove the worst hit
-  trackCandidate.RemoveTrackParamAtHit((AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt(worstHitNumber));
+  // Remove the worst cluster
+  trackCandidate.RemoveTrackParamAtCluster((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(worstClusterNumber));
   
-  // Re-calculate track parameters at the (new) first hit
-  RetracePartialTrack(trackCandidate,(AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->UncheckedAt(1));
+  // Re-calculate track parameters at the (new) first cluster
+  RetracePartialTrack(trackCandidate,(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(1));
   
-  // Look for new hit(s) in next station
-  return FollowTrackInStation(trackCandidate,nextStation);
+  // skip track with absolute bending momentum out of limits
+  Double_t bendingMomentum = TMath::Abs(1. / ((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First())->GetInverseBendingMomentum());
+  if (bendingMomentum < GetRecoParam()->GetMinBendingMomentum() ||
+      bendingMomentum > GetRecoParam()->GetMaxBendingMomentum()) return kFALSE;
+  
+  // Look for new cluster(s) in next station
+  return FollowTrackInStation(trackCandidate, clusterStore, nextStation);
   
 }
 
@@ -815,24 +1220,24 @@ Bool_t AliMUONTrackReconstructorK::RecoverTrack(AliMUONTrack &trackCandidate, In
 Bool_t AliMUONTrackReconstructorK::RunSmoother(AliMUONTrack &track)
 {
   /// Compute new track parameters and their covariances using smoother
-  AliDebug(1,"Enter RunSmoother");
+  AliDebug(1,"Enter UseSmoother");
   
-  AliMUONTrackParam *previousTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtHit()->First();
+  AliMUONTrackParam *previousTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->First();
   
-  // Smoothed parameters and covariances at first hit = filtered parameters and covariances
+  // Smoothed parameters and covariances at first cluster = filtered parameters and covariances
   previousTrackParam->SetSmoothParameters(previousTrackParam->GetParameters());
   previousTrackParam->SetSmoothCovariances(previousTrackParam->GetCovariances());
   
-  // Compute local chi2 at first hit
-  AliMUONHitForRec *hitForRecAtHit = previousTrackParam->GetHitForRecPtr();
-  Double_t dX = hitForRecAtHit->GetNonBendingCoor() - previousTrackParam->GetNonBendingCoor();
-  Double_t dY = hitForRecAtHit->GetBendingCoor() - previousTrackParam->GetBendingCoor();
-  Double_t localChi2 = dX * dX / hitForRecAtHit->GetNonBendingReso2() + dY * dY / hitForRecAtHit->GetBendingReso2();
+  // Compute local chi2 at first cluster
+  AliMUONVCluster *cluster = previousTrackParam->GetClusterPtr();
+  Double_t dX = cluster->GetX() - previousTrackParam->GetNonBendingCoor();
+  Double_t dY = cluster->GetY() - previousTrackParam->GetBendingCoor();
+  Double_t chi2 = dX * dX / cluster->GetErrX2() + dY * dY / cluster->GetErrY2();
   
-  // Save local chi2 at first hit
-  previousTrackParam->SetLocalChi2(localChi2);
+  // Save local chi2 at first cluster
+  previousTrackParam->SetLocalChi2(chi2);
   
-  AliMUONTrackParam *currentTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtHit()->After(previousTrackParam);
+  AliMUONTrackParam *currentTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(previousTrackParam);
   while (currentTrackParam) {
     
     // Get variables
@@ -872,19 +1277,19 @@ Bool_t AliMUONTrackReconstructorK::RunSmoother(AliMUONTrack &track)
     // Save smoothed covariances
     currentTrackParam->SetSmoothCovariances(smoothCovariances);
     
-    // Compute smoothed residual: r(k n) = hit - X(k n)
-    hitForRecAtHit = currentTrackParam->GetHitForRecPtr();
+    // Compute smoothed residual: r(k n) = cluster - X(k n)
+    cluster = currentTrackParam->GetClusterPtr();
     TMatrixD smoothResidual(2,1);
     smoothResidual.Zero();
-    smoothResidual(0,0) = hitForRecAtHit->GetNonBendingCoor() - smoothParameters(0,0);
-    smoothResidual(1,0) = hitForRecAtHit->GetBendingCoor() - smoothParameters(2,0);
+    smoothResidual(0,0) = cluster->GetX() - smoothParameters(0,0);
+    smoothResidual(1,0) = cluster->GetY() - smoothParameters(2,0);
     
-    // Compute weight of smoothed residual: W(k n) = (hitCov - C(k n))^-1
+    // Compute weight of smoothed residual: W(k n) = (clusterCov - C(k n))^-1
     TMatrixD smoothResidualWeight(2,2);
-    smoothResidualWeight(0,0) = hitForRecAtHit->GetNonBendingReso2() - smoothCovariances(0,0);
+    smoothResidualWeight(0,0) = cluster->GetErrX2() - smoothCovariances(0,0);
     smoothResidualWeight(0,1) = - smoothCovariances(0,2);
     smoothResidualWeight(1,0) = - smoothCovariances(2,0);
-    smoothResidualWeight(1,1) = hitForRecAtHit->GetBendingReso2() - smoothCovariances(2,2);
+    smoothResidualWeight(1,1) = cluster->GetErrY2() - smoothCovariances(2,2);
     if (smoothResidualWeight.Determinant() != 0) {
       smoothResidualWeight.Invert();
     } else {
@@ -900,7 +1305,7 @@ Bool_t AliMUONTrackReconstructorK::RunSmoother(AliMUONTrack &track)
     currentTrackParam->SetLocalChi2(localChi2(0,0));
     
     previousTrackParam = currentTrackParam;
-    currentTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtHit()->After(previousTrackParam);
+    currentTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(previousTrackParam);
   }
   
   return kTRUE;
@@ -908,139 +1313,248 @@ Bool_t AliMUONTrackReconstructorK::RunSmoother(AliMUONTrack &track)
 }
 
   //__________________________________________________________________________
-void AliMUONTrackReconstructorK::ImproveTracks()
+void AliMUONTrackReconstructorK::ComplementTracks(const AliMUONVClusterStore& clusterStore)
 {
-  /// Improve tracks by removing clusters with local chi2 highter than the defined cut
-  /// Recompute track parameters and covariances at the remaining clusters
-  AliDebug(1,"Enter ImproveTracks");
-  
-  Double_t localChi2, worstLocalChi2;
-  Int_t worstChamber;
-  AliMUONTrackParam *trackParamAtHit, *worstTrackParamAtHit;
-  Bool_t smoothed;
-  
-  // Remove double track to improve only "good" tracks
-  RemoveDoubleTracks();
+  /// Complete tracks by adding missing clusters (if there is an overlap between
+  /// two detection elements, the track may have two clusters in the same chamber)
+  /// Recompute track parameters and covariances at each clusters
+  AliDebug(1,"Enter ComplementTracks");
+  
+  Int_t chamberId, detElemId;
+  Double_t chi2OfCluster, addChi2TrackAtCluster, bestAddChi2TrackAtCluster;
+  Double_t maxChi2OfCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
+                                  GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
+  Bool_t foundOneCluster, trackModified;
+  AliMUONVCluster *cluster;
+  AliMUONTrackParam *trackParam, *previousTrackParam, *nextTrackParam, trackParamAtCluster, bestTrackParamAtCluster;
   
   AliMUONTrack *track = (AliMUONTrack*) fRecTracksPtr->First();
   while (track) {
+    trackModified = kFALSE;
     
-    while (!track->IsImproved()) {
+    trackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->First();
+    previousTrackParam = trackParam;
+    while (trackParam) {
+      foundOneCluster = kFALSE;
+      bestAddChi2TrackAtCluster = 1.e10;
+      chamberId = trackParam->GetClusterPtr()->GetChamberId();
+      detElemId = trackParam->GetClusterPtr()->GetDetElemId();
       
-      // Run smoother if required
-      smoothed = kFALSE;
-      if (fgkRunSmoother) smoothed = RunSmoother(*track);
+      // prepare nextTrackParam before adding new cluster because of the sorting
+      nextTrackParam = (AliMUONTrackParam*)track->GetTrackParamAtCluster()->After(trackParam);
       
-      // Use standard procedure to compute local chi2 if smoother not required or not working
-      if (!smoothed) {
-        
-        // Update track parameters and covariances
-        track->UpdateCovTrackParamAtHit();
-        
-        // Compute local chi2 of each hits
-        track->ComputeLocalChi2(kTRUE);
-      }
+      // Create iterators to loop over clusters in current chamber
+      TIter nextInCh(clusterStore.CreateChamberIterator(chamberId,chamberId));
       
-      // Look for the hit to remove
-      worstTrackParamAtHit = NULL;
-      worstLocalChi2 = 0.;
-      trackParamAtHit = (AliMUONTrackParam*)track->GetTrackParamAtHit()->First();
-      while (trackParamAtHit) {
+      // look for one second candidate in the same chamber
+      while ( ( cluster = static_cast<AliMUONVCluster*>(nextInCh()) ) ) {
         
-        // Pick up hit with the worst chi2
-        localChi2 = trackParamAtHit->GetLocalChi2();
-        if (localChi2 > worstLocalChi2) {
-          worstLocalChi2 = localChi2;
-          worstTrackParamAtHit = trackParamAtHit;
-        }
-        
-      trackParamAtHit = (AliMUONTrackParam*)track->GetTrackParamAtHit()->After(trackParamAtHit);
-      }
-      
-      // Check if bad removable hit found
-      if (!worstTrackParamAtHit) {
-        track->SetImproved(kTRUE);
-        break;
-      }
-      
-      // check whether the worst hit is removable or not
-      if (!worstTrackParamAtHit->IsRemovable()) {
-        track->SetImproved(kTRUE);
-        break;
-      }
-      
-      // Check whether the worst chi2 is under requirement or not
-      if (worstLocalChi2 < 2. * fgkSigmaToCutForImprovement * fgkSigmaToCutForImprovement) { // 2 because 2 quantities in chi2
-        track->SetImproved(kTRUE);
-        break;
+       // look for a cluster in another detection element
+       if (cluster->GetDetElemId() == detElemId) continue;
+       
+       // try to add the current cluster fast
+       if (!TryOneClusterFast(*trackParam, cluster)) continue;
+       
+       // try to add the current cluster accurately
+       // never use track parameters at last cluster because the covariance matrix is meaningless
+       if (nextTrackParam) chi2OfCluster = TryOneCluster(*trackParam, cluster, trackParamAtCluster);
+       else chi2OfCluster = TryOneCluster(*previousTrackParam, cluster, trackParamAtCluster);
+       
+       // if good chi2 then consider to add this cluster to the track
+       if (chi2OfCluster < maxChi2OfCluster) {
+          
+         // Compute local track parameters including current cluster using kalman filter
+          addChi2TrackAtCluster = RunKalmanFilter(trackParamAtCluster);
+          
+         // keep track of the best cluster
+         if (addChi2TrackAtCluster < bestAddChi2TrackAtCluster) {
+           bestAddChi2TrackAtCluster = addChi2TrackAtCluster;
+           bestTrackParamAtCluster = trackParamAtCluster;
+           foundOneCluster = kTRUE;
+         }
+         
+       }
+       
       }
       
-      // Reset the second hit in the same station as the bad one as being NOT removable
-      worstChamber = worstTrackParamAtHit->GetHitForRecPtr()->GetChamberNumber();
-      if (worstChamber%2 == 0) ((AliMUONTrackParam*)track->GetTrackParamAtHit()->After(worstTrackParamAtHit))->SetRemovable(kFALSE);
-      else ((AliMUONTrackParam*)track->GetTrackParamAtHit()->Before(worstTrackParamAtHit))->SetRemovable(kFALSE);
-      
-      // Save pointer to the trackParamAtHit next to the one to be removed
-      trackParamAtHit = (AliMUONTrackParam*)track->GetTrackParamAtHit()->After(worstTrackParamAtHit);
-      
-      // Remove the worst hit
-      track->RemoveTrackParamAtHit(worstTrackParamAtHit);
-      
-      // Re-calculate track parameters
-      // - from the hit immediately downstream the one suppressed
-      // - or from the begining - if parameters have been re-computed using the standard method (kalman parameters have been lost)
-      //                       - or if the removed hit was the last one
-      if (smoothed && trackParamAtHit) RetracePartialTrack(*track,trackParamAtHit);
-      else RetraceTrack(*track,kTRUE);
-      
-      // Printout for debuging
-      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-        cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(track)+1 << " improved " << endl;
+      // add new cluster if any
+      if (foundOneCluster) {
+       
+       // Printout for debuging
+       if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+         cout << "ComplementTracks: found one cluster in chamber(1..): " << chamberId+1 << endl;
+         bestTrackParamAtCluster.GetClusterPtr()->Print();
+         if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
+           cout<<endl<<"Track parameters and covariances at cluster:"<<endl;
+           bestTrackParamAtCluster.GetParameters().Print();
+           bestTrackParamAtCluster.GetCovariances().Print();
+         }
+       }
+       
+       trackParam->SetRemovable(kTRUE);
+       bestTrackParamAtCluster.SetRemovable(kTRUE);
+       track->AddTrackParamAtCluster(bestTrackParamAtCluster,*(bestTrackParamAtCluster.GetClusterPtr()));
+       trackModified = kTRUE;
       }
       
+      previousTrackParam = trackParam;
+      trackParam = nextTrackParam;
     }
     
+    // re-compute track parameters using kalman filter if needed
+    if (trackModified) RetraceTrack(*track,kTRUE);
+    
     track = (AliMUONTrack*) fRecTracksPtr->After(track);
   }
   
 }
 
-  //__________________________________________________________________________
-void AliMUONTrackReconstructorK::Finalize()
+//__________________________________________________________________________
+void AliMUONTrackReconstructorK::ImproveTrack(AliMUONTrack &track)
 {
-  /// Fill AliMUONTrack's fHitForRecAtHit array
-  AliMUONTrack *track;
-  AliMUONTrackParam *trackParamAtHit;
-  Bool_t smoothed = kFALSE;
+  /// Improve the given track by removing removable clusters with local chi2 highter than the defined cut
+  /// Removable clusters are identified by the method AliMUONTrack::TagRemovableClusters()
+  /// Recompute track parameters and covariances at the remaining clusters
+  AliDebug(1,"Enter ImproveTrack");
   
-  track = (AliMUONTrack*) fRecTracksPtr->First();
-  while (track) {
+  Double_t localChi2, worstLocalChi2;
+  AliMUONTrackParam *trackParamAtCluster, *worstTrackParamAtCluster, *nextTrackParam;
+  Bool_t smoothed;
+  Double_t sigmaCut2 = GetRecoParam()->GetSigmaCutForImprovement() *
+                       GetRecoParam()->GetSigmaCutForImprovement();
+  
+  while (!track.IsImproved()) {
+    
+    // identify removable clusters
+    track.TagRemovableClusters(GetRecoParam()->RequestedStationMask());
+    
+    // Run smoother if required
+    smoothed = kFALSE;
+    if (GetRecoParam()->UseSmoother()) smoothed = RunSmoother(track);
     
-    // update track parameters (using smoother if required) if not already done
-    if (!track->IsImproved()) {
-      smoothed = kFALSE;
-      if (fgkRunSmoother) smoothed = RunSmoother(*track);
-      if (!smoothed) track->UpdateCovTrackParamAtHit();
+    // Use standard procedure to compute local chi2 if smoother not required or not working
+    if (!smoothed) {
+      
+      // Update track parameters and covariances
+      track.UpdateCovTrackParamAtCluster();
+      
+      // Compute local chi2 of each clusters
+      track.ComputeLocalChi2(kTRUE);
     }
     
-    trackParamAtHit = (AliMUONTrackParam*) (track->GetTrackParamAtHit()->First());
-    while (trackParamAtHit) {
+    // Look for the cluster to remove
+    worstTrackParamAtCluster = 0x0;
+    worstLocalChi2 = 0.;
+    trackParamAtCluster = (AliMUONTrackParam*)track.GetTrackParamAtCluster()->First();
+    while (trackParamAtCluster) {
       
-      // copy smoothed parameters and covariances if any
-      if (smoothed) {
-        trackParamAtHit->SetParameters(trackParamAtHit->GetSmoothParameters());
-        trackParamAtHit->SetCovariances(trackParamAtHit->GetSmoothCovariances());
+      // save parameters into smooth parameters in case of smoother did not work properly
+      if (GetRecoParam()->UseSmoother() && !smoothed) {
+       trackParamAtCluster->SetSmoothParameters(trackParamAtCluster->GetParameters());
+       trackParamAtCluster->SetSmoothCovariances(trackParamAtCluster->GetCovariances());
       }
       
-      // update array of track hits
-      track->AddHitForRecAtHit(trackParamAtHit->GetHitForRecPtr());
+      // Pick up cluster with the worst chi2
+      localChi2 = trackParamAtCluster->GetLocalChi2();
+      if (localChi2 > worstLocalChi2) {
+       worstLocalChi2 = localChi2;
+       worstTrackParamAtCluster = trackParamAtCluster;
+      }
       
-      trackParamAtHit = (AliMUONTrackParam*) (track->GetTrackParamAtHit()->After(trackParamAtHit));
+      trackParamAtCluster = (AliMUONTrackParam*)track.GetTrackParamAtCluster()->After(trackParamAtCluster);
     }
     
-    track = (AliMUONTrack*) fRecTracksPtr->After(track);
+    // Check if worst cluster found
+    if (!worstTrackParamAtCluster) {
+      AliWarning("Bad local chi2 values?");
+      break;
+    }
+    
+    // Check whether the worst chi2 is under requirement or not
+    if (worstLocalChi2 < 2. * sigmaCut2) { // 2 because 2 quantities in chi2
+      track.SetImproved(kTRUE);
+      break;
+    }
+    
+    // if the worst cluster is not removable then stop improvement
+    if (!worstTrackParamAtCluster->IsRemovable()) break;
+    
+    // get track parameters at cluster next to the one to be removed
+    nextTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(worstTrackParamAtCluster);
+    
+    // Remove the worst cluster
+    track.RemoveTrackParamAtCluster(worstTrackParamAtCluster);
+    
+    // Re-calculate track parameters
+    // - from the cluster immediately downstream the one suppressed
+    // - or from the begining - if parameters have been re-computed using the standard method (kalman parameters have been lost)
+    //                 - or if the removed cluster was the last one
+    if (smoothed && nextTrackParam) RetracePartialTrack(track,nextTrackParam);
+    else RetraceTrack(track,kTRUE);
+    
+    // Printout for debuging
+    if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructorK") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+      cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(&track)+1 << " improved " << endl;
+    }
     
   }
+  
+}
+
+//__________________________________________________________________________
+void AliMUONTrackReconstructorK::FinalizeTrack(AliMUONTrack &track)
+{
+  /// Update track parameters and covariances at each attached cluster
+  /// using smoother if required, if not already done
+  
+  AliMUONTrackParam *trackParamAtCluster;
+  Bool_t smoothed = kFALSE;
+  
+  // update track parameters (using smoother if required) if not already done
+  if (!track.IsImproved()) {
+    smoothed = kFALSE;
+    if (GetRecoParam()->UseSmoother()) smoothed = RunSmoother(track);
+    if (!smoothed) track.UpdateCovTrackParamAtCluster();
+  } else smoothed = GetRecoParam()->UseSmoother();
+  
+  // copy smoothed parameters and covariances if any
+  if (smoothed) {
     
+    trackParamAtCluster = (AliMUONTrackParam*) (track.GetTrackParamAtCluster()->First());
+    while (trackParamAtCluster) {
+      
+      trackParamAtCluster->SetParameters(trackParamAtCluster->GetSmoothParameters());
+      trackParamAtCluster->SetCovariances(trackParamAtCluster->GetSmoothCovariances());
+      
+      trackParamAtCluster = (AliMUONTrackParam*) (track.GetTrackParamAtCluster()->After(trackParamAtCluster));
+    }
+    
+  }
+  
+}
+
+  //__________________________________________________________________________
+Bool_t AliMUONTrackReconstructorK::RefitTrack(AliMUONTrack &track, Bool_t enableImprovement)
+{
+  /// re-fit the given track
+  
+  // check validity of the track
+  if (track.GetNClusters() < 3) {
+    AliWarning("the track does not contain enough clusters --> unable to refit");
+    return kFALSE;
+  }
+  
+  // re-compute track parameters and covariances using Kalman filter
+  RetraceTrack(track,kTRUE);
+  
+  // Improve the reconstructed tracks if required
+  track.SetImproved(kFALSE);
+  if (enableImprovement && GetRecoParam()->ImproveTracks()) ImproveTrack(track);
+  
+  // Fill AliMUONTrack data members
+  FinalizeTrack(track);
+  
+  return kTRUE;
+  
 }