]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONVTrackReconstructor.cxx
Filling residuals (GlobalQA) only for tracks with kTPCin
[u/mrichter/AliRoot.git] / MUON / AliMUONVTrackReconstructor.cxx
index 4b0e24ea2f84c6a7f9565827ee3612fb6bc41130..6c0d15da0bee72f38b3d9e5e1d09433626d0bc9c 100644 (file)
 /// * ValidateTracksWithTrigger to match tracker/trigger tracks
 ///
 /// Several options and adjustable parameters are available for both KALMAN and ORIGINAL
-/// tracking algorithms. They can be changed by using:
-/// AliMUONRecoParam *muonRecoParam = AliMUONRecoParam::GetLow(High)FluxParam();
-/// muonRecoParam->Set...(); // see methods in AliMUONRecoParam.h for details
-/// AliMUONReconstructor::SetRecoParam(muonRecoParam);
+/// tracking algorithms. They can be changed through the AliMUONRecoParam object
+/// set in the reconstruction macro or read from the CDB
+/// (see methods in AliMUONRecoParam.h file for details)
 ///
 /// Main parameters and options are:
 /// - *fgkSigmaToCutForTracking* : quality cut used to select new clusters to be
@@ -74,6 +73,8 @@
 #include "AliMUONVClusterServer.h"
 #include "AliMUONVTriggerStore.h"
 #include "AliMUONVTriggerTrackStore.h"
+#include "AliMUONRecoParam.h"
+
 #include "AliMpDEManager.h"
 #include "AliMpArea.h"
 
@@ -93,21 +94,22 @@ ClassImp(AliMUONVTrackReconstructor) // Class implementation in ROOT context
 /// \endcond
 
   //__________________________________________________________________________
-AliMUONVTrackReconstructor::AliMUONVTrackReconstructor(AliMUONVClusterServer& clusterServer)
-  : TObject(),
-    fRecTracksPtr(0x0),
-    fNRecTracks(0),
-  fClusterServer(clusterServer)
+AliMUONVTrackReconstructor::AliMUONVTrackReconstructor(const AliMUONRecoParam* recoParam,
+                                                       AliMUONVClusterServer* clusterServer)
+: TObject(),
+fRecTracksPtr(0x0),
+fNRecTracks(0),
+fClusterServer(clusterServer),
+fkRecoParam(recoParam)
 {
   /// Constructor for class AliMUONVTrackReconstructor
+  /// WARNING: if clusterServer=0x0, no clusterization will be possible at this level
   
   // Memory allocation for the TClonesArray of reconstructed tracks
   fRecTracksPtr = new TClonesArray("AliMUONTrack", 100);
   
   // set the magnetic field for track extrapolations
-  const AliMagF* kField = AliTracker::GetFieldMap();
-  if (!kField) AliFatal("No field available");
-  AliMUONTrackExtrap::SetField(kField);
+  AliMUONTrackExtrap::SetField();
 }
 
   //__________________________________________________________________________
