]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HMPID/AliHMPIDRecon.cxx
Bug in area evaluation for reconstruction of ring. Important for PbPb events (high...
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDRecon.cxx
index 865dd8c5217877ed41704e56fbb6006053b686d7..d9b40bd90d3719e58366d1dbc256936fa5b0f2fa 100644 (file)
 // for single chamber                                                   //
 //////////////////////////////////////////////////////////////////////////
 
-#include "AliHMPIDRecon.h"  //class header
+#include "AliHMPIDRecon.h"   //class header
+#include "AliHMPIDParam.h"   //CkovAngle()
 #include "AliHMPIDCluster.h" //CkovAngle()
-#include <TRotation.h>     //TracePhoton()
-#include <TH1D.h>          //HoughResponse()
-#include <TClonesArray.h>  //CkovAngle()
-#include <AliESDtrack.h>   //CkovAngle()
+#include <TMinuit.h>         //FitEllipse()
+#include <TRotation.h>       //TracePhot()
+#include <TH1D.h>            //HoughResponse()
+#include <TClonesArray.h>    //CkovAngle()
+#include <AliESDtrack.h>     //CkovAngle()
 
 const Double_t AliHMPIDRecon::fgkRadThick=1.5;
 const Double_t AliHMPIDRecon::fgkWinThick=0.5;
 const Double_t AliHMPIDRecon::fgkGapThick=8.0;
-const Double_t AliHMPIDRecon::fgkRadIdx  =1.292;
 const Double_t AliHMPIDRecon::fgkWinIdx  =1.5787;
 const Double_t AliHMPIDRecon::fgkGapIdx  =1.0005;
 
-
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 AliHMPIDRecon::AliHMPIDRecon():TTask("RichRec","RichPat"),
+  fRadNmean(1.292),  
   fPhotCnt(-1),
   fCkovSigma2(0),
   fIsWEIGHT(kFALSE),
@@ -52,99 +53,217 @@ AliHMPIDRecon::AliHMPIDRecon():TTask("RichRec","RichPat"),
     fPhotPhi [i] = -1;
     fPhotWei [i] =  0;
   }
