Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef TrackingTools_TransientTrackingRecHit_SeedingLayerSetsHits
0002 #define TrackingTools_TransientTrackingRecHit_SeedingLayerSetsHits
0003 
0004 #include "DataFormats/TrackerRecHit2D/interface/BaseTrackerRecHit.h"
0005 #include "DataFormats/TrackingRecHit/interface/mayown_ptr.h"
0006 
0007 #include <vector>
0008 #include <string>
0009 #include <utility>
0010 
0011 class DetLayer;
0012 
0013 /**
0014  * Class to store TransientTrackingRecHits, names, and DetLayer
0015  * pointers of each seeding layer. It is required that all contained
0016  * SeedingLayerSets have the same number of layers in each set.
0017  */
0018 class SeedingLayerSetsHits {
0019 public:
0020   using TkHit = BaseTrackerRecHit;
0021   using TkHitRef = BaseTrackerRecHit const &;
0022   using HitPointer = mayown_ptr<BaseTrackerRecHit>;
0023   using OwnedHits = std::vector<HitPointer>;
0024 
0025   using ConstRecHitPointer = BaseTrackerRecHit const *;
0026   using Hits = std::vector<ConstRecHitPointer>;
0027 
0028   typedef unsigned short LayerSetIndex;
0029   typedef unsigned short LayerIndex;
0030   typedef unsigned int HitIndex;
0031 
0032   /**
0033    * Auxiliary class to represent a single SeedingLayer. Holds a
0034    * pointer to SeedingLayerSetsHits and the index of the
0035    * SeedingLayer. All calls are forwarded to SeedingLayerSetsHits.
0036    */
0037   class SeedingLayer {
0038   public:
0039     SeedingLayer() : seedingLayerSets_(nullptr), index_(0) {}
0040     SeedingLayer(const SeedingLayerSetsHits *sls, LayerIndex index) : seedingLayerSets_(sls), index_(index) {}
0041 
0042     /**
0043      * Index of the SeedingLayer within SeedingLayerSetsHits.
0044      *
0045      * The index is unique within a SeedingLayerSetsHits object, and
0046      * is the same for all SeedingLayers with the same name.
0047      */
0048     LayerIndex index() const { return index_; }
0049     const std::string &name() const { return (*seedingLayerSets_->layerNames_)[index_]; }
0050     const DetLayer *detLayer() const { return (*seedingLayerSets_->layerDets_)[index_]; }
0051     Hits hits() const { return seedingLayerSets_->hits(index_); }
0052 
0053   private:
0054     const SeedingLayerSetsHits *seedingLayerSets_;
0055     LayerIndex index_;
0056   };
0057 
0058   /**
0059    * Auxiliary class to represent a set of SeedingLayers (e.g. BPIX1+BPIX2+BPIX3).
0060    *
0061    * Holds a pointer to SeedingLayerSetsHits, and iterators to
0062    * SeedingLayerSetsHits::layerSetIndices_ to for the first and last+1
0063    * layer of the set.
0064    */
0065   class SeedingLayerSet {
0066   public:
0067     class const_iterator {
0068     public:
0069       typedef std::vector<LayerSetIndex>::const_iterator internal_iterator_type;
0070       typedef SeedingLayer value_type;
0071       typedef internal_iterator_type::difference_type difference_type;
0072 
0073       const_iterator() : seedingLayerSets_(nullptr) {}
0074       const_iterator(const SeedingLayerSetsHits *sls, internal_iterator_type iter)
0075           : seedingLayerSets_(sls), iter_(iter) {}
0076 
0077       value_type operator*() const { return SeedingLayer(seedingLayerSets_, *iter_); }
0078 
0079       const_iterator &operator++() {
0080         ++iter_;
0081         return *this;
0082       }
0083       const_iterator operator++(int) {
0084         const_iterator clone(*this);
0085         ++clone;
0086         return clone;
0087       }
0088 
0089       bool operator==(const const_iterator &other) const { return iter_ == other.iter_; }
0090       bool operator!=(const const_iterator &other) const { return !operator==(other); }
0091 
0092     private:
0093       const SeedingLayerSetsHits *seedingLayerSets_;
0094       internal_iterator_type iter_;
0095     };
0096 
0097     SeedingLayerSet() : seedingLayerSets_(nullptr) {}
0098     SeedingLayerSet(const SeedingLayerSetsHits *sls,
0099                     std::vector<LayerSetIndex>::const_iterator begin,
0100                     std::vector<LayerSetIndex>::const_iterator end)
0101         : seedingLayerSets_(sls), begin_(begin), end_(end) {}
0102 
0103     /// Number of layers in this set
0104     LayerSetIndex size() const { return end_ - begin_; }
0105 
0106     /**
0107      * Slices the layer set
0108      *
0109      * E.g. slicing BPix1+BPix2+BPix3+BPix4 with (0,2) will give
0110      * BPix1+BPix2.
0111      *
0112      * \param begin  Index for the first layer of the slice
0113      * \param end    Index for the one-beyond-last layer of the slice
0114      *
0115      * It is caller's responsibility to guarantee that "begin <
0116      * size()" and "0 < end <= size()" and "begin < end".
0117      */
0118     SeedingLayerSet slice(size_t begin, size_t end) const {
0119       assert(begin < size());
0120       assert(0 < end && end <= size());
0121       assert(begin < end);
0122       return SeedingLayerSet(seedingLayerSets_, begin_ + begin, begin_ + end);
0123     }
0124 
0125     /// Get a given SeedingLayer (index is between 0 and size()-1)
0126     SeedingLayer operator[](LayerSetIndex index) const { return SeedingLayer(seedingLayerSets_, *(begin_ + index)); }
0127 
0128     // iterators for range-for
0129     const_iterator begin() const { return const_iterator(seedingLayerSets_, begin_); }
0130     const_iterator cbegin() const { return begin(); }
0131     const_iterator end() const { return const_iterator(seedingLayerSets_, end_); }
0132     const_iterator cend() const { return end(); }
0133 
0134   private:
0135     const SeedingLayerSetsHits *seedingLayerSets_;
0136     std::vector<LayerSetIndex>::const_iterator begin_;  // Iterator to SeedingLayerSetsHits::layerSetIndices_, first layer
0137     std::vector<LayerSetIndex>::const_iterator end_;  // Iterator to SeedingLayerSetsHits::layerSetIndices_, last+1 layer
0138   };
0139 
0140   class const_iterator {
0141   public:
0142     typedef std::vector<LayerSetIndex>::const_iterator internal_iterator_type;
0143     typedef SeedingLayerSet value_type;
0144     typedef internal_iterator_type::difference_type difference_type;
0145 
0146     const_iterator() : seedingLayerSets_(nullptr) {}
0147     const_iterator(const SeedingLayerSetsHits *sls, internal_iterator_type iter)
0148         : seedingLayerSets_(sls), iter_(iter) {}
0149 
0150     value_type operator*() const {
0151       return SeedingLayerSet(seedingLayerSets_, iter_, iter_ + seedingLayerSets_->nlayers_);
0152     }
0153 
0154     const_iterator &operator++() {
0155       std::advance(iter_, seedingLayerSets_->nlayers_);
0156       return *this;
0157     }
0158     const_iterator operator++(int) {
0159       const_iterator clone(*this);
0160       ++(*this);
0161       return clone;
0162     }
0163 
0164     bool operator==(const const_iterator &other) const { return iter_ == other.iter_; }
0165     bool operator!=(const const_iterator &other) const { return !operator==(other); }
0166 
0167   private:
0168     const SeedingLayerSetsHits *seedingLayerSets_;
0169     internal_iterator_type iter_;
0170   };
0171 
0172   SeedingLayerSetsHits() = default;
0173 
0174   /**
0175    * Constructor.
0176    *
0177    * \param nlayers         Number of layers in each SeedingLayerSet
0178    * \param layerSetIndices Pointer to a vector holding the indices of layer sets (pointer to vector is stored)
0179    * \param layerNames      Pointer to a vector holding the layer names (pointer to vector is stored)
0180    * \param layerDets       Pointer to a vector of pointers to layer DetLayer objects (pointer to vector is stored)
0181    */
0182   SeedingLayerSetsHits(unsigned short nlayers,
0183                        const std::vector<LayerSetIndex> *layerSetIndices,
0184                        const std::vector<std::string> *layerNames,
0185                        const std::vector<const DetLayer *> *layerDets);
0186 
0187   ~SeedingLayerSetsHits() = default;
0188   SeedingLayerSetsHits(SeedingLayerSetsHits const &) = delete;
0189   SeedingLayerSetsHits &operator=(SeedingLayerSetsHits const &) = delete;
0190   SeedingLayerSetsHits(SeedingLayerSetsHits &&) = default;
0191   SeedingLayerSetsHits &operator=(SeedingLayerSetsHits &&) = default;
0192 
0193   void shrink_to_fit();
0194   void addHits(LayerIndex layerIndex, OwnedHits &&hits);
0195 
0196   /// Get number of layers in each SeedingLayerSets
0197   unsigned short numberOfLayersInSet() const { return nlayers_; }
0198   /// Get the number of SeedingLayerSets
0199   unsigned short size() const { return nlayers_ > 0 ? layerSetIndices_->size() / nlayers_ : 0; }
0200 
0201   /// Get the SeedingLayerSet at a given index
0202   SeedingLayerSet operator[](LayerSetIndex index) const {
0203     std::vector<LayerSetIndex>::const_iterator begin = layerSetIndices_->begin() + nlayers_ * index;
0204     std::vector<LayerSetIndex>::const_iterator end = begin + nlayers_;
0205     return SeedingLayerSet(this, begin, end);
0206   }
0207 
0208   // iterators for range-for
0209   const_iterator begin() const { return const_iterator(this, layerSetIndices_->begin()); }
0210   const_iterator cbegin() const { return begin(); }
0211   const_iterator end() const { return const_iterator(this, layerSetIndices_->end()); }
0212   const_iterator cend() const { return end(); }
0213 
0214   // for more efficient edm::Event::put()
0215   void swap(SeedingLayerSetsHits &other) {
0216     std::swap(nlayers_, other.nlayers_);
0217     std::swap(layerSetIndices_, other.layerSetIndices_);
0218     layerHitIndices_.swap(other.layerHitIndices_);
0219     std::swap(layerNames_, other.layerNames_);
0220     std::swap(layerDets_, other.layerDets_);
0221     rechits_.swap(other.rechits_);
0222   }
0223 
0224   void print() const;
0225 
0226 private:
0227   Hits hits(LayerIndex layerIndex) const;
0228 
0229   /// Number of layers in a SeedingLayerSet
0230   unsigned short nlayers_ = 0;
0231 
0232   /**
0233    * Stores SeedingLayerSets as nlayers_ consecutive layer indices.
0234    * Layer indices point to layerHitRanges_, layerNames_, and
0235    * layerDets_. Hence layerSetIndices.size() == nlayers_*"number of layer sets"
0236    */
0237   const std::vector<LayerSetIndex> *layerSetIndices_ = nullptr;
0238 
0239   // following are indexed by LayerIndex
0240   std::vector<HitIndex> layerHitIndices_;                     // Indices to first hits in rechits_
0241   const std::vector<std::string> *layerNames_ = nullptr;      // Names of the layers
0242   const std::vector<const DetLayer *> *layerDets_ = nullptr;  // Pointers to corresponding DetLayer objects
0243 
0244   /**
0245    * List of RecHits of all SeedingLayers. Hits of each layer are
0246    * identified by the begin indices in layerHitIndices_.
0247    */
0248   OwnedHits rechits_;
0249 };
0250 
0251 #endif