CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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;
109  maxEnergy = theHitENERGY[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() + 0.5f; };
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() + 0.5f; };
127  return weighted_mean(this->theHitENERGY, y_pos);
128  }
129 
130  inline float time() const {
131  auto t = [this](unsigned int i) { return this->theHitTIME[i]; };
132  return weighted_mean(this->theHitENERGY, t);
133  }
134 
135  inline float timeError() const {
136  auto t_err = [this](unsigned int i) { return this->theHitTIME_ERROR[i]; };
137  return weighted_mean_error(this->theHitENERGY, t_err);
138  }
139 
140  // Return number of hits.
141  inline int size() const { return theHitENERGY.size(); }
142 
143  // Return cluster dimension in the x direction.
144  inline int sizeX() const { return rowSpan() + 1; }
145 
146  // Return cluster dimension in the y direction.
147  inline int sizeY() const { return colSpan() + 1; }
148 
149  inline float energy() const {
150  return std::accumulate(theHitENERGY.begin(), theHitENERGY.end(), 0.f);
151  } // Return total cluster energy.
152 
153  inline int minHitRow() const { return theMinHitRow; } // The min x index.
154  inline int maxHitRow() const { return minHitRow() + rowSpan(); } // The max x index.
155  inline int minHitCol() const { return theMinHitCol; } // The min y index.
156  inline int maxHitCol() const { return minHitCol() + colSpan(); } // The max y index.
157 
158  const std::vector<uint8_t>& hitOffset() const { return theHitOffset; }
159  const std::vector<float>& hitENERGY() const { return theHitENERGY; }
160  const std::vector<float>& hitTIME() const { return theHitTIME; }
161  const std::vector<float>& hitTIME_ERROR() const { return theHitTIME_ERROR; }
162 
163  // infinite faster than above...
164  FTLHit hit(int i) const {
165  return FTLHit(minHitRow() + theHitOffset[i * 2],
166  minHitCol() + theHitOffset[i * 2 + 1],
167  theHitENERGY[i],
168  theHitTIME[i],
169  theHitTIME_ERROR[i]);
170  }
171 
172  FTLHit seed() const { return hit(seed_); }
173 
174  int colSpan() const { return theHitColSpan; }
175 
176  int rowSpan() const { return theHitRowSpan; }
177 
178  const DetId& id() const { return theid; }
179  const DetId& detid() const { return id(); }
180 
181  bool overflowCol() const { return overflow_(theHitColSpan); }
182 
183  bool overflowRow() const { return overflow_(theHitRowSpan); }
184 
185  bool overflow() const { return overflowCol() || overflowRow(); }
186 
187  void packCol(uint16_t ymin, uint16_t yspan) {
188  theMinHitCol = ymin;
189  theHitColSpan = std::min(yspan, uint16_t(MAXSPAN));
190  }
191  void packRow(uint16_t xmin, uint16_t xspan) {
192  theMinHitRow = xmin;
193  theHitRowSpan = std::min(xspan, uint16_t(MAXSPAN));
194  }
195 
196  void setClusterErrorX(float errx) { err_x = errx; }
197  void setClusterErrorY(float erry) { err_y = erry; }
198  void setClusterErrorTime(float errtime) { err_time = errtime; }
199  float getClusterErrorX() const { return err_x; }
200  float getClusterErrorY() const { return err_y; }
201  float getClusterErrorTime() const { return err_time; }
202 
203 private:
205 
206  std::vector<uint8_t> theHitOffset;
207  std::vector<float> theHitENERGY;
208  std::vector<float> theHitTIME;
209  std::vector<float> theHitTIME_ERROR;
210 
211  uint16_t theMinHitRow = MAXPOS; // Minimum hit index in the x direction (low edge).
212  uint16_t theMinHitCol = MAXPOS; // Minimum hit index in the y direction (left edge).
213  uint8_t theHitRowSpan = 0; // Span hit index in the x direction (low edge).
214  uint8_t theHitColSpan = 0; // Span hit index in the y direction (left edge).
215 
216  float err_x = -99999.9f;
217  float err_y = -99999.9f;
218  float err_time = -99999.9f;
219 
220  uint8_t seed_;
221 
222  template <typename SumFunc, typename OutFunc>
223  float weighted_sum(const std::vector<float>& weights, SumFunc&& sumFunc, OutFunc&& outFunc) const {
224  float tot = 0;
225  float sumW = 0;
226  for (unsigned int i = 0; i < weights.size(); ++i) {
227  tot += sumFunc(i);
228  sumW += weights[i];
229  }
230  return outFunc(tot, sumW);
231  }
232 
233  template <typename Value>
234  float weighted_mean(const std::vector<float>& weights, Value&& value) const {
235  auto sumFunc = [&weights, value](unsigned int i) { return weights[i] * value(i); };
236  auto outFunc = [](float x, float y) {
237  if (y > 0)
238  return (float)x / y;
239  else
240  return -999.f;
241  };
242  return weighted_sum(weights, sumFunc, outFunc);
243  }
244 
245  template <typename Err>
246  float weighted_mean_error(const std::vector<float>& weights, Err&& err) const {
247  auto sumFunc = [&weights, err](unsigned int i) { return weights[i] * weights[i] * err(i) * err(i); };
248  auto outFunc = [](float x, float y) {
249  if (y > 0)
250  return (float)sqrt(x) / y;
251  else
252  return -999.f;
253  };
254  return weighted_sum(weights, sumFunc, outFunc);
255  }
256 
257  static int overflow_(uint16_t span) { return span == uint16_t(MAXSPAN); }
258 };
259 
260 // Comparison operators (needed by DetSetVector & SortedCollection )
261 inline bool operator<(const FTLCluster& one, const FTLCluster& other) {
262  if (one.detid() == other.detid()) {
263  if (one.minHitRow() < other.minHitRow()) {
264  return true;
265  } else if (one.minHitRow() > other.minHitRow()) {
266  return false;
267  } else if (one.minHitCol() < other.minHitCol()) {
268  return true;
269  } else {
270  return false;
271  }
272  }
273  return one.detid() < other.detid();
274 }
275 
276 inline bool operator<(const FTLCluster& one, const uint32_t& detid) { return one.detid() < detid; }
277 
278 inline bool operator<(const uint32_t& detid, const FTLCluster& other) { return detid < other.detid(); }
279 
280 #endif
constexpr FTLHitPos(int row, int col)
Definition: FTLCluster.h:60
void setClusterErrorTime(float errtime)
Definition: FTLCluster.h:198
int maxHitRow() const
Definition: FTLCluster.h:154
float err_y
Definition: FTLCluster.h:217
uint16_t theMinHitCol
Definition: FTLCluster.h:212
const std::vector< float > & hitTIME_ERROR() const
Definition: FTLCluster.h:161
Err
Definition: cuy.py:875
float y() const
Definition: FTLCluster.h:125
uint8_t theHitColSpan
Definition: FTLCluster.h:214
constexpr Shift(int dx, int dy)
Definition: FTLCluster.h:46
constexpr int dy() const
Definition: FTLCluster.h:49
uint8_t theHitRowSpan
Definition: FTLCluster.h:213
int minHitRow() const
Definition: FTLCluster.h:153
constexpr uint16_t time_error()
Definition: FTLCluster.h:33
float time() const
Definition: FTLCluster.h:130
void setClusterErrorX(float errx)
Definition: FTLCluster.h:196
constexpr FTLHit()
Definition: FTLCluster.h:26
std::vector< float > theHitTIME_ERROR
Definition: FTLCluster.h:209
static constexpr unsigned int MAXPOS
Definition: FTLCluster.h:73
float err_time
Definition: FTLCluster.h:218
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
bool overflowCol() const
Definition: FTLCluster.h:181
static constexpr unsigned int MAXSPAN
Definition: FTLCluster.h:72
constexpr uint16_t energy()
Definition: FTLCluster.h:31
void packCol(uint16_t ymin, uint16_t yspan)
Definition: FTLCluster.h:187
void setClusterErrorY(float erry)
Definition: FTLCluster.h:197
float weighted_sum(const std::vector< float > &weights, SumFunc &&sumFunc, OutFunc &&outFunc) const
Definition: FTLCluster.h:223
std::vector< uint8_t > theHitOffset
Definition: FTLCluster.h:206
FTLHit seed() const
Definition: FTLCluster.h:172
float timeError() const
Definition: FTLCluster.h:135
int sizeX() const
Definition: FTLCluster.h:144
int rowSpan() const
Definition: FTLCluster.h:176
float x() const
Definition: FTLCluster.h:120
T sqrt(T t)
Definition: SSEVec.h:19
std::vector< float > theHitTIME
Definition: FTLCluster.h:208
const std::vector< float > & hitENERGY() const
Definition: FTLCluster.h:159
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:191
DetId key_type
Definition: FTLCluster.h:22
uint16_t theMinHitRow
Definition: FTLCluster.h:211
const std::vector< uint8_t > & hitOffset() const
Definition: FTLCluster.h:158
DetId theid
Definition: FTLCluster.h:204
int size() const
Definition: FTLCluster.h:141
float err_x
Definition: FTLCluster.h:216
float getClusterErrorY() const
Definition: FTLCluster.h:200
const DetId & id() const
Definition: FTLCluster.h:178
constexpr FTLHitPos()
Definition: FTLCluster.h:59
std::vector< float > theHitENERGY
Definition: FTLCluster.h:207
Definition: DetId.h:17
float energy() const
Definition: FTLCluster.h:149
constexpr uint16_t y()
Definition: FTLCluster.h:30
float getClusterErrorX() const
Definition: FTLCluster.h:199
constexpr int dx() const
Definition: FTLCluster.h:48
bool operator<(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:70
int minHitCol() const
Definition: FTLCluster.h:155
FTLHit hit(int i) const
Definition: FTLCluster.h:164
int maxHitCol() const
Definition: FTLCluster.h:156
static unsigned int const shift
constexpr FTLHitPos operator+(const Shift &shift) const
Definition: FTLCluster.h:63
int colSpan() const
Definition: FTLCluster.h:174
static int overflow_(uint16_t span)
Definition: FTLCluster.h:257
const std::vector< float > & hitTIME() const
Definition: FTLCluster.h:160
constexpr int col() const
Definition: FTLCluster.h:62
bool overflow() const
Definition: FTLCluster.h:185
uint8_t seed_
Definition: FTLCluster.h:220
float weighted_mean(const std::vector< float > &weights, Value &&value) const
Definition: FTLCluster.h:234
bool overflowRow() const
Definition: FTLCluster.h:183
constexpr uint16_t x()
Definition: FTLCluster.h:29
float getClusterErrorTime() const
Definition: FTLCluster.h:201
const DetId & detid() const
Definition: FTLCluster.h:179
constexpr Shift()
Definition: FTLCluster.h:47
int sizeY() const
Definition: FTLCluster.h:147
float weighted_mean_error(const std::vector< float > &weights, Err &&err) const
Definition: FTLCluster.h:246