]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONTrack.cxx
Adding AliMUONQAIndices in base
[u/mrichter/AliRoot.git] / MUON / AliMUONTrack.cxx
index 10f0460415bf6e52819e31ecaf8648acaad8465d..f1e32e0dd735eeee967378f9f78913debc0ad3d3 100644 (file)
 #include "AliMUONVCluster.h"
 #include "AliMUONVClusterStore.h"
 #include "AliMUONObjectPair.h"
-#include "AliMUONConstants.h"
 #include "AliMUONTrackExtrap.h"
+#include "AliMUONConstants.h"
+#include "AliMUONTrackParam.h"
 
 #include "AliLog.h"
 
 #include <TMath.h>
-#include <TMatrixD.h>
 
 #include <Riostream.h>
 
 ClassImp(AliMUONTrack) // Class implementation in ROOT context
 /// \endcond
 
+
+const Double_t AliMUONTrack::fgkMaxChi2 = 1.e10; ///< maximum chi2 above which the track can be considered as abnormal
+
+
 //__________________________________________________________________________
 AliMUONTrack::AliMUONTrack()
   : TObject(),
@@ -53,9 +57,8 @@ AliMUONTrack::AliMUONTrack()
     fGlobalChi2(-1.),
     fImproved(kFALSE),
     fMatchTrigger(-1),
-    floTrgNum(-1),
     fChi2MatchTrigger(0.),
-    fTrackID(0),
+    fTrackID(-1),
     fTrackParamAtVertex(0x0),
     fHitsPatternInTrigCh(0),
     fLocalTrigger(0)
