Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-12 03:09:11

0001 #ifndef DataFormats_MuonDigiCollection_h
0002 #define DataFormats_MuonDigiCollection_h
0003 
0004 /**
0005  * \file
0006  * Declaration of class MuonDigiCollection
0007  *
0008  * \author Stefano ARGIRO
0009  * \date 05 Aug 2005
0010  */
0011 
0012 #include <vector>
0013 #include <map>
0014 #include <iterator>
0015 
0016 /**
0017  * \class DigiContainerIteratorAdaptor MuonDigiCollection.h "/MuonDigiCollection.h"
0018  * 
0019  * \brief An iterator adaptor for a map<Index, vector<Digi> >
0020  * that when dereferenced returns a 
0021  * pair<Index, pair<vector<Digi>::const_iterator,
0022  * vector<Digi>::const_iterator > > 
0023  * where the two iterators point to begin and and of the vector
0024  *
0025  * \author Stefano ARGIRO
0026  * \date 05 Aug 2005
0027 */
0028 
0029 template <typename IndexType, typename DigiType>
0030 class DigiContainerIterator {
0031 public:
0032   typedef typename std::vector<DigiType>::const_iterator DigiRangeIterator;
0033   typedef std::map<IndexType, std::vector<DigiType> > BaseContainer;
0034   typedef typename BaseContainer::const_iterator BaseIterator;
0035 
0036   typedef std::pair<IndexType, std::pair<DigiRangeIterator, DigiRangeIterator> > value_type;
0037   typedef value_type reference;
0038   typedef void pointer;
0039   typedef typename DigiRangeIterator::difference_type difference_type;
0040   typedef typename DigiRangeIterator::iterator_category iterator_category;
0041 
0042   DigiContainerIterator(void) {}
0043   DigiContainerIterator(BaseIterator i) : base_(i) {}
0044   // implicit copy constructor
0045   // implicit assignment operator
0046   // implicit destructor
0047 
0048   DigiContainerIterator operator++(int) { return DigiContainerIterator(base_++); }
0049 
0050   DigiContainerIterator& operator++(void) {
0051     ++base_;
0052     return *this;
0053   }
0054 
0055   bool operator==(const DigiContainerIterator& x) const { return x.base_ == base_; }
0056 
0057   bool operator!=(const DigiContainerIterator& x) const { return x.base_ != base_; }
0058 
0059   value_type operator*(void) const {
0060     return std::make_pair(base_->first, std::make_pair(base_->second.begin(), base_->second.end()));
0061   }
0062 
0063 private:
0064   BaseIterator base_;
0065 };
0066 
0067 /**
0068  * \class MuonDigiCollection MuonDigiCollection.h "/MuonDigiCollection.h"
0069  *
0070  * \brief A container for a generic type of digis indexed by 
0071  *          some index, implemented with a map<IndexType, vector<DigiType> > 
0072  *
0073  *   Example: 
0074  *
0075  *   \code
0076  *   typedef MuonDigiCollection<DTDetId,DTDigi> DTDigiCollection
0077  *   \endcode
0078  *
0079  *   \note: Requirements
0080  *   - IndexType must provide operator <    
0081  *
0082  *   \author Stefano ARGIRO
0083  *   \date 05 Aug 2005
0084  */
0085 
0086 template <typename IndexType, typename DigiType>
0087 class MuonDigiCollection {
0088 public:
0089   MuonDigiCollection() {}
0090 
0091   //  void swap(MuonDigiCollection<IndexType,DigiType> & rh) { std::swap(data_,rh.data_);}
0092   void swap(MuonDigiCollection& rh) { std::swap(data_, rh.data_); }
0093 
0094   typedef typename std::vector<DigiType>::const_iterator const_iterator;
0095   typedef typename std::pair<const_iterator, const_iterator> Range;
0096 
0097   /// insert a digi for a given DetUnit  @deprecated
0098   void insertDigi(const IndexType& index, const DigiType& digi) {
0099     std::vector<DigiType>& digis = data_[index];
0100     digis.push_back(digi);
0101   }
0102 
0103   /// insert a range of digis for a  given DetUnit
0104   void put(Range range, const IndexType& index) {
0105     std::vector<DigiType>& digis = data_[index];
0106     digis.reserve(digis.size() + (range.second - range.first));
0107     std::copy(range.first, range.second, std::back_inserter(digis));
0108   }
0109 
0110 #ifndef CMS_NOCXX11
0111   /// insert a range of digis for a  given DetUnit
0112   template <typename IRange>
0113   void move(IRange range, const IndexType& index) {
0114     std::vector<DigiType>& digis = data_[index];
0115     digis.reserve(digis.size() + (range.second - range.first));
0116     digis.insert(digis.end(), std::make_move_iterator(range.first), std::make_move_iterator(range.second));
0117   }
0118 #endif
0119 
0120   /// return the digis for a given DetUnit
0121   Range get(const IndexType& index) const {
0122     typename container::const_iterator it = data_.find(index);
0123     if (it == data_.end()) {
0124       // if data_ is empty there is no other way to get an empty range
0125       static const std::vector<DigiType> empty;
0126       return std::make_pair(empty.end(), empty.end());
0127     }
0128     const std::vector<DigiType>& digis = (*it).second;
0129     return std::make_pair(digis.begin(), digis.end());
0130   }
0131 
0132   typedef DigiContainerIterator<IndexType, DigiType> DigiRangeIterator;
0133 
0134   DigiRangeIterator begin() const { return data_.begin(); }
0135 
0136   DigiRangeIterator end() const { return data_.end(); }
0137 
0138 private:
0139   typedef std::map<IndexType, std::vector<DigiType> > container;
0140   container data_;
0141 
0142 };  // MuonDigiCollection
0143 
0144 template <typename IndexType, typename DigiType>
0145 inline void swap(MuonDigiCollection<IndexType, DigiType>& rh, MuonDigiCollection<IndexType, DigiType>& lh) {
0146   rh.swap(lh);
0147 }
0148 
0149 #endif