@@ -136,23 +138,31 @@ void AliMUONVTrackReconstructor::EventReconstruct(AliMUONVClusterStore& clusterS
   // Reset array of tracks
   ResetTracks();
   
-  // Look for candidates from at least 3 aligned points in stations(1..) 4 and 5
-  MakeTrackCandidates(clusterStore);
+  // Look for candidates from clusters in stations(1..) 4 and 5 (abort in case of failure)
+  if (!MakeTrackCandidates(clusterStore)) return;
+  
+  // Look for extra candidates from clusters in stations(1..) 4 and 5 (abort in case of failure)
+  if (GetRecoParam()->MakeMoreTrackCandidates()) {
+    if (!MakeMoreTrackCandidates(clusterStore)) return;
+  }
   
   // Stop tracking if no candidate found
   if (fRecTracksPtr->GetEntriesFast() == 0) return;
   
-  // Follow tracks in stations(1..) 3, 2 and 1
-  FollowTracks(clusterStore);
+  // Follow tracks in stations(1..) 3, 2 and 1 (abort in case of failure)
+  if (!FollowTracks(clusterStore)) return;
   
   // Complement the reconstructed tracks
-  if (AliMUONReconstructor::GetRecoParam()->ComplementTracks()) ComplementTracks(clusterStore);
+  if (GetRecoParam()->ComplementTracks()) {
+    if (ComplementTracks(clusterStore)) RemoveIdenticalTracks();
+  }
   
   // Improve the reconstructed tracks
-  if (AliMUONReconstructor::GetRecoParam()->ImproveTracks()) ImproveTracks();
+  if (GetRecoParam()->ImproveTracks()) ImproveTracks();
   
-  // Remove double tracks
-  RemoveDoubleTracks();
+  // Remove connected tracks
+  if (GetRecoParam()->RemoveConnectedTracksInSt12()) RemoveConnectedTracks(kFALSE);
+  else RemoveConnectedTracks(kTRUE);
   
   // Fill AliMUONTrack data members
   Finalize();
@@ -167,18 +177,16 @@ void AliMUONVTrackReconstructor::EventReconstruct(AliMUONVClusterStore& clusterS
 }
 
   //__________________________________________________________________________
-TClonesArray* AliMUONVTrackReconstructor::MakeSegmentsInStation(const AliMUONVClusterStore& clusterStore, Int_t station)
+TClonesArray* AliMUONVTrackReconstructor::MakeSegmentsBetweenChambers(const AliMUONVClusterStore& clusterStore, Int_t ch1, Int_t ch2)
 {
-  /// To make the list of segments in station(0..) "Station" from the list of clusters to be reconstructed.
+  /// To make the list of segments from the list of clusters in the 2 given chambers.
   /// Return a new TClonesArray of segments.
   /// It is the responsibility of the user to delete it afterward.
-  AliDebug(1,Form("Enter MakeSegmentsPerStation (1..) %d",station+1));
+  AliDebug(1,Form("Enter MakeSegmentsBetweenChambers (1..) %d-%d", ch1+1, ch2+1));
   
   AliMUONVCluster *cluster1, *cluster2;
   AliMUONObjectPair *segment;
-  Double_t bendingSlope = 0, impactParam = 0., bendingMomentum = 0.; // to avoid compilation warning
-  Int_t ch1 = 2 * station;
-  Int_t ch2 = ch1 + 1;
+  Double_t nonBendingSlope = 0, bendingSlope = 0, impactParam = 0., bendingMomentum = 0.; // to avoid compilation warning
   
   // Create iterators to loop over clusters in both chambers
   TIter nextInCh1(clusterStore.CreateChamberIterator(ch1,ch1));
@@ -196,32 +204,45 @@ TClonesArray* AliMUONVTrackReconstructor::MakeSegmentsInStation(const AliMUONVCl
     // Loop over clusters in the second chamber of the station
     while ( ( cluster2 = static_cast<AliMUONVCluster*>(nextInCh2()) ) ) {
       
+      // non bending slope
+      nonBendingSlope = (cluster1->GetX() - cluster2->GetX()) / (cluster1->GetZ() - cluster2->GetZ());
+      
+      // check if non bending slope is within tolerances
+      if (TMath::Abs(nonBendingSlope) > GetRecoParam()->GetMaxNonBendingSlope()) continue;
+      
       // bending slope
       bendingSlope = (cluster1->GetY() - cluster2->GetY()) / (cluster1->GetZ() - cluster2->GetZ());
       
-      // impact parameter
-      impactParam = cluster1->GetY() - cluster1->GetZ() * bendingSlope;
-     
-      // absolute value of bending momentum
-      bendingMomentum = TMath::Abs(AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(impactParam));
-      
-      // check for bending momentum within tolerances
-      if ((bendingMomentum < AliMUONReconstructor::GetRecoParam()->GetMaxBendingMomentum()) &&
-         (bendingMomentum > AliMUONReconstructor::GetRecoParam()->GetMinBendingMomentum())) {
-        
-       // make new segment
-        segment = new ((*segments)[segments->GetLast()+1]) AliMUONObjectPair(cluster1, cluster2, kFALSE, kFALSE);
-        
-       // Printout for debug
-       if (AliLog::GetGlobalDebugLevel() > 1) {
-          cout << "segmentIndex(0...): " << segments->GetLast() << endl;
-          segment->Dump();
-          cout << "Cluster in first chamber" << endl;
-          cluster1->Print();
-          cout << "Cluster in second chamber" << endl;
-          cluster2->Print();
-        }
+      // check the bending momentum of the bending slope depending if the field is ON or OFF
+      if (AliMUONTrackExtrap::IsFieldON()) {
+       
+       // impact parameter
+       impactParam = cluster1->GetY() - cluster1->GetZ() * bendingSlope;
+       
+       // absolute value of bending momentum
+       bendingMomentum = TMath::Abs(AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(impactParam));
        
+       // check if bending momentum is within tolerances
+       if (bendingMomentum < GetRecoParam()->GetMinBendingMomentum() ||
+           bendingMomentum > GetRecoParam()->GetMaxBendingMomentum()) continue;
+       
+      } else {
+       
+       // check if non bending slope is within tolerances
+       if (TMath::Abs(bendingSlope) > GetRecoParam()->GetMaxBendingSlope()) continue;
+      
+      }
+      // make new segment
+      segment = new ((*segments)[segments->GetLast()+1]) AliMUONObjectPair(cluster1, cluster2, kFALSE, kFALSE);
+      
+      // Printout for debug
+      if (AliLog::GetGlobalDebugLevel() > 1) {
+       cout << "segmentIndex(0...): " << segments->GetLast() << endl;
+       segment->Dump();
+       cout << "Cluster in first chamber" << endl;
+       cluster1->Print();
+       cout << "Cluster in second chamber" << endl;
+       cluster2->Print();
       }
       
     }
@@ -229,28 +250,91 @@ TClonesArray* AliMUONVTrackReconstructor::MakeSegmentsInStation(const AliMUONVCl
   }
   
   // Printout for debug
-  AliDebug(1,Form("Station: %d  NSegments:  %d ", station+1, segments->GetEntriesFast()));
+  AliDebug(1,Form("chambers%d-%d: NSegments =  %d ", ch1+1, ch2+1, segments->GetEntriesFast()));
   
   return segments;
 }
 
+  //__________________________________________________________________________
+void AliMUONVTrackReconstructor::RemoveUsedSegments(TClonesArray& segments)
+{
+  /// To remove pairs of clusters already attached to a track
+  AliDebug(1,"Enter RemoveUsedSegments");
+  Int_t nSegments = segments.GetEntriesFast();
+  Int_t nTracks = fRecTracksPtr->GetEntriesFast();
+  AliMUONObjectPair *segment;
+  AliMUONTrack *track;
+  AliMUONVCluster *cluster, *cluster1, *cluster2;
+  Bool_t foundCluster1, foundCluster2, removeSegment;
+  
+  // Loop over segments
+  for (Int_t iSegment=0; iSegment<nSegments; iSegment++) {
+    segment = (AliMUONObjectPair*) segments.UncheckedAt(iSegment);
+    
+    cluster1 = (AliMUONVCluster*) segment->First();
+    cluster2 = (AliMUONVCluster*) segment->Second();
+    removeSegment = kFALSE;
+    
+    // Loop over tracks
+    for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
+      track = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iTrack);
+      
+      // skip empty slot
+      if (!track) continue;
+      
+      foundCluster1 = kFALSE;
+      foundCluster2 = kFALSE;
+      
+      // Loop over clusters
+      Int_t nClusters = track->GetNClusters();
+      for (Int_t iCluster = 0; iCluster < nClusters; iCluster++) {
+        cluster = ((AliMUONTrackParam*) track->GetTrackParamAtCluster()->UncheckedAt(iCluster))->GetClusterPtr();
+       
+       // check if both clusters are in that track
+       if (cluster == cluster1) foundCluster1 = kTRUE;
+       else if (cluster == cluster2) foundCluster2 = kTRUE;
+       
+       if (foundCluster1 && foundCluster2) {
+         removeSegment = kTRUE;
+         break;
+       }
+       
+      }
+      
+      if (removeSegment) break;
+      
+    }
+    
+    if (removeSegment) segments.RemoveAt(iSegment);
+      
+  }
+  
+  segments.Compress();
+  
+  // Printout for debug
+  AliDebug(1,Form("NSegments =  %d ", segments.GetEntriesFast()));
+}
+
   //__________________________________________________________________________
 void AliMUONVTrackReconstructor::RemoveIdenticalTracks()
 {
   /// To remove identical tracks:
   /// Tracks are considered identical if they have all their clusters in common.
   /// One keeps the track with the larger number of clusters if need be
-  AliMUONTrack *track1, *track2, *trackToRemove;
+  AliMUONTrack *track1, *track2;
+  Int_t nTracks = fRecTracksPtr->GetEntriesFast();
   Int_t clustersInCommon, nClusters1, nClusters2;
-  Bool_t removedTrack1;
   // Loop over first track of the pair
-  track1 = (AliMUONTrack*) fRecTracksPtr->First();
-  while (track1) {
-    removedTrack1 = kFALSE;
+  for (Int_t iTrack1 = 0; iTrack1 < nTracks; iTrack1++) {
+    track1 = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iTrack1);
+    // skip empty slot
+    if (!track1) continue;
     nClusters1 = track1->GetNClusters();
     // Loop over second track of the pair
-    track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
-    while (track2) {
+    for (Int_t iTrack2 = iTrack1+1; iTrack2 < nTracks; iTrack2++) {
+      track2 = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iTrack2);
+      // skip empty slot
+      if (!track2) continue;
       nClusters2 = track2->GetNClusters();
       // number of clusters in common between two tracks
       clustersInCommon = track1->ClustersInCommon(track2);
@@ -259,27 +343,18 @@ void AliMUONVTrackReconstructor::RemoveIdenticalTracks()
         // decide which track to remove
         if (nClusters2 > nClusters1) {
          // remove track1 and continue the first loop with the track next to track1
-         trackToRemove = track1;
-         track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
-          fRecTracksPtr->Remove(trackToRemove);
-         fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
+          fRecTracksPtr->RemoveAt(iTrack1);
          fNRecTracks--;
-         removedTrack1 = kTRUE;
          break;
        } else {
          // remove track2 and continue the second loop with the track next to track2
-         trackToRemove = track2;
-         track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
-         fRecTracksPtr->Remove(trackToRemove);
-         fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
+         fRecTracksPtr->RemoveAt(iTrack2);
          fNRecTracks--;
         }
-      } else track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
+      }
     } // track2
-    if (removedTrack1) continue;
-    track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
   } // track1
