]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HMPID/AliHMPIDRecon.cxx
Move HMPID QA code to PWG1.
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDRecon.cxx
index 062ade9881bac9c6d097c330dd22da081355bd8e..27069b2a598108cf7b39e40a3f5717767f5c8eab 100644 (file)
 //////////////////////////////////////////////////////////////////////////
 
 #include "AliHMPIDRecon.h"   //class header
-#include "AliHMPIDParam.h"   //CkovAngle()
 #include "AliHMPIDCluster.h" //CkovAngle()
 #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::fgkWinIdx  =1.5787;
-const Double_t AliHMPIDRecon::fgkGapIdx  =1.0005;
-
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-AliHMPIDRecon::AliHMPIDRecon():TTask("RichRec","RichPat"),
-  fRadNmean(1.292),  
+AliHMPIDRecon::AliHMPIDRecon():
+  TTask("RichRec","RichPat"),
   fPhotCnt(-1),
+  fPhotFlag(0x0),
+  fPhotCkov(0x0),
+  fPhotPhi(0x0),
+  fPhotWei(0x0),
   fCkovSigma2(0),
   fIsWEIGHT(kFALSE),
   fDTheta(0.001),
   fWindowWidth(0.045),
-  fTrkDir(TVector3(0,0,1)),fTrkPos(TVector2(30,40))  
+  fRingArea(0),
+  fRingAcc(0),
+  fTrkDir(0,0,1),  // Just for test
+  fTrkPos(30,40),  // Just for test
+  fMipPos(0,0),
+  fPc(0,0),
+  fParam(AliHMPIDParam::Instance())
 {
-// main ctor
-  for (Int_t i=0; i<3000; i++) {
-    fPhotFlag[i] =  0;
-    fPhotCkov[i] = -1;
-    fPhotPhi [i] = -1;
-    fPhotWei [i] =  0;
-  }
+//..
+//init of data members
+//..
+  
+  fParam->SetRefIdx(fParam->MeanIdxRad()); // initialization of ref index to a default one
+}
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliHMPIDRecon::InitVars(Int_t n)
+{
+//..
+//Init some variables
+//..
+  if(n<=0) return;
+  fPhotFlag = new Int_t[n];
+  fPhotCkov = new Double_t[n];
+  fPhotPhi  = new Double_t[n];
+  fPhotWei  = new Double_t[n];
+//
+}
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliHMPIDRecon::DeleteVars()const
+{
+//..
+//Delete variables
+//..
+  delete [] fPhotFlag;
+  delete [] fPhotCkov;
+  delete [] fPhotPhi;
+  delete [] fPhotWei;
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-void AliHMPIDRecon::CkovAngle(AliESDtrack *pTrk,TClonesArray *pCluLst,Double_t nmean)
+void AliHMPIDRecon::CkovAngle(AliESDtrack *pTrk,TClonesArray *pCluLst,Int_t index,Double_t nmean,Float_t xRa,Float_t yRa)
 {
 // 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();
+
+  const Int_t nMinPhotAcc = 3;                      // Minimum number of photons required to perform the pattern recognition
   
-  if(pCluLst->GetEntries()>pParam->MultCut()) fIsWEIGHT = kTRUE; // offset to take into account bkg in reconstruction
-  else                                        fIsWEIGHT = kFALSE;
+  Int_t nClusTot = pCluLst->GetEntries();
+  if(nClusTot>fParam->MultCut()) fIsWEIGHT = kTRUE; // offset to take into account bkg in reconstruction
+  else                           fIsWEIGHT = kFALSE;
 
-  Float_t xRa,yRa,th,ph;       
-  pTrk->GetHMPIDtrk(xRa,yRa,th,ph);        //initialize this track: th and ph angles at middle of RAD 
+  InitVars(nClusTot);
   
-  ph+=TMath::Pi();                         // right XYZ local orientation
+  Float_t xPc,yPc,th,ph;
+  pTrk->GetHMPIDtrk(xPc,yPc,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;                                                      
+  fParam->SetRefIdx(nmean);
+
+  Float_t mipX=-1,mipY=-1;
+  Int_t chId=-1,mipQ=-1,sizeClu = -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    
+    if(iClu == index) {                                                                       // this is the MIP! not a photon candidate: just store mip info
+      mipX = pClu->X();
+      mipY = pClu->Y();
+      mipQ=(Int_t)pClu->Q();
+      sizeClu=pClu->Size();
+      continue;                                                             
+    }
     chId=pClu->Ch();
-    if(pClu->Q()>pParam->QCut()){                                                             //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-TMath::Pi();                                               //actual phi   Cerenkov (in TRS): -pi to come back to "unusual" ref system (X,Y,-Z)
-        fPhotCnt++;                                                                           //increment counter of photon candidates
-      }
+    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)
+      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 
 
-  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->SetHMPIDmip(mipX,mipY,mipQ,fPhotCnt);                                                 //store mip info in any case 
+  pTrk->SetHMPIDcluIdx(chId,index+1000*sizeClu);                                              //set index of cluster
+  
+  if(fPhotCnt<nMinPhotAcc) {                                                                  //no reconstruction with <=3 photon candidates
+    pTrk->SetHMPIDsignal(kNoPhotAccept);                                                      //set the appropriate flag
+    return;
+  }
   
-  pTrk->SetHMPIDchi2(fCkovSigma2);                                                            //errors squared 
+  
+  fMipPos.Set(mipX,mipY);
+  
+  
+//PATTERN RECOGNITION STARTED: 
+  
+  Int_t iNrec=FlagPhot(HoughResponse());                                                      //flag photons according to individual theta ckov with respect to most probable
+  pTrk->SetHMPIDmip(mipX,mipY,mipQ,iNrec);                                                    //store mip info 
 
-}//ThetaCerenkov()
+  if(iNrec<nMinPhotAcc){
+    pTrk->SetHMPIDsignal(kNoPhotAccept);                                                      //no photon candidates are accepted
+    return;
+  }
+  Double_t thetaC = FindRingCkov(pCluLst->GetEntries());                                    //find the best reconstructed theta Cherenkov
+//    FindRingGeom(thetaC,2);
+  pTrk->SetHMPIDsignal(thetaC);                                                             //store theta Cherenkov
+  pTrk->SetHMPIDchi2(fCkovSigma2);                                                          //store errors squared
+
+  DeleteVars();
+}//CkovAngle()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Bool_t AliHMPIDRecon::FindPhotCkov(Double_t cluX,Double_t cluY,Double_t &thetaCer,Double_t &phiCer)
 {
@@ -112,18 +161,16 @@ Bool_t AliHMPIDRecon::FindPhotCkov(Double_t cluX,Double_t cluY,Double_t &thetaCe
 
   TVector3 dirCkov;
   
-  Double_t zRad=-0.5*AliHMPIDRecon::fgkRadThick
-                    -AliHMPIDRecon::fgkWinThick
-                    -AliHMPIDRecon::fgkGapThick;                    //z position of middle of RAD
-
-  TVector3 rad(fTrkPos.X(),fTrkPos.Y(),zRad);                       //impact point at middle of RAD
-  TVector3  pc(cluX,cluY,0);                                        //mip at PC: z=0 @ PC
+  Double_t zRad= -0.5*fParam->RadThick()-0.5*fParam->WinThick();     //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*fParam->WinThick()+fParam->GapIdx());   //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 phi=(pc-rad).Phi();                                       //phi of photon
     
-  Double_t ckov1=0,ckov2=0.75+fTrkDir.Theta();                      //start to find theta cerenkov in DRS
-  const Double_t kTol=0.01; 
+  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 kFALSE;
@@ -133,11 +180,11 @@ Bool_t AliHMPIDRecon::FindPhotCkov(Double_t cluX,Double_t cluY,Double_t &thetaCe
     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 
+    if     (dist> kTol) ckov1=ckov;                           //cluster @ larger ckov
     else if(dist<-kTol) ckov2=ckov;                           //cluster @ smaller ckov
     else{                                                     //precision achived: ckov in DRS found
       dirCkov.SetMagThetaPhi(1,ckov,phi);                     //
-      RecPhot(dirCkov,thetaCer,phiCer);                       //find ckov (in TRS:the effective Cherenkov angle!)
+      Lors2Trs(dirCkov,thetaCer,phiCer);                       //find ckov (in TRS:the effective Cherenkov angle!)
       return kTRUE;
     }
   }
@@ -148,31 +195,32 @@ TVector2 AliHMPIDRecon::TraceForward(TVector3 dirCkov)const
   //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);
-  if(dirCkov.Theta() > TMath::ASin(1./fRadNmean))  return pos;      //total refraction on WIN-GAP boundary
-  Double_t zRad=-0.5*AliHMPIDRecon::fgkRadThick
-                    -AliHMPIDRecon::fgkWinThick
-                    -AliHMPIDRecon::fgkGapThick;                    //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,-fgkWinThick-fgkGapThick);              //go to RAD-WIN boundary  
-  Refract  (dirCkov,         fRadNmean,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
+  Double_t thetaCer = dirCkov.Theta();
+  if(thetaCer > TMath::ASin(1./fParam->GetRefIdx())) return pos;          //total refraction on WIN-GAP boundary
+  Double_t zRad= -0.5*fParam->RadThick()-0.5*fParam->WinThick();          //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*fParam->WinThick());          //go to RAD-WIN boundary  
+  Refract  (dirCkov,         fParam->GetRefIdx(),fParam->WinIdx());       //RAD-WIN refraction
+  Propagate(dirCkov,posCkov,            0.5*fParam->WinThick());          //go to WIN-GAP boundary
+  Refract  (dirCkov,         fParam->WinIdx(),fParam->GapIdx());          //WIN-GAP refraction
+  Propagate(dirCkov,posCkov,0.5*fParam->WinThick()+fParam->GapThick());   //go to PC
   pos.Set(posCkov.X(),posCkov.Y());
   return pos;
 }//TraceForward()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-void AliHMPIDRecon::RecPhot(TVector3 dirCkov,Double_t &thetaCer,Double_t &phiCer)
+void AliHMPIDRecon::Lors2Trs(TVector3 dirCkov,Double_t &thetaCer,Double_t &phiCer)const
 {
   //Theta Cerenkov reconstruction 
-  // Arguments: (x,y) of initial point in LORS, dirCkov photon vector in LORS
-  //   Returns: thetaCer theta cerenkov reconstructed
+  // Arguments: dirCkov photon vector in LORS
+  //   Returns: thetaCer of photon in TRS
+  //              phiCer of photon in TRS
 //  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 mtheta;   mtheta.RotateY(-fTrkDir.Theta());
+  TRotation mphi;       mphi.RotateZ(-fTrkDir.Phi());
   TRotation mrot=mtheta*mphi;
   TVector3 dirCkovTRS;
   dirCkovTRS=mrot*dirCkov;
@@ -180,21 +228,99 @@ void AliHMPIDRecon::RecPhot(TVector3 dirCkov,Double_t &thetaCer,Double_t &phiCer
   thetaCer= dirCkovTRS.Theta();                                        //actual value of thetaCerenkov of the photon
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Double_t AliHMPIDRecon::FindRingArea(Double_t ckovAng)const
+void AliHMPIDRecon::Trs2Lors(TVector3 dirCkov,Double_t &thetaCer,Double_t &phiCer)const
 {
-// Find area inside the cerenkov ring which lays inside PCs
-// Arguments: ckovAng - cerenkov angle    
+  //Theta Cerenkov reconstruction 
+  // Arguments: dirCkov photon vector in TRS
+  //   Returns: thetaCer of photon in LORS
+  //              phiCer of photon in LORS
+  TRotation mtheta;   mtheta.RotateY(fTrkDir.Theta());
+  TRotation mphi;       mphi.RotateZ(fTrkDir.Phi());
+  TRotation mrot=mphi*mtheta;
+  TVector3 dirCkovLORS;
+  dirCkovLORS=mrot*dirCkov;
+  phiCer  = dirCkovLORS.Phi();                                          //actual value of the phi of the photon
+  thetaCer= dirCkovLORS.Theta();                                        //actual value of thetaCerenkov of the photon
+}
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+void AliHMPIDRecon::FindRingGeom(Double_t ckovAng,Int_t level)
+{
+// Find area covered in the PC acceptance
+// Arguments: ckovAng - cerenkov angle
+//            level   - precision in finding area and portion of ring accepted (multiple of 50)
 //   Returns: area of the ring in cm^2 for given theta ckov
    
-  const Int_t kN=100;
+  Int_t kN=50*level;
+  Int_t nPoints = 0;
   Double_t area=0;
+  
+  Bool_t first=kFALSE;
+  TVector2 pos1;
+  
   for(Int_t i=0;i<kN;i++){
-    TVector2 pos1=TracePhot(ckovAng,Double_t(TMath::TwoPi()*i    /kN));//trace this photon 
-    TVector2 pos2=TracePhot(ckovAng,Double_t(TMath::TwoPi()*(i+1)/kN));//trace the next photon 
-    area+=(pos1-fTrkPos)*(pos2-fTrkPos);                               //add area of the triangle... 
+   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(!fParam->IsInside(pos1.X(),pos1.Y(),0)) {
+       pos1 = IntWithEdge(fMipPos,pos1);                                                            // find the very first intersection...
+     } else {
+       if(!AliHMPIDParam::IsInDead(pos1.X(),pos1.Y())) nPoints++;                                   //photon is accepted if not in dead zone
+     }
+     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(!fParam->IsInside(pos2.X(),pos2.Y(),0)) {
+     pos2 = IntWithEdge(fMipPos,pos2);
+   } else {
+     if(!AliHMPIDParam::IsInDead(pos2.X(),pos2.Y())) nPoints++;                                     //photon is accepted if not in dead zone
+   }
+   area+=TMath::Abs((pos1-fMipPos).X()*(pos2-fMipPos).Y()-(pos1-fMipPos).Y()*(pos2-fMipPos).X());   //add area of the triangle...           
+   pos1 = pos2;
   }
-  return area;
-}//FindRingArea()
+//---  find area and length of the ring;
+  fRingAcc = (Double_t)nPoints/(Double_t)kN;
+  area*=0.5;
+  fRingArea = area;
+}//FindRingGeom()
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+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
+
+  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.);
+  if(pint.X()>=0 && pint.X()<=fParam->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() + (fParam->SizeAllY()-p1.Y())/m),(Double_t)(fParam->SizeAllY()));
+  if(pint.X()>=0 && pint.X()<=fParam->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())));
+  if(pint.Y()>=0 && pint.Y()<=fParam->SizeAllY() &&
+     pint.Y()>=ymin && pint.Y()<=ymax            &&
+     pint.X()>=xmin && pint.X()<=xmax) return pint;
+  //intersection with righ Y  
+  pint.Set((Double_t)(fParam->SizeAllX()),(Double_t)(p1.Y() + m*(fParam->SizeAllX()-p1.X())));
+  if(pint.Y()>=0 && pint.Y()<=fParam->SizeAllY() &&
+     pint.Y()>=ymin && pint.Y()<=ymax            &&
+     pint.X()>=xmin && pint.X()<=xmax) return pint;
+  return p1;
+}//IntWithEdge()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Double_t AliHMPIDRecon::FindRingCkov(Int_t)
 {
@@ -216,14 +342,14 @@ Double_t AliHMPIDRecon::FindRingCkov(Int_t)
       weightThetaCerenkov += fPhotCkov[i]*fPhotWei[i];
       wei += fPhotWei[i];                                                    //collect weight as sum of all candidate weghts   
       
-      sigma2 += 1./Sigma2(fPhotCkov[i],fPhotPhi[i]);
+      sigma2 += 1./fParam->Sigma2(fTrkDir.Theta(),fTrkDir.Phi(),fPhotCkov[i],fPhotPhi[i]);
     }
   }//candidates loop
   
   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()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -247,6 +373,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++;
