CMS 3D CMS Logo

HitStructures.cc
Go to the documentation of this file.
2 
4 #include "Matriplex/Memory.h"
5 
6 #include "Debug.h"
7 
8 namespace mkfit {
9 
10  void LayerOfHits::Initializator::setup(float qmin, float qmax, float dq) {
11  assert(qmax > qmin);
12  float extent = qmax - qmin;
13  m_nq = std::ceil(extent / dq);
14  float extra = 0.5f * (m_nq * dq - extent);
15  m_qmin = qmin - extra;
16  m_qmax = qmax + extra;
17  }
18 
19  LayerOfHits::Initializator::Initializator(const LayerInfo &li, float qmin, float qmax, unsigned int nq)
20  : m_linfo(li), m_qmin(qmin), m_qmax(qmax), m_nq(nq) {}
21 
22  LayerOfHits::Initializator::Initializator(const LayerInfo &li, float qmin, float qmax, float dq) : m_linfo(li) {
23  setup(qmin, qmax, dq);
24  }
25 
27  if (li.is_barrel())
28  setup(li.zmin(), li.zmax(), li.q_bin());
29  else
30  setup(li.rin(), li.rout(), li.q_bin());
31  }
32 
34  : m_ax_phi(-Const::PI, Const::PI),
35  m_ax_eta(i.m_qmin, i.m_qmax, i.m_nq),
36  m_binnor(m_ax_phi, m_ax_eta, true, false) // yes-radix, no-keep-cons
37  {
38  m_layer_info = &i.m_linfo;
40 
42  }
43 
45 #ifdef COPY_SORTED_HITS
46  free_hits();
47 #endif
48  }
49 
50 #ifdef COPY_SORTED_HITS
51  void LayerOfHits::alloc_hits(int size) {
52  m_hits = (Hit *)Matriplex::aligned_alloc64(sizeof(Hit) * size);
53  m_capacity = size;
54  for (int ihit = 0; ihit < m_capacity; ihit++) {
55  m_hits[ihit] = Hit();
56  }
57  }
58 
59  void LayerOfHits::free_hits() { std::free(m_hits); }
60 #endif
61 
62  //==============================================================================
63 
65  m_hit_infos.clear();
66  m_ext_idcs.clear();
69  m_n_hits = 0;
71  }
72 
73  //==============================================================================
74 
75  void LayerOfHits::suckInHits(const HitVec &hitv) {
76  m_ext_hits = &hitv;
77  m_n_hits = hitv.size();
78 
80 
81 #ifdef COPY_SORTED_HITS
82  if (m_capacity < m_n_hits) {
83  free_hits();
84  alloc_hits(m_n_hits);
85  }
86 #endif
87 
88  std::vector<HitInfo> hinfos;
90  hinfos.reserve(m_n_hits);
91  m_hit_infos.reserve(m_n_hits);
92  }
93 
94  // Factor to get from hit sigma to half-length in q direction.
95  const float hl_fac = is_pixel() ? 3.0f : std::sqrt(3.0f);
96 
97  for (unsigned int i = 0; i < m_n_hits; ++i) {
98  const Hit &h = hitv[i];
99 
100  float phi = h.phi();
101  float q = m_is_barrel ? h.z() : h.r();
102 
104 
105  if (Config::usePhiQArrays) {
106  float half_length, qbar;
107  if (m_is_barrel) {
108  half_length = hl_fac * std::sqrt(h.ezz());
109  qbar = h.r();
110  } else {
111  half_length = hl_fac * std::sqrt(h.exx() + h.eyy());
112  qbar = h.z();
113  }
114  hinfos.emplace_back(HitInfo({phi, q, half_length, qbar}));
115  }
116  }
117 
119 
120  for (unsigned int i = 0; i < m_n_hits; ++i) {
121  unsigned int j = m_binnor.m_ranks[i];
122 #ifdef COPY_SORTED_HITS
123  memcpy(&m_hits[i], &hitv[j], sizeof(Hit));
124 #endif
125  if (Config::usePhiQArrays) {
126  m_hit_infos.emplace_back(hinfos[j]);
127  }
128  }
129  }
130 
131  //==============================================================================
132 
133  void LayerOfHits::suckInDeads(const DeadVec &deadv) {
134  m_dead_bins.assign(m_dead_bins.size(), false);
135 
136  for (const auto &d : deadv) {
137  bin_index_t q_bin_1 = qBinChecked(d.q1);
138  bin_index_t q_bin_2 = qBinChecked(d.q2) + 1;
139  bin_index_t phi_bin_1 = phiBin(d.phi1);
140  bin_index_t phi_bin_2 = phiMaskApply(phiBin(d.phi2) + 1);
141 
142  for (bin_index_t q_bin = q_bin_1; q_bin != q_bin_2; q_bin++) {
143  const unsigned int qoff = q_bin * m_ax_phi.size_of_N();
144  for (bin_index_t pb = phi_bin_1; pb != phi_bin_2; pb = phiMaskApply(pb + 1)) {
145  m_dead_bins[qoff + pb] = true;
146  }
147  }
148  }
149  }
150 
151  //==============================================================================
152 
154  m_ext_hits = &hitv;
155  m_n_hits = 0;
156 
157  m_binnor.begin_registration(128); // initial reserve for cons vectors
158  }
159 
160  void LayerOfHits::registerHit(unsigned int idx) {
161  const Hit &h = (*m_ext_hits)[idx];
162 
163  m_ext_idcs.push_back(idx);
166 
167  float phi = h.phi();
168  float q = m_is_barrel ? h.z() : h.r();
169 
171 
172  if (Config::usePhiQArrays) {
173  // Factor to get from hit sigma to half-length in q direction.
174  const float hl_fac = is_pixel() ? 3.0f : std::sqrt(3.0f);
175  float half_length, qbar;
176  if (m_is_barrel) {
177  half_length = hl_fac * std::sqrt(h.ezz());
178  qbar = h.r();
179  } else {
180  half_length = hl_fac * std::sqrt(h.exx() + h.eyy());
181  qbar = h.z();
182  }
183  m_hit_infos.emplace_back(HitInfo({phi, q, half_length, qbar}));
184  }
185  }
186 
187  void LayerOfHits::endRegistrationOfHits(bool build_original_to_internal_map) {
188  m_n_hits = m_ext_idcs.size();
189  if (m_n_hits == 0)
190  return;
191 
193 
194  // copy q/phi
195 
196 #ifdef COPY_SORTED_HITS
197  if (m_capacity < m_n_hits) {
198  free_hits();
199  alloc_hits(m_n_hits);
200  }
201 #endif
202 
203  std::vector<HitInfo> hinfos;
204  if (Config::usePhiQArrays) {
205  hinfos.swap(m_hit_infos);
206  m_hit_infos.reserve(m_n_hits);
207  }
208 
209  for (unsigned int i = 0; i < m_n_hits; ++i) {
210  unsigned int j = m_binnor.m_ranks[i]; // index in intermediate
211  unsigned int k = m_ext_idcs[j]; // index in external hit_vec
212 
213 #ifdef COPY_SORTED_HITS
214  memcpy(&m_hits[i], &hitv[k], sizeof(Hit));
215 #endif
216 
217  if (Config::usePhiQArrays) {
218  m_hit_infos.emplace_back(hinfos[j]);
219  }
220 
221  // Redirect m_binnor.m_ranks[i] to point to external/original index.
222  m_binnor.m_ranks[i] = k;
223  }
224 
225  if (build_original_to_internal_map) {
226  if (m_max_ext_idx - m_min_ext_idx + 1 > 8 * m_n_hits) {
227  // If this happens we might:
228  // a) Use external indices for everything. -- *** We are now. ***
229  // b) Build these maps for seeding layers only.
230  // c) Have a flag in hit-on-track that tells us if the hit index has been remapped,
231  // essentially, if it is a seed hit. This might be smart anyway.
232  // One could use index < -256 or something similar.
233 
234  printf(
235  "LayerOfHits::endRegistrationOfHits() original_to_internal index map vector is largish: m_n_hits=%d, "
236  "map_vector_size=%d\n",
237  m_n_hits,
239  }
240 
241  m_ext_idcs.resize(m_max_ext_idx - m_min_ext_idx + 1);
242  for (unsigned int i = 0; i < m_n_hits; ++i) {
244  }
245  }
246 
247  // We can release m_hit_infos and, if not used, also m_ext_idcs -- and realloc them
248  // on next beginRegistration().
249  // If binnor had keep_cons on we could use it for pre-selection in selectHitIndices()
250  // instead of q and phi arrays -- assuming sufficient precision can be achieved..
251  }
252 
254  for (bin_index_t qb = 0; qb <= m_ax_eta.m_last_N_bin; ++qb) {
255  printf("%c bin %d\n", is_barrel() ? 'Z' : 'R', qb);
256  for (bin_index_t pb = 0; pb <= m_ax_phi.m_last_N_bin; ++pb) {
257  if (pb % 8 == 0)
258  printf(" Phi %4d: ", pb);
259  auto content = m_binnor.get_content(pb, qb);
260  printf("%5d,%4d %s", content.first, content.count, ((pb + 1) % 8 == 0) ? "\n" : "");
261  }
262  }
263  }
264 
265  //==============================================================================
266  // EventOfHits
267  //==============================================================================
268 
269  EventOfHits::EventOfHits(const TrackerInfo &trk_inf) : m_n_layers(trk_inf.n_layers()) {
270  m_layers_of_hits.reserve(trk_inf.n_layers());
271  for (int ii = 0; ii < trk_inf.n_layers(); ++ii) {
272  m_layers_of_hits.emplace_back(LayerOfHits::Initializator(trk_inf.layer(ii)));
273  }
274  }
275 
276 } // end namespace mkfit
void endRegistrationOfHits(bool build_original_to_internal_map)
constexpr int32_t ceil(float num)
void setup(float qmin, float qmax, float dq)
float rin() const
Definition: TrackerInfo.h:67
unsigned int m_n_hits
float q_bin() const
Definition: TrackerInfo.h:74
bin_index_t qBinChecked(float q) const
Definition: HitStructures.h:73
const HitVec * m_ext_hits
C_pair get_content(B_pair n_bin) const
Definition: binnor.h:240
bool is_pixel() const
unsigned int m_min_ext_idx
assert(be >=bs)
float zmax() const
Definition: TrackerInfo.h:71
void finalize_registration()
Definition: binnor.h:289
constexpr bool usePhiQArrays
Definition: Config.h:71
void free(void *ptr) noexcept
std::vector< C > m_ranks
Definition: binnor.h:211
float zmin() const
Definition: TrackerInfo.h:70
unsigned int size_of_N() const
Definition: binnor.h:143
bin_index_t phiBin(float phi) const
Definition: HitStructures.h:76
T sqrt(T t)
Definition: SSEVec.h:19
unsigned int * m_hit_ranks
void register_entry_safe(typename A1::real_t r1, typename A2::real_t r2)
Definition: binnor.h:282
int n_layers() const
Definition: TrackerInfo.h:202
void suckInDeads(const DeadVec &deadv)
double f[11][100]
d
Definition: ztail.py:151
void begin_registration(C n_items)
Definition: binnor.h:264
std::vector< bool > m_dead_bins
SeedingHitSet::ConstRecHitPointer Hit
ii
Definition: cuy.py:589
std::vector< Hit > HitVec
const LayerInfo & layer(int l) const
Definition: TrackerInfo.h:203
Initializator(const LayerInfo &li, float qmin, float qmax, unsigned int nq)
std::vector< DeadRegion > DeadVec
Definition: Hit.h:280
LayerOfHits(const LayerOfHits::Initializator &i)
unsigned int size_of_N() const
Definition: binnor.h:88
unsigned int m_max_ext_idx
void suckInHits(const HitVec &hitv)
float rout() const
Definition: TrackerInfo.h:68
void beginRegistrationOfHits(const HitVec &hitv)
EventOfHits(const TrackerInfo &trk_inf)
const I m_last_N_bin
Definition: binnor.h:36
void * aligned_alloc64(std::size_t size)
Definition: Memory.h:13
std::vector< unsigned int > m_ext_idcs
bool is_barrel() const
void registerHit(unsigned int idx)
const LayerInfo * m_layer_info
unsigned short bin_index_t
Definition: HitStructures.h:26
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
bin_index_t phiMaskApply(bin_index_t in) const
Definition: HitStructures.h:79
std::vector< LayerOfHits > m_layers_of_hits
std::vector< HitInfo > m_hit_infos
bool is_barrel() const
Definition: TrackerInfo.h:77
void reset_contents(bool shrink_vectors=true)
Definition: binnor.h:252