]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/UPGRADE/AliITSUTrackerGlo.cxx
Reco Interface: changed sensor mapping in the layer to account for multiple rows
[u/mrichter/AliRoot.git] / ITS / UPGRADE / AliITSUTrackerGlo.cxx
index adfe112de747ccf03cc93747919ea3ea94ad12cf..ac51a7cc9d88c7ea5fee30e144d44ad01796298e 100644 (file)
@@ -35,7 +35,6 @@
 #include "AliITSUReconstructor.h"
 #include "AliITSReconstructor.h"
 #include "AliITSUSeed.h"
-#include "AliITSUAux.h"
 #include "AliITSUClusterPix.h"
 #include "AliITSUGeomTGeo.h"
 #include "AliCodeTimer.h"
@@ -54,6 +53,7 @@ const Double_t AliITSUTrackerGlo::fgkToler =  1e-6;// tolerance for layer on-sur
 AliITSUTrackerGlo::AliITSUTrackerGlo(AliITSUReconstructor* rec)
 :  fReconstructor(rec)
   ,fITS(0)
+  ,fMatLUT(0)
   ,fCurrESDtrack(0)
   ,fCurrESDtrMClb(kDummyLabel)
   ,fCurrMass(kPionMass)
@@ -61,6 +61,7 @@ AliITSUTrackerGlo::AliITSUTrackerGlo(AliITSUReconstructor* rec)
   ,fCountITSin(0)
   ,fCountITSout(0)
   ,fCountITSrefit(0)
+  ,fNTracksESD(0)
   ,fHypStore(100)
   ,fLayerMaxCandidates(1000)
   ,fLayerCandidates(0)
@@ -78,9 +79,12 @@ AliITSUTrackerGlo::AliITSUTrackerGlo(AliITSUReconstructor* rec)
   ,fCurrTrackCond(0)
   ,fCurrActLrID(-1)
   ,fCurrLayer(0)
-  ,fTrackPhase(-1)
-#ifdef  _FILL_CONTROL_HISTOS_
-  ,fCHistoArr(0)
+  ,fTrackPhaseID(-1)
+  ,fCurrPassID(-1)
+  ,fUseMatLUT(kFALSE)
+#ifdef  _ITSU_TUNING_MODE_
+  ,fCHistoArrCorr(0)
+  ,fCHistoArrFake(0)
 #endif
 {
   // Default constructor
@@ -95,18 +99,26 @@ AliITSUTrackerGlo::~AliITSUTrackerGlo()
   delete[] fLayerCandidates;
   if (fWorkHyp) fWorkHyp->SetTPCSeed(0); // this hypothesis does not own the seed
   delete fWorkHyp;
+  delete fMatLUT;
   //
-#ifdef  _FILL_CONTROL_HISTOS_
-  if (fCHistoArr) {
+#ifdef  _ITSU_TUNING_MODE_
+  if (fCHistoArrCorr || fCHistoArrFake) {
     TFile* ctrOut = TFile::Open("itsuControlHistos.root","recreate");
     ctrOut->cd();
     AliInfo("Storing control histos");
-    fCHistoArr->Print();
     //    ctrOut->WriteObject(fCHistoArr,"controlH","kSingleKey");
-    fCHistoArr->Write();
+    if (fCHistoArrCorr) {
+      fCHistoArrCorr->Write();
+      delete fCHistoArrCorr;
+    }
+    if (fCHistoArrFake) {
+      fCHistoArrFake->Write();
+      delete fCHistoArrFake;
+    }
     ctrOut->Close();
     delete ctrOut;
-    fCHistoArr = 0;
+    fCHistoArrCorr = 0;
+    fCHistoArrFake = 0;    
   }
 #endif 
   //
@@ -120,13 +132,23 @@ void AliITSUTrackerGlo::Init(AliITSUReconstructor* rec)
   fITS = rec->GetITSInterface();
   fNLrActive = fITS->GetNLayersActive();
   fWorkHyp = new AliITSUTrackHyp(fNLrActive);
+  // create material lookup table
+  const int kNTest = 1000;
+  const double kStepsPerCM=5;
+  fMatLUT  = new AliITSUMatLUT(fITS->GetRMin(),fITS->GetRMax(),Nint(kStepsPerCM*(fITS->GetRMax()-fITS->GetRMin())));
+  double zmn = 1e6;
+  for (int ilr=fITS->GetNLayers();ilr--;) {
+    AliITSURecoLayer* lr = fITS->GetLayer(ilr);
+    if (zmn>Abs(lr->GetZMin())) zmn = Abs(lr->GetZMin());
+    if (zmn>Abs(lr->GetZMax())) zmn = Abs(lr->GetZMax());
+  }
+  fMatLUT->FillData(kNTest,-zmn,zmn);
   //
   if (fLayerMaxCandidates<1) fLayerMaxCandidates = 1000;
   fLayerCandidates = new AliITSUSeed*[fLayerMaxCandidates];
   fSeedsPool.ExpandCreateFast(1000); // RS TOCHECK
   fFreeSeedsID.Set(1000);
   fESDIndex.Set(1000);
-  
   //
 }
 
@@ -139,7 +161,7 @@ void AliITSUTrackerGlo::CreateDefaultTrackCond()
   //
   cond->SetNLayers(fNLrActive);
   cond->AddNewCondition(fNLrActive);
-  cond->AddGroupPattern( 0xffff ); // require all layers hit
+  cond->AddGroupPattern( 0xffff, fNLrActive ); // require all layers hit
   cond->Init();
   //
   fDefTrackConds.AddLast(cond);
@@ -157,8 +179,9 @@ Int_t AliITSUTrackerGlo::Clusters2Tracks(AliESDEvent *esdEv)
   AliCodeTimerAuto("",0);
   SetTrackingPhase(kClus2Tracks);
   //
-#ifdef  _FILL_CONTROL_HISTOS_
-  if (!fCHistoArr) BookControlHistos();
+#ifdef  _ITSU_TUNING_MODE_
+  if (!fCHistoArrCorr) fCHistoArrCorr = BookControlHistos("Corr");
+  if (!fCHistoArrFake) fCHistoArrFake = BookControlHistos("Fake");
 #endif
   static int evID = 0;
   static TArrayF esdTrPt(fESDIndex.GetSize()); 
@@ -171,9 +194,10 @@ Int_t AliITSUTrackerGlo::Clusters2Tracks(AliESDEvent *esdEv)
   fCountITSrefit = 0;
   //
   ResetSeedsPool();
-  int nTrESD = esdEv->GetNumberOfTracks();
-  AliInfo(Form("Will try to find prolongations for %d tracks",nTrESD));
+  fNTracksESD = esdEv->GetNumberOfTracks();
+  AliInfo(Form("Will try to find prolongations for %d tracks",fNTracksESD));
   int nTrackCond = AliITSUReconstructor::GetRecoParam()->GetNTrackingConditions();
+  fUseMatLUT = AliITSUReconstructor::GetRecoParam()->GetUseMatLUT(fTrackPhaseID);
   if (nTrackCond<1) {
     if (!fDefTrackConds.GetEntriesFast()) {
       AliInfo("No tracking conditions found in recoparams, creating default one requesting all layers hit");
@@ -190,34 +214,40 @@ Int_t AliITSUTrackerGlo::Clusters2Tracks(AliESDEvent *esdEv)
     first = kFALSE;
   }
   fHypStore.Delete();
-  if (fHypStore.GetSize()<nTrESD) fHypStore.Expand(nTrESD+100);
+  if (fHypStore.GetSize()<fNTracksESD) fHypStore.Expand(fNTracksESD+100);
   //
   fITS->ProcessClusters();
   //
-#ifdef  _FILL_CONTROL_HISTOS_
+#ifdef  _ITSU_TUNING_MODE_
   FlagSplitClusters(); // tmp RS
 #endif
   //
   // the tracks will be reconstructed in decreasing pt order, sort them
-  if (fESDIndex.GetSize()<nTrESD) {
-    fESDIndex.Set(nTrESD+200);
-    esdTrPt.Set(nTrESD+200);
+  if (fESDIndex.GetSize()<fNTracksESD) {
+    fESDIndex.Set(fNTracksESD+200);
+    esdTrPt.Set(fNTracksESD+200);
   }
   int* esdTrackIndex = fESDIndex.GetArray();
   float* trPt = esdTrPt.GetArray();
-  for (int itr=nTrESD;itr--;) trPt[itr] = esdEv->GetTrack(itr)->Pt();
-  Sort(nTrESD,trPt,esdTrackIndex,kTRUE);    
+  for (int itr=fNTracksESD;itr--;) {
+    AliESDtrack* esdTr = esdEv->GetTrack(itr);
+    esdTr->SetStatus(AliESDtrack::kITSupg);   // Flag all tracks as participating in the ITSup reco
+    trPt[itr] = esdTr->Pt();
+  }
+  Sort(fNTracksESD,trPt,esdTrackIndex,kTRUE);    
   //
   for (int icnd=0;icnd<nTrackCond;icnd++) {
+    fCurrPassID = icnd;
     fCurrTrackCond = (AliITSUTrackCond*)trackConds->UncheckedAt(icnd);
     if (!fCurrTrackCond->IsInitDone()) fCurrTrackCond->Init();
     // select ESD tracks to propagate
-    for (int itr=0;itr<nTrESD;itr++) {
+    for (int itr=0;itr<fNTracksESD;itr++) {
       int trID = esdTrackIndex[itr];
       fCurrESDtrack = esdEv->GetTrack(trID);
       fCurrESDtrMClb = fCurrESDtrack->GetLabel();
       //
-      if (!NeedToProlong(fCurrESDtrack)) continue;  // are we interested in this track?
+      
+      if (!NeedToProlong(fCurrESDtrack,trID)) continue;  // are we interested in this track?
       /*
        // if specific tracks should be checked, create a global array int wtc[] = {evITS*10000+trID, ...};
       Bool_t dbg = kFALSE;
@@ -226,29 +256,33 @@ Int_t AliITSUTrackerGlo::Clusters2Tracks(AliESDEvent *esdEv)
       for (int k=0;k<nwtc;k++) {
        if (wtc[k]==evID*10000+trID) {
          dbg = kTRUE;
-         printf("\n\n\n\n\n\n\n");
          printf("At esdTr: %d MC: %d\n",wtc[k],fCurrESDtrMClb);
-         printf("\n\n\n\n\n\n\n");
          break;
        }
       }
       AliLog::SetClassDebugLevel("AliITSUTrackerGlo",dbg ? 10:0);
       */
-
-      AliDebug(1,Form("Processing track %d(esd%d) | M=%.3f Pt=%.3f | MCLabel: %d",itr,trID,fCurrESDtrack->GetMass(kTRUE),fCurrESDtrack->Pt(),fCurrESDtrMClb));//RS
+#ifdef _ITSU_DEBUG_
+      AliDebug(1,Form("Processing track %d(esd%d) | M=%.3f Pt=%.3f | MCLabel: %d",itr,trID,fCurrESDtrack->GetMassForTracking(kTRUE),fCurrESDtrack->Pt(),fCurrESDtrMClb));//RS
+#endif
       FindTrack(fCurrESDtrack, trID);
     }   
     //
+#ifdef _ITSU_DEBUG_
     if (AliDebugLevelClass()>+2) {
       AliInfo(Form("SeedsPool: %d, BookedUpTo: %d, free: %d",fSeedsPool.GetSize(),fSeedsPool.GetEntriesFast(),fNFreeSeeds));
       fHypStore.Print();
     }
+#endif
     FinalizeHypotheses();
+#ifdef  _ITSU_TUNING_MODE_
+    CheckClusterUsage(); //!!RS
+#endif
     //
     //AliLog::SetClassDebugLevel("AliITSUTrackerGlo",0);     // in case wtc array was defined, uncomment this
   }
   //
-  AliInfo(Form("%d ITSin for %d tried TPC seeds out of %d ESD tracks\n",fCountITSin,fCountProlongationTrials,nTrESD));
+  AliInfo(Form("%d ITSin for %d tried TPC seeds out of %d ESD tracks\n",fCountITSin,fCountProlongationTrials,fNTracksESD));
   evID++;
   return 0;
 }
@@ -261,16 +295,19 @@ Int_t AliITSUTrackerGlo::PropagateBack(AliESDEvent *esdEv)
   AliCodeTimerAuto("",0);
   //
   SetTrackingPhase(kPropBack);
-  int nTrESD = esdEv->GetNumberOfTracks();
-  AliDebug(1,Form("Will propagate back %d tracks",nTrESD));
+  fNTracksESD = esdEv->GetNumberOfTracks();
+  fUseMatLUT = AliITSUReconstructor::GetRecoParam()->GetUseMatLUT(fTrackPhaseID);
+#ifdef _ITSU_DEBUG_
+  AliDebug(1,Form("Will propagate back %d tracks",fNTracksESD));
+#endif
   //
   double bz0 = GetBz();
   Double_t xyzTrk[3],xyzVtx[3]={GetX(),GetY(),GetZ()};
   AliITSUTrackHyp dummyTr;
   const double kWatchStep=10.; // for larger steps watch arc vs segment difference
-  Double_t times[AliPID::kSPECIES];
+  Double_t times[AliPID::kSPECIESC];
   //
-  for (int itr=0;itr<nTrESD;itr++) {
+  for (int itr=0;itr<fNTracksESD;itr++) {
     fCurrESDtrack = esdEv->GetTrack(itr);
     fCurrESDtrMClb = fCurrESDtrack->GetLabel();
     // Start time integral and add distance from current position to vertex 
@@ -281,7 +318,7 @@ Int_t AliITSUTrackerGlo::PropagateBack(AliESDEvent *esdEv)
     {
       double dxs = xyzTrk[0] - xyzVtx[0];
       double dys = xyzTrk[1] - xyzVtx[1];
-      double dzs = xyzTrk[2] - xyzVtx[2];
+      //      double dzs = xyzTrk[2] - xyzVtx[2];
       // RS: for large segment steps d use approximation of cicrular arc s by
       // s = 2R*asin(d/2R) = d/p asin(p) \approx d/p (p + 1/6 p^3) = d (1+1/6 p^2)
       // where R is the track radius, p = d/2R = 2C*d (C is the abs curvature)
@@ -292,7 +329,9 @@ Int_t AliITSUTrackerGlo::PropagateBack(AliESDEvent *esdEv)
        double fcarc = 1.+crv*crv*dst/6.;
        dst *= fcarc*fcarc;
       }
-      dst += dzs*dzs;
+      // RS: temporary hack since we don't have SPD vertex:      
+      //      dst += dzs*dzs;
+      dst *= 1+fCurrESDtrack->GetTgl()*fCurrESDtrack->GetTgl();
       dst = Sqrt(dst); 
     }
     //    
@@ -302,7 +341,7 @@ Int_t AliITSUTrackerGlo::PropagateBack(AliESDEvent *esdEv)
       dummyTr.AliExternalTrackParam::operator=(*fCurrESDtrack);
       dummyTr.StartTimeIntegral();
       dummyTr.AddTimeStep(dst);
-      dummyTr.GetIntegratedTimes(times); 
+      dummyTr.GetIntegratedTimes(times,AliPID::kSPECIESC); 
       fCurrESDtrack->SetIntegratedTimes(times);
       fCurrESDtrack->SetIntegratedLength(dummyTr.GetIntegratedLength());
       continue;
@@ -313,16 +352,18 @@ Int_t AliITSUTrackerGlo::PropagateBack(AliESDEvent *esdEv)
     fCurrHyp->StartTimeIntegral();
     fCurrHyp->AddTimeStep(dst);
     fCurrHyp->ResetCovariance(10000);
-    double chi2 = RefitTrack(fCurrHyp,fITS->GetRMax());
+    int nclFit = 0;
+    double chi2 = RefitTrack(fCurrHyp,fITS->GetRMax(),nclFit);
     if (chi2>0) { // propagate to exit from the ITS/TPC screen
-      int ndf = fCurrHyp->GetWinner()->GetNLayersHit()*2-5;
+      int ndf = nclFit*2-5;
       if (ndf>0) chi2 /= ndf;
       fCurrHyp->SetChi2(chi2);
       UpdateESDTrack(fCurrHyp,AliESDtrack::kITSout);
-      fCountITSout++;
     }
     else {
+#ifdef _ITSU_DEBUG_
       AliDebug(2,Form("Refit Failed for track %d | ESDtrack#%d (MClb:%d)",itr,fCurrESDtrack->GetID(),fCurrESDtrMClb));
+#endif
       //fCurrHyp->AliExternalTrackParam::Print();
       //fCurrHyp->GetWinner()->Print();
     }
@@ -330,7 +371,7 @@ Int_t AliITSUTrackerGlo::PropagateBack(AliESDEvent *esdEv)
   }
   //
   AliInfo(Form("%d ITSout in %d ITSin tracks for %d tried TPC seeds out of %d ESD tracks\n",
-              fCountITSout,fCountITSin,fCountProlongationTrials,nTrESD));
+              fCountITSout,fCountITSin,fCountProlongationTrials,fNTracksESD));
   //
   return 0;
 }