@@ -77,9 +80,8 @@ AliMUONTrack::AliMUONTrack(AliMUONObjectPair *segment, Double_t bendingVertexDis
     fGlobalChi2(0.),
     fImproved(kFALSE),
     fMatchTrigger(-1),
-    floTrgNum(-1),    
     fChi2MatchTrigger(0.),
-    fTrackID(0),
+    fTrackID(-1),
     fTrackParamAtVertex(0x0),
     fHitsPatternInTrigCh(0),
     fLocalTrigger(0)
@@ -141,24 +143,31 @@ AliMUONTrack::AliMUONTrack(AliMUONObjectPair *segment, Double_t bendingVertexDis
   paramCov(3,2) = paramCov(2,3);
   paramCov(3,3) = ( firstCluster->GetErrY2() + lastCluster->GetErrY2() ) / dZ / dZ;
   // Inverse bending momentum (vertex resolution + bending slope resolution + 10% error on dipole parameters+field)
-  paramCov(4,4) = ( ( bendingVertexDispersion*bendingVertexDispersion +
-                   (z1 * z1 * lastCluster->GetErrY2() + z2 * z2 * firstCluster->GetErrY2()) / dZ / dZ) /
-                  bendingImpact / bendingImpact + 0.1 * 0.1) * inverseBendingMomentum * inverseBendingMomentum ;
-  paramCov(2,4) = - z2 * firstCluster->GetErrY2() * inverseBendingMomentum / bendingImpact / dZ;
-  paramCov(4,2) = paramCov(2,4);
-  paramCov(3,4) = - (z1 * lastCluster->GetErrY2() + z2 * firstCluster->GetErrY2()) * inverseBendingMomentum / bendingImpact / dZ / dZ;
-  paramCov(4,3) = paramCov(3,4);
-  
-  // Set covariances
+  if (AliMUONTrackExtrap::IsFieldON()) {
+    paramCov(4,4) = ( ( bendingVertexDispersion*bendingVertexDispersion +
+                      (z1 * z1 * lastCluster->GetErrY2() + z2 * z2 * firstCluster->GetErrY2()) / dZ / dZ) /
+                    bendingImpact / bendingImpact + 0.1 * 0.1) * inverseBendingMomentum * inverseBendingMomentum ;
+    paramCov(2,4) = - z2 * firstCluster->GetErrY2() * inverseBendingMomentum / bendingImpact / dZ;
+    paramCov(4,2) = paramCov(2,4);
+    paramCov(3,4) = - (z1 * lastCluster->GetErrY2() + z2 * firstCluster->GetErrY2()) * inverseBendingMomentum / bendingImpact / dZ / dZ;
+    paramCov(4,3) = paramCov(3,4);
+  } else paramCov(4,4) = inverseBendingMomentum*inverseBendingMomentum;
   trackParamAtFirstCluster.SetCovariances(paramCov);
   
   // Compute and set track parameters covariances at last cluster
-  paramCov(1,0) = - paramCov(1,0);
-  paramCov(0,1) = - paramCov(0,1);
-  paramCov(3,2) = - paramCov(3,2);
-  paramCov(2,3) = - paramCov(2,3);
-  paramCov(2,4) = z1 * lastCluster->GetErrY2() * inverseBendingMomentum / bendingImpact / dZ;
-  paramCov(4,2) = paramCov(2,4);
+  // Non bending plane
+  paramCov(0,0) = lastCluster->GetErrX2();
+  paramCov(0,1) = - lastCluster->GetErrX2() / dZ;
+  paramCov(1,0) = paramCov(0,1);
+  // Bending plane
+  paramCov(2,2) = lastCluster->GetErrY2();
+  paramCov(2,3) = - lastCluster->GetErrY2() / dZ;
+  paramCov(3,2) = paramCov(2,3);
+  // Inverse bending momentum (vertex resolution + bending slope resolution + 10% error on dipole parameters+field)
+  if (AliMUONTrackExtrap::IsFieldON()) {
+    paramCov(2,4) = z1 * lastCluster->GetErrY2() * inverseBendingMomentum / bendingImpact / dZ;
+    paramCov(4,2) = paramCov(2,4);
+  }
   trackParamAtLastCluster.SetCovariances(paramCov);
   
   // Add track parameters at clusters
@@ -179,7 +188,6 @@ AliMUONTrack::AliMUONTrack(const AliMUONTrack& track)
     fGlobalChi2(track.fGlobalChi2),
     fImproved(track.fImproved),
     fMatchTrigger(track.fMatchTrigger),
-    floTrgNum(track.floTrgNum),    
     fChi2MatchTrigger(track.fChi2MatchTrigger),
     fTrackID(track.fTrackID),
     fTrackParamAtVertex(0x0),
@@ -264,7 +272,6 @@ AliMUONTrack & AliMUONTrack::operator=(const AliMUONTrack& track)
   fGlobalChi2         =  track.fGlobalChi2;
   fImproved           =  track.fImproved;
   fMatchTrigger       =  track.fMatchTrigger;
-  floTrgNum           =  track.floTrgNum;
   fChi2MatchTrigger   =  track.fChi2MatchTrigger;
   fTrackID            =  track.fTrackID; 
   fHitsPatternInTrigCh = track.fHitsPatternInTrigCh;
@@ -309,9 +316,8 @@ void AliMUONTrack::Reset()
   fGlobalChi2 = -1.;
   fImproved = kFALSE;
   fMatchTrigger = -1;
-  floTrgNum = -1;
   fChi2MatchTrigger = 0.;
-  fTrackID = 0;
+  fTrackID = -1;
   fHitsPatternInTrigCh = 0;
   fLocalTrigger = 0;
   delete fTrackParamAtCluster; fTrackParamAtCluster = 0x0;
@@ -375,15 +381,17 @@ void AliMUONTrack::RemoveTrackParamAtCluster(AliMUONTrackParam *trackParam)
 }
 
   //__________________________________________________________________________
-void AliMUONTrack::UpdateTrackParamAtCluster()
+Bool_t AliMUONTrack::UpdateTrackParamAtCluster()
 {
   /// Update track parameters at each attached cluster
+  /// Return kFALSE in case of failure (i.e. extrapolation problem)
   
   if (GetNClusters() == 0) {
     AliWarning("no cluster attached to the track");
-    return;
+    return kFALSE;
   }
   
+  Bool_t extrapStatus = kTRUE;
   AliMUONTrackParam* startingTrackParam = (AliMUONTrackParam*) fTrackParamAtCluster->First();
   AliMUONTrackParam* trackParamAtCluster = (AliMUONTrackParam*) fTrackParamAtCluster->After(startingTrackParam);
   while (trackParamAtCluster) {
@@ -393,26 +401,32 @@ void AliMUONTrack::UpdateTrackParamAtCluster()
     trackParamAtCluster->SetZ(startingTrackParam->GetZ());
     
     // extrapolation to the given z
-    AliMUONTrackExtrap::ExtrapToZ(trackParamAtCluster, trackParamAtCluster->GetClusterPtr()->GetZ());
+    if (!AliMUONTrackExtrap::ExtrapToZ(trackParamAtCluster, trackParamAtCluster->GetClusterPtr()->GetZ())) extrapStatus = kFALSE;
     
     // prepare next step
     startingTrackParam = trackParamAtCluster;
     trackParamAtCluster = (AliMUONTrackParam*) (fTrackParamAtCluster->After(trackParamAtCluster));
   }
 
+  // set global chi2 to max value in case of problem during track extrapolation
+  if (!extrapStatus) SetGlobalChi2(2.*MaxChi2());
+  return extrapStatus;
+  
 }
 
   //__________________________________________________________________________
-void AliMUONTrack::UpdateCovTrackParamAtCluster()
+Bool_t AliMUONTrack::UpdateCovTrackParamAtCluster()
 {
   /// Update track parameters and their covariances at each attached cluster
   /// Include effects of multiple scattering in chambers
+  /// Return kFALSE in case of failure (i.e. extrapolation problem)
   
   if (GetNClusters() == 0) {
     AliWarning("no cluster attached to the track");
-    return;
+    return kFALSE;
   }
   
+  Bool_t extrapStatus = kTRUE;
   AliMUONTrackParam* startingTrackParam = (AliMUONTrackParam*) fTrackParamAtCluster->First();
   AliMUONTrackParam* trackParamAtCluster = (AliMUONTrackParam*) fTrackParamAtCluster->After(startingTrackParam);
   Int_t expectedChamber = startingTrackParam->GetClusterPtr()->GetChamberId() + 1;
@@ -425,20 +439,20 @@ void AliMUONTrack::UpdateCovTrackParamAtCluster()
     trackParamAtCluster->SetCovariances(startingTrackParam->GetCovariances());
     
     // add MCS effect
-    AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(),1.);
+    AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(expectedChamber-1),-1.);
     
     // add MCS in missing chambers if any
     currentChamber = trackParamAtCluster->GetClusterPtr()->GetChamberId();
     while (currentChamber > expectedChamber) {
       // extrapolation to the missing chamber
-      AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, AliMUONConstants::DefaultChamberZ(expectedChamber));
+      if (!AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, AliMUONConstants::DefaultChamberZ(expectedChamber))) extrapStatus = kFALSE;
       // add MCS effect
