]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MFT/AliMuonForwardTrackFinder.cxx
Add option to run on some local files with test mode using the alien plugin
[u/mrichter/AliRoot.git] / MFT / AliMuonForwardTrackFinder.cxx
index 822b5bc18b05dd44cb047852e87fe55698111f74..f9bc1e726259901502521954b6eb01ad17ee64e1 100644 (file)
@@ -71,10 +71,13 @@ AliMuonForwardTrackFinder::AliMuonForwardTrackFinder():
   fRun(0),
   fNEventsToAnalyze(0),
   fSigmaClusterCut(0),
+  fScaleSigmaClusterCut(1.),
+  fGlobalTrackingDiverged(kFALSE),
   fChi2GlobalCut(0),
   fSigmaSpectrometerCut(0),
-  fExtrapOriginTransvError(0),
-  fGaussianBlurZVert(0),
+  fVertexErrorX(0.015),
+  fVertexErrorY(0.015),
+  fVertexErrorZ(0.010),
   fNFinalCandidatesCut(0),
   fReadDir(0),
   fOutDir(0),
@@ -92,9 +95,6 @@ AliMuonForwardTrackFinder::AliMuonForwardTrackFinder():
   fEv(0),
   fLabelMC(0),
 
-  fHistPtSpectrometer(0), 
-  fHistPtMuonTrackWithGoodMatch(0),
-  fHistPtMuonTrackWithBadMatch(0),
   fHistRadiusEndOfAbsorber(0), 
   fHistNGoodClustersForFinalTracks(0),
   fHistDistanceGoodClusterFromTrackMinusDistanceBestClusterFromTrackAtLastPlane(0),
@@ -153,9 +153,9 @@ AliMuonForwardTrackFinder::AliMuonForwardTrackFinder():
   fOutputEventTree(0),
   fMuonForwardTracks(0),
   fMatchingMode(-1),
-  fMinResearchRadiusAtLastPlane(0),
   fGRPData(0),
-  fRunInfo(0)
+  fRunInfo(0),
+  fBransonCorrection(kTRUE)
 
 {
 
@@ -194,6 +194,8 @@ AliMuonForwardTrackFinder::AliMuonForwardTrackFinder():
 
     fIsPlaneMandatory[iPlane] = kFALSE;
     
+    fMinResearchRadiusAtPlane[iPlane] = 0.;
+
   }
 
   //  fNextTrack = 0;
@@ -215,34 +217,91 @@ AliMuonForwardTrackFinder::AliMuonForwardTrackFinder():
 
 AliMuonForwardTrackFinder::~AliMuonForwardTrackFinder() {
 
-  delete *fMFTClusterArray;
-  delete *fMFTClusterArrayFront;
-  delete *fMFTClusterArrayBack;
+  for (Int_t iPlane=0; iPlane<AliMFTConstants::fNMaxPlanes; iPlane++) {
+
+    delete fHistNTracksAfterExtrapolation[iPlane];
+    delete fHistChi2Cluster_GoodCluster[iPlane];
+    delete fHistChi2Cluster_BadCluster[iPlane];
+    delete fHistResearchRadius[iPlane];
+    
+    delete fHistChi2Cluster_GoodCluster[iPlane];
+    delete fHistChi2Cluster_BadCluster[iPlane];
+    
+    delete fHistGlobalChi2AtPlaneFor_GOOD_CandidatesOfTrackableMuons[iPlane];
+    delete fHistGlobalChi2AtPlaneFor_BAD_CandidatesOfTrackableMuons[iPlane];
+    
+    for (Int_t i=0; i<4; i++) delete fGrMFTPlane[i][iPlane];
+    delete fCircleExt[iPlane];
+    delete fCircleInt[iPlane];
+    
+    delete fTxtTrackChi2[iPlane];
+    
+    delete fMFTClusterArray[iPlane];
+    delete fMFTClusterArrayFront[iPlane];
+    delete fMFTClusterArrayBack[iPlane];
+
+  }
+
+  delete fNtuFinalCandidates;
+  delete fNtuFinalBestCandidates;
 
-  delete fIsPlaneMandatory;
+  delete fHistRadiusEndOfAbsorber;
 
-  delete fZPlane;
-  delete fRPlaneMax;
-  delete fRPlaneMin;
+  delete fHistNGoodClustersForFinalTracks; 
+  delete fHistDistanceGoodClusterFromTrackMinusDistanceBestClusterFromTrackAtLastPlane;                //
+  delete fHistDistanceGoodClusterFromTrackAtLastPlane;                                         //
 
-  delete *fHistNTracksAfterExtrapolation;
-  delete *fHistResearchRadius;
-  delete *fHistChi2Cluster_GoodCluster;
-  delete *fHistChi2Cluster_BadCluster;
+  delete fCanvas;
 
-  delete *fHistGlobalChi2AtPlaneFor_GOOD_CandidatesOfTrackableMuons;
-  delete *fHistGlobalChi2AtPlaneFor_BAD_CandidatesOfTrackableMuons;
+  delete fTxtMuonHistory;
+  delete fTxtTrackGoodClusters;
+  delete fTxtTrackFinalChi2;
+  delete fTxtTrackMomentum;
+  delete fTxtFinalCandidates;
+  delete fTxtDummy;
+  delete fTxtAllClust;
+  delete fTxtClustGoodChi2;
+  delete fTxtClustMC;
+  delete fTxtClustOfTrack;
+  delete fMrkAllClust;
+  delete fMrkClustGoodChi2;
+  delete fMrkClustMC;
+  delete fMrkClustOfTrack;
+  delete fFileCluster;
+  delete fFileESD;
+  delete fFile_gAlice;
 
-  for (Int_t i=0; i<4; i++) delete  *fGrMFTPlane[i];
-  delete *fCircleExt;
-  delete *fCircleInt;
+  delete fRunLoader;
+  delete fMFTLoader;
+  delete fMuonRecoCheck;
 
-  delete *fTxtTrackChi2;
+  delete fMFTClusterTree;
 
-  delete fNClustersGlobalTrack;
-  delete fNDFGlobalTrack;
+  delete fMuonTrackReco;
+  delete fCurrentTrack;
+  delete fFinalBestCandidate;
 
-  delete fIsGoodClusterInPlane;
+  delete fCandidateTracks;
+
+  delete fTrackStore;
+  delete fTrackRefStore;
+  
+  delete fNextTrack;
+  
+  delete fStack;
+
+  delete fMFT;
+  delete fSegmentation;
+
+  delete fOutputTreeFile; 
+  delete fOutputQAFile;
+  delete fOutputEventTree;
+
+  delete fMuonForwardTracks;
+
+  delete fGRPData;
+  delete fRunInfo;       
 
 }
 
