]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONTrackReconstructor.cxx
- AliMUONRecoParam.cxx:
[u/mrichter/AliRoot.git] / MUON / AliMUONTrackReconstructor.cxx
index 586ec97ae0acbb137c3f3b931a47c8830cafd21c..d087b33c5ebf19e8cd70d83ab8bf0239214028c0 100644 (file)
@@ -53,8 +53,8 @@ ClassImp(AliMUONTrackReconstructor) // Class implementation in ROOT context
 /// \endcond
 
   //__________________________________________________________________________
-AliMUONTrackReconstructor::AliMUONTrackReconstructor(AliMUONVClusterServer& clusterServer)
-  : AliMUONVTrackReconstructor(clusterServer)
+AliMUONTrackReconstructor::AliMUONTrackReconstructor(const AliMUONRecoParam* recoParam, AliMUONVClusterServer* clusterServer)
+  : AliMUONVTrackReconstructor(recoParam,clusterServer)
 {
   /// Constructor
 }
@@ -80,19 +80,26 @@ void AliMUONTrackReconstructor::MakeTrackCandidates(AliMUONVClusterStore& cluste
 
   AliDebug(1,"Enter MakeTrackCandidates");
 
-  // Ask the clustering to reconstruct all clusters in station 4 and 5
-  if (AliMUONReconstructor::GetRecoParam()->CombineClusterTrackReco()) {
-    fClusterServer.Clusterize(6, clusterStore, AliMpArea());
-    fClusterServer.Clusterize(7, clusterStore, AliMpArea());
-    fClusterServer.Clusterize(8, clusterStore, AliMpArea());
-    fClusterServer.Clusterize(9, clusterStore, AliMpArea());
+  // 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(clusterStore,istat);
+    segments = MakeSegmentsBetweenChambers(clusterStore, 2*istat, 2*istat+1);
     
     // Loop over segments
     for (Int_t iseg=0; iseg<segments->GetEntriesFast(); iseg++) 
@@ -100,22 +107,16 @@ void AliMUONTrackReconstructor::MakeTrackCandidates(AliMUONVClusterStore& cluste
       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++;
       
-      // Printout for debuging
-      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
-        cout<<endl<<"Track parameter covariances at first cluster:"<<endl;
-        ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->First())->GetCovariances().Print();
-      }
-      
       // Look for compatible cluster(s) in the other station
-      if (AliMUONReconstructor::GetRecoParam()->MakeTrackCandidatesFast())
+      if (GetRecoParam()->MakeTrackCandidatesFast())
        clusterFound = FollowLinearTrackInStation(*track, clusterStore, 7-istat);
       else clusterFound = FollowTrackInStation(*track, clusterStore, 7-istat);
       
       // Remove track if no cluster found
-      if (!clusterFound) {
+      if (!clusterFound && GetRecoParam()->RequestStation(7-istat)) {
         fRecTracksPtr->Remove(track);
        fNRecTracks--;
       }
@@ -129,7 +130,78 @@ void AliMUONTrackReconstructor::MakeTrackCandidates(AliMUONVClusterStore& cluste
   fRecTracksPtr->Compress(); // this is essential before checking tracks
   
   // Keep all different tracks or only the best ones as required
-  if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) RemoveIdenticalTracks();
+  if (GetRecoParam()->TrackAllTracks()) RemoveIdenticalTracks();
+  else RemoveDoubleTracks();
+  
+  AliDebug(1,Form("Number of good candidates = %d",fNRecTracks));
+  
+}
+
+  //__________________________________________________________________________
+void AliMUONTrackReconstructor::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;
+  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 in the station
+      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
+  
+  // 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));
@@ -145,10 +217,9 @@ void AliMUONTrackReconstructor::FollowTracks(AliMUONVClusterStore& clusterStore)
   AliMUONTrack *track, *nextTrack;
   AliMUONTrackParam *trackParam, *nextTrackParam;
   Int_t currentNRecTracks;
-  Bool_t clusterFound;
   
-  Double_t sigmaCut2 = AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
-                       AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking();
+  Double_t sigmaCut2 = GetRecoParam()->GetSigmaCutForTracking() *
+                       GetRecoParam()->GetSigmaCutForTracking();
   
   for (Int_t station = 2; station >= 0; station--) {
     
@@ -165,19 +236,24 @@ void AliMUONTrackReconstructor::FollowTracks(AliMUONVClusterStore& clusterStore)
       // Fit the track:
       // Do not take into account the multiple scattering to speed up the fit
       // Calculate the track parameter covariance matrix
-      // If "station" is station(1..) 3 then use the vertex to better constrain the fit
-      if (station==2) Fit(*track, kFALSE, kTRUE, kTRUE);
+      // If there is no cluster out of station 4 or 5 then use the vertex to better constrain the fit
+      if (((AliMUONTrackParam*) track->GetTrackParamAtCluster()->First())->GetClusterPtr()->GetChamberId() > 5)
+       Fit(*track, kFALSE, kTRUE, kTRUE);
       else Fit(*track, kFALSE, kFALSE, kTRUE);
       
-      // Remove the track if the normalized chi2 is too high
-      if (track->GetNormalizedChi2() > sigmaCut2) {
-       fRecTracksPtr->Remove(track);
+      // remove track with absolute bending momentum out of limits
+      // or if the normalized chi2 is too high
+      Double_t bendingMomentum = TMath::Abs(1. / ((AliMUONTrackParam*)track->GetTrackParamAtCluster()->First())->GetInverseBendingMomentum());
+      if (bendingMomentum < GetRecoParam()->GetMinBendingMomentum() ||
+         bendingMomentum > GetRecoParam()->GetMaxBendingMomentum() ||
+         track->GetNormalizedChi2() > sigmaCut2) {
+       fRecTracksPtr->Remove(track);
        fNRecTracks--;
        continue;
       }
       
       // save parameters from fit into smoothed parameters to complete track afterward
-      if (AliMUONReconstructor::GetRecoParam()->ComplementTracks()) {
+      if (GetRecoParam()->ComplementTracks()) {
        
        if (station==2) { // save track parameters on stations 4 and 5
          
@@ -194,26 +270,30 @@ void AliMUONTrackReconstructor::FollowTracks(AliMUONVClusterStore& clusterStore)
          
        } else { // or save track parameters on last station only
          
-         // save parameters from fit
          trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
-         trackParam->SetSmoothParameters(trackParam->GetParameters());
-         trackParam->SetSmoothCovariances(trackParam->GetCovariances());
-         
-         // save parameters extrapolated to the second chamber of the same station if it has been hit
-         nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
-         if (nextTrackParam->GetClusterPtr()->GetChamberId() < 2*(station+2)) {
+         if (trackParam->GetClusterPtr()->GetChamberId() < 2*(station+2)) {
            
-           // reset parameters and covariances
-           nextTrackParam->SetParameters(trackParam->GetParameters());
-           nextTrackParam->SetZ(trackParam->GetZ());
-           nextTrackParam->SetCovariances(trackParam->GetCovariances());
-           
-           // extrapolate them to the z of the corresponding cluster
-           AliMUONTrackExtrap::ExtrapToZCov(nextTrackParam, nextTrackParam->GetClusterPtr()->GetZ());
+           // save parameters from fit
+           trackParam->SetSmoothParameters(trackParam->GetParameters());
+           trackParam->SetSmoothCovariances(trackParam->GetCovariances());
            
-           // save them
-           nextTrackParam->SetSmoothParameters(nextTrackParam->GetParameters());
-           nextTrackParam->SetSmoothCovariances(nextTrackParam->GetCovariances());
+           // save parameters extrapolated to the second chamber of the same station if it has been hit
+           nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
+           if (nextTrackParam->GetClusterPtr()->GetChamberId() < 2*(station+2)) {
+             
+             // reset parameters and covariances
+             nextTrackParam->SetParameters(trackParam->GetParameters());
+             nextTrackParam->SetZ(trackParam->GetZ());
+             nextTrackParam->SetCovariances(trackParam->GetCovariances());
+             
+             // extrapolate them to the z of the corresponding cluster
+             AliMUONTrackExtrap::ExtrapToZCov(nextTrackParam, nextTrackParam->GetClusterPtr()->GetZ());
+             
+             // save them
+             nextTrackParam->SetSmoothParameters(nextTrackParam->GetParameters());
+             nextTrackParam->SetSmoothCovariances(nextTrackParam->GetCovariances());
+             
+           }
            
          }
          
@@ -221,23 +301,32 @@ void AliMUONTrackReconstructor::FollowTracks(AliMUONVClusterStore& clusterStore)
        
       }
       
-      // Printout for debuging
-      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
-        cout<<endl<<"Track parameter covariances at first cluster:"<<endl;
-        ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->First())->GetCovariances().Print();
-      }
-      
       // Look for compatible cluster(s) in station(0..) "station"
-      clusterFound = FollowTrackInStation(*track, clusterStore, station);
-      
-      // Try to recover track if required
-      if (!clusterFound && AliMUONReconstructor::GetRecoParam()->RecoverTracks())
-       clusterFound = RecoverTrack(*track, clusterStore, station);
-      
-      // remove track if no cluster found
-      if (!clusterFound) {
-       fRecTracksPtr->Remove(track);
-       fNRecTracks--;
+      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--;
+       } 
+       
       }
       
     }
@@ -246,7 +335,7 @@ void AliMUONTrackReconstructor::FollowTracks(AliMUONVClusterStore& clusterStore)
     fRecTracksPtr->Compress();
     
     // Keep only the best tracks if required
-    if (!AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
+    if (!GetRecoParam()->TrackAllTracks()) RemoveDoubleTracks();
     
   }
   
@@ -271,31 +360,37 @@ void AliMUONTrackReconstructor::FollowTracks(AliMUONVClusterStore& clusterStore)
     if (track->GetNormalizedChi2() > sigmaCut2) {
       fRecTracksPtr->Remove(track);
       fNRecTracks--;
+      track = nextTrack;
+      continue;
     }
     
     // save parameters from fit into smoothed parameters to complete track afterward
-    if (AliMUONReconstructor::GetRecoParam()->ComplementTracks()) {
+    if (GetRecoParam()->ComplementTracks()) {
       
-      // save parameters from fit
       trackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
-      trackParam->SetSmoothParameters(trackParam->GetParameters());
-      trackParam->SetSmoothCovariances(trackParam->GetCovariances());
-      
-      // save parameters extrapolated to the second chamber of the same station if it has been hit
-      nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
-      if (nextTrackParam->GetClusterPtr()->GetChamberId() < 2) {
+      if (trackParam->GetClusterPtr()->GetChamberId() < 2) {
        
-       // reset parameters and covariances
-       nextTrackParam->SetParameters(trackParam->GetParameters());
-       nextTrackParam->SetZ(trackParam->GetZ());
-       nextTrackParam->SetCovariances(trackParam->GetCovariances());
+       // save parameters from fit
+       trackParam->SetSmoothParameters(trackParam->GetParameters());
+       trackParam->SetSmoothCovariances(trackParam->GetCovariances());
        
-       // extrapolate them to the z of the corresponding cluster
-       AliMUONTrackExtrap::ExtrapToZCov(nextTrackParam, nextTrackParam->GetClusterPtr()->GetZ());
-       
-       // save them
-       nextTrackParam->SetSmoothParameters(nextTrackParam->GetParameters());
-       nextTrackParam->SetSmoothCovariances(nextTrackParam->GetCovariances());
+       // save parameters extrapolated to the second chamber of the same station if it has been hit
+       nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParam);
+       if (nextTrackParam->GetClusterPtr()->GetChamberId() < 2) {
+         
+         // reset parameters and covariances
+         nextTrackParam->SetParameters(trackParam->GetParameters());
+         nextTrackParam->SetZ(trackParam->GetZ());
+         nextTrackParam->SetCovariances(trackParam->GetCovariances());
+         
+         // extrapolate them to the z of the corresponding cluster
+         AliMUONTrackExtrap::ExtrapToZCov(nextTrackParam, nextTrackParam->GetClusterPtr()->GetZ());
+         
+         // save them
+         nextTrackParam->SetSmoothParameters(nextTrackParam->GetParameters());
+         nextTrackParam->SetSmoothCovariances(nextTrackParam->GetCovariances());
+         
+       }
        
       }
       
@@ -307,6 +402,144 @@ void AliMUONTrackReconstructor::FollowTracks(AliMUONVClusterStore& clusterStore)
   
   fRecTracksPtr->Compress();
   
+}
+
+  //__________________________________________________________________________
+Bool_t AliMUONTrackReconstructor::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.
+  AliDebug(1,Form("Enter FollowTrackInChamber(1..) %d", nextChamber+1));
+  
+  Double_t chi2WithOneCluster = 1.e10;
+  Double_t maxChi2WithOneCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
+                                       GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
+  Double_t bestChi2WithOneCluster = maxChi2WithOneCluster;
+  Bool_t foundOneCluster = kFALSE;
+  AliMUONTrack *newTrack = 0x0;
+  AliMUONVCluster *cluster;
+  AliMUONTrackParam extrapTrackParam;
+  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","AliMUONTrackReconstructor") >= 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.);
+  
+  // 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));
+    // add MCS effect
+    AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
+  }
+  
+  //Extrapolate trackCandidate to chamber
+  AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(nextChamber));
+  
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 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","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+    cout << "FollowTrackInStation: 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 cluster 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
+    chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCh, cluster, extrapTrackParamAtCluster);
+    
+    // if good chi2 then consider to add cluster
+    if (chi2WithOneCluster < maxChi2WithOneCluster) {
+      foundOneCluster = kTRUE;
+      
+      // Printout for debuging
+      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+       cout << "FollowTrackInStation: found one cluster in chamber(1..): " << nextChamber+1
+       << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
+       cluster->Print();
+      }
+      
+      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);
+       fNRecTracks++;
+       
+       // Printout for debuging
+       if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+         cout << "FollowTrackInStation: added one cluster in chamber(1..): " << nextChamber+1 << endl;
+         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
+       }
+       
+      } else if (chi2WithOneCluster < bestChi2WithOneCluster) {
+       // keep track of the best single cluster except if a couple of clusters has already been found
+       bestChi2WithOneCluster = chi2WithOneCluster;
+       bestTrackParamAtCluster = extrapTrackParamAtCluster;
+      }
+      
+    }
+    
+  }
+  
+  // fill out the best track if required else clean up the fRecTracksPtr array
+  if (!GetRecoParam()->TrackAllTracks()) {
+    if (foundOneCluster) {
+      UpdateTrack(trackCandidate,bestTrackParamAtCluster);
+      
+      // Printout for debuging
+      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+        cout << "FollowTrackInStation: added the best cluster in chamber(1..): " << bestTrackParamAtCluster.GetClusterPtr()->GetChamberId()+1 << endl;
+        if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
+      }
+      
+    } else return kFALSE;
+    
+  } else if (foundOneCluster) {
+    
+    // remove obsolete track
+    fRecTracksPtr->Remove(&trackCandidate);
+    fNRecTracks--;
+    
+  } else return kFALSE;
+  
+  return kTRUE;
+  
 }
 
   //__________________________________________________________________________