-      AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(),1.);
+      AliMUONTrackExtrap::AddMCSEffect(trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(expectedChamber),-1.);
       expectedChamber++;
     }
     
     // extrapolation to the z of the current cluster
-    AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, trackParamAtCluster->GetClusterPtr()->GetZ());
+    if (!AliMUONTrackExtrap::ExtrapToZCov(trackParamAtCluster, trackParamAtCluster->GetClusterPtr()->GetZ())) extrapStatus = kFALSE;
     
     // prepare next step
     expectedChamber = currentChamber + 1;
@@ -446,67 +460,142 @@ void AliMUONTrack::UpdateCovTrackParamAtCluster()
     trackParamAtCluster = (AliMUONTrackParam*) (fTrackParamAtCluster->After(trackParamAtCluster));
   }
   
+  // set global chi2 to max value in case of problem during track extrapolation
+  if (!extrapStatus) SetGlobalChi2(2.*MaxChi2());
+  return extrapStatus;
+  
 }
 
   //__________________________________________________________________________
-Bool_t AliMUONTrack::IsValid(UInt_t requestedStationMask)
+Bool_t AliMUONTrack::IsValid(UInt_t requestedStationMask, Bool_t request2ChInSameSt45)
 {
-  /// check the validity of the current track (at least one cluster per requested station)
+  /// check the validity of the current track:
+  /// at least one cluster per requested station
+  /// and at least 2 chambers in stations 4 & 5 that contain cluster(s)
+  /// + if request2ChInSameSt45 = kTRUE: 2 chambers hit in the same station (4 or 5)
   
   Int_t nClusters = GetNClusters();
   AliMUONTrackParam *trackParam;
-  Int_t currentStation(0);
+  Int_t currentCh, currentSt, previousCh = -1, nChHitInSt4 = 0, nChHitInSt5 = 0;
   UInt_t presentStationMask(0);
   
+  // first loop over clusters
   for (Int_t i = 0; i < nClusters; i++) {
     trackParam = (AliMUONTrackParam*) fTrackParamAtCluster->UncheckedAt(i);
-    currentStation = trackParam->GetClusterPtr()->GetChamberId()/2;
-    presentStationMask |= ( 1 << currentStation );
+    
+    currentCh = trackParam->GetClusterPtr()->GetChamberId();
+    currentSt = currentCh/2;
+    
+    // build present station mask
+    presentStationMask |= ( 1 << currentSt );
+    
+    // count the number of chambers hit in station 4 that contain cluster(s)
+    if (currentSt == 3 && currentCh != previousCh) {
+      nChHitInSt4++;
+      previousCh = currentCh;
+    }
+    
+    // count the number of chambers hit in station 5 that contain cluster(s)
+    if (currentSt == 4 && currentCh != previousCh) {
+      nChHitInSt5++;
+      previousCh = currentCh;
+    }
+    
   }
   
-  return ( (requestedStationMask & presentStationMask) == requestedStationMask );
+  // at least one cluster per requested station
+  if ((requestedStationMask & presentStationMask) != requestedStationMask) return kFALSE;
+  
+  // 2 chambers hit in the same station (4 or 5)
+  if (request2ChInSameSt45) return (nChHitInSt4 == 2 || nChHitInSt5 == 2);
+  // or 2 chambers hit in station 4 & 5 together
+  else return (nChHitInSt4+nChHitInSt5 >= 2);
+  
 }
 
   //__________________________________________________________________________
 void AliMUONTrack::TagRemovableClusters(UInt_t requestedStationMask) {
   /// Identify clusters that can be removed from the track,
-  /// with the only requirement to have at least 1 cluster per requested station
+  /// with the only requirements to have at least 1 cluster per requested station
+  /// and at least 2 chambers over 4 in stations 4 & 5 that contain cluster(s)
   
   Int_t nClusters = GetNClusters();
   AliMUONTrackParam *trackParam, *nextTrackParam;
-  Int_t currentCh, nextCh;
+  Int_t currentCh, nextCh, currentSt, nextSt, previousCh = -1, nChHitInSt45 = 0;
   
-  // reset flags to kFALSE for all clusters in required station
+  // first loop over clusters
   for (Int_t i = 0; i < nClusters; i++) {
     trackParam = (AliMUONTrackParam*) fTrackParamAtCluster->UncheckedAt(i);
-    Int_t stationId = ( 1 << (trackParam->GetClusterPtr()->GetChamberId()/2) );
-    UInt_t m = ( 1 << stationId );
-    if ( m & requestedStationMask )
-      trackParam->SetRemovable(kFALSE);
+    
+    currentCh = trackParam->GetClusterPtr()->GetChamberId();
+    currentSt = currentCh/2;
+    
+    // reset flags to kFALSE for all clusters in required station
+    if ((1 << currentSt) & requestedStationMask) trackParam->SetRemovable(kFALSE);
     else trackParam->SetRemovable(kTRUE);
+    
+    // count the number of chambers in station 4 & 5 that contain cluster(s)
+    if (currentCh > 5 && currentCh != previousCh) {
+      nChHitInSt45++;
+      previousCh = currentCh;
+    }
+    
   }
   
-  // loop over track parameters
+  // second loop over clusters
   for (Int_t i = 0; i < nClusters; i++) {
     trackParam = (AliMUONTrackParam*) fTrackParamAtCluster->UncheckedAt(i);
     
     currentCh = trackParam->GetClusterPtr()->GetChamberId();
+    currentSt = currentCh/2;
     
-    // loop over next track parameters
-    for (Int_t j = i+1; j < nClusters; j++) {
-      nextTrackParam = (AliMUONTrackParam*) fTrackParamAtCluster->UncheckedAt(j);
+    // make sure they are more than 2 clusters in 2 different chambers of stations 4 & 5
+    // but 2 clusters in he same chamber will still be flagged as removable
+    if (nChHitInSt45 < 3 && currentSt > 2) {
+      
+      if (i == nClusters-1) {
+       
+       trackParam->SetRemovable(kFALSE);
+      
+      } else {
+       
+       nextTrackParam = (AliMUONTrackParam*) fTrackParamAtCluster->UncheckedAt(i+1);
+       nextCh = nextTrackParam->GetClusterPtr()->GetChamberId();
+       
+       // set clusters in the same chamber as being removable
+       if (nextCh == currentCh) {
+         trackParam->SetRemovable(kTRUE);
+         nextTrackParam->SetRemovable(kTRUE);
+         i++; // skip cluster already checked
+       } else {
+         trackParam->SetRemovable(kFALSE);
+       }
+       
+      }
       
-      nextCh = nextTrackParam->GetClusterPtr()->GetChamberId();
+    } else {
       
-      // check if the 2 clusters are on the same station
-      if (nextCh/2 != currentCh/2) break;
+      // skip clusters already flag as removable
+      if (trackParam->IsRemovable()) continue;
       
-      // set clusters in the same station as being removable
-      trackParam->SetRemovable(kTRUE);
-      nextTrackParam->SetRemovable(kTRUE);
+      // loop over next track parameters
+      for (Int_t j = i+1; j < nClusters; j++) {
+       nextTrackParam = (AliMUONTrackParam*) fTrackParamAtCluster->UncheckedAt(j);
+       
+       nextCh = nextTrackParam->GetClusterPtr()->GetChamberId();
+       nextSt = nextCh/2;
+       
+       // set clusters in the same station as being removable
+       if (nextSt == currentSt) {
+         trackParam->SetRemovable(kTRUE);
+         nextTrackParam->SetRemovable(kTRUE);
+         i++; // skip cluster already checked
+       }
+       
+      }
       
     }
-    
+      
   }
     
 }
@@ -640,12 +729,12 @@ Double_t AliMUONTrack::ComputeGlobalChi2(Bool_t accountForMCS)
   /// Compute the chi2 of the track accounting for multiple scattering or not according to the flag
   /// - Assume that track parameters at each cluster are corrects
   /// - Assume the cluster weights matrices are corrects
-  /// - Return negative value if chi2 computation failed
+  /// - Return a value of chi2 higher than the maximum allowed if computation failed
   AliDebug(1,"Enter ComputeGlobalChi2");
   
   if (!fTrackParamAtCluster) {
     AliWarning("no cluster attached to this track");
-    return 1.e10;
+    return 2.*MaxChi2();
   }
   
   Double_t chi2 = 0.;
@@ -864,7 +953,7 @@ void AliMUONTrack::ComputeMCSCovariances(TMatrixD& mcsCovariances) const
         AliMUONTrackExtrap::ExtrapToZ(&extrapTrackParam, zMCS[size]);
         
         // Save multiple scattering dispersion angle in missing chamber
-        mcsAngle2[size] = AliMUONTrackExtrap::GetMCSAngle2(extrapTrackParam,AliMUONConstants::ChamberThicknessInX0(),1.);
+        mcsAngle2[size] = AliMUONTrackExtrap::GetMCSAngle2(extrapTrackParam,AliMUONConstants::ChamberThicknessInX0(expectedChamber),1.);
         
       } else mcsAngle2[size] = 0.;
       
