Skip to content

Commit

Permalink
Revert moving general purpose functions to common CMSSW file
Browse files Browse the repository at this point in the history
  • Loading branch information
VourMa committed Jul 27, 2024
1 parent 9268f49 commit e076050
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 249 deletions.
29 changes: 0 additions & 29 deletions HeterogeneousCore/AlpakaInterface/interface/binarySearch.h

This file was deleted.

57 changes: 0 additions & 57 deletions HeterogeneousCore/AlpakaInterface/interface/geomFunctions.h

This file was deleted.

2 changes: 0 additions & 2 deletions RecoTracker/LST/plugins/LSTPhase2OTHitsInputProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ void LSTPhase2OTHitsInputProducer::produce(edm::StreamID iID, edm::Event& iEvent
ph2_hits.reserve(phase2OTHits.dataSize());

for (auto const& it : phase2OTHits) {
//for (auto it = phase2OTHits.begin(); it != phase2OTHits.end(); it++) {
const DetId hitId = it.detId();
for (auto const& hit : it) {
//for (auto hit = it->begin(); hit != it->end(); hit++) {
ph2_detId.push_back(hitId.rawId());
ph2_x.push_back(hit.globalPosition().x());
ph2_y.push_back(hit.globalPosition().y());
Expand Down
78 changes: 73 additions & 5 deletions RecoTracker/LSTCore/src/alpaka/Hit.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef RecoTracker_LSTCore_src_alpaka_Hit_h
#define RecoTracker_LSTCore_src_alpaka_Hit_h

#include "HeterogeneousCore/AlpakaInterface/interface/binarySearch.h"
#include "HeterogeneousCore/AlpakaInterface/interface/geomFunctions.h"
#include "RecoTracker/LSTCore/interface/alpaka/Constants.h"
#include "RecoTracker/LSTCore/interface/Module.h"

Expand Down Expand Up @@ -109,6 +107,76 @@ namespace lst {
inline void setData(HitsBuffer& buf) { data_.setData(buf); }
};

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float eta(TAcc const& acc, float x, float y, float z) {
float r3 = alpaka::math::sqrt(acc, x * x + y * y + z * z);
float rt = alpaka::math::sqrt(acc, x * x + y * y);
float eta = ((z > 0) - (z < 0)) * alpaka::math::acosh(acc, r3 / rt);
return eta;
};

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float phi_mpi_pi(TAcc const& acc, float x) {
if (alpaka::math::abs(acc, x) <= float(M_PI))
return x;

constexpr float o2pi = 1.f / (2.f * float(M_PI));
float n = alpaka::math::round(acc, x * o2pi);
return x - n * float(2.f * float(M_PI));
};

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float phi(TAcc const& acc, float x, float y) {
return phi_mpi_pi(acc, float(M_PI) + alpaka::math::atan2(acc, -y, -x));
};

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float deltaPhi(TAcc const& acc, float x1, float y1, float x2, float y2) {
float phi1 = phi(acc, x1, y1);
float phi2 = phi(acc, x2, y2);
return phi_mpi_pi(acc, (phi2 - phi1));
};

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float deltaPhiChange(TAcc const& acc, float x1, float y1, float x2, float y2) {
return deltaPhi(acc, x1, y1, x2 - x1, y2 - y1);
};

ALPAKA_FN_ACC ALPAKA_FN_INLINE float calculate_dPhi(float phi1, float phi2) {
// Calculate dPhi
float dPhi = phi1 - phi2;

// Normalize dPhi to be between -pi and pi
if (dPhi > float(M_PI)) {
dPhi -= 2 * float(M_PI);
} else if (dPhi < -float(M_PI)) {
dPhi += 2 * float(M_PI);
}

return dPhi;
};

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE int binary_search(const unsigned int* data, // Array that we are searching over
unsigned int search_val, // Value we want to find in data array
unsigned int ndata) // Number of elements in data array
{
unsigned int low = 0;
unsigned int high = ndata - 1;

while (low <= high) {
unsigned int mid = (low + high) / 2;
unsigned int test_val = data[mid];
if (test_val == search_val)
return mid;
else if (test_val > search_val)
high = mid - 1;
else
low = mid + 1;
}
// Couldn't find search value in array.
return -1;
};

struct moduleRangesKernel {
template <typename TAcc>
ALPAKA_FN_ACC void operator()(TAcc const& acc,
Expand Down Expand Up @@ -154,19 +222,19 @@ namespace lst {
int iDetId = hitsInGPU.detid[ihit];

hitsInGPU.rts[ihit] = alpaka::math::sqrt(acc, ihit_x * ihit_x + ihit_y * ihit_y);
hitsInGPU.phis[ihit] = cms::alpakatools::phi(acc, ihit_x, ihit_y);
hitsInGPU.phis[ihit] = lst::phi(acc, ihit_x, ihit_y);
hitsInGPU.etas[ihit] =
((ihit_z > 0) - (ihit_z < 0)) *
alpaka::math::acosh(
acc,
alpaka::math::sqrt(acc, ihit_x * ihit_x + ihit_y * ihit_y + ihit_z * ihit_z) / hitsInGPU.rts[ihit]);
int found_index = cms::alpakatools::binary_search(modulesInGPU.mapdetId, iDetId, nModules);
int found_index = binary_search(modulesInGPU.mapdetId, iDetId, nModules);
uint16_t lastModuleIndex = modulesInGPU.mapIdx[found_index];

hitsInGPU.moduleIndices[ihit] = lastModuleIndex;

if (modulesInGPU.subdets[lastModuleIndex] == Endcap && modulesInGPU.moduleType[lastModuleIndex] == TwoS) {
found_index = cms::alpakatools::binary_search(geoMapDetId, iDetId, nEndCapMap);
found_index = binary_search(geoMapDetId, iDetId, nEndCapMap);
float phi = geoMapPhi[found_index];
float cos_phi = alpaka::math::cos(acc, phi);
hitsInGPU.highEdgeXs[ihit] = ihit_x + 2.5f * cos_phi;
Expand Down
6 changes: 3 additions & 3 deletions RecoTracker/LSTCore/src/alpaka/Kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ namespace lst {
float eta2 = __H2F(quintupletsInGPU.eta[jx]);
float phi2 = __H2F(quintupletsInGPU.phi[jx]);
float dEta = alpaka::math::abs(acc, eta1 - eta2);
float dPhi = cms::alpakatools::calculate_dPhi(phi1, phi2);
float dPhi = lst::calculate_dPhi(phi1, phi2);
float score_rphisum2 = __H2F(quintupletsInGPU.score_rphisum[jx]);

if (dEta > 0.1f)
Expand Down Expand Up @@ -239,7 +239,7 @@ namespace lst {
float score_rphisum2 = __H2F(quintupletsInGPU.score_rphisum[jx]);

float dEta = alpaka::math::abs(acc, eta1 - eta2);
float dPhi = cms::alpakatools::calculate_dPhi(phi1, phi2);
float dPhi = lst::calculate_dPhi(phi1, phi2);

if (dEta > 0.1f)
continue;
Expand Down Expand Up @@ -410,7 +410,7 @@ namespace lst {
}
if (secondpass) {
float dEta = alpaka::math::abs(acc, eta_pix1 - eta_pix2);
float dPhi = cms::alpakatools::calculate_dPhi(phi_pix1, phi_pix2);
float dPhi = lst::calculate_dPhi(phi_pix1, phi_pix2);

float dR2 = dEta * dEta + dPhi * dPhi;
if ((npMatched >= 1) || (dR2 < 1e-5f)) {
Expand Down
42 changes: 20 additions & 22 deletions RecoTracker/LSTCore/src/alpaka/MiniDoublet.h
Original file line number Diff line number Diff line change
Expand Up @@ -707,21 +707,21 @@ namespace lst {
shiftedZ = zUpper;
shiftedRt2 = xn * xn + yn * yn;

dPhi = cms::alpakatools::deltaPhi(acc, xLower, yLower, shiftedX, shiftedY); //function from Hit.cc
noShiftedDphi = cms::alpakatools::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
dPhi = lst::deltaPhi(acc, xLower, yLower, shiftedX, shiftedY); //function from Hit.cc
noShiftedDphi = lst::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
} else {
shiftedX = xn;
shiftedY = yn;
shiftedZ = zLower;
shiftedRt2 = xn * xn + yn * yn;
dPhi = cms::alpakatools::deltaPhi(acc, shiftedX, shiftedY, xUpper, yUpper);
noShiftedDphi = cms::alpakatools::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
dPhi = lst::deltaPhi(acc, shiftedX, shiftedY, xUpper, yUpper);
noShiftedDphi = lst::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
}
} else {
shiftedX = 0;
shiftedY = 0;
shiftedZ = 0;
dPhi = cms::alpakatools::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
dPhi = lst::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
noShiftedDphi = dPhi;
}

Expand All @@ -740,26 +740,24 @@ namespace lst {
// But I still placed this check for safety. (TODO: After checking explicitly if not needed remove later?)
// setdeltaPhiChange(lowerHit.rt() < upperHitMod.rt() ? lowerHit.deltaPhiChange(upperHitMod) : upperHitMod.deltaPhiChange(lowerHit));

dPhiChange = (rtLower * rtLower < shiftedRt2)
? cms::alpakatools::deltaPhiChange(acc, xLower, yLower, shiftedX, shiftedY)
: cms::alpakatools::deltaPhiChange(acc, shiftedX, shiftedY, xLower, yLower);
noShiftedDphiChange = rtLower < rtUpper ? cms::alpakatools::deltaPhiChange(acc, xLower, yLower, xUpper, yUpper)
: cms::alpakatools::deltaPhiChange(acc, xUpper, yUpper, xLower, yLower);
dPhiChange = (rtLower * rtLower < shiftedRt2) ? lst::deltaPhiChange(acc, xLower, yLower, shiftedX, shiftedY)
: lst::deltaPhiChange(acc, shiftedX, shiftedY, xLower, yLower);
noShiftedDphiChange = rtLower < rtUpper ? lst::deltaPhiChange(acc, xLower, yLower, xUpper, yUpper)
: lst::deltaPhiChange(acc, xUpper, yUpper, xLower, yLower);
} else {
// dPhi Change should be calculated so that the upper hit has higher rt.
// In principle, this kind of check rt_lower < rt_upper should not be necessary because the hit shifting should have taken care of this.
// (i.e. the strip hit is shifted to be aligned in the line of sight from interaction point to pixel hit of PS module guaranteeing rt ordering)
// But I still placed this check for safety. (TODO: After checking explicitly if not needed remove later?)

dPhiChange = (shiftedRt2 < rtUpper * rtUpper)
? cms::alpakatools::deltaPhiChange(acc, shiftedX, shiftedY, xUpper, yUpper)
: cms::alpakatools::deltaPhiChange(acc, xUpper, yUpper, shiftedX, shiftedY);
noShiftedDphiChange = rtLower < rtUpper ? cms::alpakatools::deltaPhiChange(acc, xLower, yLower, xUpper, yUpper)
: cms::alpakatools::deltaPhiChange(acc, xUpper, yUpper, xLower, yLower);
dPhiChange = (shiftedRt2 < rtUpper * rtUpper) ? lst::deltaPhiChange(acc, shiftedX, shiftedY, xUpper, yUpper)
: lst::deltaPhiChange(acc, xUpper, yUpper, shiftedX, shiftedY);
noShiftedDphiChange = rtLower < rtUpper ? lst::deltaPhiChange(acc, xLower, yLower, xUpper, yUpper)
: lst::deltaPhiChange(acc, xUpper, yUpper, xLower, yLower);
}
} else {
// When it is flat lying module, whichever is the lowerSide will always have rt lower
dPhiChange = cms::alpakatools::deltaPhiChange(acc, xLower, yLower, xUpper, yUpper);
dPhiChange = lst::deltaPhiChange(acc, xLower, yLower, xUpper, yUpper);
noShiftedDphiChange = dPhiChange;
}

Expand Down Expand Up @@ -836,21 +834,21 @@ namespace lst {
shiftedX = xn;
shiftedY = yn;
shiftedZ = zUpper;
dPhi = cms::alpakatools::deltaPhi(acc, xLower, yLower, shiftedX, shiftedY);
noShiftedDphi = cms::alpakatools::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
dPhi = lst::deltaPhi(acc, xLower, yLower, shiftedX, shiftedY);
noShiftedDphi = lst::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
} else {
shiftedX = xn;
shiftedY = yn;
shiftedZ = zLower;
dPhi = cms::alpakatools::deltaPhi(acc, shiftedX, shiftedY, xUpper, yUpper);
noShiftedDphi = cms::alpakatools::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
dPhi = lst::deltaPhi(acc, shiftedX, shiftedY, xUpper, yUpper);
noShiftedDphi = lst::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
}
} else {
shiftedX = xn;
shiftedY = yn;
shiftedZ = zUpper;
dPhi = cms::alpakatools::deltaPhi(acc, xLower, yLower, xn, yn);
noShiftedDphi = cms::alpakatools::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
dPhi = lst::deltaPhi(acc, xLower, yLower, xn, yn);
noShiftedDphi = lst::deltaPhi(acc, xLower, yLower, xUpper, yUpper);
}

// dz needs to change if it is a PS module where the strip hits are shifted in order to properly account for the case when a tilted module falls under "endcap logic"
Expand Down
Loading

0 comments on commit e076050

Please sign in to comment.