@@ -334,10 +567,10 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
   
   Double_t chi2WithOneCluster = 1.e10;
   Double_t chi2WithTwoClusters = 1.e10;
-  Double_t maxChi2WithOneCluster = 2. * AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
-                                       AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
-  Double_t maxChi2WithTwoClusters = 4. * AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
-                                        AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking(); // 4 because 4 quantities in chi2
+  Double_t maxChi2WithOneCluster = 2. * GetRecoParam()->GetSigmaCutForTracking() *
+                                       GetRecoParam()->GetSigmaCutForTracking(); // 2 because 2 quantities in chi2
+  Double_t maxChi2WithTwoClusters = 4. * GetRecoParam()->GetSigmaCutForTracking() *
+                                        GetRecoParam()->GetSigmaCutForTracking(); // 4 because 4 quantities in chi2
   Double_t bestChi2WithOneCluster = maxChi2WithOneCluster;
   Double_t bestChi2WithTwoClusters = maxChi2WithTwoClusters;
   Bool_t foundOneCluster = kFALSE;
@@ -345,6 +578,7 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
   AliMUONTrack *newTrack = 0x0;
   AliMUONVCluster *clusterCh1, *clusterCh2;
   AliMUONTrackParam extrapTrackParam;
+  AliMUONTrackParam extrapTrackParamAtCh;
   AliMUONTrackParam extrapTrackParamAtCluster1;
   AliMUONTrackParam extrapTrackParamAtCluster2;
   AliMUONTrackParam bestTrackParamAtCluster1;
