CMS 3D CMS Logo

TrackUtilities.h
Go to the documentation of this file.
1 #ifndef DataFormats_TrackSoA_interface_alpaka_TrackUtilities_h
2 #define DataFormats_TrackSoA_interface_alpaka_TrackUtilities_h
3 
4 #include <algorithm>
5 #include <cmath>
6 #include <cstdint>
7 
8 #include <alpaka/alpaka.hpp>
9 
12 
13 // Methods that operate on View and ConstView of the TrackSoA, and cannot be class methods.
14 template <typename TrackerTraits>
15 struct TracksUtilities {
17  using TrackSoAConstView = typename reco::TrackSoA<TrackerTraits>::template Layout<>::ConstView;
19 
20  // state at the beam spot: { phi, tip, 1/pt, cotan(theta), zip }
21 
22  template <typename V3, typename M3, typename V2, typename M2>
23  ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static constexpr void copyFromCircle(
24  TrackSoAView &tracks, V3 const &cp, M3 const &ccov, V2 const &lp, M2 const &lcov, float b, int32_t i) {
25  tracks[i].state() << cp.template cast<float>(), lp.template cast<float>();
26 
27  tracks[i].state()(2) = tracks[i].state()(2) * b;
28  auto cov = tracks[i].covariance();
29  cov(0) = ccov(0, 0);
30  cov(1) = ccov(0, 1);
31  cov(2) = b * float(ccov(0, 2));
32  cov(4) = cov(3) = 0;
33  cov(5) = ccov(1, 1);
34  cov(6) = b * float(ccov(1, 2));
35  cov(8) = cov(7) = 0;
36  cov(9) = b * b * float(ccov(2, 2));
37  cov(11) = cov(10) = 0;
38  cov(12) = lcov(0, 0);
39  cov(13) = lcov(0, 1);
40  cov(14) = lcov(1, 1);
41  }
42 
43  template <typename V5, typename M5>
44  ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static constexpr void copyFromDense(TrackSoAView &tracks,
45  V5 const &v,
46  M5 const &cov,
47  int32_t i) {
48  tracks[i].state() = v.template cast<float>();
49  for (int j = 0, ind = 0; j < 5; ++j)
50  for (auto k = j; k < 5; ++k)
51  tracks[i].covariance()(ind++) = cov(j, k);
52  }
53 
54  template <typename V5, typename M5>
55  ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static constexpr void copyToDense(const TrackSoAConstView &tracks,
56  V5 &v,
57  M5 &cov,
58  int32_t i) {
59  v = tracks[i].state().template cast<typename V5::Scalar>();
60  for (int j = 0, ind = 0; j < 5; ++j) {
61  cov(j, j) = tracks[i].covariance()(ind++);
62  for (auto k = j + 1; k < 5; ++k)
63  cov(k, j) = cov(j, k) = tracks[i].covariance()(ind++);
64  }
65  }
66 
67  ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static constexpr int computeNumberOfLayers(const TrackSoAConstView &tracks,
68  int32_t i) {
69  auto pdet = tracks.detIndices().begin(i);
70  int nl = 1;
71  auto ol = pixelTopology::getLayer<TrackerTraits>(*pdet);
72  for (; pdet < tracks.detIndices().end(i); ++pdet) {
73  auto il = pixelTopology::getLayer<TrackerTraits>(*pdet);
74  if (il != ol)
75  ++nl;
76  ol = il;
77  }
78  return nl;
79  }
80 
81  ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static constexpr int nHits(const TrackSoAConstView &tracks, int i) {
82  return tracks.detIndices().size(i);
83  }
84 };
85 
86 namespace pixelTrack {
87 
88  template <typename TrackerTraits, typename Enable = void>
89  struct QualityCutsT {};
90 
91  template <typename TrackerTraits>
92  struct QualityCutsT<TrackerTraits, pixelTopology::isPhase1Topology<TrackerTraits>> {
94  using TrackSoAConstView = typename reco::TrackSoA<TrackerTraits>::template Layout<>::ConstView;
95  float chi2Coeff[4];
96  float chi2MaxPt; // GeV
97  float chi2Scale;
98 
99  struct Region {
100  float maxTip; // cm
101  float minPt; // GeV
102  float maxZip; // cm
103  };
104 
105  Region triplet;
106  Region quadruplet;
107 
108  ALPAKA_FN_ACC ALPAKA_FN_INLINE bool isHP(const TrackSoAConstView &tracks, int nHits, int it) const {
109  // impose "region cuts" based on the fit results (phi, Tip, pt, cotan(theta)), Zip)
110  // default cuts:
111  // - for triplets: |Tip| < 0.3 cm, pT > 0.5 GeV, |Zip| < 12.0 cm
112  // - for quadruplets: |Tip| < 0.5 cm, pT > 0.3 GeV, |Zip| < 12.0 cm
113  // (see CAHitNtupletGeneratorGPU.cc)
114  auto const &region = (nHits > 3) ? quadruplet : triplet;
115  return (std::abs(reco::tip(tracks, it)) < region.maxTip) and (tracks.pt(it) > region.minPt) and
116  (std::abs(reco::zip(tracks, it)) < region.maxZip);
117  }
118 
119  ALPAKA_FN_ACC ALPAKA_FN_INLINE bool strictCut(const TrackSoAConstView &tracks, int it) const {
120  auto roughLog = [](float x) {
121  // max diff [0.5,12] at 1.25 0.16143
122  // average diff 0.0662998
123  union IF {
124  uint32_t i;
125  float f;
126  };
127  IF z;
128  z.f = x;
129  uint32_t lsb = 1 < 21;
130  z.i += lsb;
131  z.i >>= 21;
132  auto f = z.i & 3;
133  int ex = int(z.i >> 2) - 127;
134 
135  // log2(1+0.25*f)
136  // averaged over bins
137  const float frac[4] = {0.160497f, 0.452172f, 0.694562f, 0.901964f};
138  return float(ex) + frac[f];
139  };
140 
141  float pt = std::min<float>(tracks.pt(it), chi2MaxPt);
142  float chi2Cut = chi2Scale * (chi2Coeff[0] + roughLog(pt) * chi2Coeff[1]);
143  if (tracks.chi2(it) >= chi2Cut) {
144 #ifdef NTUPLE_FIT_DEBUG
145  printf("Bad chi2 %d pt %f eta %f chi2 %f\n", it, tracks.pt(it), tracks.eta(it), tracks.chi2(it));
146 #endif
147  return true;
148  }
149  return false;
150  }
151  };
152 
153  template <typename TrackerTraits>
154  struct QualityCutsT<TrackerTraits, pixelTopology::isPhase2Topology<TrackerTraits>> {
156  using TrackSoAConstView = typename reco::TrackSoA<TrackerTraits>::template Layout<>::ConstView;
157 
158  float maxChi2;
159  float minPt;
160  float maxTip;
161  float maxZip;
162 
163  ALPAKA_FN_ACC ALPAKA_FN_INLINE bool isHP(const TrackSoAConstView &tracks, int nHits, int it) const {
164  return (std::abs(reco::tip(tracks, it)) < maxTip) and (tracks.pt(it) > minPt) and
165  (std::abs(reco::zip(tracks, it)) < maxZip);
166  }
167  ALPAKA_FN_ACC ALPAKA_FN_INLINE bool strictCut(const TrackSoAConstView &tracks, int it) const {
168  return tracks.chi2(it) >= maxChi2;
169  }
170  };
171 
172 } // namespace pixelTrack
173 
174 // TODO: Should those be placed in the ALPAKA_ACCELERATOR_NAMESPACE
177 
178 #endif // DataFormats_TrackSoA_interface_alpaka_TrackUtilities_h
ALPAKA_FN_ACC ALPAKA_FN_INLINE bool isHP(const TrackSoAConstView &tracks, int nHits, int it) const
typename TrackSoA< TrackerTraits >::template TrackSoALayout<>::ConstView TrackSoAConstView
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr float zip(ConstView const &tracks, int32_t i)
Definition: TracksSoA.h:90
ALPAKA_FN_ACC ALPAKA_FN_INLINE bool strictCut(const TrackSoAConstView &tracks, int it) const
typename std::enable_if< std::is_base_of< Phase2, T >::value >::type isPhase2Topology
typename TrackSoA< TrackerTraits >::template TrackSoALayout<>::View TrackSoAView
minPt
Definition: PV_cfg.py:223
ALPAKA_FN_HOST_ACC static ALPAKA_FN_INLINE constexpr void copyToDense(const TrackSoAConstView &tracks, V5 &v, M5 &cov, int32_t i)
ALPAKA_FN_HOST_ACC static ALPAKA_FN_INLINE constexpr void copyFromDense(TrackSoAView &tracks, V5 const &v, M5 const &cov, int32_t i)
typename TrackSoA< TrackerTraits >::template TrackSoALayout<>::View TrackSoAView
typename TrackSoA< TrackerTraits >::template TrackSoALayout<>::ConstView TrackSoAConstView
ALPAKA_FN_HOST_ACC static ALPAKA_FN_INLINE constexpr int computeNumberOfLayers(const TrackSoAConstView &tracks, int32_t i)
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr float tip(ConstView const &tracks, int32_t i)
Definition: TracksSoA.h:85
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
typename TrackSoA< TrackerTraits >::template TrackSoALayout<>::View TrackSoAView
typename std::enable_if< std::is_base_of< Phase1, T >::value >::type isPhase1Topology
ALPAKA_FN_HOST_ACC static ALPAKA_FN_INLINE constexpr void copyFromCircle(TrackSoAView &tracks, V3 const &cp, M3 const &ccov, V2 const &lp, M2 const &lcov, float b, int32_t i)
uint32_t hindex_type
Definition: TracksSoA.h:25
typename TrackSoA< TrackerTraits >::hindex_type hindex_type
double b
Definition: hdecay.h:120
ALPAKA_FN_ACC ALPAKA_FN_INLINE bool isHP(const TrackSoAConstView &tracks, int nHits, int it) const
ALPAKA_FN_ACC ALPAKA_FN_INLINE bool strictCut(const TrackSoAConstView &tracks, int it) const
ALPAKA_FN_HOST_ACC static ALPAKA_FN_INLINE constexpr int nHits(const TrackSoAConstView &tracks, int i)
float x
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
typename TrackSoA< TrackerTraits >::template TrackSoALayout<>::ConstView TrackSoAConstView