+//hidden algorithm
+  fMipX=fMipY=fThTrkFit=fPhTrkFit=fCkovFit=fMipQ=fRadX=fRadY=-999;
+  fIdxMip=fNClu=0;
+  fCkovSig2=0;
+  for (Int_t i=0; i<100; i++) {
+    fXClu[i] = fYClu[i] = 0;
+    fClCk[i] = kTRUE;
+  }
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-void AliHMPIDRecon::CkovAngle(AliESDtrack *pTrk,TClonesArray *pCluLst)
+void AliHMPIDRecon::CkovAngle(AliESDtrack *pTrk,TClonesArray *pCluLst,Double_t nmean,Double_t qthre)
 {
 // Pattern recognition method based on Hough transform
 // Arguments:   pTrk     - track for which Ckov angle is to be found
 //              pCluLst  - list of clusters for this chamber   
 //   Returns:            - track ckov angle, [rad], 
+    
+  AliHMPIDParam *pParam=AliHMPIDParam::Instance();
   
-  if(pCluLst->GetEntries()>200) fIsWEIGHT = kTRUE; // offset to take into account bkg in reconstruction
-  else                          fIsWEIGHT = kFALSE;
+  if(pCluLst->GetEntries()>pParam->MultCut()) fIsWEIGHT = kTRUE; // offset to take into account bkg in reconstruction
+  else                                        fIsWEIGHT = kFALSE;
 
-  // Photon Flag:  Flag = 0 initial set; Flag = 1 good candidate (charge compatible with photon); Flag = 2 photon used for the ring;
-  Float_t xPc,yPc,th,ph;      pTrk->GetHMPIDtrk(xPc,yPc,th,ph);  SetTrack(xPc,yPc,th,ph); //initialize this track            
+  Float_t xRa,yRa,th,ph;
+  pTrk->GetHMPIDtrk(xRa,yRa,th,ph);        //initialize this track: th and ph angles at middle of RAD 
+  SetTrack(xRa,yRa,th,ph);
+
+  fRadNmean=nmean;
 
-  
-  
   Float_t dMin=999,mipX=-1,mipY=-1;Int_t chId=-1,mipId=-1,mipQ=-1;                                                                           
   fPhotCnt=0;                                                      
   for (Int_t iClu=0; iClu<pCluLst->GetEntriesFast();iClu++){//clusters loop
     AliHMPIDCluster *pClu=(AliHMPIDCluster*)pCluLst->UncheckedAt(iClu);                       //get pointer to current cluster    
     chId=pClu->Ch();
-    if(pClu->Q()>100){                                                                        //charge compartible with MIP clusters      
-      Float_t dX=xPc-pClu->X(),dY=yPc-pClu->Y(),d =TMath::Sqrt(dX*dX+dY*dY);                  //distance between current cluster and intersection point
-      if( d < dMin) {mipId=iClu; dMin=d;mipX=pClu->X();mipY=pClu->Y();mipQ=(Int_t)pClu->Q();}//current cluster is closer, overwrite data for min cluster
-    }else{                                                                                    //charge compartible with photon cluster
-      fPhotCkov[fPhotCnt]=FindPhotCkov(pClu->X(),pClu->Y());                                  //find ckov angle for this  photon candidate
-      fPhotCnt++;         //increment counter of photon candidates
+    if(pClu->Q()>qthre){                                                                      //charge compartible with MIP clusters      
+      Float_t dX=fPc.X()-pClu->X(),dY=fPc.Y()-pClu->Y(),d =TMath::Sqrt(dX*dX+dY*dY);          //distance between current cluster and intersection point
+      if( d < dMin) {mipId=iClu; dMin=d;mipX=pClu->X();mipY=pClu->Y();mipQ=(Int_t)pClu->Q();} //current cluster is closer, overwrite data for min cluster
+    }else{                                                                                    //charge compatible with photon cluster
+      Double_t thetaCer,phiCer;
+      if(FindPhotCkov(pClu->X(),pClu->Y(),thetaCer,phiCer)){                                  //find ckov angle for this  photon candidate
+        fPhotCkov[fPhotCnt]=thetaCer;                                                         //actual theta Cerenkov (in TRS)
+        fPhotPhi [fPhotCnt]=phiCer;                                                           //actual phi   Cerenkov (in TRS): -pi to come back to "unusual" ref system (X,Y,-Z)
+       //PH        Printf("photon n. %i reconstructed theta = %f",fPhotCnt,fPhotCkov[fPhotCnt]);
+        fPhotCnt++;                                                                           //increment counter of photon candidates
+      }
     }
   }//clusters loop
-  Int_t iNacc=FlagPhot(HoughResponse());                                   //flag photons according to individual theta ckov with respect to most probable
-
-                 pTrk->SetHMPIDmip      (mipX,mipY,mipQ,iNacc);                 //store mip info 
+  fMipPos.Set(mipX,mipY);
+  if(fPhotCnt<=3) pTrk->SetHMPIDsignal(kNoPhotAccept);                                        //no reconstruction with <=3 photon candidates
+  Int_t iNacc=FlagPhot(HoughResponse());                                                      //flag photons according to individual theta ckov with respect to most probable
+  pTrk->SetHMPIDmip(mipX,mipY,mipQ,iNacc);                                                    //store mip info 
 
-  if(mipId==-1) {pTrk->SetHMPIDsignal   (kMipQdcCut);  return;}                 //no clusters with QDC more the threshold at all
-  if(dMin>1)    {pTrk->SetHMPIDsignal   (kMipDistCut); return;}                 //closest cluster with enough charge is still too far from intersection
-                 pTrk->SetHMPIDcluIdx(chId,mipId); 
-  if(iNacc<1)    pTrk->SetHMPIDsignal(kNoPhotAccept);                         //no photon candidates is accepted  
-  else           pTrk->SetHMPIDsignal(FindRingCkov(pCluLst->GetEntries()));   //find best Theta ckov for ring i.e. track
-  
-                 pTrk->SetHMPIDchi2(fCkovSigma2);                              //error squared 
+  if(mipId==-1)              {pTrk->SetHMPIDsignal(kMipQdcCut);  return;}                     //no clusters with QDC more the threshold at all
+  if(dMin>pParam->DistCut()) {pTrk->SetHMPIDsignal(kMipDistCut); return;}                     //closest cluster with enough charge is still too far from intersection
+  pTrk->SetHMPIDcluIdx(chId,mipId);                                                           //set index of cluster
+  if(iNacc<1){
+    pTrk->SetHMPIDsignal(kNoPhotAccept);                                                      //no photon candidates is accepted
+  }
+  else {
+    pTrk->SetHMPIDsignal(FindRingCkov(pCluLst->GetEntries()));                                //find best Theta ckov for ring i.e. track
+    pTrk->SetHMPIDchi2(fCkovSigma2);                                                          //errors squared
+  }
 
-}//ThetaCerenkov()
+}//CkovAngle()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Double_t AliHMPIDRecon::FindPhotCkov(Double_t cluX,Double_t cluY)
+Bool_t AliHMPIDRecon::FindPhotCkov(Double_t cluX,Double_t cluY,Double_t &thetaCer,Double_t &phiCer)
 {
 // Finds Cerenkov angle  for this photon candidate
 // Arguments: cluX,cluY - position of cadidate's cluster  
-//   Returns: Cerenkov angle 
+// Returns: Cerenkov angle 
 
-  TVector2 pos(cluX,cluY); Double_t cluR=(pos-fTrkPos).Mod();  Double_t phi=FindPhotPhi(cluX,cluY);      
-  Double_t ckov1=0,ckov2=0.75;
-  const Double_t kTol=0.05; 
+  TVector3 dirCkov;
+  
+  Double_t zRad= -0.5*fgkRadThick-0.5*fgkWinThick;                   //z position of middle of RAD
+  TVector3 rad(fTrkPos.X(),fTrkPos.Y(),zRad);                        //impact point at middle of RAD
+  TVector3  pc(cluX,cluY,0.5*fgkWinThick+fgkGapIdx);                 //mip at PC
+  Double_t cluR = TMath::Sqrt((cluX-fTrkPos.X())*(cluX-fTrkPos.X())+
+                              (cluY-fTrkPos.Y())*(cluY-fTrkPos.Y()));//ref. distance impact RAD-CLUSTER   
+  Double_t phi=(pc-rad).Phi();                                       //phi of photon
+    
+  Double_t ckov1=0;
+  Double_t ckov2=0.75+fTrkDir.Theta();                        //start to find theta cerenkov in DRS
+  const Double_t kTol=0.01;
   Int_t iIterCnt = 0;
   while(1){
-    if(iIterCnt>=50) return -1;
+    if(iIterCnt>=50) return kFALSE;
     Double_t ckov=0.5*(ckov1+ckov2);
-    Double_t dist=cluR-TracePhot(ckov,phi,pos); iIterCnt++;   //get distance between trial point and cluster position
-    if     (dist> kTol) ckov1=ckov;                           //cluster @ larger ckov 
+    dirCkov.SetMagThetaPhi(1,ckov,phi);
+    TVector2 posC=TraceForward(dirCkov);                      //trace photon with actual angles
+    Double_t dist=cluR-(posC-fTrkPos).Mod();                  //get distance between trial point and cluster position
+    if(posC.X()==-999) dist = - 999;                          //total reflection problem
+    iIterCnt++;                                               //counter step
+    if     (dist> kTol) ckov1=ckov;                           //cluster @ larger ckov
     else if(dist<-kTol) ckov2=ckov;                           //cluster @ smaller ckov
-    else                return ckov;                          //precision achived         
+    else{                                                     //precision achived: ckov in DRS found
+      dirCkov.SetMagThetaPhi(1,ckov,phi);                     //
+      RecPhot(dirCkov,thetaCer,phiCer);                       //find ckov (in TRS:the effective Cherenkov angle!)
+      return kTRUE;
+    }
   }
 }//FindPhotTheta()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Double_t AliHMPIDRecon::FindPhotPhi(Double_t cluX,Double_t cluY)
