]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONVTrackReconstructor.cxx
new SPD geometry (A.Pulvirenti)
[u/mrichter/AliRoot.git] / MUON / AliMUONVTrackReconstructor.cxx
index cf4adca890696ecb84e3cdf7f9fdc2a16ca9c791..150d9924766b187d11220a234633e1ca97a505ac 100644 (file)
 
 /* $Id$ */
 
-////////////////////////////////////
-//
-// Virtual MUON track reconstructor in ALICE (class renamed from AliMUONEventReconstructor)
-//
-// This class contains as data:
-// * a pointer to the array of hits to be reconstructed (the event)
-// * a pointer to the array of segments made with these hits inside each station
-// * a pointer to the array of reconstructed tracks
-//
-// It contains as methods, among others:
-// * EventReconstruct to build the muon tracks
-// * EventReconstructTrigger to build the trigger tracks
-//
-////////////////////////////////////
-
-#include <stdlib.h>
-#include <Riostream.h>
-#include <TTree.h>
+//-----------------------------------------------------------------------------
+/// \class AliMUONVTrackReconstructor
+/// Virtual MUON track reconstructor in ALICE (class renamed from AliMUONEventReconstructor)
+///
+/// This class contains as data:
+/// * a pointer to the array of hits to be reconstructed (the event)
+/// * a pointer to the array of reconstructed tracks
+///
+/// It contains as methods, among others:
+/// * EventReconstruct to build the muon tracks
+/// * EventReconstructTrigger to build the trigger tracks
+///
+/// Several options and adjustable parameters are available for both Kalman and Original
+/// tracking algorithms (hard coded for the moment in AliMUONVTrackReconstructor.cxx):
+/// - *fgkSigmaToCutForTracking* : quality cut used to select new clusters to be
+///   attached to the track candidate and to select good tracks.
+/// - *fgkMakeTrackCandidatesFast* : if this flag is set to 'true', the track candidates
+///   are made assuming linear propagation between stations 4 and 5.
+/// - *fgkTrackAllTracks* : according to the value of this flag, in case that several
+///   new clusters pass the quality cut, either we consider all the possibilities
+///   (duplicating tracks) or we attach only the best cluster.
+/// - *fgkRecoverTracks* : if this flag is set to 'true', we try to recover the tracks
+///   lost during the tracking by removing the worst of the 2 clusters attached in the
+///   previous station.
+/// - *fgkImproveTracks* : if this flag is set to 'true', we try to improve the quality
+///   of the tracks at the end of the tracking by removing clusters that do not pass
+///   new quality cut (within the limit that we must keep at least one cluster per
+///   the station).
+/// - *fgkSigmaToCutForImprovement* : quality cut used when we try to improve the
+///   quality of the tracks.
+///
+///  \author Philippe Pillot
+//-----------------------------------------------------------------------------
 
 #include "AliMUONVTrackReconstructor.h"
-#include "AliMUONData.h"
+
 #include "AliMUONConstants.h"
+#include "AliMUONRawCluster.h"
 #include "AliMUONHitForRec.h"
+#include "AliMUONObjectPair.h"
 #include "AliMUONTriggerTrack.h"
 #include "AliMUONTriggerCircuit.h"
 #include "AliMUONLocalTrigger.h"
 #include "AliMUONGlobalTrigger.h"
-#include "AliMUONSegment.h"
 #include "AliMUONTrack.h"
 #include "AliMUONTrackParam.h"
 #include "AliMUONTrackExtrap.h"
-#include "AliMagF.h"
+#include "AliMUONTrackHitPattern.h"
+#include "AliMUONVTrackStore.h"
+#include "AliMUONVClusterStore.h"
+#include "AliMUONRawCluster.h"
+#include "AliMUONVTriggerStore.h"
+#include "AliMUONVTriggerTrackStore.h"
+#include "AliMpDEManager.h"
+
 #include "AliLog.h"
 #include "AliTracker.h"
 
+#include <TClonesArray.h>
+#include <TMath.h>
+#include <TMatrixD.h>
+
+#include <Riostream.h>
+
+/// \cond CLASSIMP
 ClassImp(AliMUONVTrackReconstructor) // Class implementation in ROOT context
+/// \endcond
+
 
 //************* Defaults parameters for reconstruction
 const Double_t AliMUONVTrackReconstructor::fgkDefaultMinBendingMomentum = 3.0;
 const Double_t AliMUONVTrackReconstructor::fgkDefaultMaxBendingMomentum = 3000.0;
-const Double_t AliMUONVTrackReconstructor::fgkDefaultBendingResolution = 0.01;
-const Double_t AliMUONVTrackReconstructor::fgkDefaultNonBendingResolution = 0.144;
-const Double_t AliMUONVTrackReconstructor::fgkDefaultMaxSigma2Distance = 16.0;
-// Simple magnetic field:
-// Value taken from macro MUONtracking.C: 0.7 T, hence 7 kG
-// Length and Position from reco_muon.F, with opposite sign:
-// Length = ZMAGEND-ZCOIL
-// Position = (ZMAGEND+ZCOIL)/2
-// to be ajusted differently from real magnetic field ????
-const Double_t AliMUONVTrackReconstructor::fgkDefaultSimpleBValue = 7.0;
-const Double_t AliMUONVTrackReconstructor::fgkDefaultSimpleBLength = 428.0;
-const Double_t AliMUONVTrackReconstructor::fgkDefaultSimpleBPosition = 1019.0;
-
-//__________________________________________________________________________
-AliMUONVTrackReconstructor::AliMUONVTrackReconstructor(AliMUONData* data)
+const Double_t AliMUONVTrackReconstructor::fgkDefaultMaxNormChi2MatchTrigger = 16.0;
+const Double_t AliMUONVTrackReconstructor::fgkMaxTrackingDistanceBending    = 2.;
+const Double_t AliMUONVTrackReconstructor::fgkMaxTrackingDistanceNonBending = 2.;
+
+const Double_t AliMUONVTrackReconstructor::fgkSigmaToCutForTracking = 6.0;
+const Bool_t   AliMUONVTrackReconstructor::fgkMakeTrackCandidatesFast = kFALSE;
+const Bool_t   AliMUONVTrackReconstructor::fgkTrackAllTracks = kTRUE;
+const Bool_t   AliMUONVTrackReconstructor::fgkRecoverTracks = kTRUE;
+const Bool_t   AliMUONVTrackReconstructor::fgkImproveTracks = kTRUE;
+const Double_t AliMUONVTrackReconstructor::fgkSigmaToCutForImprovement = 4.0;
+
+
+  //__________________________________________________________________________
+AliMUONVTrackReconstructor::AliMUONVTrackReconstructor()
   : TObject(),
     fMinBendingMomentum(fgkDefaultMinBendingMomentum),
     fMaxBendingMomentum(fgkDefaultMaxBendingMomentum),
-    fBendingResolution(fgkDefaultBendingResolution),
-    fNonBendingResolution(fgkDefaultNonBendingResolution),
-    fMaxSigma2Distance(fgkDefaultMaxSigma2Distance),
-    fChamberThicknessInX0(AliMUONConstants::DefaultChamberThicknessInX0()),
-    fSimpleBValue(fgkDefaultSimpleBValue),
-    fSimpleBLength(fgkDefaultSimpleBLength),
-    fSimpleBPosition(fgkDefaultSimpleBPosition),
-    fSegmentMaxDistBending(0x0),
-    fSegmentMaxDistNonBending(0x0),
+    fMaxNormChi2MatchTrigger(fgkDefaultMaxNormChi2MatchTrigger),
     fHitsForRecPtr(0x0),
     fNHitsForRec(0),
     fNHitsForRecPerChamber(0x0),
     fIndexOfFirstHitForRecPerChamber(0x0),
-    fSegmentsPtr(0x0),
-    fNSegments(0x0),
     fRecTracksPtr(0x0),