@@ -258,16 +385,15 @@ Int_t AliHMPIDRecon::FlagPhot(Double_t ckov)
 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 in DRS, [rad]    
+// Arguments: ckovThe,ckovPhi- photon ckov angles in TRS, [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  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);
+  
+  Double_t theta,phi;
+  TVector3  dirTRS,dirLORS;   
+  dirTRS.SetMagThetaPhi(1,ckovThe,ckovPhi);                     //photon in TRS
+  Trs2Lors(dirTRS,theta,phi);
+  dirLORS.SetMagThetaPhi(1,theta,phi);                          //photon in LORS
+  return TraceForward(dirLORS);                                 //now foward tracing
 }//TracePhot()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 void AliHMPIDRecon::Propagate(const TVector3 dir,TVector3 &pos,Double_t z)const
@@ -294,14 +420,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);
@@ -317,8 +444,12 @@ 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 diffArea = FindRingArea(upperlimit)-FindRingArea(lowerlimit);
+      Double_t lowerlimit = ((Double_t)bin)*fDTheta - 0.5*fDTheta;  Double_t upperlimit = ((Double_t)bin)*fDTheta + 0.5*fDTheta;
+      FindRingGeom(lowerlimit);
+      Double_t areaLow  = GetRingArea();
+      FindRingGeom(upperlimit);
+      Double_t areaHigh = GetRingArea();
+      Double_t diffArea = areaHigh - areaLow;
       if(diffArea>0) weight = 1./diffArea;
     }
     photsw->Fill(angle,weight);
