Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-10 01:50:17

0001 #ifndef DataFormats_L1Trigger_RegionalOutput_h
0002 #define DataFormats_L1Trigger_RegionalOutput_h
0003 
0004 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
0005 #include "DataFormats/Common/interface/RefProd.h"
0006 #include "DataFormats/Common/interface/Ref.h"
0007 #include <vector>
0008 
0009 namespace l1t {
0010   template <typename T>
0011   class RegionalOutput {
0012   public:
0013     typedef typename T::value_type value_type;
0014     typedef edm::Ref<T> ref;
0015     typedef edm::RefProd<T> refprod;
0016 
0017     class iterator {
0018     public:
0019       typedef typename T::value_type value_type;
0020       typedef ptrdiff_t difference_type;
0021       iterator(const RegionalOutput<T>& src, unsigned int idx) : src_(&src), idx_(idx) {}
0022       iterator(iterator const& it) : src_(it.src_), idx_(it.idx_) {}
0023       iterator() : src_(nullptr), idx_(0) {}
0024       iterator& operator++() {
0025         ++idx_;
0026         return *this;
0027       }
0028       iterator operator++(int) {
0029         iterator ci = *this;
0030         ++idx_;
0031         return ci;
0032       }
0033       iterator& operator--() {
0034         --idx_;
0035         return *this;
0036       }
0037       iterator operator--(int) {
0038         iterator ci = *this;
0039         --idx_;
0040         return ci;
0041       }
0042       difference_type operator-(iterator const& o) const { return idx_ - o.idx_; }
0043       iterator operator+(difference_type n) const { return iterator(src_, idx_ + n); }
0044       iterator operator-(difference_type n) const { return iterator(src_, idx_ - n); }
0045       bool operator<(iterator const& o) const { return idx_ < o.idx_; }
0046       bool operator==(iterator const& ci) const { return idx_ == ci.idx_; }
0047       bool operator!=(iterator const& ci) const { return idx_ != ci.idx_; }
0048       value_type const& operator*() const { return src_->objAt(idx_); }
0049       value_type const* operator->() const { return &src_->objAt(idx_); }
0050       iterator& operator+=(difference_type d) {
0051         idx_ += d;
0052         return *this;
0053       }
0054       iterator& operator-=(difference_type d) {
0055         idx_ -= d;
0056         return *this;
0057       }
0058       value_type const& operator[](difference_type d) const { return src_->objAt(idx_ + d); }
0059       // interface to get EDM refs & related stuff
0060       edm::Ref<T> ref() const { return src_->refAt(idx_); }
0061       edm::ProductID id() const { return src_->id(); }
0062       unsigned int idx() const { return idx_; }
0063       unsigned int key() const { return idx_; }
0064 
0065     private:
0066       const RegionalOutput<T>* src_;
0067       unsigned int idx_;
0068     };
0069     typedef iterator const_iterator;
0070 
0071     class Region {
0072     public:
0073       typedef typename T::value_type value_type;
0074       typedef typename RegionalOutput<T>::iterator iterator;
0075       typedef typename RegionalOutput<T>::const_iterator const_iterator;
0076 
0077       const value_type& operator[](unsigned int idx) const { return src_->objAt(ibegin_ + idx); }
0078       const value_type& front() const { return src_->objAt(ibegin_); }
0079       const value_type& back() const { return src_->objAt(iend_ - 1); }
0080       iterator begin() const { return iterator(*src_, ibegin_); }
0081       iterator end() const { return iterator(*src_, iend_); }
0082       unsigned int size() const { return iend_ - ibegin_; }
0083       bool empty() const { return (iend_ == ibegin_); }
0084       // interface to get EDM refs & related stuff
0085       ref refAt(unsigned int idx) const { return src_->refAt(ibegin_ + idx); }
0086       edm::ProductID id() const { return src_->id(); }
0087 
0088     private:
0089       const RegionalOutput<T>* src_;
0090       unsigned int ibegin_, iend_;
0091       friend class RegionalOutput<T>;
0092       Region(const RegionalOutput<T>* src, unsigned int ibegin, unsigned int iend)
0093           : src_(src), ibegin_(ibegin), iend_(iend) {}
0094     };
0095 
0096     RegionalOutput() : refprod_(), values_(), regions_(), etas_(), phis_() {}
0097     RegionalOutput(const edm::RefProd<T>& prod) : refprod_(prod), values_(), regions_(), etas_(), phis_() {}
0098 
0099     void addRegion(const std::vector<int>& indices, const float eta, const float phi) {
0100       regions_.emplace_back((regions_.empty() ? 0 : regions_.back()) + indices.size());
0101       values_.insert(values_.end(), indices.begin(), indices.end());
0102       etas_.push_back(eta);
0103       phis_.push_back(phi);
0104     }
0105 
0106     edm::ProductID id() const { return refprod_.id(); }
0107     unsigned int size() const { return values_.size(); }
0108     unsigned int nRegions() const { return regions_.size(); }
0109     bool empty() const { return values_.empty(); }
0110     void clear() {
0111       values_.clear();
0112       regions_.clear();
0113       etas_.clear();
0114       phis_.clear();
0115     }
0116     void shrink_to_fit() {
0117       values_.shrink_to_fit();
0118       regions_.shrink_to_fit();
0119       etas_.shrink_to_fit();
0120       phis_.shrink_to_fit();
0121     }
0122 
0123     const_iterator begin() const { return const_iterator(this, 0); }
0124     const_iterator end() const { return const_iterator(this, values_.size()); }
0125 
0126     Region region(unsigned int ireg) const {
0127       if (ireg >= regions_.size())
0128         throw cms::Exception("Region index out of bounds");
0129       return Region(this, ireg == 0 ? 0 : regions_[ireg - 1], regions_[ireg]);
0130     }
0131 
0132     const float eta(unsigned int ireg) const { return etas_[ireg]; }
0133     const float phi(unsigned int ireg) const { return phis_[ireg]; }
0134 
0135     ref refAt(unsigned int idx) const { return ref(refprod_, values_[idx]); }
0136     const value_type& objAt(unsigned int idx) const { return (*refprod_)[values_[idx]]; }
0137 
0138     //Used by ROOT storage
0139     CMS_CLASS_VERSION(3)
0140 
0141   protected:
0142     refprod refprod_;
0143     std::vector<unsigned int> values_;   // list of indices to objects in each region, flattened.
0144     std::vector<unsigned int> regions_;  // for each region, store the index of one-past the last object in values
0145     std::vector<float> etas_;            // floatEtaCenter of each PFregion
0146     std::vector<float> phis_;            // floatPhiCenter of each PFregion
0147   };
0148 }  // namespace l1t
0149 #endif