@@ -876,7 +965,7 @@ void AliMUONTrack::ComputeMCSCovariances(TMatrixD& mcsCovariances) const
     zMCS[size] = trackParamAtCluster->GetZ();
     
     // Save multiple scattering dispersion angle in current chamber
-    mcsAngle2[size] = AliMUONTrackExtrap::GetMCSAngle2(*trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(),1.);
+    mcsAngle2[size] = AliMUONTrackExtrap::GetMCSAngle2(*trackParamAtCluster,AliMUONConstants::ChamberThicknessInX0(currentChamber),1.);
     
     // Save indice in zMCS array corresponding to the current cluster
     indices[iCluster] = size;
@@ -919,50 +1008,88 @@ Int_t AliMUONTrack::ClustersInCommon(AliMUONTrack* track) const
   /// Returns the number of clusters in common between the current track ("this")
   /// and the track pointed to by "track".
   if (!fTrackParamAtCluster || !this->fTrackParamAtCluster) return 0;
+  Int_t nCluster1 = this->GetNClusters();
+  Int_t nCluster2 = track->GetNClusters();
   Int_t clustersInCommon = 0;
   AliMUONTrackParam *trackParamAtCluster1, *trackParamAtCluster2;
   // Loop over clusters of first track
-  trackParamAtCluster1 = (AliMUONTrackParam*) this->fTrackParamAtCluster->First();
-  while (trackParamAtCluster1) {
+  for(Int_t iCluster1 = 0; iCluster1 < nCluster1; iCluster1++) {
+    trackParamAtCluster1 = (AliMUONTrackParam*) this->fTrackParamAtCluster->UncheckedAt(iCluster1);
     // Loop over clusters of second track
-    trackParamAtCluster2 = (AliMUONTrackParam*) track->fTrackParamAtCluster->First();
-    while (trackParamAtCluster2) {
+    for(Int_t iCluster2 = 0; iCluster2 < nCluster2; iCluster2++) {
+      trackParamAtCluster2 = (AliMUONTrackParam*) track->fTrackParamAtCluster->UncheckedAt(iCluster2);
       // Increment "clustersInCommon" if both trackParamAtCluster1 & 2 point to the same cluster
       if ((trackParamAtCluster1->GetClusterPtr()) == (trackParamAtCluster2->GetClusterPtr())) {
-        clustersInCommon++;
+       clustersInCommon++;
        break;
       }
-      trackParamAtCluster2 = (AliMUONTrackParam*) track->fTrackParamAtCluster->After(trackParamAtCluster2);
-    } // trackParamAtCluster2
-    trackParamAtCluster1 = (AliMUONTrackParam*) this->fTrackParamAtCluster->After(trackParamAtCluster1);
-  } // trackParamAtCluster1
+    }
+  }
+  return clustersInCommon;
+}
+
+  //__________________________________________________________________________
+Int_t AliMUONTrack::ClustersInCommonInSt345(AliMUONTrack* track) const
+{
+  /// Returns the number of clusters in common on stations 3, 4 and 5
+  /// between the current track ("this") and the track pointed to by "track".
+  if (!fTrackParamAtCluster || !this->fTrackParamAtCluster) return 0;
+  Int_t nCluster1 = this->GetNClusters();
+  Int_t nCluster2 = track->GetNClusters();
+  Int_t clustersInCommon = 0;
+  AliMUONTrackParam *trackParamAtCluster1, *trackParamAtCluster2;
+  // Loop over clusters of first track
+  for(Int_t iCluster1 = 0; iCluster1 < nCluster1; iCluster1++) {
+    trackParamAtCluster1 = (AliMUONTrackParam*) this->fTrackParamAtCluster->UncheckedAt(iCluster1);
+    if (trackParamAtCluster1->GetClusterPtr()->GetChamberId() < 4) continue;
+    // Loop over clusters of second track
+    for(Int_t iCluster2 = 0; iCluster2 < nCluster2; iCluster2++) {
+      trackParamAtCluster2 = (AliMUONTrackParam*) track->fTrackParamAtCluster->UncheckedAt(iCluster2);
+      if (trackParamAtCluster2->GetClusterPtr()->GetChamberId() < 4) continue;
+      // Increment "clustersInCommon" if both trackParamAtCluster1 & 2 point to the same cluster
+      if ((trackParamAtCluster1->GetClusterPtr()) == (trackParamAtCluster2->GetClusterPtr())) {
+       clustersInCommon++;
+       break;
+      }
+    }
+  }
   return clustersInCommon;
 }
 