-  return;
+  fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
 }
 
   //__________________________________________________________________________
@@ -290,6 +365,50 @@ void AliMUONVTrackReconstructor::RemoveDoubleTracks()
   /// which has the smaller number of clusters are in common with the other track.
   /// Among two identical tracks, one keeps the track with the larger number of clusters
   /// or, if these numbers are equal, the track with the minimum chi2.
+  AliMUONTrack *track1, *track2;
+  Int_t nTracks = fRecTracksPtr->GetEntriesFast();
+  Int_t clustersInCommon2, nClusters1, nClusters2;
+  // Loop over first track of the pair
+  for (Int_t iTrack1 = 0; iTrack1 < nTracks; iTrack1++) {
+    track1 = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iTrack1);
+    // skip empty slot
+    if (!track1) continue;
+    nClusters1 = track1->GetNClusters();
+    // Loop over second track of the pair
+    for (Int_t iTrack2 = iTrack1+1; iTrack2 < nTracks; iTrack2++) {
+      track2 = (AliMUONTrack*) fRecTracksPtr->UncheckedAt(iTrack2);
+      // skip empty slot
+      if (!track2) continue;
+      nClusters2 = track2->GetNClusters();
+      // number of clusters in common between two tracks
+      clustersInCommon2 = 2 * track1->ClustersInCommon(track2);
+      // check for identical tracks
+      if (clustersInCommon2 > nClusters1 || clustersInCommon2 > nClusters2) {
+        // decide which track to remove
+        if ((nClusters1 > nClusters2) || ((nClusters1 == nClusters2) && (track1->GetGlobalChi2() <= track2->GetGlobalChi2()))) {
+         // remove track2 and continue the second loop with the track next to track2
+         fRecTracksPtr->RemoveAt(iTrack2);
+         fNRecTracks--;
+        } else {
+         // else remove track1 and continue the first loop with the track next to track1
+          fRecTracksPtr->RemoveAt(iTrack1);
+         fNRecTracks--;
+         break;
+        }
+      }
+    } // track2
+  } // track1
+  fRecTracksPtr->Compress(); // this is essential to retrieve the TClonesArray afterwards
+}
+
+  //__________________________________________________________________________
+void AliMUONVTrackReconstructor::RemoveConnectedTracks(Bool_t inSt345)
+{
+  /// To remove double tracks:
+  /// Tracks are considered identical if they share 1 cluster or more.
+  /// If inSt345=kTRUE only stations 3, 4 and 5 are considered.
+  /// Among two identical tracks, one keeps the track with the larger number of clusters
+  /// or, if these numbers are equal, the track with the minimum chi2.
   AliMUONTrack *track1, *track2, *trackToRemove;
   Int_t clustersInCommon, nClusters1, nClusters2;
   Bool_t removedTrack1;
@@ -303,9 +422,10 @@ void AliMUONVTrackReconstructor::RemoveDoubleTracks()
     while (track2) {
       nClusters2 = track2->GetNClusters();
       // number of clusters in common between two tracks
-      clustersInCommon = track1->ClustersInCommon(track2);
+      if (inSt345) clustersInCommon = track1->ClustersInCommonInSt345(track2);
+      else clustersInCommon = track1->ClustersInCommon(track2);
       // check for identical tracks
-      if (((nClusters1 < nClusters2) && (2 * clustersInCommon > nClusters1)) || (2 * clustersInCommon > nClusters2)) {
+      if (clustersInCommon > 0) {
         // decide which track to remove
         if ((nClusters1 > nClusters2) || ((nClusters1 == nClusters2) && (track1->GetGlobalChi2() <= track2->GetGlobalChi2()))) {
          // remove track2 and continue the second loop with the track next to track2
@@ -333,37 +453,49 @@ void AliMUONVTrackReconstructor::RemoveDoubleTracks()
 }
 
   //__________________________________________________________________________
-void AliMUONVTrackReconstructor::AskForNewClustersInStation(const AliMUONTrackParam &trackParam,
-                                                           AliMUONVClusterStore& clusterStore, Int_t station)
+void AliMUONVTrackReconstructor::AskForNewClustersInChamber(const AliMUONTrackParam &trackParam,
+                                                           AliMUONVClusterStore& clusterStore, Int_t chamber)
 {
   /// Ask the clustering to reconstruct new clusters around the track candidate position
-  /// in the 2 chambers of the given station
   
-  // maximum shift of the searching area due to distance between detection elements and the track slope
-  static const Double_t kgMaxShift = 2.; // 2 cm
+  // check if the current chamber is useable
+  if (!fClusterServer || !GetRecoParam()->UseChamber(chamber)) return;
   
-  // extrapolate track parameters to the second chamber of the station
-  AliMUONTrackParam extrapTrackParam(trackParam);
-  AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(2*station+1));
+  // maximum distance between the center of the chamber and a detection element
+  // (accounting for the inclination of the chamber)
+  static const Double_t kMaxDZ = 15.; // 15 cm
   
-  // build the searching area
-  TVector2 position(extrapTrackParam.GetNonBendingCoor(), extrapTrackParam.GetBendingCoor());
-  TVector2 dimensions(AliMUONReconstructor::GetRecoParam()->GetMaxNonBendingDistanceToTrack() + kgMaxShift,
-                     AliMUONReconstructor::GetRecoParam()->GetMaxBendingDistanceToTrack() + kgMaxShift);
-  AliMpArea area2(position, dimensions);
+  // extrapolate track parameters to the chamber
+  AliMUONTrackParam extrapTrackParam(trackParam);
+  AliMUONTrackExtrap::ExtrapToZCov(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(chamber));
+  
+  // build the searching area using the track resolution and the maximum-distance-to-track value
+  const TMatrixD& kParamCov = extrapTrackParam.GetCovariances();
+  Double_t errX2 = kParamCov(0,0) + kMaxDZ * kMaxDZ * kParamCov(1,1) + 2. * kMaxDZ * TMath::Abs(kParamCov(0,1));
+  Double_t errY2 = kParamCov(2,2) + kMaxDZ * kMaxDZ * kParamCov(3,3) + 2. * kMaxDZ * TMath::Abs(kParamCov(2,3));
+  Double_t dX = TMath::Abs(trackParam.GetNonBendingSlope()) * kMaxDZ +
+               GetRecoParam()->GetMaxNonBendingDistanceToTrack() +
+               GetRecoParam()->GetSigmaCutForTracking() * TMath::Sqrt(errX2);
+  Double_t dY = TMath::Abs(trackParam.GetBendingSlope()) * kMaxDZ +
+               GetRecoParam()->GetMaxBendingDistanceToTrack() +
+               GetRecoParam()->GetSigmaCutForTracking() * TMath::Sqrt(errY2);
+  AliMpArea area(extrapTrackParam.GetNonBendingCoor(), 
+                 extrapTrackParam.GetBendingCoor(),
+                 dX, dY);
   
   // ask to cluterize in the given area of the given chamber
-  fClusterServer.Clusterize(2*station+1, clusterStore, area2);
+  fClusterServer->Clusterize(chamber, clusterStore, area, GetRecoParam());
   
-  // extrapolate track parameters to the first chamber of the station
-  AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParam, AliMUONConstants::DefaultChamberZ(2*station));
-  
-  // build the searching area
-  position.Set(extrapTrackParam.GetNonBendingCoor(), extrapTrackParam.GetBendingCoor());
-  AliMpArea area1(position, dimensions);
-  
-  // ask to cluterize in the given area of the given chamber
-  fClusterServer.Clusterize(2*station, clusterStore, area1);
+}
+
+  //__________________________________________________________________________
+void AliMUONVTrackReconstructor::AskForNewClustersInStation(const AliMUONTrackParam &trackParam,
+                                                           AliMUONVClusterStore& clusterStore, Int_t station)
+{
+  /// Ask the clustering to reconstruct new clusters around the track candidate position
+  /// in the 2 chambers of the given station
+  AskForNewClustersInChamber(trackParam, clusterStore, 2*station+1);
+  AskForNewClustersInChamber(trackParam, clusterStore, 2*station);
 }
 
   //__________________________________________________________________________
@@ -396,18 +528,26 @@ Double_t AliMUONVTrackReconstructor::TryOneCluster(const AliMUONTrackParam &trac
 }
 
   //__________________________________________________________________________
-Bool_t AliMUONVTrackReconstructor::TryOneClusterFast(const AliMUONTrackParam &trackParam, AliMUONVCluster* cluster)
+Bool_t AliMUONVTrackReconstructor::TryOneClusterFast(const AliMUONTrackParam &trackParam, const AliMUONVCluster* cluster)
 {
-/// Test the compatibility between the track and the cluster within a wide fix window
-/// assuming linear propagation of the track:
+/// Test the compatibility between the track and the cluster
+/// given the track resolution + the maximum-distance-to-track value
+/// and assuming linear propagation of the track:
 /// return kTRUE if they are compatibles
   
   Double_t dZ = cluster->GetZ() - trackParam.GetZ();
   Double_t dX = cluster->GetX() - (trackParam.GetNonBendingCoor() + trackParam.GetNonBendingSlope() * dZ);
   Double_t dY = cluster->GetY() - (trackParam.GetBendingCoor() + trackParam.GetBendingSlope() * dZ);
+  const TMatrixD& kParamCov = trackParam.GetCovariances();
+  Double_t errX2 = kParamCov(0,0) + dZ * dZ * kParamCov(1,1) + 2. * dZ * kParamCov(0,1);
+  Double_t errY2 = kParamCov(2,2) + dZ * dZ * kParamCov(3,3) + 2. * dZ * kParamCov(2,3);
+
+  Double_t dXmax = GetRecoParam()->GetSigmaCutForTracking() * TMath::Sqrt(errX2) +
+                   GetRecoParam()->GetMaxNonBendingDistanceToTrack();
+  Double_t dYmax = GetRecoParam()->GetSigmaCutForTracking() * TMath::Sqrt(errY2) +
+                  GetRecoParam()->GetMaxBendingDistanceToTrack();
   
-  if (TMath::Abs(dX) > AliMUONReconstructor::GetRecoParam()->GetMaxNonBendingDistanceToTrack() ||
-      TMath::Abs(dY) > AliMUONReconstructor::GetRecoParam()->GetMaxBendingDistanceToTrack()) return kFALSE;
+  if (TMath::Abs(dX) > dXmax || TMath::Abs(dY) > dYmax) return kFALSE;
   
   return kTRUE;
   
@@ -457,6 +597,129 @@ Double_t AliMUONVTrackReconstructor::TryTwoClustersFast(const AliMUONTrackParam
 }
 
   //__________________________________________________________________________
+Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInChamber(AliMUONTrack &trackCandidate, const AliMUONVClusterStore& clusterStore,
+                                                             Int_t nextChamber)
+{
+  /// Follow trackCandidate in chamber(0..) nextChamber assuming linear propagation, 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 FollowLinearTrackInChamber(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 trackParam;
+  AliMUONTrackParam extrapTrackParamAtCluster;
+  AliMUONTrackParam bestTrackParamAtCluster;
+  
+  // Get track parameters according to the propagation direction
+  if (nextChamber > 7) trackParam = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
+  else trackParam = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
+  
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
+    cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
+    trackParam.GetParameters().Print();
+    trackParam.GetCovariances().Print();
+  }
+  
+  // Add MCS effect
+  AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
+  
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+    cout << "FollowLinearTrackInChamber: look for cluster in chamber(1..): " << nextChamber+1 << endl;
+  }
+  
+  // Create iterators to loop over clusters in chamber
+  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(trackParam, cluster)) continue;
+    
+    // try to add the current cluster accuratly
+    extrapTrackParamAtCluster = trackParam;
+    AliMUONTrackExtrap::LinearExtrapToZ(&extrapTrackParamAtCluster, cluster->GetZ());
+    chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCluster, cluster, extrapTrackParamAtCluster);
+    
+    // if good chi2 then consider to add cluster
+    if (chi2WithOneCluster < maxChi2WithOneCluster) {
+      foundOneCluster = kTRUE;
+      
+      // Printout for debuging
+      if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+       cout << "FollowLinearTrackInChamber: 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);
+       if (GetRecoParam()->RequestStation(nextChamber/2))
+         extrapTrackParamAtCluster.SetRemovable(kFALSE);
+       else extrapTrackParamAtCluster.SetRemovable(kTRUE);
+       newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster,*cluster);
+       newTrack->SetGlobalChi2(trackCandidate.GetGlobalChi2()+chi2WithOneCluster);
+       fNRecTracks++;
+       
+       // Printout for debuging
+       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+         cout << "FollowLinearTrackInChamber: added one cluster in chamber(1..): " << nextChamber+1 << endl;
+         if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
+       }
+       
+      } else if (chi2WithOneCluster < bestChi2WithOneCluster) {
+       // keep track of the best cluster
+       bestChi2WithOneCluster = chi2WithOneCluster;
+       bestTrackParamAtCluster = extrapTrackParamAtCluster;
+      }
+      
+    }
+    
+  }
+  
+  // fill out the best track if required else clean up the fRecTracksPtr array
+  if (!GetRecoParam()->TrackAllTracks()) {
+    if (foundOneCluster) {
+      if (GetRecoParam()->RequestStation(nextChamber/2))
+       bestTrackParamAtCluster.SetRemovable(kFALSE);
+      else bestTrackParamAtCluster.SetRemovable(kTRUE);
+      trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster,*(bestTrackParamAtCluster.GetClusterPtr()));
+      trackCandidate.SetGlobalChi2(trackCandidate.GetGlobalChi2()+bestChi2WithOneCluster);
+      
+      // Printout for debuging
+      if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+        cout << "FollowLinearTrackInChamber: 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;
+  
+}
+
+//__________________________________________________________________________
 Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trackCandidate, const AliMUONVClusterStore& clusterStore,
                                                              Int_t nextStation)
 {
@@ -483,10 +746,10 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
   
   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;
@@ -508,6 +771,13 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
   if (nextStation==4) trackParam = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->Last();
   else trackParam = *(AliMUONTrackParam*)trackCandidate.GetTrackParamAtCluster()->First();
   
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 2) || (AliLog::GetGlobalDebugLevel() >= 2)) {
+    cout<<endl<<"Track parameters and covariances at first cluster:"<<endl;
+    trackParam.GetParameters().Print();
+    trackParam.GetCovariances().Print();
+  }
+  
   // Add MCS effect
   AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
   
