Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:27:48

0001 #ifndef RecoTracker_TkHitPairs_IntermediateHitDoublets_h
0002 #define RecoTracker_TkHitPairs_IntermediateHitDoublets_h
0003 
0004 #include "RecoTracker/TkHitPairs/interface/LayerHitMapCache.h"
0005 #include "TrackingTools/TransientTrackingRecHit/interface/SeedingLayerSetsHits.h"
0006 
0007 namespace ihd {
0008   /**
0009    * Class to hold TrackingRegion and begin+end indices to a vector of
0010    * seeding layer sets.
0011    *
0012    * The LayerHitMapCache is also hold here as it is a per-region object.
0013    *
0014    * In practice the vector being indexed can be anything.
0015    */
0016   class RegionIndex {
0017   public:
0018     RegionIndex(const TrackingRegion* reg, unsigned int ind)
0019         : region_(reg), layerSetBeginIndex_(ind), layerSetEndIndex_(ind) {}
0020     RegionIndex(RegionIndex&&) = default;
0021     RegionIndex& operator=(RegionIndex&&) = default;
0022 
0023     void setLayerSetsEnd(unsigned int end) { layerSetEndIndex_ = end; }
0024 
0025     const TrackingRegion& region() const { return *region_; }
0026 
0027     LayerHitMapCache& layerHitMapCache() { return cache_; }
0028     const LayerHitMapCache& layerHitMapCache() const { return cache_; }
0029 
0030     unsigned int layerSetBeginIndex() const { return layerSetBeginIndex_; }
0031     unsigned int layerSetEndIndex() const { return layerSetEndIndex_; }
0032 
0033   private:
0034     const TrackingRegion* region_;  /// pointer to TrackingRegion (owned elsewhere)
0035     LayerHitMapCache cache_;
0036     unsigned int layerSetBeginIndex_;  /// index of the beginning of layer sets of this region
0037     unsigned int layerSetEndIndex_;    /// index of the end (one-past-last) of layer sets of this region
0038   };
0039 
0040   /**
0041    * Helper class to provide nice interface to loop over the layer sets of a region
0042    *
0043    * \tparam T Concrete type in a vector<T> actually holding the layer sets
0044    *
0045    * Templatized because used here and in RegionSeedingHitSets
0046    */
0047   template <typename T>
0048   class RegionLayerSets {
0049   public:
0050     using const_iterator = typename std::vector<T>::const_iterator;
0051 
0052     // Taking T* to have compatible interface with IntermediateHitTriplets::RegionLayerSets
0053     template <typename TMP>
0054     RegionLayerSets(const TrackingRegion* region,
0055                     const LayerHitMapCache* cache,
0056                     const TMP*,
0057                     const_iterator begin,
0058                     const_iterator end)
0059         : region_(region), cache_(cache), layerSetsBegin_(begin), layerSetsEnd_(end) {}
0060 
0061     const TrackingRegion& region() const { return *region_; }
0062     const LayerHitMapCache& layerHitMapCache() const { return *cache_; }
0063 
0064     const_iterator begin() const { return layerSetsBegin_; }
0065     const_iterator cbegin() const { return begin(); }
0066     const_iterator end() const { return layerSetsEnd_; }
0067     const_iterator cend() const { return end(); }
0068 
0069   private:
0070     const TrackingRegion* region_;
0071     const LayerHitMapCache* cache_;
0072     const const_iterator layerSetsBegin_;
0073     const const_iterator layerSetsEnd_;
0074   };
0075 
0076   /**
0077    * Helper class providing a generic iterator to loop over
0078    * TrackingRegions of IntermediateHitDoublets,
0079    * IntermediateHitTriplets, or RegionsSeedingHitSets
0080    *
0081    * \tparam ValueType   Type to be returned by operator*() (should be something inexpensive)
0082    * \tparam HitSetType  Type of the holder of data (currently IntermediateHitDoublets, IntermediateHitTriplets, or RegionsSeedingHitSets)
0083    */
0084   template <typename ValueType, typename HitSetType>
0085   class const_iterator {
0086   public:
0087     using internal_iterator_type = typename std::vector<RegionIndex>::const_iterator;
0088     using value_type = ValueType;
0089     using difference_type = internal_iterator_type::difference_type;
0090 
0091     const_iterator(const HitSetType* hst, internal_iterator_type iter) : hitSets_(hst), iter_(iter) {}
0092 
0093     value_type operator*() const {
0094       return value_type(&(iter_->region()),
0095                         &(iter_->layerHitMapCache()),
0096                         hitSets_,
0097                         hitSets_->layerSetsBegin() + iter_->layerSetBeginIndex(),
0098                         hitSets_->layerSetsBegin() + iter_->layerSetEndIndex());
0099     }
0100 
0101     const_iterator& operator++() {
0102       ++iter_;
0103       return *this;
0104     }
0105     const_iterator operator++(int) {
0106       const_iterator clone(*this);
0107       ++iter_;
0108       return clone;
0109     }
0110 
0111     bool operator==(const const_iterator& other) const { return iter_ == other.iter_; }
0112     bool operator!=(const const_iterator& other) const { return !operator==(other); }
0113 
0114   private:
0115     const HitSetType* hitSets_;
0116     internal_iterator_type iter_;
0117   };
0118 }  // namespace ihd
0119 
0120 /**
0121  * Container of temporary information delivered from hit pair
0122  * generator to hit triplet generator via edm::Event.
0123  *
0124  * The iterator loops over regions, and the value_type of that has an
0125  * iterator looping over the layer pairs of the region.
0126  *
0127  * Pointers to SeedingLayerSetsHits and TrackingRegion are stored, so
0128  * the lifetime of those objects should be at least as long as the
0129  * lifetime of this object.
0130  */
0131 class IntermediateHitDoublets {
0132 public:
0133   using LayerPair = std::tuple<SeedingLayerSetsHits::LayerIndex, SeedingLayerSetsHits::LayerIndex>;
0134   using RegionIndex = ihd::RegionIndex;
0135 
0136   /**
0137    * This class stores the indices of a layer pair, and the doublets
0138    * generated from there.
0139    *
0140    * The layer indices are those from SeedingLayerSetsHits.
0141    *
0142    * Since the doublets are stored directly here, the same class works
0143    * nicely for both storage and use.
0144    */
0145   class LayerPairHitDoublets {
0146   public:
0147     LayerPairHitDoublets(const SeedingLayerSetsHits::SeedingLayerSet& layerSet, HitDoublets&& doublets)
0148         : layerPair_(layerSet[0].index(), layerSet[1].index()), doublets_(std::move(doublets)) {}
0149 
0150     const LayerPair& layerPair() const { return layerPair_; }
0151     SeedingLayerSetsHits::LayerIndex innerLayerIndex() const { return std::get<0>(layerPair_); }
0152     SeedingLayerSetsHits::LayerIndex outerLayerIndex() const { return std::get<1>(layerPair_); }
0153 
0154     const HitDoublets& doublets() const { return doublets_; }
0155 
0156   private:
0157     LayerPair layerPair_;   /// pair of indices to the layer
0158     HitDoublets doublets_;  /// container of the doublets
0159   };
0160 
0161   ////////////////////
0162 
0163   /// Helper class providing nice interface to loop over layer sets of a region
0164   using RegionLayerSets = ihd::RegionLayerSets<LayerPairHitDoublets>;
0165 
0166   ////////////////////
0167 
0168   /// Iterator over regions
0169   using const_iterator = ihd::const_iterator<RegionLayerSets, IntermediateHitDoublets>;
0170 
0171   ////////////////////
0172 
0173   /// Helper class enforcing correct way of filling the doublets of a region
0174   class RegionFiller {
0175   public:
0176     RegionFiller() : obj_(nullptr) {}
0177     explicit RegionFiller(IntermediateHitDoublets* obj) : obj_(obj) {}
0178 
0179     ~RegionFiller() = default;
0180 
0181     bool valid() const { return obj_ != nullptr; }
0182 
0183     LayerHitMapCache& layerHitMapCache() { return obj_->regions_.back().layerHitMapCache(); }
0184 
0185     void addDoublets(const SeedingLayerSetsHits::SeedingLayerSet& layerSet, HitDoublets&& doublets) {
0186       obj_->layerPairs_.emplace_back(layerSet, std::move(doublets));
0187       obj_->regions_.back().setLayerSetsEnd(obj_->layerPairs_.size());
0188     }
0189 
0190   private:
0191     IntermediateHitDoublets* obj_;
0192   };
0193 
0194   // allows declaring local variables with auto
0195   static RegionFiller dummyFiller() { return RegionFiller(); }
0196 
0197   ////////////////////
0198 
0199   IntermediateHitDoublets() : seedingLayers_(nullptr) {}
0200   explicit IntermediateHitDoublets(const SeedingLayerSetsHits* seedingLayers) : seedingLayers_(seedingLayers) {}
0201   IntermediateHitDoublets(const IntermediateHitDoublets& rh);  // only to make ROOT dictionary generation happy
0202   IntermediateHitDoublets(IntermediateHitDoublets&&) = default;
0203   IntermediateHitDoublets& operator=(IntermediateHitDoublets&&) = default;
0204   ~IntermediateHitDoublets() = default;
0205 
0206   void reserve(size_t nregions, size_t nlayersets) {
0207     regions_.reserve(nregions);
0208     layerPairs_.reserve(nregions * nlayersets);
0209   }
0210 
0211   void shrink_to_fit() {
0212     regions_.shrink_to_fit();
0213     layerPairs_.shrink_to_fit();
0214   }
0215 
0216   RegionFiller beginRegion(const TrackingRegion* region) {
0217     regions_.emplace_back(region, layerPairs_.size());
0218     return RegionFiller(this);
0219   }
0220 
0221   const SeedingLayerSetsHits& seedingLayerHits() const { return *seedingLayers_; }
0222   bool empty() const { return regions_.empty(); }
0223   size_t regionSize() const { return regions_.size(); }
0224   size_t layerPairsSize() const { return layerPairs_.size(); }
0225 
0226   const_iterator begin() const { return const_iterator(this, regions_.begin()); }
0227   const_iterator cbegin() const { return begin(); }
0228   const_iterator end() const { return const_iterator(this, regions_.end()); }
0229   const_iterator cend() const { return end(); }
0230 
0231   // used internally by all the helper classes
0232   std::vector<RegionIndex>::const_iterator regionsBegin() const { return regions_.begin(); }
0233   std::vector<RegionIndex>::const_iterator regionsEnd() const { return regions_.end(); }
0234   std::vector<LayerPairHitDoublets>::const_iterator layerSetsBegin() const { return layerPairs_.begin(); }
0235   std::vector<LayerPairHitDoublets>::const_iterator layerSetsEnd() const { return layerPairs_.end(); }
0236 
0237 private:
0238   const SeedingLayerSetsHits* seedingLayers_;  /// Pointer to SeedingLayerSetsHits (owned elsewhere)
0239 
0240   std::vector<RegionIndex> regions_;  /// Container of regions, each element has indices pointing to layerPairs_
0241   std::vector<LayerPairHitDoublets> layerPairs_;  /// Container of layer pairs and doublets for all regions
0242 };
0243 
0244 #endif