+  //__________________________________________________________________________
+Int_t AliMUONTrack::GetNDF() const
+{
+  /// return the number of degrees of freedom
+  
+  Int_t ndf = 2 * GetNClusters() - 5;
+  return (ndf > 0) ? ndf : 0;
+}
+
   //__________________________________________________________________________
 Double_t AliMUONTrack::GetNormalizedChi2() const
 {
-  /// return the chi2 value divided by the number of degrees of freedom (or 1.e10 if ndf < 0)
+  /// return the chi2 value divided by the number of degrees of freedom (or FLT_MAX if ndf <= 0)
   
-  Double_t numberOfDegFree = (2. * GetNClusters() - 5.);
-  if (numberOfDegFree > 0.) return fGlobalChi2 / numberOfDegFree;
-  else return fGlobalChi2; // system is under-constraint
+  Double_t ndf = (Double_t) GetNDF();
+  return (ndf > 0.) ? fGlobalChi2 / ndf : 2.*MaxChi2();
 }
 
   //__________________________________________________________________________
-Bool_t* AliMUONTrack::CompatibleTrack(AliMUONTrack *track, Double_t sigmaCut) const
+Int_t AliMUONTrack::FindCompatibleClusters(AliMUONTrack &track, Double_t sigmaCut, Bool_t compatibleCluster[10]) const
 {
-  /// for each chamber: return kTRUE (kFALSE) if clusters are compatible (not compatible)
+  /// Try to match clusters from this track with clusters from the given track within the provided sigma cut:
+  /// - Fill the array compatibleCluster[iCh] with kTRUE if a compatible cluster has been found in chamber iCh.
+  /// - Return the number of clusters of "this" track matched with one cluster of the given track.
   AliMUONTrackParam *trackParamAtCluster1, *trackParamAtCluster2;
   AliMUONVCluster *cluster1, *cluster2;
-  Double_t chi2, dX, dY, dZ;
+  Double_t chi2, dX, dY;
   Double_t chi2Max = sigmaCut * sigmaCut;
-  Double_t dZMax = 1.; // 1 cm
   
-  Bool_t *compatibleCluster = new Bool_t[AliMUONConstants::NTrackingCh()]; 
+  // initialization
+  Int_t nMatchClusters = 0;
   for ( Int_t ch = 0; ch < AliMUONConstants::NTrackingCh(); ch++) compatibleCluster[ch] = kFALSE;
 
-  if (!fTrackParamAtCluster || !this->fTrackParamAtCluster) return compatibleCluster;
+  if (!track.fTrackParamAtCluster || !this->fTrackParamAtCluster) return nMatchClusters;
   
   // Loop over clusters of first track
   trackParamAtCluster1 = (AliMUONTrackParam*) this->fTrackParamAtCluster->First();
@@ -971,36 +1098,49 @@ Bool_t* AliMUONTrack::CompatibleTrack(AliMUONTrack *track, Double_t sigmaCut) co
     cluster1 = trackParamAtCluster1->GetClusterPtr();
     
     // Loop over clusters of second track
-    trackParamAtCluster2 = (AliMUONTrackParam*) track->fTrackParamAtCluster->First();
+    trackParamAtCluster2 = (AliMUONTrackParam*) track.fTrackParamAtCluster->First();
     while (trackParamAtCluster2) {
       
       cluster2 = trackParamAtCluster2->GetClusterPtr();
       
       //prepare next step
-      trackParamAtCluster2 = (AliMUONTrackParam*) track->fTrackParamAtCluster->After(trackParamAtCluster2);
+      trackParamAtCluster2 = (AliMUONTrackParam*) track.fTrackParamAtCluster->After(trackParamAtCluster2);
       
-      // z direction
-      dZ = cluster1->GetZ() - cluster2->GetZ();
-      if (dZ > dZMax) continue;
+      // check DE Id
+      if (cluster1->GetDetElemId() != cluster2->GetDetElemId()) continue;
       
-      // non bending direction
+      // check local chi2
       dX = cluster1->GetX() - cluster2->GetX();
-      chi2 = dX * dX / (cluster1->GetErrX2() + cluster2->GetErrX2());
-      if (chi2 > chi2Max) continue;
-      
-      // bending direction
       dY = cluster1->GetY() - cluster2->GetY();
-      chi2 = dY * dY / (cluster1->GetErrY2() + cluster2->GetErrY2());
-      if (chi2 > chi2Max) continue;
+      chi2 = dX * dX / (cluster1->GetErrX2() + cluster2->GetErrX2()) + dY * dY / (cluster1->GetErrY2() + cluster2->GetErrY2());
+      if (chi2 > 2. * chi2Max) continue; // 2 because 2 quantities in chi2
       
       compatibleCluster[cluster1->GetChamberId()] = kTRUE;
+      nMatchClusters++;
       break;
     }
     
     trackParamAtCluster1 = (AliMUONTrackParam*) this->fTrackParamAtCluster->After(trackParamAtCluster1);
   }
   