@@ -539,6 +809,7 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
         cout << "FollowLinearTrackInStation: found one cluster in chamber(1..): " << ch2+1
             << " (Chi2 = " << chi2WithOneCluster << ")" << endl;
+       clusterCh2->Print();
         cout << "                      look for second clusters in chamber(1..): " << ch1+1 << " ..." << endl;
       }
       
@@ -567,16 +838,18 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
          // Printout for debuging
          if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
            cout << "FollowLinearTrackInStation: found one cluster in chamber(1..): " << ch1+1
-                << " (Chi2 = " << chi2WithTwoClusters << ")" << endl;
+                << " (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);
            extrapTrackParamAtCluster1.SetRemovable(kTRUE);
            newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster1,*clusterCh1);
            extrapTrackParamAtCluster2.SetRemovable(kTRUE);
            newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster2,*clusterCh2);
+           newTrack->SetGlobalChi2(newTrack->GetGlobalChi2()+chi2WithTwoClusters);
            fNRecTracks++;
            
            // Tag clusterCh1 as used
@@ -603,11 +876,14 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
       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);
-         extrapTrackParamAtCluster2.SetRemovable(kFALSE);
+         if (GetRecoParam()->RequestStation(nextStation))
+           extrapTrackParamAtCluster2.SetRemovable(kFALSE);
+         else extrapTrackParamAtCluster2.SetRemovable(kTRUE);
          newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster2,*clusterCh2);
