CMS 3D CMS Logo

RiemannFitOnGPU.h
Go to the documentation of this file.
1 //
2 // Author: Felice Pantaleo, CERN
3 //
4 
5 #include <cstdint>
6 
7 #include <cuda_runtime.h>
8 
15 
16 #include "HelixFitOnGPU.h"
17 
18 template <typename TrackerTraits>
20 template <typename TrackerTraits>
22 template <typename TrackerTraits>
24 
25 template <int N, typename TrackerTraits>
26 __global__ void kernel_FastFit(Tuples<TrackerTraits> const *__restrict__ foundNtuplets,
28  uint32_t nHits,
30  double *__restrict__ phits,
31  float *__restrict__ phits_ge,
32  double *__restrict__ pfast_fit,
33  uint32_t offset) {
34  constexpr uint32_t hitsInFit = N;
35 
36  assert(hitsInFit <= nHits);
37 
41 
42  // look in bin for this hit multiplicity
44 
45 #ifdef RIEMANN_DEBUG
46  if (0 == local_start)
47  printf("%d Ntuple of size %d for %d hits to fit\n", tupleMultiplicity->size(nHits), nHits, hitsInFit);
48 #endif
49 
50  for (int local_idx = local_start, nt = riemannFit::maxNumberOfConcurrentFits; local_idx < nt;
51  local_idx += gridDim.x * blockDim.x) {
52  auto tuple_idx = local_idx + offset;
53  if (tuple_idx >= tupleMultiplicity->size(nHits))
54  break;
55 
56  // get it from the ntuple container (one to one to helix)
57  auto tkid = *(tupleMultiplicity->begin(nHits) + tuple_idx);
58  assert(int(tkid) < foundNtuplets->nOnes());
59 
60  assert(foundNtuplets->size(tkid) == nHits);
61 
62  riemannFit::Map3xNd<N> hits(phits + local_idx);
63  riemannFit::Map4d fast_fit(pfast_fit + local_idx);
64  riemannFit::Map6xNf<N> hits_ge(phits_ge + local_idx);
65 
66  // Prepare data structure
67  auto const *hitId = foundNtuplets->begin(tkid);
68  for (unsigned int i = 0; i < hitsInFit; ++i) {
69  auto hit = hitId[i];
70  float ge[6];
71  hh.cpeParams().detParams(hh[hit].detectorIndex()).frame.toGlobal(hh[hit].xerrLocal(), 0, hh[hit].yerrLocal(), ge);
72 
73  hits.col(i) << hh[hit].xGlobal(), hh[hit].yGlobal(), hh[hit].zGlobal();
74  hits_ge.col(i) << ge[0], ge[1], ge[2], ge[3], ge[4], ge[5];
75  }
76  riemannFit::fastFit(hits, fast_fit);
77 
78  // no NaN here....
79  assert(fast_fit(0) == fast_fit(0));
80  assert(fast_fit(1) == fast_fit(1));
81  assert(fast_fit(2) == fast_fit(2));
82  assert(fast_fit(3) == fast_fit(3));
83  }
84 }
85 
86 template <int N, typename TrackerTraits>
87 __global__ void kernel_CircleFit(TupleMultiplicity<TrackerTraits> const *__restrict__ tupleMultiplicity,
88  uint32_t nHits,
89  double bField,
90  double *__restrict__ phits,
91  float *__restrict__ phits_ge,
92  double *__restrict__ pfast_fit_input,
94  uint32_t offset) {
96  assert(N <= nHits);
97 
98  // same as above...
99 
100  // look in bin for this hit multiplicity
101  auto local_start = blockIdx.x * blockDim.x + threadIdx.x;
102  for (int local_idx = local_start, nt = riemannFit::maxNumberOfConcurrentFits; local_idx < nt;
103  local_idx += gridDim.x * blockDim.x) {
104  auto tuple_idx = local_idx + offset;
105  if (tuple_idx >= tupleMultiplicity->size(nHits))
106  break;
107 
108  riemannFit::Map3xNd<N> hits(phits + local_idx);
109  riemannFit::Map4d fast_fit(pfast_fit_input + local_idx);
110  riemannFit::Map6xNf<N> hits_ge(phits_ge + local_idx);
111 
112  riemannFit::VectorNd<N> rad = (hits.block(0, 0, 2, N).colwise().norm());
113 
115  riemannFit::loadCovariance2D(hits_ge, hits_cov);
116 
117  circle_fit[local_idx] = riemannFit::circleFit(hits.block(0, 0, 2, N), hits_cov, fast_fit, rad, bField, true);
118 
119 #ifdef RIEMANN_DEBUG
120 // auto tkid = *(tupleMultiplicity->begin(nHits) + tuple_idx);
121 // printf("kernelCircleFit circle.par(0,1,2): %d %f,%f,%f\n", tkid,
122 // circle_fit[local_idx].par(0), circle_fit[local_idx].par(1), circle_fit[local_idx].par(2));
123 #endif
124  }
125 }
126 
127 template <int N, typename TrackerTraits>
128 __global__ void kernel_LineFit(TupleMultiplicity<TrackerTraits> const *__restrict__ tupleMultiplicity,
129  uint32_t nHits,
130  double bField,
132  double *__restrict__ phits,
133  float *__restrict__ phits_ge,
134  double *__restrict__ pfast_fit_input,
135  riemannFit::CircleFit *__restrict__ circle_fit,
136  uint32_t offset) {
138  assert(N <= nHits);
139 
140  // same as above...
141 
142  // look in bin for this hit multiplicity
143  auto local_start = (blockIdx.x * blockDim.x + threadIdx.x);
144  for (int local_idx = local_start, nt = riemannFit::maxNumberOfConcurrentFits; local_idx < nt;
145  local_idx += gridDim.x * blockDim.x) {
146  auto tuple_idx = local_idx + offset;
147  if (tuple_idx >= tupleMultiplicity->size(nHits))
148  break;
149 
150  // get it for the ntuple container (one to one to helix)
151  int32_t tkid = *(tupleMultiplicity->begin(nHits) + tuple_idx);
152 
153  riemannFit::Map3xNd<N> hits(phits + local_idx);
154  riemannFit::Map4d fast_fit(pfast_fit_input + local_idx);
155  riemannFit::Map6xNf<N> hits_ge(phits_ge + local_idx);
156 
157  auto const &line_fit = riemannFit::lineFit(hits, hits_ge, circle_fit[local_idx], fast_fit, bField, true);
158 
160 
162  circle_fit[local_idx].par,
163  circle_fit[local_idx].cov,
164  line_fit.par,
165  line_fit.cov,
166  1.f / float(bField),
167  tkid);
168  results_view[tkid].pt() = bField / std::abs(circle_fit[local_idx].par(2));
169  results_view[tkid].eta() = asinhf(line_fit.par(0));
170  results_view[tkid].chi2() = (circle_fit[local_idx].chi2 + line_fit.chi2) / (2 * N - 5);
171 
172 #ifdef RIEMANN_DEBUG
173  printf("kernelLineFit size %d for %d hits circle.par(0,1,2): %d %f,%f,%f\n",
174  N,
175  nHits,
176  tkid,
177  circle_fit[local_idx].par(0),
178  circle_fit[local_idx].par(1),
179  circle_fit[local_idx].par(2));
180  printf("kernelLineFit line.par(0,1): %d %f,%f\n", tkid, line_fit.par(0), line_fit.par(1));
181  printf("kernelLineFit chi2 cov %f/%f %e,%e,%e,%e,%e\n",
182  circle_fit[local_idx].chi2,
183  line_fit.chi2,
184  circle_fit[local_idx].cov(0, 0),
185  circle_fit[local_idx].cov(1, 1),
186  circle_fit[local_idx].cov(2, 2),
187  line_fit.cov(0, 0),
188  line_fit.cov(1, 1));
189 #endif
190  }
191 }
const dim3 threadIdx
Definition: cudaCompat.h:29
const dim3 gridDim
Definition: cudaCompat.h:33
#define __global__
Definition: cudaCompat.h:19
const dim3 blockDim
Definition: cudaCompat.h:30
uint32_t double OutputSoAView< TrackerTraits > results_view
Vector3d par
parameter: (X0,Y0,R)
Definition: FitResult.h:27
TrackSoAView< TrackerTraits > OutputSoAView
__host__ __device__ LineFit lineFit(const M3xN &hits, const M6xN &hits_ge, const CircleFit &circle, const V4 &fast_fit, const double bField, const bool error)
Perform an ordinary least square fit in the s-z plane to compute the parameters cotTheta and Zip...
Definition: RiemannFit.h:785
typename TrackSoA< TrackerTraits >::HitContainer Tuples
__host__ __device__ CircleFit circleFit(const M2xN &hits2D, const Matrix2Nd< N > &hits_cov2D, const V4 &fast_fit, const VectorNd< N > &rad, const double bField, const bool error)
Fit a generic number of 2D points with a circle using Riemann-Chernov algorithm. Covariance matrix of...
Definition: RiemannFit.h:455
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t TrackingRecHitSoAConstView< TrackerTraits > double *__restrict__ float *__restrict__ double *__restrict__ uint32_t offset
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t TrackingRecHitSoAConstView< TrackerTraits > double *__restrict__ float *__restrict__ phits_ge
Eigen::Matrix< double, N, 1 > VectorNd
Definition: FitUtils.h:33
Eigen::Map< Matrix3xNd< N >, 0, Eigen::Stride< 3 *stride, stride > > Map3xNd
Definition: HelixFitOnGPU.h:24
uint32_t double double *__restrict__ float *__restrict__ double *__restrict__ pfast_fit_input
TupleMultiplicity< TrackerTraits > const *__restrict__ tupleMultiplicity
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
auto local_start
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t TrackingRecHitSoAConstView< TrackerTraits > double *__restrict__ float *__restrict__ double *__restrict__ pfast_fit
const dim3 blockIdx
Definition: cudaCompat.h:32
Eigen::Map< Matrix6xNf< N >, 0, Eigen::Stride< 6 *stride, stride > > Map6xNf
Definition: HelixFitOnGPU.h:29
int nt
Definition: AMPTWrapper.h:42
Eigen::Matrix< double, 2 *N, 2 *N > Matrix2Nd
Definition: FitUtils.h:21
assert(hitsInFit<=nHits)
#define N
Definition: blowfish.cc:9
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t TrackingRecHitSoAConstView< TrackerTraits > hh
typename TrackingRecHitSoA< TrackerTraits >::template TrackingRecHitSoALayout<>::ConstView TrackingRecHitSoAConstView
Eigen::Map< Vector4d, 0, Eigen::InnerStride< stride > > Map4d
Definition: HelixFitOnGPU.h:31
uint32_t double double *__restrict__ float *__restrict__ double *__restrict__ riemannFit::CircleFit * circle_fit
__host__ __device__ void fromCircleToPerigee(CircleFit &circle)
Transform circle parameter from (X0,Y0,R) to (phi,Tip,q/R) and consequently covariance matrix...
Definition: FitUtils.h:196
typename TrackSoA< TrackerTraits >::template TrackSoALayout<>::View TrackSoAView
__host__ __device__ void loadCovariance2D(M6xNf const &ge, M2Nd &hits_cov)
Definition: FitUtils.h:88
static constexpr __host__ __device__ void copyFromCircle(TrackSoAView &tracks, V3 const &cp, M3 const &ccov, V2 const &lp, M2 const &lcov, float b, int32_t i)
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t TrackingRecHitSoAConstView< TrackerTraits > double *__restrict__ phits
__host__ __device__ void fastFit(const M3xN &hits, V4 &result)
A very fast helix fit: it fits a circle by three points (first, middle and last point) and a line by ...
Definition: RiemannFit.h:375
constexpr uint32_t maxNumberOfConcurrentFits
Definition: HelixFitOnGPU.h:13
uint32_t double bField