]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITStrackerMI.cxx
Modifications needed to use PID framework based mass during tracking and
[u/mrichter/AliRoot.git] / ITS / AliITStrackerMI.cxx
index d6d1f7263b937561b093ab56129af19600068d15..15cc909281a6a3e690c11e0df969960d02315117 100644 (file)
 #include <TString.h>
 #include <TRandom.h>
 #include <TTreeStream.h>
+#include <TVector3.h>
+#include <TBits.h>
 
 #include "AliLog.h"
+#include "AliGeomManager.h"
 #include "AliITSPlaneEff.h"
-#include "AliITSCalibrationSPD.h"
-#include "AliITSCalibrationSDD.h"
-#include "AliITSCalibrationSSD.h"
-#include "AliCDBEntry.h"
-#include "AliCDBManager.h"
-#include "AliAlignObj.h"
 #include "AliTrackPointArray.h"
-#include "AliESDVertex.h"
 #include "AliESDEvent.h"
+#include "AliESDV0Params.h"
 #include "AliESDtrack.h"
 #include "AliV0.h"
-#include "AliHelix.h"
 #include "AliITSChannelStatus.h"
 #include "AliITSDetTypeRec.h"
 #include "AliITSRecPoint.h"
+#include "AliITSRecPointContainer.h"
 #include "AliITSgeomTGeo.h"
 #include "AliITSReconstructor.h"
 #include "AliITSClusterParam.h"
@@ -60,6 +57,8 @@
 #include "AliITSPlaneEffSSD.h"
 #include "AliITSV0Finder.h"
 #include "AliITStrackerMI.h"
+#include "AliMathBase.h"
+
 
 ClassImp(AliITStrackerMI)
 
@@ -81,6 +80,9 @@ fEsd(0),
 fTrackingPhase("Default"),
 fUseTGeo(3),
 fNtracks(0),
+fFlagFakes(kFALSE),
+fSelectBestMIP03(kFALSE),
+fUseImproveKalman(kFALSE),
 fxOverX0Pipe(-1.),
 fxTimesRhoPipe(-1.),
 fxOverX0PipeTrks(0),
@@ -92,12 +94,25 @@ fxTimesRhoLayerTrks(0),
 fDebugStreamer(0),
 fITSChannelStatus(0),
 fkDetTypeRec(0),