@@ -355,16 +589,26 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
   for (Int_t i = 0; i < nClusters; i++) clusterCh1Used[i] = kFALSE;
   Int_t iCluster1;
   
-  // Get track parameters
-  AliMUONTrackParam extrapTrackParamAtCh(*(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First());
+  // Get track parameters according to the propagation direction
+  if (nextStation==4) extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
+  else extrapTrackParamAtCh = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
+  
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 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.);
   
-  // Add MCS in the missing chamber if any (only 1 chamber can be missing according to tracking criteria)
-  if (ch1 < ch2 && extrapTrackParamAtCh.GetClusterPtr()->GetChamberId() > 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(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch2 + 1));
+    currentChamber--;
+    AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(currentChamber));
     // add MCS effect
     AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
   }
@@ -374,7 +618,8 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
   
   // Printout for debuging
   if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
-    cout<<endl<<"Track parameter covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch2)<<":"<<endl;
+    cout<<endl<<"Track parameters and covariances at first cluster extrapolated to z = "<<AliMUONConstants::DefaultChamberZ(ch2)<<":"<<endl;
+    extrapTrackParamAtCh.GetParameters().Print();
     extrapTrackParamAtCh.GetCovariances().Print();
   }
   
@@ -385,7 +630,7 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
   
   // 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 (AliMUONReconstructor::GetRecoParam()->CombineClusterTrackReco()) {
