+ // Find EMC-CPV distance
+ distance2Cpv = (vecCpv - vecEmc).Mag() ;
+
+ if (fESD != 0x0) {
+ AliPHOSGetter * gime = AliPHOSGetter::Instance() ;
+ const AliPHOSGeometry * geom = gime->PHOSGeometry() ;
+
+ Double_t rPHOS = geom->GetIPtoCrystalSurface();
+
+ //PH Acceptance boundaries for each PHOS module
+ Int_t nModules = geom->GetNModules();
+ Double_t * thmin = new Double_t[nModules];// theta min
+ Double_t * thmax = new Double_t[nModules];// theta max
+ Double_t * phmin = new Double_t[nModules];// phi min
+ Double_t * phmax = new Double_t[nModules];// phi max
+
+ for (Int_t imod=0; imod<nModules; imod++) {
+ geom->EmcModuleCoverage(imod,
+ thmin[imod],thmax[imod],
+ phmin[imod],phmax[imod]);
+ }
+
+ // Extrapolate the global track direction if any to CPV and find the closest track
+ Int_t nTracks = fESD->GetNumberOfTracks();
+ Int_t iClosestTrack = -1;
+ Double_t minDistance = 1e6;
+ Double_t pxyz[3], xyz[3];
+ TVector3 inPHOS; //PH Used to calculate theta and phi
+
+ //PH Loop on tracks
+ AliESDtrack *track;
+ for (Int_t iTrack=0; iTrack<nTracks; iTrack++) {
+ track = fESD->GetTrack(iTrack);
+ if (!track->GetXYZAt(rPHOS, fESD->GetMagneticField(), xyz))
+ continue; //track coord on the cylinder of PHOS radius
+ if ((TMath::Abs(xyz[0])+TMath::Abs(xyz[1])+TMath::Abs(xyz[2]))<=0)
+ continue;
+ //PH Here one has to cut out the tracks which are not inside the PHOS
+ //PH acceptance
+ inPHOS.SetXYZ(xyz[0],xyz[1],xyz[2]);
+ Double_t inPhi = inPHOS.Phi();
+ Double_t inTheta = inPHOS.Theta();
+
+ Bool_t skip = kTRUE;
+ for (Int_t imod=0; imod<nModules; imod++) {
+ //PH Loop on modules to check if the track enters in the acceptance
+ if (thmin[imod] < inTheta && thmax[imod] > inTheta &&
+ phmin[imod] < inPhi && phmax[imod] > inPhi) {
+ skip = kFALSE;
+ break;
+ }
+ }
+ if (skip) continue; //PH Skip, if not in the PHOS acceptance
+
+ if (!track->GetPxPyPzAt(rPHOS, fESD->GetMagneticField(), pxyz))
+ continue; // track momentum ibid.
+ PropagateToPlane(vecDist,xyz,pxyz,"CPV",cpvClu->GetPHOSMod());
+ // Info("GetDistanceInPHOSPlane","Track %d propagation to CPV = (%f,%f,%f)",
+ // iTrack,vecDist.X(),vecDist.Y(),vecDist.Z());
+ vecDist -= vecCpv;
+ distance2Track = TMath::Sqrt(vecDist.X()*vecDist.X() + vecDist.Z()*vecDist.Z());
+ // Find the closest track to the EMC recpoint
+ if (distance2Track < minDistance) {
+ minDistance = distance2Track;
+ iClosestTrack = iTrack;
+ }
+ }
+
+ delete [] thmin;
+ delete [] thmax;
+ delete [] phmin;
+ delete [] phmax;
+
+ if (iClosestTrack != -1) {
+ track = fESD->GetTrack(iClosestTrack);
+ if (track->GetPxPyPzAt(rPHOS, fESD->GetMagneticField(), pxyz)) { // track momentum ibid.
+ TVector3 vecCpvGlobal; // Global position of the CPV recpoint
+ geom->GetGlobal((AliRecPoint*)cpvClu,vecCpvGlobal);
+ for (Int_t ixyz=0; ixyz<3; ixyz++)
+ xyz[ixyz] = vecCpvGlobal[ixyz];
+ PropagateToPlane(vecDist,xyz,pxyz,"EMC",cpvClu->GetPHOSMod());
+// Info("GetDistanceInPHOSPlane","Track %d propagation to EMC = (%f,%f,%f)",
+// iClosestTrack,vecDist.X(),vecDist.Y(),vecDist.Z());
+ vecDist -= vecEmc;
+ distance2Track = TMath::Sqrt(vecDist.X()*vecDist.X() + vecDist.Z()*vecDist.Z());
+ }
+ }
+// } else {
+// // If no ESD exists, than simply find EMC-CPV distance
+// distance = (vecCpv - vecEmc).Mag() ;
+
+ //if(distance2Track < fRcpv + 2*delta )
+ if(distance2Track < fRtpc )
+ trackindex = iClosestTrack ;
+ // toofar = kFALSE ;
+ }
+ // Info("GetDistanceInPHOSPlane","cpv-emc distance is %f cm",
+ // distance);
+ }
+
+ return distance2Cpv ;
+}