-  return compatibleCluster;
+  return nMatchClusters;
+}
+
+//__________________________________________________________________________
+Bool_t AliMUONTrack::Match(AliMUONTrack &track, Double_t sigmaCut, Int_t &nMatchClusters) const
+{
+  /// Try to match this track with the given track. Matching conditions:
+  /// - more than 50% of clusters from this track matched with clusters from the given track
+  /// - at least 1 cluster matched before and 1 cluster matched after the dipole
+  
+  Bool_t compTrack[10];
+  nMatchClusters = FindCompatibleClusters(track, sigmaCut, compTrack);
+  
+  if ((compTrack[0] || compTrack[1] || compTrack[2] || compTrack[3]) && // at least 1 cluster matched in st 1 & 2
+      (compTrack[6] || compTrack[7] || compTrack[8] || compTrack[9]) && // at least 1 cluster matched in st 4 & 5
+      2 * nMatchClusters > GetNClusters()) return kTRUE;                // more than 50% of clusters matched
+  else return kFALSE;
+  
 }
 
 //__________________________________________________________________________
@@ -1041,14 +1181,15 @@ void AliMUONTrack::Print(Option_t*) const
 
   cout << "<AliMUONTrack> No.Clusters=" << setw(2)   << GetNClusters() << 
       ", Match2Trig=" << setw(1) << GetMatchTrigger()  << 