+TVector2 AliHMPIDRecon::TraceForward(TVector3 dirCkov)const
 {
-// Finds phi angle og photon candidate by considering the cluster's position  of this candudate w.r.t track position
-  
-  Double_t emiss=0; 
-  return fPhotPhi[fPhotCnt]=TMath::ATan2(cluY-fTrkPos.Y()-emiss*TMath::Tan(fTrkDir.Theta())*TMath::Sin(fTrkDir.Phi()),
-                                         cluX-fTrkPos.X()-emiss*TMath::Tan(fTrkDir.Theta())*TMath::Cos(fTrkDir.Phi()));
+  //Trace forward a photon from (x,y) up to PC
+  // Arguments: dirCkov photon vector in LORS
+  //   Returns: pos of traced photon at PC
+  TVector2 pos(-999,-999);
+  Double_t thetaCer = dirCkov.Theta();
+  if(thetaCer > TMath::ASin(1./fRadNmean))  return pos;         //total refraction on WIN-GAP boundary
+  Double_t zRad= -0.5*fgkRadThick-0.5*fgkWinThick;              //z position of middle of RAD
+  TVector3  posCkov(fTrkPos.X(),fTrkPos.Y(),zRad);              //RAD: photon position is track position @ middle of RAD 
+  Propagate(dirCkov,posCkov,           -0.5*fgkWinThick);       //go to RAD-WIN boundary  
+  Refract  (dirCkov,         fRadNmean,fgkWinIdx);              //RAD-WIN refraction
+  Propagate(dirCkov,posCkov,            0.5*fgkWinThick);       //go to WIN-GAP boundary
+  Refract  (dirCkov,         fgkWinIdx,fgkGapIdx);              //WIN-GAP refraction
+  Propagate(dirCkov,posCkov,0.5*fgkWinThick+fgkGapThick);       //go to PC
+  pos.Set(posCkov.X(),posCkov.Y());
+  return pos;
+}//TraceForward()
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliHMPIDRecon::RecPhot(TVector3 dirCkov,Double_t &thetaCer,Double_t &phiCer)
+{
+  //Theta Cerenkov reconstruction 
+  // Arguments: (x,y) of initial point in LORS, dirCkov photon vector in LORS
+  //   Returns: thetaCer theta cerenkov reconstructed
+//  TVector3 dirTrk;
+//  dirTrk.SetMagThetaPhi(1,fTrkDir.Theta(),fTrkDir.Phi());
+//  Double_t thetaCer = TMath::ACos(dirCkov*dirTrk);
+  TRotation mtheta;   mtheta.RotateY(- fTrkDir.Theta());
+  TRotation mphi;       mphi.RotateZ(- fTrkDir.Phi());
+  TRotation mrot=mtheta*mphi;
+  TVector3 dirCkovTRS;
+  dirCkovTRS=mrot*dirCkov;
+  phiCer  = dirCkovTRS.Phi();                                          //actual value of the phi of the photon
+  thetaCer= dirCkovTRS.Theta();                                        //actual value of thetaCerenkov of the photon
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Double_t AliHMPIDRecon::FindRingArea(Double_t ckovAng)const
 {
-// Find area inside the cerenkov ring which lays inside PCs
-// Arguments: ckovThe - cernkov    
+// Find area covered in the PC acceptance
+// Arguments: ckovAng - cerenkov angle     
 //   Returns: area of the ring in cm^2 for given theta ckov
    
-  
-  TVector2 pos1,pos2;
-  
   const Int_t kN=100;
+  TVector2 pos1;
   Double_t area=0;
+  Bool_t first=kFALSE;
   for(Int_t i=0;i<kN;i++){
-    TracePhot(ckovAng,Double_t(TMath::TwoPi()*i    /kN),pos1);//trace this photon 
-    TracePhot(ckovAng,Double_t(TMath::TwoPi()*(i+1)/kN),pos2);//trace this photon 
-    area+=(pos1-fTrkPos)*(pos2-fTrkPos);
-      
+   if(!first) {
+     pos1=TracePhot(ckovAng,Double_t(TMath::TwoPi()*(i+1)/kN));                                     //find a good trace for the first photon
+     if(pos1.X()==-999) continue;                                                                   //no area: open ring                 
+     if(!AliHMPIDParam::IsInside(pos1.X(),pos1.Y(),0)) pos1 = IntWithEdge(fMipPos,pos1);            // ffind the very first intersection...
+     first=kTRUE;
+     continue;
+   }
+   TVector2 pos2=TracePhot(ckovAng,Double_t(TMath::TwoPi()*(i+1)/kN));                              //trace the next photon
+   if(pos2.X()==-999) continue;                                                                     //no area: open ring            
+   if(!AliHMPIDParam::IsInside(pos2.X(),pos2.Y(),0)) {
+     pos2 = IntWithEdge(fMipPos,pos2);
+   }
+   area+=TMath::Abs((pos1-fMipPos).X()*(pos2-fMipPos).Y()-(pos1-fMipPos).Y()*(pos2-fMipPos).X());   //add area of the triangle...           
+   pos1 = pos2;
   }
+//---  find points from ring
+  area*=0.5;
   return area;
 }//FindRingArea()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+TVector2 AliHMPIDRecon::IntWithEdge(TVector2 p1,TVector2 p2)const
+{
+// It finds the intersection of the line for 2 points traced as photons
+// and the edge of a given PC
+// Arguments: 2 points obtained tracing the photons
+//   Returns: intersection point with detector (PC) edges
+
+  AliHMPIDParam *pParam = AliHMPIDParam::Instance();
+  
+  Double_t xmin = (p1.X()<p2.X())? p1.X():p2.X(); 
+  Double_t xmax = (p1.X()<p2.X())? p2.X():p1.X(); 
+  Double_t ymin = (p1.Y()<p2.Y())? p1.Y():p2.Y(); 
+  Double_t ymax = (p1.Y()<p2.Y())? p2.Y():p1.Y(); 
+  
+  Double_t m = TMath::Tan((p2-p1).Phi());
+  TVector2 pint;
+  //intersection with low  X
+  pint.Set((Double_t)(p1.X() + (0-p1.Y())/m),0.);
+  pint.Print();
+  if(pint.X()>=0 && pint.X()<=pParam->SizeAllX() &&
+     pint.X()>=xmin && pint.X()<=xmax            &&
+     pint.Y()>=ymin && pint.Y()<=ymax) return pint;
+  //intersection with high X  
+  pint.Set((Double_t)(p1.X() + (pParam->SizeAllY()-p1.Y())/m),(Double_t)(pParam->SizeAllY()));
+  pint.Print();
+  if(pint.X()>=0 && pint.X()<=pParam->SizeAllX() &&
+     pint.X()>=xmin && pint.X()<=xmax            &&
+     pint.Y()>=ymin && pint.Y()<=ymax) return pint;
+  //intersection with left Y  
+  pint.Set(0.,(Double_t)(p1.Y() + m*(0-p1.X())));
+  pint.Print();
+  if(pint.Y()>=0 && pint.Y()<=pParam->SizeAllY() &&
+     pint.Y()>=ymin && pint.Y()<=ymax            &&
+     pint.X()>=xmin && pint.X()<=xmax) return pint;
+  //intersection with righ Y  
+  pint.Set((Double_t)(pParam->SizeAllX()),(Double_t)(p1.Y() + m*(pParam->SizeAllX()-p1.X())));
+  pint.Print();
+  if(pint.Y()>=0 && pint.Y()<=pParam->SizeAllY() &&
+     pint.Y()>=ymin && pint.Y()<=ymax            &&
+     pint.X()>=xmin && pint.X()<=xmax) return pint;
+  return p1;
+}//IntWithEdge()
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Double_t AliHMPIDRecon::FindRingCkov(Int_t)
 {
 // Loops on all Ckov candidates and estimates the best Theta Ckov for a ring formed by those candidates. Also estimates an error for that Theat Ckov
@@ -160,10 +279,10 @@ Double_t AliHMPIDRecon::FindRingCkov(Int_t)
   
   for(Int_t i=0;i<fPhotCnt;i++){//candidates loop
     if(fPhotFlag[i] == 2){
-      if(fPhotCkov[i]<=0) continue;//?????????????????Flag photos = 2 may imply CkovEta = 0?????????????? 
-      if(fPhotCkov[i]<ckovMin) ckovMin=fPhotCkov[i];  //find max and min Theta ckov from all candidates within probable window
+      if(fPhotCkov[i]<ckovMin) ckovMin=fPhotCkov[i];                         //find max and min Theta ckov from all candidates within probable window
       if(fPhotCkov[i]>ckovMax) ckovMax=fPhotCkov[i]; 
-      weightThetaCerenkov += fPhotCkov[i]*fPhotWei[i];   wei += fPhotWei[i];                 //collect weight as sum of all candidate weghts   
+      weightThetaCerenkov += fPhotCkov[i]*fPhotWei[i];
+      wei += fPhotWei[i];                                                    //collect weight as sum of all candidate weghts   
       
       sigma2 += 1./Sigma2(fPhotCkov[i],fPhotPhi[i]);
     }
@@ -172,7 +291,7 @@ Double_t AliHMPIDRecon::FindRingCkov(Int_t)
   if(sigma2>0) fCkovSigma2=1./sigma2;
   else         fCkovSigma2=1e10;  
   
-  if(wei != 0.) weightThetaCerenkov /= wei; else weightThetaCerenkov = 0.;  
+  if(wei != 0.) weightThetaCerenkov /= wei; else weightThetaCerenkov = 0.;
   return weightThetaCerenkov;
 }//FindCkovRing()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -182,6 +301,9 @@ Int_t AliHMPIDRecon::FlagPhot(Double_t ckov)
 // Arguments: ckov- value of most probable ckov angle for track as returned by HoughResponse()
 //   Returns: number of photon candidates happened to be inside the window
 
+// Photon Flag:  Flag = 0 initial set; 
+//               Flag = 1 good candidate (charge compatible with photon); 
+//               Flag = 2 photon used for the ring;
   
   Int_t steps = (Int_t)((ckov )/ fDTheta); //how many times we need to have fDTheta to fill the distance between 0  and thetaCkovHough
 
@@ -193,6 +315,7 @@ Int_t AliHMPIDRecon::FlagPhot(Double_t ckov)
 
   Int_t iInsideCnt = 0; //count photons which Theta ckov inside the window
   for(Int_t i=0;i<fPhotCnt;i++){//photon candidates loop
+    fPhotFlag[i] = 0;
     if(fPhotCkov[i] >= tmin && fPhotCkov[i] <= tmax)   { 
       fPhotFlag[i]=2;    
       iInsideCnt++;
@@ -201,32 +324,22 @@ Int_t AliHMPIDRecon::FlagPhot(Double_t ckov)
   return iInsideCnt;
 }//FlagPhot()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Double_t AliHMPIDRecon::TracePhot(Double_t ckovThe,Double_t ckovPhi,TVector2 &pos)const
+TVector2 AliHMPIDRecon::TracePhot(Double_t ckovThe,Double_t ckovPhi)const
 {
 // Trace a single Ckov photon from emission point somewhere in radiator up to photocathode taking into account ref indexes of materials it travereses
-// Arguments: ckovThe,ckovPhi- photon ckov angles, [rad]  (warning: not photon theta and phi)     
+// Arguments: ckovThe,ckovPhi- photon ckov angles in DRS, [rad]    
 //   Returns: distance between photon point on PC and track projection  
   TRotation mtheta;   mtheta.RotateY(fTrkDir.Theta());
   TRotation mphi;       mphi.RotateZ(fTrkDir.Phi());  
   TRotation mrot=mphi*mtheta;
-  
-  TVector3  posCkov(fTrkPos.X(),fTrkPos.Y(),-0.5*fgkRadThick-fgkWinThick-fgkGapThick);   //RAD: photon position is track position @ middle of RAD 
-  TVector3  dirCkov;   dirCkov.SetMagThetaPhi(1,ckovThe,ckovPhi);                        //initially photon is directed according to requested ckov angle
-                                               dirCkov=mrot*dirCkov;                     //now we know photon direction in LORS
-                       dirCkov.SetPhi(ckovPhi);   
-  if(dirCkov.Theta() > TMath::ASin(1./fgkRadIdx)) return -999;//total refraction on WIN-GAP boundary
-  
-  Propagate(dirCkov,posCkov,-fgkWinThick-fgkGapThick); //go to RAD-WIN boundary  remeber that z=0 is PC plane
-  Refract  (dirCkov,         fgkRadIdx,fgkWinIdx    ); //RAD-WIN refraction
-  Propagate(dirCkov,posCkov,-fgkGapThick           );  //go to WIN-GAP boundary
-  Refract  (dirCkov,         fgkWinIdx,fgkGapIdx    ); //WIN-GAP refraction
-  Propagate(dirCkov,posCkov,0                     );   //go to PC
-  
-  pos.Set(posCkov.X(),posCkov.Y());
-  return (pos-fTrkPos).Mod();
-}//TracePhoton()
+  TVector3  dirCkov,dirCkovTors;   
+
+  dirCkovTors.SetMagThetaPhi(1,ckovThe,ckovPhi);                    //initially photon is directed according to requested ckov angle
+  dirCkov=mrot*dirCkovTors;                                         //now we know photon direction in LORS
+  return TraceForward(dirCkov);
+}//TracePhot()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-void AliHMPIDRecon::Propagate(const TVector3 &dir,TVector3 &pos,Double_t z)const
+void AliHMPIDRecon::Propagate(const TVector3 dir,TVector3 &pos,Double_t z)const
 {
 // Finds an intersection point between a line and XY plane shifted along Z.
 // Arguments:  dir,pos   - vector along the line and any point of the line
@@ -250,14 +363,15 @@ void AliHMPIDRecon::Refract(TVector3 &dir,Double_t n1,Double_t n2)const
 //   Returns: none
 //   On exit: dir is new direction
   Double_t sinref=(n1/n2)*TMath::Sin(dir.Theta());
-  if(sinref>1.)    dir.SetXYZ(-999,-999,-999);
+  if(TMath::Abs(sinref)>1.) dir.SetXYZ(-999,-999,-999);
   else             dir.SetTheta(TMath::ASin(sinref));
 }//Refract()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Double_t AliHMPIDRecon::HoughResponse()
 {
 //
-//
+//    fIdxMip = mipId;
+
 //       
   Double_t kThetaMax=0.75;
   Int_t nChannels = (Int_t)(kThetaMax/fDTheta+0.5);
@@ -273,7 +387,7 @@ Double_t AliHMPIDRecon::HoughResponse()
     Int_t bin = (Int_t)(0.5+angle/(fDTheta));
     Double_t weight=1.;
     if(fIsWEIGHT){
-      Double_t lowerlimit = ((Double_t)bin)*fDTheta - 0.5*fDTheta;  Double_t upperlimit = ((Double_t)bin)*fDTheta + 0.5*fDTheta;   
+      Double_t lowerlimit = ((Double_t)bin)*fDTheta - 0.5*fDTheta;  Double_t upperlimit = ((Double_t)bin)*fDTheta + 0.5*fDTheta;
       Double_t diffArea = FindRingArea(upperlimit)-FindRingArea(lowerlimit);
       if(diffArea>0) weight = 1./diffArea;
     }
@@ -294,7 +408,7 @@ Double_t AliHMPIDRecon::HoughResponse()
 // evaluate the "BEST" theta ckov as the maximum value of histogramm
   Double_t *pVec = resultw->GetArray();
   Int_t locMax = TMath::LocMax(nBin,pVec);
-  phots->Delete();photsw->Delete();resultw->Delete(); // Reset and delete objects
+  delete phots;delete photsw;delete resultw; // Reset and delete objects
   
   return (Double_t)(locMax*fDTheta+0.5*fDTheta); //final most probable track theta ckov   
 }//HoughResponse()
@@ -309,7 +423,10 @@ Double_t AliHMPIDRecon::Sigma2(Double_t ckovTh, Double_t ckovPh)const
 //   Returns: absolute error on Cerenkov angle, [radians]    
   
   TVector3 v(-999,-999,-999);
-  Double_t trkBeta = 1./(TMath::Cos(ckovTh)*fgkRadIdx);
+  Double_t trkBeta = 1./(TMath::Cos(ckovTh)*fRadNmean);
+  
+  if(trkBeta > 1) trkBeta = 1;                 //protection against bad measured thetaCer  
+  if(trkBeta < 0) trkBeta = 0.0001;            //
 
   v.SetX(SigLoc (ckovTh,ckovPh,trkBeta));
   v.SetY(SigGeom(ckovTh,ckovPh,trkBeta));
@@ -326,20 +443,29 @@ Double_t AliHMPIDRecon::SigLoc(Double_t thetaC, Double_t phiC,Double_t betaM)con
 //            dip and azimuthal angles for MIP taken at the entrance to radiator, [radians]        
 //            MIP beta
 //   Returns: absolute error on Cerenkov angle, [radians]    
+  
   Double_t phiDelta = phiC - fTrkDir.Phi();
 
-  Double_t alpha =TMath::Cos(fTrkDir.Theta())-TMath::Tan(thetaC)*TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Theta());
-  Double_t k = 1.-fgkRadIdx*fgkRadIdx+alpha*alpha/(betaM*betaM);
+  Double_t sint     = TMath::Sin(fTrkDir.Theta());
+  Double_t cost     = TMath::Cos(fTrkDir.Theta());
+  Double_t sinf     = TMath::Sin(fTrkDir.Phi());
+  Double_t cosf     = TMath::Cos(fTrkDir.Phi());
+  Double_t sinfd    = TMath::Sin(phiDelta);
+  Double_t cosfd    = TMath::Cos(phiDelta);
+  Double_t tantheta = TMath::Tan(thetaC);
+  
+  Double_t alpha =cost-tantheta*cosfd*sint;                                                 // formula (11)
+  Double_t k = 1.-fRadNmean*fRadNmean+alpha*alpha/(betaM*betaM);                            // formula (after 8 in the text)
   if (k<0) return 1e10;
+  Double_t mu =sint*sinf+tantheta*(cost*cosfd*sinf+sinfd*cosf);                             // formula (10)
+  Double_t e  =sint*cosf+tantheta*(cost*cosfd*cosf-sinfd*sinf);                             // formula (9)
 
-  Double_t mu =TMath::Sin(fTrkDir.Theta())*TMath::Sin(fTrkDir.Phi())+TMath::Tan(thetaC)*(TMath::Cos(fTrkDir.Theta())*TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Phi())+TMath::Sin(phiDelta)*TMath::Cos(fTrkDir.Phi()));
-  Double_t e  =TMath::Sin(fTrkDir.Theta())*TMath::Cos(fTrkDir.Phi())+TMath::Tan(thetaC)*(TMath::Cos(fTrkDir.Theta())*TMath::Cos(phiDelta)*TMath::Cos(fTrkDir.Phi())-TMath::Sin(phiDelta)*TMath::Sin(fTrkDir.Phi()));
-
-  Double_t kk = betaM*TMath::Sqrt(k)/(8*alpha);
-  Double_t dtdxc = kk*(k*(TMath::Cos(phiDelta)*TMath::Cos(fTrkDir.Phi())-TMath::Cos(fTrkDir.Theta())*TMath::Sin(phiDelta)*TMath::Sin(fTrkDir.Phi()))-(alpha*mu/(betaM*betaM))*TMath::Sin(fTrkDir.Theta())*TMath::Sin(phiDelta));
-  Double_t dtdyc = kk*(k*(TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Phi())+TMath::Cos(fTrkDir.Theta())*TMath::Sin(phiDelta)*TMath::Cos(fTrkDir.Phi()))+(alpha* e/(betaM*betaM))*TMath::Sin(fTrkDir.Theta())*TMath::Sin(phiDelta));
+  Double_t kk = betaM*TMath::Sqrt(k)/(fgkGapThick*alpha);                                   // formula (6) and (7)
+  Double_t dtdxc = kk*(k*(cosfd*cosf-cost*sinfd*sinf)-(alpha*mu/(betaM*betaM))*sint*sinfd); // formula (6)           
+  Double_t dtdyc = kk*(k*(cosfd*sinf+cost*sinfd*cosf)+(alpha* e/(betaM*betaM))*sint*sinfd); // formula (7)            pag.4
 
