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