From 7cbd5877332dc7550b620814acc72717bf245eaf Mon Sep 17 00:00:00 2001 From: Andrea Bocci Date: Sun, 17 Mar 2024 12:07:26 +0100 Subject: [PATCH 1/2] Clean up PixelTrackProducerFromSoA code --- .../plugins/PixelTrackProducerFromSoA.cc | 80 ++++++++++--------- .../PixelTrackProducerFromSoAAlpaka.cc | 29 ++++--- 2 files changed, 58 insertions(+), 51 deletions(-) diff --git a/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoA.cc b/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoA.cc index 0adba7ef830a5..ef65feb4f8b28 100644 --- a/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoA.cc +++ b/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoA.cc @@ -1,60 +1,64 @@ +#include +#include +//#include +#include +#include +#include +#include +#include + +#include "CUDADataFormats/Common/interface/HostProduct.h" +#include "CUDADataFormats/SiPixelCluster/interface/gpuClusteringConstants.h" +#include "CUDADataFormats/Track/interface/PixelTrackUtilities.h" +#include "CUDADataFormats/Track/interface/TrackSoAHeterogeneousDevice.h" +#include "CUDADataFormats/Track/interface/TrackSoAHeterogeneousHost.h" #include "DataFormats/BeamSpot/interface/BeamSpot.h" #include "DataFormats/Common/interface/OrphanHandle.h" +#include "DataFormats/GeometrySurface/interface/Plane.h" #include "DataFormats/TrackReco/interface/Track.h" #include "DataFormats/TrackReco/interface/TrackExtra.h" #include "DataFormats/TrackReco/interface/TrackFwd.h" #include "DataFormats/TrackerCommon/interface/TrackerTopology.h" -#include "DataFormats/TrajectoryState/interface/LocalTrajectoryParameters.h" -#include "DataFormats/GeometrySurface/interface/Plane.h" #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h" +#include "DataFormats/TrajectoryState/interface/LocalTrajectoryParameters.h" +#include "FWCore/Framework/interface/ConsumesCollector.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/EventSetup.h" #include "FWCore/Framework/interface/MakerMacros.h" #include "FWCore/Framework/interface/global/EDProducer.h" -#include "FWCore/Framework/interface/ConsumesCollector.h" #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/ParameterSet/interface/ParameterSetDescription.h" -#include "FWCore/Utilities/interface/InputTag.h" #include "FWCore/PluginManager/interface/ModuleDef.h" #include "FWCore/Utilities/interface/EDGetToken.h" +#include "FWCore/Utilities/interface/InputTag.h" +#include "Geometry/CommonTopologies/interface/SimplePixelTopology.h" #include "Geometry/Records/interface/TrackerTopologyRcd.h" #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" - +#include "RecoTracker/PixelTrackFitting/interface/FitUtils.h" #include "TrackingTools/AnalyticalJacobians/interface/JacobianLocalToCurvilinear.h" -#include "TrackingTools/TrajectoryParametrization/interface/GlobalTrajectoryParameters.h" #include "TrackingTools/TrajectoryParametrization/interface/CurvilinearTrajectoryError.h" -#include "RecoTracker/PixelTrackFitting/interface/FitUtils.h" - -#include "CUDADataFormats/Common/interface/HostProduct.h" -#include "CUDADataFormats/SiPixelCluster/interface/gpuClusteringConstants.h" -#include "Geometry/CommonTopologies/interface/SimplePixelTopology.h" +#include "TrackingTools/TrajectoryParametrization/interface/GlobalTrajectoryParameters.h" #include "storeTracks.h" -#include "CUDADataFormats/Track/interface/TrackSoAHeterogeneousHost.h" -#include "CUDADataFormats/Track/interface/TrackSoAHeterogeneousDevice.h" -#include "CUDADataFormats/Track/interface/PixelTrackUtilities.h" - /** - * This class creates "leagcy" reco::Track + * This class creates "legacy" reco::Track * objects from the output of SoA CA. */ template class PixelTrackProducerFromSoAT : public edm::global::EDProducer<> { using TrackSoAHost = TrackSoAHeterogeneousHost; - using tracksHelpers = TracksUtilities; - -public: + using TracksHelpers = TracksUtilities; + using HMSstorage = HostProduct; using IndToEdm = std::vector; +public: explicit PixelTrackProducerFromSoAT(const edm::ParameterSet &iConfig); ~PixelTrackProducerFromSoAT() override = default; static void fillDescriptions(edm::ConfigurationDescriptions &descriptions); - using HMSstorage = HostProduct; - private: void produce(edm::StreamID streamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override; @@ -140,24 +144,23 @@ void PixelTrackProducerFromSoAT::produce(edm::StreamID streamID, auto const &rechits = iEvent.get(cpuHits_); std::vector hitmap; auto const &rcs = rechits.data(); - auto nhits = rcs.size(); + auto const nhits = rcs.size(); hitmap.resize(nhits, nullptr); auto const *hitsModuleStart = iEvent.get(hmsToken_).get(); - auto fc = hitsModuleStart; - for (auto const &h : rcs) { - auto const &thit = static_cast(h); - auto detI = thit.det()->index(); + for (auto const &hit : rcs) { + auto const &thit = static_cast(hit); + auto const detI = thit.det()->index(); auto const &clus = thit.firstClusterRef(); assert(clus.isPixel()); - auto i = fc[detI] + clus.pixelCluster().originalId(); - if (i >= hitmap.size()) - hitmap.resize(i + 256, nullptr); // only in case of hit overflow in one module + auto const idx = hitsModuleStart[detI] + clus.pixelCluster().originalId(); + if (idx >= hitmap.size()) + hitmap.resize(idx + 256, nullptr); // only in case of hit overflow in one module - assert(nullptr == hitmap[i]); - hitmap[i] = &h; + assert(nullptr == hitmap[idx]); + hitmap[idx] = &hit; } std::vector hits; @@ -172,10 +175,10 @@ void PixelTrackProducerFromSoAT::produce(edm::StreamID streamID, int32_t nt = 0; - //sort index by pt + // sort index by pt std::vector sortIdxs(nTracks); std::iota(sortIdxs.begin(), sortIdxs.end(), 0); - //sort good-quality tracks by pt, keep bad-quality tracks in the bottom + // sort good-quality tracks by pt, keep bad-quality tracks in the bottom std::sort(sortIdxs.begin(), sortIdxs.end(), [&](int32_t const i1, int32_t const i2) { if (quality[i1] >= minQuality_ && quality[i2] >= minQuality_) return tsoa.view()[i1].pt() > tsoa.view()[i2].pt(); @@ -183,16 +186,16 @@ void PixelTrackProducerFromSoAT::produce(edm::StreamID streamID, return quality[i1] > quality[i2]; }); - //store the index of the SoA: indToEdm[index_SoAtrack] -> index_edmTrack (if it exists) + // store the index of the SoA: indToEdm[index_SoAtrack] -> index_edmTrack (if it exists) indToEdm.resize(sortIdxs.size(), -1); for (const auto &it : sortIdxs) { - auto nHits = tracksHelpers::nHits(tsoa.view(), it); + auto nHits = TracksHelpers::nHits(tsoa.view(), it); assert(nHits >= 3); auto q = quality[it]; if (q < minQuality_) continue; - if (nHits < minNumberOfHits_) //move to nLayers? + if (nHits < minNumberOfHits_) // move to nLayers? continue; indToEdm[it] = nt; ++nt; @@ -203,13 +206,12 @@ void PixelTrackProducerFromSoAT::produce(edm::StreamID streamID, hits[iHit] = hitmap[*(b + iHit)]; // mind: this values are respect the beamspot! - float chi2 = tsoa.view()[it].chi2(); - float phi = tracksHelpers::phi(tsoa.view(), it); + float phi = TracksHelpers::phi(tsoa.view(), it); riemannFit::Vector5d ipar, opar; riemannFit::Matrix5d icov, ocov; - tracksHelpers::template copyToDense(tsoa.view(), ipar, icov, it); + TracksHelpers::template copyToDense(tsoa.view(), ipar, icov, it); riemannFit::transformToPerigeePlane(ipar, icov, opar, ocov); LocalTrajectoryParameters lpar(opar(0), opar(1), opar(2), opar(3), opar(4), 1.); diff --git a/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoAAlpaka.cc b/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoAAlpaka.cc index 4402a1891b2a4..b6b21d5c17199 100644 --- a/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoAAlpaka.cc +++ b/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoAAlpaka.cc @@ -1,12 +1,20 @@ +#include +#include +#include +#include +#include +#include +#include #include #include "DataFormats/BeamSpot/interface/BeamSpot.h" #include "DataFormats/GeometrySurface/interface/Plane.h" #include "DataFormats/SiPixelClusterSoA/interface/ClusteringConstants.h" -#include "DataFormats/TrackSoA/interface/TracksHost.h" #include "DataFormats/TrackReco/interface/Track.h" #include "DataFormats/TrackReco/interface/TrackExtra.h" #include "DataFormats/TrackReco/interface/TrackFwd.h" +#include "DataFormats/TrackSoA/interface/TracksHost.h" +#include "DataFormats/TrackSoA/interface/alpaka/TrackUtilities.h" #include "DataFormats/TrackerCommon/interface/TrackerTopology.h" #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h" #include "DataFormats/TrajectoryState/interface/LocalTrajectoryParameters.h" @@ -22,13 +30,11 @@ #include "Geometry/CommonTopologies/interface/SimplePixelTopology.h" #include "Geometry/Records/interface/TrackerTopologyRcd.h" #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h" +#include "RecoTracker/PixelTrackFitting/interface/alpaka/FitUtils.h" #include "TrackingTools/AnalyticalJacobians/interface/JacobianLocalToCurvilinear.h" #include "TrackingTools/TrajectoryParametrization/interface/CurvilinearTrajectoryError.h" #include "TrackingTools/TrajectoryParametrization/interface/GlobalTrajectoryParameters.h" -#include "DataFormats/TrackSoA/interface/alpaka/TrackUtilities.h" -#include "RecoTracker/PixelTrackFitting/interface/alpaka/FitUtils.h" - #include "storeTracks.h" /** @@ -40,13 +46,12 @@ template class PixelTrackProducerFromSoAAlpaka : public edm::global::EDProducer<> { - using TkSoAHost = TracksHost; - using tracksHelpers = TracksUtilities; + using TrackSoAHost = TracksHost; + using TracksHelpers = TracksUtilities; using HMSstorage = std::vector; - -public: using IndToEdm = std::vector; +public: explicit PixelTrackProducerFromSoAAlpaka(const edm::ParameterSet &iConfig); ~PixelTrackProducerFromSoAAlpaka() override = default; @@ -57,7 +62,7 @@ class PixelTrackProducerFromSoAAlpaka : public edm::global::EDProducer<> { // Event Data tokens const edm::EDGetTokenT tBeamSpot_; - const edm::EDGetTokenT tokenTrack_; + const edm::EDGetTokenT tokenTrack_; const edm::EDGetTokenT cpuHits_; const edm::EDGetTokenT hmsToken_; // Event Setup tokens @@ -180,7 +185,7 @@ void PixelTrackProducerFromSoAAlpaka::produce(edm::StreamID strea //store the index of the SoA: indToEdm[index_SoAtrack] -> index_edmTrack (if it exists) indToEdm.resize(sortIdxs.size(), -1); for (const auto &it : sortIdxs) { - auto nHits = tracksHelpers::nHits(tsoa.view(), it); + auto nHits = TracksHelpers::nHits(tsoa.view(), it); assert(nHits >= 3); auto q = quality[it]; @@ -197,13 +202,12 @@ void PixelTrackProducerFromSoAAlpaka::produce(edm::StreamID strea hits[iHit] = hitmap[*(b + iHit)]; // mind: this values are respect the beamspot! - float chi2 = tsoa.view()[it].chi2(); float phi = reco::phi(tsoa.view(), it); riemannFit::Vector5d ipar, opar; riemannFit::Matrix5d icov, ocov; - tracksHelpers::template copyToDense(tsoa.view(), ipar, icov, it); + TracksHelpers::template copyToDense(tsoa.view(), ipar, icov, it); riemannFit::transformToPerigeePlane(ipar, icov, opar, ocov); LocalTrajectoryParameters lpar(opar(0), opar(1), opar(2), opar(3), opar(4), 1.); @@ -249,6 +253,7 @@ void PixelTrackProducerFromSoAAlpaka::produce(edm::StreamID strea #ifdef GPU_DEBUG std::cout << "processed " << nt << " good tuples " << tracks.size() << " out of " << indToEdm.size() << std::endl; #endif + // store tracks storeTracks(iEvent, tracks, httopo); iEvent.put(std::move(indToEdmP)); From 72686871aa6538ff79b66f36705a26e734c6a0e5 Mon Sep 17 00:00:00 2001 From: Andrea Bocci Date: Sun, 17 Mar 2024 12:12:50 +0100 Subject: [PATCH 2/2] Sort by pT only good-quality tracks Port from CUDA to Alpaka the changes in #42428 by Silvio Donato. --- .../plugins/PixelTrackProducerFromSoAAlpaka.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoAAlpaka.cc b/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoAAlpaka.cc index b6b21d5c17199..5769c8c53976c 100644 --- a/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoAAlpaka.cc +++ b/RecoTracker/PixelTrackFitting/plugins/PixelTrackProducerFromSoAAlpaka.cc @@ -178,8 +178,12 @@ void PixelTrackProducerFromSoAAlpaka::produce(edm::StreamID strea //sort index by pt std::vector sortIdxs(nTracks); std::iota(sortIdxs.begin(), sortIdxs.end(), 0); + // sort good-quality tracks by pt, keep bad-quality tracks at the bottom std::sort(sortIdxs.begin(), sortIdxs.end(), [&](int32_t const i1, int32_t const i2) { - return tsoa.view()[i1].pt() > tsoa.view()[i2].pt(); + if (quality[i1] >= minQuality_ && quality[i2] >= minQuality_) + return tsoa.view()[i1].pt() > tsoa.view()[i2].pt(); + else + return quality[i1] > quality[i2]; }); //store the index of the SoA: indToEdm[index_SoAtrack] -> index_edmTrack (if it exists)