-  return  TMath::Sqrt(0.2*0.2*dtdxc*dtdxc + 0.25*0.25*dtdyc*dtdyc);
+  Double_t errX = 0.2,errY=0.25;                                                            //end of page 7
+  return  TMath::Sqrt(errX*errX*dtdxc*dtdxc + errY*errY*dtdyc*dtdyc);
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Double_t AliHMPIDRecon::SigCrom(Double_t thetaC, Double_t phiC,Double_t betaM)const
@@ -350,12 +476,19 @@ Double_t AliHMPIDRecon::SigCrom(Double_t thetaC, Double_t phiC,Double_t betaM)co
 //            dip and azimuthal angles for MIP taken at the entrance to radiator, [radians]        
 //            MIP beta
 //   Returns: absolute error on Cerenkov angle, [radians]    
+  
   Double_t phiDelta = phiC - fTrkDir.Phi();
-  Double_t alpha =TMath::Cos(fTrkDir.Theta())-TMath::Tan(thetaC)*TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Theta());
 
-  Double_t dtdn = TMath::Cos(fTrkDir.Theta())*fgkRadIdx*betaM*betaM/(alpha*TMath::Tan(thetaC));
+  Double_t sint     = TMath::Sin(fTrkDir.Theta());
+  Double_t cost     = TMath::Cos(fTrkDir.Theta());
+  Double_t cosfd    = TMath::Cos(phiDelta);
+  Double_t tantheta = TMath::Tan(thetaC);
+  
+  Double_t alpha =cost-tantheta*cosfd*sint;                                                 // formula (11)
+  Double_t dtdn = cost*fRadNmean*betaM*betaM/(alpha*tantheta);                              // formula (12)
             
