CMS 3D CMS Logo

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