-fPlaneEff(0) {
+fPlaneEff(0),
+fSPDChipIntPlaneEff(0),
+fITSPid(0)
+
+ {
   //Default constructor
   Int_t i;
   for(i=0;i<4;i++) fSPDdetzcentre[i]=0.;
-  for(i=0;i<2;i++) {fxOverX0Shield[i]=-1.;fxTimesRhoShield[i]=-1.;}
+  for(i=0;i<2;i++) {
+    fxOverX0Shield[i]=-1.;
+    fxTimesRhoShield[i]=-1.;
+    fConstraint[i]=0;
+  }
   for(i=0;i<6;i++) {fxOverX0Layer[i]=-1.;fxTimesRhoLayer[i]=-1.;}
+  fOriginal.SetOwner();
+  for(i=0;i<AliITSgeomTGeo::kNLayers;i++)fForceSkippingOfLayer[i]=0;
+  for(i=0;i<100000;i++)fBestTrackIndex[i]=0;
+  fITSPid=new AliITSPIDResponse();
+
 }
 //------------------------------------------------------------------------
 AliITStrackerMI::AliITStrackerMI(const Char_t *geom) : AliTracker(),
@@ -116,6 +131,9 @@ fEsd(0),
 fTrackingPhase("Default"),
 fUseTGeo(3),
 fNtracks(0),
+fFlagFakes(kFALSE),
+fSelectBestMIP03(kFALSE),
+fUseImproveKalman(kFALSE),
 fxOverX0Pipe(-1.),
 fxTimesRhoPipe(-1.),
 fxOverX0PipeTrks(0),
@@ -127,7 +145,9 @@ fxTimesRhoLayerTrks(0),
 fDebugStreamer(0),
 fITSChannelStatus(0),
 fkDetTypeRec(0),
-fPlaneEff(0) {
+fPlaneEff(0),
+fSPDChipIntPlaneEff(0),
+fITSPid(0) {
   //--------------------------------------------------------------------
   //This is the AliITStrackerMI constructor
   //--------------------------------------------------------------------
@@ -135,6 +155,7 @@ fPlaneEff(0) {
     AliWarning("\"geom\" is actually a dummy argument !");
   }
 
+  fOriginal.SetOwner();
   fCoefficients = 0;
   fAfterV0     = kFALSE;
 
@@ -143,9 +164,11 @@ fPlaneEff(0) {
     Int_t ndet=AliITSgeomTGeo::GetNDetectors(i);
 
     Double_t xyz[3], &x=xyz[0], &y=xyz[1], &z=xyz[2];
-    AliITSgeomTGeo::GetOrigTranslation(i,1,1,xyz); 
+    AliITSgeomTGeo::GetTranslation(i,1,1,xyz); 
     Double_t poff=TMath::ATan2(y,x);
     Double_t zoff=z;
+
+    AliITSgeomTGeo::GetOrigTranslation(i,1,1,xyz);
     Double_t r=TMath::Sqrt(x*x + y*y);
 
     AliITSgeomTGeo::GetOrigTranslation(i,1,2,xyz);
@@ -184,7 +207,7 @@ fPlaneEff(0) {
        
       } // end loop on detectors
     } // end loop on ladders
-    fForceSkippingOfLayer[i] = 0;
+    fForceSkippingOfLayer[i-1] = 0;
   } // end loop on layers
 
 
@@ -207,15 +230,28 @@ fPlaneEff(0) {
   }
 
   // store positions of centre of SPD modules (in z)
+  //  The convetion is that fSPDdetzcentre is ordered from -z to +z
   Double_t tr[3];
   AliITSgeomTGeo::GetTranslation(1,1,1,tr);
-  fSPDdetzcentre[0] = tr[2];
-  AliITSgeomTGeo::GetTranslation(1,1,2,tr);
-  fSPDdetzcentre[1] = tr[2];
-  AliITSgeomTGeo::GetTranslation(1,1,3,tr);
-  fSPDdetzcentre[2] = tr[2];
-  AliITSgeomTGeo::GetTranslation(1,1,4,tr);
-  fSPDdetzcentre[3] = tr[2];
+  if (tr[2]<0) { // old geom (up to v5asymmPPR)
+    AliITSgeomTGeo::GetTranslation(1,1,1,tr);
+    fSPDdetzcentre[0] = tr[2];
+    AliITSgeomTGeo::GetTranslation(1,1,2,tr);
+    fSPDdetzcentre[1] = tr[2];
+    AliITSgeomTGeo::GetTranslation(1,1,3,tr);
+    fSPDdetzcentre[2] = tr[2];
+    AliITSgeomTGeo::GetTranslation(1,1,4,tr);
+    fSPDdetzcentre[3] = tr[2];
+  } else { // new geom (from v11Hybrid)
+    AliITSgeomTGeo::GetTranslation(1,1,4,tr);
+    fSPDdetzcentre[0] = tr[2];
+    AliITSgeomTGeo::GetTranslation(1,1,3,tr);
+    fSPDdetzcentre[1] = tr[2];
+    AliITSgeomTGeo::GetTranslation(1,1,2,tr);
+    fSPDdetzcentre[2] = tr[2];
+    AliITSgeomTGeo::GetTranslation(1,1,1,tr);
+    fSPDdetzcentre[3] = tr[2];
+  }
 
   fUseTGeo = AliITSReconstructor::GetRecoParam()->GetUseTGeoInTracker();
   if(AliITSReconstructor::GetRecoParam()->GetExtendedEtaAcceptance() && fUseTGeo!=1 && fUseTGeo!=3) {
@@ -226,7 +262,8 @@ fPlaneEff(0) {
   for(Int_t i=0;i<2;i++) {fxOverX0Shield[i]=-1.;fxTimesRhoShield[i]=-1.;}
   for(Int_t i=0;i<6;i++) {fxOverX0Layer[i]=-1.;fxTimesRhoLayer[i]=-1.;}
   
-  fDebugStreamer = new TTreeSRedirector("ITSdebug.root");
+  if (AliITSReconstructor::GetRecoParam()->GetESDV0Params()->StreamLevel()>0)
+    fDebugStreamer = new TTreeSRedirector("ITSdebug.root");
 
   // only for plane efficiency evaluation
   if (AliITSReconstructor::GetRecoParam()->GetComputePlaneEff() &&
@@ -234,14 +271,26 @@ fPlaneEff(0) {
     Int_t iplane=AliITSReconstructor::GetRecoParam()->GetIPlanePlaneEff();
     if(!AliITSReconstructor::GetRecoParam()->GetLayersToSkip(iplane)==1)
       AliWarning(Form("Evaluation of Plane Eff for layer %d will be attempted without removing it from tracker",iplane));
-    if (iplane<2) fPlaneEff = new AliITSPlaneEffSPD();
+    if (iplane<2) {
+      fPlaneEff = new AliITSPlaneEffSPD();
+      fSPDChipIntPlaneEff = new Bool_t[AliITSPlaneEffSPD::kNModule*AliITSPlaneEffSPD::kNChip];
+      for (UInt_t i=0; i<AliITSPlaneEffSPD::kNModule*AliITSPlaneEffSPD::kNChip; i++) fSPDChipIntPlaneEff[i]=kFALSE;
+    }
     else if (iplane<4) fPlaneEff = new AliITSPlaneEffSDD();
     else fPlaneEff = new AliITSPlaneEffSSD();
     if(AliITSReconstructor::GetRecoParam()->GetReadPlaneEffFromOCDB())
        if(!fPlaneEff->ReadFromCDB()) {AliWarning("AliITStrackerMI reading of AliITSPlaneEff from OCDB failed") ;}
     if(AliITSReconstructor::GetRecoParam()->GetHistoPlaneEff()) fPlaneEff->SetCreateHistos(kTRUE);
   }
+  //
+  // RS
+  fSelectBestMIP03  = kFALSE;//AliITSReconstructor::GetRecoParam()->GetSelectBestMIP03();
+  fFlagFakes        = AliITSReconstructor::GetRecoParam()->GetFlagFakes();
+  fUseImproveKalman = AliITSReconstructor::GetRecoParam()->GetUseImproveKalman();
+  //
+  fITSPid=new AliITSPIDResponse();
 }
+/*
 //------------------------------------------------------------------------
 AliITStrackerMI::AliITStrackerMI(const AliITStrackerMI &tracker):AliTracker(tracker),
 fI(tracker.fI),
@@ -259,6 +308,8 @@ fEsd(tracker.fEsd),
 fTrackingPhase(tracker.fTrackingPhase),
 fUseTGeo(tracker.fUseTGeo),
 fNtracks(tracker.fNtracks),
+fFlagFakes(tracker.fFlagFakes),
+fSelectBestMIP03(tracker.fSelectBestMIP03),
 fxOverX0Pipe(tracker.fxOverX0Pipe),
 fxTimesRhoPipe(tracker.fxTimesRhoPipe),
 fxOverX0PipeTrks(0),
@@ -272,6 +323,7 @@ fITSChannelStatus(tracker.fITSChannelStatus),
 fkDetTypeRec(tracker.fkDetTypeRec),
 fPlaneEff(tracker.fPlaneEff) {
   //Copy constructor
+  fOriginal.SetOwner();
   Int_t i;
   for(i=0;i<4;i++) {
     fSPDdetzcentre[i]=tracker.fSPDdetzcentre[i];
@@ -285,6 +337,7 @@ fPlaneEff(tracker.fPlaneEff) {
     fxTimesRhoShield[i]=tracker.fxTimesRhoShield[i];
   }
 }
+
 //------------------------------------------------------------------------
 AliITStrackerMI & AliITStrackerMI::operator=(const AliITStrackerMI &tracker){
   //Assignment operator
@@ -292,6 +345,7 @@ AliITStrackerMI & AliITStrackerMI::operator=(const AliITStrackerMI &tracker){
   new(this) AliITStrackerMI(tracker);
   return *this;
 }
+*/
 //------------------------------------------------------------------------
 AliITStrackerMI::~AliITStrackerMI()
 {
@@ -306,6 +360,9 @@ AliITStrackerMI::~AliITStrackerMI()
   }
   if(fITSChannelStatus) delete fITSChannelStatus;
   if(fPlaneEff) delete fPlaneEff;
+  if(fITSPid) delete fITSPid;
+  if (fSPDChipIntPlaneEff) delete [] fSPDChipIntPlaneEff;
+
 }
 //------------------------------------------------------------------------
 void AliITStrackerMI::ReadBadFromDetTypeRec() {
@@ -336,7 +393,7 @@ void AliITStrackerMI::ReadBadFromDetTypeRec() {
        if(det.IsBad()) {nBadDetsPerLayer++;}
       } // end loop on detectors
     } // end loop on ladders
-    Info("ReadBadFromDetTypeRec",Form("Layer %d: %d bad out of %d",i-1,nBadDetsPerLayer,ndet*AliITSgeomTGeo::GetNLadders(i)));
+    AliInfo(Form("Layer %d: %d bad out of %d",i-1,nBadDetsPerLayer,ndet*AliITSgeomTGeo::GetNLadders(i)));
   } // end loop on layers
   
   return;
@@ -346,22 +403,25 @@ Int_t AliITStrackerMI::LoadClusters(TTree *cTree) {
   //--------------------------------------------------------------------
   //This function loads ITS clusters
   //--------------------------------------------------------------------
-  TBranch *branch=cTree->GetBranch("ITSRecPoints");
-  if (!branch) { 
-    Error("LoadClusters"," can't get the branch !\n");
-    return 1;
-  }
-
-  static TClonesArray dummy("AliITSRecPoint",10000), *clusters=&dummy;
-  branch->SetAddress(&clusters);
+  TClonesArray *clusters = NULL;
+  AliITSRecPointContainer* rpcont=AliITSRecPointContainer::Instance();
+  clusters=rpcont->FetchClusters(0,cTree);
+  if(!clusters) return 1;
 
+  if(!(rpcont->IsSPDActive() || rpcont->IsSDDActive() || rpcont->IsSSDActive())){
+      AliError("ITS is not in a known running configuration: SPD, SDD and SSD are not active");
+      return 1;
+  }
   Int_t i=0,j=0,ndet=0;
   Int_t detector=0;
   for (i=0; i<AliITSgeomTGeo::GetNLayers(); i++) {
     ndet=fgLayers[i].GetNdetectors();
     Int_t jmax = j + fgLayers[i].GetNladders()*ndet;
     for (; j<jmax; j++) {           
-      if (!cTree->GetEvent(j)) continue;
+      //      if (!cTree->GetEvent(j)) continue;
+      clusters = rpcont->UncheckedGetClusters(j);
+      if(!clusters)continue;
       Int_t ncl=clusters->GetEntriesFast();
       SignDeltas(clusters,GetZ());
  
@@ -370,22 +430,28 @@ Int_t AliITStrackerMI::LoadClusters(TTree *cTree) {
         detector=c->GetDetectorIndex();
 
        if (!c->Misalign()) AliWarning("Can't misalign this cluster !");
-
-        fgLayers[i].InsertCluster(new AliITSRecPoint(*c));
+       
+       Int_t retval = fgLayers[i].InsertCluster(new AliITSRecPoint(*c));
+       if(retval) {
+         AliWarning(Form("Too many clusters on layer %d!",i));
+         break;  
+       } 
       }
-      clusters->Delete();
+
       // add dead zone "virtual" cluster in SPD, if there is a cluster within 
       // zwindow cm from the dead zone      
+      //  This method assumes that fSPDdetzcentre is ordered from -z to +z
       if (i<2 && AliITSReconstructor::GetRecoParam()->GetAddVirtualClustersInDeadZone()) {
        for (Float_t xdead = 0; xdead < AliITSRecoParam::GetSPDdetxlength(); xdead += (i+1.)*AliITSReconstructor::GetRecoParam()->GetXPassDeadZoneHits()) {
          Int_t lab[4]   = {0,0,0,detector};
          Int_t info[3]  = {0,0,i};
          Float_t q      = 0.; // this identifies virtual clusters
-         Float_t hit[5] = {xdead,
+         Float_t hit[6] = {xdead,
                            0.,
                            AliITSReconstructor::GetRecoParam()->GetSigmaXDeadZoneHit2(),
                            AliITSReconstructor::GetRecoParam()->GetSigmaZDeadZoneHit2(),
-                           q};
+                           q,
+                           0.};
          Bool_t local   = kTRUE;
          Double_t zwindow = AliITSReconstructor::GetRecoParam()->GetZWindowDeadZone();
          hit[1] = fSPDdetzcentre[0]+0.5*AliITSRecoParam::GetSPDdetzlength();
@@ -415,8 +481,6 @@ Int_t AliITStrackerMI::LoadClusters(TTree *cTree) {
     fgLayers[i].SortClusters();
   }
 
-  dummy.Clear();
-
   // check whether we have to skip some layers
   SetForceSkippingOfLayer();
 
@@ -477,7 +541,12 @@ Int_t AliITStrackerMI::Clusters2Tracks(AliESDEvent *event) {
   AliDebug(2,Form("SKIPPING %d %d %d %d %d %d",ForceSkippingOfLayer(0),ForceSkippingOfLayer(1),ForceSkippingOfLayer(2),ForceSkippingOfLayer(3),ForceSkippingOfLayer(4),ForceSkippingOfLayer(5)));
 
   fTrackingPhase="Clusters2Tracks";
-
+  //
+  // RS
+  fSelectBestMIP03  = kFALSE;//AliITSReconstructor::GetRecoParam()->GetSelectBestMIP03();
+  fFlagFakes        = AliITSReconstructor::GetRecoParam()->GetFlagFakes();
+  fUseImproveKalman = AliITSReconstructor::GetRecoParam()->GetUseImproveKalman();
+  //
   TObjArray itsTracks(15000);
   fOriginal.Clear();
   fEsd = event;         // store pointer to the esd 
@@ -492,11 +561,11 @@ Int_t AliITStrackerMI::Clusters2Tracks(AliESDEvent *event) {
     }
   }
   // temporary
-
+  Int_t noesd = 0;
   {/* Read ESD tracks */
-    Double_t pimass = TDatabasePDG::Instance()->GetParticle(211)->Mass();
     Int_t nentr=event->GetNumberOfTracks();
-    Info("Clusters2Tracks", "Number of ESD tracks: %d\n", nentr);
+    noesd=nentr;
+    //    Info("Clusters2Tracks", "Number of ESD tracks: %d\n", nentr);
     while (nentr--) {
       AliESDtrack *esd=event->GetTrack(nentr);
       //  ---- for debugging:
@@ -506,20 +575,10 @@ Int_t AliITStrackerMI::Clusters2Tracks(AliESDEvent *event) {
       if (esd->GetStatus()&AliESDtrack::kTPCout) continue;
       if (esd->GetStatus()&AliESDtrack::kITSin) continue;
       if (esd->GetKinkIndex(0)>0) continue;   //kink daughter
-      AliITStrackMI *t=0;
-      try {
-        t=new AliITStrackMI(*esd);
-      } catch (const Char_t *msg) {
-        //Warning("Clusters2Tracks",msg);
-        delete t;
-        continue;
-      }
+      AliITStrackMI *t = new AliITStrackMI(*esd);
       t->GetDZ(GetX(),GetY(),GetZ(),t->GetDP());              //I.B.
       Double_t vdist = TMath::Sqrt(t->GetD(0)*t->GetD(0)+t->GetD(1)*t->GetD(1));
 
-
-      // look at the ESD mass hypothesys !
-      if (t->GetMass()<0.9*pimass) t->SetMass(pimass); 
       // write expected q
       t->SetExpQ(TMath::Max(0.8*t->GetESDtrack()->GetTPCsignal(),30.));
 
@@ -590,6 +649,7 @@ Int_t AliITStrackerMI::Clusters2Tracks(AliESDEvent *event) {
        besttrack->SetFakeRatio(1.);
        CookLabel(besttrack,0.); //For comparison only
        UpdateESDtrack(besttrack,AliESDtrack::kITSin);
+       t->SetWinner(besttrack);
 
        if (fConstraint[fPass]&&(!besttrack->IsGoldPrimary())) continue;  //to be tracked also without vertex constrain 
 
@@ -599,28 +659,36 @@ Int_t AliITStrackerMI::Clusters2Tracks(AliESDEvent *event) {
      }
      GetBestHypothesysMIP(itsTracks); 
   } // end loop on the two tracking passes
-
+  //
+  if (fFlagFakes) FlagFakes(itsTracks);
+  //
   if(event->GetNumberOfV0s()>0) AliITSV0Finder::UpdateTPCV0(event,this);
   if(AliITSReconstructor::GetRecoParam()->GetFindV0s()) AliITSV0Finder::FindV02(event,this);
   fAfterV0 = kTRUE;
   //
-  itsTracks.Delete();
+  itsTracks.Clear();
   //
   Int_t entries = fTrackHypothesys.GetEntriesFast();
   for (Int_t ientry=0; ientry<entries; ientry++) {
-    TObjArray * array =(TObjArray*)fTrackHypothesys.UncheckedAt(ientry);
+    TObjArray * array =(TObjArray*)fTrackHypothesys.At(ientry);
     if (array) array->Delete();
     delete fTrackHypothesys.RemoveAt(ientry); 
   }
 
   fTrackHypothesys.Delete();
+  entries = fBestHypothesys.GetEntriesFast();
+  for (Int_t ientry=0; ientry<entries; ientry++) {
+    TObjArray * array =(TObjArray*)fBestHypothesys.At(ientry);
+    if (array) array->Delete();
+    delete fBestHypothesys.RemoveAt(ientry);
+  }
   fBestHypothesys.Delete();
   fOriginal.Clear();
   delete [] fCoefficients;
   fCoefficients=0;
   DeleteTrksMaterialLUT();
 
-  Info("Clusters2Tracks","Number of prolonged tracks: %d\n",ntrk);
+  AliInfo(Form("Number of prolonged tracks: %d out of %d ESD tracks",ntrk,noesd));
 
   fTrackingPhase="Default";
   
@@ -634,64 +702,62 @@ Int_t AliITStrackerMI::PropagateBack(AliESDEvent *event) {
   //--------------------------------------------------------------------
   fTrackingPhase="PropagateBack";
   Int_t nentr=event->GetNumberOfTracks();
-  Info("PropagateBack", "Number of ESD tracks: %d\n", nentr);
-
+  //  Info("PropagateBack", "Number of ESD tracks: %d\n", nentr);
+  double bz0 = GetBz();
+  const double kWatchStep=10.; // for larger steps watch arc vs segment difference
+  //
   Int_t ntrk=0;
   for (Int_t i=0; i<nentr; i++) {
      AliESDtrack *esd=event->GetTrack(i);
 
-     if ((esd->GetStatus()&AliESDtrack::kITSin)==0) continue;
-     if (esd->GetStatus()&AliESDtrack::kITSout) continue;
-
-     AliITStrackMI *t=0;
-     try {
-        t=new AliITStrackMI(*esd);
-     } catch (const Char_t *msg) {
-       //Warning("PropagateBack",msg);
-        delete t;
-        continue;
-     }
-     t->SetExpQ(TMath::Max(0.8*t->GetESDtrack()->GetTPCsignal(),30.));
-
-     ResetTrackToFollow(*t);
-
-     /*
-     // propagate to vertex [SR, GSI 17.02.2003]
-     // Start Time measurement [SR, GSI 17.02.2003], corrected by I.Belikov
-     if (CorrectForPipeMaterial(&fTrackToFollow,"inward")) {
-       if (fTrackToFollow.PropagateToVertex(event->GetVertex()))
-        fTrackToFollow.StartTimeIntegral();
-       // from vertex to outside pipe
-       CorrectForPipeMaterial(&fTrackToFollow,"outward");
-       }*/
      // Start time integral and add distance from current position to vertex 
+     if (esd->GetStatus()&AliESDtrack::kITSout) continue;
+     AliITStrackMI t(*esd);
      Double_t xyzTrk[3],xyzVtx[3]={GetX(),GetY(),GetZ()};
-     fTrackToFollow.GetXYZ(xyzTrk);
+     t.GetXYZ(xyzTrk); 
      Double_t dst2 = 0.;
-     for (Int_t icoord=0; icoord<3; icoord++) {  
-       Double_t di = xyzTrk[icoord] - xyzVtx[icoord];
-       dst2 += di*di; 
+     {
+       double dxs = xyzTrk[0] - xyzVtx[0];
+       double dys = xyzTrk[1] - xyzVtx[1];
+       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)
+       // Hence s^2/d^2 = (1+1/6 p^2)^2
+       dst2 = dxs*dxs + dys*dys;
+       if (dst2 > kWatchStep*kWatchStep) { // correct circular part for arc/segment factor
+        double crv = TMath::Abs(esd->GetC(bz0));
+        double fcarc = 1.+crv*crv*dst2/6.;
+        dst2 *= fcarc*fcarc;
+       }
+       dst2 += dzs*dzs;
      }
-     fTrackToFollow.StartTimeIntegral();
-     fTrackToFollow.AddTimeStep(TMath::Sqrt(dst2));
+     t.StartTimeIntegral();
+     t.AddTimeStep(TMath::Sqrt(dst2));
+     //
+     // transfer the time integral to ESD track
+     esd->SetStatus(AliESDtrack::kTIME);
+     Double_t times[10];t.GetIntegratedTimes(times); esd->SetIntegratedTimes(times);
+     esd->SetIntegratedLength(t.GetIntegratedLength());
+     //
+     if ((esd->GetStatus()&AliESDtrack::kITSin)==0) continue;
 
+     t.SetExpQ(TMath::Max(0.8*t.GetESDtrack()->GetTPCsignal(),30.));
+     ResetTrackToFollow(t);
+     //
      fTrackToFollow.ResetCovariance(10.); fTrackToFollow.ResetClusters();
-     if (RefitAt(AliITSRecoParam::GetrInsideITSscreen(),&fTrackToFollow,t)) {
-        if (!CorrectForTPCtoITSDeadZoneMaterial(&fTrackToFollow)) {
-          delete t;
-          continue;
-        }
-        fTrackToFollow.SetLabel(t->GetLabel());
-        //fTrackToFollow.CookdEdx();
-        CookLabel(&fTrackToFollow,0.); //For comparison only
-        fTrackToFollow.UpdateESDtrack(AliESDtrack::kITSout);
-        //UseClusters(&fTrackToFollow);
-        ntrk++;
+     if (RefitAt(AliITSRecoParam::GetrInsideITSscreen(),&fTrackToFollow,&t)) {
+       if (!CorrectForTPCtoITSDeadZoneMaterial(&fTrackToFollow)) continue;
+       //       fTrackToFollow.SetLabel(t.GetLabel());              // why do we neet this
+       //fTrackToFollow.CookdEdx();
+       CookLabel(&fTrackToFollow,0.); //For comparison only // why do we need this?
+       fTrackToFollow.UpdateESDtrack(AliESDtrack::kITSout);
+       //UseClusters(&fTrackToFollow);
+       ntrk++;
      }
-     delete t;
   }
 
-  Info("PropagateBack","Number of back propagated ITS tracks: %d\n",ntrk);
+  AliInfo(Form("Number of back propagated ITS tracks: %d out of %d ESD tracks",ntrk,nentr));
 
   fTrackingPhase="Default";
 
@@ -708,8 +774,18 @@ Int_t AliITStrackerMI::RefitInward(AliESDEvent *event) {
 
   if(AliITSReconstructor::GetRecoParam()->GetFindV0s()) AliITSV0Finder::RefitV02(event,this);
 
+  Bool_t doExtra=AliITSReconstructor::GetRecoParam()->GetSearchForExtraClusters();
+  if(!doExtra) AliDebug(2,"Do not search for extra clusters");
+
   Int_t nentr=event->GetNumberOfTracks();
-  Info("RefitInward", "Number of ESD tracks: %d\n", nentr);
+  //  Info("RefitInward", "Number of ESD tracks: %d\n", nentr);
+
+  // only for PlaneEff and in case of SPD (for FO studies)
+  if( AliITSReconstructor::GetRecoParam()->GetComputePlaneEff() &&
+      AliITSReconstructor::GetRecoParam()->GetIPlanePlaneEff()>=0 && 
+      AliITSReconstructor::GetRecoParam()->GetIPlanePlaneEff()<2) {
+      for (UInt_t i=0; i<AliITSPlaneEffSPD::kNModule*AliITSPlaneEffSPD::kNChip; i++) fSPDChipIntPlaneEff[i]=kFALSE;     
+  }
 
   Int_t ntrk=0;
   for (Int_t i=0; i<nentr; i++) {
@@ -720,14 +796,8 @@ Int_t AliITStrackerMI::RefitInward(AliESDEvent *event) {
     if (esd->GetStatus()&AliESDtrack::kTPCout)
       if ((esd->GetStatus()&AliESDtrack::kTPCrefit)==0) continue;
 
-    AliITStrackMI *t=0;
-    try {
-        t=new AliITStrackMI(*esd);
-    } catch (const Char_t *msg) {
-      //Warning("RefitInward",msg);
-        delete t;
-        continue;
-    }
+    AliITStrackMI *t = new AliITStrackMI(*esd);
+
     t->SetExpQ(TMath::Max(0.8*t->GetESDtrack()->GetTPCsignal(),30.));
     if (!CorrectForTPCtoITSDeadZoneMaterial(t)) {
        delete t;
@@ -737,21 +807,29 @@ Int_t AliITStrackerMI::RefitInward(AliESDEvent *event) {
     ResetTrackToFollow(*t);
     fTrackToFollow.ResetClusters();
 
-    if ((esd->GetStatus()&AliESDtrack::kTPCin)==0)
+    // ITS standalone tracks
+    if ((esd->GetStatus()&AliESDtrack::kTPCin)==0) {
       fTrackToFollow.ResetCovariance(10.);
+      // protection for loopers that can have parameters screwed up
+      if(TMath::Abs(fTrackToFollow.GetY())>1000. ||
+        TMath::Abs(fTrackToFollow.GetZ())>1000.) {
+       delete t;
+       continue;
+      }
+    }
 
     //Refitting...
     Bool_t pe=(AliITSReconstructor::GetRecoParam()->GetComputePlaneEff() &&
                AliITSReconstructor::GetRecoParam()->GetIPlanePlaneEff()>=0);
 
     AliDebug(2,Form("Refit LABEL %d  %d",t->GetLabel(),t->GetNumberOfClusters()));
-    if (RefitAt(AliITSRecoParam::GetrInsideSPD1(),&fTrackToFollow,t,kTRUE,pe)) {
+    if (RefitAt(AliITSRecoParam::GetrInsideSPD1(),&fTrackToFollow,t,doExtra,pe)) {
        AliDebug(2,"  refit OK");
        fTrackToFollow.SetLabel(t->GetLabel());
        //       fTrackToFollow.CookdEdx();
        CookdEdx(&fTrackToFollow);
 
-       CookLabel(&fTrackToFollow,0.0); //For comparison only
+       CookLabel(&fTrackToFollow,0.0); //For comparison only // RS why do we need this?
 
        //The beam pipe
        if (CorrectForPipeMaterial(&fTrackToFollow,"inward")) {
@@ -768,7 +846,7 @@ Int_t AliITStrackerMI::RefitInward(AliESDEvent *event) {
     delete t;
   }
 
-  Info("RefitInward","Number of refitted tracks: %d\n",ntrk);
+  AliInfo(Form("Number of refitted tracks: %d out of %d ESD tracks",ntrk,nentr));
 
   fTrackingPhase="Default";
 
@@ -802,6 +880,7 @@ Bool_t AliITStrackerMI::GetTrackPoint(Int_t index, AliTrackPoint& p) const {
   p.SetCharge(cl->GetQ());
   p.SetDriftTime(cl->GetDriftTime());
   p.SetChargeRatio(cl->GetChargeRatio());
+  p.SetClusterType(cl->GetClusterType());
   AliGeomManager::ELayerID iLayer = AliGeomManager::kInvalidLayer; 
   switch (l) {
   case 0:
@@ -851,7 +930,7 @@ Bool_t AliITStrackerMI::GetTrackPointTrackingError(Int_t index,
   detxy[1] = det.GetR()*TMath::Sin(det.GetPhi());
   Double_t alpha = t->GetAlpha();
   Double_t xdetintrackframe = detxy[0]*TMath::Cos(alpha)+detxy[1]*TMath::Sin(alpha);
-  Float_t phi = TMath::ASin(t->GetSnpAt(xdetintrackframe,GetBz()));
+  Float_t phi = TMath::ASin(t->GetSnpAt(xdetintrackframe+cl->GetX(),GetBz()));
   phi += alpha-det.GetPhi();
   Float_t tgphi = TMath::Tan(phi);
 
@@ -874,6 +953,7 @@ Bool_t AliITStrackerMI::GetTrackPointTrackingError(Int_t index,
   p.SetCharge(cl->GetQ());
   p.SetDriftTime(cl->GetDriftTime());
   p.SetChargeRatio(cl->GetChargeRatio());
+  p.SetClusterType(cl->GetClusterType());
 
   AliGeomManager::ELayerID iLayer = AliGeomManager::kInvalidLayer; 
   switch (l) {
@@ -937,20 +1017,22 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
   TObjArray *bestarray = (TObjArray*)fBestHypothesys.At(esdindex);
   if (!bestarray){
     bestarray = new TObjArray(5);
+    bestarray->SetOwner();
     fBestHypothesys.AddAt(bestarray,esdindex);
   }
 
   //
   //setup tree of the prolongations
   //
-  static AliITStrackMI tracks[7][100];
+  const int kMaxTr = 100; //RS
+  static AliITStrackMI tracks[7][kMaxTr];
   AliITStrackMI *currenttrack;
   static AliITStrackMI currenttrack1;
   static AliITStrackMI currenttrack2;  
   static AliITStrackMI backuptrack;
   Int_t ntracks[7];
-  Int_t nindexes[7][100];
-  Float_t normalizedchi2[100];
+  Int_t nindexes[7][kMaxTr];
+  Float_t normalizedchi2[kMaxTr];
   for (Int_t ilayer=0;ilayer<6;ilayer++) ntracks[ilayer]=0;
   otrack->SetNSkipped(0);
   new (&(tracks[6][0])) AliITStrackMI(*otrack);
@@ -973,8 +1055,9 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
     Int_t nskipped=0;
     Float_t nused =0;
     for (Int_t itrack =0; itrack<ntracks[ilayer+1]; itrack++) {
+      //      printf("LR %d Tr:%d NSeeds: %d\n",ilayer,itrack,ntracks[ilayer+1]);
       //set current track
-      if (ntracks[ilayer]>=100) break;  
+      if (ntracks[ilayer]>=kMaxTr) break;  
       if (tracks[ilayer+1][nindexes[ilayer+1][itrack]].GetNSkipped()>0) nskipped++;
       if (tracks[ilayer+1][nindexes[ilayer+1][itrack]].GetNUsed()>2.) nused++;
       if (ntracks[ilayer]>15+ilayer){
@@ -999,8 +1082,13 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
       if (!currenttrack1.GetXYZ(trackGlobXYZ1)) continue;
 
       // Get the budget to the primary vertex for the current track being prolonged
-      Double_t budgetToPrimVertex = GetEffectiveThickness();
-
+      Double_t budgetToPrimVertex = 0;
+      double xMSLrs[9],x2X0MSLrs[9]; // needed for ImproveKalman
+      int nMSLrs = 0;
+      //
+      if (fUseImproveKalman) nMSLrs = GetEffectiveThicknessLbyL(xMSLrs,x2X0MSLrs);
+      else budgetToPrimVertex = GetEffectiveThickness();
+      //
       // check if we allow a prolongation without point
       Int_t skip = CheckSkipLayer(&currenttrack1,ilayer,idet);
       if (skip) {
@@ -1017,7 +1105,11 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
        if(LocalModuleCoord(ilayer,idet,vtrack,xloc,zloc)) { // local module coords
          vtrack->SetModuleIndexInfo(ilayer,idet,modstatus,xloc,zloc);
        }
-       if(constrain) vtrack->Improve(budgetToPrimVertex,xyzVtx,ersVtx);
+       if(constrain && AliITSReconstructor::GetRecoParam()->GetImproveWithVertex()) {
+         fUseImproveKalman ? 
+           vtrack->ImproveKalman(xyzVtx,ersVtx,xMSLrs,x2X0MSLrs,nMSLrs) : 
+           vtrack->Improve(budgetToPrimVertex,xyzVtx,ersVtx);
+       }
        ntracks[ilayer]++;
        continue;
       }
@@ -1108,7 +1200,11 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
                TMath::Abs(updatetrack->GetD(1)/(1.+ilayer)) > // z
                AliITSReconstructor::GetRecoParam()->GetMaxDZforPrimTrk()) isPrim=kFALSE;
          }
-         if (isPrim) updatetrack->Improve(budgetToPrimVertex,xyzVtx,ersVtx);
+         if (isPrim && AliITSReconstructor::GetRecoParam()->GetImproveWithVertex()) {
+           fUseImproveKalman ? 
+             updatetrack->ImproveKalman(xyzVtx,ersVtx,xMSLrs,x2X0MSLrs,nMSLrs) : 
+             updatetrack->Improve(budgetToPrimVertex,xyzVtx,ersVtx);
+         }
        }
        updatetrack->SetNDeadZone(updatetrack->GetNDeadZone()+1);
        if (dead) {
@@ -1127,9 +1223,9 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
       clidx=-1;
       // loop over clusters in the road
       while ((cl=layer.GetNextCluster(clidx))!=0) { 
-       if (ntracks[ilayer]>95) break; //space for skipped clusters  
+       if (ntracks[ilayer]>int(0.95*kMaxTr)) break; //space for skipped clusters  
        Bool_t changedet =kFALSE;  
-       if (cl->GetQ()==0 && deadzoneSPD==kTRUE) continue;
+       if (TMath::Abs(cl->GetQ())<1.e-13 && deadzoneSPD==kTRUE) continue;
        Int_t idetc=cl->GetDetectorIndex();
 
        if (currenttrack->GetDetectorIndex()==idetc) { // track already on the cluster's detector
@@ -1141,6 +1237,9 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
               (currenttrack->GetY()-cl->GetY())*(currenttrack->GetY()-cl->GetY())*msy > 1. ) 
            {  // cluster not associated to track
              AliDebug(2,"not associated");
+             // MvL: added here as well
+             // bring track back to ideal detector plane
+             currenttrack->Propagate(xTrOrig);
              continue;
            }
          // bring track back to ideal detector plane
@@ -1173,13 +1272,13 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
        // chi2 cut
        AliDebug(2,Form("chi2 %f max %f",chi2trkcl,AliITSReconstructor::GetRecoParam()->GetMaxChi2s(ilayer)));
        if (chi2trkcl < AliITSReconstructor::GetRecoParam()->GetMaxChi2s(ilayer)) {
-         if (cl->GetQ()==0) deadzoneSPD=kTRUE; // only 1 prolongation with virtual cluster       
-         if (ntracks[ilayer]>=100) continue;
+         if (TMath::Abs(cl->GetQ())<1.e-13) deadzoneSPD=kTRUE; // only 1 prolongation with virtual cluster       
+         if (ntracks[ilayer]>=kMaxTr) continue;
          AliITStrackMI * updatetrack = new (&tracks[ilayer][ntracks[ilayer]]) AliITStrackMI(*currenttrack);
          updatetrack->SetClIndex(ilayer,-1);
          if (changedet) new (&currenttrack2) AliITStrackMI(backuptrack);
 
-         if (cl->GetQ()!=0) { // real cluster
+         if (TMath::Abs(cl->GetQ())>1.e-13) { // real cluster
            if (!UpdateMI(updatetrack,cl,chi2trkcl,(ilayer<<28)+clidx)) {
              AliDebug(2,"update failed");
              continue;
@@ -1215,7 +1314,11 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
                  TMath::Abs(updatetrack->GetD(1)/(1.+ilayer)) > // z
                  AliITSReconstructor::GetRecoParam()->GetMaxDZforPrimTrk()) isPrim=kFALSE;
            }
-           if (isPrim) updatetrack->Improve(budgetToPrimVertex,xyzVtx,ersVtx);
+           if (isPrim && AliITSReconstructor::GetRecoParam()->GetImproveWithVertex()) {
+             fUseImproveKalman ? 
+               updatetrack->ImproveKalman(xyzVtx,ersVtx,xMSLrs,x2X0MSLrs,nMSLrs) : 
+               updatetrack->Improve(budgetToPrimVertex,xyzVtx,ersVtx);
+           }
          } //apply vertex constrain              
          ntracks[ilayer]++;
        }  // create new hypothesis
@@ -1226,32 +1329,23 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
       } // loop over possible prolongations 
      
       // allow one prolongation without clusters
-      if (constrain && itrack<=1 && currenttrack1.GetNSkipped()==0 && deadzoneSPD==kFALSE && ntracks[ilayer]<100) {
+      if (constrain && itrack<=1 && TMath::Abs(currenttrack1.GetNSkipped())<1.e-13 && deadzoneSPD==kFALSE && ntracks[ilayer]<kMaxTr) {
        AliITStrackMI* vtrack = new (&tracks[ilayer][ntracks[ilayer]]) AliITStrackMI(currenttrack1);
        // apply correction for material of the current layer
        CorrectForLayerMaterial(vtrack,ilayer,trackGlobXYZ1,"inward");
        vtrack->SetClIndex(ilayer,-1);
        modstatus = 3; // skipped 
        vtrack->SetModuleIndexInfo(ilayer,idet,modstatus,xloc,zloc);
-       vtrack->Improve(budgetToPrimVertex,xyzVtx,ersVtx);
+       if(AliITSReconstructor::GetRecoParam()->GetImproveWithVertex()) {
+         fUseImproveKalman ? 
+           vtrack->ImproveKalman(xyzVtx,ersVtx,xMSLrs,x2X0MSLrs,nMSLrs) : 
+           vtrack->Improve(budgetToPrimVertex,xyzVtx,ersVtx);
+       }
        vtrack->IncrementNSkipped();
        ntracks[ilayer]++;
       }
-
-      // allow one prolongation without clusters for tracks with |tgl|>1.1
-      if (constrain && itrack==0 && TMath::Abs(currenttrack1.GetTgl())>1.1) {  //big theta - for low flux
-       AliITStrackMI* vtrack = new (&tracks[ilayer][ntracks[ilayer]]) AliITStrackMI(currenttrack1);
-       // apply correction for material of the current layer
-       CorrectForLayerMaterial(vtrack,ilayer,trackGlobXYZ1,"inward");
-       vtrack->SetClIndex(ilayer,-1);
-       modstatus = 3; // skipped
-       vtrack->SetModuleIndexInfo(ilayer,idet,modstatus,xloc,zloc);
-       vtrack->Improve(budgetToPrimVertex,xyzVtx,ersVtx);
-       vtrack->SetNDeadZone(vtrack->GetNDeadZone()+1);
-       ntracks[ilayer]++;
-      }
-     
       
+
     } // loop over tracks in layer ilayer+1
 
     //loop over track candidates for the current layer
@@ -1280,7 +1374,8 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
     TMath::Sort(ntracks[ilayer],normalizedchi2,nindexes[ilayer],kFALSE); 
     ntracks[ilayer] = TMath::Min(accepted,7+2*ilayer);
     if (ntracks[ilayer]<golden+2+ilayer) ntracks[ilayer]=TMath::Min(golden+2+ilayer,accepted);
-    if (ntracks[ilayer]>90) ntracks[ilayer]=90; 
+    //    if (ntracks[ilayer]>90) ntracks[ilayer]=90; 
+    if (ntracks[ilayer]>int(kMaxTr*0.9)) ntracks[ilayer]=int(kMaxTr*0.9); 
   } // end loop over layers
 
 
@@ -1322,15 +1417,12 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
     for (Int_t i=0;i<TMath::Min(2,ntracks[2]);i++) {
       AliITStrackMI & track= tracks[2][nindexes[2][i]];
       if (track.GetNumberOfClusters()<3) continue;
-      if (!constrain && track.GetNormChi2(2) >
+      if (track.GetNormChi2(2) >
          AliITSReconstructor::GetRecoParam()->GetMaxNormChi2NonCForHypothesis()) continue;
-      if (constrain) track.SetNSkipped(track.GetNSkipped()+2);      
-      if (!constrain){
-       track.SetD(0,track.GetD(GetX(),GetY()));
-       track.SetNSkipped(track.GetNSkipped()+7./(7.+8.*TMath::Abs(track.GetD(0))));
-       if (track.GetNumberOfClusters()+track.GetNDeadZone()+track.GetNSkipped()>6) {
-         track.SetNSkipped(6-track.GetNumberOfClusters()+track.GetNDeadZone());
-       }
+      track.SetD(0,track.GetD(GetX(),GetY()));
+      track.SetNSkipped(track.GetNSkipped()+7./(7.+8.*TMath::Abs(track.GetD(0))));
+      if (track.GetNumberOfClusters()+track.GetNDeadZone()+track.GetNSkipped()>6) {
+       track.SetNSkipped(6-track.GetNumberOfClusters()+track.GetNDeadZone());
       }
       AddTrackHypothesys(new AliITStrackMI(track), esdindex);
     }
@@ -1369,7 +1461,7 @@ void AliITStrackerMI::FollowProlongationTree(AliITStrackMI * otrack, Int_t esdin
       Double_t xrp[3]; vertex->GetXYZ(xrp[0],xrp[1],xrp[2]);  //I.B.
       Int_t nearestold  = GetNearestLayer(xrp);               //I.B.
       Int_t nearest     = nearestold; 
-      for (Int_t ilayer =nearest;ilayer<8;ilayer++){
+      for (Int_t ilayer =nearest;ilayer<7;ilayer++){
        if (ntracks[nearest]==0){
          nearest = ilayer;
        }
@@ -1446,7 +1538,51 @@ fNMaxSigmaCl(3)
     fClusterTracks[0][i]=-1;
     fClusterTracks[1][i]=-1;
     fClusterTracks[2][i]=-1;    
-    fClusterTracks[3][i]=-1;    
+    fClusterTracks[3][i]=-1;
+    fY[i]=0;    
+    fZ[i]=0;    
+  }
+  fYB[0]=0;
+  fYB[1]=0;
+
+  for (Int_t j=0; j<AliITSRecoParam::kMaxClusterPerLayer5; j++) {
+    for (Int_t j1=0; j1<6; j1++) {
+      fClusters5[j1][j]=0;
+      fClusterIndex5[j1][j]=-1;
+      fY5[j1][j]=0;
+      fZ5[j1][j]=0;
+      fN5[j1]=0;
+      fBy5[j1][0]=0;
+      fBy5[j1][1]=0;
+    }
+  }
+
+  for (Int_t j=0; j<AliITSRecoParam::kMaxClusterPerLayer10; j++) {
+    for (Int_t j1=0; j1<11; j1++) {
+      fClusters10[j1][j]=0;
+      fClusterIndex10[j1][j]=-1;
+      fY10[j1][j]=0;
+      fZ10[j1][j]=0;
+      fN10[j1]=0;
+      fBy10[j1][0]=0;
+      fBy10[j1][1]=0;
+    }
+  }
+
+  for (Int_t j=0; j<AliITSRecoParam::kMaxClusterPerLayer20; j++) {
+    for (Int_t j1=0; j1<21; j1++) {
+      fClusters20[j1][j]=0;
+      fClusterIndex20[j1][j]=-1;
+      fY20[j1][j]=0;
+      fZ20[j1][j]=0;
+      fN20[j1]=0;
+      fBy20[j1][0]=0;
+      fBy20[j1][1]=0;
+    }
+  }
+  for(Int_t i=0;i<AliITSRecoParam::kMaxClusterPerLayer;i++){
+    fClusters[i]=NULL;
+    fClusterIndex[i]=0;
   }
 }
 //------------------------------------------------------------------------
@@ -1485,7 +1621,61 @@ fNMaxSigmaCl(3) {
   //--------------------------------------------------------------------
   fDetectors=new AliITSdetector[fNladders*fNdetectors];
   fRoad=2*fR*TMath::Sqrt(TMath::Pi()/1.);//assuming that there's only one cluster
+
+  for (Int_t i=0; i<AliITSRecoParam::GetMaxClusterPerLayer(); i++) {
+    fClusterWeight[i]=0;
+    fClusterTracks[0][i]=-1;
+    fClusterTracks[1][i]=-1;
+    fClusterTracks[2][i]=-1;    
+    fClusterTracks[3][i]=-1;    
+    fY[i]=0;    
+    fZ[i]=0;    
+  }
+
+  fYB[0]=0;
+  fYB[1]=0;
+
+ for (Int_t j=0; j<AliITSRecoParam::kMaxClusterPerLayer5; j++) {
+    for (Int_t j1=0; j1<6; j1++) {
+      fClusters5[j1][j]=0;
+      fClusterIndex5[j1][j]=-1;
+      fY5[j1][j]=0;
+      fZ5[j1][j]=0;
+      fN5[j1]=0;
+      fBy5[j1][0]=0;
+      fBy5[j1][1]=0;
+    }
+  }
+
+  for (Int_t j=0; j<AliITSRecoParam::kMaxClusterPerLayer10; j++) {
+    for (Int_t j1=0; j1<11; j1++) {
+      fClusters10[j1][j]=0;
+      fClusterIndex10[j1][j]=-1;
+      fY10[j1][j]=0;
+      fZ10[j1][j]=0;
+      fN10[j1]=0;
+      fBy10[j1][0]=0;
+      fBy10[j1][1]=0;
+    }
+  }
+
+  for (Int_t j=0; j<AliITSRecoParam::kMaxClusterPerLayer20; j++) {
+    for (Int_t j1=0; j1<21; j1++) {
+      fClusters20[j1][j]=0;
+      fClusterIndex20[j1][j]=-1;
+      fY20[j1][j]=0;
+      fZ20[j1][j]=0;
+      fN20[j1]=0;
+      fBy20[j1][0]=0;
+      fBy20[j1][1]=0;
+    }
+  }
+  for(Int_t i=0;i<AliITSRecoParam::kMaxClusterPerLayer;i++){
+    fClusters[i]=NULL;
+    fClusterIndex[i]=0;
+  }
 }
+/*
 //------------------------------------------------------------------------
 AliITStrackerMI::AliITSlayer::AliITSlayer(const AliITSlayer& layer):
 fR(layer.fR),
@@ -1519,6 +1709,7 @@ fNMaxSigmaCl(layer.fNMaxSigmaCl)
 {
   //Copy constructor
 }
+*/
 //------------------------------------------------------------------------
 AliITStrackerMI::AliITSlayer::~AliITSlayer() {
   //--------------------------------------------------------------------
@@ -1586,7 +1777,6 @@ Int_t AliITStrackerMI::AliITSlayer::InsertCluster(AliITSRecPoint *cl) {
   //This function adds a cluster to this layer
   //--------------------------------------------------------------------
   if (fN==AliITSRecoParam::GetMaxClusterPerLayer()) {
-    ::Error("InsertCluster","Too many clusters !\n");
     return 1;
   }
   fCurrentSlice=-1;
@@ -1910,16 +2100,25 @@ FindDetectorIndex(Double_t phi, Double_t z) const {
   Double_t dz=fZOffset-z;
   Double_t nnz = dz*(fNdetectors-1)*0.5/fZOffset+0.5;
   Int_t nz = (nnz<0 ? -1 : (Int_t)nnz);
-  if (nz>=fNdetectors) return -1;
-  if (nz<0)            return -1;
+  if (nz>=fNdetectors || nz<0) {
+    //printf("ndet %d phi %f z %f  np %d nz %d\n",fNdetectors,phi,z,np,nz);
+    return -1;
+  }
 
   // ad hoc correction for 3rd ladder of SDD inner layer,
   // which is reversed (rotated by pi around local y)
   // this correction is OK only from AliITSv11Hybrid onwards
   if (GetR()>12. && GetR()<20.) { // SDD inner
     if(np==2) { // 3rd ladder
-      nz = (fNdetectors-1) - nz;
-    } 
+      Double_t posMod252[3];
+      AliITSgeomTGeo::GetTranslation(252,posMod252);
+      // check the Z coordinate of Mod 252: if negative 
+      // (old SDD geometry in AliITSv11Hybrid)
+      // the swap of numeration whould be applied
+      if(posMod252[2]<0.){
+       nz = (fNdetectors-1) - nz;
+      } 
+    }
   }
   //printf("ndet %d phi %f z %f  np %d nz %d\n",fNdetectors,phi,z,np,nz);
 
@@ -1948,7 +2147,7 @@ const AliITSRecPoint *AliITStrackerMI::AliITSlayer::GetNextCluster(Int_t &ci,Boo
       if (z+fNMaxSigmaCl*TMath::Sqrt(fClusters[i]->GetSigmaZ2())<fZmin+fNMaxSigmaCl*fMaxSigmaClZ) continue;
       if (z-fNMaxSigmaCl*TMath::Sqrt(fClusters[i]->GetSigmaZ2())>fZmax-fNMaxSigmaCl*fMaxSigmaClZ) continue;
       //
-      if (fClusters[i]->GetQ()==0&&fSkip==2) continue;
+      if (TMath::Abs(fClusters[i]->GetQ())<1.e-13 && fSkip==2) continue;
       ci=i;
       if (!test) fI=i+1;
       return fClusters[i];
@@ -1957,7 +2156,7 @@ const AliITSRecPoint *AliITStrackerMI::AliITSlayer::GetNextCluster(Int_t &ci,Boo
     for (Int_t i=fI; i<fImax; i++) {
       if (fYcs[i]<fYmin) continue;
       if (fYcs[i]>fYmax) continue;
-      if (fClustersCs[i]->GetQ()==0&&fSkip==2) continue;
+      if (TMath::Abs(fClustersCs[i]->GetQ())<1.e-13 && fSkip==2) continue;
       ci=fClusterIndexCs[i];
       if (!test) fI=i+1;
       return fClustersCs[i];
@@ -2170,6 +2369,46 @@ Double_t AliITStrackerMI::GetEffectiveThickness()
   }
   return d/(xn*xn);
 }
+
+//------------------------------------------------------------------------
+Int_t AliITStrackerMI::GetEffectiveThicknessLbyL(Double_t* xMS, Double_t* x2x0MS)
+{
+  //--------------------------------------------------------------------
+  // Returns the array of layers between the current layer and the vertex
+  //--------------------------------------------------------------------
+  //
+  if(fUseTGeo!=0) {
+    if(fxOverX0Layer[0]<0) BuildMaterialLUT("Layers");
+    if(fxOverX0Shield[0]<0) BuildMaterialLUT("Shields");
+    if(fxOverX0Pipe<0) BuildMaterialLUT("Pipe");
+  }
+
+  int nl = 0;
+  double x0 = 0;
+  for (int il=fI;il--;) {
+    //
+    if (il==3) {
+      x2x0MS[nl] = (fUseTGeo==0 ? AliITSRecoParam::Getdshield(1) : fxOverX0Shield[1]);
+      xMS[nl++]  = AliITSRecoParam::GetrInsideShield(1);
+    }
+    else if (il==1) {
+      x2x0MS[nl] = (fUseTGeo==0 ? AliITSRecoParam::Getdshield(0) : fxOverX0Shield[0]);
+      xMS[nl++]  = AliITSRecoParam::GetrInsideShield(0);
+    }
+    //
+    x2x0MS[nl] = (fUseTGeo==0 ? fgLayers[il].GetThickness(0,0,x0) : fxOverX0Layer[il]);
+    xMS[nl++]  = fgLayers[il].GetR();
+    //
+  }
+  //
+  // beam pipe
+  x2x0MS[nl]  = (fUseTGeo==0 ? AliITSRecoParam::GetdPipe() : fxOverX0Pipe);
+  xMS[nl++]  = AliITSRecoParam::GetrPipe();
+  //
+  return nl;
+}
+
+
 //------------------------------------------------------------------------
 Int_t AliITStrackerMI::AliITSlayer::InRoad() const {
   //-------------------------------------------------------------------
@@ -2237,14 +2476,32 @@ Bool_t AliITStrackerMI::RefitAt(Double_t xx,AliITStrackMI *track,
     index[k]=clusters[k]; 
   }
 
-  // special for cosmics: check which the innermost layer crossed
-  // by the track
-  Int_t innermostlayer=5;
-  Double_t drphi = TMath::Abs(track->GetD(0.,0.));
-  for(innermostlayer=0; innermostlayer<AliITSgeomTGeo::GetNLayers(); innermostlayer++) {
-    if(drphi < fgLayers[innermostlayer].GetR()) break;
+  // special for cosmics and TPC prolonged tracks: 
+  // propagate to the innermost of:
+  // - innermost layer crossed by the track
+  // - innermost layer where a cluster was associated to the track
+  static AliITSRecoParam *repa = NULL;
+  if(!repa){
+    repa = (AliITSRecoParam*) AliITSReconstructor::GetRecoParam();
+    if(!repa){
+      repa = AliITSRecoParam::GetHighFluxParam();
+      AliWarning("Using default AliITSRecoParam class");
+    }
+  }
+  Int_t evsp=repa->GetEventSpecie();
+  ULong_t trStatus=0;
+  if(track->GetESDtrack()) trStatus=track->GetStatus();
+  Int_t innermostlayer=0;
+  if((evsp&AliRecoParam::kCosmic) || (trStatus&AliESDtrack::kTPCin))  {
+    innermostlayer=5;
+    Double_t drphi = TMath::Abs(track->GetD(0.,0.));
+    for(innermostlayer=0; innermostlayer<AliITSgeomTGeo::GetNLayers(); innermostlayer++) {
+      if( (drphi < (fgLayers[innermostlayer].GetR()+1.)) ||
+         index[innermostlayer] >= 0 ) break;
+    }
+
+    AliDebug(2,Form(" drphi  %f  innermost %d",drphi,innermostlayer));
   }
-  AliDebug(2,Form(" drphi  %f  innermost %d",drphi,innermostlayer));
 
   Int_t modstatus=1; // found
   Float_t xloc,zloc;
@@ -2303,6 +2560,9 @@ Bool_t AliITStrackerMI::RefitAt(Double_t xx,AliITStrackMI *track,
 
 
      const AliITSdetector &det=layer.GetDetector(idet);
+     // only for ITS-SA tracks refit
+     if (ilayer>1 && fTrackingPhase.Contains("RefitInward") && !(track->GetStatus()&AliESDtrack::kTPCin)) track->SetCheckInvariant(kFALSE);
+     // 
      if (!track->Propagate(det.GetPhi(),det.GetR())) return kFALSE;
 
      track->SetDetectorIndex(idet);
@@ -2362,7 +2622,7 @@ Bool_t AliITStrackerMI::RefitAt(Double_t xx,AliITStrackMI *track,
      track->SetModuleIndexInfo(ilayer,idet,modstatus,xloc,zloc);
 
 
-     if (extra) { // search for extra clusters in overlapped modules
+     if (extra && clAcc) { // search for extra clusters in overlapped modules
        AliITStrackV2 tmp(*track);
        if (!ComputeRoad(track,ilayer,idet,zmin,zmax,ymin,ymax)) return kFALSE;
        layer.SelectClusters(zmin,zmax,ymin,ymax);
@@ -2396,7 +2656,7 @@ Bool_t AliITStrackerMI::RefitAt(Double_t xx,AliITStrackMI *track,
      // cross material
      // add time if going outward
      if(!CorrectForLayerMaterial(track,ilayer,oldGlobXYZ,dir)) return kFALSE;
-                 
+     track->SetCheckInvariant(kTRUE);
   } // end loop on layers
 
   if (!track->PropagateTo(xx,0.,0.)) return kFALSE;
@@ -2504,12 +2764,17 @@ Double_t AliITStrackerMI::GetNormalizedChi2(AliITStrackMI * track, Int_t mode)
   // or there is a dead from OCDB)
   Float_t deadzonefactor = 0.; 
   if (track->GetNDeadZone()>0.) {    
-    Float_t sumDeadZoneProbability=0; 
-    for(Int_t ilay=0;ilay<6;ilay++) sumDeadZoneProbability+=track->GetDeadZoneProbability(ilay);
-    Float_t nDeadZoneWithProbNot1=(Float_t)(track->GetNDeadZone())-sumDeadZoneProbability;
-    if(nDeadZoneWithProbNot1>0.) {
-      Float_t deadZoneProbability = sumDeadZoneProbability - (Float_t)((Int_t)sumDeadZoneProbability);
-      deadZoneProbability /= nDeadZoneWithProbNot1;
+    Int_t sumDeadZoneProbability=0; 
+    for(Int_t ilay=0;ilay<6;ilay++) {
+      if(track->GetDeadZoneProbability(ilay)>0.) sumDeadZoneProbability++;
+    }
+    Int_t nDeadZoneWithProbNot1=(Int_t)(track->GetNDeadZone())-sumDeadZoneProbability;
+    if(nDeadZoneWithProbNot1>0) {
+      Float_t deadZoneProbability = track->GetNDeadZone()-(Float_t)sumDeadZoneProbability;
+      AliDebug(2,Form("nDeadZone %f sumDZProbability %d nDZWithProbNot1 %d deadZoneProb %f\n",track->GetNDeadZone(),sumDeadZoneProbability,nDeadZoneWithProbNot1,deadZoneProbability));
+      deadZoneProbability /= (Float_t)nDeadZoneWithProbNot1;
+      Float_t one = 1.;
+      deadZoneProbability = TMath::Min(deadZoneProbability,one);
       deadzonefactor = 3.*(1.1-deadZoneProbability);  
     }
   }  
@@ -2517,8 +2782,9 @@ Double_t AliITStrackerMI::GetNormalizedChi2(AliITStrackMI * track, Int_t mode)
   Double_t normchi2 = 2*track->GetNSkipped()+match+deadzonefactor+(1+(2*track->GetNSkipped()+deadzonefactor)/track->GetNumberOfClusters())*
     (chi2)/TMath::Max(double(sum-track->GetNSkipped()),
                                1./(1.+track->GetNSkipped()));     
- return normchi2;
+  AliDebug(2,Form("match %f deadzonefactor %f chi2 %f sum %f skipped %f\n",match,deadzonefactor,chi2,sum,track->GetNSkipped()));
+  AliDebug(2,Form("NormChi2 %f  cls %d\n",normchi2,track->GetNumberOfClusters()));
+  return normchi2;
 }
 //------------------------------------------------------------------------
 Double_t AliITStrackerMI::GetMatchingChi2(const AliITStrackMI * track1,const AliITStrackMI * track2)
@@ -2568,30 +2834,46 @@ Double_t  AliITStrackerMI::GetSPDDeadZoneProbability(Double_t zpos, Double_t zer
   //
   //  return probability that given point (characterized by z position and error) 
   //  is in SPD dead zone
+  //     This method assumes that fSPDdetzcentre is ordered from -z to +z
   //
   Double_t probability = 0.;
-  Double_t absz = TMath::Abs(zpos);
-  Double_t nearestz = (absz<2.) ? 0.5*(fSPDdetzcentre[1]+fSPDdetzcentre[2]) : 
-                                  0.5*(fSPDdetzcentre[2]+fSPDdetzcentre[3]);
-  if (TMath::Abs(absz-nearestz)>0.25+3.*zerr) return probability;
+  Double_t nearestz = 0.,distz=0.;
+  Int_t    nearestzone = -1;
+  Double_t mindistz = 1000.;
+
+  // find closest dead zone
+  for (Int_t i=0; i<3; i++) {
+    distz=TMath::Abs(zpos-0.5*(fSPDdetzcentre[i]+fSPDdetzcentre[i+1]));
+    if (distz<mindistz) {
+      nearestzone=i;
+      nearestz=0.5*(fSPDdetzcentre[i]+fSPDdetzcentre[i+1]);
+      mindistz=distz;
+    }
+  }
+
+  // too far from dead zone
+  if (TMath::Abs(zpos-nearestz)>0.25+3.*zerr) return probability;
+
+
   Double_t zmin, zmax;   
-  if (zpos<-6.) { // dead zone at z = -7
+  if (nearestzone==0) { // dead zone at z = -7
     zmin = fSPDdetzcentre[0] + 0.5*AliITSRecoParam::GetSPDdetzlength();
     zmax = fSPDdetzcentre[1] - 0.5*AliITSRecoParam::GetSPDdetzlength();
-  } else if (zpos>6.) { // dead zone at z = +7
-    zmin = fSPDdetzcentre[2] + 0.5*AliITSRecoParam::GetSPDdetzlength();
-    zmax = fSPDdetzcentre[3] - 0.5*AliITSRecoParam::GetSPDdetzlength();
-  } else if (absz<2.) { // dead zone at z = 0
+  } else if (nearestzone==1) { // dead zone at z = 0
     zmin = fSPDdetzcentre[1] + 0.5*AliITSRecoParam::GetSPDdetzlength();
     zmax = fSPDdetzcentre[2] - 0.5*AliITSRecoParam::GetSPDdetzlength();
+  } else if (nearestzone==2) { // dead zone at z = +7
+    zmin = fSPDdetzcentre[2] + 0.5*AliITSRecoParam::GetSPDdetzlength();
+    zmax = fSPDdetzcentre[3] - 0.5*AliITSRecoParam::GetSPDdetzlength();
   } else {
     zmin = 0.;
     zmax = 0.;
   }
   // probability that the true z is in the range [zmin,zmax] (i.e. inside 
   // dead zone)
-  probability = 0.5*( TMath::Erf((zpos-zmin)/zerr/TMath::Sqrt(2.)) - 
-                     TMath::Erf((zpos-zmax)/zerr/TMath::Sqrt(2.)) );
+  probability = 0.5*( AliMathBase::ErfFast((zpos-zmin)/zerr/TMath::Sqrt(2.)) - 
+                     AliMathBase::ErfFast((zpos-zmax)/zerr/TMath::Sqrt(2.)) );
+  AliDebug(2,Form("zpos %f +- %f nearestzone %d  zmin zmax %f %f prob %f\n",zpos,zerr,nearestzone,zmin,zmax,probability));
   return probability;
 }
 //------------------------------------------------------------------------
@@ -2719,9 +3001,9 @@ Float_t AliITStrackerMI::GetNumberOfSharedClusters(AliITStrackMI* track,Int_t id
     Int_t l=(index & 0xf0000000) >> 28;
     Int_t c=(index & 0x0fffffff) >> 00;
     if (c>fgLayers[l].GetNumberOfClusters()) continue;
-    if (ny[l]==0){
-      printf("problem\n");
-    }
+    // if (ny[l]<1.e-13){
+    //   printf("problem\n");
+    // }
     AliITSRecPoint *cl = (AliITSRecPoint*)GetCluster(index);
     Float_t weight=1;
     //
@@ -2742,6 +3024,7 @@ Float_t AliITStrackerMI::GetNumberOfSharedClusters(AliITStrackMI* track,Int_t id
       if (fgLayers[l].GetClusterTracks(itrack,c)>=0 && fgLayers[l].GetClusterTracks(itrack,c)!=id){
        list[l]=index;
        clist[l] = (AliITSRecPoint*)GetCluster(index);
+       track->SetSharedWeight(l,weight);
        shared+=weight; 
        break;
       }
@@ -2769,9 +3052,9 @@ Int_t AliITStrackerMI::GetOverlapTrack(const AliITStrackMI *track, Int_t trackID
     Int_t index = clusterlist[icluster];
     Int_t l=(index & 0xf0000000) >> 28;
     Int_t c=(index & 0x0fffffff) >> 00;
-    if (ny[l]==0){
-      printf("problem\n");
-    }
+    // if (ny[l]<1.e-13){
+    //   printf("problem\n");
+    // }
     if (c>fgLayers[l].GetNumberOfClusters()) continue;
     //if (l>3) continue;
     AliITSRecPoint *cl = (AliITSRecPoint*)GetCluster(index);
@@ -2804,7 +3087,7 @@ Int_t AliITStrackerMI::GetOverlapTrack(const AliITStrackMI *track, Int_t trackID
     else{
       //
       Int_t tracks2[24], cluster[24];
-      for (Int_t i=0;i<trackindex;i++){ tracks2[i]=-1; cluster[i]=0;}
+      for (Int_t i=0;i<24;i++){ tracks2[i]=-1; cluster[i]=0;}
       Int_t index =0;
       //
       for (Int_t i=0;i<trackindex;i++){
@@ -2861,7 +3144,7 @@ Int_t AliITStrackerMI::GetOverlapTrack(const AliITStrackMI *track, Int_t trackID
   return sharedtrack;
 }
 //------------------------------------------------------------------------
-AliITStrackMI *  AliITStrackerMI::GetBest2Tracks(Int_t trackID1, Int_t trackID2, Float_t th0, Float_t th1){
+AliITStrackMI *  AliITStrackerMI::GetBest2Tracks(Int_t trackID1, Int_t trackID2, Float_t th0, Float_t th1,AliITStrackMI* original){
   //
   // try to find track hypothesys without conflicts
   // with minimal chi2;
@@ -2875,7 +3158,7 @@ AliITStrackMI *  AliITStrackerMI::GetBest2Tracks(Int_t trackID1, Int_t trackID2,
   AliITStrackMI * track10=(AliITStrackMI*) arr1->UncheckedAt(0);
   AliITStrackMI * track20=(AliITStrackMI*) arr2->UncheckedAt(0);
   if (track10->Pt()>0.5+track20->Pt()) return track10;
-
+  //
   for (Int_t itrack=0;itrack<entries1;itrack++){
     AliITStrackMI * track=(AliITStrackMI*) arr1->UncheckedAt(itrack);
     UnRegisterClusterTracks(track,trackID1);
@@ -3073,12 +3356,14 @@ AliITStrackMI *  AliITStrackerMI::GetBest2Tracks(Int_t trackID1, Int_t trackID2,
     track1->SetChi2MIP(8,index1);
     fBestTrackIndex[trackID1] =index1;
     UpdateESDtrack(track1, AliESDtrack::kITSin);
+    original->SetWinner(track1);
   }  
   else if (track10->GetChi2MIP(0)<th1){
     track10->SetChi2MIP(5,maxconflicts);
     track10->SetChi2MIP(6,maxchi2);    
     //    track10->UpdateESDtrack(AliESDtrack::kITSin);
     UpdateESDtrack(track10,AliESDtrack::kITSin);
+    original->SetWinner(track10);
   }   
   
   for (Int_t itrack=0;itrack<entries1;itrack++){
@@ -3128,13 +3413,11 @@ void AliITStrackerMI::AddTrackHypothesys(AliITStrackMI * track, Int_t esdindex)
   // add track to the list of hypothesys
   //------------------------------------------------------------------
 
-  if (esdindex>=fTrackHypothesys.GetEntriesFast()) 
-    fTrackHypothesys.Expand(TMath::Max(fTrackHypothesys.GetSize(),esdindex*2+10));
   //
   TObjArray * array = (TObjArray*) fTrackHypothesys.At(esdindex);
   if (!array) {
     array = new TObjArray(10);
-    fTrackHypothesys.AddAt(array,esdindex);
+    fTrackHypothesys.AddAtAndExpand(array,esdindex);
   }
   array->AddLast(track);
 }
@@ -3154,6 +3437,7 @@ void AliITStrackerMI::SortTrackHypothesys(Int_t esdindex, Int_t maxcut, Int_t mo
   Float_t minchi2=10000;
   Int_t maxn=0;
   AliITStrackMI * besttrack=0;
+  //
   for (Int_t itrack=0;itrack<array->GetEntriesFast();itrack++){
     AliITStrackMI * track = (AliITStrackMI*)array->At(itrack);
     if (!track) continue;
@@ -3169,6 +3453,7 @@ void AliITStrackerMI::SortTrackHypothesys(Int_t esdindex, Int_t maxcut, Int_t mo
     if (chi2<AliITSReconstructor::GetRecoParam()->GetMaxChi2PerCluster(0)){
       if (track->GetNumberOfClusters()<maxn) continue;
       maxn = track->GetNumberOfClusters();
+      //      if (fSelectBestMIP03 && track->GetChi2MIP(3)>0) chi2 *= track->GetChi2MIP(3); // RS    
       if (chi2<minchi2){
        minchi2=chi2;
        besttrack=track;
@@ -3203,9 +3488,13 @@ void AliITStrackerMI::SortTrackHypothesys(Int_t esdindex, Int_t maxcut, Int_t mo
   for (Int_t itrack=0;itrack<entries;itrack++){
     AliITStrackMI * track = (AliITStrackMI*)array->At(itrack);
     if (track){
-      track->SetChi2MIP(0,GetNormalizedChi2(track, mode));            
-      if (track->GetChi2MIP(0)<AliITSReconstructor::GetRecoParam()->GetMaxChi2PerCluster(0)) 
-       chi2[itrack] = track->GetChi2MIP(0);
+      AliDebug(2,Form("track %d  ncls %d\n",itrack,track->GetNumberOfClusters()));      
+      double chi2t = GetNormalizedChi2(track, mode);
+      track->SetChi2MIP(0,chi2t);
+      if (chi2t<AliITSReconstructor::GetRecoParam()->GetMaxChi2PerCluster(0)) {
+       if (fSelectBestMIP03 && track->GetChi2MIP(3)>0) chi2t *= track->GetChi2MIP(3); // RS
+       chi2[itrack] = chi2t;
+      }
       else{
        if (track->GetConstrain() || track->GetNumberOfClusters()>5){  //keep best short tracks - without vertex constrain
          delete array->RemoveAt(itrack);            
@@ -3216,6 +3505,7 @@ void AliITStrackerMI::SortTrackHypothesys(Int_t esdindex, Int_t maxcut, Int_t mo
   //
   TMath::Sort(entries,chi2,index,kFALSE);
   besttrack = (AliITStrackMI*)array->At(index[0]);
+  if(besttrack) AliDebug(2,Form("ncls best track %d\n",besttrack->GetNumberOfClusters()));
   if (besttrack&&besttrack->GetChi2MIP(0)<AliITSReconstructor::GetRecoParam()->GetMaxChi2PerCluster(0)){
     for (Int_t j=0;j<6;j++){
       if (besttrack->GetClIndex(j)>=0){
@@ -3232,15 +3522,18 @@ void AliITStrackerMI::SortTrackHypothesys(Int_t esdindex, Int_t maxcut, Int_t mo
   for (Int_t itrack=0;itrack<entries;itrack++){
     AliITStrackMI * track = (AliITStrackMI*)array->At(itrack);
     if (track){      
-      track->SetChi2MIP(0,GetNormalizedChi2(track,mode));            
-      if (track->GetChi2MIP(0)<AliITSReconstructor::GetRecoParam()->GetMaxChi2PerCluster(0)) 
-       chi2[itrack] = track->GetChi2MIP(0)-0*(track->GetNumberOfClusters()+track->GetNDeadZone()); 
-      else
-       {
-         if (track->GetConstrain() || track->GetNumberOfClusters()>5){  //keep best short tracks - without vertex constrain
-           delete array->RemoveAt(itrack);     
-         }
+      double chi2t = GetNormalizedChi2(track, mode);
+      track->SetChi2MIP(0,chi2t);
+      AliDebug(2,Form("track %d  ncls %d\n",itrack,track->GetNumberOfClusters()));            
+      if (track->GetChi2MIP(0)<AliITSReconstructor::GetRecoParam()->GetMaxChi2PerCluster(0)) {
+       if (fSelectBestMIP03 && track->GetChi2MIP(3)>0) chi2t *= track->GetChi2MIP(3); // RS
+       chi2[itrack] = chi2t;  //-0*(track->GetNumberOfClusters()+track->GetNDeadZone()); 
+      }
+      else {
+       if (track->GetConstrain() || track->GetNumberOfClusters()>5){  //keep best short tracks - without vertex constrain
+         delete array->RemoveAt(itrack);       
        }
+      }
     }   
   }
   entries = array->GetEntriesFast();  
@@ -3251,6 +3544,7 @@ void AliITStrackerMI::SortTrackHypothesys(Int_t esdindex, Int_t maxcut, Int_t mo
     TMath::Sort(entries,chi2,index,kFALSE);
     besttrack = (AliITStrackMI*)array->At(index[0]);
     if (besttrack){
+      AliDebug(2,Form("ncls best track %d     %f   %f\n",besttrack->GetNumberOfClusters(),besttrack->GetChi2MIP(0),chi2[index[0]]));
       //
       for (Int_t j=0;j<6;j++){
        if (besttrack->GetNz(j)>0&&besttrack->GetNy(j)>0){
@@ -3311,6 +3605,7 @@ AliITStrackMI * AliITStrackerMI::GetBestHypothesys(Int_t esdindex, AliITStrackMI
   //-------------------------------------------------------------
   // try to find best hypothesy
   // currently - minimal chi2 of track+backpropagated track+matching to the tpc track
+  // RS: optionally changing this to product of Chi2MIP(0)*Chi2MIP(3) == (chi2*chi2_interpolated)
   //-------------------------------------------------------------
   if (fTrackHypothesys.GetEntriesFast()<=esdindex) return 0;
   TObjArray * array = (TObjArray*) fTrackHypothesys.At(esdindex);
@@ -3341,7 +3636,10 @@ AliITStrackMI * AliITStrackerMI::GetBestHypothesys(Int_t esdindex, AliITStrackMI
     backtrack = new(backtrack) AliITStrackMI(*track); 
     if (track->GetConstrain()) {
       if (!CorrectForPipeMaterial(backtrack,"inward")) continue;
-      if (!backtrack->Improve(0,xyzVtx,ersVtx))         continue;     
+      if (AliITSReconstructor::GetRecoParam()->GetImproveWithVertex()) {
+       if (fUseImproveKalman) {if (!backtrack->ImproveKalman(xyzVtx,ersVtx,0,0,0)) continue;}
+       else                   {if (!backtrack->Improve(0,xyzVtx,ersVtx)) continue;}
+      }
       backtrack->ResetCovariance(10.);      
     }else{
       backtrack->ResetCovariance(10.);
@@ -3367,7 +3665,7 @@ AliITStrackMI * AliITStrackerMI::GetBestHypothesys(Int_t esdindex, AliITStrackMI
     forwardtrack = new(forwardtrack) AliITStrackMI(*original);
     forwardtrack->ResetClusters();
     x = track->GetX();
-    RefitAt(x,forwardtrack,track);
+    if (!RefitAt(x,forwardtrack,track) && fSelectBestMIP03) continue;  // w/o fwd track MIP03 is meaningless
     track->SetChi2MIP(2,NormalizedChi2(forwardtrack,0));    
     if  (track->GetChi2MIP(2)>AliITSReconstructor::GetRecoParam()->GetMaxChi2PerCluster(2)*6.0)  continue;
     if  (!(track->GetConstrain())&&track->GetChi2MIP(2)>AliITSReconstructor::GetRecoParam()->GetMaxChi2PerCluster(2))  continue;
@@ -3390,7 +3688,9 @@ AliITStrackMI * AliITStrackerMI::GetBestHypothesys(Int_t esdindex, AliITStrackMI
       track->SetChi2MIP(3,1000);
       continue; 
     }
-    Double_t chi2 = track->GetChi2MIP(0)+track->GetNUsed();    
+    Double_t chi2 = track->GetChi2MIP(0); // +track->GetNUsed();    //RS
+    if (fSelectBestMIP03) chi2 *= track->GetChi2MIP(3);
+    else chi2 += track->GetNUsed();
     //
     for (Int_t ichi=0;ichi<5;ichi++){
       forwardtrack->SetChi2MIP(ichi, track->GetChi2MIP(ichi));
@@ -3408,6 +3708,9 @@ AliITStrackMI * AliITStrackerMI::GetBestHypothesys(Int_t esdindex, AliITStrackMI
   }
   delete backtrack;
   delete forwardtrack;
+
+  if (!besttrack)  return 0;
+
   Int_t accepted=0;
   for (Int_t i=0;i<entries;i++){    
     AliITStrackMI * track = (AliITStrackMI*)array->At(i);
@@ -3415,8 +3718,10 @@ AliITStrackMI * AliITStrackerMI::GetBestHypothesys(Int_t esdindex, AliITStrackMI
     if (!track) continue;
     
     if (accepted>checkmax || track->GetChi2MIP(3)>AliITSReconstructor::GetRecoParam()->GetMaxChi2PerCluster(3)*6. || 
-       (track->GetNumberOfClusters()<besttrack->GetNumberOfClusters()-1.)||
-       track->GetChi2MIP(0)>besttrack->GetChi2MIP(0)+2.*besttrack->GetNUsed()+3.){
+       (track->GetNumberOfClusters()<besttrack->GetNumberOfClusters()-1.)
+       // RS: don't apply this cut when fSelectBestMIP03 is on
+       || (!fSelectBestMIP03 && (track->GetChi2MIP(0)>besttrack->GetChi2MIP(0)+2.*besttrack->GetNUsed()+3.))
+       ){
       if (track->GetConstrain() || track->GetNumberOfClusters()>5){  //keep best short tracks - without vertex constrain
        delete array->RemoveAt(i);    
        continue;
@@ -3450,6 +3755,9 @@ AliITStrackMI * AliITStrackerMI::GetBestHypothesys(Int_t esdindex, AliITStrackMI
     longtrack =track;
   }
   //if (longtrack) besttrack=longtrack;
+  //
+  // RS do shared cluster analysis here only if the new sharing analysis is not requested
+  //RRR if (fFlagFakes) return besttrack;
 
   Int_t list[6];
   AliITSRecPoint * clist[6];
@@ -3466,7 +3774,7 @@ AliITStrackMI * AliITStrackerMI::GetBestHypothesys(Int_t esdindex, AliITStrackMI
     Int_t sharedtrack = GetOverlapTrack(besttrack, esdindex, nshared, list, overlist);
     if (sharedtrack>=0){
       //
-      besttrack = GetBest2Tracks(esdindex,sharedtrack,10,5.5);     
+      besttrack = GetBest2Tracks(esdindex,sharedtrack,10,5.5,original);     
       if (besttrack){
        shared = GetNumberOfSharedClusters(besttrack,esdindex,list,clist);
       }
@@ -3547,13 +3855,15 @@ void  AliITStrackerMI::GetBestHypothesysMIP(TObjArray &itsTracks)
       }
       if (trackHyp->GetNumberOfClusters()+trackHyp->GetNDeadZone()<minn) continue;
       Float_t chi2 = trackHyp->GetChi2MIP(0);
-      if (fAfterV0){
+      if (fSelectBestMIP03) chi2 *= trackHyp->GetChi2MIP(3);
+      if (trackHyp->GetNumberOfClusters()+trackHyp->GetNDeadZone()>minn) maxchi2 = chi2; //trackHyp->GetChi2MIP(0);
+      //
+      if (fAfterV0){ // ??? RS
        if (!trackHyp->GetGoldV0()&&trackHyp->GetConstrain()==kFALSE) chi2+=5;
       }
-      if (trackHyp->GetNumberOfClusters()+trackHyp->GetNDeadZone()>minn) maxchi2 = trackHyp->GetChi2MIP(0);       
-      //
       if (chi2 > maxchi2) continue;
-      minn= trackHyp->GetNumberOfClusters()+trackHyp->GetNDeadZone();
+      minn = trackHyp->GetNumberOfClusters()+trackHyp->GetNDeadZone();
+      if (fSelectBestMIP03) minn++; // allow next to longest to win
       maxchi2 = chi2;
       longtrack=trackHyp;
     }    
@@ -3580,31 +3890,168 @@ void  AliITStrackerMI::GetBestHypothesysMIP(TObjArray &itsTracks)
        Int_t sharedtrack = GetOverlapTrack(longtrack, i, nshared, list, overlist);
        //if (sharedtrack==-1) sharedtrack=0;
        if (sharedtrack>=0) {       
-         besttrack = GetBest2Tracks(i,sharedtrack,10,5.5);                       
+         besttrack = GetBest2Tracks(i,sharedtrack,10,5.5,track);                         
        }
       }   
       if (besttrack&&fAfterV0) {
        UpdateESDtrack(besttrack,AliESDtrack::kITSin);
+       track->SetWinner(besttrack);
       }
-      if (besttrack&&fConstraint[fPass]) 
-       UpdateESDtrack(besttrack,AliESDtrack::kITSin);
-      if (besttrack->GetChi2MIP(0)+besttrack->GetNUsed()>1.5 && fConstraint[fPass]) {
-       if ( TMath::Abs(besttrack->GetD(0))>0.1 || 
-            TMath::Abs(besttrack->GetD(1))>0.1 ) track->SetReconstructed(kFALSE);      
-      }       
-
-    }    
+      if (besttrack) {
+       if (fConstraint[fPass]) {
+         UpdateESDtrack(besttrack,AliESDtrack::kITSin);
+         track->SetWinner(besttrack);
+       }
+       if (besttrack->GetChi2MIP(0)+besttrack->GetNUsed()>1.5 && fConstraint[fPass]) {
+         if ( TMath::Abs(besttrack->GetD(0))>0.1 || 
+              TMath::Abs(besttrack->GetD(1))>0.1 ) track->SetReconstructed(kFALSE);    
+       }       
+      }
+    }
   }
 } 
+
+//------------------------------------------------------------------------
+void AliITStrackerMI::FlagFakes(const TObjArray &itsTracks)
+{
+  //
+  // RS: flag those tracks which are suxpected to have fake clusters
+  //
+  const double kThreshPt = 0.5;
+  AliRefArray *refArr[6];
+  //
+  for (int i=0;i<6;i++) {
+    int ncl = fgLayers[i].GetNumberOfClusters();
+    refArr[i] = new AliRefArray(ncl,TMath::Min(ncl,1000));
+  }
+  Int_t nentries = itsTracks.GetEntriesFast();
+  //
+  // fill cluster->track associations
+  for (Int_t itr=0;itr<nentries;itr++){
+    AliITStrackMI* track = (AliITStrackMI*)itsTracks.UncheckedAt(itr);   
+    if (!track) continue;
+    AliITStrackMI* trackITS = track->GetWinner();
+    if (!trackITS) continue;
+    for (int il=trackITS->GetNumberOfClusters();il--;) {
+      int idx = trackITS->GetClusterIndex(il);
+      Int_t l=(idx & 0xf0000000) >> 28, c=(idx & 0x0fffffff) >> 00;
+      //      if (c>fgLayers[l].GetNumberOfClusters()) continue;
+      refArr[l]->AddReference(c, itr);
+    }
+  }
+  //
+  const UInt_t kMaxRef = 100;
+  UInt_t crefs[kMaxRef];
+  Int_t ncrefs=0;
+  // process tracks with shared clusters
+  for (int itr=0;itr<nentries;itr++){
+    AliITStrackMI* track0 = (AliITStrackMI*)itsTracks.UncheckedAt(itr);  
+    AliITStrackMI* trackH0 = track0->GetWinner(); 
+    if (!trackH0) continue;
+    AliESDtrack* esd0 = track0->GetESDtrack();
+    //
+    for (int il=0;il<trackH0->GetNumberOfClusters();il++) {
+      int idx = trackH0->GetClusterIndex(il);
+      Int_t l=(idx & 0xf0000000) >> 28, c=(idx & 0x0fffffff) >> 00;
+      ncrefs = refArr[l]->GetReferences(c,crefs,kMaxRef);                
+      if (ncrefs<2) continue; // there will be always self-reference, for sharing needs at least 2
+      esd0->SetITSSharedFlag(l); 
+      for (int ir=ncrefs;ir--;) {
+       if (int(crefs[ir]) <= itr) continue; // ==:selfreference, <: the same pair will be checked with >
+       AliITStrackMI* track1 = (AliITStrackMI*)itsTracks.UncheckedAt(crefs[ir]);
+       AliITStrackMI* trackH1 = track1->GetWinner(); 
+       AliESDtrack* esd1 = track1->GetESDtrack();
+       esd1->SetITSSharedFlag(l);
+       //
+       double pt0 = trackH0->Pt(), pt1 = trackH1->Pt(), res = 0.;      
+       if      (pt0>kThreshPt && pt0-pt1>0.2+0.2*(pt0-kThreshPt) ) res = -100;
+       else if (pt1>kThreshPt && pt1-pt0>0.2+0.2*(pt1-kThreshPt) ) res = 100;
+
+       // select the one with smallest chi2's product
+       res += trackH0->GetChi2MIP(0)*trackH0->GetChi2MIP(3); 
+       res -= trackH1->GetChi2MIP(0)*trackH1->GetChi2MIP(3);
+       //
+       if (res<0) esd1->SetITSFakeFlag();  // esd0 is winner
+       else       esd0->SetITSFakeFlag();  // esd1 is winner
+      }
+      //
+    }
+    //
+  }
+  //
+  for (int i=6;i--;) delete refArr[i];
+}
+
+
+
 //------------------------------------------------------------------------
 void AliITStrackerMI::CookLabel(AliITStrackMI *track,Float_t wrong) const {
   //--------------------------------------------------------------------
   //This function "cooks" a track label. If label<0, this track is fake.
   //--------------------------------------------------------------------
+  const int kMaxLbPerCl = 3;
+  int lbID[36],lbStat[36];
+  Int_t nLab=0, nCl = track->GetNumberOfClusters();
+  //
+  //  track->SetLabel(-1);
+  //  track->SetFakeRatio(0);
+  //
+  for (Int_t i=0;i<nCl;i++) { // fill all labels
+    Int_t cindex = track->GetClusterIndex(i);
+    //    Int_t l=(cindex & 0xf0000000) >> 28;
+    AliITSRecPoint *cl = (AliITSRecPoint*)GetCluster(cindex);
+    //
+    for (int imc=0;imc<kMaxLbPerCl;imc++) { // labels within single cluster
+      int trLb = cl->GetLabel(imc);
+      if (trLb<0) break;
+      // search this mc track in already accounted ones
+      int iLab;
+      for (iLab=0;iLab<nLab;iLab++) if (lbID[iLab]==trLb) break;
+      if (iLab<nLab) lbStat[iLab]++;
+      else {
+       lbID[nLab] = trLb;
+       lbStat[nLab++] = 1;
+      }
+    } // loop over given cluster's labels   
+  } // loop over clusters
+  //
+  if (nLab<1) return; // no labels at all
+  //
   Int_t tpcLabel=-1; 
-     
-  if ( track->GetESDtrack())   tpcLabel =  TMath::Abs(track->GetESDtrack()->GetTPCLabel());
+  if (track->GetESDtrack() && track->GetESDtrack()->IsOn(AliESDtrack::kTPCin)){
+    tpcLabel = TMath::Abs(track->GetESDtrack()->GetTPCLabel());
+  }
+  //
+  // find majority label
+  if (nCl && nLab) {
+    int maxLab=0,tpcLabID=-1;
+    for (int ilb=nLab;ilb--;) {
+      int st = lbStat[ilb];
+      if (lbStat[maxLab]<st) maxLab = ilb;
+      if (lbID[ilb] == tpcLabel) tpcLabID = ilb;
+    }
+    // if there is an equal choice, prefer ITS label consistent with TPC label
+    if (tpcLabel>0 && (tpcLabID!=maxLab) && lbStat[maxLab]==lbStat[tpcLabID]) maxLab=tpcLabID;
+                                                                              
+    track->SetFakeRatio(1.-float(lbStat[maxLab])/nCl);
+    track->SetLabel( lbStat[maxLab]>=nCl-wrong ? lbID[maxLab] : -lbID[maxLab]);
+  }
+  //
+}
 
+/*
+//------------------------------------------------------------------------
+void AliITStrackerMI::CookLabel(AliITStrackMI *track,Float_t wrong) const {
+  //--------------------------------------------------------------------
+  //This function "cooks" a track label. If label<0, this track is fake.
+  //--------------------------------------------------------------------
+  Int_t tpcLabel=-1; 
+     
+  if (track->GetESDtrack()){
+    tpcLabel = track->GetESDtrack()->GetTPCLabel();
+    ULong_t trStatus=track->GetESDtrack()->GetStatus();
+    if(!(trStatus&AliESDtrack::kTPCin)) tpcLabel=track->GetLabel(); // for ITSsa tracks
+  }
    track->SetChi2MIP(9,0);
    Int_t nwrong=0;
    for (Int_t i=0;i<track->GetNumberOfClusters();i++){
@@ -3613,9 +4060,8 @@ void AliITStrackerMI::CookLabel(AliITStrackMI *track,Float_t wrong) const {
      AliITSRecPoint *cl = (AliITSRecPoint*)GetCluster(cindex);
      Int_t isWrong=1;
      for (Int_t ind=0;ind<3;ind++){
-       if (tpcLabel>0)
-        if (cl->GetLabel(ind)==tpcLabel) isWrong=0;
-       AliDebug(2,Form("icl %d  ilab %d lab %d",i,ind,cl->GetLabel(ind)));
+       if (cl->GetLabel(ind)==TMath::Abs(tpcLabel)) isWrong=0;
+       //AliDebug(2,Form("icl %d  ilab %d lab %d",i,ind,cl->GetLabel(ind)));
      }
      track->SetChi2MIP(9,track->GetChi2MIP(9)+isWrong*(2<<l));
      nwrong+=isWrong;
@@ -3623,17 +4069,19 @@ void AliITStrackerMI::CookLabel(AliITStrackMI *track,Float_t wrong) const {
    Int_t nclusters = track->GetNumberOfClusters();
    if (nclusters > 0) //PH Some tracks don't have any cluster
      track->SetFakeRatio(double(nwrong)/double(nclusters));
-   if (tpcLabel>0){
-     if (track->GetFakeRatio()>wrong) track->SetLabel(-tpcLabel);
-     else
-       track->SetLabel(tpcLabel);
+   if (tpcLabel>0 && track->GetFakeRatio()>wrong) {
+     track->SetLabel(-tpcLabel);
+   } else {
+     track->SetLabel(tpcLabel);
    }
-   AliDebug(2,Form(" nls %d wrong %d  label %d  tpcLabel %d\n",nclusters,nwrong,track->GetLabel(),tpcLabel));
-   
+   AliDebug(2,Form(" nls %d wrong %d  label %d  tpcLabel %d\n",nclusters,nwrong,track->GetLabel(),tpcLabel)); 
 }
+*/
+
 //------------------------------------------------------------------------
-void AliITStrackerMI::CookdEdx(AliITStrackMI* track)
-{
+void AliITStrackerMI::CookdEdx(AliITStrackMI* track){
+  //
+  // Fill the dE/dx in this track
   //
   track->SetChi2MIP(9,0);
   for (Int_t i=0;i<track->GetNumberOfClusters();i++){
@@ -3666,15 +4114,15 @@ Double_t AliITStrackerMI::GetPredictedChi2MI(AliITStrackMI* track, const AliITSR
   //
   // Compute predicted chi2
   //
+  // Take into account the mis-alignment (bring track to cluster plane)
+  Double_t xTrOrig=track->GetX();
+  if (!track->Propagate(xTrOrig+cluster->GetX())) return 1000.;
   Float_t erry,errz,covyz;
   Float_t theta = track->GetTgl();
   Float_t phi   = track->GetSnp();
-  phi = TMath::Abs(phi)*TMath::Sqrt(1./((1.-phi)*(1.+phi)));
+  phi *= TMath::Sqrt(1./((1.-phi)*(1.+phi)));
   AliITSClusterParam::GetError(layer,cluster,theta,phi,track->GetExpQ(),erry,errz,covyz);
   AliDebug(2,Form(" chi2: tr-cl   %f  %f   tr X %f cl X %f",track->GetY()-cluster->GetY(),track->GetZ()-cluster->GetZ(),track->GetX(),cluster->GetX()));
-  // Take into account the mis-alignment (bring track to cluster plane)
-  Double_t xTrOrig=track->GetX();
-  if (!track->Propagate(xTrOrig+cluster->GetX())) return 1000.;
   AliDebug(2,Form(" chi2: tr-cl   %f  %f   tr X %f cl X %f",track->GetY()-cluster->GetY(),track->GetZ()-cluster->GetZ(),track->GetX(),cluster->GetX()));
   Double_t chi2 = track->GetPredictedChi2MI(cluster->GetY(),cluster->GetZ(),erry,errz,covyz);
   // Bring the track back to detector plane in ideal geometry
@@ -3713,7 +4161,7 @@ Int_t AliITStrackerMI::UpdateMI(AliITStrackMI* track, const AliITSRecPoint* cl,D
     }
   }
 
-  if (cl->GetQ()<=0) return 0;  // ingore the "virtual" clusters
+  if (TMath::Abs(cl->GetQ())<1.e-13) return 0;  // ingore the "virtual" clusters
 
 
   // Take into account the mis-alignment (bring track to cluster plane)
@@ -3747,8 +4195,9 @@ void AliITStrackerMI::GetDCASigma(const AliITStrackMI* track, Float_t & sigmarfi
   //to be paramterized using external parameters in future 
   //
   // 
-  sigmarfi = 0.0040+1.4 *TMath::Abs(track->GetC())+332.*track->GetC()*track->GetC();
-  sigmaz   = 0.0110+4.37*TMath::Abs(track->GetC());
+  Double_t curv=track->GetC();
+  sigmarfi = 0.0040+1.4 *TMath::Abs(curv)+332.*curv*curv;
+  sigmaz   = 0.0110+4.37*TMath::Abs(curv);
 }
 //------------------------------------------------------------------------
 void AliITStrackerMI::SignDeltas(const TObjArray *clusterArray, Float_t vz)
@@ -3830,9 +4279,9 @@ void AliITStrackerMI::UpdateESDtrack(AliITStrackMI* track, ULong_t flags) const
   AliITStrackMI * oldtrack = (AliITStrackMI*)(track->GetESDtrack()->GetITStrack());
   if (oldtrack) delete oldtrack; 
   track->GetESDtrack()->SetITStrack(new AliITStrackMI(*track));
-  if (TMath::Abs(track->GetDnorm(1))<0.000000001){
-    printf("Problem\n");
-  }
+  // if (TMath::Abs(track->GetDnorm(1))<0.000000001){
+  //   printf("Problem\n");
+  // }
 }
 //------------------------------------------------------------------------
 Int_t AliITStrackerMI::GetNearestLayer(const Double_t *xr) const{
@@ -3875,7 +4324,7 @@ void AliITStrackerMI::BuildMaterialLUT(TString material) {
   } else {
     Error("BuildMaterialLUT","Wrong layer name\n");
   }
-
+  const double kAngEps = 1e-4; // tiny slope to avoid tracks strictly normal to Z axis
   for(Int_t imat=ifirst; imat<=ilast; imat++) {
     Double_t param[5]={0.,0.,0.,0.,0.};
     for (Int_t i=0; i<n; i++) {
@@ -3887,8 +4336,9 @@ void AliITStrackerMI::BuildMaterialLUT(TString material) {
       point1[2] = z;
       point2[0] = rmax[imat]*cosphi;
       point2[1] = rmax[imat]*sinphi;
-      point2[2] = z;
+      point2[2] = z+(rmax[imat]-rmin[imat])*kAngEps;
       AliTracker::MeanMaterialBudget(point1,point2,mparam);
+      if (mparam[1]>999) {n--; continue;}  // skip anomalous values in failed propagation
       for(Int_t j=0;j<5;j++) param[j]+=mparam[j];
     }
     for(Int_t j=0;j<5;j++) param[j]/=(Float_t)n;
@@ -4280,14 +4730,15 @@ void AliITStrackerMI::SetForceSkippingOfLayer() {
   //-----------------------------------------------------------------
 
   const AliEventInfo *eventInfo = GetEventInfo();
+  
   for(Int_t l=0; l<AliITSgeomTGeo::kNLayers; l++) {
     fForceSkippingOfLayer[l] = 0;
     // check reco param
     if(AliITSReconstructor::GetRecoParam()->GetLayersToSkip(l)) fForceSkippingOfLayer[l] = 1;
     // check run info
 
-    if(eventInfo) {
+    if(eventInfo && 
+       AliITSReconstructor::GetRecoParam()->GetSkipSubdetsNotInTriggerCluster()) {
       AliDebug(2,Form("GetEventInfo->GetTriggerCluster: %s",eventInfo->GetTriggerCluster()));
       if(l==0 || l==1)  {
        if(!strstr(eventInfo->GetTriggerCluster(),"ITSSPD")) fForceSkippingOfLayer[l] = 1;
@@ -4308,17 +4759,22 @@ Int_t AliITStrackerMI::CheckSkipLayer(const AliITStrackMI *track,
   // without clusters, because we want to skip the layer.
   // In this case the return value is > 0:
   // return 1: the user requested to skip a layer
-  // return 2: track outside z acceptance of SSD/SDD and will cross both SPD
+  // return 2: track outside z acceptance
   //-----------------------------------------------------------------
 
   if (ForceSkippingOfLayer(ilayer)) return 1;
 
-  if (idet<0 && ilayer>1 && AliITSReconstructor::GetRecoParam()->GetExtendedEtaAcceptance()) {
+  Int_t innerLayCanSkip=0; // was 2, changed on 05.11.2009
+
+  if (idet<0 &&  // out in z
+      ilayer>innerLayCanSkip && 
+      AliITSReconstructor::GetRecoParam()->GetExtendedEtaAcceptance()) {
     // check if track will cross SPD outer layer
     Double_t phiAtSPD2,zAtSPD2;
     if (track->GetPhiZat(fgLayers[1].GetR(),phiAtSPD2,zAtSPD2)) {
       if (TMath::Abs(zAtSPD2)<2.*AliITSRecoParam::GetSPDdetzlength()) return 2;
     }
+    return 2; // always allow skipping, changed on 05.11.2009
   }
 
   return 0;
@@ -4333,6 +4789,7 @@ Int_t AliITStrackerMI::CheckDeadZone(AliITStrackMI *track,
   // without clusters, because there is a dead zone in the road.
   // In this case the return value is > 0:
   // return 1: dead zone at z=0,+-7cm in SPD
+  //     This method assumes that fSPDdetzcentre is ordered from -z to +z
   // return 2: all road is "bad" (dead or noisy) from the OCDB
   // return 3: at least a chip is "bad" (dead or noisy) from the OCDB
   // return 4: at least a single channel is "bad" (dead or noisy) from the OCDB
@@ -4348,8 +4805,8 @@ Int_t AliITStrackerMI::CheckDeadZone(AliITStrackMI *track,
                          fSPDdetzcentre[3] - 0.5*AliITSRecoParam::GetSPDdetzlength()};
     for (Int_t i=0; i<3; i++)
       if (track->GetZ()-dz<zmaxdead[i] && track->GetZ()+dz>zmindead[i]) {
-       AliDebug(2,Form("crack SPD %d",ilayer));
-       return 1; 
+       AliDebug(2,Form("crack SPD %d track z %f   %f   %f  %f\n",ilayer,track->GetZ(),dz,zmaxdead[i],zmindead[i]));
+       if (GetSPDDeadZoneProbability(track->GetZ(),TMath::Sqrt(track->GetSigmaZ2()))>0.1) return 1; 
       } 
   }
 
@@ -4377,7 +4834,7 @@ Int_t AliITStrackerMI::CheckDeadZone(AliITStrackMI *track,
 
   // check if the road overlaps with bad chips
   Float_t xloc,zloc;
-  LocalModuleCoord(ilayer,idet,track,xloc,zloc);
+  if(!(LocalModuleCoord(ilayer,idet,track,xloc,zloc)))return 0;
   Float_t zlocmin = zloc-dz;
   Float_t zlocmax = zloc+dz;
   Float_t xlocmin = xloc-dy;
@@ -4386,10 +4843,10 @@ Int_t AliITStrackerMI::CheckDeadZone(AliITStrackMI *track,
 
   // check if road goes out of detector
   Bool_t touchNeighbourDet=kFALSE; 
-  if (TMath::Abs(xlocmin)>0.5*detSizeX) {xlocmin=-0.5*detSizeX; touchNeighbourDet=kTRUE;} 
-  if (TMath::Abs(xlocmax)>0.5*detSizeX) {xlocmax=+0.5*detSizeX; touchNeighbourDet=kTRUE;} 
-  if (TMath::Abs(zlocmin)>0.5*detSizeZ) {zlocmin=-0.5*detSizeZ; touchNeighbourDet=kTRUE;} 
-  if (TMath::Abs(zlocmax)>0.5*detSizeZ) {zlocmax=+0.5*detSizeZ; touchNeighbourDet=kTRUE;} 
+  if (TMath::Abs(xlocmin)>0.5*detSizeX) {xlocmin=-0.4999*detSizeX; touchNeighbourDet=kTRUE;} 
+  if (TMath::Abs(xlocmax)>0.5*detSizeX) {xlocmax=+0.4999*detSizeX; touchNeighbourDet=kTRUE;} 
+  if (TMath::Abs(zlocmin)>0.5*detSizeZ) {zlocmin=-0.4999*detSizeZ; touchNeighbourDet=kTRUE;} 
+  if (TMath::Abs(zlocmax)>0.5*detSizeZ) {zlocmax=+0.4999*detSizeZ; touchNeighbourDet=kTRUE;} 
   AliDebug(2,Form("layer %d det %d zmim zmax %f %f xmin xmax %f %f   %f %f",ilayer,idet,zlocmin,zlocmax,xlocmin,xlocmax,detSizeZ,detSizeX));
 
   // check if this detector is bad
@@ -4402,6 +4859,7 @@ Int_t AliITStrackerMI::CheckDeadZone(AliITStrackMI *track,
     }
   }
 
+  if(zlocmin>zlocmax)return 0;
   Int_t nChipsInRoad = segm->GetChipsInLocalWindow(chipsInRoad,zlocmin,zlocmax,xlocmin,xlocmax);
   AliDebug(2,Form("lay %d nChipsInRoad %d",ilayer,nChipsInRoad));
   if (!nChipsInRoad) return 0;
@@ -4460,7 +4918,7 @@ Bool_t AliITStrackerMI::LocalModuleCoord(Int_t ilayer,Int_t idet,
   xloc=0.; 
   zloc=0.;
 
-  if(idet<0) return kFALSE;
+  if(idet<0) return kTRUE; // track out of z acceptance of layer
 
   Int_t ndet=AliITSgeomTGeo::GetNDetectors(ilayer+1); // layers from 1 to 6 
 
@@ -4482,7 +4940,8 @@ Bool_t AliITStrackerMI::LocalModuleCoord(Int_t ilayer,Int_t idet,
   return kTRUE;
 }
 //------------------------------------------------------------------------
-Bool_t AliITStrackerMI::IsOKForPlaneEff(const AliITStrackMI* track, const Int_t *clusters, Int_t ilayer) const {
+//------------------------------------------------------------------------
+Bool_t AliITStrackerMI::IsOKForPlaneEff(const AliITStrackMI* track, const Int_t *clusters, Int_t ilayer){
 //
 // Method to be optimized further: 
 // Aim: decide whether a track can be used for PlaneEff evaluation
@@ -4518,19 +4977,30 @@ Bool_t AliITStrackerMI::IsOKForPlaneEff(const AliITStrackMI* track, const Int_t
   AliITStrackMI tmp(*track);
 
 // require a minimal number of cluster in other layers and eventually clusters in closest layers 
-  Int_t ncl=0; 
-  for(Int_t lay=AliITSgeomTGeo::kNLayers-1;lay>ilayer;lay--) {
+  Int_t nclout=0; Int_t nclin=0;
+  for(Int_t lay=AliITSgeomTGeo::kNLayers-1;lay>ilayer;lay--) { // count n. of cluster in outermost layers
+ AliDebug(2,Form("trak=%d  lay=%d  ; index=%d ESD label= %d",tmp.GetLabel(),lay,
+                    tmp.GetClIndex(lay),((AliESDtrack*)tmp.GetESDtrack())->GetLabel())) ;
+   // if (tmp.GetClIndex(lay)>=0) nclout++;
+if(index[lay]>=0)nclout++;
+  }
+  for(Int_t lay=ilayer-1; lay>=0;lay--) { // count n. of cluster in innermost layers
     AliDebug(2,Form("trak=%d  lay=%d  ; index=%d ESD label= %d",tmp.GetLabel(),lay,
                     tmp.GetClIndex(lay),((AliESDtrack*)tmp.GetESDtrack())->GetLabel())) ;
-    if (tmp.GetClIndex(lay)>=0) ncl++;
+   if (index[lay]>=0) nclin++; 
   }
+  Int_t ncl=nclout+nclin;
   Bool_t nextout = kFALSE;
   if(ilayer==AliITSgeomTGeo::kNLayers-1) nextout=kTRUE; // you are already on the outermost layer
   else nextout = ((tmp.GetClIndex(ilayer+1)>=0)? kTRUE : kFALSE );
   Bool_t nextin = kFALSE;
   if(ilayer==0) nextin=kTRUE; // you are already on the innermost layer
   else nextin = ((index[ilayer-1]>=0)? kTRUE : kFALSE );
-  if(ncl<AliITSgeomTGeo::kNLayers-(ilayer+1)-AliITSReconstructor::GetRecoParam()->GetMaxMissingClustersPlaneEff()) 
+  // maximum number of missing clusters allowed in outermost layers
+  if(nclout<AliITSgeomTGeo::kNLayers-(ilayer+1)-AliITSReconstructor::GetRecoParam()->GetMaxMissingClustersOutPlaneEff()) 
+     return kFALSE; 
+  // maximum number of missing clusters allowed (both in innermost and in outermost layers)
+  if(ncl<AliITSgeomTGeo::kNLayers-1-AliITSReconstructor::GetRecoParam()->GetMaxMissingClustersPlaneEff()) 
      return kFALSE; 
   if(AliITSReconstructor::GetRecoParam()->GetRequireClusterInOuterLayerPlaneEff() && !nextout)  return kFALSE;
   if(AliITSReconstructor::GetRecoParam()->GetRequireClusterInInnerLayerPlaneEff() && !nextin)   return kFALSE;
@@ -4560,14 +5030,22 @@ Bool_t AliITStrackerMI::IsOKForPlaneEff(const AliITStrackMI* track, const Int_t
   //***************
   // DEFINITION OF SEARCH ROAD FOR accepting a track
   //
-  //For the time being they are hard-wired, later on from AliITSRecoParam
-  // Double_t nsigx=AliITSRecoParam::GetNSigXFarFromBoundary();
-  // Double_t nsigz=AliITSRecoParam::GetNSigZFarFromBoundary();
-  Double_t nsigz=4; 
-  Double_t nsigx=4; 
-  Double_t dx=nsigx*TMath::Sqrt(tmp.GetSigmaY2());  // those are precisions in the tracking reference system
-  Double_t dz=nsigz*TMath::Sqrt(tmp.GetSigmaZ2());  // Use it also for the module reference system, as it is
-                                                // done for RecPoints
+  Double_t nsigx=AliITSReconstructor::GetRecoParam()->GetNSigXFromBoundaryPlaneEff();
+  Double_t nsigz=AliITSReconstructor::GetRecoParam()->GetNSigZFromBoundaryPlaneEff();
+  Double_t distx=AliITSReconstructor::GetRecoParam()->GetDistXFromBoundaryPlaneEff();
+  Double_t distz=AliITSReconstructor::GetRecoParam()->GetDistZFromBoundaryPlaneEff();
+  Double_t dx=nsigx*TMath::Sqrt(tmp.GetSigmaY2()) + distx;
+  // those are precisions in the tracking reference system
+  Double_t dz=nsigz*TMath::Sqrt(tmp.GetSigmaZ2()) + distz;
+  // Use it also for the module reference system, as it is done for RecPoints
+
+  if(AliITSReconstructor::GetRecoParam()->GetSwitchOnMaxDistNSigFrmBndPlaneEff()){
+   if(nsigx*TMath::Sqrt(tmp.GetSigmaY2())<=distx) dx -= nsigx*TMath::Sqrt(tmp.GetSigmaY2());
+   else dx -= distx;
+
+   if(nsigz*TMath::Sqrt(tmp.GetSigmaZ2())<=distz) dz -= nsigz*TMath::Sqrt(tmp.GetSigmaZ2());
+   else dz -= distz;
+  }
 
   // exclude tracks at boundary between detectors
   //Double_t boundaryWidth=AliITSRecoParam::GetBoundaryWidthPlaneEff();
@@ -4575,11 +5053,32 @@ Bool_t AliITStrackerMI::IsOKForPlaneEff(const AliITStrackMI* track, const Int_t
   AliDebug(2,Form("Tracking: track impact x=%f, y=%f, z=%f",tmp.GetX(), tmp.GetY(), tmp.GetZ()));
   AliDebug(2,Form("Local:    track impact x=%f, z=%f",locx,locz));
   AliDebug(2,Form("Search Road. Tracking: dy=%f , dz=%f",dx,dz));
-
   if ( (locx-dx < blockXmn+boundaryWidth) ||
        (locx+dx > blockXmx-boundaryWidth) ||
        (locz-dz < blockZmn+boundaryWidth) ||
        (locz+dz > blockZmx-boundaryWidth) ) return kFALSE;
+
+  if(ilayer==0){
+       const AliESDEvent *myesd = ((AliESDtrack*)tmp.GetESDtrack())->GetESDEvent();
+       //The beam pipe
+       if (CorrectForPipeMaterial(&tmp,"inward")) {
+          const AliESDVertex* vtx = myesd->GetVertex();
+          if(!vtx) return kFALSE;
+          Double_t ddz[2],cov[3];
+          Double_t maxD=3.;
+          if(!tmp.PropagateToDCA(vtx,AliTracker::GetBz(),maxD,ddz,cov)) return kFALSE;
+          if(TMath::Abs(ddz[0])>=AliITSReconstructor::GetRecoParam()->GetDCACutPlaneEff()) return kFALSE;
+
+          Double_t covar[6]; vtx->GetCovMatrix(covar);
+          Double_t p[2]={tmp.GetParameter()[0]-ddz[0],tmp.GetParameter()[1]-ddz[1]};
+          Double_t c[3]={covar[2],0.,covar[5]};
+          Double_t chi2= ((AliESDtrack*)tmp.GetESDtrack())->GetPredictedChi2(p,c);
+          if (chi2>AliITSReconstructor::GetRecoParam()->GetVertexChi2CutPlaneEff()) return kFALSE;  // Use this to cut on chi^2
+
+       } else return kFALSE;
+    }
+
+
   return kTRUE;
 }
 //------------------------------------------------------------------------
@@ -4658,9 +5157,32 @@ void AliITStrackerMI::UseTrackForPlaneEff(const AliITStrackMI* track, Int_t ilay
     msz *= AliITSReconstructor::GetRecoParam()->GetNSigma2RoadZNonC();
     msy *= AliITSReconstructor::GetRecoParam()->GetNSigma2RoadYNonC();
   }
+
+  if(AliITSReconstructor::GetRecoParam()->GetSwitchOffStdSearchClusPlaneEff()){
+     Double_t nsigx=AliITSReconstructor::GetRecoParam()->GetNSigXSearchClusterPlaneEff();
+     Double_t nsigz=AliITSReconstructor::GetRecoParam()->GetNSigZSearchClusterPlaneEff();
+     Double_t distx=AliITSReconstructor::GetRecoParam()->GetDistXSearchClusterPlaneEff();
+     Double_t distz=AliITSReconstructor::GetRecoParam()->GetDistZSearchClusterPlaneEff();
+     msy = nsigx*TMath::Sqrt(tmp.GetSigmaY2()) + distx;
+     msz = nsigz*TMath::Sqrt(tmp.GetSigmaZ2()) + distz;
+
+  if(AliITSReconstructor::GetRecoParam()->GetSwitchOnMaxDistNSigSrhClusPlaneEff()){
+     if(nsigx*TMath::Sqrt(tmp.GetSigmaY2())<=distx) msy -= nsigx*TMath::Sqrt(tmp.GetSigmaY2());
+     else msy -= distx;
+
+     if(nsigz*TMath::Sqrt(tmp.GetSigmaZ2())<=distz) msz -= nsigz*TMath::Sqrt(tmp.GetSigmaZ2());
+     else msz -= distz;
+  }
+
+  msy *= msy;
+  msz *= msz;
+
+  }
+
+  if(msz==0 || msy==0){AliWarning("UseTrackForPlaneEff: null search frame"); return;}
+    
   msz = 1./msz; // 1/RoadZ^2
   msy = 1./msy; // 1/RoadY^2
-//
 
   const AliITSRecPoint *cl=0; Int_t clidx=-1, ci=-1;
   Int_t idetc=-1;
@@ -4707,10 +5229,36 @@ void AliITStrackerMI::UseTrackForPlaneEff(const AliITStrackMI* track, Int_t ilay
   }
   if(!fPlaneEff->UpDatePlaneEff(found,key))
        AliWarning(Form("UseTrackForPlaneEff: cannot UpDate PlaneEff for key=%d",key));
+
+// this for FO efficiency studies (only for SPD) // 
+   UInt_t keyFO=999999;
+   Bool_t foundFO=kFALSE;
+   if(ilayer<2){ //ONLY SPD layers for FastOr studies
+    TBits mapFO = fkDetTypeRec->GetFastOrFiredMap();
+    Int_t phase = (fEsd->GetBunchCrossNumber())%4;
+    if(!fSPDChipIntPlaneEff[key]){
+      AliITSPlaneEffSPD spd; 
+      keyFO = spd.SwitchChipKeyNumbering(key);
+      if(mapFO.TestBitNumber(keyFO))foundFO=kTRUE;
+       keyFO = key + (AliITSPlaneEffSPD::kNModule*AliITSPlaneEffSPD::kNChip)*(phase+1);
+       if(keyFO<AliITSPlaneEffSPD::kNModule*AliITSPlaneEffSPD::kNChip) {
+         AliWarning(Form("UseTrackForPlaneEff: too small keyF0 (= %d), setting it to 999999",keyFO));
+         keyFO=999999;
+       }
+       if(!fPlaneEff->UpDatePlaneEff(foundFO,keyFO))
+          AliWarning(Form("UseTrackForPlaneEff: cannot UpDate PlaneEff for FastOR for key=%d",keyFO));
+     }
+  }
+  
+
+
   if(fPlaneEff->GetCreateHistos()&&  AliITSReconstructor::GetRecoParam()->GetHistoPlaneEff()) {
     Float_t tr[4]={99999.,99999.,9999.,9999.};    // initialize to high values 
     Float_t clu[4]={-99999.,-99999.,9999.,9999.}; // (in some cases GetCov fails) 
     Int_t cltype[2]={-999,-999};
+                                                          // and the module
+
+    Float_t angleModTrack[3]={99999.,99999.,99999.}; // angles (phi, z and "absolute angle") between the track and the mormal to the module (see below)
 
     tr[0]=locx;
     tr[1]=locz;
@@ -4740,8 +5288,105 @@ void AliITStrackerMI::UseTrackForPlaneEff(const AliITStrackMI* track, Int_t ilay
         clu[3]=TMath::Sqrt(c.GetSigmaZ2());
       //}
     }
-    fPlaneEff->FillHistos(key,found,tr,clu,cltype);
+  // Compute the angles between the track and the module
+      // compute the angle "in phi direction", i.e. the angle in the transverse plane 
+      // between the normal to the module and the projection (in the transverse plane) of the 
+      // track trajectory
+    // tgphi and tglambda of the track in tracking frame with alpha=det.GetPhi
+    Float_t tgl = tmp.GetTgl();
+    Float_t phitr   = tmp.GetSnp();
+    phitr = TMath::ASin(phitr);
+    Int_t volId = AliGeomManager::LayerToVolUIDSafe(ilayer+1 ,idet );
+
+    Double_t tra[3]; AliGeomManager::GetOrigTranslation(volId,tra);
+    Double_t rot[9]; AliGeomManager::GetOrigRotation(volId,rot);
+   Double_t alpha =0.;
+    alpha = tmp.GetAlpha();
+    Double_t phiglob = alpha+phitr;
+    Double_t p[3];
+    p[0] = TMath::Cos(phiglob);
+    p[1] = TMath::Sin(phiglob);
+    p[2] = tgl;
+    TVector3 pvec(p[0],p[1],p[2]);
+    TVector3 normvec(rot[1],rot[4],rot[7]);
+    Double_t angle = pvec.Angle(normvec);
+
+    if(angle>0.5*TMath::Pi()) angle = (TMath::Pi()-angle);
+    angle *= 180./TMath::Pi();
+
+    //Trasverse Plane
+    TVector3 pt(p[0],p[1],0);
+    TVector3 normt(rot[1],rot[4],0);
+    Double_t anglet = pt.Angle(normt);
+
+    Double_t phiPt = TMath::ATan2(p[1],p[0]);
+    if(phiPt<0)phiPt+=2.*TMath::Pi();
+    Double_t phiNorm = TMath::ATan2(rot[4],rot[1]);
+    if(phiNorm<0) phiNorm+=2.*TMath::Pi();
+    if(anglet>0.5*TMath::Pi()) anglet = (TMath::Pi()-anglet);
+    if(phiNorm>phiPt) anglet*=-1.;// pt-->normt  clockwise: anglet>0
+    if((phiNorm-phiPt)>TMath::Pi()) anglet*=-1.;
+    anglet *= 180./TMath::Pi();
+
+     angleModTrack[2]=(Float_t) angle;
+     angleModTrack[0]=(Float_t) anglet;
+     // now the "angle in z" (much easier, i.e. the angle between the z axis and the track momentum + 90)
+    angleModTrack[1]=TMath::ACos(tgl/TMath::Sqrt(tgl*tgl+1.));
+    angleModTrack[1]-=TMath::Pi()/2.; // range of angle is -pi/2 , pi/2
+    angleModTrack[1]*=180./TMath::Pi(); // in degree
+
+    fPlaneEff->FillHistos(key,found,tr,clu,cltype,angleModTrack);
+
+    // For FO efficiency studies of SPD 
+    if(ilayer<2 && !fSPDChipIntPlaneEff[key]) fPlaneEff->FillHistos(keyFO,foundFO,tr,clu,cltype,angleModTrack);
   }
+  if(ilayer<2) fSPDChipIntPlaneEff[key]=kTRUE;
 return;
 }
 
+Int_t AliITStrackerMI::FindClusterOfTrack(int label, int lr, int* store) const //RS
+{
+  // find the MC cluster for the label
+  return fgLayers[lr].FindClusterForLabel(label,store);
+}
+
+/*
+Int_t AliITStrackerMI::GetPattern(const AliITStrackMI* track, char* patt)
+{
+  // creates pattarn of hits marking fake/corrects by f/c. Used for debugging (RS) 
+  strncpy(patt,"......",6); 
+  int tpcLabel = 0;
+  if (track->GetESDtrack()) tpcLabel = track->GetESDtrack()->GetTPCLabel();
+  //
+  int nwrong = 0;
+  for (Int_t i=0;i<track->GetNumberOfClusters();i++){
+    Int_t cindex = track->GetClusterIndex(i);
+    Int_t l=(cindex & 0xf0000000) >> 28;
+    AliITSRecPoint *cl = (AliITSRecPoint*)GetCluster(cindex);
+    Int_t isWrong=1;
+    for (Int_t ind=0;ind<3;ind++) if (cl->GetLabel(ind)==TMath::Abs(tpcLabel)) isWrong=0;
+    patt[l] = isWrong ? 'f':'c';
+    nwrong+=isWrong;
+  }
+  return nwrong;
+}
+*/
+//------------------------------------------------------------------------
+Int_t AliITStrackerMI::AliITSlayer::FindClusterForLabel(Int_t label, Int_t *store) const
+{ //RS
+  //--------------------------------------------------------------------
+
+  int nfound = 0;
+  for (int ic=0;ic<fN;ic++) {
+    const AliITSRecPoint *cl = GetCluster(ic);
+    for (int i=0;i<3;i++) if (cl->GetLabel(i)==label) {
+       if (nfound<50) {
+         if (store) store[nfound] = ic;
+         nfound++;
+       }
+       break;
+      }
+  }
+  return nfound;
+}
+