-  Double_t f = 0.00928*(7.75-5.635)/TMath::Sqrt(12.);
+//  Double_t f = 0.00928*(7.75-5.635)/TMath::Sqrt(12.);
+  Double_t f = 0.0172*(7.75-5.635)/TMath::Sqrt(24.);
 
   return f*dtdn;
 }//SigCrom()
@@ -370,21 +503,305 @@ Double_t AliHMPIDRecon::SigGeom(Double_t thetaC, Double_t phiC,Double_t betaM)co
 //   Returns: absolute error on Cerenkov angle, [radians]    
 
   Double_t phiDelta = phiC - fTrkDir.Phi();
-  Double_t alpha =TMath::Cos(fTrkDir.Theta())-TMath::Tan(thetaC)*TMath::Cos(phiDelta)*TMath::Sin(fTrkDir.Theta());
 
-  Double_t k = 1.-fgkRadIdx*fgkRadIdx+alpha*alpha/(betaM*betaM);
+  Double_t sint     = TMath::Sin(fTrkDir.Theta());
+  Double_t cost     = TMath::Cos(fTrkDir.Theta());
+  Double_t sinf     = TMath::Sin(fTrkDir.Phi());
+  Double_t cosfd    = TMath::Cos(phiDelta);
+  Double_t costheta = TMath::Cos(thetaC);
+  Double_t tantheta = TMath::Tan(thetaC);
+  
+  Double_t alpha =cost-tantheta*cosfd*sint;                                                  // formula (11)
+  
+  Double_t k = 1.-fRadNmean*fRadNmean+alpha*alpha/(betaM*betaM);                             // formula (after 8 in the text)
   if (k<0) return 1e10;
 
