CMS 3D CMS Logo

FTLCluster.h
Go to the documentation of this file.
1 #ifndef DataFormats_FTLRecHit_FTLCluster_h
2 #define DataFormats_FTLRecHit_FTLCluster_h
3 
11 #include <cmath>
12 #include <vector>
13 #include <cstdint>
14 #include <cassert>
15 #include <algorithm>
16 #include <numeric>
17 
19 
20 class FTLCluster {
21 public:
22  typedef DetId key_type;
23 
24  class FTLHit {
25  public:
26  constexpr FTLHit() : x_(0), y_(0), energy_(0), time_(0), time_error_(0) {}
27  constexpr FTLHit(uint16_t hit_x, uint16_t hit_y, float hit_energy, float hit_time, float hit_time_error)
28  : x_(hit_x), y_(hit_y), energy_(hit_energy), time_(hit_time), time_error_(hit_time_error) {}
29  constexpr uint16_t x() { return x_; }
30  constexpr uint16_t y() { return y_; }
31  constexpr uint16_t energy() { return energy_; }
32  constexpr uint16_t time() { return time_; }
33  constexpr uint16_t time_error() { return time_error_; }
34 
35  private:
36  uint16_t x_; //row
37  uint16_t y_; //col
38  float energy_;
39  float time_;
40  float time_error_;
41  };
42 
43  //--- Integer shift in x and y directions.
44  class Shift {
45  public:
46  constexpr Shift(int dx, int dy) : dx_(dx), dy_(dy) {}
47  constexpr Shift() : dx_(0), dy_(0) {}
48  constexpr int dx() const { return dx_; }
49  constexpr int dy() const { return dy_; }
50 
51  private:
52  int dx_;
53  int dy_;
54  };
55 
56  //--- Position of a FTL Hit
57  class FTLHitPos {
58  public:
59  constexpr FTLHitPos() : row_(0), col_(0) {}
60  constexpr FTLHitPos(int row, int col) : row_(row), col_(col) {}
61  constexpr int row() const { return row_; }
62  constexpr int col() const { return col_; }
63  constexpr FTLHitPos operator+(const Shift& shift) const {
64  return FTLHitPos(row() + shift.dx(), col() + shift.dy());
65  }
66 
67  private:
68  int row_;
69  int col_;
70  };
71 
72  static constexpr unsigned int MAXSPAN = 255;
73  static constexpr unsigned int MAXPOS = 2047;
74 
79 
81  unsigned int isize,
82  float const* energys,
83  float const* times,
84  float const* time_errors,
85  uint16_t const* xpos,
86  uint16_t const* ypos,
87  uint16_t const xmin,
88  uint16_t const ymin)
89  : theid(id),
90  theHitOffset(2 * isize),
91  theHitENERGY(energys, energys + isize),
92  theHitTIME(times, times + isize),
93  theHitTIME_ERROR(time_errors, time_errors + isize) {
94  uint16_t maxCol = 0;
95  uint16_t maxRow = 0;
96  int maxHit = -1;
97  float maxEnergy = -99999;
98  for (unsigned int i = 0; i != isize; ++i) {
99  uint16_t xoffset = xpos[i] - xmin;
100  uint16_t yoffset = ypos[i] - ymin;
101  theHitOffset[i * 2] = std::min(uint16_t(MAXSPAN), xoffset);
102  theHitOffset[i * 2 + 1] = std::min(uint16_t(MAXSPAN), yoffset);
103  if (xoffset > maxRow)
104  maxRow = xoffset;
105  if (yoffset > maxCol)
106  maxCol = yoffset;
107  if (theHitENERGY[i] > maxEnergy) {
108  maxHit = i;
110  }
111  }
112  packRow(xmin, maxRow);
113  packCol(ymin, maxCol);
114 
115  if (maxHit >= 0)
116  seed_ = std::min(uint8_t(MAXSPAN), uint8_t(maxHit));
117  }
118 
119  // linear average position (barycenter)
120  inline float x() const {
121  auto x_pos = [this](unsigned int i) { return this->theHitOffset[i * 2] + minHitRow(); };
122  return weighted_mean(this->theHitENERGY, x_pos);
123  }
124 
125  inline float y() const {
126  auto y_pos = [this](unsigned int i) { return this->theHitOffset[i * 2 + 1] + minHitCol(); };
127  return weighted_mean(this->theHitENERGY, y_pos);
128  }
129 
130  inline float positionError(const float sigmaPos) const {
131  float sumW2(0.f), sumW(0.f);
132  for (const auto& hitW : theHitENERGY) {
133  sumW2 += hitW * hitW;
134  sumW += hitW;
135  }
136  if (sumW > 0)
137  return sigmaPos * std::sqrt(sumW2) / sumW;
138  else
139  return -999.f;
140  }
141 
142  inline float time() const {
143  auto t = [this](unsigned int i) { return this->theHitTIME[i]; };
144  return weighted_mean(this->theHitENERGY, t);
145  }
146 
147  inline float timeError() const {
148  auto t_err = [this](unsigned int i) { return this->theHitTIME_ERROR[i]; };
149  return weighted_mean_error(this->theHitENERGY, t_err);
150  }
151 
152  // Return number of hits.
153  inline int size() const { return theHitENERGY.size(); }
154 
155  // Return cluster dimension in the x direction.
156  inline int sizeX() const { return rowSpan() + 1; }
157 
158  // Return cluster dimension in the y direction.
159  inline int sizeY() const { return colSpan() + 1; }
160 
161  inline float energy() const {
162  return std::accumulate(theHitENERGY.begin(), theHitENERGY.end(), 0.f);
163  } // Return total cluster energy.
164 
165  inline int minHitRow() const { return theMinHitRow; } // The min x index.
166  inline int maxHitRow() const { return minHitRow() + rowSpan(); } // The max x index.
167  inline int minHitCol() const { return theMinHitCol; } // The min y index.
168  inline int maxHitCol() const { return minHitCol() + colSpan(); } // The max y index.
169 
170  const std::vector<uint8_t>& hitOffset() const { return theHitOffset; }
171  const std::vector<float>& hitENERGY() const { return theHitENERGY; }
172  const std::vector<float>& hitTIME() const { return theHitTIME; }
173  const std::vector<float>& hitTIME_ERROR() const { return theHitTIME_ERROR; }
174 
175  // infinite faster than above...
176  FTLHit hit(int i) const {
177  return FTLHit(minHitRow() + theHitOffset[i * 2],
178  minHitCol() + theHitOffset[i * 2 + 1],
179  theHitENERGY[i],
180  theHitTIME[i],
182  }
183 
184  FTLHit seed() const { return hit(seed_); }
185 
186  int colSpan() const { return theHitColSpan; }
187 
188  int rowSpan() const { return theHitRowSpan; }
189 
190  const DetId& id() const { return theid; }
191  const DetId& detid() const { return id(); }
192 
193  bool overflowCol() const { return overflow_(theHitColSpan); }
194 
195  bool overflowRow() const { return overflow_(theHitRowSpan); }
196 
197  bool overflow() const { return overflowCol() || overflowRow(); }
198 
199  void packCol(uint16_t ymin, uint16_t yspan) {
200  theMinHitCol = ymin;
201  theHitColSpan = std::min(yspan, uint16_t(MAXSPAN));
202  }
203  void packRow(uint16_t xmin, uint16_t xspan) {
204  theMinHitRow = xmin;
205  theHitRowSpan = std::min(xspan, uint16_t(MAXSPAN));
206  }
207 
208  void setClusterPosX(float posx) { pos_x = posx; }
209  void setClusterErrorX(float errx) { err_x = errx; }
210  void setClusterErrorTime(float errtime) { err_time = errtime; }
211  float getClusterPosX() const { return pos_x; }
212  float getClusterErrorX() const { return err_x; }
213  float getClusterErrorTime() const { return err_time; }
214 
215 private:
217 
218  std::vector<uint8_t> theHitOffset;
219  std::vector<float> theHitENERGY;
220  std::vector<float> theHitTIME;
221  std::vector<float> theHitTIME_ERROR;
222 
223  uint16_t theMinHitRow = MAXPOS; // Minimum hit index in the x direction (low edge).
224  uint16_t theMinHitCol = MAXPOS; // Minimum hit index in the y direction (left edge).
225  uint8_t theHitRowSpan = 0; // Span hit index in the x direction (low edge).
226  uint8_t theHitColSpan = 0; // Span hit index in the y direction (left edge).
227 
228  float pos_x = -99999.9f; // For pixels with internal position information in one coordinate (i.e. BTL crystals)
229  float err_x = -99999.9f; // For pixels with internal position information in one coordinate (i.e. BTL crystals)
230  float err_time = -99999.9f;
231 
232  uint8_t seed_;
233 
234  template <typename SumFunc, typename OutFunc>
235  float weighted_sum(const std::vector<float>& weights, SumFunc&& sumFunc, OutFunc&& outFunc) const {
236  float tot = 0;
237  float sumW = 0;
238  for (unsigned int i = 0; i < weights.size(); ++i) {
239  tot += sumFunc(i);
240  sumW += weights[i];
241  }
242  return outFunc(tot, sumW);
243  }
244 
245  template <typename Value>
246  float weighted_mean(const std::vector<float>& weights, Value&& value) const {
247  auto sumFunc = [&weights, value](unsigned int i) { return weights[i] * value(i); };
248  auto outFunc = [](float x, float y) {
249  if (y > 0)
250  return (float)x / y;
251  else
252  return -999.f;
253  };
254  return weighted_sum(weights, sumFunc, outFunc);
255  }
256 
257  template <typename Err>
258  float weighted_mean_error(const std::vector<float>& weights, Err&& err) const {
259  auto sumFunc = [&weights, err](unsigned int i) { return weights[i] * weights[i] * err(i) * err(i); };
260  auto outFunc = [](float x, float y) {
261  if (y > 0)
262  return (float)sqrt(x) / y;
263  else
264  return -999.f;
265  };
266  return weighted_sum(weights, sumFunc, outFunc);
267  }
268 
269  static int overflow_(uint16_t span) { return span == uint16_t(MAXSPAN); }
270 };
271 
272 // Comparison operators (needed by DetSetVector & SortedCollection )
273 inline bool operator<(const FTLCluster& one, const FTLCluster& other) {
274  if (one.detid() == other.detid()) {
275  if (one.minHitRow() < other.minHitRow()) {
276  return true;
277  } else if (one.minHitRow() > other.minHitRow()) {
278  return false;
279  } else if (one.minHitCol() < other.minHitCol()) {
280  return true;
281  } else {
282  return false;
283  }
284  }
285  return one.detid() < other.detid();
286 }
287 
288 inline bool operator<(const FTLCluster& one, const uint32_t& detid) { return one.detid() < detid; }
289 
290 inline bool operator<(const uint32_t& detid, const FTLCluster& other) { return detid < other.detid(); }
291 
292 #endif
constexpr FTLHitPos(int row, int col)
Definition: FTLCluster.h:60
Err
Definition: cuy.py:875
void setClusterErrorTime(float errtime)
Definition: FTLCluster.h:210
uint16_t theMinHitCol
Definition: FTLCluster.h:224
float x() const
Definition: FTLCluster.h:120
int size() const
Definition: FTLCluster.h:153
uint8_t theHitColSpan
Definition: FTLCluster.h:226
const std::vector< uint8_t > & hitOffset() const
Definition: FTLCluster.h:170
int sizeX() const
Definition: FTLCluster.h:156
void setClusterPosX(float posx)
Definition: FTLCluster.h:208
constexpr Shift(int dx, int dy)
Definition: FTLCluster.h:46
uint8_t theHitRowSpan
Definition: FTLCluster.h:225
constexpr uint16_t time_error()
Definition: FTLCluster.h:33
void setClusterErrorX(float errx)
Definition: FTLCluster.h:209
constexpr FTLHit()
Definition: FTLCluster.h:26
std::vector< float > theHitTIME_ERROR
Definition: FTLCluster.h:221
constexpr int dy() const
Definition: FTLCluster.h:49
static constexpr unsigned int MAXPOS
Definition: FTLCluster.h:73
const std::vector< float > & hitTIME() const
Definition: FTLCluster.h:172
float err_time
Definition: FTLCluster.h:230
float pos_x
Definition: FTLCluster.h:228
FTLCluster(DetId id, unsigned int isize, float const *energys, float const *times, float const *time_errors, uint16_t const *xpos, uint16_t const *ypos, uint16_t const xmin, uint16_t const ymin)
Definition: FTLCluster.h:80
constexpr uint16_t time()
Definition: FTLCluster.h:32
float getClusterErrorTime() const
Definition: FTLCluster.h:213
static constexpr unsigned int MAXSPAN
Definition: FTLCluster.h:72
constexpr uint16_t energy()
Definition: FTLCluster.h:31
float getClusterErrorX() const
Definition: FTLCluster.h:212
void packCol(uint16_t ymin, uint16_t yspan)
Definition: FTLCluster.h:199
float y() const
Definition: FTLCluster.h:125
bool overflowCol() const
Definition: FTLCluster.h:193
std::vector< uint8_t > theHitOffset
Definition: FTLCluster.h:218
float positionError(const float sigmaPos) const
Definition: FTLCluster.h:130
int maxHitRow() const
Definition: FTLCluster.h:166
T sqrt(T t)
Definition: SSEVec.h:19
const DetId & detid() const
Definition: FTLCluster.h:191
std::vector< float > theHitTIME
Definition: FTLCluster.h:220
constexpr int row() const
Definition: FTLCluster.h:61
reco::JetExtendedAssociation::JetExtendedData Value
constexpr FTLHit(uint16_t hit_x, uint16_t hit_y, float hit_energy, float hit_time, float hit_time_error)
Definition: FTLCluster.h:27
void packRow(uint16_t xmin, uint16_t xspan)
Definition: FTLCluster.h:203
float weighted_mean(const std::vector< float > &weights, Value &&value) const
Definition: FTLCluster.h:246
double f[11][100]
DetId key_type
Definition: FTLCluster.h:22
uint16_t theMinHitRow
Definition: FTLCluster.h:223
Definition: value.py:1
DetId theid
Definition: FTLCluster.h:216
float getClusterPosX() const
Definition: FTLCluster.h:211
const std::vector< float > & hitENERGY() const
Definition: FTLCluster.h:171
float err_x
Definition: FTLCluster.h:229
constexpr FTLHitPos()
Definition: FTLCluster.h:59
int rowSpan() const
Definition: FTLCluster.h:188
float weighted_sum(const std::vector< float > &weights, SumFunc &&sumFunc, OutFunc &&outFunc) const
Definition: FTLCluster.h:235
std::vector< float > theHitENERGY
Definition: FTLCluster.h:219
float weighted_mean_error(const std::vector< float > &weights, Err &&err) const
Definition: FTLCluster.h:258
Definition: DetId.h:17
bool operator<(const FTLCluster &one, const FTLCluster &other)
Definition: FTLCluster.h:273
const DetId & id() const
Definition: FTLCluster.h:190
int minHitCol() const
Definition: FTLCluster.h:167
bool overflowRow() const
Definition: FTLCluster.h:195
float energy() const
Definition: FTLCluster.h:161
FTLHit seed() const
Definition: FTLCluster.h:184
constexpr uint16_t y()
Definition: FTLCluster.h:30
int sizeY() const
Definition: FTLCluster.h:159
int maxHitCol() const
Definition: FTLCluster.h:168
const std::vector< float > & hitTIME_ERROR() const
Definition: FTLCluster.h:173
float timeError() const
Definition: FTLCluster.h:147
int minHitRow() const
Definition: FTLCluster.h:165
static unsigned int const shift
constexpr int dx() const
Definition: FTLCluster.h:48
constexpr FTLHitPos operator+(const Shift &shift) const
Definition: FTLCluster.h:63
float time() const
Definition: FTLCluster.h:142
FTLHit hit(int i) const
Definition: FTLCluster.h:176
static int overflow_(uint16_t span)
Definition: FTLCluster.h:269
int colSpan() const
Definition: FTLCluster.h:186
constexpr int col() const
Definition: FTLCluster.h:62
uint8_t seed_
Definition: FTLCluster.h:232
constexpr uint16_t x()
Definition: FTLCluster.h:29
bool overflow() const
Definition: FTLCluster.h:197
constexpr Shift()
Definition: FTLCluster.h:47