CMS 3D CMS Logo

BrokenLineFitOnGPU.h
Go to the documentation of this file.
1 //
2 // Author: Felice Pantaleo, CERN
3 //
4 
5 // #define BROKENLINE_DEBUG
6 
7 #include <cstdint>
8 
9 #include <cuda_runtime.h>
10 
16 
17 #include "HelixFitOnGPU.h"
18 
22 
23 // #define BL_DUMP_HITS
24 
25 template <int N>
26 __global__ void kernel_BLFastFit(Tuples const *__restrict__ foundNtuplets,
28  HitsOnGPU const *__restrict__ hhp,
29  double *__restrict__ phits,
30  float *__restrict__ phits_ge,
31  double *__restrict__ pfast_fit,
32  uint32_t nHits,
33  uint32_t offset) {
34  constexpr uint32_t hitsInFit = N;
35 
36  assert(hitsInFit <= nHits);
37 
38  assert(hhp);
42 
43  // look in bin for this hit multiplicity
45 
46 #ifdef BROKENLINE_DEBUG
47  if (0 == local_start) {
48  printf("%d total Ntuple\n", foundNtuplets->nOnes());
49  printf("%d Ntuple of size %d for %d hits to fit\n", tupleMultiplicity->size(nHits), nHits, hitsInFit);
50  }
51 #endif
52 
53  for (int local_idx = local_start, nt = riemannFit::maxNumberOfConcurrentFits; local_idx < nt;
54  local_idx += gridDim.x * blockDim.x) {
55  auto tuple_idx = local_idx + offset;
56  if (tuple_idx >= tupleMultiplicity->size(nHits))
57  break;
58 
59  // get it from the ntuple container (one to one to helix)
60  auto tkid = *(tupleMultiplicity->begin(nHits) + tuple_idx);
61  assert(tkid < foundNtuplets->nOnes());
62 
63  assert(foundNtuplets->size(tkid) == nHits);
64 
65  riemannFit::Map3xNd<N> hits(phits + local_idx);
66  riemannFit::Map4d fast_fit(pfast_fit + local_idx);
67  riemannFit::Map6xNf<N> hits_ge(phits_ge + local_idx);
68 
69 #ifdef BL_DUMP_HITS
70  __shared__ int done;
71  done = 0;
72  __syncthreads();
73  bool dump = (foundNtuplets->size(tkid) == 5 && 0 == atomicAdd(&done, 1));
74 #endif
75 
76  // Prepare data structure
77  auto const *hitId = foundNtuplets->begin(tkid);
78 
79  // #define YERR_FROM_DC
80 #ifdef YERR_FROM_DC
81  // try to compute more precise error in y
82  auto dx = hhp->xGlobal(hitId[hitsInFit - 1]) - hhp->xGlobal(hitId[0]);
83  auto dy = hhp->yGlobal(hitId[hitsInFit - 1]) - hhp->yGlobal(hitId[0]);
84  auto dz = hhp->zGlobal(hitId[hitsInFit - 1]) - hhp->zGlobal(hitId[0]);
85  float ux, uy, uz;
86 #endif
87  for (unsigned int i = 0; i < hitsInFit; ++i) {
88  auto hit = hitId[i];
89  float ge[6];
90 #ifdef YERR_FROM_DC
91  auto const &dp = hhp->cpeParams().detParams(hhp->detectorIndex(hit));
92  auto status = hhp->status(hit);
93  int qbin = 4 - status.qBin;
94  assert(qbin >= 0 && qbin < 5);
95  bool nok = (status.isBigY | status.isOneY);
96  // compute cotanbeta and use it to recompute error
97  dp.frame.rotation().multiply(dx, dy, dz, ux, uy, uz);
98  auto cb = std::abs(uy / uz);
99  int bin = int(cb * (285.f / 150.f) * 8.f) - 4;
100  bin = std::max(0, std::min(15, bin));
101  float yerr = dp.sigmay[bin] * 1.e-4f;
102  yerr *= dp.yfact[qbin]; // inflate
103  yerr *= yerr;
104  yerr += dp.apeYY;
105  yerr = nok ? hhp->yerrLocal(hit) : yerr;
106  dp.frame.toGlobal(hhp->xerrLocal(hit), 0, yerr, ge);
107 #else
108  hhp->cpeParams()
109  .detParams(hhp->detectorIndex(hit))
110  .frame.toGlobal(hhp->xerrLocal(hit), 0, hhp->yerrLocal(hit), ge);
111 #endif
112 
113 #ifdef BL_DUMP_HITS
114  if (dump) {
115  printf("Hit global: %d: %d hits.col(%d) << %f,%f,%f\n",
116  tkid,
117  hhp->detectorIndex(hit),
118  i,
119  hhp->xGlobal(hit),
120  hhp->yGlobal(hit),
121  hhp->zGlobal(hit));
122  printf("Error: %d: %d hits_ge.col(%d) << %e,%e,%e,%e,%e,%e\n",
123  tkid,
124  hhp->detetectorIndex(hit),
125  i,
126  ge[0],
127  ge[1],
128  ge[2],
129  ge[3],
130  ge[4],
131  ge[5]);
132  }
133 #endif
134  hits.col(i) << hhp->xGlobal(hit), hhp->yGlobal(hit), hhp->zGlobal(hit);
135  hits_ge.col(i) << ge[0], ge[1], ge[2], ge[3], ge[4], ge[5];
136  }
137  brokenline::fastFit(hits, fast_fit);
138 
139  // no NaN here....
140  assert(fast_fit(0) == fast_fit(0));
141  assert(fast_fit(1) == fast_fit(1));
142  assert(fast_fit(2) == fast_fit(2));
143  assert(fast_fit(3) == fast_fit(3));
144  }
145 }
146 
147 template <int N>
148 __global__ void kernel_BLFit(caConstants::TupleMultiplicity const *__restrict__ tupleMultiplicity,
149  double bField,
151  double *__restrict__ phits,
152  float *__restrict__ phits_ge,
153  double *__restrict__ pfast_fit,
154  uint32_t nHits,
155  uint32_t offset) {
156  assert(N <= nHits);
157 
158  assert(results);
159  assert(pfast_fit);
160 
161  // same as above...
162 
163  // look in bin for this hit multiplicity
164  auto local_start = blockIdx.x * blockDim.x + threadIdx.x;
165  for (int local_idx = local_start, nt = riemannFit::maxNumberOfConcurrentFits; local_idx < nt;
166  local_idx += gridDim.x * blockDim.x) {
167  auto tuple_idx = local_idx + offset;
168  if (tuple_idx >= tupleMultiplicity->size(nHits))
169  break;
170 
171  // get it for the ntuple container (one to one to helix)
172  auto tkid = *(tupleMultiplicity->begin(nHits) + tuple_idx);
173 
174  riemannFit::Map3xNd<N> hits(phits + local_idx);
175  riemannFit::Map4d fast_fit(pfast_fit + local_idx);
176  riemannFit::Map6xNf<N> hits_ge(phits_ge + local_idx);
177 
179 
182 
184  brokenline::lineFit(hits_ge, fast_fit, bField, data, line);
185  brokenline::circleFit(hits, hits_ge, fast_fit, bField, data, circle);
186 
187  results->stateAtBS.copyFromCircle(circle.par, circle.cov, line.par, line.cov, 1.f / float(bField), tkid);
188  results->pt(tkid) = float(bField) / float(std::abs(circle.par(2)));
189  results->eta(tkid) = asinhf(line.par(0));
190  results->chi2(tkid) = (circle.chi2 + line.chi2) / (2 * N - 5);
191 
192 #ifdef BROKENLINE_DEBUG
193  if (!(circle.chi2 >= 0) || !(line.chi2 >= 0))
194  printf("kernelBLFit failed! %f/%f\n", circle.chi2, line.chi2);
195  printf("kernelBLFit size %d for %d hits circle.par(0,1,2): %d %f,%f,%f\n",
196  N,
197  nHits,
198  tkid,
199  circle.par(0),
200  circle.par(1),
201  circle.par(2));
202  printf("kernelBLHits line.par(0,1): %d %f,%f\n", tkid, line.par(0), line.par(1));
203  printf("kernelBLHits chi2 cov %f/%f %e,%e,%e,%e,%e\n",
204  circle.chi2,
205  line.chi2,
206  circle.cov(0, 0),
207  circle.cov(1, 1),
208  circle.cov(2, 2),
209  line.cov(0, 0),
210  line.cov(1, 1));
211 #endif
212  }
213 }
submitPVValidationJobs.dump
dump
Definition: submitPVValidationJobs.py:55
pixelCPEforGPU.h
riemannFit::CircleFit::par
Vector3d par
parameter: (X0,Y0,R)
Definition: FitResult.h:27
mps_fire.i
i
Definition: mps_fire.py:428
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
phits_ge
const caConstants::TupleMultiplicity *__restrict__ const HitsOnGPU *__restrict__ double *__restrict__ float *__restrict__ phits_ge
Definition: BrokenLineFitOnGPU.h:27
hfClusterShapes_cfi.hits
hits
Definition: hfClusterShapes_cfi.py:5
brokenline::prepareBrokenLineData
__host__ __device__ void prepareBrokenLineData(const M3xN &hits, const V4 &fast_fit, const double bField, PreparedBrokenLineData< n > &results)
Computes the data needed for the Broken Line fit procedure that are mainly common for the circle and ...
Definition: BrokenLine.h:150
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
nt
int nt
Definition: AMPTWrapper.h:42
mps_update.status
status
Definition: mps_update.py:68
min
T min(T a, T b)
Definition: MathUtil.h:58
TrackingRecHit2DHeterogeneous.h
phits
const caConstants::TupleMultiplicity *__restrict__ const HitsOnGPU *__restrict__ double *__restrict__ phits
Definition: BrokenLineFitOnGPU.h:27
cms::cudacompat::__syncthreads
void __syncthreads()
Definition: cudaCompat.h:108
bookConverter.results
results
Definition: bookConverter.py:144
TrackingRecHit2DSOAView
Definition: TrackingRecHit2DSOAView.h:16
brokenline::circleFit
__host__ __device__ void circleFit(const M3xN &hits, const M6xN &hits_ge, const V4 &fast_fit, const double bField, PreparedBrokenLineData< n > &data, karimaki_circle_fit &circle_results)
Performs the Broken Line fit in the curved track case (that is, the fit parameters are the intercepti...
Definition: BrokenLine.h:296
local_start
auto local_start
Definition: BrokenLineFitOnGPU.h:44
cms::cuda::OneToManyAssoc
Definition: OneToManyAssoc.h:143
__global__
#define __global__
Definition: cudaCompat.h:19
riemannFit::CircleFit
Definition: FitResult.h:26
riemannFit::maxNumberOfConcurrentFits
constexpr uint32_t maxNumberOfConcurrentFits
Definition: HelixFitOnGPU.h:12
brokenline::lineFit
__host__ __device__ void lineFit(const M6xN &hits_ge, const V4 &fast_fit, const double bField, const PreparedBrokenLineData< n > &data, riemannFit::LineFit &line_results)
Performs the Broken Line fit in the straight track case (that is, the fit parameters are only the int...
Definition: BrokenLine.h:445
nHits
const caConstants::TupleMultiplicity *__restrict__ const HitsOnGPU *__restrict__ double *__restrict__ float *__restrict__ double *__restrict__ uint32_t nHits
Definition: BrokenLineFitOnGPU.h:27
Calorimetry_cff.dp
dp
Definition: Calorimetry_cff.py:158
fileCollector.done
done
Definition: fileCollector.py:123
tupleMultiplicity
const caConstants::TupleMultiplicity *__restrict__ tupleMultiplicity
Definition: BrokenLineFitOnGPU.h:27
BrokenLine.h
N
#define N
Definition: blowfish.cc:9
cms::cudacompat::atomicAdd
T1 atomicAdd(T1 *a, T2 b)
Definition: cudaCompat.h:61
cms::cudacompat::gridDim
const dim3 gridDim
Definition: cudaCompat.h:33
TrackSoAHeterogeneousT
Definition: TrackSoAHeterogeneousT.h:23
riemannFit::CircleFit::chi2
float chi2
Definition: FitResult.h:35
HelixFitOnGPU.h
pfast_fit
const caConstants::TupleMultiplicity *__restrict__ const HitsOnGPU *__restrict__ double *__restrict__ float *__restrict__ double *__restrict__ pfast_fit
Definition: BrokenLineFitOnGPU.h:27
cms::cudacompat::blockDim
const dim3 blockDim
Definition: cudaCompat.h:30
riemannFit::Map6xNf
Eigen::Map< Matrix6xNf< N >, 0, Eigen::Stride< 6 *stride, stride > > Map6xNf
Definition: HelixFitOnGPU.h:28
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
riemannFit::CircleFit::cov
Matrix3d cov
Definition: FitResult.h:28
createfilelist.int
int
Definition: createfilelist.py:10
riemannFit::LineFit
Definition: FitResult.h:38
foundNtuplets
const uint32_t *__restrict__ HitContainer * foundNtuplets
Definition: CAHitNtupletGeneratorKernelsImpl.h:139
cudaCheck.h
PVValHelper::dy
Definition: PVValidationHelpers.h:50
cms::cudacompat::threadIdx
const dim3 threadIdx
Definition: cudaCompat.h:29
cms::cuda::OneToManyAssoc::nOnes
constexpr auto nOnes() const
Definition: OneToManyAssoc.h:167
brokenline::PreparedBrokenLineData
data needed for the Broken Line fit procedure.
Definition: BrokenLine.h:24
newFWLiteAna.bin
bin
Definition: newFWLiteAna.py:161
Calorimetry_cff.bField
bField
Definition: Calorimetry_cff.py:284
PVValHelper::dz
Definition: PVValidationHelpers.h:51
riemannFit::Map3xNd
Eigen::Map< Matrix3xNd< N >, 0, Eigen::Stride< 3 *stride, stride > > Map3xNd
Definition: HelixFitOnGPU.h:23
amptDefault_cfi.frame
frame
Definition: amptDefault_cfi.py:12
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
pixelTrack::HitContainer
TrackSoA::HitContainer HitContainer
Definition: TrackSoAHeterogeneousT.h:78
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
hhp
const caConstants::TupleMultiplicity *__restrict__ const HitsOnGPU *__restrict__ hhp
Definition: BrokenLineFitOnGPU.h:27
pixelTrack::TrackSoA
TrackSoAHeterogeneousT< maxNumber()> TrackSoA
Definition: TrackSoAHeterogeneousT.h:76
brokenline::fastFit
__host__ __device__ void fastFit(const M3xN &hits, V4 &result)
A very fast helix fit.
Definition: BrokenLine.h:249
offset
const caConstants::TupleMultiplicity *__restrict__ const HitsOnGPU *__restrict__ double *__restrict__ float *__restrict__ double *__restrict__ uint32_t uint32_t offset
Definition: BrokenLineFitOnGPU.h:33
cuda_assert.h
mps_splice.line
line
Definition: mps_splice.py:76
PVValHelper::dx
Definition: PVValidationHelpers.h:49
hit
Definition: SiStripHitEffFromCalibTree.cc:88
riemannFit::Map4d
Eigen::Map< Vector4d, 0, Eigen::InnerStride< stride > > Map4d
Definition: HelixFitOnGPU.h:30
cms::cudacompat::blockIdx
const dim3 blockIdx
Definition: cudaCompat.h:32
assert
assert(hitsInFit<=nHits)