@@ -343,12 +384,15 @@ Int_t AliITSUTrackerGlo::RefitInward(AliESDEvent *esdEv)
   AliCodeTimerAuto("",0);
   //
   SetTrackingPhase(kRefitInw);
-  Int_t nTrESD = esdEv->GetNumberOfTracks();
+  fUseMatLUT = AliITSUReconstructor::GetRecoParam()->GetUseMatLUT(fTrackPhaseID);
+  fNTracksESD = esdEv->GetNumberOfTracks();
   //  AliLog::SetClassDebugLevel("AliITSUTrackerGlo",10);
 
-  AliDebug(1,Form("Will refit inward %d tracks",nTrESD));
+  AliDebug(1,Form("Will refit inward %d tracks",fNTracksESD));
+  //  Bool_t uselogMS = AliExternalTrackParam::GetUseLogTermMS();
+  //  AliExternalTrackParam::SetUseLogTermMS(kTRUE);
   //
-  for (int itr=0;itr<nTrESD;itr++) {
+  for (int itr=0;itr<fNTracksESD;itr++) {
     fCurrESDtrack = esdEv->GetTrack(itr);
     fCurrESDtrMClb = fCurrESDtrack->GetLabel();
     // Start time integral and add distance from current position to vertex 
@@ -357,23 +401,29 @@ Int_t AliITSUTrackerGlo::RefitInward(AliESDEvent *esdEv)
     if (   trStat & AliESDtrack::kITSrefit ) continue; // already done
     if (  (trStat & AliESDtrack::kTPCout) && !(trStat & AliESDtrack::kTPCrefit) ) continue;
     //
-    fCurrHyp = GetTrackHyp(itr);
-    fCurrHyp->AliExternalTrackParam::operator=(*fCurrESDtrack);  // fetch current ESDtrack kinematics
+    fCurrHyp  = GetTrackHyp(itr);
     fCurrMass = fCurrHyp->GetMass();
+    *fCurrHyp = *fCurrESDtrack;  // fetch current ESDtrack kinematics
     //
-    double chi2 = RefitTrack(fCurrHyp,fITS->GetRMin());
+    int nclFit = 0;
+    //    fCurrHyp->ResetCovariance(1000); // in case we want to do SA refit
+    double chi2 = RefitTrack(fCurrHyp,fITS->GetRMin(),nclFit);
     if (chi2>0) { // propagate up to inside radius of the beam pipe      
+      int ndf = nclFit*2-5;
+      if (ndf>0) chi2 /= ndf;
       fCurrHyp->SetChi2(chi2);
       UpdateESDTrack(fCurrHyp,AliESDtrack::kITSrefit);
-      fCountITSrefit++;
     }
     else {
+#ifdef _ITSU_DEBUG_
       AliDebug(2,Form("Refit Failed for track %d |ESDtrack#%d (MClb:%d)",itr,fCurrESDtrack->GetID(),fCurrESDtrMClb));
+#endif
     }
   }    
+  //  AliExternalTrackParam::SetUseLogTermMS(uselogMS);
   //
   AliInfo(Form("%d ITSrefit in %d ITSout in %d ITSin tracks for %d tried TPC seeds out of %d ESD tracks\n",
-              fCountITSrefit,fCountITSout,fCountITSin,fCountProlongationTrials,nTrESD));
+              fCountITSrefit,fCountITSout,fCountITSin,fCountProlongationTrials,fNTracksESD));
   //
   //  AliLog::SetClassDebugLevel("AliITSUTrackerGlo",0);
   return 0;
@@ -408,11 +458,21 @@ AliCluster * AliITSUTrackerGlo::GetCluster(Int_t /*index*/) const
 } 
 
 //_________________________________________________________________________
