Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-27 23:38:49

0001 #include "RecoTracker/MkFitCore/interface/HitStructures.h"
0002 
0003 #include "RecoTracker/MkFitCore/interface/IterationConfig.h"
0004 #include "Matriplex/Memory.h"
0005 
0006 #include "Debug.h"
0007 
0008 namespace mkfit {
0009 
0010   void LayerOfHits::Initializator::setup(float qmin, float qmax, float dq) {
0011     assert(qmax > qmin);
0012     float extent = qmax - qmin;
0013     m_nq = std::ceil(extent / dq);
0014     float extra = 0.5f * (m_nq * dq - extent);
0015     m_qmin = qmin - extra;
0016     m_qmax = qmax + extra;
0017   }
0018 
0019   LayerOfHits::Initializator::Initializator(const LayerInfo &li, float qmin, float qmax, unsigned int nq)
0020       : m_linfo(li), m_qmin(qmin), m_qmax(qmax), m_nq(nq) {}
0021 
0022   LayerOfHits::Initializator::Initializator(const LayerInfo &li, float qmin, float qmax, float dq) : m_linfo(li) {
0023     setup(qmin, qmax, dq);
0024   }
0025 
0026   LayerOfHits::Initializator::Initializator(const LayerInfo &li) : m_linfo(li) {
0027     if (li.is_barrel())
0028       setup(li.zmin(), li.zmax(), li.q_bin());
0029     else
0030       setup(li.rin(), li.rout(), li.q_bin());
0031   }
0032 
0033   LayerOfHits::LayerOfHits(const LayerOfHits::Initializator &i)
0034       : m_ax_phi(-Const::PI, Const::PI),
0035         m_ax_eta(i.m_qmin, i.m_qmax, i.m_nq),
0036         m_binnor(m_ax_phi, m_ax_eta, true, false)  // yes-radix, no-keep-cons
0037   {
0038     m_layer_info = &i.m_linfo;
0039     m_is_barrel = m_layer_info->is_barrel();
0040 
0041     m_dead_bins.resize(m_ax_eta.size_of_N() * m_ax_phi.size_of_N());
0042   }
0043 
0044   LayerOfHits::~LayerOfHits() {
0045 #ifdef COPY_SORTED_HITS
0046     free_hits();
0047 #endif
0048   }
0049 
0050 #ifdef COPY_SORTED_HITS
0051   void LayerOfHits::alloc_hits(int size) {
0052     m_hits = (Hit *)Matriplex::aligned_alloc64(sizeof(Hit) * size);
0053     m_capacity = size;
0054     for (int ihit = 0; ihit < m_capacity; ihit++) {
0055       m_hits[ihit] = Hit();
0056     }
0057   }
0058 
0059   void LayerOfHits::free_hits() { std::free(m_hits); }
0060 #endif
0061 
0062   //==============================================================================
0063 
0064   void LayerOfHits::suckInHits(const HitVec &hitv) {
0065     m_n_hits = hitv.size();
0066     m_ext_hits = &hitv;
0067 
0068 #ifdef COPY_SORTED_HITS
0069     if (m_capacity < m_n_hits) {
0070       free_hits();
0071       alloc_hits(m_n_hits);
0072     }
0073 #endif
0074 
0075     if (Config::usePhiQArrays) {
0076       m_hit_phis.resize(m_n_hits);
0077       m_hit_qs.resize(m_n_hits);
0078       m_hit_infos.resize(m_n_hits);
0079     }
0080 
0081     m_binnor.reset_contents();
0082     m_binnor.begin_registration(m_n_hits);
0083 
0084     for (unsigned int i = 0; i < m_n_hits; ++i) {
0085       const Hit &h = hitv[i];
0086 
0087       HitInfo hi = {h.phi(), m_is_barrel ? h.z() : h.r()};
0088 
0089       m_binnor.register_entry_safe(hi.phi, hi.q);
0090 
0091       if (Config::usePhiQArrays) {
0092         m_hit_infos[i] = hi;
0093       }
0094     }
0095 
0096     m_binnor.finalize_registration();
0097 
0098     for (unsigned int i = 0; i < m_n_hits; ++i) {
0099       unsigned int j = m_binnor.m_ranks[i];
0100 #ifdef COPY_SORTED_HITS
0101       memcpy(&m_hits[i], &hitv[j], sizeof(Hit));
0102 #endif
0103       if (Config::usePhiQArrays) {
0104         m_hit_phis[i] = m_hit_infos[j].phi;
0105         m_hit_qs[i] = m_hit_infos[j].q;
0106       }
0107     }
0108   }
0109 
0110   //==============================================================================
0111 
0112   void LayerOfHits::suckInDeads(const DeadVec &deadv) {
0113     m_dead_bins.assign(m_dead_bins.size(), false);
0114 
0115     for (const auto &d : deadv) {
0116       bin_index_t q_bin_1 = qBinChecked(d.q1);
0117       bin_index_t q_bin_2 = qBinChecked(d.q2) + 1;
0118       bin_index_t phi_bin_1 = phiBin(d.phi1);
0119       bin_index_t phi_bin_2 = phiMaskApply(phiBin(d.phi2) + 1);
0120 
0121       for (bin_index_t q_bin = q_bin_1; q_bin != q_bin_2; q_bin++) {
0122         const unsigned int qoff = q_bin * m_ax_phi.size_of_N();
0123         for (bin_index_t pb = phi_bin_1; pb != phi_bin_2; pb = phiMaskApply(pb + 1)) {
0124           m_dead_bins[qoff + pb] = true;
0125         }
0126       }
0127     }
0128   }
0129 
0130   //==============================================================================
0131 
0132   void LayerOfHits::beginRegistrationOfHits(const HitVec &hitv) {
0133     m_ext_hits = &hitv;
0134 
0135     m_n_hits = 0;
0136     m_hit_infos.clear();
0137     m_ext_idcs.clear();
0138     m_min_ext_idx = std::numeric_limits<unsigned int>::max();
0139     m_max_ext_idx = std::numeric_limits<unsigned int>::min();
0140 
0141     m_binnor.reset_contents();
0142     m_binnor.begin_registration(128);  // initial reserve for cons vectors
0143   }
0144 
0145   void LayerOfHits::registerHit(unsigned int idx) {
0146     const Hit &h = (*m_ext_hits)[idx];
0147 
0148     m_ext_idcs.push_back(idx);
0149     m_min_ext_idx = std::min(m_min_ext_idx, idx);
0150     m_max_ext_idx = std::max(m_max_ext_idx, idx);
0151 
0152     HitInfo hi = {h.phi(), m_is_barrel ? h.z() : h.r()};
0153 
0154     m_binnor.register_entry_safe(hi.phi, hi.q);
0155 
0156     if (Config::usePhiQArrays) {
0157       m_hit_infos.emplace_back(hi);
0158     }
0159   }
0160 
0161   void LayerOfHits::endRegistrationOfHits(bool build_original_to_internal_map) {
0162     m_n_hits = m_ext_idcs.size();
0163     if (m_n_hits == 0)
0164       return;
0165 
0166     m_binnor.finalize_registration();
0167 
0168     // copy q/phi
0169 
0170 #ifdef COPY_SORTED_HITS
0171     if (m_capacity < m_n_hits) {
0172       free_hits();
0173       alloc_hits(m_n_hits);
0174     }
0175 #endif
0176 
0177     if (Config::usePhiQArrays) {
0178       m_hit_phis.resize(m_n_hits);
0179       m_hit_qs.resize(m_n_hits);
0180     }
0181 
0182     for (unsigned int i = 0; i < m_n_hits; ++i) {
0183       unsigned int j = m_binnor.m_ranks[i];  // index in intermediate
0184       unsigned int k = m_ext_idcs[j];        // index in external hit_vec
0185 
0186 #ifdef COPY_SORTED_HITS
0187       memcpy(&m_hits[i], &hitv[k], sizeof(Hit));
0188 #endif
0189 
0190       if (Config::usePhiQArrays) {
0191         m_hit_phis[i] = m_hit_infos[j].phi;
0192         m_hit_qs[i] = m_hit_infos[j].q;
0193       }
0194 
0195       // Redirect m_binnor.m_ranks[i] to point to external/original index.
0196       m_binnor.m_ranks[i] = k;
0197     }
0198 
0199     if (build_original_to_internal_map) {
0200       if (m_max_ext_idx - m_min_ext_idx + 1 > 8 * m_n_hits) {
0201         // If this happens we might:
0202         // a) Use external indices for everything. -- *** We are now. ***
0203         // b) Build these maps for seeding layers only.
0204         // c) Have a flag in hit-on-track that tells us if the hit index has been remapped,
0205         //    essentially, if it is a seed hit. This might be smart anyway.
0206         //    One could use index < -256 or something similar.
0207 
0208         printf(
0209             "LayerOfHits::endRegistrationOfHits() original_to_internal index map vector is largish: m_n_hits=%d, "
0210             "map_vector_size=%d\n",
0211             m_n_hits,
0212             m_max_ext_idx - m_min_ext_idx + 1);
0213       }
0214 
0215       m_ext_idcs.resize(m_max_ext_idx - m_min_ext_idx + 1);
0216       for (unsigned int i = 0; i < m_n_hits; ++i) {
0217         m_ext_idcs[m_hit_ranks[i] - m_min_ext_idx] = i;
0218       }
0219     }
0220 
0221     // We can release m_hit_infos and, if not used, also m_ext_idcs -- and realloc them
0222     // on next beginRegistration().
0223     // If binnor had keep_cons on we could use it for pre-selection in selectHitIndices()
0224     // instead of q and phi arrays -- assuming sufficient precision can be achieved..
0225   }
0226 
0227   void LayerOfHits::printBins() {
0228     for (bin_index_t qb = 0; qb <= m_ax_eta.m_last_N_bin; ++qb) {
0229       printf("%c bin %d\n", is_barrel() ? 'Z' : 'R', qb);
0230       for (bin_index_t pb = 0; pb <= m_ax_phi.m_last_N_bin; ++pb) {
0231         if (pb % 8 == 0)
0232           printf(" Phi %4d: ", pb);
0233         auto content = m_binnor.get_content(pb, qb);
0234         printf("%5d,%4d   %s", content.first, content.count, ((pb + 1) % 8 == 0) ? "\n" : "");
0235       }
0236     }
0237   }
0238 
0239   //==============================================================================
0240   // EventOfHits
0241   //==============================================================================
0242 
0243   EventOfHits::EventOfHits(const TrackerInfo &trk_inf) : m_n_layers(trk_inf.n_layers()) {
0244     m_layers_of_hits.reserve(trk_inf.n_layers());
0245     for (int ii = 0; ii < trk_inf.n_layers(); ++ii) {
0246       m_layers_of_hits.emplace_back(LayerOfHits::Initializator(trk_inf.layer(ii)));
0247     }
0248   }
0249 
0250 }  // end namespace mkfit