-  Double_t eTr = 0.5*1.5*betaM*TMath::Sqrt(k)/(8*alpha);
-  Double_t lambda = 1.-TMath::Sin(fTrkDir.Theta())*TMath::Sin(fTrkDir.Theta())*TMath::Sin(phiC)*TMath::Sin(phiC);
-
-  Double_t c = 1./(1.+ eTr*k/(alpha*alpha*TMath::Cos(thetaC)*TMath::Cos(thetaC)));
-  Double_t i = betaM*TMath::Tan(thetaC)*lambda*TMath::Power(k,1.5);
-  Double_t ii = 1.+eTr*betaM*i;
+  Double_t eTr = 0.5*fgkRadThick*betaM*TMath::Sqrt(k)/(fgkGapThick*alpha);                   // formula (14)
+  Double_t lambda = 1.-sint*sint*sinf*sinf;                                                  // formula (15)
 
-  Double_t err = c * (i/(alpha*alpha*8) +  ii*(1.-lambda) / ( alpha*alpha*8*betaM*(1.+eTr)) );
-  Double_t trErr = 1.5/(TMath::Sqrt(12.)*TMath::Cos(fTrkDir.Theta()));
+  Double_t c1 = 1./(1.+ eTr*k/(alpha*alpha*costheta*costheta));                              // formula (13.a)
+  Double_t c2 = betaM*TMath::Power(k,1.5)*tantheta*lambda/(fgkGapThick*alpha*alpha);         // formula (13.b)
+  Double_t c3 = (1.+eTr*k*betaM*betaM)/((1+eTr)*alpha*alpha);                                // formula (13.c)
+  Double_t c4 = TMath::Sqrt(k)*tantheta*(1-lambda)/(fgkGapThick*betaM);                      // formula (13.d)
+  Double_t dtdT = c1 * (c2+c3*c4);
+  Double_t trErr = fgkRadThick/(TMath::Sqrt(12.)*cost);
 
-  return trErr*err;
+  return trErr*dtdT;
 }//SigGeom()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+//