-Bool_t AliITSUTrackerGlo::NeedToProlong(AliESDtrack* esdTr)
+Bool_t AliITSUTrackerGlo::NeedToProlong(AliESDtrack* esdTr, Int_t esdID)
 {
   // do we need to match this track to ITS?
   //
   static double bz = GetBz();
+  //
+  // is it already prolonged
+  if (esdTr->IsOn(AliESDtrack::kITSin)) return kFALSE;
+  AliITSUTrackHyp* hyp = GetTrackHyp(esdID); // in case there is unfinished hypothesis from prev.pass, clean it
+  if (hyp) {
+    CleanHypothesis(hyp);
+    if (hyp->GetSkip()) return kFALSE; // need to skip this
+    return kTRUE;
+  }
+  //
   if (!esdTr->IsOn(AliESDtrack::kTPCin) ||
       esdTr->IsOn(AliESDtrack::kTPCout) ||
       esdTr->IsOn(AliESDtrack::kITSin)  ||
@@ -439,21 +499,23 @@ void AliITSUTrackerGlo::FindTrack(AliESDtrack* esdTr, Int_t esdID)
   AliITSUSeed seedUC;  // copy of the seed from the upper layer
   AliITSUSeed seedT;   // transient seed between the seedUC and new prolongation hypothesis
   //
+  //  AliLog::SetClassDebugLevel("AliITSUTrackerGlo",10);
   AliITSUTrackHyp* hypTr = InitHypothesis(esdTr,esdID);  // initialize prolongations hypotheses tree
   if (!hypTr || hypTr->GetSkip()) return;
   //
+  double bz = GetBz();
   fCurrHyp = fWorkHyp;
   fCurrHyp->InitFrom(hypTr);
   //
-  AliITSURecoSens *hitSens[AliITSURecoSens::kNNeighbors+1];
+  AliITSURecoSens *hitSens[AliITSURecoLayer::kNNeighbors+1];
   //
-  TObjArray clArr; // container for transfer of clusters matching to seed
+  int ilaUp = fNLrActive;     // previous active layer really checked (some may be excluded!)
   //
-  for (int ila=fNLrActive;ila--;) {
+  for (int ila=fNLrActive;ila--;ilaUp=fCurrActLrID) {
+    if (fCurrTrackCond->IsLayerExcluded(ila)) continue;
     fCurrActLrID = ila;
     fCurrLayer = fITS->GetLayerActive(ila);
     Bool_t noClSharing = fCurrTrackCond->GetClSharing(ila)==0;
-    int ilaUp = ila+1;                         // prolong seeds from layer above
     //
     // for the outermost layer the seed is created from the ESD track
     int nSeedsUp = (ilaUp==fNLrActive) ? 1 : fCurrHyp->GetNSeeds(ilaUp);
@@ -474,10 +536,14 @@ void AliITSUTrackerGlo::FindTrack(AliESDtrack* esdTr, Int_t esdID)
       }
       seedUC.ResetFMatrix();                    // reset the matrix for propagation to next layer
       // go till next active layer
+#ifdef _ITSU_DEBUG_
       AliDebug(2,Form("working on Lr:%d Seed:%d of %d for esdID=%d (MClb:%d) | pT=%.3f",ila,isd,nSeedsUp,esdID,fCurrESDtrMClb,seedUC.Pt()));
+#endif
       if (!TransportToLayer(&seedUC, fITS->GetLrIDActive(ilaUp), fITS->GetLrIDActive(ila)) ) { // external seed already prolonged
        //
+#ifdef _ITSU_DEBUG_
        AliDebug(2,Form("Transport failed | esdID=%d (MClb:%d)",esdID,fCurrESDtrMClb));
+#endif
        // Check if the seed satisfies to track definition
        if (NeedToKill(&seedUC,kTransportFailed) && seedU) KillSeed(seedU,kTRUE);
        continue; // RS TODO: decide what to do with tracks stopped on higher layers w/o killing
@@ -498,11 +564,12 @@ void AliITSUTrackerGlo::FindTrack(AliESDtrack* esdTr, Int_t esdID)
       int nsens = fCurrLayer->FindSensors(&fTrImpData[kTrPhi0], hitSens, mcquest);  // find detectors which may be hit by the track
       */
       int nsens = fCurrLayer->FindSensors(&fTrImpData[kTrPhi0], hitSens);  // find detectors which may be hit by the track
+#ifdef _ITSU_DEBUG_
       AliDebug(2,Form("Will check %d sensors on lr:%d | esdID=%d (MClb:%d)",nsens,ila,esdID,fCurrESDtrMClb));
+#endif
       //
       seedUC.SetLr(ila);
       //
-      double bz = GetBz();
       for (int isn=nsens;isn--;) {
        AliITSURecoSens* sens = hitSens[isn];
        int ncl = sens->GetNClusters();
@@ -513,16 +580,19 @@ void AliITSUTrackerGlo::FindTrack(AliESDtrack* esdTr, Int_t esdID)
        // since the transport matrix should be defined in this frame.
        double xs; // X in the TF of current seed, corresponding to intersection with sensor plane
        if (!seedT.GetTrackingXAtXAlpha(sens->GetXTF(),sens->GetPhiTF(),bz, xs)) {
+#ifdef _ITSU_DEBUG_
          if (AliDebugLevelClass()>2) {
            printf("Failed on GetTrackingXAtXAlpha: X=%.4f alp=%.3f\n",sens->GetXTF(),sens->GetPhiTF());
            seedT.Print("etp");
          }
+#endif
          continue;
        }
        if (xs<seedT.GetX()) {
          if (!PropagateSeed(&seedT,xs,fCurrMass)) continue;
        }
        else { // some low precision tracks may hit the sensor plane outside of the layer radius
+#ifdef _ITSU_DEBUG_
          if (AliDebugLevelClass()>2) {
            if (!seedT.ContainsFake()) {
              printf("WRONG PROP on L%d, sens%d of %d: %.4f > %.4f\n",ila,isn,nsens,xs,seedT.GetX());
@@ -530,6 +600,7 @@ void AliITSUTrackerGlo::FindTrack(AliESDtrack* esdTr, Int_t esdID)
              seedT.Print("etp");           
            }
          }
+#endif
          if (!seedT.PropagateParamOnlyTo(xs,bz)) continue;
        }
        //      if (!seedT.PropagateToX(xs,bz)) continue;
@@ -552,11 +623,14 @@ void AliITSUTrackerGlo::FindTrack(AliESDtrack* esdTr, Int_t esdID)
       // cluster search is done. Do we need to have a version of this seed skipping current layer
       if (!NeedToKill(&seedUC,kMissingCluster)) {
        AliITSUSeed* seedSkp = NewSeedFromPool(&seedUC);
-       double penalty = -fCurrTrackCond->GetMissPenalty(ila);
-       // to do: make penalty to account for probability to miss the cluster for good reason
-       seedSkp->SetChi2Cl(penalty);
-       AddSeedBranch(seedSkp);
-       //      AddProlongationHypothesis(seedSkp,ila);      
+       if (nsens) { // to do: make penalty to account for probability to miss the cluster for good reason
+         double penalty = -fCurrTrackCond->GetMissPenalty(ila);
+         seedSkp->SetChi2Cl(penalty);
+       }
+       if (seedSkp->GetChi2GloNrm()>fCurrTrackCond->GetMaxChi2GloNrm(ila)) {
+         MarkSeedFree(seedSkp);
+       }
+       else AddSeedBranch(seedSkp);
       }
       // transfer the new branches of the seed to the hypothesis container
       if (fNBranchesAdded) ValidateAllowedBranches(maxNBranches);
@@ -564,10 +638,12 @@ void AliITSUTrackerGlo::FindTrack(AliESDtrack* esdTr, Int_t esdID)
     }
     if (fNCandidatesAdded) ValidateAllowedCandidates(ila,maxNCandidates);
     //    ((TObjArray*)fCurrHyp->GetLayerSeeds(ila))->Sort();
+#ifdef _ITSU_DEBUG_
     if (AliDebugLevelClass()>2) { //RS
       printf(">>> All hypotheses on lr %d: \n",ila);
       for (int ih=0;ih<fCurrHyp->GetNSeeds(ila);ih++) {printf(" #%3d ",ih); fCurrHyp->GetSeed(ila,ih)->Print();}
     }
+#endif
     //
     /*
     for (int ih=0;ih<fCurrHyp->GetNSeeds(ila);ih++) {
@@ -584,11 +660,14 @@ void AliITSUTrackerGlo::FindTrack(AliESDtrack* esdTr, Int_t esdID)
   }
   //
   SaveReducedHypothesesTree(hypTr);
+#ifdef _ITSU_DEBUG_
   if (AliDebugLevelClass()>1) {
     printf("\nSaved hypotheses for esdTrack %d (MCLab:%d)\n",esdID,fCurrESDtrMClb);
     hypTr->Print("l");
   }
+#endif
   fCurrHyp = 0;
+  //  AliLog::SetClassDebugLevel("AliITSUTrackerGlo",0);
   //
 }
 
@@ -601,8 +680,7 @@ AliITSUTrackHyp* AliITSUTrackerGlo::InitHypothesis(AliESDtrack *esdTr, Int_t esd
   //
   fCountProlongationTrials++;
   //
-  fCurrMass = esdTr->GetMass();
-  if (fCurrMass<kPionMass*0.9) fCurrMass = kPionMass; // don't trust to mu, e identification from TPCin
+  fCurrMass = esdTr->GetMassForTracking();
   //
   hyp = new AliITSUTrackHyp(fNLrActive);
   hyp->SetESDTrack(esdTr);
@@ -610,8 +688,10 @@ AliITSUTrackHyp* AliITSUTrackerGlo::InitHypothesis(AliESDtrack *esdTr, Int_t esd
   hyp->SetMass(fCurrMass);
   hyp->SetTPCSeed( new AliExternalTrackParam(*esdTr) );
   SetTrackHyp(hyp,esdID);
-
+  Bool_t lut = fUseMatLUT; // for propagation from TPC use TGeo material budget always
+  fUseMatLUT = kFALSE;
   if (!TransportToLayer(hyp->GetTPCSeed(),fITS->GetNLayers(), fITS->GetLrIDActive(fNLrActive-1), fITS->GetRITSTPCRef())) hyp->SetSkip(); // propagate to outer R of ITS
+  fUseMatLUT = lut;
   //
   return hyp;
   // TO DO
@@ -654,7 +734,9 @@ Bool_t AliITSUTrackerGlo::TransportToLayer(AliITSUSeed* seed, Int_t lFrom, Int_t
       //      
       return kFALSE;
     }
+#ifdef _ITSU_DEBUG_
     AliDebug(2,Form("go in dir=%d to R=%.4f(X:%.4f)",dir,lrTo->GetR(-dir), xToGo));
+#endif
     if (!PropagateSeed(seed,xToGo,fCurrMass,100, kFALSE )) {
       //printf("FailHere2: %f %f %d\n",xts,xToGo,dir);
       //seed->Print("etp");
@@ -702,7 +784,9 @@ Bool_t AliITSUTrackerGlo::TransportToLayer(AliExternalTrackParam* seed, Int_t lF
       //      seed->Print("etp");
       return kFALSE;
     }
+#ifdef _ITSU_DEBUG_
     AliDebug(2,Form("go in dir=%d to R=%.4f(X:%.4f)",dir,lrTo->GetR(-dir), xToGo));
+#endif
     if (!PropagateSeed(seed,xToGo,fCurrMass,100, kFALSE )) {
       //printf("FailHere2: %f %f %d\n",xts,xToGo,dir);
       //seed->Print("etp");
@@ -744,7 +828,9 @@ Bool_t AliITSUTrackerGlo::TransportToLayerX(AliExternalTrackParam* seed, Int_t l
     }
     if ( (dir>0&&xToGo>xStop) || (dir<0&&xToGo<xStop) ) xToGo = xStop;
     //
+#ifdef _ITSU_DEBUG_
     AliDebug(2,Form("go in dir=%d to R=%.4f(X:%.4f)",dir,lrTo->GetR(-dir), xToGo));
+#endif
     if (!PropagateSeed(seed,xToGo,fCurrMass,100, kFALSE )) {
       //printf("FailHere2: %f %f %d\n",xts,xToGo,dir);
       //seed->Print("etp");
@@ -764,7 +850,9 @@ Bool_t AliITSUTrackerGlo::GoToExitFromLayer(AliITSUSeed* seed, AliITSURecoLayer*
   double xToGo = lr->GetR(dir);
   if (check) { // do we need to track till the surface of the current layer ?
     double curR2 = seed->GetX()*seed->GetX() + seed->GetY()*seed->GetY(); // current radius
+#ifdef _ITSU_DEBUG_
     AliDebug(3,Form(" dir:%d Cur: %e Tgt: %e",dir,Sqrt(curR2),xToGo));
+#endif
     if      (dir>0) { if (curR2-xToGo*xToGo>-fgkToler) return kTRUE; } // on the surface or outside of the layer
     else if (dir<0) { if (xToGo*xToGo-curR2>-fgkToler) return kTRUE; } // on the surface or outside of the layer
   }
@@ -787,7 +875,9 @@ Bool_t AliITSUTrackerGlo::GoToExitFromLayer(AliExternalTrackParam* seed, AliITSU
     if      (dir>0) { if (curR2-xToGo*xToGo>-fgkToler) return kTRUE; } // on the surface or outside of the layer
     else if (dir<0) { if (xToGo*xToGo-curR2>-fgkToler) return kTRUE; } // on the surface or outside of the layer
   }
+#ifdef _ITSU_DEBUG_
   AliDebug(2,Form(" dir=%d : from R=%.4f -> R=%.4f",dir,Sqrt(seed->GetX()*seed->GetX() + seed->GetY()*seed->GetY()), xToGo));
+#endif
   if (!seed->GetXatLabR(xToGo,xToGo,GetBz(),dir)) return kFALSE;
   // go via layer to its boundary, applying material correction.
   if (!PropagateSeed(seed,xToGo,fCurrMass, lr->GetMaxStep())) return kFALSE;
@@ -922,45 +1012,36 @@ Int_t AliITSUTrackerGlo::CheckCluster(AliITSUSeed* track, Int_t lr, Int_t clID)
   if (cl->GetLabel(0)>=0) {
     for (int i=0;i<3;i++) if (cl->GetLabel(i)>=0 && cl->GetLabel(i)==currLabel) {goodCl = kTRUE; break;}
   }
-  Bool_t goodSeed = !track->ContainsFake();
   //
   if (TMath::Abs(cl->GetX())>kTolerX) { // if due to the misalingment X is large, propagate track only
     if (!track->PropagateParamOnlyTo(track->GetX()+cl->GetX(),GetBz())) {
-      if (goodCl&&goodSeed && AliDebugLevelClass()>2 ) {
+      //
+#ifdef _ITSU_DEBUG_
+      if (AliDebugLevelClass()>2 && goodCl && !track->ContainsFake()) {
        AliDebug(2,Form("Lost good cl on L:%d failed propagation. |ESDtrack#%d (MClb:%d)",lr,fCurrESDtrack->GetID(),fCurrESDtrMClb)); 
        track->Print("etp");
        cl->Print();
       }
+#endif
       return kStopSearchOnSensor; // propagation failed, seedT is intact
     }
   }
   double dy = cl->GetY()-track->GetY();
   double dz = cl->GetZ()-track->GetZ();
   //
-#ifdef  _FILL_CONTROL_HISTOS_
-  int hcOffs = (1+fTrackPhase)*kHistosPhase + lr;
-  double htrPt=-1;
-  if (goodCl && (((AliITSUClusterPix*)cl)->GetNPix()>1 || !((AliITSUClusterPix*)cl)->IsSplit()) && goodSeed && fCHistoArr /* && track->GetChi2Penalty()<1e-5*/) {
-    htrPt = track->Pt();
-    ((TH2*)fCHistoArr->At(kHResY+hcOffs))->Fill(htrPt,dy);
-    ((TH2*)fCHistoArr->At(kHResZ+hcOffs))->Fill(htrPt,dz);
-    double errY = track->GetSigmaY2();
-    double errZ = track->GetSigmaZ2();
-    if (errY>0) ((TH2*)fCHistoArr->At(kHResYP+hcOffs))->Fill(htrPt,dy/Sqrt(errY));
-    if (errZ>0) ((TH2*)fCHistoArr->At(kHResZP+hcOffs))->Fill(htrPt,dz/Sqrt(errZ));
-  }
-#endif  
-  //
   double dy2 = dy*dy;
   double tol2 = (track->GetSigmaY2() + AliITSUReconstructor::GetRecoParam()->GetSigmaY2(lr))*
     fCurrTrackCond->GetNSigmaRoadY(lr)*fCurrTrackCond->GetNSigmaRoadY(lr); // RS TOOPTIMIZE
   if (dy2>tol2) {                          // the clusters are sorted in Z(col) then in Y(row). 
-    if (goodCl&&goodSeed && AliDebugLevelClass()>2) {    
+    //
+#ifdef _ITSU_DEBUG_
+    if (AliDebugLevelClass()>2 && goodCl &&  !track->ContainsFake()) {    
       AliDebug(2,Form("Lost good cl: dy2=%e > tol2=%e |ESDtrack#%d (MClb:%d)",dy2,tol2,fCurrESDtrack->GetID(),fCurrESDtrMClb)); 
       track->Print("etp");
       cl->Print();
       PrintSeedClusters(track);
     }
+#endif
     // clusters are sorted in increasing Y and the loop goes from last (highers Y) to 1st.
     // if the track has too large y for this cluster (dy<0) it will be so for all other clusters of the sensor
     if (dy<0) return kStopSearchOnSensor;  // No chance that other cluster of this sensor will match (all Y's will be even larger)
@@ -970,12 +1051,14 @@ Int_t AliITSUTrackerGlo::CheckCluster(AliITSUSeed* track, Int_t lr, Int_t clID)
   tol2 = (track->GetSigmaZ2() + AliITSUReconstructor::GetRecoParam()->GetSigmaZ2(lr))*
     fCurrTrackCond->GetNSigmaRoadZ(lr)*fCurrTrackCond->GetNSigmaRoadZ(lr); // RS TOOPTIMIZE
   if (dz2>tol2) {
-    if (goodCl&&goodSeed && AliDebugLevelClass()>2) {
+#ifdef _ITSU_DEBUG_
+    if (AliDebugLevelClass()>2 && goodCl &&  !track->ContainsFake()) {
       AliDebug(2,Form("Lost good cl on L:%d : dz2=%e > tol2=%e |ESDtrack#%d (MClb:%d)",lr,dz2,tol2,fCurrESDtrack->GetID(),fCurrESDtrMClb)); 
       track->Print("etp");
       cl->Print();
       PrintSeedClusters(track);
     }
+#endif
     return kClusterNotMatching; // Other clusters may match
   }
   //
@@ -984,49 +1067,58 @@ Int_t AliITSUTrackerGlo::CheckCluster(AliITSUSeed* track, Int_t lr, Int_t clID)
   Double_t cov[3]={cl->GetSigmaY2(), cl->GetSigmaYZ(), cl->GetSigmaZ2()};
   double chi2 = track->GetPredictedChi2(p,cov);
   //
-#ifdef  _FILL_CONTROL_HISTOS_
-  if (htrPt>0) {
-    ((TH2*)fCHistoArr->At(kHChi2Cl+hcOffs))->Fill(htrPt,chi2);
-  }
-#endif
-  //
   if (chi2>fCurrTrackCond->GetMaxTr2ClChi2(lr)) {
-    if (goodCl&&goodSeed && AliDebugLevelClass()>2) {
+#ifdef _ITSU_DEBUG_
+    if (AliDebugLevelClass()>2 && goodCl &&  !track->ContainsFake()) {
       AliDebug(2,Form("Lost good cl on L:%d : Chi2=%e > Chi2Max=%e |dy: %+.3e dz: %+.3e |ESDtrack#%d (MClb:%d)\n",
                      lr,chi2,fCurrTrackCond->GetMaxTr2ClChi2(lr),dy,dz,fCurrESDtrack->GetID(),fCurrESDtrMClb)); 
       track->Print("etp");
       cl->Print("");
       PrintSeedClusters(track);
     }
+#endif
     return kClusterNotMatching;
   }
   //
   track = NewSeedFromPool(track);  // input track will be reused, use its clone for updates
   if (!track->Update()) {
-    if (goodCl&&goodSeed && AliDebugLevelClass()>2) {
+#ifdef _ITSU_DEBUG_
+    if (AliDebugLevelClass()>2 && goodCl &&  !track->ContainsFake()) {
       AliDebug(2,Form("Lost good cluster on L:%d : failed to update",lr));
       track->Print("etp");
       cl->Print("");
       PrintSeedClusters(track);
     }
+#endif
     MarkSeedFree(track);
     return kClusterNotMatching;
   }
   track->SetChi2Cl(chi2);
   track->SetLrClusterID(lr,clID);
+  //
+  if (track->GetChi2GloNrm()>fCurrTrackCond->GetMaxChi2GloNrm(lr)) {
+#ifdef _ITSU_DEBUG_
+    if (AliDebugLevelClass()>2 && goodCl &&  !track->ContainsFake()) {
+      AliDebug(2,Form("Lost good cl on L:%d : Chi2Glo=%e > Chi2Max=%e |dy: %+.3e dz: %+.3e |ESDtrack#%d (MClb:%d)\n",
+                     lr,track->GetChi2GloNrm(),fCurrTrackCond->GetMaxChi2GloNrm(lr),dy,dz,fCurrESDtrack->GetID(),fCurrESDtrMClb)); 
+      track->Print("etp");
+      cl->Print("");
+      PrintSeedClusters(track);
+    }
+#endif
+    MarkSeedFree(track);
+    return kClusterNotMatching;
+  }
   //  cl->IncreaseClusterUsage(); // do this only for winners
   //
   track->SetFake(!goodCl);
   //
+#ifdef _ITSU_DEBUG_
   AliDebug(2,Form("AddCl(%d) Cl%d lr:%d: dY:%+8.4f dZ:%+8.4f (MC: %5d %5d %5d) |Chi2=%f(%c)| ",
               goodCl,clID,lr,dy,dz2,cl->GetLabel(0),cl->GetLabel(1),cl->GetLabel(2), chi2, track->IsFake() ? '-':'+'));
+#endif
   //
   AddSeedBranch(track);
-#ifdef  _FILL_CONTROL_HISTOS_
-  if (htrPt>0) {
-    ((TH2*)fCHistoArr->At(kHChi2Nrm+hcOffs))->Fill(htrPt,track->GetChi2GloNrm());
-  }
-#endif
   //
   return kClusterMatching;
 }
@@ -1039,7 +1131,7 @@ Bool_t AliITSUTrackerGlo::NeedToKill(AliITSUSeed *seed, Int_t flag)
   if (flag==kMissingCluster) {
     int lastChecked = seed->GetLayerID();
     UShort_t patt = seed->GetHitsPattern();
-    if (lastChecked) patt |= ~(kMask<<lastChecked); // not all layers were checked, complete unchecked ones by potential hits
+    if (lastChecked) patt |= (~(kMask<<lastChecked))&fCurrTrackCond->GetAllowedLayers(); // not all layers were checked, complete unchecked ones by potential hits
     Bool_t seedOK = fCurrTrackCond->CheckPattern(patt);
     return !seedOK;
   }
@@ -1053,7 +1145,7 @@ Bool_t AliITSUTrackerGlo::PropagateSeed(AliITSUSeed *seed, Double_t xToGo, Doubl
   const Double_t kEpsilon = 1e-5;
   Double_t xpos     = seed->GetX();
   Int_t dir         = (xpos<xToGo) ? 1:-1;
-  Double_t xyz0[3],xyz1[3],param[7];
+  Double_t xyz0[3],xyz1[3];
   //
   Bool_t updTime = dir>0 && seed->IsStartedTimeIntegral();
   if (matCorr || updTime) seed->GetXYZ(xyz1);   //starting global position
@@ -1069,11 +1161,10 @@ Bool_t AliITSUTrackerGlo::PropagateSeed(AliITSUSeed *seed, Double_t xToGo, Doubl
       xyz0[2]=xyz1[2];
       seed->GetXYZ(xyz1);    //  // global pos at the end of step
       if (matCorr) {
-       MeanMaterialBudget(xyz0,xyz1,param);    
-       Double_t xrho=param[0]*param[4], xx0=param[1];
+       Double_t xrho,xx0;
+       ds = GetMaterialBudget(xyz0,xyz1,xx0,xrho);     
        if (dir>0) xrho = -xrho; // outward should be negative
        if (!seed->ApplyMaterialCorrection(xx0,xrho,mass,kFALSE)) {AliInfo("Fail2"); seed->Print("etp"); return kFALSE;}
-       ds = param[4];
       }
        else { // matCorr is not requested but time integral is
        double d0 = xyz1[0]-xyz0[0];
@@ -1095,12 +1186,14 @@ Bool_t AliITSUTrackerGlo::PropagateSeed(AliExternalTrackParam *seed, Double_t xT
   const Double_t kEpsilon = 1e-5;
   Double_t xpos     = seed->GetX();
   Int_t dir         = (xpos<xToGo) ? 1:-1;
-  Double_t xyz0[3],xyz1[3],param[7];
+  Double_t xyz0[3],xyz1[3];
   //
+#ifdef _ITSU_DEBUG_
   if (AliDebugLevelClass()>1) {
     AliDebug(2,Form("Before Propagate to X=%f with M=%.3f MaxStep=%.4f MatCorr=%d",xToGo,mass,maxStep,matCorr));
     seed->AliExternalTrackParam::Print();
   }
+#endif
   Bool_t updTime = dir>0 && seed->IsStartedTimeIntegral();
   if (matCorr || updTime) seed->GetXYZ(xyz1);   //starting global position
   while ( (xToGo-xpos)*dir > kEpsilon){
@@ -1116,11 +1209,10 @@ Bool_t AliITSUTrackerGlo::PropagateSeed(AliExternalTrackParam *seed, Double_t xT
       seed->GetXYZ(xyz1);    //  // global pos at the end of step
       //
       if (matCorr) {
-       MeanMaterialBudget(xyz0,xyz1,param);    
-       Double_t xrho=param[0]*param[4], xx0=param[1];
+       Double_t xrho,xx0;
+       ds = GetMaterialBudget(xyz0,xyz1,xx0,xrho);     
        if (dir>0) xrho = -xrho; // outward should be negative
        if (!seed->CorrectForMeanMaterial(xx0,xrho,mass)) return kFALSE;
-       ds = param[4];
       }
       else { // matCorr is not requested but time integral is
        double d0 = xyz1[0]-xyz0[0];
@@ -1133,10 +1225,12 @@ Bool_t AliITSUTrackerGlo::PropagateSeed(AliExternalTrackParam *seed, Double_t xT
     //
     xpos = seed->GetX();
   }
+#ifdef _ITSU_DEBUG_
   if (AliDebugLevelClass()>1) {
     AliDebug(2,Form("After Propagate to X=%f",xToGo));
     seed->AliExternalTrackParam::Print();
   }
+#endif
   return kTRUE;
 }
 
@@ -1148,31 +1242,40 @@ Bool_t AliITSUTrackerGlo::FinalizeHypothesis(AliITSUTrackHyp* hyp)
   AliITSUSeed* winner = 0;
   fCurrHyp = hyp;
   fCurrMass = hyp->GetMass();
-  if (!(winner=hyp->DefineWinner())) return kFALSE;
-  FlagSeedClusters(winner,kTRUE);
+  if (!(winner=hyp->DefineWinner(fCurrTrackCond->GetActiveLrInner()))) return kFALSE;
   CookMCLabel(hyp);
   //
-#ifdef  _FILL_CONTROL_HISTOS_
-  if (fCHistoArr) {
-    TH2* hchimt = 0;
-    TH2* hchiSA = 0;
-    int lb = Abs(hyp->GetESDTrack()->GetTPCLabel());
-    if ( hyp->GetITSLabel()==lb ) {
-      hchimt = (TH2*)fCHistoArr->At(kHChiMatchCorr);
-      hchiSA = (TH2*)fCHistoArr->At(kHChiITSSACorr);
-    }
-    else if ( Abs(hyp->GetITSLabel()) == lb ) hchimt = (TH2*)fCHistoArr->At(kHChiMatchFake);
-    else if ( hyp->GetITSLabel()>=0 ) hchimt = (TH2*)fCHistoArr->At(kHChiMatchCorrMiss);
-    else hchimt = (TH2*)fCHistoArr->At(kHChiMatchFakeMiss);
-    if (!hchiSA) hchiSA =  (TH2*)fCHistoArr->At(kHChiITSSAFake);
-    //    printf("MTStatus: ITS:%+5d TPC:%+5d chimt:%e chi2SA:%e-> %s\n",hyp->GetITSLabel(),lb,winner->GetChi2ITSTPC(),winner->GetChi2ITSSA(),hchimt->GetName());
-    if (hchimt) hchimt->Fill(hyp->GetTPCSeed()->Pt(),winner->GetChi2ITSTPC());
-    if (hchiSA) hchiSA->Fill(hyp->GetTPCSeed()->Pt(),winner->GetChi2ITSSA());      
+#ifdef  _ITSU_TUNING_MODE_  // fill tuning histos
+  TObjArray* dest = hyp->GetLabel()>=0 ? fCHistoArrCorr : fCHistoArrFake;
+  if (dest) {
+    AliITSUSeed* sd = winner;
+    double htrPt = hyp->Pt();
+    do {
+      int clID,lrID;
+      if ( (clID=sd->GetLrCluster(lrID))<0 ) continue;
+      ((TH2*)dest->At( GetHistoID(lrID,kHChi2Nrm     ,fCurrPassID) ))->Fill(htrPt,sd->GetChi2GloNrm());
+      ((TH2*)dest->At( GetHistoID(lrID,kHBestInBranch,fCurrPassID) ))->Fill(htrPt,sd->GetOrdBranch());
+      ((TH2*)dest->At( GetHistoID(lrID,kHBestInCand  ,fCurrPassID) ))->Fill(htrPt,sd->GetOrdCand());
+      //
+      if (dest==fCHistoArrFake && !sd->IsFake()) continue; // for the fake seeds fill only fake clusters part
+      //
+      ((TH2*)dest->At( GetHistoID(lrID,kHResY        ,fCurrPassID) ))->Fill(htrPt,sd->GetResidY());
+      ((TH2*)dest->At( GetHistoID(lrID,kHResZ        ,fCurrPassID) ))->Fill(htrPt,sd->GetResidZ());
+      ((TH2*)dest->At( GetHistoID(lrID,kHResYP       ,fCurrPassID) ))->Fill(htrPt,sd->GetPullY());
+      ((TH2*)dest->At( GetHistoID(lrID,kHResZP       ,fCurrPassID) ))->Fill(htrPt,sd->GetPullZ());
+      ((TH2*)dest->At( GetHistoID(lrID,kHChi2Cl      ,fCurrPassID) ))->Fill(htrPt,sd->GetChi2Cl());
+      //
+    } while((sd=(AliITSUSeed*)sd->GetParent()));
+    //
+    ((TH2*)dest->At( GetHistoID(-1,kHChiMatch,fCurrPassID) ))->Fill(htrPt,winner->GetChi2ITSTPC());
+    ((TH2*)dest->At( GetHistoID(-1,kHChiITSSA,fCurrPassID) ))->Fill(htrPt,winner->GetChi2ITSSA());
   }
-#endif    
   //
-  UpdateESDTrack(hyp,AliESDtrack::kITSin);
-  return kTRUE;
+#endif
+  //
+  CheckClusterSharingConflicts(hyp);
+  return hyp->GetWinner();  // winner might change of disappear after resolving conflicts
+  //
 }
 
 //______________________________________________________________________________
@@ -1180,102 +1283,149 @@ void AliITSUTrackerGlo::FinalizeHypotheses()
 {
   // select winner for each hypothesis, remove cl. sharing conflicts
   AliCodeTimerAuto("",0); 
-  AliDebug(2,"TODO");
   //
-  int nh = fHypStore.GetEntriesFast();
-  for (int ih=0;ih<nh;ih++) {
-    if (FinalizeHypothesis( GetTrackHyp(ih) )) fCountITSin++;
-  }
+  int* esdTrackIndex = fESDIndex.GetArray();
+  for (int ih=0;ih<fNTracksESD;ih++) FinalizeHypothesis(GetTrackHyp(esdTrackIndex[ih])); // finlize in decreasing pt order
   //
-  AliITSUSeed* winner = 0;
+  for (int ih=fNTracksESD;ih--;) UpdateESDTrack(GetTrackHyp(esdTrackIndex[ih]),AliESDtrack::kITSin); 
+  //
+}
 
-#ifdef  _FILL_CONTROL_HISTOS_
-  // if requested, collect cluster sharing statistics
-  TH2* hShare = 0;
-  if (fCHistoArr && (hShare=(TH2*)fCHistoArr->At(kHClShare))) {
-    for (int ih=0;ih<nh;ih++) {
-      AliITSUTrackHyp* hyp = GetTrackHyp(ih);
-      if (!hyp || !(winner=hyp->GetWinner())) continue;
-      int lrID = 0;
-      double pt = hyp->Pt();
+//______________________________________________________________________________
+void AliITSUTrackerGlo::CheckClusterSharingConflicts(AliITSUTrackHyp* hyp)
+{
+  // remove eventual cluster sharing conflicts
+  AliITSUSeed* winner = 0;
+  if (!(winner=hyp->GetWinner())) return;
+  UShort_t idH = (UShort_t)hyp->GetUniqueID()+1;
+  int lrID,clID;
+  int inLr = fCurrTrackCond->GetActiveLrInner();
+  AliITSUSeed* winSD=winner;
+  do {
+    if ( (clID=winSD->GetLrCluster(lrID))<0 ) continue;
+    AliITSUClusterPix* cl = (AliITSUClusterPix*)fITS->GetLayerActive(lrID)->GetCluster(clID);
+    Int_t refID = cl->GetRecoInfo();                // was it already referred by some track (with refID-1 if >0)
+    if (!refID) {cl->SetRecoInfo(idH); continue;}   // if not, refer from track to cluster
+    if (refID==idH) continue;                          // ignore reference to itself
+    // the cluster is already used by other track, need to resolve the conflict
+    AliITSUTrackHyp* hypC = GetTrackHyp(refID-1);   // competitor
+    //    printf("Ref to %d (%p) from %d Cl %d %d\n",refID-1,hypC, idH-1,clID,lrID);
+    AliITSUSeed* winnerC = hypC->GetWinner();
+    if (!winnerC) {
+      printf("Missing winner, ERROR\n");
+      continue;
+    }
+    //
+#ifdef _ITSU_DEBUG_
+    if (AliDebugLevelClass()>1) { //------------------------ DEBUG -------------------
+      AliInfo(Form("Shared cl#%4d on Lr:%d: Hyp%3d/Q:%6.3f vs Hyp%3d/Q:%6.3f",
+                  clID,lrID,idH-1,winner->GetQualityVar(),refID-1,winnerC->GetQualityVar()));
+      // dump winner of hyp
+      printf("#%4d Pt:%.3f Chi:%6.3f/%6.3f/%6.3f Ncl:%d MCits%+5d MCtpc:%+5d |",
+            idH-1,winner->Pt(),winner->GetChi2GloNrm(),winner->GetChi2ITSTPC(),winner->GetChi2ITSSA(),
+            winner->GetNLayersHit(),hyp->GetITSLabel(),hyp->GetESDTrack()->GetTPCLabel());
+      int prevL=-1;
+      AliITSUSeed* sd = winner;
       do {
-       int clID = winner->GetLrCluster(lrID);
-       if (clID<0) continue;
-       AliITSUClusterPix* cl = (AliITSUClusterPix*)fITS->GetLayerActive(lrID)->GetCluster(clID);
-       if (!cl->IsClusterShared()) continue;
-       hShare->Fill(pt,winner->IsFake() ? lrID+fNLrActive : lrID);
-      } while ((winner=(AliITSUSeed*)winner->GetParent()));
-    }  
-  }
-#endif  
-
-  AliRefArray** refArr = new AliRefArray*[fNLrActive];
-  for (int ilr=0;ilr<fNLrActive;ilr++) refArr[ilr] = new AliRefArray(1000);
-  for (int ih=0;ih<nh;ih++) {
-    AliITSUTrackHyp* hyp = GetTrackHyp(ih);
-    if (!hyp || !(winner=hyp->GetWinner())) continue;
-    int lrID = 0;
-    do {
-      int clID = winner->GetLrCluster(lrID);
-      if (clID<0) continue;
-      AliITSUClusterPix* cl = (AliITSUClusterPix*)fITS->GetLayerActive(lrID)->GetCluster(clID);
-      if (!cl->IsClusterShared()) continue;
-      refArr[lrID]->AddReference(clID,ih);
-    } while ((winner=(AliITSUSeed*)winner->GetParent()));
-  }    
-  //  /*
-  UInt_t refs[100];
-  for (int ilr=0;ilr<fNLrActive;ilr++) {
-    int ncl = fITS->GetLayerActive(ilr)->GetNClusters();
-    printf("\nClusterSharingDump: Lr %d (%d cl)\n",ilr,ncl);
-    int cnt = 0;
-    for (int icl=0;icl<ncl;icl++) {
-      if (!refArr[ilr]->HasReference(icl)) continue;
-      int nref = refArr[ilr]->GetReferences(icl,refs,100);
-      printf("--- cl%3d(#%d): NShare=%4d\n",cnt++,icl,nref);     
-      for (int ir=0;ir<nref;ir++) {
-       AliITSUTrackHyp* hyp = GetTrackHyp(refs[ir]);
-       winner = hyp->GetWinner();
-       AliESDtrack* esdTr = hyp->GetESDTrack();
-       printf("#%4d Pt:%.3f Chi:%6.2f Ncl:%d MCits%+5d MCtpc:%+5d ESD:%4d |",
-              refs[ir],winner->Pt(),winner->GetChi2GloNrm(),winner->GetNLayersHit(),
-              hyp->GetITSLabel(),esdTr->GetTPCLabel(),esdTr->GetID());
-       int prevL=-1;
-       do {
-         int lrs;
-         int clID = winner->GetLrCluster(lrs);
-         if (clID<0) continue;
-         while( lrs>++prevL ) printf("%4s        ","----");
-         printf("%4d (%5.1f)",clID,winner->GetChi2Cl());
-       } while ((winner=(AliITSUSeed*)winner->GetParent()));
-       printf("|\n");
+       int lrs;
+       int clIDt = sd->GetLrCluster(lrs);
+       if (clIDt<0) continue;
+       while( lrs>++prevL ) printf("%4s        ","----");
+       printf("%4d (%5.1f)",clIDt,sd->GetChi2Cl());
+      } while ((sd=(AliITSUSeed*)sd->GetParent()));
+      printf("|\n");
+      // dump winner of hypC
+      printf("#%4d Pt:%.3f Chi:%6.3f/%6.3f/%6.3f Ncl:%d MCits%+5d MCtpc:%+5d |",
+            refID-1,winnerC->Pt(),winnerC->GetChi2GloNrm(),winnerC->GetChi2ITSTPC(),winnerC->GetChi2ITSSA(),
+            winnerC->GetNLayersHit(),hypC->GetITSLabel(),hypC->GetESDTrack()->GetTPCLabel());
+      prevL=-1;
+      sd = winnerC;
+      do {
+       int lrs;
+       int clIDt = sd->GetLrCluster(lrs);
+       if (clIDt<0) continue;
+       while( lrs>++prevL ) printf("%4s        ","----");
+       printf("%4d (%5.1f)",clIDt,sd->GetChi2Cl());
+      } while ((sd=(AliITSUSeed*)sd->GetParent()));
+      printf("|\n");
+    }  //-------------------------------------------------- DEBUG -------------------
+#endif
+    //
+    if (winner->GetQualityVar()<winnerC->GetQualityVar()) { // current tracks is better then competitor track
+      FlagSeedClusters(winnerC,kFALSE,refID); // unflag cluster usage by loser
+      cl->SetRecoInfo(idH);
+      //
+      //if ( hypC->GetLabel()>=0) AliInfo(Form("KILLING CORRECT: %d",refID-1));
+      winnerC->Kill();
+      if (hypC->DefineWinner(inLr)) {             // find new winner instead of suppressed candidate
+       CookMCLabel(hypC);
+       CheckClusterSharingConflicts(hypC);   // and check its sharing conflicts again
+       if (winner->IsKilled()) break;        // the current winner might have been killed during check of new winner of hypC!
+      }
+    }    
+    else { // competitor hypC is better than the hyp
+      FlagSeedClusters(winner,kFALSE,idH); // unflag cluster usage by loser
+      winner->Kill();
+      //if ( hyp->GetLabel()>=0) AliInfo(Form("KILLING CORRECT: %d",idH-1));
+      if (hyp->DefineWinner(inLr)) {
+       CookMCLabel(hyp);
+       CheckClusterSharingConflicts(hyp);
       }
+      break;
     }
-  }
-  //  */
-  delete[] refArr;
-
+    //   
+  } while ((winSD=(AliITSUSeed*)winSD->GetParent()));
+  //
+  return;
 }
 
 //______________________________________________________________________________
 void AliITSUTrackerGlo::UpdateESDTrack(AliITSUTrackHyp* hyp,Int_t flag)
 {
   // update ESD track with current best hypothesis
+  if (!hyp) return;
   AliESDtrack* esdTr = hyp->GetESDTrack();
-  if (!esdTr) return;
+  if (!esdTr || esdTr->IsOn(flag)) return;
   AliITSUSeed* win = hyp->GetWinner();
-  if (!win) return;
+  if (!win || win->IsKilled()) return;
   double chiSav;
   //
   switch (flag) {
   case AliESDtrack::kITSin: 
     esdTr->UpdateTrackParams(hyp,flag); // update kinematics
-    // TODO: set cluster info
+    fCountITSin++;
+    // set fakes cluster info
+    {
+      UShort_t clfake = 0;
+      Int_t    clSplit = 0;
+      AliITSUSeed* sd = win;
+      int ip=0;
+      do {
+       int lr, clID = sd->GetLrCluster(lr);
+       if (sd->IsFake()) clfake |= 0x1<<lr;
+       if (clID>=0) {
+         esdTr->SetITSModuleIndex(ip++, sd->GetLrClusterID());
+         AliITSUClusterPix *cl = (AliITSUClusterPix*)fITS->GetLayerActive(lr)->GetCluster(clID);
+#ifdef  _ITSU_TUNING_MODE_
+         if (cl->IsSplit()) clSplit |= 0x1<<lr;
+#endif
+       }
+      } while ((sd=(AliITSUSeed*)sd->GetParent()));
+      //
+      // RS: Temporary set special flag for tracks from the afterburner
+      if (fCurrPassID>0) clfake |= 0x1<<7;
+      //
+      esdTr->SetITSSharedMap(clfake);
+      esdTr->SetITSModuleIndex(10,clSplit);
+    }
+    // TEMPORARY: store iteration id
+    esdTr->SetITSModuleIndex(11,fCurrPassID);
     break;
     //
   case AliESDtrack::kITSout: 
     // here the stored chi2 will correspond to backward ITS-SA fit
     esdTr->UpdateTrackParams(hyp,flag); // update kinematics
+    fCountITSout++;
     // TODO: avoid setting friend
     break;
     //
@@ -1286,6 +1436,7 @@ void AliITSUTrackerGlo::UpdateESDTrack(AliITSUTrackHyp* hyp,Int_t flag)
     hyp->SetChi2(win->GetChi2ITSTPC());
     esdTr->UpdateTrackParams(hyp,flag); // update kinematics
     hyp->SetChi2(chiSav);
+    fCountITSrefit++;
     // TODO: avoid setting cluster info
     break;
   default:
@@ -1293,12 +1444,12 @@ void AliITSUTrackerGlo::UpdateESDTrack(AliITSUTrackHyp* hyp,Int_t flag)
   }
   //
   esdTr->SetITSLabel(hyp->GetITSLabel());
-  // transfer module indices
+  // transfer chip indices
   // TODO
 }
 
 //______________________________________________________________________________
-Double_t AliITSUTrackerGlo::RefitTrack(AliExternalTrackParam* trc, Double_t rDest, Int_t stopCond)
+Double_t AliITSUTrackerGlo::RefitTrack(AliITSUTrackHyp* trc, Double_t rDest, Int_t &nclFit, Int_t stopCond)
 {
   // refit track till radius rDest. The cluster,mass info is taken from fCurrHyp (and its winner seed)
   // if stopCond<0 : propagate till last cluster then stop
@@ -1312,20 +1463,23 @@ Double_t AliITSUTrackerGlo::RefitTrack(AliExternalTrackParam* trc, Double_t rDes
   lrStart = fITS->FindFirstLayerID(rCurr,dir);
   lrStop  = fITS->FindLastLayerID(rDest,dir); // lr id before which we have to stop
   //
+#ifdef _ITSU_DEBUG_
   if (AliDebugLevelClass()>2) {
     printf("Refit %d: Lr:%d (%f) -> Lr:%d (%f)\n",dir,lrStart,rCurr, lrStop,rDest);
     printf("Before refit: "); trc->Print();
   }
+#endif
   if (lrStop<0 || lrStart<0) AliFatal(Form("Failed to find start(%d) or last(%d) layers. Track from %.3f to %.3f",lrStart,lrStop,rCurr,rDest));
   //
   Int_t clInfo[2*AliITSUAux::kMaxLayers];
   Int_t nCl = fCurrHyp->FetchClusterInfo(clInfo);
   fCurrMass = fCurrHyp->GetMass();
-  AliExternalTrackParam tmpTr(*trc);
+  AliITSUTrackHyp tmpTr(*(AliKalmanTrack*)trc);
   double chi2 = 0;
-  int iclLr[2],nclLr,clCount=0;
+  int iclLr[2],nclLr;
+  nclFit = 0;
   //
-  int lrStop1 = lrStop+1;
+  int lrStop1 = lrStop+dir;
   for (int ilr=lrStart;ilr!=lrStop1;ilr+=dir) {
     AliITSURecoLayer* lr = fITS->GetLayer(ilr);
     if ( dir*(rCurr-lr->GetR(dir))>0) continue; // this layer is already passed
@@ -1349,29 +1503,37 @@ Double_t AliITSUTrackerGlo::RefitTrack(AliExternalTrackParam* trc, Double_t rDes
       AliITSUClusterPix* clus =  (AliITSUClusterPix*)lr->GetCluster(iclLr[icl]);
       AliITSURecoSens* sens = lr->GetSensorFromID(clus->GetVolumeId());
       if (!tmpTr.Rotate(sens->GetPhiTF())) {
+#ifdef _ITSU_DEBUG_
        AliESDtrack* trESD = fCurrHyp->GetESDTrack();
        AliDebug(2,Form("Failed on rotate to %f | ESDtrack#%d (MClb:%d)",sens->GetPhiTF(),trESD->GetID(),trESD->GetTPCLabel()));
+#endif
        return -1;
       }
       //
       double xClus = sens->GetXTF()+clus->GetX();
       if (!transportedToLayer) {
        if (ilr!=lrStart && !TransportToLayerX(&tmpTr,lrStart,ilr,xClus)) {
+#ifdef _ITSU_DEBUG_
          AliESDtrack* trESD = fCurrHyp->GetESDTrack();
          AliDebug(2,Form("Failed to transport %d -> %d | ESDtrack#%d (MClb:%d)\n",lrStart,ilr,trESD->GetID(),trESD->GetTPCLabel()));
+#endif
          return -1; // go to the entrance to the layer
        }
        lrStart = ilr;
        transportedToLayer = kTRUE;
       }
       //
+#ifdef _ITSU_DEBUG_
       if (AliDebugLevelClass()>1) {
        AliDebug(2,Form("Propagate to cl:%d on lr %d Need to go %.4f -> %.4f",icl,ilrA,tmpTr.GetX(),xClus));
       }
+#endif
       //
       if (!PropagateSeed(&tmpTr,xClus,fCurrMass)) {
+#ifdef _ITSU_DEBUG_
        AliESDtrack* trESD = fCurrHyp->GetESDTrack();
        AliDebug(2,Form("Failed on propagate to %f (dir=%d) | ESDtrack#%d (MClb:%d)",xClus,dir,trESD->GetID(),trESD->GetTPCLabel()));
+#endif
        return -1;
       }
       //
@@ -1380,33 +1542,37 @@ Double_t AliITSUTrackerGlo::RefitTrack(AliExternalTrackParam* trc, Double_t rDes
       double chi2cl = tmpTr.GetPredictedChi2(p,cov);
       chi2 += chi2cl;
       //
-#ifdef  _FILL_CONTROL_HISTOS_
-      int hcOffs = (1+fTrackPhase)*kHistosPhase + ilrA;
-      double htrPt=-1;
-      if (fCHistoArr && fTrackPhase>kClus2Tracks && trc->GetLabel()>=0/* && trc->Charge()>0*/) {
-       htrPt = tmpTr.Pt();
+#ifdef  _ITSU_TUNING_MODE_
+      TObjArray* dest = trc->GetLabel()>=0 ? fCHistoArrCorr : fCHistoArrFake;
+      if (dest && fTrackPhaseID>kClus2Tracks) {
+       //
+       double htrPt = tmpTr.Pt();
        double dy = p[0]-tmpTr.GetY();
        double dz = p[1]-tmpTr.GetZ();
-       ((TH2*)fCHistoArr->At(kHResY+hcOffs))->Fill(htrPt,dy);
-       ((TH2*)fCHistoArr->At(kHResZ+hcOffs))->Fill(htrPt,dz);
+       ((TH2*)dest->At( GetHistoID(ilrA,kHResY,0,fTrackPhaseID) ))->Fill(htrPt,dy);
+       ((TH2*)dest->At( GetHistoID(ilrA,kHResZ,0,fTrackPhaseID) ))->Fill(htrPt,dz);
        double errY = tmpTr.GetSigmaY2();
        double errZ = tmpTr.GetSigmaZ2();
-       if (errY>0) ((TH2*)fCHistoArr->At(kHResYP+hcOffs))->Fill(htrPt,dy/Sqrt(errY));
-       if (errZ>0) ((TH2*)fCHistoArr->At(kHResZP+hcOffs))->Fill(htrPt,dz/Sqrt(errZ));
-       ((TH2*)fCHistoArr->At(kHChi2Cl+hcOffs))->Fill(htrPt,chi2cl);
+       if (errY>0) ((TH2*)dest->At( GetHistoID(ilrA,kHResYP,0,fTrackPhaseID) ))->Fill(htrPt,dy/Sqrt(errY));
+       if (errZ>0) ((TH2*)dest->At( GetHistoID(ilrA,kHResZP,0,fTrackPhaseID) ))->Fill(htrPt,dz/Sqrt(errZ));
+       ((TH2*)dest->At( GetHistoID(ilrA,kHChi2Cl,0,fTrackPhaseID) ))->Fill(htrPt,chi2cl);
       }
 #endif  
       //      
       if ( !tmpTr.Update(p,cov) ) {
+#ifdef _ITSU_DEBUG_
        AliESDtrack* trESD = fCurrHyp->GetESDTrack();
        AliDebug(2,Form("Failed on Update | ESDtrack#%d (MClb:%d)",trESD->GetID(),trESD->GetTPCLabel()));
+#endif
        return -1;
       }
+#ifdef _ITSU_DEBUG_
       if (AliDebugLevelClass()>1) {
        printf("AfterRefit: "); tmpTr.Print();
       }
-      if (++clCount==nCl && stopCond<0) {
-       *trc = tmpTr;
+#endif
+      if (++nclFit==nCl && stopCond<0) {
+       *trc = (AliKalmanTrack&)tmpTr;
        return chi2; // it was requested to not propagate after last update
       }
     }
@@ -1417,22 +1583,28 @@ Double_t AliITSUTrackerGlo::RefitTrack(AliExternalTrackParam* trc, Double_t rDes
   //
   if (lrStart!=lrStop) {
     if (!TransportToLayer(&tmpTr,lrStart,lrStop)) {
-      AliDebug(-1,Form("Failed on TransportToLayer %d->%d | ESDtrack#%d (MClb:%d), X=%f",lrStart,lrStop,fCurrESDtrack->GetID(),fCurrESDtrMClb,tmpTr.GetX()));
-      if (stopCond>0) return -chi2; // rDest was obligatory
+#ifdef _ITSU_DEBUG_
+      AliDebug(1,Form("Failed on TransportToLayer %d->%d | ESDtrack#%d (MClb:%d), X=%f",lrStart,lrStop,fCurrESDtrack->GetID(),fCurrESDtrMClb,tmpTr.GetX()));
+#endif
+      return (stopCond>0) ? -chi2 : chi2; // rDest was obligatory
     }    
     if (!GoToExitFromLayer(&tmpTr,fITS->GetLayer(lrStop),dir)) {
-      AliDebug(-1,Form("Failed on GoToExitFromLayer %d | ESDtrack#%d (MClb:%d), X=%f",lrStop,fCurrESDtrack->GetID(),fCurrESDtrMClb,tmpTr.GetX()));
-      if (stopCond>0) return -chi2; // rDest was obligatory
+#ifdef _ITSU_DEBUG_
+      AliDebug(1,Form("Failed on GoToExitFromLayer %d | ESDtrack#%d (MClb:%d), X=%f",lrStop,fCurrESDtrack->GetID(),fCurrESDtrMClb,tmpTr.GetX()));
+#endif
+      return (stopCond>0) ? -chi2 : chi2; // rDest was obligatory
     }
   }
   // go to the destination radius. Note that here we don't select direction to avoid precision problems
   if (!tmpTr.GetXatLabR(rDest,rDest,GetBz(),0) || !PropagateSeed(&tmpTr,rDest,fCurrMass, 100, kFALSE)) {
-    if (stopCond>0) return -chi2; // rDest was obligatory
+    return (stopCond>0) ? -chi2 : chi2; // rDest was obligatory
   }
-  *trc=tmpTr;
+  *trc = (AliKalmanTrack&)tmpTr;
+#ifdef _ITSU_DEBUG_
   if (AliDebugLevelClass()>2) {
     printf("After refit (now at lr %d): ",lrStop); trc->Print();
   }
+#endif
   return chi2;
 }
 
@@ -1522,30 +1694,12 @@ void AliITSUTrackerGlo::ValidateAllowedBranches(Int_t acceptMax)
   //  if (nb<fNBranchesAdded) printf("ValidateAllowedBranches: %d of %d (%d) on lr %d\n",nb,fNBranchesAdded,fNCandidatesAdded,ilr);
   // disable unused branches
   AliITSUSeed** branches = &fLayerCandidates[fNCandidatesAdded];
-#ifdef  _FILL_CONTROL_HISTOS_
-  int bestID = -1;
-  for (int ib=0;ib<fNBranchesAdded;ib++) {
-    AliITSUSeed* sd = branches[ib];
-    if (!sd->ContainsFake() && (bestID<0 || sd->Compare(branches[bestID])<0) ) bestID = ib;
-  }
-  if (bestID>=0) {
-    TH2* hb = (TH2*)fCHistoArr->At(kHBestInBranch + (1+fTrackPhase)*kHistosPhase + fCurrActLrID);
-    if (hb) hb->Fill(branches[bestID]->Pt(), bestID);
-  }
+  //
+#ifdef  _ITSU_TUNING_MODE_
+  for (int ib=0;ib<fNBranchesAdded;ib++) branches[ib]->SetOrdBranch(ib);
 #endif
   //
-  for (int ib=nb;ib<fNBranchesAdded;ib++) {
-    //
-#ifdef  _FILL_CONTROL_HISTOS_    
-    if (AliDebugLevelClass()>-2 && !branches[ib]->ContainsFake() /*&& branches[ib]->GetNLayersHit()*/
-       && (bestID<0 || branches[ib]->Compare(branches[bestID])<0 ) ) {
-      printf("Suppress good branch as %d of %d |",ib,fNBranchesAdded); branches[ib]->Print();
-      //      printf("Survivors : \n");
-      //      for (int j=0;j<nb;j++) branches[j]->Print();
-    }
-#endif    
-    MarkSeedFree(branches[ib]);
-  }
+  for (int ib=nb;ib<fNBranchesAdded;ib++) MarkSeedFree(branches[ib]);
   fNCandidatesAdded += nb; // update total candidates counter
   fNBranchesAdded = 0; // reset branches counter
   //
@@ -1571,20 +1725,8 @@ void AliITSUTrackerGlo::ValidateAllowedCandidates(Int_t ilr, Int_t acceptMax)
   for (int i=fNCandidatesAdded;i--;) chi2[i] = fLayerCandidates[i]->GetChi2GloNrm();
   Sort(fNCandidatesAdded,chi2,index,kFALSE);
   //
-#ifdef  _FILL_CONTROL_HISTOS_
-  int bestID = -1;
-  for (int ib=0;ib<fNCandidatesAdded;ib++) {
-    AliITSUSeed* sd = fLayerCandidates[index[ib]];
-    if (!sd->ContainsFake() && (bestID<0 || sd->Compare(fLayerCandidates[index[bestID]])<0) ) bestID = ib;
-  }
-  if (bestID>=0) {
-    TH2* hb = (TH2*)fCHistoArr->At(kHBestInCand + (1+fTrackPhase)*kHistosPhase + fCurrActLrID);
-    if (hb) hb->Fill(fLayerCandidates[index[bestID]]->Pt(), bestID);
-  }
-#endif
-  //
   int nacc=0,nb=0;
-  if (ilr>0) { // just take 1st acceptMax candidates
+  if (ilr>fCurrTrackCond->GetActiveLrInner()) { // just take 1st acceptMax candidates
     nb = Min(fNCandidatesAdded,acceptMax);
     for (int ib=0;ib<nb;ib++) AddProlongationHypothesis(fLayerCandidates[index[ib]],ilr);
   }
@@ -1601,18 +1743,8 @@ void AliITSUTrackerGlo::ValidateAllowedCandidates(Int_t ilr, Int_t acceptMax)
     }
     fCurrHyp->SetWinner(wn0); // restore original winner (NULL?)
   }
-  //
   // disable unused candidates
-  for (int ib=nb;ib<fNCandidatesAdded;ib++) {
-    //
-#ifdef  _FILL_CONTROL_HISTOS_
-    if (AliDebugLevelClass()>-2 && !fLayerCandidates[index[ib]]->ContainsFake() /*&& fLayerCandidates[index[ib]]->GetNLayersHit()*/
-       && (bestID<0 || fLayerCandidates[index[ib]]->Compare(fLayerCandidates[index[bestID]])<0 ) ) {
-      printf("Suppress good candidate as %d of %d |",index[ib],fNCandidatesAdded); fLayerCandidates[index[ib]]->Print();
-    }
-#endif
-    MarkSeedFree(fLayerCandidates[index[ib]]);    
-  }
+  for (int ib=nb;ib<fNCandidatesAdded;ib++) MarkSeedFree(fLayerCandidates[index[ib]]);    
   fNCandidatesAdded = 0; // reset candidates counter
   //
 }
@@ -1623,23 +1755,26 @@ Bool_t AliITSUTrackerGlo::CheckBackwardMatching(AliITSUSeed* seed)
   // check seed backward propagation chi2 and matching to TPC 
   double bz0 = GetBz();
   double rDest = fITS->GetRITSTPCRef(); // reference radius for comparison
-  static AliExternalTrackParam trback;
-  trback = *seed;
+  AliITSUTrackHyp trback;
+  trback.AliExternalTrackParam::operator=(*seed);
   trback.ResetCovariance(10000);
-  int ndf = seed->GetNLayersHit()*2-5;
-  double chi2sa = RefitTrack(&trback,rDest,1);
+  int nclFit = 0;
+  double chi2sa = RefitTrack(&trback,rDest,nclFit,1);
   if (chi2sa<0) return kFALSE;
+  int ndf = nclFit*2-5;
   if (ndf>0) chi2sa /= ndf;
-  if (chi2sa>fCurrTrackCond->GetMaxITSSAChi2()) return kFALSE;
+  if (chi2sa>fCurrTrackCond->GetMaxITSSAChi2(nclFit)) return kFALSE;
   //
   // relate to TPC track at outer layer
   AliExternalTrackParam* tpcSeed = fCurrHyp->GetTPCSeed();
   if (!trback.Rotate(tpcSeed->GetAlpha()) || !trback.PropagateParamOnlyTo(tpcSeed->GetX(),bz0)) {
+#ifdef _ITSU_DEBUG_
     if (AliDebugLevelClass()>+1 && !seed->ContainsFake()) {
       AliInfo(Form("Failed to propagate seed to TPC track @ X:%.3f Alpha:%.3f",tpcSeed->GetX(),tpcSeed->GetAlpha()));
       seed->Print("etp");
       trback.Print();
     }
+#endif
     return kFALSE; 
   }
   double chi2Match = trback.GetPredictedChi2(tpcSeed)/5;
@@ -1656,14 +1791,14 @@ void AliITSUTrackerGlo::SaveReducedHypothesesTree(AliITSUTrackHyp* dest)
   // remove those hypothesis seeds which dont lead to candidates at final layer
   //
   // 1st, flag the seeds to save
-  int lr0 = 0;
+  int lr0 = fCurrTrackCond->GetActiveLrInner();
   for (int isd=0;isd<fCurrHyp->GetNSeeds(lr0);isd++) {
     AliITSUSeed* seed = fCurrHyp->RemoveSeed(lr0,isd);
     if (!seed) continue;
     seed->FlagTree(AliITSUSeed::kSave);
     dest->AddSeed(seed,lr0);
   }
-  for (int ilr=1;ilr<fNLrActive;ilr++) {
+  for (int ilr=0;ilr<fNLrActive;ilr++) {
     int nsd = fCurrHyp->GetNSeeds(ilr);
     for (int isd=0;isd<nsd;isd++) {
       AliITSUSeed* seed = fCurrHyp->RemoveSeed(ilr,isd);
@@ -1676,6 +1811,21 @@ void AliITSUTrackerGlo::SaveReducedHypothesesTree(AliITSUTrackHyp* dest)
   //  AliInfo(Form("SeedsPool: %d, BookedUpTo: %d, free: %d",fSeedsPool.GetSize(),fSeedsPool.GetEntriesFast(),fNFreeSeeds));
 }
 
+//__________________________________________________________________
+void AliITSUTrackerGlo::CleanHypothesis(AliITSUTrackHyp* hyp)
+{
+  // clean hypothesis
+  hyp->SetWinner(0);
+  for (int ilr=0;ilr<fNLrActive;ilr++) {
+    int nsd = hyp->GetNSeeds(ilr);
+    for (int isd=0;isd<nsd;isd++) {
+      AliITSUSeed* seed = hyp->RemoveSeed(ilr,isd);
+      if (!seed) continue; // already discarded or saved
+      MarkSeedFree(seed);
+    }
+  }
+}
+
 //__________________________________________________________________
 void AliITSUTrackerGlo::FlagSplitClusters()
 {
@@ -1733,29 +1883,82 @@ void AliITSUTrackerGlo::PrintSeedClusters(const AliITSUSeed* seed, Option_t* opt
 }
 
 //__________________________________________________________________
-void AliITSUTrackerGlo::FlagSeedClusters(const AliITSUSeed* seed, Bool_t flg)
+void AliITSUTrackerGlo::FlagSeedClusters(const AliITSUSeed* seed, Bool_t flg, UShort_t ref)
 {
   // mark used clusters
   int lrID,clID;
   while (seed) {
-    if ( (clID=seed->GetLrCluster(lrID))>=0 ) ((AliITSUClusterPix*)fITS->GetLayerActive(lrID)->GetCluster(clID))->ModClUsage(flg);
+    if ( (clID=seed->GetLrCluster(lrID))>=0 ) {
+      AliITSUClusterPix* cl = (AliITSUClusterPix*)fITS->GetLayerActive(lrID)->GetCluster(clID);
+      if (ref) { // do we need to set or delete cluster->track ref?
+       if (flg) {
+         if (!cl->GetRecoInfo()) cl->SetRecoInfo(ref);  // set ref only if cluster already does not refer to other track, inc.counter
+       }
+       else {
+         if (cl->GetRecoInfo()==ref) cl->SetRecoInfo(0); // unset reference only if it refers to ref, decrease counter     
+       }
+      }
+    }
     seed = (AliITSUSeed*)seed->GetParent();
   }
   //
 }
 
+//__________________________________________________________________
+void AliITSUTrackerGlo::CheckClusterUsage()
+{
+  // check cluster usage info
+  printf("ClusterUsage at pass %d\n",fCurrPassID);
+  for (int ilr=0;ilr<fNLrActive;ilr++) {
+    AliITSURecoLayer* lr = fITS->GetLayerActive(ilr);
+    int ncl = lr->GetNClusters();
+    int nclUsed = 0;
+    int nclUs0CntShr1 = 0;
+    int nclUs1Cnt0 = 0;
+    int nclUs1Shr1 = 0;
+    int nclUseL = 0;
+    for (int icl=0;icl<ncl;icl++) {
+      AliITSUClusterPix *cl = (AliITSUClusterPix*)lr->GetCluster(icl);
+      int usc = cl->GetClUsage();
+      if (usc<1) {
+       if (cl->IsClusterUsed() || cl->IsClusterShared()) {
+         nclUs0CntShr1++;
+         printf("Lr%d Cl%4d: UseCnt=0 but UseFlg=%d UseShr=%d\n",ilr,icl,cl->IsClusterUsed(),cl->IsClusterShared());
+       }
+       continue;
+      }
+      nclUsed++;
+      if (!cl->IsClusterUsed()) {
+       nclUs1Cnt0++;
+       printf("Lr%d Cl%4d: UseCnt=%d but UseFlg=%d UseShr=%d\n",ilr,icl,usc, cl->IsClusterUsed(),cl->IsClusterShared());
+      }
+      if (cl->IsClusterShared()) {
+       nclUs1Shr1++;
+       printf("Lr%d Cl%4d: UseCnt=%d but UseFlg=%d UseShr=%d\n",ilr,icl,usc, cl->IsClusterUsed(),cl->IsClusterShared());
+      }
+      if (usc>1) {
+       nclUseL++;
+       printf("Lr%d Cl%4d: UseCnt=%d!! but UseFlg=%d UseShr=%d\n",ilr,icl,usc, cl->IsClusterUsed(),cl->IsClusterShared());
+      }
+    }
+    printf("ClUsage Lr%d: Ncl=%5d Nusd=%5d (%5.2f) Use0CS1:%4d Use1C0:%4d Use1S1:%4d UseL:%d\n",
+          ilr,ncl,nclUsed,ncl? float(nclUsed)/ncl : 0, nclUs0CntShr1,nclUs1Cnt0,nclUs1Shr1,nclUseL);
+  }
+}
 
 
-#ifdef  _FILL_CONTROL_HISTOS_
+#ifdef  _ITSU_TUNING_MODE_
 //__________________________________________________________________
-void AliITSUTrackerGlo::BookControlHistos()
+TObjArray* AliITSUTrackerGlo::BookControlHistos(const char* pref)
 {
   // book special control histos
-  if (fCHistoArr) return;
+  TString prefS = pref;
+  prefS.ToLower();
+  TObjArray* dest = new TObjArray;
+  dest->SetOwner(kTRUE);
+  //
   const int kNResDef=7;
   const double kResDef[kNResDef]={0.05,0.05,0.3, 0.05,1,0.5,1.5};
-  fCHistoArr = new TObjArray();
-  fCHistoArr->SetOwner(kTRUE);
   const double ptMax=10;
   const double plMax=10;
   const double chiMax=100;
@@ -1766,92 +1969,91 @@ void AliITSUTrackerGlo::BookControlHistos()
   const int maxBr  = 15;
   const int maxCand = 200;
   TString ttl;
-  for (int stp=0;stp<kNTrackingPhases;stp++) {
-    for (int ilr=0;ilr<fNLrActive;ilr++) {
-      int hoffs = (1+stp)*kHistosPhase + ilr;
-      double mxdf = ilr>=kNResDef ? kResDef[kNResDef-1] : kResDef[ilr];
-      ttl = Form("S%d_residY%d",stp,ilr);
-      TH2F* hdy = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax,nresbins,-mxdf,mxdf);
-      fCHistoArr->AddAtAndExpand(hdy,hoffs + kHResY);
-      hdy->SetDirectory(0);
+  for (int phase=0;phase<kNTrackingPhases;phase++) {
+    for (int pass=0;pass<AliITSUReconstructor::GetRecoParam()->GetNTrackingConditions();pass++) {
       //
-      ttl = Form("S%d_residYPull%d",stp,ilr);  
-      TH2F* hdyp = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax,nplbins,-plMax,plMax);
-      fCHistoArr->AddAtAndExpand(hdyp,hoffs + kHResYP);
-      hdyp->SetDirectory(0);
-      //
-      ttl = Form("S%d_residZ%d",stp,ilr);      
-      TH2F* hdz = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax,nresbins,-mxdf,mxdf);
-      fCHistoArr->AddAtAndExpand(hdz,hoffs + kHResZ);
-      hdz->SetDirectory(0);
-      //
-      ttl = Form("S%d_residZPull%d",stp,ilr);          
-      TH2F* hdzp = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax,nplbins,-plMax,plMax);
-      hdzp->SetDirectory(0);
-      fCHistoArr->AddAtAndExpand(hdzp,hoffs + kHResZP);
-      //
-      ttl = Form("S%d_chi2Cl%d",stp,ilr);              
-      TH2F* hchi = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins,0.,chiMax);
-      hchi->SetDirectory(0);
-      fCHistoArr->AddAtAndExpand(hchi,hoffs + kHChi2Cl);
-      //
-      ttl = Form("S%d_chi2Nrm%d",stp,ilr);             
-      TH2F* hchiN = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins,0.,chiMax);
-      hchiN->SetDirectory(0);
-      fCHistoArr->AddAtAndExpand(hchiN,hoffs + kHChi2Nrm);
-      //
-      if (stp==0) { // these histos make sense only for clusters2tracks stage
-       ttl = Form("S%d_bestInBranch%d",stp,ilr);               
+      for (int ilr=0;ilr<fNLrActive;ilr++) {
+       //
+       // ----------------- These are histos to be filled in Cluster2Tracks of each pass. 
+       // PropagateBack and RefitInward will be stored among the histos of 1st pass
+       if (pass>0 && phase!=kClus2Tracks) continue;
+       //
+       double mxdf = ilr>=kNResDef ? kResDef[kNResDef-1] : kResDef[ilr];
+       ttl = Form("Pass%d_S%d_residY%d_%s",pass,phase,ilr,pref);
+       TH2F* hdy = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax,nresbins,-mxdf,mxdf);
+       dest->AddAtAndExpand(hdy,GetHistoID(ilr,kHResY,pass,phase));
+       hdy->SetDirectory(0);
+       //
+       ttl = Form("Pass%d_S%d_residYPull%d_%s",pass,phase,ilr,pref);   
+       TH2F* hdyp = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax,nplbins,-plMax,plMax);
+       dest->AddAtAndExpand(hdyp,GetHistoID(ilr,kHResYP,pass,phase));
+       hdyp->SetDirectory(0);
+       //
+       ttl = Form("Pass%d_S%d_residZ%d_%s",pass,phase,ilr,pref);       
+       TH2F* hdz = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax,nresbins,-mxdf,mxdf);
+       dest->AddAtAndExpand(hdz,GetHistoID(ilr,kHResZ,pass,phase));
+       hdz->SetDirectory(0);
+       //
+       ttl = Form("Pass%d_S%d_residZPull%d_%s",pass,phase,ilr,pref);           
+       TH2F* hdzp = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax,nplbins,-plMax,plMax);
+       hdzp->SetDirectory(0);
+       dest->AddAtAndExpand(hdzp,GetHistoID(ilr,kHResZP,pass,phase));
+       //
+       ttl = Form("Pass%d_S%d_chi2Cl%d_%s",pass,phase,ilr,pref);               
+       TH2F* hchi = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins,0.,chiMax);
+       hchi->SetDirectory(0);
+       dest->AddAtAndExpand(hchi,GetHistoID(ilr,kHChi2Cl,pass,phase));
+       //
+       // ------------------- These histos are filled for Clusters2Tracks only
+       if (phase!=kClus2Tracks) continue;
+       //
+       ttl = Form("Pass%d_S%d_chi2Nrm%d_%s",pass,phase,ilr,pref);              
+       TH2F* hchiN = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins,0.,chiMax);
+       hchiN->SetDirectory(0);
+       dest->AddAtAndExpand(hchiN,GetHistoID(ilr,kHChi2Nrm,pass,phase));
+       //
+       ttl = Form("Pass%d_S%d_bestInBranch%d_%s",pass,phase,ilr,pref);         
        TH2* hnbr = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, maxBr,-0.5,maxBr-0.5);
        hnbr->SetDirectory(0);
-       fCHistoArr->AddAtAndExpand(hnbr,hoffs + kHBestInBranch);
+       dest->AddAtAndExpand(hnbr,GetHistoID(ilr,kHBestInBranch,pass,phase));
        //
-       ttl = Form("S%d_bestInCands%d",stp,ilr);                
+       ttl = Form("Pass%d_S%d_bestInCands%d_%s",pass,phase,ilr,pref);          
        TH2* hncn = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, maxCand,-0.5,maxCand-0.5);
        hncn->SetDirectory(0);
-       fCHistoArr->AddAtAndExpand(hncn,hoffs + kHBestInCand);
+       dest->AddAtAndExpand(hncn,GetHistoID(ilr,kHBestInCand,pass,phase));
        //
+      } // loop over layers
+      //
+      // these are special histos, filled not per layer but in the end of track fit in Clusters2Tracks in EVERY pass
+      //  
+      if (phase==kClus2Tracks) {
+       TH2* hchiMatch = 0; 
+       ttl = Form("Pass%d_Chi2Match_%s",pass,pref);
+       hchiMatch = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins,0.,chiMax);
+       hchiMatch->SetDirectory(0);
+       dest->AddAtAndExpand(hchiMatch,GetHistoID(-1,kHChiMatch,pass,phase));
+       // 
+       TH2* hchiSA = 0; 
+       ttl = Form("Pass%d_Chi2ITSSA_%s",pass,pref);
+       hchiSA = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins/2,0.,chiMax/2);
+       hchiSA->SetDirectory(0);
+       dest->AddAtAndExpand(hchiSA,GetHistoID(-1,kHChiITSSA,pass,phase));
+       //      
       }
-    }
-  }
-  // custom histos
-  ttl = Form("ClSharing");
-  TH2* hclShare = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, 2*fNLrActive,0,2*fNLrActive);
-  hclShare->SetDirectory(0);
-  fCHistoArr->AddAtAndExpand(hclShare,kHClShare);
-  //  
-  TH2* hchiMatch = 0; 
-  ttl = Form("Chi2MatchCorr");
-  hchiMatch = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins,0.,chiMax);
-  hchiMatch->SetDirectory(0);
-  fCHistoArr->AddAtAndExpand(hchiMatch,kHChiMatchCorr);
-  // 
-  ttl = Form("Chi2MatchFake");
-  hchiMatch = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins,0.,chiMax);
-  hchiMatch->SetDirectory(0);
-  fCHistoArr->AddAtAndExpand(hchiMatch,kHChiMatchFake);
-  // 
-  ttl = Form("Chi2MatchCorrMiss");
-  hchiMatch = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins,0.,chiMax);
-  hchiMatch->SetDirectory(0);
-  fCHistoArr->AddAtAndExpand(hchiMatch,kHChiMatchCorrMiss);
-  // 
-  ttl = Form("Chi2MatchFakeMiss");
-  hchiMatch = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins,0.,chiMax);
-  hchiMatch->SetDirectory(0);
-  fCHistoArr->AddAtAndExpand(hchiMatch,kHChiMatchFakeMiss);
-  // 
-  TH2* hchiSA = 0; 
-  ttl = Form("Chi2ITSSACorr");
-  hchiSA = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins/2,0.,chiMax/2);
-  hchiSA->SetDirectory(0);
-  fCHistoArr->AddAtAndExpand(hchiSA,kHChiITSSACorr);
-  // 
-  ttl = Form("Chi2ITSSAFake");
-  hchiSA = new TH2F(ttl.Data(),ttl.Data(),nptbins,0,ptMax, nchbins/2,0.,chiMax/2);
-  hchiSA->SetDirectory(0);
-  fCHistoArr->AddAtAndExpand(hchiSA,kHChiITSSAFake);
-  // 
+    } // loop over tracking passes
+  }// loop over tracking phases
+  //
+  return dest;
+}
 
+//__________________________________________________________________
+Int_t AliITSUTrackerGlo::GetHistoID(Int_t lr, Int_t hid, Int_t pass, Int_t phase)
+{
+  // get id for the requested histo
+  if (lr<0) lr=-1;
+  lr++;
+  return pass*kHistosPass + phase*kHistosPhase + lr*kMaxHID + hid;
+  //
 }
+
 #endif