@@ -338,97 +469,36 @@ 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()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Double_t AliHMPIDRecon::Sigma2(Double_t ckovTh, Double_t ckovPh)const
+  Double_t AliHMPIDRecon::FindRingExt(Double_t ckov,Int_t ch,Double_t xPc,Double_t yPc,Double_t thRa,Double_t phRa)
 {
-// Analithical calculation of total error (as a sum of localization, geometrical and chromatic errors) on Cerenkov angle for a given Cerenkov photon 
-// created by a given MIP. Fromulae according to CERN-EP-2000-058 
-// Arguments: Cerenkov and azimuthal angles for Cerenkov photon, [radians]
-//            dip and azimuthal angles for MIP taken at the entrance to radiator, [radians]        
-//            MIP beta
-//   Returns: absolute error on Cerenkov angle, [radians]    
+// To find the acceptance of the ring even from external inputs. 
+//    
+//       
+  Double_t xRa = xPc - (fParam->RadThick()+fParam->WinThick()+fParam->GapThick())*TMath::Cos(phRa)*TMath::Tan(thRa); //just linear extrapolation back to RAD
+  Double_t yRa = yPc - (fParam->RadThick()+fParam->WinThick()+fParam->GapThick())*TMath::Sin(phRa)*TMath::Tan(thRa);
   
-  TVector3 v(-999,-999,-999);
-  Double_t trkBeta = 1./(TMath::Cos(ckovTh)*fRadNmean);
-
-  v.SetX(SigLoc (ckovTh,ckovPh,trkBeta));
-  v.SetY(SigGeom(ckovTh,ckovPh,trkBeta));
-  v.SetZ(SigCrom(ckovTh,ckovPh,trkBeta));
-
-  return v.Mag2();
-}
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Double_t AliHMPIDRecon::SigLoc(Double_t thetaC, Double_t phiC,Double_t betaM)const
-{
-// Analithical calculation of localization error (due to finite segmentation of PC) on Cerenkov angle for a given Cerenkov photon 
-// created by a given MIP. Fromulae according to CERN-EP-2000-058 
-// Arguments: Cerenkov and azimuthal angles for Cerenkov photon, [radians]
-//            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.-fRadNmean*fRadNmean+alpha*alpha/(betaM*betaM);
-  if (k<0) return 1e10;
+  Int_t nStep = 500;
+  Int_t nPhi = 0;  
 
-  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));
-
-  return  TMath::Sqrt(0.2*0.2*dtdxc*dtdxc + 0.25*0.25*dtdyc*dtdyc);
-}
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Double_t AliHMPIDRecon::SigCrom(Double_t thetaC, Double_t phiC,Double_t betaM)const
-{
-// Analithical calculation of chromatic error (due to lack of knowledge of Cerenkov photon energy) on Cerenkov angle for a given Cerenkov photon 
-// created by a given MIP. Fromulae according to CERN-EP-2000-058 
-// Arguments: Cerenkov and azimuthal angles for Cerenkov photon, [radians]
-//            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())*fRadNmean*betaM*betaM/(alpha*TMath::Tan(thetaC));
-            
-  Double_t f = 0.00928*(7.75-5.635)/TMath::Sqrt(12.);
-
-  return f*dtdn;
-}//SigCrom()
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Double_t AliHMPIDRecon::SigGeom(Double_t thetaC, Double_t phiC,Double_t betaM)const
-{
-// Analithical calculation of geometric error (due to lack of knowledge of creation point in radiator) on Cerenkov angle for a given Cerenkov photon 
-// created by a given MIP. Formulae according to CERN-EP-2000-058 
-// Arguments: Cerenkov and azimuthal angles for Cerenkov photon, [radians]
-//            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.-fRadNmean*fRadNmean+alpha*alpha/(betaM*betaM);
-  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 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()));
-
-  return trErr*err;
-}//SigGeom()
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  Int_t ipc,ipadx,ipady;
+    
+  if(ckov>0){
+    SetTrack(xRa,yRa,thRa,phRa);
+    for(Int_t j=0;j<nStep;j++){
+      TVector2 pos; pos=TracePhot(ckov,j*TMath::TwoPi()/(Double_t)(nStep-1));
+      if(fParam->IsInDead(pos.X(),pos.Y())) continue;
+      fParam->Lors2Pad(pos.X(),pos.Y(),ipc,ipadx,ipady);
+      ipadx+=(ipc%2)*fParam->kPadPcX;
+      ipady+=(ipc/2)*fParam->kPadPcY;
+      if(fParam->IsDeadPad(ipadx,ipady,ch)) continue;
+      nPhi++;
+    }//point loop
+  return ((Double_t)nPhi/(Double_t)nStep); 
+  }//if
+  return -1;
+}