-      ", LoTrgNum=" << setw(3) << GetLoTrgNum()  << 
+      ", LoTrgNum=" << setw(3) << LoCircuit()  << 
     ", Chi2-tracking-trigger=" << setw(8) << setprecision(5) <<  GetChi2MatchTrigger();
-  cout << Form(" HitTriggerPattern %x",fHitsPatternInTrigCh) << endl;
+  cout << Form(" HitTriggerPattern %x",fHitsPatternInTrigCh);
+  cout << Form(" MClabel=%d",fTrackID) << endl;
   if (fTrackParamAtCluster) fTrackParamAtCluster->First()->Print("FULL");
 }
 
 //__________________________________________________________________________
-void AliMUONTrack::SetLocalTrigger(Int_t loCirc, Int_t loStripX, Int_t loStripY, Int_t loDev, Int_t loLpt, Int_t loHpt)
+void AliMUONTrack::SetLocalTrigger(Int_t loCirc, Int_t loStripX, Int_t loStripY, Int_t loDev, Int_t loLpt, Int_t loHpt, UChar_t respWithoutChamber)
 {
   /// pack the local trigger information and store
 
@@ -1061,6 +1202,51 @@ void AliMUONTrack::SetLocalTrigger(Int_t loCirc, Int_t loStripX, Int_t loStripY,
   fLocalTrigger += loDev    << 17;
   fLocalTrigger += loLpt    << 22;
   fLocalTrigger += loHpt    << 24;
+  fLocalTrigger += respWithoutChamber << 26;
 
 }
 
+//__________________________________________________________________________
+void AliMUONTrack::FindMCLabel()
+{
+  /// Determine the MC label from the label of the attached clusters and fill fMCLabel data member:
+  /// More than 50% of clusters, including 1 before and 1 after the dipole, must share the same label
+  
+  Int_t nClusters = GetNClusters();
+  Int_t halfCluster = nClusters/2;
+  
+  // reset MC label
+  fTrackID = -1;
+  
+  // loop over first clusters (if nClusters left < (nClusters-halfCluster) the conditions cannot be fulfilled)
+  for (Int_t iCluster1 = 0; iCluster1 < nClusters-halfCluster; iCluster1++) {
+    AliMUONVCluster* cluster1 = ((AliMUONTrackParam*) fTrackParamAtCluster->UncheckedAt(iCluster1))->GetClusterPtr();
+    
+    // if the first cluster is not on station 1 or 2 the conditions cannot be fulfilled
+    if (cluster1->GetChamberId() > 3) return;
+    
+    Int_t label1 = cluster1->GetMCLabel();
+    if (label1 < 0) continue;
+    
+    Int_t nIdenticalLabel = 1;
+    
+    // Loop over next clusters
+    for (Int_t iCluster2 = iCluster1+1; iCluster2 < nClusters; iCluster2++) {
+      AliMUONVCluster* cluster2 = ((AliMUONTrackParam*) fTrackParamAtCluster->UncheckedAt(iCluster2))->GetClusterPtr();
+      
+      if (cluster2->GetMCLabel() != label1) continue;
+      
+      nIdenticalLabel++;
+      
+      // stop as soon as conditions are fulfilled
+      if (nIdenticalLabel > halfCluster && cluster2->GetChamberId() > 5) {
+        fTrackID = label1;
+       return;
+      }
+      
+    }
+    
+  }
+  
+}
+