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  for (unsigned int i = 0; i < m_n_hits; ++i) {
95  const Hit &h = hitv[i];
96 
97  float phi = h.phi();
98  float q = m_is_barrel ? h.z() : h.r();
99 
101 
102  if (Config::usePhiQArrays) {
103  const float sqrt3 = std::sqrt(3);
104  float half_length, qbar;
105  if (m_is_barrel) {
106  half_length = sqrt3 * std::sqrt(h.ezz());
107  qbar = h.r();
108  } else {
109  half_length = sqrt3 * std::sqrt(h.exx() + h.eyy());
110  qbar = h.z();
111  }
112  hinfos.emplace_back(HitInfo({phi, q, half_length, qbar}));
113  }
114  }
115 
117 
118  for (unsigned int i = 0; i < m_n_hits; ++i) {
119  unsigned int j = m_binnor.m_ranks[i];
120 #ifdef COPY_SORTED_HITS
121  memcpy(&m_hits[i], &hitv[j], sizeof(Hit));
122 #endif
123  if (Config::usePhiQArrays) {
124  m_hit_infos.emplace_back(hinfos[j]);
125  }
126  }
127  }
128 
129  //==============================================================================
130 
131  void LayerOfHits::suckInDeads(const DeadVec &deadv) {
132  m_dead_bins.assign(m_dead_bins.size(), false);
133 
134  for (const auto &d : deadv) {
135  bin_index_t q_bin_1 = qBinChecked(d.q1);
136  bin_index_t q_bin_2 = qBinChecked(d.q2) + 1;
137  bin_index_t phi_bin_1 = phiBin(d.phi1);
138  bin_index_t phi_bin_2 = phiMaskApply(phiBin(d.phi2) + 1);
139 
140  for (bin_index_t q_bin = q_bin_1; q_bin != q_bin_2; q_bin++) {
141  const unsigned int qoff = q_bin * m_ax_phi.size_of_N();
142  for (bin_index_t pb = phi_bin_1; pb != phi_bin_2; pb = phiMaskApply(pb + 1)) {
143  m_dead_bins[qoff + pb] = true;
144  }
145  }
146  }
147  }
148 
149  //==============================================================================
150 
152  m_ext_hits = &hitv;
153  m_n_hits = 0;
154 
155  m_binnor.begin_registration(128); // initial reserve for cons vectors
156  }
157 
158  void LayerOfHits::registerHit(unsigned int idx) {
159  const Hit &h = (*m_ext_hits)[idx];
160 
161  m_ext_idcs.push_back(idx);
164 
165  float phi = h.phi();
166  float q = m_is_barrel ? h.z() : h.r();
167 
169 
170  if (Config::usePhiQArrays) {
171  const float sqrt3 = std::sqrt(3);
172  float half_length, qbar;
173  if (m_is_barrel) {
174  half_length = sqrt3 * std::sqrt(h.ezz());
175  qbar = h.r();
176  } else {
177  half_length = sqrt3 * std::sqrt(h.exx() + h.eyy());
178  qbar = h.z();
179  }
180  m_hit_infos.emplace_back(HitInfo({phi, q, half_length, qbar}));
181  }
182  }
183 
184  void LayerOfHits::endRegistrationOfHits(bool build_original_to_internal_map) {
185  m_n_hits = m_ext_idcs.size();
186  if (m_n_hits == 0)
187  return;
188 
190 
191  // copy q/phi
192 
193 #ifdef COPY_SORTED_HITS
194  if (m_capacity < m_n_hits) {
195  free_hits();
196  alloc_hits(m_n_hits);
197  }
198 #endif
199 
200  std::vector<HitInfo> hinfos;
201  if (Config::usePhiQArrays) {
202  hinfos.swap(m_hit_infos);
203  m_hit_infos.reserve(m_n_hits);
204  }
205 
206  for (unsigned int i = 0; i < m_n_hits; ++i) {
207  unsigned int j = m_binnor.m_ranks[i]; // index in intermediate
208  unsigned int k = m_ext_idcs[j]; // index in external hit_vec
209 
210 #ifdef COPY_SORTED_HITS
211  memcpy(&m_hits[i], &hitv[k], sizeof(Hit));
212 #endif
213 
214  if (Config::usePhiQArrays) {
215  m_hit_infos.emplace_back(hinfos[j]);
216  }
217 
218  // Redirect m_binnor.m_ranks[i] to point to external/original index.
219  m_binnor.m_ranks[i] = k;
220  }
221 
222  if (build_original_to_internal_map) {
223  if (m_max_ext_idx - m_min_ext_idx + 1 > 8 * m_n_hits) {
224  // If this happens we might:
225  // a) Use external indices for everything. -- *** We are now. ***
226  // b) Build these maps for seeding layers only.
227  // c) Have a flag in hit-on-track that tells us if the hit index has been remapped,
228  // essentially, if it is a seed hit. This might be smart anyway.
229  // One could use index < -256 or something similar.
230 
231  printf(
232  "LayerOfHits::endRegistrationOfHits() original_to_internal index map vector is largish: m_n_hits=%d, "
233  "map_vector_size=%d\n",
234  m_n_hits,
236  }
237 
238  m_ext_idcs.resize(m_max_ext_idx - m_min_ext_idx + 1);
239  for (unsigned int i = 0; i < m_n_hits; ++i) {
241  }
242  }
243 
244  // We can release m_hit_infos and, if not used, also m_ext_idcs -- and realloc them
245  // on next beginRegistration().
246  // If binnor had keep_cons on we could use it for pre-selection in selectHitIndices()
247  // instead of q and phi arrays -- assuming sufficient precision can be achieved..
248  }
249 
251  for (bin_index_t qb = 0; qb <= m_ax_eta.m_last_N_bin; ++qb) {
252  printf("%c bin %d\n", is_barrel() ? 'Z' : 'R', qb);
253  for (bin_index_t pb = 0; pb <= m_ax_phi.m_last_N_bin; ++pb) {
254  if (pb % 8 == 0)
255  printf(" Phi %4d: ", pb);
256  auto content = m_binnor.get_content(pb, qb);
257  printf("%5d,%4d %s", content.first, content.count, ((pb + 1) % 8 == 0) ? "\n" : "");
258  }
259  }
260  }
261 
262  //==============================================================================
263  // EventOfHits
264  //==============================================================================
265 
266  EventOfHits::EventOfHits(const TrackerInfo &trk_inf) : m_n_layers(trk_inf.n_layers()) {
267  m_layers_of_hits.reserve(trk_inf.n_layers());
268  for (int ii = 0; ii < trk_inf.n_layers(); ++ii) {
269  m_layers_of_hits.emplace_back(LayerOfHits::Initializator(trk_inf.layer(ii)));
270  }
271  }
272 
273 } // 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:66
unsigned int m_n_hits
float q_bin() const
Definition: TrackerInfo.h:73
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
unsigned int m_min_ext_idx
assert(be >=bs)
float zmax() const
Definition: TrackerInfo.h:70
void finalize_registration()
Definition: binnor.h:289
constexpr bool usePhiQArrays
Definition: Config.h:69
void free(void *ptr) noexcept
std::vector< C > m_ranks
Definition: binnor.h:211
float zmin() const
Definition: TrackerInfo.h:69
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:201
void suckInDeads(const DeadVec &deadv)
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:202
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:67
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:76
void reset_contents(bool shrink_vectors=true)
Definition: binnor.h:252