CMS 3D CMS Logo

Hit.h
Go to the documentation of this file.
1 #ifndef RecoTracker_MkFitCore_interface_Hit_h
2 #define RecoTracker_MkFitCore_interface_Hit_h
3 
6 
7 #include <cmath>
8 #include <vector>
9 #include <string_view>
10 
11 namespace mkfit {
12 
13  template <typename T>
14  inline T sqr(T x) {
15  return x * x;
16  }
17  template <typename T>
18  inline T cube(T x) {
19  return x * x * x;
20  }
21 
22  inline float squashPhiGeneral(float phi) {
23  return phi - floor(0.5 * Const::InvPI * (phi + Const::PI)) * Const::TwoPI;
24  }
25 
26  inline float squashPhiMinimal(float phi) {
27  return phi >= Const::PI ? phi - Const::TwoPI : (phi < -Const::PI ? phi + Const::TwoPI : phi);
28  }
29 
30  inline float getRad2(float x, float y) { return x * x + y * y; }
31 
32  inline float getInvRad2(float x, float y) { return 1.0f / (x * x + y * y); }
33 
34  inline float getPhi(float x, float y) { return std::atan2(y, x); }
35 
36  inline float getTheta(float r, float z) { return std::atan2(r, z); }
37 
38  inline float getEta(float r, float z) { return -1.0f * std::log(std::tan(getTheta(r, z) / 2.0f)); }
39 
40  inline float getEta(float theta) { return -1.0f * std::log(std::tan(theta / 2.0f)); }
41 
42  inline float getEta(float x, float y, float z) {
43  const float theta = std::atan2(std::sqrt(x * x + y * y), z);
44  return -1.0f * std::log(std::tan(theta / 2.0f));
45  }
46 
47  inline float getHypot(float x, float y) { return std::sqrt(x * x + y * y); }
48 
49  inline float getRadErr2(float x, float y, float exx, float eyy, float exy) {
50  return (x * x * exx + y * y * eyy + 2.0f * x * y * exy) / getRad2(x, y);
51  }
52 
53  inline float getInvRadErr2(float x, float y, float exx, float eyy, float exy) {
54  return (x * x * exx + y * y * eyy + 2.0f * x * y * exy) / cube(getRad2(x, y));
55  }
56 
57  inline float getPhiErr2(float x, float y, float exx, float eyy, float exy) {
58  const float rad2 = getRad2(x, y);
59  return (y * y * exx + x * x * eyy - 2.0f * x * y * exy) / (rad2 * rad2);
60  }
61 
62  inline float getThetaErr2(
63  float x, float y, float z, float exx, float eyy, float ezz, float exy, float exz, float eyz) {
64  const float rad2 = getRad2(x, y);
65  const float rad = std::sqrt(rad2);
66  const float hypot2 = rad2 + z * z;
67  const float dthetadx = x * z / (rad * hypot2);
68  const float dthetady = y * z / (rad * hypot2);
69  const float dthetadz = -rad / hypot2;
70  return dthetadx * dthetadx * exx + dthetady * dthetady * eyy + dthetadz * dthetadz * ezz +
71  2.0f * dthetadx * dthetady * exy + 2.0f * dthetadx * dthetadz * exz + 2.0f * dthetady * dthetadz * eyz;
72  }
73 
74  inline float getEtaErr2(float x, float y, float z, float exx, float eyy, float ezz, float exy, float exz, float eyz) {
75  const float rad2 = getRad2(x, y);
76  const float detadx = -x / (rad2 * std::sqrt(1 + rad2 / (z * z)));
77  const float detady = -y / (rad2 * std::sqrt(1 + rad2 / (z * z)));
78  const float detadz = 1.0f / (z * std::sqrt(1 + rad2 / (z * z)));
79  return detadx * detadx * exx + detady * detady * eyy + detadz * detadz * ezz + 2.0f * detadx * detady * exy +
80  2.0f * detadx * detadz * exz + 2.0f * detady * detadz * eyz;
81  }
82 
83  inline float getPxPxErr2(float ipt, float phi, float vipt, float vphi) { // ipt = 1/pT, v = variance
84  const float iipt2 = 1.0f / (ipt * ipt); //iipt = 1/(1/pT) = pT
85  const float cosP = std::cos(phi);
86  const float sinP = std::sin(phi);
87  return iipt2 * (iipt2 * cosP * cosP * vipt + sinP * sinP * vphi);
88  }
89 
90  inline float getPyPyErr2(float ipt, float phi, float vipt, float vphi) { // ipt = 1/pT, v = variance
91  const float iipt2 = 1.0f / (ipt * ipt); //iipt = 1/(1/pT) = pT
92  const float cosP = std::cos(phi);
93  const float sinP = std::sin(phi);
94  return iipt2 * (iipt2 * sinP * sinP * vipt + cosP * cosP * vphi);
95  }
96 
97  inline float getPzPzErr2(float ipt, float theta, float vipt, float vtheta) { // ipt = 1/pT, v = variance
98  const float iipt2 = 1.0f / (ipt * ipt); //iipt = 1/(1/pT) = pT
99  const float cotT = 1.0f / std::tan(theta);
100  const float cscT = 1.0f / std::sin(theta);
101  return iipt2 * (iipt2 * cotT * cotT * vipt + cscT * cscT * cscT * cscT * vtheta);
102  }
103 
104  struct MCHitInfo {
106  MCHitInfo(int track, int layer, int ithlayerhit, int mcHitID)
107  : mcTrackID_(track), layer_(layer), ithLayerHit_(ithlayerhit), mcHitID_(mcHitID) {}
108 
110  int layer_;
112  int mcHitID_;
113 
114  int mcTrackID() const { return mcTrackID_; }
115  int layer() const { return layer_; }
116  int mcHitID() const { return mcHitID_; }
117  static void reset();
118  };
119  typedef std::vector<MCHitInfo> MCHitInfoVec;
120 
122  public:
124  MeasurementState(const SVector3& p, const SVector6& e) : pos_(p), err_(e) {}
126  for (int i = 0; i < 6; ++i)
127  err_[i] = e.Array()[i];
128  }
129  const SVector3& parameters() const { return pos_; }
132  for (int i = 0; i < 6; ++i)
133  result.Array()[i] = err_[i];
134  return result;
135  }
138  };
139 
140  class Hit {
141  public:
142  Hit() : mcHitID_(-1) {}
143 
144  Hit(const SVector3& position, const SMatrixSym33& error, int mcHitID = -1)
146 
147  const SVector3& position() const { return state_.parameters(); }
148  const SVector3& parameters() const { return state_.parameters(); }
149  const SMatrixSym33 error() const { return state_.errors(); }
150 
151  const float* posArray() const { return state_.pos_.Array(); }
152  const float* errArray() const { return state_.err_.Array(); }
153 
154  // Non-const versions needed for CopyOut of Matriplex.
156  SVector6& error_nc() { return state_.err_; }
157 
158  float r() const {
159  return sqrtf(state_.parameters().At(0) * state_.parameters().At(0) +
160  state_.parameters().At(1) * state_.parameters().At(1));
161  }
162  float x() const { return state_.parameters().At(0); }
163  float y() const { return state_.parameters().At(1); }
164  float z() const { return state_.parameters().At(2); }
165  float exx() const { return state_.errors().At(0, 0); }
166  float eyy() const { return state_.errors().At(1, 1); }
167  float ezz() const { return state_.errors().At(2, 2); }
168  float phi() const { return getPhi(state_.parameters().At(0), state_.parameters().At(1)); }
169  float eta() const {
170  return getEta(state_.parameters().At(0), state_.parameters().At(1), state_.parameters().At(2));
171  }
172  float ephi() const { return getPhiErr2(x(), y(), exx(), eyy(), state_.errors().At(0, 1)); }
173  float eeta() const {
174  return getEtaErr2(x(),
175  y(),
176  z(),
177  exx(),
178  eyy(),
179  ezz(),
180  state_.errors().At(0, 1),
181  state_.errors().At(0, 2),
182  state_.errors().At(1, 2));
183  }
184 
185  const MeasurementState& measurementState() const { return state_; }
186 
187  int mcHitID() const { return mcHitID_; }
188  int layer(const MCHitInfoVec& globalMCHitInfo) const { return globalMCHitInfo[mcHitID_].layer(); }
189  int mcTrackID(const MCHitInfoVec& globalMCHitInfo) const { return globalMCHitInfo[mcHitID_].mcTrackID(); }
190 
191  // Indices for "fake" hit addition
192  // Only if fake_hit_idx==-1, then count as missing hit for candidate score
193  static constexpr int kHitMissIdx = -1; // hit is missed
194  static constexpr int kHitStopIdx = -2; // track is stopped
195  static constexpr int kHitEdgeIdx = -3; // track not in sensitive region of detector
196  static constexpr int kHitMaxClusterIdx = -5; // hit cluster size > maxClusterSize
197  static constexpr int kHitInGapIdx = -7; // track passing through inactive module
198  static constexpr int kHitCCCFilterIdx = -9; // hit filtered via CCC (unused)
199 
200  static constexpr int kMinChargePerCM = 1620;
201  static constexpr int kChargePerCMBits = 8;
202  static constexpr int kDetIdInLayerBits = 14;
203  static constexpr int kClusterSizeBits = 5;
204  static constexpr int kMaxClusterSize = (1 << kClusterSizeBits) - 1;
205 
206  struct PackedData {
208  unsigned int charge_pcm : kChargePerCMBits; // MIMI see set/get funcs; applicable for phase-0/1
209  unsigned int span_rows : kClusterSizeBits;
210  unsigned int span_cols : kClusterSizeBits;
211 
213 
214  void set_charge_pcm(int cpcm) {
215  if (cpcm < kMinChargePerCM)
216  charge_pcm = 0;
217  else
218  charge_pcm = std::min((1 << kChargePerCMBits) - 1, ((cpcm - kMinChargePerCM) >> 3) + 1);
219  }
220  unsigned int get_charge_pcm() const {
221  if (charge_pcm == 0)
222  return 0;
223  else
224  return ((charge_pcm - 1) << 3) + kMinChargePerCM;
225  }
226  };
227 
228  unsigned int detIDinLayer() const { return pdata_.detid_in_layer; }
229  unsigned int chargePerCM() const { return pdata_.get_charge_pcm(); }
230  unsigned int spanRows() const { return pdata_.span_rows + 1; }
231  unsigned int spanCols() const { return pdata_.span_cols + 1; }
232 
233  static unsigned int minChargePerCM() { return kMinChargePerCM; }
234  static unsigned int maxChargePerCM() { return kMinChargePerCM + (((1 << kChargePerCMBits) - 2) << 3); }
235  static unsigned int maxSpan() { return kMaxClusterSize; }
236 
237  void setupAsPixel(unsigned int id, int rows, int cols) {
239  pdata_.charge_pcm = (1 << kChargePerCMBits) - 1;
242  }
243 
244  void setupAsStrip(unsigned int id, int cpcm, int rows) {
246  pdata_.set_charge_pcm(cpcm);
248  }
249 
250  private:
252  int mcHitID_;
254  };
255 
256  typedef std::vector<Hit> HitVec;
257 
258  struct HitOnTrack {
259  int index : 24;
260  int layer : 8;
261 
262  HitOnTrack() : index(-1), layer(-1) {}
263  HitOnTrack(int i, int l) : index(i), layer(l) {}
264 
265  bool operator<(const HitOnTrack o) const {
266  if (layer != o.layer)
267  return layer < o.layer;
268  return index < o.index;
269  }
270  };
271 
272  typedef std::vector<HitOnTrack> HoTVec;
273 
274  void print(std::string_view label, const MeasurementState& s);
275 
276  struct DeadRegion {
277  float phi1, phi2, q1, q2;
278  DeadRegion(float a1, float a2, float b1, float b2) : phi1(a1), phi2(a2), q1(b1), q2(b2) {}
279  };
280  typedef std::vector<DeadRegion> DeadVec;
281 
282  struct BeamSpot {
283  float x = 0, y = 0, z = 0;
284  float sigmaZ = 5;
285  float beamWidthX = 5e-4, beamWidthY = 5e-4;
286  float dxdz = 0, dydz = 0;
287 
288  BeamSpot() = default;
289  BeamSpot(float ix, float iy, float iz, float is, float ibx, float iby, float idxdz, float idydz)
290  : x(ix), y(iy), z(iz), sigmaZ(is), beamWidthX(ibx), beamWidthY(iby), dxdz(idxdz), dydz(idydz) {}
291  };
292 } // end namespace mkfit
293 #endif
float x() const
Definition: Hit.h:162
weight_default_t b1[25]
Definition: b1.h:9
static constexpr int kDetIdInLayerBits
Definition: Hit.h:202
float squashPhiMinimal(float phi)
Definition: Hit.h:26
bool operator<(const HitOnTrack o) const
Definition: Hit.h:265
void setupAsStrip(unsigned int id, int cpcm, int rows)
Definition: Hit.h:244
float getThetaErr2(float x, float y, float z, float exx, float eyy, float ezz, float exy, float exz, float eyz)
Definition: Hit.h:62
SVector6 & error_nc()
Definition: Hit.h:156
float beamWidthX
Definition: Hit.h:285
float phi2
Definition: Hit.h:277
unsigned int detid_in_layer
Definition: Hit.h:207
const SMatrixSym33 error() const
Definition: Hit.h:149
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
float phi() const
Definition: Hit.h:168
int mcHitID() const
Definition: Hit.h:116
unsigned int chargePerCM() const
Definition: Hit.h:229
ROOT::Math::SVector< float, 3 > SVector3
Definition: MatrixSTypes.h:14
T sqr(T x)
Definition: Hit.h:14
int mcHitID_
Definition: Hit.h:252
float getTheta(float r, float z)
Definition: Hit.h:36
int mcTrackID() const
Definition: Hit.h:114
unsigned int span_cols
Definition: Hit.h:210
const SVector3 & position() const
Definition: Hit.h:147
float getEta(float r, float z)
Definition: Hit.h:38
constexpr float TwoPI
Definition: Config.h:8
int layer(const MCHitInfoVec &globalMCHitInfo) const
Definition: Hit.h:188
std::vector< MCHitInfo > MCHitInfoVec
Definition: Hit.h:119
float exx() const
Definition: Hit.h:165
float getInvRad2(float x, float y)
Definition: Hit.h:32
const float * posArray() const
Definition: Hit.h:151
static unsigned int maxChargePerCM()
Definition: Hit.h:234
float getInvRadErr2(float x, float y, float exx, float eyy, float exy)
Definition: Hit.h:53
static constexpr int kHitStopIdx
Definition: Hit.h:194
unsigned int get_charge_pcm() const
Definition: Hit.h:220
float getRadErr2(float x, float y, float exx, float eyy, float exy)
Definition: Hit.h:49
static constexpr int kHitInGapIdx
Definition: Hit.h:197
float squashPhiGeneral(float phi)
Definition: Hit.h:22
void setupAsPixel(unsigned int id, int rows, int cols)
Definition: Hit.h:237
float ezz() const
Definition: Hit.h:167
float getPxPxErr2(float ipt, float phi, float vipt, float vphi)
Definition: Hit.h:83
int mcTrackID(const MCHitInfoVec &globalMCHitInfo) const
Definition: Hit.h:189
char const * label
static constexpr int kChargePerCMBits
Definition: Hit.h:201
float q1
Definition: Hit.h:277
float x
Definition: Hit.h:283
static void reset()
Definition: Hit.cc:6
unsigned int detIDinLayer() const
Definition: Hit.h:228
static unsigned int maxSpan()
Definition: Hit.h:235
void set_charge_pcm(int cpcm)
Definition: Hit.h:214
BeamSpot()=default
constexpr float PI
Definition: Config.h:7
float y
Definition: Hit.h:283
float eta() const
Definition: Hit.h:169
T sqrt(T t)
Definition: SSEVec.h:19
int mcTrackID_
Definition: Hit.h:109
const float * errArray() const
Definition: Hit.h:152
float getEtaErr2(float x, float y, float z, float exx, float eyy, float ezz, float exy, float exz, float eyz)
Definition: Hit.h:74
const MeasurementState & measurementState() const
Definition: Hit.h:185
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
float q2
Definition: Hit.h:277
float eyy() const
Definition: Hit.h:166
float getRad2(float x, float y)
Definition: Hit.h:30
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
float dydz
Definition: Hit.h:286
PackedData pdata_
Definition: Hit.h:253
double f[11][100]
float getHypot(float x, float y)
Definition: Hit.h:47
weight_default_t b2[10]
Definition: b2.h:9
float ephi() const
Definition: Hit.h:172
int layer() const
Definition: Hit.h:115
Hit(const SVector3 &position, const SMatrixSym33 &error, int mcHitID=-1)
Definition: Hit.h:144
static constexpr int kMinChargePerCM
Definition: Hit.h:200
std::vector< Hit > HitVec
const SVector3 & parameters() const
Definition: Hit.h:148
unsigned int span_rows
Definition: Hit.h:209
float y() const
Definition: Hit.h:163
MeasurementState(const SVector3 &p, const SMatrixSym33 &e)
Definition: Hit.h:125
float dxdz
Definition: Hit.h:286
unsigned int spanCols() const
Definition: Hit.h:231
static constexpr int kHitCCCFilterIdx
Definition: Hit.h:198
BeamSpot(float ix, float iy, float iz, float is, float ibx, float iby, float idxdz, float idydz)
Definition: Hit.h:289
std::vector< DeadRegion > DeadVec
Definition: Hit.h:280
void print(std::string_view label, const MeasurementState &s)
Definition: Hit.cc:8
SMatrixSym33 errors() const
Definition: Hit.h:130
float getPhi(float x, float y)
Definition: Hit.h:34
T cube(T x)
Definition: Hit.h:18
unsigned int spanRows() const
Definition: Hit.h:230
std::vector< HitOnTrack > HoTVec
Definition: Hit.h:272
MCHitInfo(int track, int layer, int ithlayerhit, int mcHitID)
Definition: Hit.h:106
static constexpr int kHitMaxClusterIdx
Definition: Hit.h:196
ROOT::Math::SVector< float, 6 > SVector6
Definition: MatrixSTypes.h:10
float z() const
Definition: Hit.h:164
static constexpr int kMaxClusterSize
Definition: Hit.h:204
int ithLayerHit_
Definition: Hit.h:111
float beamWidthY
Definition: Hit.h:285
Hit()
Definition: Hit.h:142
HitOnTrack(int i, int l)
Definition: Hit.h:263
float r() const
Definition: Hit.h:158
int mcHitID_
Definition: Hit.h:112
float x
DeadRegion(float a1, float a2, float b1, float b2)
Definition: Hit.h:278
float eeta() const
Definition: Hit.h:173
unsigned int charge_pcm
Definition: Hit.h:208
static constexpr int kHitMissIdx
Definition: Hit.h:193
float sigmaZ
Definition: Hit.h:284
rows
Definition: mysort.py:12
static constexpr int kClusterSizeBits
Definition: Hit.h:203
ROOT::Math::SMatrix< float, 3, 3, ROOT::Math::MatRepSym< float, 3 > > SMatrixSym33
Definition: MatrixSTypes.h:13
long double T
MeasurementState state_
Definition: Hit.h:251
SVector3 & parameters_nc()
Definition: Hit.h:155
constexpr float InvPI
Definition: Config.h:12
float getPyPyErr2(float ipt, float phi, float vipt, float vphi)
Definition: Hit.h:90
Geom::Theta< T > theta() const
static constexpr int kHitEdgeIdx
Definition: Hit.h:195
const SVector3 & parameters() const
Definition: Hit.h:129
static unsigned int minChargePerCM()
Definition: Hit.h:233
float getPzPzErr2(float ipt, float theta, float vipt, float vtheta)
Definition: Hit.h:97
int mcHitID() const
Definition: Hit.h:187
float getPhiErr2(float x, float y, float exx, float eyy, float exy)
Definition: Hit.h:57
float z
Definition: Hit.h:283
MeasurementState(const SVector3 &p, const SVector6 &e)
Definition: Hit.h:124
float phi1
Definition: Hit.h:277
int layer_
Definition: Hit.h:110