+         newTrack->SetGlobalChi2(newTrack->GetGlobalChi2()+chi2WithOneCluster);
          fNRecTracks++;
          
          // Printout for debuging
@@ -630,7 +906,7 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
   
   // 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) {
+  if (GetRecoParam()->TrackAllTracks() || !foundTwoClusters) {
     
     // Printout for debuging
     if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
@@ -642,7 +918,7 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
     
     // add MCS effect for next step
     AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
-      
+    
     // reset cluster iterator of chamber 1
     nextInCh1.Reset();
     iCluster1 = -1;
@@ -655,12 +931,12 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
       
       // try to add the current cluster fast
       if (!TryOneClusterFast(trackParam, clusterCh1)) continue;
-       
+      
       // try to add the current cluster accuratly
       extrapTrackParamAtCluster1 = trackParam;
       AliMUONTrackExtrap::LinearExtrapToZ(&extrapTrackParamAtCluster1, clusterCh1->GetZ());
       chi2WithOneCluster = TryOneCluster(extrapTrackParamAtCluster1, 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) {
@@ -670,13 +946,17 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
        if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
          cout << "FollowLinearTrackInStation: 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);
-         extrapTrackParamAtCluster1.SetRemovable(kFALSE);
+         if (GetRecoParam()->RequestStation(nextStation))
+           extrapTrackParamAtCluster1.SetRemovable(kFALSE);
+         else extrapTrackParamAtCluster1.SetRemovable(kTRUE);
          newTrack->AddTrackParamAtCluster(extrapTrackParamAtCluster1,*clusterCh1);
+         newTrack->SetGlobalChi2(newTrack->GetGlobalChi2()+chi2WithOneCluster);
          fNRecTracks++;
          
          // Printout for debuging
@@ -698,12 +978,13 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
   }
   
   // fill out the best track if required else clean up the fRecTracksPtr array
-  if (!AliMUONReconstructor::GetRecoParam()->TrackAllTracks()) {
+  if (!GetRecoParam()->TrackAllTracks()) {
     if (foundTwoClusters) {
       bestTrackParamAtCluster1.SetRemovable(kTRUE);
       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster1,*(bestTrackParamAtCluster1.GetClusterPtr()));
       bestTrackParamAtCluster2.SetRemovable(kTRUE);
       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster2,*(bestTrackParamAtCluster2.GetClusterPtr()));
+      trackCandidate.SetGlobalChi2(trackCandidate.GetGlobalChi2()+bestChi2WithTwoClusters);
       
       // Printout for debuging
       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
@@ -712,8 +993,11 @@ Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trac
       }
       
     } else if (foundOneCluster) {
-      bestTrackParamAtCluster1.SetRemovable(kFALSE);
+      if (GetRecoParam()->RequestStation(nextStation))
+       bestTrackParamAtCluster1.SetRemovable(kFALSE);
+      else bestTrackParamAtCluster1.SetRemovable(kTRUE);
       trackCandidate.AddTrackParamAtCluster(bestTrackParamAtCluster1,*(bestTrackParamAtCluster1.GetClusterPtr()));
+      trackCandidate.SetGlobalChi2(trackCandidate.GetGlobalChi2()+bestChi2WithOneCluster);
       
       // Printout for debuging
       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
@@ -751,9 +1035,6 @@ void AliMUONVTrackReconstructor::ImproveTracks()
   
   AliMUONTrack *track, *nextTrack;
   
-  // Remove double track to improve only "good" tracks
-  RemoveDoubleTracks();
-  
   track = (AliMUONTrack*) fRecTracksPtr->First();
   while (track) {
     
@@ -780,6 +1061,7 @@ void AliMUONVTrackReconstructor::ImproveTracks()
 void AliMUONVTrackReconstructor::Finalize()
 {
   /// Recompute track parameters and covariances at each attached cluster from those at the first one
+  /// Set the label pointing to the corresponding MC track
   
   AliMUONTrack *track;
   
@@ -788,6 +1070,8 @@ void AliMUONVTrackReconstructor::Finalize()
     
     FinalizeTrack(*track);
     
+    track->FindMCLabel();
+    
     track = (AliMUONTrack*) fRecTracksPtr->After(track);
     
   }
@@ -834,16 +1118,10 @@ void AliMUONVTrackReconstructor::EventReconstructTrigger(const AliMUONTriggerCir
   
   while ( ( locTrg = static_cast<AliMUONLocalTrigger*>(next()) ) )
   {
-    Bool_t xTrig=kFALSE;
-    Bool_t yTrig=kFALSE;
+    Bool_t xTrig=locTrg->IsTrigX();
+    Bool_t yTrig=locTrg->IsTrigY();
     
     Int_t localBoardId = locTrg->LoCircuit();
-    if ( locTrg->LoSdev()==1 && locTrg->LoDev()==0 && 
-         locTrg->LoStripX()==0) xTrig=kFALSE; // no trigger in X
-    else xTrig=kTRUE;                         // trigger in X
-    if (locTrg->LoTrigY()==1 && 
-        locTrg->LoStripY()==15 ) yTrig = kFALSE; // no trigger in Y
-    else yTrig = kTRUE;                          // trigger in Y
     
     if (xTrig && yTrig) 
     { // make Trigger Track if trigger in X and Y
@@ -851,13 +1129,7 @@ void AliMUONVTrackReconstructor::EventReconstructTrigger(const AliMUONTriggerCir
       Float_t y11 = circuit.GetY11Pos(localBoardId, locTrg->LoStripX()); 
       // need first to convert deviation to [0-30] 
       // (see AliMUONLocalTriggerBoard::LocalTrigger)
-      Int_t deviation = locTrg->LoDev(); 
-      Int_t sign = 0;
-      if ( !locTrg->LoSdev() &&  deviation ) sign=-1;
-      if ( !locTrg->LoSdev() && !deviation ) sign= 0;
-      if (  locTrg->LoSdev() == 1 )          sign=+1;
-      deviation *= sign;
-      deviation += 15;
+      Int_t deviation = locTrg->GetDeviation(); 
       Int_t stripX21 = locTrg->LoStripX()+deviation+1;
       Float_t y21 = circuit.GetY21Pos(localBoardId, stripX21);       
       Float_t x11 = circuit.GetX11Pos(localBoardId, locTrg->LoStripY());
@@ -867,6 +1139,9 @@ void AliMUONVTrackReconstructor::EventReconstructTrigger(const AliMUONTriggerCir
       
       Float_t thetax = TMath::ATan2( x11 , z11 );
       Float_t thetay = TMath::ATan2( (y21-y11) , (z21-z11) );
+
+      CorrectThetaRange(thetax);
+      CorrectThetaRange(thetay);
       
       triggerTrack.SetX11(x11);
       triggerTrack.SetY11(y11);
@@ -880,3 +1155,13 @@ void AliMUONVTrackReconstructor::EventReconstructTrigger(const AliMUONTriggerCir
   } // end of loop on Local Trigger
 }
 
+//__________________________________________________________________________
+void AliMUONVTrackReconstructor::CorrectThetaRange(Float_t& theta)
+{
+  /// The angles of the trigger tracks, obtained with ATan2, 
+  /// have values around +pi and -pi. On the contrary, the angles 
+  /// used in the tracker tracks have values around 0.
+  /// This function sets the same range for the trigger tracks angles.
+  if (theta < -TMath::PiOver2()) theta += TMath::Pi();
+  else if(theta > TMath::PiOver2()) theta -= TMath::Pi();
+}