+// From here HTA....
+//
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Bool_t AliHMPIDRecon::CkovHiddenTrk(AliESDtrack *pTrk,TClonesArray *pCluLst,Double_t nmean, Double_t qthre)
+{
+// Pattern recognition method without any infos from tracking:HTA (Hidden Track Algorithm)...
+// The method finds in the chmber the cluster with the highest charge
+// compatibile with a MIP, then the strategy is applied
+// Arguments:  pTrk     - pointer to ESD track
+//             pCluLs   - list of clusters for a given chamber 
+//             nmean    - mean freon ref. index
+//   Returns:           - 0=ok,1=not fitted 
+  
+  fRadNmean=nmean;
+
+  if(pCluLst->GetEntriesFast()>100) return kFALSE;                                            //boundary check for CluX,CluY...
+  Float_t mipX=-1,mipY=-1;Int_t mipId=-1,mipQ=-1;                                                                           
+  Double_t qRef = 0;
+  Int_t nCh=0;
+  for (Int_t iClu=0;iClu<pCluLst->GetEntriesFast();iClu++){                                   //clusters loop
+    AliHMPIDCluster *pClu=(AliHMPIDCluster*)pCluLst->UncheckedAt(iClu);                       //get pointer to current cluster    
+    nCh = pClu->Ch();
+    fXClu[iClu] = pClu->X();fYClu[iClu] = pClu->Y();                                          //store x,y for fitting procedure
+    fClCk[iClu] = kTRUE;                                                                      //all cluster are accepted at this stage to be reconstructed
+    if(pClu->Q()>qRef){                                                                       //searching the highest charge to select a MIP      
+      qRef = pClu->Q();
+      mipId=iClu; mipX=pClu->X();mipY=pClu->Y();mipQ=(Int_t)pClu->Q();
+    }                                                                                    
+  }//clusters loop
+
+  fNClu = pCluLst->GetEntriesFast();
+  if(qRef>qthre){                                                                     //charge compartible with MIP clusters
+    fIdxMip = mipId;
+    fClCk[mipId] = kFALSE;
+    fMipX = mipX; fMipY=mipY; fMipQ = qRef;
+    if(!DoRecHiddenTrk(pCluLst)) {
+      pTrk->SetHMPIDsignal(kNoPhotAccept);
+      return kFALSE;
+    }                                                                           //Do track and ring reconstruction,if problems returns 1
+    pTrk->SetHMPIDtrk(fRadX,fRadY,fThTrkFit,fPhTrkFit);                                        //store track intersection info
+    pTrk->SetHMPIDmip(fMipX,fMipY,(Int_t)fMipQ,fNClu);                                         //store mip info 
+    pTrk->SetHMPIDcluIdx(nCh,fIdxMip);                                                         //set cham number and index of cluster
+    pTrk->SetHMPIDsignal(fCkovFit);                                                            //find best Theta ckov for ring i.e. track
+    pTrk->SetHMPIDchi2(fCkovSig2);                                                             //errors squared
+//    Printf(" n clusters tot %i accepted %i",pCluLst->GetEntriesFast(),fNClu);
+    return kTRUE;
+  }
+  
+  return kFALSE;
+}//CkovHiddenTrk()
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Bool_t AliHMPIDRecon::DoRecHiddenTrk(TClonesArray *pCluLst)
+{
+// Pattern recognition method without any infos from tracking...
+// First a preclustering filter to avoid part of the noise
+// Then only ellipsed-rings are fitted (no possibility, 
+// for the moment, to reconstruct very inclined tracks)
+// Finally a fitting with (th,ph) free, starting by very close values
+// previously evaluated.
+// Arguments:   none
+//   Returns:   none
+  Double_t phiRec;
+  if(!CluPreFilter(pCluLst)) {return kFALSE;}
+  if(!FitEllipse(phiRec)) {return kFALSE;}
+  Int_t nClTmp1 = pCluLst->GetEntriesFast()-1;  //minus MIP...
+  Int_t nClTmp2 = 0;
+  while(nClTmp1 != nClTmp2){
+    SetNClu(pCluLst->GetEntriesFast());
+    if(!FitFree(phiRec)) {return kFALSE;}
+    nClTmp2 = NClu();
+    if(nClTmp2!=nClTmp1) {nClTmp1=nClTmp2;nClTmp2=0;}
+  }
+  fNClu = nClTmp2;
+  return kTRUE;
+}//DoRecHiddenTrk()
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Bool_t AliHMPIDRecon::CluPreFilter(TClonesArray *pCluLst)
+{
+// Filter of bkg clusters
+// based on elliptical-shapes...
+//
+  if(pCluLst->GetEntriesFast()>50||pCluLst->GetEntriesFast()<4) return kFALSE; 
+  else return kTRUE;
+}
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Bool_t AliHMPIDRecon::FitEllipse(Double_t &phiRec)
+{
+//Fit a set of clusters with an analitical conical section function:
+  //
+  // Ax^2 + B*y^2 + 2Hxy + 2Gx + 2Fy + 1 = 0   ---> conical section
+  //
+  //  H*H - A*B > 0 hyperbola
+  //            < 0 ellipse
+  //            = 0 parabola
+  //
+  // tan 2alfa = 2H/(A-B)  alfa=angle of rotation
+  //
+  // coordinate of the centre of the conical section:
+  //   x = x' + a
+  //   y = y' + b
+  //
+  //       HF - BG
+  //  a = ---------
+  //       AB - H^2
+  //
+  //       HG - AF
+  //  b = --------
+  //       AB - H^2
+  Double_t cA,cB,cF,cG,cH;
+  Double_t aArg=-1;      Int_t iErrFlg;                                                //tmp vars for TMinuit
+
+  if(!gMinuit) gMinuit = new TMinuit(5);                                               //init MINUIT with this number of parameters (5 params)
+  gMinuit->mncler();                                                                   // reset Minuit list of paramters
+  gMinuit->SetObjectFit((TObject*)this);  gMinuit->SetFCN(AliHMPIDRecon::FunMinEl);    //set fit function
+  gMinuit->mnexcm("SET PRI",&aArg,1,iErrFlg);                                          //suspend all printout from TMinuit 
+  gMinuit->mnexcm("SET NOW",&aArg,0,iErrFlg);                                          //suspend all warning printout from TMinuit
+  
+  Double_t d1,d2,d3;
+  TString sName;
+
+  gMinuit->mnparm(0," A ",1,0.01,0,0,iErrFlg);
+  gMinuit->mnparm(1," B ",1,0.01,0,0,iErrFlg);
+  gMinuit->mnparm(2," H ",1,0.01,0,0,iErrFlg);
+  gMinuit->mnparm(3," G ",1,0.01,0,0,iErrFlg);
+  gMinuit->mnparm(4," F ",1,0.01,0,0,iErrFlg);
+
+  gMinuit->mnexcm("SIMPLEX",&aArg,0,iErrFlg);
+  gMinuit->mnexcm("MIGRAD" ,&aArg,0,iErrFlg);   
+  gMinuit->mnpout(0,sName,cA,d1,d2,d3,iErrFlg);
+  gMinuit->mnpout(1,sName,cB,d1,d2,d3,iErrFlg);
+  gMinuit->mnpout(2,sName,cH,d1,d2,d3,iErrFlg);
+  gMinuit->mnpout(3,sName,cG,d1,d2,d3,iErrFlg);
+  gMinuit->mnpout(4,sName,cF,d1,d2,d3,iErrFlg);
+  delete gMinuit;
+
+  Double_t i2 = cA*cB-cH*cH;                                       //quartic invariant : i2 > 0  ellipse, i2 < 0 hyperbola
+  if(i2<=0) return kFALSE;
+  Double_t aX = (cH*cF-cB*cG)/i2;                                  //x centre of the canonical section 
+  Double_t bY = (cH*cG-cA*cF)/i2;                                  //y centre of the canonical section 
+  Double_t alfa1 = TMath::ATan(2*cH/(cA-cB));                      //alpha = angle of rotation of the conical section
+  if(alfa1<0) alfa1+=TMath::Pi(); 
+  alfa1*=0.5;
+//  Double_t alfa2 = alfa1+TMath::Pi();
+  Double_t phiref = TMath::ATan2(bY-fMipY,aX-fMipX);               //evaluate in a unique way the angle of rotation comparing it
+  if(phiref<0) phiref+=TMath::TwoPi();                             //with the vector that points to the centre from the mip 
+  if(i2<0) phiref+=TMath::Pi();
+  if(phiref>TMath::TwoPi()) phiref-=TMath::TwoPi();
+
+//  Printf(" alfa1 %f",alfa1*TMath::RadToDeg());
+//  Printf(" alfa2 %f",alfa2*TMath::RadToDeg());
+//  Printf(" firef %f",phiref*TMath::RadToDeg());
+//  if(TMath::Abs(alfa1-phiref)<TMath::Abs(alfa2-phiref)) phiRec = alfa1; else phiRec = alfa2;  
+  
+//  Printf("FitEllipse: phi reconstructed %f",phiRec*TMath::RadToDeg());
+  phiRec=phiref;
+  return kTRUE;
+//
+}
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Bool_t AliHMPIDRecon::FitFree(Double_t phiRec)
+{
+// Fit performed by minimizing RMS/sqrt(n) of the
+// photons reconstructed. First phi is fixed and theta
+// is fouond, then (th,ph) of the track
+// as free parameters
+// Arguments:    PhiRec phi of the track
+//   Returns:    none
+  Double_t aArg=-1;  Int_t iErrFlg;                                                    //tmp vars for TMinuit
+  if(!gMinuit) gMinuit = new TMinuit(2);                                               //init MINUIT with this number of parameters (5 params)
+  gMinuit->mncler();                                                                   // reset Minuit list of paramters
+  gMinuit->SetObjectFit((TObject*)this);  gMinuit->SetFCN(AliHMPIDRecon::FunMinPhot);  //set fit function
+  gMinuit->mnexcm("SET PRI",&aArg,1,iErrFlg);                                          //suspend all printout from TMinuit 
+  gMinuit->mnexcm("SET NOW",&aArg,0,iErrFlg);                                          //suspend all warning printout from TMinuit
+  
+  Double_t d1,d2,d3;
+  TString sName;
+  Double_t th,ph;
+  
+  gMinuit->mnparm(0," theta ",  0.01,0.01,0,TMath::PiOver2(),iErrFlg);
+  gMinuit->mnparm(1," phi   ",phiRec,0.01,0,TMath::TwoPi()  ,iErrFlg);
+  
+  gMinuit->FixParameter(1);
+  gMinuit->mnexcm("SIMPLEX" ,&aArg,0,iErrFlg);   
+  gMinuit->mnexcm("MIGRAD"  ,&aArg,0,iErrFlg);
+  gMinuit->Release(1);  
+  gMinuit->mnexcm("MIGRAD"  ,&aArg,0,iErrFlg);
+  
+  gMinuit->mnpout(0,sName,th,d1,d2,d3,iErrFlg);
+  gMinuit->mnpout(1,sName,ph,d1,d2,d3,iErrFlg);   
+
+  Double_t outPar[2] = {th,ph}; Double_t g; Double_t f;Int_t flag = 3;
+  gMinuit->Eval(2, &g, f, outPar,flag);  
+
+  SetTrkFit(th,ph);
+  
+  return kTRUE;
+}
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Double_t AliHMPIDRecon::FunConSect(Double_t *c,Double_t x,Double_t y)
+{
+  return c[0]*x*x+c[1]*y*y+2*c[2]*x*y+2*c[3]*x+2*c[4]*y+1;
+}
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliHMPIDRecon::FunMinEl(Int_t &/* */,Double_t* /* */,Double_t &f,Double_t *par,Int_t /* */)
+{
+  AliHMPIDRecon *pRec=(AliHMPIDRecon*)gMinuit->GetObjectFit();
+  Double_t minFun = 0;
+  Int_t np = pRec->NClu();
+  for(Int_t i=0;i<np;i++) {
+    if(i==pRec->IdxMip()) continue;
+    Double_t el = pRec->FunConSect(par,pRec->XClu(i),pRec->YClu(i));
+    minFun +=el*el;
+  }
+  f = minFun;
+}
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliHMPIDRecon::FunMinPhot(Int_t &/* */,Double_t* /* */,Double_t &f,Double_t *par,Int_t iflag)
+{
+  AliHMPIDRecon *pRec=(AliHMPIDRecon*)gMinuit->GetObjectFit();
+  Double_t sizeCh = 0.5*fgkRadThick+fgkWinThick+fgkGapThick;
+  Double_t thTrk = par[0]; 
+  Double_t phTrk = par[1]; 
+  Double_t xrad = pRec->MipX() - sizeCh*TMath::Tan(thTrk)*TMath::Cos(phTrk);
+  Double_t yrad = pRec->MipY() - sizeCh*TMath::Tan(thTrk)*TMath::Sin(phTrk);
+  pRec->SetRadXY(xrad,yrad);
+  pRec->SetTrack(xrad,yrad,thTrk,phTrk);
+
+  Double_t meanCkov =0;
+  Double_t meanCkov2=0;
+  Double_t thetaCer,phiCer;
+  Int_t nClAcc = 0;
+  Int_t nClTot=pRec->NClu();
+    
+  for(Int_t i=0;i<nClTot;i++) {
+    if(!(pRec->ClCk(i))) continue;
+    pRec->FindPhotCkov(pRec->XClu(i),pRec->YClu(i),thetaCer,phiCer);  
+    meanCkov  += thetaCer;
+    meanCkov2 += thetaCer*thetaCer;
+    nClAcc++;
+  }
+  if(nClAcc==0) {f=999;return;}
+  meanCkov/=nClAcc;
+  Double_t rms = (meanCkov2 - meanCkov*meanCkov*nClAcc)/nClAcc;
+  if(rms<0) Printf(" rms2 = %f, strange!!!",rms);
+  rms = TMath::Sqrt(rms);
+  f = rms/TMath::Sqrt(nClAcc);
+  
+  
+  if(iflag==3) {
+    Printf("FunMinPhot before: photons candidates %i used %i",nClTot,nClAcc);
+    nClAcc = 0;
+    Double_t meanCkov1=0;
+    Double_t meanCkov2=0;
+    for(Int_t i=0;i<nClTot;i++) {
+      if(!(pRec->ClCk(i))) continue;
+      pRec->FindPhotCkov(pRec->XClu(i),pRec->YClu(i),thetaCer,phiCer);  
+      if(TMath::Abs(thetaCer-meanCkov)<2*rms) {
+        meanCkov1 += thetaCer;
+        meanCkov2 += thetaCer*thetaCer;
+        nClAcc++;
+      } else pRec->SetClCk(i,kFALSE);
+    }
+    meanCkov1/=nClAcc;
+    Double_t rms2 = (meanCkov2 - meanCkov*meanCkov*nClAcc)/nClAcc;
+    Printf("FunMinPhot after: photons candidates %i used %i thetaCer %f",nClTot,nClAcc,meanCkov1);
+    pRec->SetCkovFit(meanCkov1);
+    pRec->SetCkovSig2(rms2);
+    pRec->SetNClu(nClAcc);
+  }
+}//FunMinPhot()
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+//
+// ended Hidden track algorithm....
+//
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++