+  if (GetRecoParam()->CombineClusterTrackReco()) {
     if (nextStation < 3) AskForNewClustersInStation(extrapTrackParamAtCh, clusterStore, nextStation);
   }
   
@@ -410,6 +655,7 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
       if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
         cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch2+1
             << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
+       clusterCh2->Print();
         cout << "                      look for second clusters in chamber(1..): " << ch1+1 << " ..." << endl;
       }
       
@@ -444,10 +690,11 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
          // Printout for debuging
          if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
            cout << "FollowTrackInStation: found second cluster in chamber(1..): " << ch1+1
-                << " (Global Chi2 = " << chi2WithTwoClusters << ")" << endl;
+                << " (Global Chi2 = " << chi2WithTwoClusters << ")" << endl;
+           clusterCh1->Print();
          }
          
-         if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
+         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,extrapTrackParamAtCluster1,extrapTrackParamAtCluster2);
@@ -477,7 +724,7 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
       if (!foundSecondCluster) {
         foundOneCluster = kTRUE;
         
-       if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
+       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,extrapTrackParamAtCluster2);
@@ -503,12 +750,7 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
   
   // 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 clusters has been found
-  if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
-    
-    // Printout for debuging
-    if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-      cout << "FollowTrackInStation: look for single clusters in chamber(1..): " << ch1+1 << endl;
-    }
+  if (GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
     
     // add MCS effect for next step
     AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtCh,AliMUONConstants::ChamberThicknessInX0(),1.);
