CMS 3D CMS Logo

PixelTrackProducerFromSoAAlpaka.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cmath>
3 #include <iostream>
4 #include <memory>
5 #include <numeric>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
37 
38 #include "storeTracks.h"
39 
45 //#define GPU_DEBUG
46 
47 template <typename TrackerTraits>
51  using HMSstorage = std::vector<uint32_t>;
52  using IndToEdm = std::vector<uint32_t>;
53 
54 public:
55  explicit PixelTrackProducerFromSoAAlpaka(const edm::ParameterSet &iConfig);
56  ~PixelTrackProducerFromSoAAlpaka() override = default;
57 
58  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
59 
60 private:
61  void produce(edm::StreamID streamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override;
62 
63  // Event Data tokens
68  // Event Setup tokens
71 
72  int32_t const minNumberOfHits_;
74 };
75 
76 template <typename TrackerTraits>
78  : tBeamSpot_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"))),
79  tokenTrack_(consumes(iConfig.getParameter<edm::InputTag>("trackSrc"))),
80  cpuHits_(consumes<SiPixelRecHitCollectionNew>(iConfig.getParameter<edm::InputTag>("pixelRecHitLegacySrc"))),
81  hmsToken_(consumes<HMSstorage>(iConfig.getParameter<edm::InputTag>("pixelRecHitLegacySrc"))),
82  idealMagneticFieldToken_(esConsumes()),
83  ttTopoToken_(esConsumes()),
84  minNumberOfHits_(iConfig.getParameter<int>("minNumberOfHits")),
85  minQuality_(pixelTrack::qualityByName(iConfig.getParameter<std::string>("minQuality"))) {
87  throw cms::Exception("PixelTrackConfiguration")
88  << iConfig.getParameter<std::string>("minQuality") + " is not a pixelTrack::Quality";
89  }
91  throw cms::Exception("PixelTrackConfiguration")
92  << iConfig.getParameter<std::string>("minQuality") + " not supported";
93  }
94  produces<TrackingRecHitCollection>();
95  produces<reco::TrackExtraCollection>();
96  // TrackCollection refers to TrackingRechit and TrackExtra
97  // collections, need to declare its production after them to work
98  // around a rare race condition in framework scheduling
99  produces<reco::TrackCollection>();
100  produces<IndToEdm>();
101 }
102 
103 template <typename TrackerTraits>
106  desc.add<edm::InputTag>("beamSpot", edm::InputTag("offlineBeamSpot"));
107  desc.add<edm::InputTag>("trackSrc", edm::InputTag("pixelTracksAlpaka"));
108  desc.add<edm::InputTag>("pixelRecHitLegacySrc", edm::InputTag("siPixelRecHitsPreSplittingLegacy"));
109  desc.add<int>("minNumberOfHits", 0);
110  desc.add<std::string>("minQuality", "loose");
111  descriptions.addWithDefaultLabel(desc);
112 }
113 
114 template <typename TrackerTraits>
117  const edm::EventSetup &iSetup) const {
118  // enum class Quality : uint8_t { bad = 0, edup, dup, loose, strict, tight, highPurity };
127 
128 #ifdef GPU_DEBUG
129  std::cout << "Converting soa helix in reco tracks" << std::endl;
130 #endif
131 
132  auto indToEdmP = std::make_unique<IndToEdm>();
133  auto &indToEdm = *indToEdmP;
134 
135  auto const &idealField = iSetup.getData(idealMagneticFieldToken_);
136 
138 
139  auto const &httopo = iSetup.getData(ttTopoToken_);
140 
141  const auto &bsh = iEvent.get(tBeamSpot_);
142  GlobalPoint bs(bsh.x0(), bsh.y0(), bsh.z0());
143 
144  auto const &rechits = iEvent.get(cpuHits_);
145  std::vector<TrackingRecHit const *> hitmap;
146  auto const &rcs = rechits.data();
147  auto const nhits = rcs.size();
148 
149  hitmap.resize(nhits, nullptr);
150 
151  auto const &hitsModuleStart = iEvent.get(hmsToken_);
152 
153  for (auto const &hit : rcs) {
154  auto const &thit = static_cast<BaseTrackerRecHit const &>(hit);
155  auto const detI = thit.det()->index();
156  auto const &clus = thit.firstClusterRef();
157  assert(clus.isPixel());
158  auto const idx = hitsModuleStart[detI] + clus.pixelCluster().originalId();
159  if (idx >= hitmap.size())
160  hitmap.resize(idx + 256, nullptr); // only in case of hit overflow in one module
161 
162  assert(nullptr == hitmap[idx]);
163  hitmap[idx] = &hit;
164  }
165 
166  std::vector<const TrackingRecHit *> hits;
167  hits.reserve(5);
168 
169  auto const &tsoa = iEvent.get(tokenTrack_);
170  auto const *quality = tsoa.view().quality();
171  auto const &hitIndices = tsoa.view().hitIndices();
172  auto nTracks = tsoa.view().nTracks();
173 
174  tracks.reserve(nTracks);
175 
176  int32_t nt = 0;
177 
178  //sort index by pt
179  std::vector<int32_t> sortIdxs(nTracks);
180  std::iota(sortIdxs.begin(), sortIdxs.end(), 0);
181  // sort good-quality tracks by pt, keep bad-quality tracks at the bottom
182  std::sort(sortIdxs.begin(), sortIdxs.end(), [&](int32_t const i1, int32_t const i2) {
183  if (quality[i1] >= minQuality_ && quality[i2] >= minQuality_)
184  return tsoa.view()[i1].pt() > tsoa.view()[i2].pt();
185  else
186  return quality[i1] > quality[i2];
187  });
188 
189  //store the index of the SoA: indToEdm[index_SoAtrack] -> index_edmTrack (if it exists)
190  indToEdm.resize(sortIdxs.size(), -1);
191  for (const auto &it : sortIdxs) {
192  auto nHits = TracksHelpers::nHits(tsoa.view(), it);
193  assert(nHits >= 3);
194  auto q = quality[it];
195 
196  if (q < minQuality_)
197  continue;
198  if (nHits < minNumberOfHits_) //move to nLayers?
199  continue;
200  indToEdm[it] = nt;
201  ++nt;
202 
203  hits.resize(nHits);
204  auto b = hitIndices.begin(it);
205  for (int iHit = 0; iHit < nHits; ++iHit)
206  hits[iHit] = hitmap[*(b + iHit)];
207 
208  // mind: this values are respect the beamspot!
209  float chi2 = tsoa.view()[it].chi2();
210  float phi = reco::phi(tsoa.view(), it);
211 
212  riemannFit::Vector5d ipar, opar;
213  riemannFit::Matrix5d icov, ocov;
214  TracksHelpers::template copyToDense<riemannFit::Vector5d, riemannFit::Matrix5d>(tsoa.view(), ipar, icov, it);
215  riemannFit::transformToPerigeePlane(ipar, icov, opar, ocov);
216 
217  LocalTrajectoryParameters lpar(opar(0), opar(1), opar(2), opar(3), opar(4), 1.);
219  for (int i = 0; i < 5; ++i)
220  for (int j = i; j < 5; ++j)
221  m(i, j) = ocov(i, j);
222 
223  float sp = std::sin(phi);
224  float cp = std::cos(phi);
225  Surface::RotationType rot(sp, -cp, 0, 0, 0, -1.f, cp, sp, 0);
226 
227  Plane impPointPlane(bs, rot);
229  impPointPlane.toGlobal(lpar.position()), impPointPlane.toGlobal(lpar.momentum()), lpar.charge(), &idealField);
230  JacobianLocalToCurvilinear jl2c(impPointPlane, lpar, idealField);
231 
232  AlgebraicSymMatrix55 mo = ROOT::Math::Similarity(jl2c.jacobian(), m);
233 
234  int ndof = 2 * hits.size() - 5;
235  chi2 = chi2 * ndof;
236  GlobalPoint vv = gp.position();
237  math::XYZPoint pos(vv.x(), vv.y(), vv.z());
238  GlobalVector pp = gp.momentum();
239  math::XYZVector mom(pp.x(), pp.y(), pp.z());
240 
241  auto track = std::make_unique<reco::Track>(chi2, ndof, pos, mom, gp.charge(), CurvilinearTrajectoryError(mo));
242 
243  // bad and edup not supported as fit not present or not reliable
244  auto tkq = recoQuality[int(q)];
245  track->setQuality(tkq);
246  // loose,tight and HP are inclusive
247  if (reco::TrackBase::highPurity == tkq) {
248  track->setQuality(reco::TrackBase::tight);
249  track->setQuality(reco::TrackBase::loose);
250  } else if (reco::TrackBase::tight == tkq) {
251  track->setQuality(reco::TrackBase::loose);
252  }
253  track->setQuality(tkq);
254  // filter???
255  tracks.emplace_back(track.release(), hits);
256  }
257 #ifdef GPU_DEBUG
258  std::cout << "processed " << nt << " good tuples " << tracks.size() << " out of " << indToEdm.size() << std::endl;
259 #endif
260 
261  // store tracks
262  storeTracks(iEvent, tracks, httopo);
263  iEvent.put(std::move(indToEdmP));
264 }
265 
269 
PixelTrackProducerFromSoAAlpaka(const edm::ParameterSet &iConfig)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
Eigen::Matrix< double, 5, 5 > Matrix5d
Definition: FitResult.h:17
const edm::EDGetTokenT< reco::BeamSpot > tBeamSpot_
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
Quality qualityByName(std::string const &name)
void storeTracks(Ev &ev, const TWH &tracksWithHits, const TrackerTopology &ttopo)
Definition: storeTracks.h:19
T z() const
Definition: PV3DBase.h:61
~PixelTrackProducerFromSoAAlpaka() override=default
TrackQuality
track quality
Definition: TrackBase.h:150
int index() const
Definition: GeomDet.h:83
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Eigen::Matrix< double, 5, 1 > Vector5d
Definition: FitResult.h:13
assert(be >=bs)
const edm::EDGetTokenT< HMSstorage > hmsToken_
void transformToPerigeePlane(VI5 const &ip, MI5 const &icov, VO5 &op, MO5 &ocov)
Definition: FitUtils.h:60
Definition: Plane.h:16
const GeomDet * det() const
string quality
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
int iEvent
Definition: GenABIO.cc:224
void produce(edm::StreamID streamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
const edm::EDGetTokenT< SiPixelRecHitCollectionNew > cpuHits_
LocalVector momentum() const
Momentum vector in the local frame.
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr float phi(ConstView const &tracks, int32_t i)
Definition: TracksSoA.h:80
double f[11][100]
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int nt
Definition: AMPTWrapper.h:42
TrackCharge charge() const
Charge (-1, 0 or 1)
GlobalPoint toGlobal(const Point2DBase< Scalar, LocalTag > lp) const
Definition: Surface.h:79
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
double b
Definition: hdecay.h:120
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > ttTopoToken_
fixed size matrix
HLT enums.
std::vector< TrackWithRecHits > TracksWithRecHits
const edm::EDGetTokenT< TrackSoAHost > tokenTrack_
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > idealMagneticFieldToken_
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
LocalPoint position() const
Local x and y position coordinates.
def move(src, dest)
Definition: eostools.py:511