#include "AliHMPIDPid.h"
#include "AliHMPIDParam.h"
#include "AliHMPIDRecon.h"
#include <AliESDtrack.h>
#include <TRandom.h> //Resolution()
AliHMPIDPid::AliHMPIDPid():TNamed("HMPIDrec","HMPIDPid")
{
}
void AliHMPIDPid::FindPid(AliESDtrack *pTrk,Double_t nmean,Int_t nsp,Double_t *prob)
{
AliPID *pPid = new AliPID();
Double_t thetaCerExp = -999.;
if(pTrk->GetHMPIDsignal()<=0) thetaCerExp = pTrk->GetHMPIDsignal();
else thetaCerExp = pTrk->GetHMPIDsignal() - (Int_t)pTrk->GetHMPIDsignal();
if(thetaCerExp<=0){
for(Int_t iPart=0;iPart<nsp;iPart++) prob[iPart]=1.0/(Float_t)nsp;
delete pPid ; pPid=0x0; return;
}
Double_t p[3] = {0}, pmod = 0;
if(pTrk->GetOuterHmpPxPyPz(p)) pmod = TMath::Sqrt(p[0]*p[0]+p[1]*p[1]+p[2]*p[2]);
else {
for(Int_t iPart=0;iPart<nsp;iPart++) prob[iPart]=1.0/(Float_t)nsp;
delete pPid ; pPid=0x0; return;
}
Double_t hTot=0;
Double_t *h = new Double_t [nsp];
Bool_t desert = kTRUE;
for(Int_t iPart=0;iPart<nsp;iPart++){
h[iPart] = 0;
Double_t mass = pPid->ParticleMass(iPart);
Double_t cosThetaTh = TMath::Sqrt(mass*mass+pmod*pmod)/(nmean*pmod);
if(cosThetaTh>1) continue;
Double_t thetaCerTh = TMath::ACos(cosThetaTh);
Double_t sigmaRing = Resolution(iPart,thetaCerTh,pTrk);
if(sigmaRing==0) continue;
if(TMath::Abs(thetaCerExp-thetaCerTh)<4*sigmaRing) desert = kFALSE;
h[iPart] =TMath::Gaus(thetaCerTh,thetaCerExp,sigmaRing,kTRUE);
hTot +=h[iPart];
}
for(Int_t iPart=0;iPart<nsp;iPart++) {
if(!desert) prob[iPart]=h[iPart]/hTot;
else prob[iPart]=1.0/(Float_t)nsp;
}
delete [] h;
delete pPid ; pPid=0x0;
}
Double_t AliHMPIDPid::Resolution(Int_t iPart, Double_t thetaCerTh, AliESDtrack *pTrk)
{
AliHMPIDParam *pParam = AliHMPIDParam::Instance();
AliHMPIDRecon rec;
Float_t xPc,yPc,thRa,phRa;
Float_t x, y;
Int_t q, nph;
pTrk->GetHMPIDtrk(xPc,yPc,thRa,phRa);
pTrk->GetHMPIDmip(x,y,q,nph);
Double_t xRa = xPc - (pParam->RadThick()+pParam->WinThick()+pParam->GapThick())*TMath::Cos(phRa)*TMath::Tan(thRa);
Double_t yRa = yPc - (pParam->RadThick()+pParam->WinThick()+pParam->GapThick())*TMath::Sin(phRa)*TMath::Tan(thRa);
rec.SetTrack(xRa,yRa,thRa,phRa);
Double_t occupancy = pTrk->GetHMPIDoccupancy();
Double_t thetaMax = TMath::ACos(1./pParam->MeanIdxRad());
Int_t nPhotsTh = (Int_t)(12.*TMath::Sin(thetaCerTh)*TMath::Sin(thetaCerTh)/(TMath::Sin(thetaMax)*TMath::Sin(thetaMax))+0.01);
Double_t sigmatot = 0;
Int_t nTrks = 20;
for(Int_t iTrk=0;iTrk<nTrks;iTrk++) {
Double_t invSigma = 0;
Int_t nPhotsAcc = 0;
Int_t nPhots = 0;
if(nph<nPhotsTh+TMath::Sqrt(nPhotsTh) && nph>nPhotsTh-TMath::Sqrt(nPhotsTh)) nPhots = nph;
else nPhots = gRandom->Poisson(nPhotsTh);
for(Int_t j=0;j<nPhots;j++){
Double_t phi = gRandom->Rndm()*TMath::TwoPi();
TVector2 pos; pos=rec.TracePhot(thetaCerTh,phi);
if(!pParam->IsInside(pos.X(),pos.Y())) continue;
if(pParam->IsInDead(pos.X(),pos.Y())) continue;
Double_t sigma2 = pParam->Sigma2(thRa,phRa,thetaCerTh,phi);
if(sigma2!=0) {
invSigma += 1./sigma2;
nPhotsAcc++;
}
}
if(invSigma!=0) sigmatot += 1./TMath::Sqrt(invSigma);
}
return (sigmatot/nTrks)*pParam->SigmaCorrFact(iPart,occupancy);
}