-    fNRecTracks(0),
-    fMUONData(data),
-    fTriggerTrack(new AliMUONTriggerTrack()),
-    fTriggerCircuit(0x0)
+    fNRecTracks(0)
 {
   /// Constructor for class AliMUONVTrackReconstructor
-  fSegmentMaxDistBending = new Double_t[AliMUONConstants::NTrackingSt()];
-  fSegmentMaxDistNonBending = new Double_t[AliMUONConstants::NTrackingSt()];
   fNHitsForRecPerChamber = new Int_t[AliMUONConstants::NTrackingCh()];
   fIndexOfFirstHitForRecPerChamber = new Int_t[AliMUONConstants::NTrackingCh()];
 
-  SetReconstructionParametersToDefaults();
-  
   // Memory allocation for the TClonesArray of hits for reconstruction
   // Is 10000 the right size ????
   fHitsForRecPtr = new TClonesArray("AliMUONHitForRec", 10000);
-
-  // Memory allocation for the TClonesArray's of segments in stations
-  // Is 2000 the right size ????
-  fSegmentsPtr = new TClonesArray*[AliMUONConstants::NTrackingSt()];
-  fNSegments = new Int_t[AliMUONConstants::NTrackingSt()];
-  for (Int_t st = 0; st < AliMUONConstants::NTrackingSt(); st++) {
-    fSegmentsPtr[st] = new TClonesArray("AliMUONSegment", 2000);
-    fNSegments[st] = 0; // really needed or GetEntriesFast sufficient ????
-  }
-
-  const AliMagF* kField = AliTracker::GetFieldMap();
-  if (!kField) AliFatal("No field available");
- // Sign of fSimpleBValue according to sign of Bx value at (50,50,-950).
-  Float_t b[3], x[3];
-  x[0] = 50.; x[1] = 50.; x[2] = -950.;
-  kField->Field(x,b);
-
-  fSimpleBValue    = TMath::Sign(fSimpleBValue,(Double_t) b[0]);
-  fSimpleBPosition = TMath::Sign(fSimpleBPosition,(Double_t) x[2]);
-  // See how to get fSimple(BValue, BLength, BPosition)
-  // automatically calculated from the actual magnetic field ????
+  
+  // Memory allocation for the TClonesArray of reconstructed tracks
+  fRecTracksPtr = new TClonesArray("AliMUONTrack", 10);
   
   // set the magnetic field for track extrapolations
+  const AliMagF* kField = AliTracker::GetFieldMap();
+  if (!kField) AliFatal("No field available");
   AliMUONTrackExtrap::SetField(kField);
 }
 
   //__________________________________________________________________________
-AliMUONVTrackReconstructor::~AliMUONVTrackReconstructor(void)
+AliMUONVTrackReconstructor::~AliMUONVTrackReconstructor()
 {
   /// Destructor for class AliMUONVTrackReconstructor
-  delete [] fSegmentMaxDistBending;
-  delete [] fSegmentMaxDistNonBending;
   delete [] fNHitsForRecPerChamber;
   delete [] fIndexOfFirstHitForRecPerChamber;
-  delete fTriggerTrack;
   delete fHitsForRecPtr;
-  // Correct destruction of everything ????
-  for (Int_t st = 0; st < AliMUONConstants::NTrackingSt(); st++) delete fSegmentsPtr[st];
-  delete [] fSegmentsPtr;
-  delete [] fNSegments;
-}
-
-  //__________________________________________________________________________
-void AliMUONVTrackReconstructor::SetReconstructionParametersToDefaults(void)
-{
-  /// Set reconstruction parameters for making segments to default values
-  // Would be much more convenient with a structure (or class) ????
-
-  // ******** Parameters for making segments
-  // should be parametrized ????
-  // according to interval between chambers in a station ????
-  // Maximum distance in non bending plane
-  // 5 * 0.22 just to remember the way it was made in TRACKF_STAT
-  // SIGCUT*DYMAX(IZ)
-  for (Int_t st = 0; st < AliMUONConstants::NTrackingCh()/2; st++)
-    fSegmentMaxDistNonBending[st] = 5. * 0.22;
-  // Maximum distance in bending plane:
-  // values from TRACKF_STAT, corresponding to (J psi 20cm),
-  // scaled to the real distance between chambers in a station
-  fSegmentMaxDistBending[0] = TMath::Abs( 1.5 *
-                                         (AliMUONConstants::DefaultChamberZ(1) - AliMUONConstants::DefaultChamberZ(0)) / 20.0);
-  fSegmentMaxDistBending[1] = TMath::Abs( 1.5 *
-                                         (AliMUONConstants::DefaultChamberZ(3) - AliMUONConstants::DefaultChamberZ(2)) / 20.0);
-  fSegmentMaxDistBending[2] = TMath::Abs( 3.0 *
-                                         (AliMUONConstants::DefaultChamberZ(5) - AliMUONConstants::DefaultChamberZ(4)) / 20.0);
-  fSegmentMaxDistBending[3] = TMath::Abs( 6.0 *
-                                         (AliMUONConstants::DefaultChamberZ(7) - AliMUONConstants::DefaultChamberZ(6)) / 20.0);
-  fSegmentMaxDistBending[4] = TMath::Abs( 6.0 *
-                                         (AliMUONConstants::DefaultChamberZ(9) - AliMUONConstants::DefaultChamberZ(8)) / 20.0);
-
-  return;
+  delete fRecTracksPtr;
 }
 
   //__________________________________________________________________________
