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