@@ -516,6 +758,18 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
     //Extrapolate trackCandidate to chamber "ch1"
     AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParamAtCh, AliMUONConstants::DefaultChamberZ(ch1));
     
+    // Printout for debuging
+    if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 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","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+      cout << "FollowTrackInStation: look for single clusters in chamber(1..): " << ch1+1 << endl;
+    }
+    
     // reset cluster iterator of chamber 1
     nextInCh1.Reset();
     iCluster1 = -1;
@@ -531,19 +785,20 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
       
       // try to add the current cluster accuratly
       chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCh, clusterCh1, extrapTrackParamAtCluster1);
-    
+      
       // 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 (chi2WithOneCluster < maxChi2WithOneCluster) {
         foundOneCluster = kTRUE;
-         
+       
        // Printout for debuging
        if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
          cout << "FollowTrackInStation: found one cluster in chamber(1..): " << ch1+1
               << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
+         clusterCh1->Print();
        }
        
-       if (AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
+       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,extrapTrackParamAtCluster1);
@@ -568,7 +823,7 @@ Bool_t AliMUONTrackReconstructor::FollowTrackInStation(AliMUONTrack &trackCandid
   }
   
   // fill out the best track if required else clean up the fRecTracksPtr array
-  if (!AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
+  if (!GetRecoParam()->TrackAllTracks()) {
     if (foundTwoClusters) {
       UpdateTrack(trackCandidate,bestTrackParamAtCluster1,bestTrackParamAtCluster2);
       
@@ -726,7 +981,9 @@ void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackPar
                       deltaY*deltaY / cluster->GetErrY2();
   
   // Flag cluster as being not removable
-  trackParamAtCluster.SetRemovable(kFALSE);
+  if (GetRecoParam()->RequestStation(cluster->GetChamberId()/2))
+    trackParamAtCluster.SetRemovable(kFALSE);
+  else trackParamAtCluster.SetRemovable(kTRUE);
   trackParamAtCluster.SetLocalChi2(0.); // --> Local chi2 not used
   
   // Update the chi2 of the new track
@@ -734,7 +991,6 @@ void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackPar
   
   // Update TrackParamAtCluster
   track.AddTrackParamAtCluster(trackParamAtCluster,*cluster);
-  track.GetTrackParamAtCluster()->Sort();
   
 }
 
@@ -772,7 +1028,6 @@ void AliMUONTrackReconstructor::UpdateTrack(AliMUONTrack &track, AliMUONTrackPar
   // Update TrackParamAtCluster
   track.AddTrackParamAtCluster(trackParamAtCluster1,*cluster1);
   track.AddTrackParamAtCluster(trackParamAtCluster2,*cluster2);
-  track.GetTrackParamAtCluster()->Sort();
   
 }
 
@@ -788,25 +1043,32 @@ Bool_t AliMUONTrackReconstructor::RecoverTrack(AliMUONTrack &trackCandidate, Ali
   if (nextStation > 1) return kFALSE;
   
   Int_t worstClusterNumber = -1;
-  Double_t localChi2, worstLocalChi2 = 0.;
+  Double_t localChi2, worstLocalChi2 = -1.;
   
   // 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;
     
+    // reset the current cluster as beig not removable if it is on a required station
+    if (GetRecoParam()->RequestStation(nextStation+1)) trackParamAtCluster->SetRemovable(kFALSE);
+    
     // Pick up cluster with the worst chi2
     localChi2 = trackParamAtCluster->GetLocalChi2();
     if (localChi2 > worstLocalChi2) {
       worstLocalChi2 = localChi2;
       worstClusterNumber = clusterNumber;
     }
+    
   }
   
-  // Reset best cluster as being NOT removable
-  ((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt((worstClusterNumber+1)%2))->SetRemovable(kFALSE);
+  // check if worst cluster found
+  if (worstClusterNumber < 0) return kFALSE;
   
   // Remove the worst cluster
   trackCandidate.RemoveTrackParamAtCluster((AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->UncheckedAt(worstClusterNumber));
@@ -830,10 +1092,10 @@ void AliMUONTrackReconstructor::SetVertexErrXY2ForFit(AliMUONTrack &trackCandida
   /// the "trackCandidate" to do not influence the result by changing track resolution at vertex
   AliDebug(1,"Enter SetVertexForFit");
   
-  Double_t nonBendingReso2 = AliMUONReconstructor::GetRecoParam()->GetNonBendingVertexDispersion() *
-                             AliMUONReconstructor::GetRecoParam()->GetNonBendingVertexDispersion();
-  Double_t bendingReso2 = AliMUONReconstructor::GetRecoParam()->GetBendingVertexDispersion() *
-                         AliMUONReconstructor::GetRecoParam()->GetBendingVertexDispersion();
+  Double_t nonBendingReso2 = GetRecoParam()->GetNonBendingVertexDispersion() *
+                             GetRecoParam()->GetNonBendingVertexDispersion();
+  Double_t bendingReso2 = GetRecoParam()->GetBendingVertexDispersion() *
+                         GetRecoParam()->GetBendingVertexDispersion();
   
   // add multiple scattering effets
   AliMUONTrackParam paramAtVertex(*((AliMUONTrackParam*)(trackCandidate.GetTrackParamAtCluster()->First())));
@@ -854,6 +1116,7 @@ void AliMUONTrackReconstructor::Fit(AliMUONTrack &track, Bool_t includeMCS, Bool
   /// w/wo multiple Coulomb scattering according to "includeMCS".
   /// w/wo constraining the vertex according to "fitWithVertex".
   /// calculating or not the covariance matrix according to "calcCov".
+  AliDebug(1,"Enter Fit");
   
   Double_t benC, errorParam, invBenP, nonBenC, x, y;
   AliMUONTrackParam *trackParam;
@@ -900,15 +1163,14 @@ void AliMUONTrackReconstructor::Fit(AliMUONTrack &track, Bool_t includeMCS, Bool
   gMinuit->SetFCN(TrackChi2);
   
   // Set fitted parameters (!! The order is very important for the covariance matrix !!)
+  // Mandatory limits to avoid NaN values of parameters
   trackParam = (AliMUONTrackParam*) (track.GetTrackParamAtCluster()->First());
-  // could be tried with no limits for the search (min=max=0) ????
-  // mandatory limits in non Bending to avoid NaN values of parameters
+  Double_t maxIBM = 1. / GetRecoParam()->GetMinBendingMomentum();
   gMinuit->mnparm(0, "X", trackParam->GetNonBendingCoor(), 0.03, -500.0, 500.0, status);
-  gMinuit->mnparm(1, "NonBenS", trackParam->GetNonBendingSlope(), 0.001, -0.5, 0.5, status);
-  // mandatory limits in Bending to avoid NaN values of parameters
+  gMinuit->mnparm(1, "NonBenS", trackParam->GetNonBendingSlope(), 0.001, -1., 1., status);
   gMinuit->mnparm(2, "Y", trackParam->GetBendingCoor(), 0.10, -500.0, 500.0, status);
-  gMinuit->mnparm(3, "BenS", trackParam->GetBendingSlope(), 0.001, -0.5, 0.5, status);
-  gMinuit->mnparm(4, "InvBenP", trackParam->GetInverseBendingMomentum(), 0.003, -0.4, 0.4, status);
+  gMinuit->mnparm(3, "BenS", trackParam->GetBendingSlope(), 0.001, -1.5, 1.5, status);
+  gMinuit->mnparm(4, "InvBenP", trackParam->GetInverseBendingMomentum(), 0.003, -maxIBM, maxIBM, status);
   
   // minimization
   gMinuit->mnexcm("MIGRAD", arg, 0, status);
@@ -995,15 +1257,12 @@ void AliMUONTrackReconstructor::ComplementTracks(const AliMUONVClusterStore& clu
   
   Int_t chamberId, detElemId;
   Double_t chi2OfCluster, bestChi2OfCluster;
-  Double_t sigmaCut2 = AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking() *
-                       AliMUONReconstructor::GetRecoParam()->GetSigmaCutForTracking();
+  Double_t sigmaCut2 = GetRecoParam()->GetSigmaCutForTracking() *
+                       GetRecoParam()->GetSigmaCutForTracking();
   Bool_t foundOneCluster, trackModified;
   AliMUONVCluster* cluster;
   AliMUONTrackParam *trackParam, *nextTrackParam, copyOfTrackParam, trackParamAtCluster, bestTrackParamAtCluster;
   
-  // Remove double track to complete only "good" tracks
-  RemoveDoubleTracks();
-  
   AliMUONTrack *track = (AliMUONTrack*) fRecTracksPtr->First();
   while (track) {
     trackModified = kFALSE;
@@ -1049,9 +1308,19 @@ void AliMUONTrackReconstructor::ComplementTracks(const AliMUONVClusterStore& clu
       
       // add new cluster if any
       if (foundOneCluster) {
-       UpdateTrack(*track,bestTrackParamAtCluster);
-       bestTrackParamAtCluster.SetAloneInChamber(kFALSE);
-       trackParam->SetAloneInChamber(kFALSE);
+       
+       // Printout for debuging
+       if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+         cout << "ComplementTracks: found one cluster in chamber(1..): " << chamberId+1 << endl;
+         bestTrackParamAtCluster.GetClusterPtr()->Print();
+         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;
       }
       
@@ -1067,152 +1336,120 @@ void AliMUONTrackReconstructor::ComplementTracks(const AliMUONVClusterStore& clu
 }
 
   //__________________________________________________________________________
-void AliMUONTrackReconstructor::ImproveTracks()
+void AliMUONTrackReconstructor::ImproveTrack(AliMUONTrack &track)
 {
-  /// Improve tracks by removing clusters with local chi2 highter than the defined cut
+  /// Improve the given track by removing clusters with local chi2 highter than the defined cut
   /// Recompute track parameters and covariances at the remaining clusters
-  AliDebug(1,"Enter ImproveTracks");
+  AliDebug(1,"Enter ImproveTrack");
   
   Double_t localChi2, worstLocalChi2;
-  Int_t worstChamber, previousChamber;
-  AliMUONTrack *track, *nextTrack;
-  AliMUONTrackParam *trackParamAtCluster, *worstTrackParamAtCluster, *previousTrackParam, *nextTrackParam;
-  Double_t sigmaCut2 = AliMUONReconstructor::GetRecoParam()->GetSigmaCutForImprovement() *
-                      AliMUONReconstructor::GetRecoParam()->GetSigmaCutForImprovement();
+  AliMUONTrackParam *trackParamAtCluster, *worstTrackParamAtCluster;
+  Double_t sigmaCut2 = GetRecoParam()->GetSigmaCutForImprovement() *
+                      GetRecoParam()->GetSigmaCutForImprovement();
   
-  // Remove double track to improve only "good" tracks
-  RemoveDoubleTracks();
-  
-  track = (AliMUONTrack*) fRecTracksPtr->First();
-  while (track) {
+  while (!track.IsImproved()) {
     
-    // prepare next track in case the actual track is suppressed
-    nextTrack = (AliMUONTrack*) fRecTracksPtr->After(track);
+    // identify removable clusters
+    track.TagRemovableClusters(GetRecoParam()->RequestedStationMask());
     
-    while (!track->IsImproved()) {
-      
-      // Update track parameters and covariances
-      track->UpdateCovTrackParamAtCluster();
-      
-      // Compute local chi2 of each clusters
-      track->ComputeLocalChi2(kTRUE);
-      
-      // Look for the cluster to remove
-      worstTrackParamAtCluster = NULL;
-      worstLocalChi2 = 0.;
-      trackParamAtCluster = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
-      while (trackParamAtCluster) {
-        
-        // Pick up cluster with the worst chi2
-        localChi2 = trackParamAtCluster->GetLocalChi2();
-        if (localChi2 > worstLocalChi2) {
-          worstLocalChi2 = localChi2;
-          worstTrackParamAtCluster = trackParamAtCluster;
-        }
-        
-      trackParamAtCluster = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(trackParamAtCluster);
-      }
-      
-      // Check if bad cluster found
-      if (!worstTrackParamAtCluster) {
-        track->SetImproved(kTRUE);
-        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 remove the entire track
-      if (!worstTrackParamAtCluster->IsRemovable() && worstTrackParamAtCluster->IsAloneInChamber()) {
-       fRecTracksPtr->Remove(track);
-       fNRecTracks--;
-        break;
-      }
-      
-      // Reset the second cluster in the same station as being not removable
-      // or reset the second cluster in the same chamber as being alone
-      worstChamber = worstTrackParamAtCluster->GetClusterPtr()->GetChamberId();
-      previousTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->Before(worstTrackParamAtCluster);
-      nextTrackParam = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(worstTrackParamAtCluster);
-      if (worstTrackParamAtCluster->IsAloneInChamber()) { // Worst cluster removable and alone in chamber
-       
-       if (worstChamber%2 == 0) { // Modify flags in next chamber
-         
-         nextTrackParam->SetRemovable(kFALSE);
-         if (!nextTrackParam->IsAloneInChamber()) // Make sure both clusters in second chamber are not removable anymore
-           ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->After(nextTrackParam))->SetRemovable(kFALSE);
-         
-       } else { // Modify flags in previous chamber
-         
-         previousTrackParam->SetRemovable(kFALSE);
-         if (!previousTrackParam->IsAloneInChamber()) // Make sure both clusters in second chamber are not removable anymore
-           ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->Before(previousTrackParam))->SetRemovable(kFALSE);
-         
-       }
-       
-      } else { // Worst cluster not alone in its chamber
-        
-       if (previousTrackParam) previousChamber = previousTrackParam->GetClusterPtr()->GetChamberId();
-       else previousChamber = -1;
-       
-       if (previousChamber == worstChamber) { // the second cluster on the same chamber is the previous one
-         
-         previousTrackParam->SetAloneInChamber(kTRUE);
-         // transfert the removability to the second cluster
-         if (worstTrackParamAtCluster->IsRemovable()) previousTrackParam->SetRemovable(kTRUE);
-         
-       } else { // the second cluster on the same chamber is the next one
-         
-         nextTrackParam->SetAloneInChamber(kTRUE);
-         // transfert the removability to the second cluster
-         if (worstTrackParamAtCluster->IsRemovable()) nextTrackParam->SetRemovable(kTRUE);
-         
-       }
-       
-      }
-      
-      // Remove the worst cluster
-      track->RemoveTrackParamAtCluster(worstTrackParamAtCluster);
-      
-      // Re-fit the track:
-      // Take into account the multiple scattering
-      // Calculate the track parameter covariance matrix
-      Fit(*track, kTRUE, kFALSE, kTRUE);
+    // Update track parameters and covariances
+    track.UpdateCovTrackParamAtCluster();
+    
+    // Compute local chi2 of each clusters
+    track.ComputeLocalChi2(kTRUE);
+    
+    // Look for the cluster to remove
+    worstTrackParamAtCluster = NULL;
+    worstLocalChi2 = 0.;
+    trackParamAtCluster = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->First();
+    while (trackParamAtCluster) {
       
-      // Printout for debuging
-      if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
-        cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(track)+1 << " improved " << endl;
+      // Pick up cluster with the worst chi2
+      localChi2 = trackParamAtCluster->GetLocalChi2();
+      if (localChi2 > worstLocalChi2) {
+       worstLocalChi2 = localChi2;
+       worstTrackParamAtCluster = trackParamAtCluster;
       }
       
+    trackParamAtCluster = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->After(trackParamAtCluster);
+    }
+    
+    // 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;
+    
+    // Remove the worst cluster
+    track.RemoveTrackParamAtCluster(worstTrackParamAtCluster);
+    
+    // Re-fit the track:
+    // Take into account the multiple scattering
+    // Calculate the track parameter covariance matrix
+    Fit(track, kTRUE, kFALSE, kTRUE);
+    
+    // Printout for debuging
+    if ((AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+      cout << "ImproveTracks: track " << fRecTracksPtr->IndexOf(&track)+1 << " improved " << endl;
     }
     
-    track = nextTrack;
   }
   
-  // compress the array in case of some tracks have been removed
-  fRecTracksPtr->Compress();
-  
 }
 
   //__________________________________________________________________________
-void AliMUONTrackReconstructor::Finalize()
+void AliMUONTrackReconstructor::FinalizeTrack(AliMUONTrack &track)
+{
+  /// Recompute track parameters and covariances at each attached cluster
+  /// from those at the first one, if not already done
+  AliDebug(1,"Enter FinalizeTrack");
+  if (!track.IsImproved()) track.UpdateCovTrackParamAtCluster();
+}
+
+  //__________________________________________________________________________
+Bool_t AliMUONTrackReconstructor::RefitTrack(AliMUONTrack &track, Bool_t enableImprovement)
 {
-  /// Recompute track parameters and covariances at each attached cluster from those at the first one
+  /// re-fit the given track
   
-  AliMUONTrack *track;
+  // check validity of the track
+  if (track.GetNClusters() < 3) {
+    AliWarning("the track does not contain enough clusters --> unable to refit");
+    return kFALSE;
+  }
   
-  track = (AliMUONTrack*) fRecTracksPtr->First();
-  while (track) {
-    
-    // update track parameters if not already done
-    if (!track->IsImproved()) track->UpdateCovTrackParamAtCluster();
-    
-    track = (AliMUONTrack*) fRecTracksPtr->After(track);
-    
+  // reset the seed (i.e. parameters at first cluster) before fitting
+  AliMUONTrackParam* firstTrackParam = (AliMUONTrackParam*) track.GetTrackParamAtCluster()->First();
+  if (firstTrackParam->GetInverseBendingMomentum() == 0.) {
+    AliWarning("track parameters at first chamber are not initialized --> unable to refit");
+    return kFALSE;
   }
   
+  // compute track parameters at each cluster from parameters at the first one
+  // necessary to compute multiple scattering effect during refitting
+  track.UpdateTrackParamAtCluster();
+  
+  // Re-fit the track:
+  // Take into account the multiple scattering
+  // Calculate the track parameter covariance matrix
+  Fit(track, kTRUE, kFALSE, kTRUE);
+  
+  // Improve the reconstructed tracks if required
+  track.SetImproved(kFALSE);
+  if (enableImprovement && GetRecoParam()->ImproveTracks()) ImproveTrack(track);
+  
+  // Fill AliMUONTrack data members
+  FinalizeTrack(track);
+  
+  return kTRUE;
+  
 }