-void AliMUONVTrackReconstructor::EventReconstruct(void)
+void AliMUONVTrackReconstructor::ResetHitsForRec()
 {
-  // To reconstruct one event
-  AliDebug(1,"Enter EventReconstruct");
-  ResetTracks(); //AZ
-  ResetSegments(); //AZ
-  ResetHitsForRec(); //AZ
-  AddHitsForRecFromRawClusters();
-  MakeSegments();
-  MakeTracks();
-  if (fMUONData->IsTriggerTrackBranchesInTree()) 
-    ValidateTracksWithTrigger(); 
-  
-  // Add tracks to MUON data container 
-  for(Int_t i=0; i<fNRecTracks; i++) {
-    AliMUONTrack * track = (AliMUONTrack*) fRecTracksPtr->At(i);
-    fMUONData->AddRecTrack(*track);
+  /// To reset the TClonesArray of HitsForRec,
+  /// and the number of HitForRec and the index of the first HitForRec per chamber
+  if (fHitsForRecPtr) fHitsForRecPtr->Clear("C");
+  fNHitsForRec = 0;
+  for (Int_t ch = 0; ch < AliMUONConstants::NTrackingCh(); ch++) {
+    fNHitsForRecPerChamber[ch] = 0;
+    fIndexOfFirstHitForRecPerChamber[ch] = 0;
   }
 }
 
   //__________________________________________________________________________
-void AliMUONVTrackReconstructor::ResetTracks(void)
+void AliMUONVTrackReconstructor::ResetTracks()
 {
   /// To reset the TClonesArray of reconstructed tracks
-  if (fRecTracksPtr) fRecTracksPtr->Delete();
-  // Delete in order that the Track destructors are called,
-  // hence the space for the TClonesArray of pointers to TrackHit's is freed
+  if (fRecTracksPtr) fRecTracksPtr->Clear("C");
   fNRecTracks = 0;
   return;
 }
 
   //__________________________________________________________________________
-void AliMUONVTrackReconstructor::ResetSegments(void)
+void AliMUONVTrackReconstructor::EventReconstruct(const AliMUONVClusterStore& clusterStore, AliMUONVTrackStore& trackStore)
 {
-  /// To reset the TClonesArray of segments and the number of Segments for all stations
-  for (Int_t st = 0; st < AliMUONConstants::NTrackingCh()/2; st++) {
-    if (fSegmentsPtr[st]) fSegmentsPtr[st]->Clear();
-    fNSegments[st] = 0;
+  /// To reconstruct one event
+  AliDebug(1,"");
+  
+  ResetTracks();
+  ResetHitsForRec();
+  AddHitsForRecFromRawClusters(clusterStore);
+  MakeTracks();
+
+  // Add tracks to MUON data container 
+  for (Int_t i=0; i<fNRecTracks; ++i) 
+  {
+    AliMUONTrack * track = (AliMUONTrack*) fRecTracksPtr->At(i);
+    trackStore.Add(*track);
   }
-  return;
 }
 
   //__________________________________________________________________________
-void AliMUONVTrackReconstructor::ResetHitsForRec(void)
+void AliMUONVTrackReconstructor::AddHitsForRecFromRawClusters(const AliMUONVClusterStore& clusterStore)
 {
-  /// To reset the array and the number of HitsForRec,
-  /// and also the number of HitsForRec
-  /// and the index of the first HitForRec per chamber
-  if (fHitsForRecPtr) fHitsForRecPtr->Delete();
-  fNHitsForRec = 0;
-  for (Int_t ch = 0; ch < AliMUONConstants::NTrackingCh(); ch++)
-    fNHitsForRecPerChamber[ch] = fIndexOfFirstHitForRecPerChamber[ch] = 0;
-  return;
+  /// Build internal array of hit for rec from clusterStore
+  
+  TIter next(clusterStore.CreateIterator());
+  AliMUONRawCluster* clus(0x0);
+  Int_t iclus(0);
+  
+  while ( ( clus = static_cast<AliMUONRawCluster*>(next()) ) )
+  {
+    // new AliMUONHitForRec from raw cluster
+    // and increment number of AliMUONHitForRec's (total and in chamber)
+    AliMUONHitForRec* hitForRec = new ((*fHitsForRecPtr)[fNHitsForRec]) AliMUONHitForRec(clus);
+    fNHitsForRec++;
+    // more information into HitForRec
+    hitForRec->SetBendingReso2(clus->GetErrY() * clus->GetErrY());
+    hitForRec->SetNonBendingReso2(clus->GetErrX() * clus->GetErrX());
+    //  original raw cluster
+    Int_t ch = AliMpDEManager::GetChamberId(clus->GetDetElemId());
+    hitForRec->SetChamberNumber(ch);
+    hitForRec->SetHitNumber(iclus);
+    // Z coordinate of the raw cluster (cm)
+    hitForRec->SetZ(clus->GetZ(0));
+    if (AliLog::GetDebugLevel("MUON","AliMUONTrackReconstructor") >= 3) {
+      cout << "Chamber " << ch <<" raw cluster  " << iclus << " : " << endl;
+      clus->Print("full");
+      cout << "AliMUONHitForRec number (1...): " << fNHitsForRec << endl;
+      hitForRec->Print("full");
+    }
+    ++iclus;
+  } // end of chamber loop
+  
+  SortHitsForRecWithIncreasingChamber(); 
+  
+  AliDebug(1,"End of AddHitsForRecFromRawClusters");
+  
+  if (AliLog::GetGlobalDebugLevel() > 0) 
+  {
+    AliDebug(1, Form("NHitsForRec: %d",fNHitsForRec));
+    for (Int_t ch = 0; ch < AliMUONConstants::NTrackingCh(); ch++) 
+    {
+      AliDebug(1, Form("Chamber(0...): %d",ch));
+      AliDebug(1, Form("NHitsForRec: %d", fNHitsForRecPerChamber[ch]));
+      AliDebug(1, Form("Index(first HitForRec): %d", fIndexOfFirstHitForRecPerChamber[ch]));
+      for (Int_t hit = fIndexOfFirstHitForRecPerChamber[ch];
+           hit < fIndexOfFirstHitForRecPerChamber[ch] + fNHitsForRecPerChamber[ch];
+           hit++) {
+        AliDebug(1, Form("HitForRec index(0...): %d",hit));
+        ((*fHitsForRecPtr)[hit])->Dump();
+      }
+    }
+  }
 }
 
   //__________________________________________________________________________
@@ -265,304 +266,713 @@ void AliMUONVTrackReconstructor::SortHitsForRecWithIncreasingChamber()
 }
 
   //__________________________________________________________________________
-void AliMUONVTrackReconstructor::MakeSegmentsPerStation(Int_t Station)
+void AliMUONVTrackReconstructor::MakeTracks()
+{
+  /// To make the tracks from the list of segments and points in all stations
+  AliDebug(1,"Enter MakeTracks");
+  // Look for candidates from at least 3 aligned points in stations(1..) 4 and 5
+  MakeTrackCandidates();
+  if (fRecTracksPtr->GetEntriesFast() == 0) return;
+  // Follow tracks in stations(1..) 3, 2 and 1
+  FollowTracks();
+  // Improve the reconstructed tracks
+  if (fgkImproveTracks) ImproveTracks();
+  // Remove double tracks
+  RemoveDoubleTracks();
+  // Fill AliMUONTrack data members
+  Finalize();
+}
+
+  //__________________________________________________________________________
+TClonesArray* AliMUONVTrackReconstructor::MakeSegmentsInStation(Int_t station)
 {
-  /// To make the list of segments in station number "Station" (0...)
-  /// from the list of hits to be reconstructed.
-  /// Updates "fNSegments"[Station].
-  /// Segments in stations 4 and 5 are sorted
-  /// according to increasing absolute value of "impact parameter"
+  /// To make the list of segments in station(0..) "Station" from the list of hits to be reconstructed.
+  /// Return a new TClonesArray of segments.
+  /// It is the responsibility of the user to delete it afterward.
+  AliDebug(1,Form("Enter MakeSegmentsPerStation (0...) %d",station));
+  
   AliMUONHitForRec *hit1Ptr, *hit2Ptr;
-  AliMUONSegment *segment;
-  Bool_t last2st;
-  Double_t bendingSlope, distBend, distNonBend, extBendCoor, extNonBendCoor,
-      impactParam = 0., maxImpactParam = 0., minImpactParam = 0.; // =0 to avoid compilation warnings.
-  AliDebug(1,Form("Enter MakeSegmentsPerStation (0...) %d",Station));
-  // first and second chambers (0...) in the station
-  Int_t ch1 = 2 * Station;
+  AliMUONObjectPair *segment;
+  Double_t bendingSlope = 0, impactParam = 0., bendingMomentum = 0.; // to avoid compilation warning
+                                                                     // first and second chambers (0...) in the station
+  Int_t ch1 = 2 * station;
   Int_t ch2 = ch1 + 1;
-  // variable true for stations downstream of the dipole:
-  // Station(0..4) equal to 3 or 4
-  if ((Station == 3) || (Station == 4)) {
-    last2st = kTRUE;
-    // maximum impact parameter (cm) according to fMinBendingMomentum...
-    maxImpactParam =
-      TMath::Abs(GetImpactParamFromBendingMomentum(fMinBendingMomentum));
-    // minimum impact parameter (cm) according to fMaxBendingMomentum...
-    minImpactParam =
-      TMath::Abs(GetImpactParamFromBendingMomentum(fMaxBendingMomentum));
-  }
-  else last2st = kFALSE;
-  // extrapolation factor from Z of first chamber to Z of second chamber
-  // dZ to be changed to take into account fine structure of chambers ????
-  Double_t extrapFact;
-  // index for current segment
-  Int_t segmentIndex = 0;
-  // Loop over HitsForRec in the first chamber of the station
+  
+  // list of segments
+  TClonesArray *segments = new TClonesArray("AliMUONObjectPair", fNHitsForRecPerChamber[ch2]);
+  segments->SetOwner(kTRUE);
+  
+  // Loop over HitForRec's in the first chamber of the station
   for (Int_t hit1 = fIndexOfFirstHitForRecPerChamber[ch1];
        hit1 < fIndexOfFirstHitForRecPerChamber[ch1] + fNHitsForRecPerChamber[ch1];
-       hit1++) {
+       hit1++) 
+  {
     // pointer to the HitForRec
     hit1Ptr = (AliMUONHitForRec*) ((*fHitsForRecPtr)[hit1]);
-    // extrapolation,
-    // on the straight line joining the HitForRec to the vertex (0,0,0),
-    // to the Z of the second chamber of the station
-    // Loop over HitsForRec in the second chamber of the station
+    // Loop over HitsForRec's in the second chamber of the station
     for (Int_t hit2 = fIndexOfFirstHitForRecPerChamber[ch2];
-        hit2 < fIndexOfFirstHitForRecPerChamber[ch2] + fNHitsForRecPerChamber[ch2];
-        hit2++) {
+         hit2 < fIndexOfFirstHitForRecPerChamber[ch2] + fNHitsForRecPerChamber[ch2];
+         hit2++) 
+    {
       // pointer to the HitForRec
       hit2Ptr = (AliMUONHitForRec*) ((*fHitsForRecPtr)[hit2]);
-      // absolute values of distances, in bending and non bending planes,
-      // between the HitForRec in the second chamber
-      // and the previous extrapolation
-      extrapFact = hit2Ptr->GetZ()/ hit1Ptr->GetZ();
-      extBendCoor = extrapFact * hit1Ptr->GetBendingCoor();
-      extNonBendCoor = extrapFact * hit1Ptr->GetNonBendingCoor();
-      distBend = TMath::Abs(hit2Ptr->GetBendingCoor() - extBendCoor);
-      distNonBend = TMath::Abs(hit2Ptr->GetNonBendingCoor() - extNonBendCoor);
-      if (last2st) {
-       // bending slope
-       if ( hit1Ptr->GetZ() - hit2Ptr->GetZ() != 0.0 ) {
-         bendingSlope = (hit1Ptr->GetBendingCoor() - hit2Ptr->GetBendingCoor()) /
-           (hit1Ptr->GetZ() - hit2Ptr->GetZ());
-         // absolute value of impact parameter
-         impactParam =
-           TMath::Abs(hit1Ptr->GetBendingCoor() - hit1Ptr->GetZ() * bendingSlope);
-        } 
-        else {
-          AliWarning("hit1Ptr->GetZ() = hit2Ptr->GetZ(): impactParam set to maxImpactParam");
-          impactParam = maxImpactParam;   
-        }   
-      }
-      // check for distances not too large,
-      // and impact parameter not too big if stations downstream of the dipole.
-      // Conditions "distBend" and "impactParam" correlated for these stations ????
-      if ((distBend < fSegmentMaxDistBending[Station]) &&
-         (distNonBend < fSegmentMaxDistNonBending[Station]) &&
-         (!last2st || (impactParam < maxImpactParam)) &&
-         (!last2st || (impactParam > minImpactParam))) {
-       // make new segment
-       segment = new ((*fSegmentsPtr[Station])[segmentIndex])
-         AliMUONSegment(hit1Ptr, hit2Ptr);
-       // update "link" to this segment from the hit in the first chamber
-       if (hit1Ptr->GetNSegments() == 0)
-         hit1Ptr->SetIndexOfFirstSegment(segmentIndex);
-       hit1Ptr->SetNSegments(hit1Ptr->GetNSegments() + 1);
-       if (AliLog::GetGlobalDebugLevel() > 1) {
-         cout << "segmentIndex(0...): " << segmentIndex
-              << "  distBend: " << distBend
-              << "  distNonBend: " << distNonBend
-              << endl;
-         segment->Dump();
-         cout << "HitForRec in first chamber" << endl;
-         hit1Ptr->Dump();
-         cout << "HitForRec in second chamber" << endl;
-         hit2Ptr->Dump();
-       };
-       // increment index for current segment
-       segmentIndex++;
+      if ( hit1Ptr->GetZ() - hit2Ptr->GetZ() != 0. ) 
+      {
+        // bending slope
+        bendingSlope = (hit1Ptr->GetBendingCoor() - hit2Ptr->GetBendingCoor()) / (hit1Ptr->GetZ() - hit2Ptr->GetZ());
+        // impact parameter
+        impactParam = hit1Ptr->GetBendingCoor() - hit1Ptr->GetZ() * bendingSlope;
+        // absolute value of bending momentum
+        bendingMomentum = TMath::Abs(AliMUONTrackExtrap::GetBendingMomentumFromImpactParam(impactParam));
+      } else 
+      {
+        AliWarning("hit1Ptr->GetZ() = hit2Ptr->GetZ(): no segment created");
+        continue;
+      }   
+      // check for bending momentum within tolerances
+      if ((bendingMomentum < fMaxBendingMomentum) && (bendingMomentum > fMinBendingMomentum)) 
+      {
+        // make new segment
+        segment = new ((*segments)[segments->GetLast()+1]) AliMUONObjectPair(hit1Ptr, hit2Ptr, kFALSE, kFALSE);
+        if (AliLog::GetGlobalDebugLevel() > 1) {
+          cout << "segmentIndex(0...): " << segments->GetLast() << endl;
+          segment->Dump();
+          cout << "HitForRec in first chamber" << endl;
+          hit1Ptr->Dump();
+          cout << "HitForRec in second chamber" << endl;
+          hit2Ptr->Dump();
+        }
       }
     } //for (Int_t hit2
   } // for (Int_t hit1...
-  fNSegments[Station] = segmentIndex;
-  // Sorting according to "impact parameter" if station(1..5) 4 or 5,
-  // i.e. Station(0..4) 3 or 4, using the function "Compare".
-  // After this sorting, it is impossible to use
-  // the "fNSegments" and "fIndexOfFirstSegment"
-  // of the HitForRec in the first chamber to explore all segments formed with it.
-  // Is this sorting really needed ????
-  if ((Station == 3) || (Station == 4)) (fSegmentsPtr[Station])->Sort();
-  AliDebug(1,Form("Station: %d  NSegments:  %d ", Station, fNSegments[Station]));
+  AliDebug(1,Form("Station: %d  NSegments:  %d ", station, segments->GetEntriesFast()));
+  return segments;
+}
+
+  //__________________________________________________________________________
+void AliMUONVTrackReconstructor::RemoveIdenticalTracks()
+{
+  /// To remove identical tracks:
+  /// Tracks are considered identical if they have all their hits in common.
+  /// One keeps the track with the larger number of hits if need be
+  AliMUONTrack *track1, *track2, *trackToRemove;
+  Int_t hitsInCommon, nHits1, nHits2;
+  Bool_t removedTrack1;
+  // Loop over first track of the pair
+  track1 = (AliMUONTrack*) fRecTracksPtr->First();
+  while (track1) {
+    removedTrack1 = kFALSE;
+    nHits1 = track1->GetNTrackHits();
+    // Loop over second track of the pair
+    track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
+    while (track2) {
+      nHits2 = track2->GetNTrackHits();
+      // number of hits in common between two tracks
+      hitsInCommon = track1->HitsInCommon(track2);
+      // check for identical tracks
+      if ((hitsInCommon == nHits1) || (hitsInCommon == nHits2)) {
+        // decide which track to remove
+        if (nHits2 > nHits1) {
+         // 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
+         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
+         fNRecTracks--;
+        }
+      } else track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
+    } // track2
+    if (removedTrack1) continue;
+    track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
+  } // track1
+  return;
+}
+
+  //__________________________________________________________________________
+void AliMUONVTrackReconstructor::RemoveDoubleTracks()
+{
+  /// To remove double tracks:
+  /// Tracks are considered identical if more than half of the hits of the track
+  /// which has the smaller number of hits are in common with the other track.
+  /// Among two identical tracks, one keeps the track with the larger number of hits
+  /// or, if these numbers are equal, the track with the minimum chi2.
+  AliMUONTrack *track1, *track2, *trackToRemove;
+  Int_t hitsInCommon, nHits1, nHits2;
+  Bool_t removedTrack1;
+  // Loop over first track of the pair
+  track1 = (AliMUONTrack*) fRecTracksPtr->First();
+  while (track1) {
+    removedTrack1 = kFALSE;
+    nHits1 = track1->GetNTrackHits();
+    // Loop over second track of the pair
+    track2 = (AliMUONTrack*) fRecTracksPtr->After(track1);
+    while (track2) {
+      nHits2 = track2->GetNTrackHits();
+      // number of hits in common between two tracks
+      hitsInCommon = track1->HitsInCommon(track2);
+      // check for identical tracks
+      if (((nHits1 < nHits2) && (2 * hitsInCommon > nHits1)) || (2 * hitsInCommon > nHits2)) {
+        // decide which track to remove
+        if ((nHits1 > nHits2) || ((nHits1 == nHits2) && (track1->GetFitFMin() <= track2->GetFitFMin()))) {
+         // 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
+         fNRecTracks--;
+        } else {
+         // else 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
+         fNRecTracks--;
+         removedTrack1 = kTRUE;
+         break;
+        }
+      } else track2 = (AliMUONTrack*) fRecTracksPtr->After(track2);
+    } // track2
+    if (removedTrack1) continue;
+    track1 = (AliMUONTrack*) fRecTracksPtr->After(track1);
+  } // track1
   return;
 }
 
   //__________________________________________________________________________
-Double_t AliMUONVTrackReconstructor::GetImpactParamFromBendingMomentum(Double_t BendingMomentum) const
+Double_t AliMUONVTrackReconstructor::TryOneHitForRec(const AliMUONTrackParam &trackParam, AliMUONHitForRec* hitForRec,
+                                                    AliMUONTrackParam &trackParamAtHit, Bool_t updatePropagator)
 {
-  /// Returns impact parameter at vertex in bending plane (cm),
-  /// from the signed bending momentum "BendingMomentum" in bending plane (GeV/c),
-  /// using simple values for dipole magnetic field.
-  /// The sign of "BendingMomentum" is the sign of the charge.
-  return (-0.0003 * fSimpleBValue * fSimpleBLength * fSimpleBPosition /
-         BendingMomentum);
+/// Test the compatibility between the track and the hitForRec (using trackParam's covariance matrix):
+/// return the corresponding Chi2
+/// return trackParamAtHit
+  
+  // extrapolate track parameters and covariances at the z position of the tested hit
+  trackParamAtHit = trackParam;
+  AliMUONTrackExtrap::ExtrapToZCov(&trackParamAtHit, hitForRec->GetZ(), updatePropagator);
+  
+  // set pointer to hit into trackParamAtHit
+  trackParamAtHit.SetHitForRecPtr(hitForRec);
+  
+  // Set differences between trackParam and hitForRec in the bending and non bending directions
+  Double_t dX = hitForRec->GetNonBendingCoor() - trackParamAtHit.GetNonBendingCoor();
+  Double_t dY = hitForRec->GetBendingCoor() - trackParamAtHit.GetBendingCoor();
+  
+  // Calculate errors and covariances
+  const TMatrixD& kParamCov = trackParamAtHit.GetCovariances();
+  Double_t sigma2X = kParamCov(0,0) + hitForRec->GetNonBendingReso2();
+  Double_t sigma2Y = kParamCov(2,2) + hitForRec->GetBendingReso2();
+  
+  // Compute chi2
+  return dX * dX / sigma2X + dY * dY / sigma2Y;
+  
 }
 
   //__________________________________________________________________________
-Double_t AliMUONVTrackReconstructor::GetBendingMomentumFromImpactParam(Double_t ImpactParam) const
+Bool_t AliMUONVTrackReconstructor::TryOneHitForRecFast(const AliMUONTrackParam &trackParam, AliMUONHitForRec* hitForRec)
 {
-  /// Returns signed bending momentum in bending plane (GeV/c),
-  /// the sign being the sign of the charge for particles moving forward in Z,
-  /// from the impact parameter "ImpactParam" at vertex in bending plane (cm),
-  /// using simple values for dipole magnetic field.
-  return (-0.0003 * fSimpleBValue * fSimpleBLength * fSimpleBPosition /
-         ImpactParam);
+/// Test the compatibility between the track and the hitForRec within a wide fix window
+/// assuming linear propagation of the track:
+/// return kTRUE if they are compatibles
+  
+  Double_t dZ = hitForRec->GetZ() - trackParam.GetZ();
+  Double_t dX = hitForRec->GetNonBendingCoor() - (trackParam.GetNonBendingCoor() + trackParam.GetNonBendingSlope() * dZ);
+  Double_t dY = hitForRec->GetBendingCoor() - (trackParam.GetBendingCoor() + trackParam.GetBendingSlope() * dZ);
+  
+  if (TMath::Abs(dX) > fgkMaxTrackingDistanceNonBending || TMath::Abs(dY) > fgkMaxTrackingDistanceBending) return kFALSE;
+  
+  return kTRUE;
+  
 }
 
   //__________________________________________________________________________
-void AliMUONVTrackReconstructor::ValidateTracksWithTrigger(void)
+Double_t AliMUONVTrackReconstructor::TryTwoHitForRecFast(const AliMUONTrackParam &trackParamAtHit1, AliMUONHitForRec* hitForRec2,
+                                                        AliMUONTrackParam &trackParamAtHit2)
 {
-  /// Try to match track from tracking system with trigger track
-  AliMUONTrack *track;
-  AliMUONTrackParam trackParam; 
-  AliMUONTriggerTrack *triggerTrack;
+/// Test the compatibility between the track and the 2 hitForRec together (using trackParam's covariance matrix)
+/// assuming linear propagation between the two hits:
+/// return the corresponding Chi2 accounting for covariances between the 2 hitForRec
+/// return trackParamAtHit1 & 2
   
-  fMUONData->SetTreeAddress("RL");
-  fMUONData->GetRecTriggerTracks();
-  TClonesArray *recTriggerTracks = fMUONData->RecTriggerTracks();
+  // extrapolate linearly track parameters at the z position of the second hit
+  trackParamAtHit2 = trackParamAtHit1;
+  AliMUONTrackExtrap::LinearExtrapToZ(&trackParamAtHit2, hitForRec2->GetZ());
   
-  Bool_t matchTrigger;
-  Double_t distSigma[3]={1,1,0.02}; // sigma of distributions (trigger-track) X,Y,slopeY
+  // set pointer to hit2 into trackParamAtHit2
+  trackParamAtHit2.SetHitForRecPtr(hitForRec2);
+  
+  // Set differences between track and hitForRec in the bending and non bending directions
+  AliMUONHitForRec* hitForRec1 = trackParamAtHit1.GetHitForRecPtr();
+  Double_t dX1 = hitForRec1->GetNonBendingCoor() - trackParamAtHit1.GetNonBendingCoor();
+  Double_t dX2 = hitForRec2->GetNonBendingCoor() - trackParamAtHit2.GetNonBendingCoor();
+  Double_t dY1 = hitForRec1->GetBendingCoor() - trackParamAtHit1.GetBendingCoor();
+  Double_t dY2 = hitForRec2->GetBendingCoor() - trackParamAtHit2.GetBendingCoor();
+  
+  // Calculate errors and covariances
+  const TMatrixD& kParamCov1 = trackParamAtHit1.GetCovariances();
+  const TMatrixD& kParamCov2 = trackParamAtHit2.GetCovariances();
+  Double_t dZ = trackParamAtHit2.GetZ() - trackParamAtHit1.GetZ();
+  Double_t sigma2X1 = kParamCov1(0,0) + hitForRec1->GetNonBendingReso2();
+  Double_t sigma2X2 = kParamCov2(0,0) + hitForRec2->GetNonBendingReso2();
+  Double_t covX1X2  = kParamCov1(0,0) + dZ * kParamCov1(0,1);
+  Double_t sigma2Y1 = kParamCov1(2,2) + hitForRec1->GetBendingReso2();
+  Double_t sigma2Y2 = kParamCov2(2,2) + hitForRec2->GetBendingReso2();
+  Double_t covY1Y2  = kParamCov1(2,2) + dZ * kParamCov1(2,3);
+  
+  // Compute chi2
+  Double_t detX = sigma2X1 * sigma2X2 - covX1X2 * covX1X2;
+  Double_t detY = sigma2Y1 * sigma2Y2 - covY1Y2 * covY1Y2;
+  if (detX == 0. || detY == 0.) return 1.e10;
+  return   (dX1 * dX1 * sigma2X2 + dX2 * dX2 * sigma2X1 - 2. * dX1 * dX2 * covX1X2) / detX
+        + (dY1 * dY1 * sigma2Y2 + dY2 * dY2 * sigma2Y1 - 2. * dY1 * dY2 * covY1Y2) / detY;
+  
+}
+
+  //__________________________________________________________________________
+Bool_t AliMUONVTrackReconstructor::FollowLinearTrackInStation(AliMUONTrack &trackCandidate, Int_t nextStation)
+{
+  /// Follow trackCandidate in station(0..) nextStation assuming linear propagation, and search for compatible HitForRec(s)
+  /// Keep all possibilities or only the best one(s) according to the flag fgkTrackAllTracks:
+  /// kTRUE:  duplicate "trackCandidate" if there are several possibilities and add the new tracks at the end of
+  ///         fRecTracksPtr to avoid conficts with other track candidates at this current stage of the tracking procedure.
+  ///         Remove the obsolete "trackCandidate" at the end.
+  /// kFALSE: add only the best hit(s) to the "trackCandidate". Try to add a couple of hits in priority.
+  /// return kTRUE if new hits have been found (otherwise return kFALSE)
+  AliDebug(1,Form("Enter FollowLinearTrackInStation(1..) %d", nextStation+1));
+  
+  // Order the chamber according to the propagation direction (tracking starts with chamber 2):
+  // - nextStation == station(1...) 5 => forward propagation
+  // - nextStation < station(1...) 5 => backward propagation
+  Int_t ch1, ch2;
+  if (nextStation==4) {
+    ch1 = 2*nextStation+1;
+    ch2 = 2*nextStation;
+  } else {
+    ch1 = 2*nextStation;
+    ch2 = 2*nextStation+1;
+  }
+  
+  Double_t chi2WithOneHitForRec = 1.e10;
+  Double_t chi2WithTwoHitForRec = 1.e10;
+  Double_t maxChi2WithOneHitForRec = 2. * fgkSigmaToCutForTracking * fgkSigmaToCutForTracking; // 2 because 2 quantities in chi2
+  Double_t maxChi2WithTwoHitForRec = 4. * fgkSigmaToCutForTracking * fgkSigmaToCutForTracking; // 4 because 4 quantities in chi2
+  Double_t bestChi2WithOneHitForRec = maxChi2WithOneHitForRec;
+  Double_t bestChi2WithTwoHitForRec = maxChi2WithTwoHitForRec;
+  Bool_t foundOneHit = kFALSE;
+  Bool_t foundTwoHits = kFALSE;
+  AliMUONTrack *newTrack = 0x0;
+  AliMUONHitForRec *hitForRecCh1, *hitForRecCh2;
+  AliMUONTrackParam extrapTrackParamAtHit1;
+  AliMUONTrackParam extrapTrackParamAtHit2;
+  AliMUONTrackParam bestTrackParamAtHit1;
+  AliMUONTrackParam bestTrackParamAtHit2;
+  Bool_t *hitForRecCh1Used = new Bool_t[fNHitsForRecPerChamber[ch1]];
+  for (Int_t hit1 = 0; hit1 < fNHitsForRecPerChamber[ch1]; hit1++) hitForRecCh1Used[hit1] = kFALSE;
+  
+  // Get track parameters
+  AliMUONTrackParam trackParam(*(AliMUONTrackParam*)trackCandidate.GetTrackParamAtHit()->First());
+  
+  // Add MCS effect
+  AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
+  
+  // Printout for debuging
+  if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+    cout << "FollowLinearTrackInStation: look for hits in chamber(1..): " << ch2+1 << endl;
+  }
+  
+  // look for candidates in chamber 2 
+  for (Int_t hit2 = 0; hit2 < fNHitsForRecPerChamber[ch2]; hit2++) {
+    
+    hitForRecCh2 = (AliMUONHitForRec*) fHitsForRecPtr->UncheckedAt(fIndexOfFirstHitForRecPerChamber[ch2]+hit2);
+    
+    // try to add the current hit fast
+    if (!TryOneHitForRecFast(trackParam, hitForRecCh2)) continue;
+    
+    // try to add the current hit accuratly
+    extrapTrackParamAtHit2 = trackParam;
+    AliMUONTrackExtrap::LinearExtrapToZ(&extrapTrackParamAtHit2, hitForRecCh2->GetZ());
+    chi2WithOneHitForRec = TryOneHitForRec(extrapTrackParamAtHit2, hitForRecCh2, extrapTrackParamAtHit2);
+    
+    // if good chi2 then try to attach a hitForRec in the other chamber too
+    if (chi2WithOneHitForRec < maxChi2WithOneHitForRec) {
+      Bool_t foundSecondHit = kFALSE;
+      
+      // Printout for debuging
+      if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+        cout << "FollowLinearTrackInStation: found one hit in chamber(1..): " << ch2+1
+            << " (Chi2 = " << chi2WithOneHitForRec << ")" << endl;
+        cout << "                      look for second hits in chamber(1..): " << ch1+1 << " ..." << endl;
+      }
+      
+      // add MCS effect
+      AliMUONTrackExtrap::AddMCSEffect(&extrapTrackParamAtHit2,AliMUONConstants::ChamberThicknessInX0(),1.);
+      
+      for (Int_t hit1 = 0; hit1 < fNHitsForRecPerChamber[ch1]; hit1++) {
+        
+       hitForRecCh1 = (AliMUONHitForRec*) fHitsForRecPtr->UncheckedAt(fIndexOfFirstHitForRecPerChamber[ch1]+hit1);
+       
+        // try to add the current hit fast
+        if (!TryOneHitForRecFast(extrapTrackParamAtHit2, hitForRecCh1)) continue;
+       
+       // try to add the current hit in addition to the one found on the previous chamber
+       chi2WithTwoHitForRec = TryTwoHitForRecFast(extrapTrackParamAtHit2, hitForRecCh1, extrapTrackParamAtHit1);
+        
+       // if good chi2 then consider to add the 2 hitForRec to the "trackCandidate"
+       if (chi2WithTwoHitForRec < maxChi2WithTwoHitForRec) {
+         foundSecondHit = kTRUE;
+         foundTwoHits = kTRUE;
+          
+         // Printout for debuging
+         if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+           cout << "FollowLinearTrackInStation: found one hit in chamber(1..): " << ch1+1
+                << " (Chi2 = " << chi2WithTwoHitForRec << ")" << endl;
+         }
+         
+         if (fgkTrackAllTracks) {
+           // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new hitForRec's
+            newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
+           extrapTrackParamAtHit1.SetRemovable(kTRUE);
+           newTrack->AddTrackParamAtHit(&extrapTrackParamAtHit1,hitForRecCh1);
+           extrapTrackParamAtHit2.SetRemovable(kTRUE);
+           newTrack->AddTrackParamAtHit(&extrapTrackParamAtHit2,hitForRecCh2);
+           newTrack->GetTrackParamAtHit()->Sort();
+           fNRecTracks++;
+           
+           // Tag hitForRecCh1 as used
+           hitForRecCh1Used[hit1] = kTRUE;
+           
+           // Printout for debuging
+           if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+             cout << "FollowLinearTrackInStation: added two hits in station(1..): " << nextStation+1 << endl;
+             if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
+           }
+           
+          } else if (chi2WithTwoHitForRec < bestChi2WithTwoHitForRec) {
+           // keep track of the best couple of hits
+           bestChi2WithTwoHitForRec = chi2WithTwoHitForRec;
+           bestTrackParamAtHit1 = extrapTrackParamAtHit1;
+           bestTrackParamAtHit2 = extrapTrackParamAtHit2;
+          }
+         
+       }
+       
+      }
+      
+      // if no hitForRecCh1 found then consider to add hitForRecCh2 only
+      if (!foundSecondHit) {
+       foundOneHit = kTRUE;
+        
+       if (fgkTrackAllTracks) {
+         // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new hitForRec's
+          newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
+         extrapTrackParamAtHit2.SetRemovable(kFALSE);
+         newTrack->AddTrackParamAtHit(&extrapTrackParamAtHit2,hitForRecCh2);
+         newTrack->GetTrackParamAtHit()->Sort();
+         fNRecTracks++;
+         
+         // Printout for debuging
+         if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+           cout << "FollowLinearTrackInStation: added one hit in chamber(1..): " << ch2+1 << endl;
+           if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
+         }
+         
+       } else if (!foundTwoHits && chi2WithOneHitForRec < bestChi2WithOneHitForRec) {
+         // keep track of the best single hitForRec except if a couple of hits has already been found
+         bestChi2WithOneHitForRec = chi2WithOneHitForRec;
+         bestTrackParamAtHit1 = extrapTrackParamAtHit2;
+        }
+       
+      }
+      
+    }
+    
+  }
+  
+  // look for candidates in chamber 1 not already attached to a track
+  // if we want to keep all possible tracks or if no good couple of hitForRec has been found
+  if (fgkTrackAllTracks || !foundTwoHits) {
+    
+    // Printout for debuging
+    if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+      cout << "FollowLinearTrackInStation: look for single hits in chamber(1..): " << ch1+1 << endl;
+    }
+    
+    //Extrapolate trackCandidate to chamber "ch2"
+    AliMUONTrackExtrap::LinearExtrapToZ(&trackParam, AliMUONConstants::DefaultChamberZ(ch2));
+    
+    // add MCS effect for next step
+    AliMUONTrackExtrap::AddMCSEffect(&trackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
+      
+    for (Int_t hit1 = 0; hit1 < fNHitsForRecPerChamber[ch1]; hit1++) {
+      
+      hitForRecCh1 = (AliMUONHitForRec*) fHitsForRecPtr->UncheckedAt(fIndexOfFirstHitForRecPerChamber[ch1]+hit1);
+      
+      if (hitForRecCh1Used[hit1]) continue; // Skip hitForRec already used
+      
+      // try to add the current hit fast
+      if (!TryOneHitForRecFast(trackParam, hitForRecCh1)) continue;
+       
+      // try to add the current hit accuratly
+      extrapTrackParamAtHit1 = trackParam;
+      AliMUONTrackExtrap::LinearExtrapToZ(&extrapTrackParamAtHit1, hitForRecCh1->GetZ());
+      chi2WithOneHitForRec = TryOneHitForRec(extrapTrackParamAtHit1, hitForRecCh1, extrapTrackParamAtHit1);
+    
+      // if good chi2 then consider to add hitForRecCh1
+      // We do not try to attach a hitForRec in the other chamber too since it has already been done above
+      if (chi2WithOneHitForRec < maxChi2WithOneHitForRec) {
+       foundOneHit = kTRUE;
+       
+       // Printout for debuging
+       if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+         cout << "FollowLinearTrackInStation: found one hit in chamber(1..): " << ch1+1
+              << " (Chi2 = " << chi2WithOneHitForRec << ")" << endl;
+       }
+       
+       if (fgkTrackAllTracks) {
+         // copy trackCandidate into a new track put at the end of fRecTracksPtr and add the new hitForRec's
+         newTrack = new ((*fRecTracksPtr)[fRecTracksPtr->GetLast()+1]) AliMUONTrack(trackCandidate);
+         extrapTrackParamAtHit1.SetRemovable(kFALSE);
+         newTrack->AddTrackParamAtHit(&extrapTrackParamAtHit1,hitForRecCh1);
+         newTrack->GetTrackParamAtHit()->Sort();
+         fNRecTracks++;
+         
+         // Printout for debuging
+         if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+           cout << "FollowLinearTrackInStation: added one hit in chamber(1..): " << ch1+1 << endl;
+           if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
+         }
+         
+       } else if (chi2WithOneHitForRec < bestChi2WithOneHitForRec) {
+         // keep track of the best single hitForRec except if a couple of hits has already been found
+         bestChi2WithOneHitForRec = chi2WithOneHitForRec;
+         bestTrackParamAtHit1 = extrapTrackParamAtHit1;
+       }
+       
+      }
+      
+    }
+    
+  }
+  
+  // fill out the best track if required else clean up the fRecTracksPtr array
+  if (!fgkTrackAllTracks) {
+    if (foundTwoHits) {
+      bestTrackParamAtHit1.SetRemovable(kTRUE);
+      trackCandidate.AddTrackParamAtHit(&bestTrackParamAtHit1,bestTrackParamAtHit1.GetHitForRecPtr());
+      bestTrackParamAtHit2.SetRemovable(kTRUE);
+      trackCandidate.AddTrackParamAtHit(&bestTrackParamAtHit2,bestTrackParamAtHit2.GetHitForRecPtr());
+      trackCandidate.GetTrackParamAtHit()->Sort();
+      
+      // Printout for debuging
+      if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+        cout << "FollowLinearTrackInStation: added the two best hits in station(1..): " << nextStation+1 << endl;
+        if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
+      }
+      
+    } else if (foundOneHit) {
+      bestTrackParamAtHit1.SetRemovable(kFALSE);
+      trackCandidate.AddTrackParamAtHit(&bestTrackParamAtHit1,bestTrackParamAtHit1.GetHitForRecPtr());
+      trackCandidate.GetTrackParamAtHit()->Sort();
+      
+      // Printout for debuging
+      if ((AliLog::GetDebugLevel("MUON","AliMUONVTrackReconstructor") >= 1) || (AliLog::GetGlobalDebugLevel() >= 1)) {
+        cout << "FollowLinearTrackInStation: added the best hit in chamber(1..): " << bestTrackParamAtHit1.GetHitForRecPtr()->GetChamberNumber()+1 << endl;
+        if (AliLog::GetGlobalDebugLevel() >= 3) newTrack->RecursiveDump();
+      }
+      
+    } else return kFALSE;
+    
+  } else if (foundOneHit || foundTwoHits) {
+    
+    // remove obsolete track
+    fRecTracksPtr->Remove(&trackCandidate);
+    fNRecTracks--;
+    
+  } else return kFALSE;
+  
+  return kTRUE;
+  
+}
+
+  //__________________________________________________________________________
+void AliMUONVTrackReconstructor::ValidateTracksWithTrigger(AliMUONVTrackStore& trackStore,
+                                                           const AliMUONVTriggerTrackStore& triggerTrackStore,
+                                                           const AliMUONVTriggerStore& triggerStore,
+                                                           const AliMUONTrackHitPattern& trackHitPattern)
+{
+  /// Try to match track from tracking system with trigger track
+  static const Double_t kDistSigma[3]={1,1,0.02}; // sigma of distributions (trigger-track) X,Y,slopeY
+  
+  Int_t matchTrigger;
+  Int_t loTrgNum(-1);
   Double_t distTriggerTrack[3];
-  Double_t chi2MatchTrigger, xTrack, yTrack, ySlopeTrack, dTrigTrackMin2, dTrigTrack2 = 0.;
+  Double_t xTrack, yTrack, ySlopeTrack, chi2MatchTrigger, minChi2MatchTrigger, chi2;
+
+  TIter itTrack(trackStore.CreateIterator());
+  AliMUONTrack* track;
   
-  track = (AliMUONTrack*) fRecTracksPtr->First();
-  while (track) {
-    matchTrigger = kFALSE;
+  while ( ( track = static_cast<AliMUONTrack*>(itTrack()) ) )
+  {
+    matchTrigger = 0;
     chi2MatchTrigger = 0.;
+    loTrgNum = -1;
+    Int_t doubleMatch=-1; // Check if track matches 2 trigger tracks
+    Double_t doubleChi2 = -1.;
     
-    trackParam = *((AliMUONTrackParam*) (track->GetTrackParamAtHit()->Last()));
+    AliMUONTrackParam trackParam(*((AliMUONTrackParam*) (track->GetTrackParamAtHit()->Last())));
     AliMUONTrackExtrap::ExtrapToZ(&trackParam, AliMUONConstants::DefaultChamberZ(10)); // extrap to 1st trigger chamber
     
     xTrack = trackParam.GetNonBendingCoor();
     yTrack = trackParam.GetBendingCoor();
     ySlopeTrack = trackParam.GetBendingSlope();
-    dTrigTrackMin2 = 999.;
-  
-    triggerTrack = (AliMUONTriggerTrack*) recTriggerTracks->First();
-    while(triggerTrack){
-      distTriggerTrack[0] = (triggerTrack->GetX11()-xTrack)/distSigma[0];
-      distTriggerTrack[1] = (triggerTrack->GetY11()-yTrack)/distSigma[1];
-      distTriggerTrack[2] = (TMath::Tan(triggerTrack->GetThetay())-ySlopeTrack)/distSigma[2];
-      dTrigTrack2 = 0.;
-      for (Int_t iVar = 0; iVar < 3; iVar++) dTrigTrack2 += distTriggerTrack[iVar]*distTriggerTrack[iVar];
-      if (dTrigTrack2 < dTrigTrackMin2 && dTrigTrack2 < fMaxSigma2Distance) {
-        dTrigTrackMin2 = dTrigTrack2;
-        matchTrigger = kTRUE;
-        chi2MatchTrigger =  dTrigTrack2/3.; // Normalized Chi2, 3 variables (X,Y,slopeY)
+    minChi2MatchTrigger = 999.;
+    
+    AliMUONTriggerTrack *triggerTrack;
+    TIter itTriggerTrack(triggerTrackStore.CreateIterator());
+    while ( ( triggerTrack = static_cast<AliMUONTriggerTrack*>(itTriggerTrack() ) ) )
+    {
+      distTriggerTrack[0] = (triggerTrack->GetX11()-xTrack)/kDistSigma[0];
+      distTriggerTrack[1] = (triggerTrack->GetY11()-yTrack)/kDistSigma[1];
+      distTriggerTrack[2] = (TMath::Tan(triggerTrack->GetThetay())-ySlopeTrack)/kDistSigma[2];
+      chi2 = 0.;
+      for (Int_t iVar = 0; iVar < 3; iVar++) chi2 += distTriggerTrack[iVar]*distTriggerTrack[iVar];
+      chi2 /= 3.; // Normalized Chi2: 3 degrees of freedom (X,Y,slopeY)
+      if (chi2 < fMaxNormChi2MatchTrigger) 
+      {
+        Bool_t isDoubleTrack = (TMath::Abs(chi2 - minChi2MatchTrigger)<1.);
+        if (chi2 < minChi2MatchTrigger && chi2 < fMaxNormChi2MatchTrigger) 
+        {
+          if(isDoubleTrack)
+          {
+            doubleMatch = loTrgNum;
+            doubleChi2 = chi2MatchTrigger;
+          }
+          minChi2MatchTrigger = chi2;
+          chi2MatchTrigger = chi2;
+          loTrgNum = triggerTrack->GetLoTrgNum();
+          AliMUONLocalTrigger* locTrg = triggerStore.FindLocal(loTrgNum);
+          matchTrigger=1;
+          if(locTrg->LoLpt()>0)matchTrigger=2;
+          if(locTrg->LoHpt()>0)matchTrigger=3;
+        }
+        else if(isDoubleTrack) 
+        {
+          doubleMatch = triggerTrack->GetLoTrgNum();
+          doubleChi2 = chi2;
+        }
+      }
+    }
+    if(doubleMatch>=0)
+    { // If two trigger tracks match, select the one passing more trigger cuts
+      AliDebug(1, Form("Two candidates found: %i and %i",loTrgNum,doubleMatch));
+      AliMUONLocalTrigger* locTrg1 = triggerStore.FindLocal(doubleMatch);
+      if((locTrg1->LoLpt()>0 && matchTrigger<2) || (locTrg1->LoHpt() && matchTrigger<3))
+      {
+        if(locTrg1->LoHpt()>0)matchTrigger=3;
+        else matchTrigger=2;
+        loTrgNum = doubleMatch;
+        chi2MatchTrigger=doubleChi2;
       }
-      triggerTrack = (AliMUONTriggerTrack*) recTriggerTracks->After(triggerTrack);
     }
     
     track->SetMatchTrigger(matchTrigger);
+    track->SetLoTrgNum(loTrgNum);
     track->SetChi2MatchTrigger(chi2MatchTrigger);
     
-    track = (AliMUONTrack*) fRecTracksPtr->After(track);
-  }
-
-  return;
-}
-
-//__________________________________________________________________________
-void AliMUONVTrackReconstructor::EventReconstructTrigger(void)
-{
-  /// To reconstruct trigger for one event
-  AliDebug(1,"Enter EventReconstructTrigger");
-  MakeTriggerTracks();  
-  return;
+    AliMUONLocalTrigger* locTrg = static_cast<AliMUONLocalTrigger*>(triggerStore.FindLocal(loTrgNum));
+    
+    if (locTrg)
+    {    
+      track->SetLocalTrigger(locTrg->LoCircuit(),
+                             locTrg->LoStripX(),
+                             locTrg->LoStripY(),
+                             locTrg->LoDev(),
+                             locTrg->LoLpt(),
+                             locTrg->LoHpt());
+    }    
+  }  
+  
+  trackHitPattern.GetHitPattern(trackStore,triggerStore);
 }
 
   //__________________________________________________________________________
-Bool_t AliMUONVTrackReconstructor::MakeTriggerTracks(void)
+void AliMUONVTrackReconstructor::EventReconstructTrigger(const AliMUONTriggerCircuit& circuit,
+                                                         const AliMUONVTriggerStore& triggerStore,
+                                                         AliMUONVTriggerTrackStore& triggerTrackStore)
 {
-    // To make the trigger tracks from Local Trigger
-  AliDebug(1, "Enter MakeTriggerTracks");
-  
-  TTree* treeR;
-  Int_t nTRentries;
-  UChar_t gloTrigPat;
-  TClonesArray *localTrigger;
-  TClonesArray *globalTrigger;
-  AliMUONLocalTrigger *locTrg;
-  AliMUONGlobalTrigger *gloTrg;
-
-  treeR = fMUONData->TreeR();
-  if (!treeR) {
-    AliWarning("TreeR is not loaded");
-    return kFALSE;
-  }
+  /// To make the trigger tracks from Local Trigger
+  AliDebug(1, "");
+  
+  AliMUONGlobalTrigger* globalTrigger = triggerStore.Global();
   
-  nTRentries = Int_t(treeR->GetEntries());
-   
-  treeR->GetEvent(0); // only one entry  
+  UChar_t gloTrigPat = 0;
 
-  if (!(fMUONData->IsTriggerBranchesInTree())) {
-    AliWarning(Form("Trigger information is not avalaible, nTRentries = %d not equal to 1",nTRentries));
-    return kFALSE;
+  if (globalTrigger)
+  {
+    gloTrigPat = globalTrigger->GetGlobalResponse();
   }
-
-  fMUONData->SetTreeAddress("TC");
-  fMUONData->GetTrigger();
-
-  // global trigger for trigger pattern
-  gloTrigPat = 0;
-  globalTrigger = fMUONData->GlobalTrigger(); 
-  gloTrg = (AliMUONGlobalTrigger*)globalTrigger->UncheckedAt(0);
-  if (gloTrg)
-    gloTrigPat = gloTrg->GetGlobalResponse();
-
-  // local trigger for tracking 
-  localTrigger = fMUONData->LocalTrigger();    
-  Int_t nlocals = (Int_t) (localTrigger->GetEntries());
+  
+  TIter next(triggerStore.CreateIterator());
+  AliMUONLocalTrigger* locTrg(0x0);
 
   Float_t z11 = AliMUONConstants::DefaultChamberZ(10);
   Float_t z21 = AliMUONConstants::DefaultChamberZ(12);
-
-  Float_t y11 = 0.;
-  Int_t stripX21 = 0;
-  Float_t y21 = 0.;
-  Float_t x11 = 0.;
-
-  for (Int_t i=0; i<nlocals; i++) { // loop on Local Trigger
-    locTrg = (AliMUONLocalTrigger*)localTrigger->UncheckedAt(i);      
-
-    AliDebug(1, "AliMUONTrackReconstructor::MakeTriggerTrack using NEW trigger \n");
-    AliMUONTriggerCircuit* circuit = 
-      (AliMUONTriggerCircuit*)fTriggerCircuit->At(locTrg->LoCircuit()-1); // -1 !!!
-
-    y11 = circuit->GetY11Pos(locTrg->LoStripX()); 
-    stripX21 = locTrg->LoStripX()+locTrg->LoDev()+1;
-    y21 = circuit->GetY21Pos(stripX21);       
-    x11 = circuit->GetX11Pos(locTrg->LoStripY());
-    
-    AliDebug(1, Form(" MakeTriggerTrack %d %d %d %d %d %f %f %f \n",i,locTrg->LoCircuit(),
-                    locTrg->LoStripX(),locTrg->LoStripX()+locTrg->LoDev()+1,locTrg->LoStripY(),y11, y21, x11));
+      
+  AliMUONTriggerTrack triggerTrack;
+  
+  while ( ( locTrg = static_cast<AliMUONLocalTrigger*>(next()) ) )
+  {
+    Bool_t xTrig=kFALSE;
+    Bool_t yTrig=kFALSE;
     
-    Float_t thetax = TMath::ATan2( x11 , z11 );
-    Float_t thetay = TMath::ATan2( (y21-y11) , (z21-z11) );
+    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
     
-    fTriggerTrack->SetX11(x11);
-    fTriggerTrack->SetY11(y11);
-    fTriggerTrack->SetThetax(thetax);
-    fTriggerTrack->SetThetay(thetay);
-    fTriggerTrack->SetGTPattern(gloTrigPat);
-         
-    fMUONData->AddRecTriggerTrack(*fTriggerTrack);
+    if (xTrig && yTrig) 
+    { // make Trigger Track if trigger in X and Y
+      
+      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 stripX21 = locTrg->LoStripX()+deviation+1;
+      Float_t y21 = circuit.GetY21Pos(localBoardId, stripX21);       
+      Float_t x11 = circuit.GetX11Pos(localBoardId, locTrg->LoStripY());
+      
+      AliDebug(1, Form(" MakeTriggerTrack %d %d %d %d %f %f %f \n",locTrg->LoCircuit(),
+                       locTrg->LoStripX(),locTrg->LoStripX()+locTrg->LoDev()+1,locTrg->LoStripY(),y11, y21, x11));
+      
+      Float_t thetax = TMath::ATan2( x11 , z11 );
+      Float_t thetay = TMath::ATan2( (y21-y11) , (z21-z11) );
+      
+      triggerTrack.SetX11(x11);
+      triggerTrack.SetY11(y11);
+      triggerTrack.SetThetax(thetax);
+      triggerTrack.SetThetay(thetay);
+      triggerTrack.SetGTPattern(gloTrigPat);
+      triggerTrack.SetLoTrgNum(localBoardId);
+      
+      triggerTrackStore.Add(triggerTrack);
+    } // board is fired 
   } // end of loop on Local Trigger
-  
-  return kTRUE;    
-}
-
-//__________________________________________________________________________
-void AliMUONVTrackReconstructor::EventDumpTrigger(void)
-{
-  /// Dump reconstructed trigger event 
-  /// and the particle parameters
-  AliMUONTriggerTrack *triggertrack ;
-  Int_t nTriggerTracks = fMUONData->RecTriggerTracks()->GetEntriesFast();
-  AliDebug(1, "****** enter EventDumpTrigger ******");
-  AliDebug(1, Form("Number of Reconstructed tracks : %d ",  nTriggerTracks));
-  
-  // Loop over reconstructed tracks
-  for (Int_t trackIndex = 0; trackIndex < nTriggerTracks; trackIndex++) {
-    triggertrack = (AliMUONTriggerTrack*)fMUONData->RecTriggerTracks()->At(trackIndex);
-      printf(" trigger track number %i x11=%f y11=%f thetax=%f thetay=%f \n",
-            trackIndex,
-            triggertrack->GetX11(),triggertrack->GetY11(),
-            triggertrack->GetThetax(),triggertrack->GetThetay());      
-  } 
 }