Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:28:31

0001 #ifndef RecoTracker_PixelSeeding_IntermediateHitTriplets_h
0002 #define RecoTracker_PixelSeeding_IntermediateHitTriplets_h
0003 
0004 #include "RecoTracker/TkHitPairs/interface/LayerHitMapCache.h"
0005 #include "RecoTracker/TkHitPairs/interface/IntermediateHitDoublets.h"
0006 #include "TrackingTools/TransientTrackingRecHit/interface/SeedingLayerSetsHits.h"
0007 #include "RecoTracker/PixelSeeding/interface/OrderedHitTriplets.h"
0008 
0009 /**
0010  * Container of temporary information delivered from hit triplet
0011  * generator to hit quadruplet generator via edm::Event.
0012  *
0013  * The iterator loops over regions, the value_type of that has an
0014  * iterator looping over the layer triplets of the region, and the
0015  * value_type of that has an iterator looping over the hit triplets of
0016  * the layer triplet.
0017  *
0018  * Pointers to SeedingLayerSetsHits and TrackingRegion are stored, so
0019  * the lifetime of those objects should be at least as long as the
0020  * lifetime of this object.
0021  */
0022 class IntermediateHitTriplets {
0023 public:
0024   using LayerPair = std::tuple<SeedingLayerSetsHits::LayerIndex, SeedingLayerSetsHits::LayerIndex>;
0025   using LayerTriplet =
0026       std::tuple<SeedingLayerSetsHits::LayerIndex, SeedingLayerSetsHits::LayerIndex, SeedingLayerSetsHits::LayerIndex>;
0027   using RegionIndex = ihd::RegionIndex;
0028 
0029   ////////////////////
0030 
0031   /**
0032    * Helper class holding the layer triplet indices (to
0033    * SeedingLayerSetsHits), and indices of the hit triplets from this
0034    * layer triplet (to the hit triplet vector)
0035    *
0036    * As only the indices of hit triplets are stored, a separate class
0037    * (LayerHitTriplets) is provided with nicer interface.
0038    */
0039   class PLayerHitTriplets {
0040   public:
0041     PLayerHitTriplets(const LayerTriplet &layerTriplet, unsigned int tripletsBegin)
0042         : layerTriplet_(layerTriplet), tripletsBegin_(tripletsBegin), tripletsEnd_(tripletsBegin) {}
0043 
0044     void setTripletsEnd(unsigned int end) { tripletsEnd_ = end; }
0045 
0046     const LayerTriplet &layerTriplet() const { return layerTriplet_; }
0047 
0048     unsigned int tripletsBegin() const { return tripletsBegin_; }
0049     unsigned int tripletsEnd() const { return tripletsEnd_; }
0050 
0051   private:
0052     LayerTriplet layerTriplet_;
0053     unsigned int tripletsBegin_;
0054     unsigned int tripletsEnd_;
0055   };
0056 
0057   ////////////////////
0058 
0059   /**
0060    * Helper class providing a nice interface for the hit triplets of a
0061    * layer triplet.
0062    */
0063   class LayerHitTriplets {
0064   public:
0065     using const_iterator = std::vector<OrderedHitTriplet>::const_iterator;
0066 
0067     LayerHitTriplets(const IntermediateHitTriplets *hitSets, const PLayerHitTriplets *layerTriplet)
0068         : hitSets_(hitSets), layerTriplet_(layerTriplet) {}
0069 
0070     using TripletRange =
0071         std::pair<std::vector<OrderedHitTriplet>::const_iterator, std::vector<OrderedHitTriplet>::const_iterator>;
0072 
0073     SeedingLayerSetsHits::LayerIndex innerLayerIndex() const { return std::get<0>(layerTriplet_->layerTriplet()); }
0074     SeedingLayerSetsHits::LayerIndex middleLayerIndex() const { return std::get<1>(layerTriplet_->layerTriplet()); }
0075     SeedingLayerSetsHits::LayerIndex outerLayerIndex() const { return std::get<2>(layerTriplet_->layerTriplet()); }
0076 
0077     const_iterator begin() const { return hitSets_->tripletsBegin() + layerTriplet_->tripletsBegin(); }
0078     const_iterator cbegin() const { return begin(); }
0079     const_iterator end() const { return hitSets_->tripletsBegin() + layerTriplet_->tripletsEnd(); }
0080     const_iterator cend() const { return end(); }
0081 
0082   private:
0083     const IntermediateHitTriplets *hitSets_;
0084     const PLayerHitTriplets *layerTriplet_;
0085   };
0086 
0087   ////////////////////
0088 
0089   /**
0090    * Helper class to provide nice interface to loop over the layer sets of a region
0091    *
0092    * The value_type of the iterator is LayerHitTriplets, which has an
0093    * iterator for the hit triplets.
0094    *
0095    * Can not use ihd::RegionLayerSets<T> here because of having
0096    * separate classes for storage (PLayerHitTriplets) and use
0097    * (LayerHitTriplets).
0098    */
0099   class RegionLayerSets {
0100   public:
0101     using PLayerHitTripletsConstIterator = std::vector<PLayerHitTriplets>::const_iterator;
0102     using TripletConstIterator = std::vector<OrderedHitTriplet>::const_iterator;
0103 
0104     class const_iterator {
0105     public:
0106       using internal_iterator_type = PLayerHitTripletsConstIterator;
0107       using value_type = LayerHitTriplets;
0108       using difference_type = internal_iterator_type::difference_type;
0109 
0110       struct end_tag {};
0111 
0112       /**
0113        * Constructor for an iterator pointing to a valid element
0114        */
0115       const_iterator(const IntermediateHitTriplets *hitSets, const RegionLayerSets *regionLayerSets)
0116           : hitSets_(hitSets), regionLayerSets_(regionLayerSets), iter_(regionLayerSets->layerSetsBegin()) {
0117         assert(regionLayerSets->layerSetsBegin() != regionLayerSets->layerSetsEnd());
0118       }
0119 
0120       /**
0121        * Constructor for an iterator pointing to an invalid element (i.e. end)
0122        *
0123        * The end_tag parameter is used to differentiate this constructor from the other one.
0124        */
0125       const_iterator(const IntermediateHitTriplets *hitSets, const RegionLayerSets *regionLayerSets, end_tag)
0126           : iter_(regionLayerSets->layerSetsEnd()) {}
0127 
0128       value_type operator*() const { return value_type(hitSets_, &(*iter_)); }
0129 
0130       const_iterator &operator++() {
0131         ++iter_;
0132         return *this;
0133       }
0134 
0135       const_iterator operator++(int) {
0136         const_iterator clone(*this);
0137         operator++();
0138         return clone;
0139       }
0140 
0141       bool operator==(const const_iterator &other) const { return iter_ == other.iter_; }
0142       bool operator!=(const const_iterator &other) const { return !operator==(other); }
0143 
0144     private:
0145       const IntermediateHitTriplets *hitSets_;
0146       const RegionLayerSets *regionLayerSets_;
0147       internal_iterator_type iter_;
0148     };
0149 
0150     RegionLayerSets(const TrackingRegion *region,
0151                     const LayerHitMapCache *cache,
0152                     const IntermediateHitTriplets *hitSets,
0153                     PLayerHitTripletsConstIterator tripletBegin,
0154                     PLayerHitTripletsConstIterator tripletEnd)
0155         : region_(region), cache_(cache), hitSets_(hitSets), layerSetsBegin_(tripletBegin), layerSetsEnd_(tripletEnd) {}
0156 
0157     const TrackingRegion &region() const { return *region_; }
0158     const LayerHitMapCache &layerHitMapCache() const { return *cache_; }
0159     size_t layerTripletsSize() const { return std::distance(layerSetsBegin_, layerSetsEnd_); }
0160 
0161     const_iterator begin() const {
0162       if (layerSetsBegin_ != layerSetsEnd_)
0163         return const_iterator(hitSets_, this);
0164       else
0165         return end();
0166     }
0167     const_iterator cbegin() const { return begin(); }
0168     const_iterator end() const { return const_iterator(hitSets_, this, const_iterator::end_tag()); }
0169     const_iterator cend() const { return end(); }
0170 
0171     // used internally by the LayerHitTriplets helper class
0172     PLayerHitTripletsConstIterator layerSetsBegin() const { return layerSetsBegin_; }
0173     PLayerHitTripletsConstIterator layerSetsEnd() const { return layerSetsEnd_; }
0174 
0175   private:
0176     const TrackingRegion *region_ = nullptr;
0177     const LayerHitMapCache *cache_ = nullptr;
0178     const IntermediateHitTriplets *hitSets_ = nullptr;
0179     const PLayerHitTripletsConstIterator layerSetsBegin_;
0180     const PLayerHitTripletsConstIterator layerSetsEnd_;
0181   };
0182 
0183   ////////////////////
0184 
0185   /// Iterator over regions
0186   using const_iterator = ihd::const_iterator<RegionLayerSets, IntermediateHitTriplets>;
0187 
0188   ////////////////////
0189 
0190   /// Helper class enforcing correct way of filling the doublets of a region
0191   class RegionFiller {
0192   public:
0193     RegionFiller() : obj_(nullptr) {}
0194     explicit RegionFiller(IntermediateHitTriplets *obj) : obj_(obj) {}
0195 
0196     ~RegionFiller() = default;
0197 
0198     bool valid() const { return obj_ != nullptr; }
0199 
0200     LayerHitMapCache &layerHitMapCache() { return obj_->regions_.back().layerHitMapCache(); }
0201 
0202     void addTriplets(const LayerPair &layerPair,
0203                      const std::vector<SeedingLayerSetsHits::SeedingLayer> &thirdLayers,
0204                      const OrderedHitTriplets &triplets,
0205                      const std::vector<int> &thirdLayerIndex,
0206                      const std::vector<size_t> &permutations) {
0207       assert(triplets.size() == thirdLayerIndex.size());
0208       assert(triplets.size() == permutations.size());
0209 
0210       if (triplets.empty()) {
0211         return;
0212       }
0213 
0214       int prevLayer = -1;
0215       for (size_t i = 0, size = permutations.size(); i < size; ++i) {
0216         // We go through the 'triplets' in the order defined by
0217         // 'permutations', which is sorted such that we first go through
0218         // triplets from (3rd) layer 0, then layer 1 and so on.
0219         const size_t realIndex = permutations[i];
0220 
0221         const int layer = thirdLayerIndex[realIndex];
0222         if (layer != prevLayer) {
0223           prevLayer = layer;
0224           obj_->layerTriplets_.emplace_back(
0225               LayerTriplet(std::get<0>(layerPair), std::get<1>(layerPair), thirdLayers[layer].index()),
0226               obj_->hitTriplets_.size());
0227         }
0228 
0229         obj_->hitTriplets_.emplace_back(triplets[realIndex]);
0230         obj_->layerTriplets_.back().setTripletsEnd(obj_->hitTriplets_.size());
0231       }
0232 
0233       obj_->regions_.back().setLayerSetsEnd(obj_->layerTriplets_.size());
0234     }
0235 
0236   private:
0237     IntermediateHitTriplets *obj_;
0238   };
0239 
0240   // allows declaring local variables with auto
0241   static RegionFiller dummyFiller() { return RegionFiller(); }
0242 
0243   ////////////////////
0244 
0245   IntermediateHitTriplets() : seedingLayers_(nullptr) {}
0246   explicit IntermediateHitTriplets(const SeedingLayerSetsHits *seedingLayers) : seedingLayers_(seedingLayers) {}
0247   IntermediateHitTriplets(const IntermediateHitTriplets &rh);  // only to make ROOT dictionary generation happy
0248   IntermediateHitTriplets(IntermediateHitTriplets &&) = default;
0249   IntermediateHitTriplets &operator=(IntermediateHitTriplets &&) = default;
0250   ~IntermediateHitTriplets() = default;
0251 
0252   void reserve(size_t nregions, size_t nlayersets, size_t ntriplets) {
0253     regions_.reserve(nregions);
0254     layerTriplets_.reserve(nregions * nlayersets);
0255     hitTriplets_.reserve(ntriplets);
0256   }
0257 
0258   void shrink_to_fit() {
0259     regions_.shrink_to_fit();
0260     layerTriplets_.shrink_to_fit();
0261     hitTriplets_.shrink_to_fit();
0262   }
0263 
0264   RegionFiller beginRegion(const TrackingRegion *region) {
0265     regions_.emplace_back(region, layerTriplets_.size());
0266     return RegionFiller(this);
0267   }
0268 
0269   const SeedingLayerSetsHits &seedingLayerHits() const { return *seedingLayers_; }
0270   bool empty() const { return regions_.empty(); }
0271   size_t regionSize() const { return regions_.size(); }
0272   size_t tripletsSize() const { return hitTriplets_.size(); }
0273 
0274   const_iterator begin() const { return const_iterator(this, regions_.begin()); }
0275   const_iterator cbegin() const { return begin(); }
0276   const_iterator end() const { return const_iterator(this, regions_.end()); }
0277   const_iterator cend() const { return end(); }
0278 
0279   // used internally by all the helper classes
0280   std::vector<RegionIndex>::const_iterator regionsBegin() const { return regions_.begin(); }
0281   std::vector<RegionIndex>::const_iterator regionsEnd() const { return regions_.end(); }
0282   std::vector<PLayerHitTriplets>::const_iterator layerSetsBegin() const { return layerTriplets_.begin(); }
0283   std::vector<PLayerHitTriplets>::const_iterator layerSetsEnd() const { return layerTriplets_.end(); }
0284   std::vector<OrderedHitTriplet>::const_iterator tripletsBegin() const { return hitTriplets_.begin(); }
0285   std::vector<OrderedHitTriplet>::const_iterator tripletsEnd() const { return hitTriplets_.end(); }
0286 
0287 private:
0288   const SeedingLayerSetsHits *seedingLayers_;  /// Pointer to SeedingLayerSetsHits (owned elsewhere)
0289 
0290   std::vector<RegionIndex> regions_;  /// Container of regions, each element has indices pointing to layerTriplets_
0291   std::vector<PLayerHitTriplets>
0292       layerTriplets_;  /// Container of layer triplets, each element has indices pointing to hitTriplets_
0293   std::vector<OrderedHitTriplet> hitTriplets_;  /// Container of hit triplets for all layer triplets and regions
0294 };
0295 
0296 #endif