@@ -273,10 +332,10 @@ void AliMuonForwardTrackFinder::Init(Int_t nRun,
   Char_t gAliceName[300];
   Char_t clusterName[300];
   
-  sprintf(geoFileName , "%s/geometry.root",      fReadDir.Data());
-  sprintf(esdFileName , "%s/AliESDs.root" ,      fReadDir.Data());
-  sprintf(gAliceName  , "%s/galice.root"  ,      fReadDir.Data());
-  sprintf(clusterName , "%s/MFT.RecPoints.root", fReadDir.Data());
+  snprintf(geoFileName , 300, "%s/geometry.root",      fReadDir.Data());
+  snprintf(esdFileName , 300, "%s/AliESDs.root" ,      fReadDir.Data());
+  snprintf(gAliceName  , 300, "%s/galice.root"  ,      fReadDir.Data());
+  snprintf(clusterName , 300, "%s/MFT.RecPoints.root", fReadDir.Data());
   
   // Import TGeo geometry (needed by AliMUONTrackExtrap::ExtrapToVertex)
   if (!gGeoManager) {
@@ -348,7 +407,6 @@ void AliMuonForwardTrackFinder::Init(Int_t nRun,
   SetNFinalCandidatesCut(10);
   SetRAbsorberCut(26.4);
   SetLowPtCut(0.5);
-  SetExtrapOriginTransvError(1.0);
 
 }
 
@@ -366,10 +424,20 @@ Bool_t AliMuonForwardTrackFinder::LoadNextEvent() {
   
   AliInfo(Form(" **** analyzing event # %d  \n", fEv));
   
-  fTrackStore    = fMuonRecoCheck->ReconstructedTracks(fEv);
+  fTrackStore = fMuonRecoCheck->ReconstructedTracks(fEv);
+  if (fTrackStore->IsEmpty()) {
+    AliInfo("fTrackStore Is Empty: exiting NOW!");
+    return kFALSE;
+  }
+  AliInfo("fTrackStore contains tracks!");
+
+  AliDebug(2, Form("Getting fMuonRecoCheck->ReconstructibleTracks(%d)", fEv));
   fTrackRefStore = fMuonRecoCheck->ReconstructibleTracks(fEv);
   
+  AliDebug(2, Form("Getting fRunLoader->GetEvent(%d)", fEv));
   fRunLoader->GetEvent(fEv);
+
+  AliDebug(2, Form("fMFTLoader->TreeR() = %p",fMFTLoader->TreeR()));
   if (!fMFTLoader->TreeR()->GetEvent()) return kFALSE;
   for (Int_t iPlane=0; iPlane<fNPlanesMFT; iPlane++) {
     AliDebug(1, Form("plane %02d: nClusters = %d\n", iPlane, (fMFT->GetRecPointsList(iPlane))->GetEntries()));
@@ -380,7 +448,7 @@ Bool_t AliMuonForwardTrackFinder::LoadNextEvent() {
   fRunLoader -> LoadKinematics();
   fStack = fRunLoader->Stack();
   fNextTrack = fTrackStore->CreateIterator();
-  fMuonForwardTracks->Clear();
+  fMuonForwardTracks->Delete();
 
   fEv++;
   
@@ -396,23 +464,20 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
 
   // load next muon track from the reconstructed event
 
-  if (fCountRealTracksAnalyzed>fMaxNTracksToBeAnalyzed) return kFALSE;
-
+  if (fCountRealTracksAnalyzed>=fMaxNTracksToBeAnalyzed) return kFALSE;
   if (!fCountRealTracksAnalyzed) if (!LoadNextEvent()) return kFALSE;
-  if (fCountRealTracksAnalyzed==fMaxNTracksToBeAnalyzed) {
+
+  if (!fGlobalTrackingDiverged) {
+    while ( !(fMuonTrackReco = static_cast<AliMUONTrack*>(fNextTrack->Next())) ) if (!LoadNextEvent()) return kFALSE;
     fCountRealTracksAnalyzed++;
-    if (!LoadNextEvent()) return kFALSE;
+    fCountRealTracksAnalyzedOfEvent++;
   }
 
-  while ( !(fMuonTrackReco = static_cast<AliMUONTrack*>(fNextTrack->Next())) ) if (!LoadNextEvent()) return kFALSE;
-
   AliDebug(1, "**************************************************************************************\n");
   AliDebug(1, Form("***************************   MUON TRACK %3d   ***************************************\n", fCountRealTracksAnalyzedOfEvent));
   AliDebug(1, "**************************************************************************************\n");
 
-  fCountRealTracksAnalyzed++;
-
-  fCandidateTracks -> Clear();
+  fCandidateTracks -> Delete();
 
   fLabelMC = -1;
   fDistanceFromGoodClusterAndTrackAtLastPlane = -1.;
@@ -437,32 +502,52 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
   
   // ------------------------------------- ...done!
 
-  if (fMuonTrackReco->GetMCLabel()>=0) fCountRealTracksWithRefMC++;
-  
   fLabelMC = fMuonTrackReco->GetMCLabel();
 
+  Int_t motherPdg=0;
+  if (fLabelMC>=0) {
+    if (!fGlobalTrackingDiverged) fCountRealTracksWithRefMC++;
+    if (fStack->Particle(fLabelMC)->GetFirstMother() != -1) {
+      motherPdg = fStack->Particle(fStack->Particle(fLabelMC)->GetFirstMother())->GetPdgCode();
+    }
+  }
+
   CheckCurrentMuonTrackable();
 
-  if (fMuonTrackReco->GetMatchTrigger()) fCountRealTracksWithRefMC_andTrigger++;
+  if (!fGlobalTrackingDiverged) if (fMuonTrackReco->GetMatchTrigger()) fCountRealTracksWithRefMC_andTrigger++;
   
   // the track we are going to build, starting from fMuonTrackReco and adding the MFT clusters
   AliMuonForwardTrack *track = new ((*fCandidateTracks)[0]) AliMuonForwardTrack();
-  track -> SetMUONTrack(fMuonTrackReco);
-  if (fLabelMC>=0 && fStack->Particle(fLabelMC)) track -> SetMCTrackRef(fStack->Particle(fLabelMC));
+  track -> SetMUONTrack(new AliMUONTrack(*fMuonTrackReco));
+  if (fLabelMC>=0 && fStack->Particle(fLabelMC)) track->SetMCTrackRef(new TParticle(*(fStack->Particle(fLabelMC))));
   track -> SetMCLabel(fMuonTrackReco->GetMCLabel());
   track -> SetMatchTrigger(fMuonTrackReco->GetMatchTrigger());
-  
-  // track parameters at the first tracking station in the Muon Spectrometer
-  AliMUONTrackParam *param = (AliMUONTrackParam*) (fMuonTrackReco->GetTrackParamAtCluster()->First());
-  Double_t ptSpectrometer = TMath::Sqrt(param->Px()*param->Px() + param->Py()*param->Py());
-  Double_t thetaSpectrometer = TMath::ATan(ptSpectrometer/param->Pz());
-  if (thetaSpectrometer<0.) thetaSpectrometer += TMath::Pi();
-  Double_t etaSpectrometer = -1.*TMath::Log(TMath::Tan(0.5*thetaSpectrometer));
-  //  fOutputQAFile->cd();
-  fHistPtSpectrometer -> Fill(ptSpectrometer);
-  
-  // if the transverse momentum in the Muon Spectrometer is smaller than the threshold, skip to the next track
-  if (ptSpectrometer < fLowPtCut) return 3;
+
+  // track origin
+  Double_t xVtx=-999., yVtx=-999., zVtx=-999999.;
+  if (track->GetMCTrackRef()) {
+    xVtx = track->GetMCTrackRef()->Vx();
+    yVtx = track->GetMCTrackRef()->Vy();
+    zVtx = track->GetMCTrackRef()->Vz();
+  }  
+  
+  // track kinematics
+  Double_t pt=-999., theta=-999., eta=-999.;
+  if (track->GetMCTrackRef()) {
+    pt    = track->GetMCTrackRef()->Pt();
+    theta = track->GetMCTrackRef()->Theta();
+    if (theta<0.) theta += TMath::Pi();
+    eta   = track->GetMCTrackRef()->Eta();
+  }
+  else {
+    AliMUONTrackParam *param = (AliMUONTrackParam*) (fMuonTrackReco->GetTrackParamAtCluster()->First());
+    pt    = TMath::Sqrt(param->Px()*param->Px() + param->Py()*param->Py());
+    theta = TMath::ATan(pt/param->Pz());
+    if (theta<0.) theta += TMath::Pi();
+    eta   = -1.*TMath::Log(TMath::Tan(0.5*theta));
+  }  
+  // if the transverse momentum is smaller than the threshold, skip to the next track
+  if (pt < fLowPtCut) return 3;
   
   // track parameters linearly extrapolated from the first tracking station to the end of the absorber
   AliMUONTrackParam trackParamEndOfAbsorber(*((AliMUONTrackParam*)(fMuonTrackReco->GetTrackParamAtCluster()->First())));
@@ -470,11 +555,9 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
   Double_t xEndOfAbsorber = trackParamEndOfAbsorber.GetNonBendingCoor();
   Double_t yEndOfAbsorber = trackParamEndOfAbsorber.GetBendingCoor();
   Double_t rAbsorber      = TMath::Sqrt(xEndOfAbsorber*xEndOfAbsorber + yEndOfAbsorber*yEndOfAbsorber);
-  //  fOutputQAFile->cd();
   fHistRadiusEndOfAbsorber -> Fill(rAbsorber);
   
-  // if the radial distance of the track at the end of the absorber is smaller than a radius corresponding to 
-  // 3 degrees as seen from the interaction point, skip to the next track
+  // if the radial distance of the track at the end of the absorber is smaller than a given radius, skip to the next track
   if (rAbsorber < fRAbsorberCut) return 4;
   
   //------------------------- NOW THE CYCLE OVER THE MFT PLANES STARTS ---------------------------------------
@@ -493,7 +576,11 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
        fCurrentTrack = (AliMuonForwardTrack*) fCandidateTracks->UncheckedAt(iTrack);
        // if the old track is compatible with the new cluster, the track is updated and inserted as new track in the array 
        // (several new tracks can be created for one old track)
-       FindClusterInPlane(iPlane);   
+       if (FindClusterInPlane(iPlane) == 2) {
+         fGlobalTrackingDiverged = kTRUE;
+         if (fScaleSigmaClusterCut>0) fScaleSigmaClusterCut -= 0.1;
+         return 6;
+       }
        if ((fNPlanesMFTAnalyzed-fCurrentTrack->GetNMFTClusters())>fNMaxMissingMFTClusters || fIsPlaneMandatory[iPlane]) {
          fCandidateTracks->Remove(fCurrentTrack);     // the old track is removed after the check;
        }
@@ -516,9 +603,12 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
   
   // -------------------------- END OF THE CYCLE OVER THE MFT PLANES --------------------------------------------
   
+  fGlobalTrackingDiverged = kFALSE;
+  fScaleSigmaClusterCut = 1.0;
+
   AliDebug(1, "Finished cycle over planes");
 
-  Double_t momentum = ptSpectrometer * TMath::CosH(etaSpectrometer);
+  Double_t momentum = pt * TMath::CosH(eta);
   fTxtTrackMomentum = new TLatex(0.10, 0.70, Form("P_{spectro} = %3.1f GeV/c", momentum));
 
   if (fMatchingMode==kIdealMatching) {
@@ -527,7 +617,7 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
     AliMuonForwardTrack *newTrack = (AliMuonForwardTrack*) fCandidateTracks->UncheckedAt(0);
     new ((*fMuonForwardTracks)[fMuonForwardTracks->GetEntries()]) AliMuonForwardTrack(*newTrack);
     AliDebug(1, "...track added!\n");
-    fCandidateTracks->Clear();
+    fCandidateTracks->Delete();
     fCountRealTracksAnalyzedOfEvent++;
     fCountRealTracksAnalyzedWithFinalCandidates++;
     PrintParticleHistory();
@@ -614,11 +704,13 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
                                     Double_t(fEv),
                                     Double_t(fCountRealTracksAnalyzedOfEvent),
                                     Double_t(nFinalTracks),
+                                    Double_t(fLabelMC>=0),
+                                    xVtx, yVtx, zVtx,
+                                    motherPdg,
+                                    Double_t(fMuonTrackReco->GetMatchTrigger()),
                                     Double_t(nClustersMC),
                                     Double_t(nGoodClusters),
-                                    ptSpectrometer,
-                                    thetaSpectrometer,
-                                    etaSpectrometer, 
+                                    pt, theta, eta, 
                                     chi2AtPlane[0],
                                     chi2AtPlane[1],
                                     chi2AtPlane[2],
@@ -664,6 +756,7 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
     FillPlanesWithTrackHistory();
     AliMuonForwardTrack *newTrack = (AliMuonForwardTrack*) fCandidateTracks->UncheckedAt(idBestCandidate);
     newTrack -> SetNWrongClustersMC(newTrack->GetNMFTClusters() - nGoodClustersBestCandidate);
+    newTrack -> SetTrackMCId(fRun*100000+fEv*1000+fCountRealTracksAnalyzedOfEvent);
     new ((*fMuonForwardTracks)[fMuonForwardTracks->GetEntries()]) AliMuonForwardTrack(*newTrack);
   }
 
@@ -673,11 +766,13 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
                                       Double_t(fEv),
                                       Double_t(fCountRealTracksAnalyzedOfEvent),
                                       Double_t(nFinalTracks),
+                                      Double_t(fLabelMC>=0),
+                                      xVtx, yVtx, zVtx,
+                                      motherPdg,
+                                      Double_t(fMuonTrackReco->GetMatchTrigger()),
                                       Double_t(nClustersMC),
                                       Double_t(nGoodClustersBestCandidate),
-                                      ptSpectrometer,
-                                      thetaSpectrometer,
-                                      etaSpectrometer,
+                                      pt, theta, eta,
                                       chi2HistoryForBestCandidate[0],
                                       chi2HistoryForBestCandidate[1],
                                       chi2HistoryForBestCandidate[2],
@@ -704,26 +799,18 @@ Int_t AliMuonForwardTrackFinder::LoadNextTrack() {
     DrawPlanes();
   }
 
-  if (fIsCurrentMuonTrackable) {
-    //    fOutputQAFile->cd();
-    if (nGoodClustersBestCandidate==5) fHistPtMuonTrackWithGoodMatch -> Fill(ptSpectrometer);
-    else                               fHistPtMuonTrackWithBadMatch  -> Fill(ptSpectrometer);
-  }
-
   // -------------------------------------------------------------------------------------------
 
-  fCandidateTracks->Clear();
+  fCandidateTracks->Delete();
   fFinalBestCandidate = NULL;
   
-  fCountRealTracksAnalyzedOfEvent++;
-
   return 5;
   
 }
 
 //===========================================================================================================================================
 
-void AliMuonForwardTrackFinder::FindClusterInPlane(Int_t planeId) { 
+Int_t AliMuonForwardTrackFinder::FindClusterInPlane(Int_t planeId) { 
   
   AliDebug(2, Form(">>>> executing AliMuonForwardTrackFinder::FindClusterInPlane(%d)\n", planeId));
 
@@ -740,10 +827,19 @@ void AliMuonForwardTrackFinder::FindClusterInPlane(Int_t planeId) {
     currentParamBack  = (*((AliMUONTrackParam*)(fMuonTrackReco->GetTrackParamAtCluster()->First())));
     currentParamForResearchFront = currentParamFront;
     currentParamForResearchBack  = currentParamBack;
-    AliMUONTrackExtrap::ExtrapToVertexWithoutBranson(&currentParamFront, 0.); 
-    AliMUONTrackExtrap::ExtrapToVertexWithoutBranson(&currentParamBack,  0.); 
-    AliMUONTrackExtrap::ExtrapToVertex(&currentParamForResearchFront, 0., 0., gRandom->Gaus(0,fGaussianBlurZVert), fExtrapOriginTransvError, fExtrapOriginTransvError); 
-    AliMUONTrackExtrap::ExtrapToVertex(&currentParamForResearchBack,  0., 0., gRandom->Gaus(0,fGaussianBlurZVert), fExtrapOriginTransvError, fExtrapOriginTransvError); 
+    Double_t xExtrap = gRandom->Gaus(0,fVertexErrorX);
+    Double_t yExtrap = gRandom->Gaus(0,fVertexErrorY);
+    Double_t zExtrap = gRandom->Gaus(0,fVertexErrorZ);
+    if (fBransonCorrection) {
+      AliMUONTrackExtrap::ExtrapToVertex(&currentParamFront, xExtrap, yExtrap, zExtrap, fVertexErrorX, fVertexErrorY); 
+      AliMUONTrackExtrap::ExtrapToVertex(&currentParamBack,  xExtrap, yExtrap, zExtrap, fVertexErrorX, fVertexErrorY); 
+    }
+    else {
+      AliMUONTrackExtrap::ExtrapToVertexWithoutBranson(&currentParamFront, zExtrap);
+      AliMUONTrackExtrap::ExtrapToVertexWithoutBranson(&currentParamBack,  zExtrap);
+    }
+    AliMUONTrackExtrap::ExtrapToVertex(&currentParamForResearchFront, xExtrap, yExtrap, zExtrap, fVertexErrorX, fVertexErrorY); 
+    AliMUONTrackExtrap::ExtrapToVertex(&currentParamForResearchBack,  xExtrap, yExtrap, zExtrap, fVertexErrorX, fVertexErrorY); 
   }
   else {          // MFT planes others than the last one: mult. scattering correction because of the upstream MFT planes is performed
     currentParamFront = (*((AliMUONTrackParam*)(fCurrentTrack->GetTrackParamAtCluster()->First())));
@@ -779,8 +875,8 @@ void AliMuonForwardTrackFinder::FindClusterInPlane(Int_t planeId) {
 
   Double_t researchRadiusFront = TMath::Sqrt(squaredError_X_Front + squaredError_Y_Front);
   Double_t researchRadiusBack  = TMath::Sqrt(squaredError_X_Back  + squaredError_Y_Back);
-  if (planeId==fNPlanesMFT-1 && 0.5*(researchRadiusFront+researchRadiusBack)<fMinResearchRadiusAtLastPlane) {
-    corrFact = fMinResearchRadiusAtLastPlane/(0.5*(researchRadiusFront+researchRadiusBack));
+  if (0.5*(researchRadiusFront+researchRadiusBack)<fMinResearchRadiusAtPlane[planeId]) {
+    corrFact = fMinResearchRadiusAtPlane[planeId]/(0.5*(researchRadiusFront+researchRadiusBack));
   }
   if (fIsCurrentMuonTrackable) {
     //    fOutputQAFile->cd();
@@ -796,7 +892,7 @@ void AliMuonForwardTrackFinder::FindClusterInPlane(Int_t planeId) {
 
   //---------------------------------------------------------------------------------------
 
-  Double_t chi2cut = 2.*fSigmaClusterCut*fSigmaClusterCut;     // depends on the number of variables (here, 2)
+  Double_t chi2cut = 2.*fScaleSigmaClusterCut*fScaleSigmaClusterCut*fSigmaClusterCut*fSigmaClusterCut;     // depends on the number of variables (here, 2)
   
   // Analyizing the clusters: FRONT ACTIVE ELEMENTS
   
@@ -831,8 +927,9 @@ void AliMuonForwardTrackFinder::FindClusterInPlane(Int_t planeId) {
     if (isGoodChi2) {
       AliDebug(3, Form("accepting cluster: chi2=%f (cut = %f)\n", chi2, chi2cut));
       AliMuonForwardTrack *newTrack = new ((*fCandidateTracks)[fCandidateTracks->GetEntriesFast()]) AliMuonForwardTrack(*fCurrentTrack);
+      if (fCandidateTracks->GetEntriesFast() > fMaxNCandidates) return 2;
       newTrack->AddTrackParamAtMFTCluster(currentParamFront, *cluster);    // creating new track param and attaching the cluster
-      AliDebug(1, Form("After plane %02d: newTrack->GetNMFTClusters() = %d (fCurrentTrack->GetNMFTClusters() = %d)", 
+      AliDebug(2, Form("After plane %02d: newTrack->GetNMFTClusters() = %d (fCurrentTrack->GetNMFTClusters() = %d)", 
                       planeId, newTrack->GetNMFTClusters(), fCurrentTrack->GetNMFTClusters()));
       newTrack->SetPlaneExists(planeId);
       AliDebug(2, Form("current muon is trackable: %d\n", fIsCurrentMuonTrackable));
@@ -884,8 +981,9 @@ void AliMuonForwardTrackFinder::FindClusterInPlane(Int_t planeId) {
     if (isGoodChi2) {
       AliDebug(3,Form("accepting cluster: chi2=%f (cut = %f)\n", chi2, chi2cut));
       AliMuonForwardTrack *newTrack = new ((*fCandidateTracks)[fCandidateTracks->GetEntriesFast()]) AliMuonForwardTrack(*fCurrentTrack);
+      if (fCandidateTracks->GetEntriesFast() > fMaxNCandidates) return 2;
       newTrack->AddTrackParamAtMFTCluster(currentParamBack, *cluster);    // creating new track param and attaching the cluster
-      AliDebug(1, Form("After plane %02d: newTrack->GetNMFTClusters() = %d (fCurrentTrack->GetNMFTClusters() = %d)", 
+      AliDebug(2, Form("After plane %02d: newTrack->GetNMFTClusters() = %d (fCurrentTrack->GetNMFTClusters() = %d)", 
                       planeId, newTrack->GetNMFTClusters(), fCurrentTrack->GetNMFTClusters()));
       newTrack->SetPlaneExists(planeId);
       AliDebug(2, Form("current muon is trackable: %d\n", fIsCurrentMuonTrackable));
@@ -914,6 +1012,8 @@ void AliMuonForwardTrackFinder::FindClusterInPlane(Int_t planeId) {
       fHistDistanceGoodClusterFromTrackAtLastPlane -> Fill(fDistanceFromGoodClusterAndTrackAtLastPlane);
     }
   }
+
+  return 0;
   
 }
 
@@ -1083,8 +1183,8 @@ Double_t AliMuonForwardTrackFinder::TryOneCluster(const AliMUONTrackParam &track
 void AliMuonForwardTrackFinder::SeparateFrontBackClusters() {
 
   for (Int_t iPlane=0; iPlane<fNPlanesMFT; iPlane++) {
-    fMFTClusterArrayFront[iPlane]->Clear();
-    fMFTClusterArrayBack[iPlane] ->Clear();
+    fMFTClusterArrayFront[iPlane]->Delete();
+    fMFTClusterArrayBack[iPlane] ->Delete();
     for (Int_t iCluster=0; iCluster<fMFTClusterArray[iPlane]->GetEntries(); iCluster++) {
       AliMFTCluster *cluster = (AliMFTCluster*) fMFTClusterArray[iPlane]->At(iCluster);
       if (TMath::Abs(cluster->GetZ())<TMath::Abs(fSegmentation->GetPlane(iPlane)->GetZCenter())) {
@@ -1116,11 +1216,6 @@ void AliMuonForwardTrackFinder::BookHistos() {
   const Int_t nMaxNewTracks[]  = {150,     200,   250, 600, 1000};
   const Double_t radiusPlane[] = {0.010, 0.010, 0.050, 0.5,  1.5};
 
-  fHistPtSpectrometer = new TH1D("hPtSpectrometer", "p_{T} as given by the Muon Spectrometer", 200, 0, 20.); 
-
-  fHistPtMuonTrackWithGoodMatch = new TH1D("fHistPtMuonTrackWithGoodMatch", "p_{T} of muon track with good match", 200, 0, 20.); 
-  fHistPtMuonTrackWithBadMatch  = new TH1D("fHistPtMuonTrackWithBadMatch",  "p_{T} of muon track with bad match",  200, 0, 20.); 
-
   fHistRadiusEndOfAbsorber = new TH1D("hRadiusEndOfAbsorber", "Track radial distance at the end of the absorber",  1000, 0, 100.); 
 
   fHistNGoodClustersForFinalTracks = new TH1D("hNGoodClustersForFinalTracks", "Number of Good Clusters per Final Track", 20, -0.25, 9.75);
@@ -1162,9 +1257,6 @@ void AliMuonForwardTrackFinder::BookHistos() {
   
   //------------------------------------------
   
-  fHistPtSpectrometer               -> Sumw2();
-  fHistPtMuonTrackWithGoodMatch     -> Sumw2();
-  fHistPtMuonTrackWithBadMatch      -> Sumw2();
   fHistRadiusEndOfAbsorber          -> Sumw2();
   fHistNGoodClustersForFinalTracks  -> Sumw2();
 
@@ -1184,9 +1276,9 @@ void AliMuonForwardTrackFinder::BookHistos() {
     
   }
 
-  fNtuFinalCandidates     = new TNtuple("ntuFinalCandidates",     "Final Candidates (ALL)", "run:event:muonTrack:nFinalCandidates:nClustersMC:nGoodClusters:ptSpectrometer:thetaSpectrometer:etaSpectrometer:chi2AtPlane0:chi2AtPlane1:chi2AtPlane2:chi2AtPlane3:chi2AtPlane4:chi2AtPlane5:chi2AtPlane6:chi2AtPlane7:chi2AtPlane8");
+  fNtuFinalCandidates     = new TNtuple("ntuFinalCandidates",     "Final Candidates (ALL)", "run:event:muonTrack:nFinalCandidates:MCTrackRefExists:xVtx:yVtx:zVtx:motherPdg:triggerMatch:nClustersMC:nGoodClusters:pt:theta:eta:chi2AtPlane0:chi2AtPlane1:chi2AtPlane2:chi2AtPlane3:chi2AtPlane4:chi2AtPlane5:chi2AtPlane6:chi2AtPlane7:chi2AtPlane8");
 
-  fNtuFinalBestCandidates = new TNtuple("ntuFinalBestCandidates", "Final Best Candidates",  "run:event:muonTrack:nFinalCandidates:nClustersMC:nGoodClusters:ptSpectrometer:thetaSpectrometer:etaSpectrometer:chi2AtPlane0:chi2AtPlane1:chi2AtPlane2:chi2AtPlane3:chi2AtPlane4:chi2AtPlane5:chi2AtPlane6:chi2AtPlane7:chi2AtPlane8:nClustersAtPlane0:nClustersAtPlane1:nClustersAtPlane2:nClustersAtPlane3:nClustersAtPlane4:nClustersAtPlane5:nClustersAtPlane6:nClustersAtPlane7:nClustersAtPlane8");
+  fNtuFinalBestCandidates = new TNtuple("ntuFinalBestCandidates", "Final Best Candidates",  "run:event:muonTrack:nFinalCandidates:MCTrackRefExists:xVtx:yVtx:zVtx:motherPdg:triggerMatch:nClustersMC:nGoodClusters:pt:theta:eta:chi2AtPlane0:chi2AtPlane1:chi2AtPlane2:chi2AtPlane3:chi2AtPlane4:chi2AtPlane5:chi2AtPlane6:chi2AtPlane7:chi2AtPlane8:nClustersAtPlane0:nClustersAtPlane1:nClustersAtPlane2:nClustersAtPlane3:nClustersAtPlane4:nClustersAtPlane5:nClustersAtPlane6:nClustersAtPlane7:nClustersAtPlane8");
 
 }
 
@@ -1194,9 +1286,6 @@ void AliMuonForwardTrackFinder::BookHistos() {
 
 void AliMuonForwardTrackFinder::SetTitleHistos() {
 
-  fHistPtSpectrometer              -> SetXTitle("p_{T}  [GeV/c]");
-  fHistPtMuonTrackWithGoodMatch    -> SetXTitle("p_{T}  [GeV/c]");
-  fHistPtMuonTrackWithBadMatch     -> SetXTitle("p_{T}  [GeV/c]");
   fHistRadiusEndOfAbsorber         -> SetXTitle("R_{abs}  [cm]");
   fHistNGoodClustersForFinalTracks -> SetXTitle("N_{GoodClusters}");
 
@@ -1323,12 +1412,13 @@ void AliMuonForwardTrackFinder::PrintParticleHistory() {
   TParticle *part = 0;
   if (fLabelMC>=0) part = fStack->Particle(fLabelMC);
 
-  AliDebug(1, Form("fStack->Particle(fLabelMC) = %p", part));
+  AliDebug(1, Form("fStack->Particle(%d) = %p", fLabelMC, part));
+  AliDebug(1, Form("fStack->Particle(%d)->GetPdgCode() = %d", fLabelMC, part->GetPdgCode()));
 
   if (part) {
     if (part->GetFirstMother() != -1) {
       TParticle *partMother = fStack->Particle(part->GetFirstMother());
-      AliDebug(1, Form("fStack->Particle(part->GetFirstMother() = %p", partMother));
+      AliDebug(1, Form("fStack->Particle(%d) = %p", part->GetFirstMother(), partMother));
       if (partMother) {
        Char_t newName[100];
        if (partMother->GetFirstMother() != -1) history += "...  #rightarrow ";
@@ -1337,6 +1427,8 @@ void AliMuonForwardTrackFinder::PrintParticleHistory() {
       }
     }
     Char_t newName[100];
+    AliDebug(1, Form("fStack->Particle(%d)->GetPdgCode() = %d", fLabelMC, part->GetPdgCode()));
+    AliDebug(1, Form("fStack->Particle(%d)->GetName() = %s", fLabelMC, part->GetName()));
     PDGNameConverter(part->GetName(), newName);
     history += Form("%s  at  z = %5.1f cm", newName, part->Vz());
     //  printf("%s", history.Data());
@@ -1362,7 +1454,7 @@ void AliMuonForwardTrackFinder::PrintParticleHistory() {
 
 //===========================================================================================================================================
 
-Bool_t AliMuonForwardTrackFinder::IsMother(Char_t *nameMother) {
+Bool_t AliMuonForwardTrackFinder::IsMother(const Char_t *nameMother) {
   
   Bool_t result = kFALSE;
   
@@ -1503,9 +1595,6 @@ void AliMuonForwardTrackFinder::WriteHistos() {
   fOutputQAFile = new TFile(Form("MuonGlobalTracking.QA.run%d.root", fRun), "recreate");
   fOutputQAFile -> cd();
 
-  fHistPtSpectrometer              -> Write();
-  fHistPtMuonTrackWithGoodMatch    -> Write();
-  fHistPtMuonTrackWithBadMatch     -> Write();
   fHistRadiusEndOfAbsorber         -> Write();
   fHistNGoodClustersForFinalTracks -> Write();
 
@@ -1536,50 +1625,50 @@ void AliMuonForwardTrackFinder::WriteHistos() {
 
 void AliMuonForwardTrackFinder::PDGNameConverter(const Char_t *nameIn, Char_t *nameOut) {
 
-  if      (!strcmp(nameIn, "mu+"))     sprintf(nameOut, "#mu^{+}");
-  else if (!strcmp(nameIn, "mu-"))     sprintf(nameOut, "#mu^{-}");
-  else if (!strcmp(nameIn, "pi+"))     sprintf(nameOut, "#pi^{+}");
-  else if (!strcmp(nameIn, "pi-"))     sprintf(nameOut, "#pi^{-}");
-  else if (!strcmp(nameIn, "K+"))      sprintf(nameOut, "K^{+}");
-  else if (!strcmp(nameIn, "K-"))      sprintf(nameOut, "K^{-}");
-  else if (!strcmp(nameIn, "K*+"))     sprintf(nameOut, "K^{*+}");
-  else if (!strcmp(nameIn, "K*-"))     sprintf(nameOut, "K^{*-}");
-  else if (!strcmp(nameIn, "K_S0"))    sprintf(nameOut, "K_{S}^{0}");
-  else if (!strcmp(nameIn, "K_L0"))    sprintf(nameOut, "K_{L}^{0}");
-  else if (!strcmp(nameIn, "K0"))      sprintf(nameOut, "K^{0}");
-  else if (!strcmp(nameIn, "K0_bar"))  sprintf(nameOut, "#bar{K}^{0}");
-  else if (!strcmp(nameIn, "K*0"))     sprintf(nameOut, "K^{*0}");
-  else if (!strcmp(nameIn, "K*0_bar")) sprintf(nameOut, "#bar{K}^{*0}");
-  else if (!strcmp(nameIn, "rho0"))    sprintf(nameOut, "#rho^{0}");
-  else if (!strcmp(nameIn, "rho+"))    sprintf(nameOut, "#rho^{+}");
-  else if (!strcmp(nameIn, "rho-"))    sprintf(nameOut, "#rho^{-}");
-  else if (!strcmp(nameIn, "omega"))   sprintf(nameOut, "#omega");
-  else if (!strcmp(nameIn, "eta'"))    sprintf(nameOut, "#eta'");
-  else if (!strcmp(nameIn, "phi"))     sprintf(nameOut, "#phi");
-
-  else if (!strcmp(nameIn, "D-"))     sprintf(nameOut, "D^{-}");
-  else if (!strcmp(nameIn, "D+"))     sprintf(nameOut, "D^{+}");
-  else if (!strcmp(nameIn, "D0"))     sprintf(nameOut, "D^{0}");
-  else if (!strcmp(nameIn, "D0_bar")) sprintf(nameOut, "#bar{D}^{0}");
-  else if (!strcmp(nameIn, "D*-"))    sprintf(nameOut, "D^{*-}");
-  else if (!strcmp(nameIn, "D*+"))    sprintf(nameOut, "D^{*+}");
-  else if (!strcmp(nameIn, "D_s+"))   sprintf(nameOut, "D_{s}^{+}");
-  else if (!strcmp(nameIn, "D*_s+"))  sprintf(nameOut, "D_{s}^{*+}");
-
-  else if (!strcmp(nameIn, "B-"))       sprintf(nameOut, "B^{-}");
-  else if (!strcmp(nameIn, "B+"))       sprintf(nameOut, "B^{+}");
-  else if (!strcmp(nameIn, "B_s0_bar")) sprintf(nameOut, "#bar{B}_{s}^{0}");
-
-  else if (!strcmp(nameIn, "antiproton"))  sprintf(nameOut, "#bar{p}");
-  else if (!strcmp(nameIn, "proton"))      sprintf(nameOut, "p");
-  else if (!strcmp(nameIn, "neutron"))     sprintf(nameOut, "n");
-  else if (!strcmp(nameIn, "Sigma+"))      sprintf(nameOut, "#Sigma^{+}");
-  else if (!strcmp(nameIn, "Delta+"))      sprintf(nameOut, "#Delta{+}");
-  else if (!strcmp(nameIn, "Delta--"))     sprintf(nameOut, "#Delta{--}");
-  else if (!strcmp(nameIn, "Lambda0"))     sprintf(nameOut, "#Lambda_0");
-  else if (!strcmp(nameIn, "Lambda0_bar")) sprintf(nameOut, "#bar{Lambda}_0");
-
-  else sprintf(nameOut, "%s", nameIn);
+  if      (!strcmp(nameIn, "mu+"))     snprintf(nameOut, 50, "#mu^{+}");
+  else if (!strcmp(nameIn, "mu-"))     snprintf(nameOut, 50, "#mu^{-}");
+  else if (!strcmp(nameIn, "pi+"))     snprintf(nameOut, 50, "#pi^{+}");
+  else if (!strcmp(nameIn, "pi-"))     snprintf(nameOut, 50, "#pi^{-}");
+  else if (!strcmp(nameIn, "K+"))      snprintf(nameOut, 50, "K^{+}");
+  else if (!strcmp(nameIn, "K-"))      snprintf(nameOut, 50, "K^{-}");
+  else if (!strcmp(nameIn, "K*+"))     snprintf(nameOut, 50, "K^{*+}");
+  else if (!strcmp(nameIn, "K*-"))     snprintf(nameOut, 50, "K^{*-}");
+  else if (!strcmp(nameIn, "K_S0"))    snprintf(nameOut, 50, "K_{S}^{0}");
+  else if (!strcmp(nameIn, "K_L0"))    snprintf(nameOut, 50, "K_{L}^{0}");
+  else if (!strcmp(nameIn, "K0"))      snprintf(nameOut, 50, "K^{0}");
+  else if (!strcmp(nameIn, "K0_bar"))  snprintf(nameOut, 50, "#bar{K}^{0}");
+  else if (!strcmp(nameIn, "K*0"))     snprintf(nameOut, 50, "K^{*0}");
+  else if (!strcmp(nameIn, "K*0_bar")) snprintf(nameOut, 50, "#bar{K}^{*0}");
+  else if (!strcmp(nameIn, "rho0"))    snprintf(nameOut, 50, "#rho^{0}");
+  else if (!strcmp(nameIn, "rho+"))    snprintf(nameOut, 50, "#rho^{+}");
+  else if (!strcmp(nameIn, "rho-"))    snprintf(nameOut, 50, "#rho^{-}");
+  else if (!strcmp(nameIn, "omega"))   snprintf(nameOut, 50, "#omega");
+  else if (!strcmp(nameIn, "eta'"))    snprintf(nameOut, 50, "#eta'");
+  else if (!strcmp(nameIn, "phi"))     snprintf(nameOut, 50, "#phi");
+
+  else if (!strcmp(nameIn, "D-"))     snprintf(nameOut, 50, "D^{-}");
+  else if (!strcmp(nameIn, "D+"))     snprintf(nameOut, 50, "D^{+}");
+  else if (!strcmp(nameIn, "D0"))     snprintf(nameOut, 50, "D^{0}");
+  else if (!strcmp(nameIn, "D0_bar")) snprintf(nameOut, 50, "#bar{D}^{0}");
+  else if (!strcmp(nameIn, "D*-"))    snprintf(nameOut, 50, "D^{*-}");
+  else if (!strcmp(nameIn, "D*+"))    snprintf(nameOut, 50, "D^{*+}");
+  else if (!strcmp(nameIn, "D_s+"))   snprintf(nameOut, 50, "D_{s}^{+}");
+  else if (!strcmp(nameIn, "D*_s+"))  snprintf(nameOut, 50, "D_{s}^{*+}");
+
+  else if (!strcmp(nameIn, "B-"))       snprintf(nameOut, 50, "B^{-}");
+  else if (!strcmp(nameIn, "B+"))       snprintf(nameOut, 50, "B^{+}");
+  else if (!strcmp(nameIn, "B_s0_bar")) snprintf(nameOut, 50, "#bar{B}_{s}^{0}");
+
+  else if (!strcmp(nameIn, "antiproton"))  snprintf(nameOut, 50, "#bar{p}");
+  else if (!strcmp(nameIn, "proton"))      snprintf(nameOut, 50, "p");
+  else if (!strcmp(nameIn, "neutron"))     snprintf(nameOut, 50, "n");
+  else if (!strcmp(nameIn, "Sigma+"))      snprintf(nameOut, 50, "#Sigma^{+}");
+  else if (!strcmp(nameIn, "Delta+"))      snprintf(nameOut, 50, "#Delta{+}");
+  else if (!strcmp(nameIn, "Delta--"))     snprintf(nameOut, 50, "#Delta{--}");
+  else if (!strcmp(nameIn, "Lambda0"))     snprintf(nameOut, 50, "#Lambda_0");
+  else if (!strcmp(nameIn, "Lambda0_bar")) snprintf(nameOut, 50, "#bar{Lambda}_0");
+
+  else snprintf(nameOut, 50, "%s", nameIn);
 
 }