Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:38

0001 #include <cassert>
0002 
0003 // FIXME: these 3 lines are required by other packages
0004 #include <vector>
0005 #include <iostream>
0006 using namespace std;
0007 
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 
0010 template <class T>
0011 BXVector<T>::BXVector(unsigned size,  // number of objects per BX
0012                       int bxFirst,    // first BX stored
0013                       int bxLast)     // last BX stored
0014     : bxFirst_(std::min(0, bxFirst)), bxLast_(std::max(0, bxLast)), data_(std::vector<T>(size * numBX())) {
0015   assert(bxFirst_ <= bxLast_);
0016   itrs_.clear();
0017   for (unsigned i = 0; i < numBX(); i++) {
0018     itrs_.push_back(i * size);
0019   }
0020 }
0021 
0022 // set BX range, adds an empty bx so you must push_pack to see anything
0023 template <class T>
0024 void BXVector<T>::setBXRange(int bxFirst, int bxLast) {
0025   int bxF = bxFirst_;
0026   int bxL = bxLast_;
0027   if (bxFirst < bxFirst_) {
0028     for (int i = 0; i < bxF - bxFirst; i++) {
0029       itrs_.insert(itrs_.begin(), itrs_[0]);
0030     }
0031   }
0032   if (bxFirst > bxFirst_) {
0033     for (int i = 0; i < bxFirst - bxF; i++) {
0034       deleteBX(bxF + i);
0035     }
0036   }
0037   if (bxLast > bxLast_) {
0038     for (int i = 0; i < bxLast - bxL; i++) {
0039       addBX();
0040     }
0041   }
0042   if (bxLast < bxLast_) {
0043     for (int i = 0; i < bxL - bxLast; i++) {
0044       deleteBX(bxL - i);
0045     }
0046   }
0047   bxFirst_ = bxFirst;
0048   bxLast_ = bxLast;
0049 }
0050 
0051 // add a BX, empty bx
0052 template <class T>
0053 void BXVector<T>::addBX() {
0054   itrs_.push_back(data_.size());
0055   bxLast_ = bxLast_ + 1;
0056 }
0057 
0058 // delete a BX
0059 template <class T>
0060 void BXVector<T>::deleteBX(int bx) {
0061   clearBX(bx);
0062   itrs_.erase(itrs_.begin() + indexFromBX(bx));
0063   if (bx == getFirstBX()) {
0064     bxFirst_ = bxFirst_ + 1;
0065   }
0066   if (bx == getLastBX()) {
0067     bxLast_ = bxLast_ - 1;
0068   }
0069 }
0070 // set size for a given BX
0071 template <class T>
0072 void BXVector<T>::resize(int bx, unsigned max) {
0073   T k;
0074   unsigned s = size(bx);
0075   if (size(bx) < max) {
0076     for (unsigned i = 0; i < max - s; i++) {
0077       push_back(bx, k);
0078     }
0079   } else {
0080     for (unsigned i = 0; i < s - max; i++) {
0081       erase(bx, s - i - 1);
0082     }
0083   }
0084 }
0085 
0086 // set size for all BXs
0087 template <class T>
0088 void BXVector<T>::resizeAll(unsigned size) {
0089   for (unsigned i = 0; i < itrs_.size(); ++i) {
0090     resize(i, size);
0091   }
0092 }
0093 
0094 // iterator access by BX
0095 template <class T>
0096 typename BXVector<T>::const_iterator BXVector<T>::begin(int bx) const {
0097   assert(!itrs_.empty());
0098   return data_.begin() + itrs_[indexFromBX(bx)];
0099 }
0100 
0101 // iterator access by BX
0102 template <class T>
0103 typename BXVector<T>::const_iterator BXVector<T>::end(int bx) const {
0104   return data_.begin() + itrs_[indexFromBX(bx)] + size(bx);
0105 }
0106 
0107 // get the first BX stored
0108 template <class T>
0109 int BXVector<T>::getFirstBX() const {
0110   return bxFirst_;
0111 }
0112 
0113 // get the last BX stored
0114 template <class T>
0115 int BXVector<T>::getLastBX() const {
0116   return bxLast_;
0117 }
0118 
0119 // get N objects for a given BX
0120 template <class T>
0121 unsigned BXVector<T>::size(int bx) const {
0122   if (isEmpty(bx)) {
0123     return 0;
0124   }
0125 
0126   if (indexFromBX(bx) == (itrs_.size() - 1)) {
0127     return (data_.size() - itrs_[itrs_.size() - 1]);
0128   } else {
0129     return (itrs_[indexFromBX(bx + 1)] - itrs_[indexFromBX(bx)]);
0130   }
0131 }
0132 
0133 // add element with given BX index
0134 template <class T>
0135 void BXVector<T>::push_back(int bx, T object) {
0136   if (bx >= bxFirst_ && bx <= bxLast_) {
0137     data_.insert(data_.begin() + itrs_[indexFromBX(bx)] + size(bx), object);
0138     for (unsigned k = 0; k < itrs_.size(); k++) {
0139       if (k > indexFromBX(bx)) {
0140         itrs_[k]++;
0141       }
0142     }
0143   } else {
0144     edm::LogWarning("BXVectorBXViolation")
0145         << "Something attempted to push to a BXVector BX that does not exist. The data will be ignored. bx: " << bx
0146         << " bxFirst: " << bxFirst_ << " bxLast: " << bxLast_;
0147   }
0148 }
0149 
0150 // clear entire BXVector
0151 template <class T>
0152 void BXVector<T>::clear() {
0153   data_.clear();
0154   for (unsigned k = 0; k < itrs_.size(); k++) {
0155     itrs_[k] = 0;
0156   }
0157 }
0158 
0159 // insert data in location i of bx
0160 template <class T>
0161 void BXVector<T>::insert(int bx, unsigned i, T object) {
0162   if (i > size(bx)) {
0163     //cout<<"ERROR: bx "<<bx<<" is only "<<size(bx)<<" long"<<endl;
0164   } else {
0165     data_.insert(data_.begin() + itrs_[indexFromBX(bx)] + i, object);
0166 
0167     for (unsigned k = 0; k < itrs_.size(); k++) {
0168       if (k > indexFromBX(bx)) {
0169         itrs_[k]++;
0170       }
0171     }
0172   }
0173 }
0174 
0175 // erase data in location i of bx
0176 template <class T>
0177 void BXVector<T>::erase(int bx, unsigned i) {
0178   if (i >= size(bx)) {
0179     //cout<<"ERROR: bx "<<bx<<" is only "<<size(bx)<<" long"<<endl;
0180   } else {
0181     data_.erase(data_.begin() + itrs_[indexFromBX(bx)] + i);
0182     for (unsigned k = 0; k < itrs_.size(); k++) {
0183       if (k > indexFromBX(bx)) {
0184         itrs_[k]--;
0185       }
0186     }
0187   }
0188 }
0189 
0190 // clear information for given bx but keeps record of the empty BX
0191 template <class T>
0192 void BXVector<T>::clearBX(int bx) {
0193   unsigned si = size(bx);
0194   for (unsigned i = 0; i < si; i++) {
0195     erase(bx, 0);
0196   }
0197 }
0198 
0199 // access data at a given location i in bunch crossing bx
0200 template <class T>
0201 const T& BXVector<T>::at(int bx, unsigned i) const {
0202   return data_.at(itrs_[indexFromBX(bx)] + i);
0203 }
0204 
0205 // set data at a given location i in bunch crossing bx
0206 template <class T>
0207 void BXVector<T>::set(int bx, unsigned i, const T& object) {
0208   if (i >= size(bx)) {
0209     //cout<<"ERROR: bx "<<bx<<" is only "<<size(bx)<<" long"<<endl;
0210   } else {
0211     data_.at(itrs_[indexFromBX(bx)] + i) = object;
0212   }
0213 }
0214 
0215 // redefine bunch crossing starting from 0
0216 template <class T>
0217 unsigned BXVector<T>::indexFromBX(int bx) const {
0218   return bx - bxFirst_;
0219 }
0220 
0221 // check to see if bx is empty
0222 template <class T>
0223 bool BXVector<T>::isEmpty(int bx) const {
0224   if (bx < bxFirst_) {
0225     return true;
0226   }
0227 
0228   auto const index_bx = indexFromBX(bx);
0229 
0230   if (index_bx >= itrs_.size()) {
0231     return true;
0232   }
0233 
0234   if (itrs_[index_bx] == data_.size()) {
0235     return true;
0236   }
0237 
0238   auto const index_bxPlus1 = indexFromBX(bx + 1);
0239 
0240   if (index_bxPlus1 >= itrs_.size()) {
0241     return false;
0242   }
0243 
0244   if (itrs_[index_bx] == itrs_[index_bxPlus1]) {
0245     return true;
0246   }
0247 
0248   return false;
0249 }
0250 
0251 // edm::View support
0252 template <class T>
0253 inline void BXVector<T>::fillView(edm::ProductID const& id,
0254                                   std::vector<void const*>& pointers,
0255                                   edm::FillViewHelperVector& helpers) const {
0256   edm::detail::reallyFillView(*this, id, pointers, helpers);
0257 }
0258 //
0259 namespace edm {
0260   template <class T>
0261   inline void fillView(BXVector<T> const& obj,
0262                        edm::ProductID const& id,
0263                        std::vector<void const*>& pointers,
0264                        edm::FillViewHelperVector& helpers) {
0265     obj.fillView(id, pointers, helpers);
0266   }
0267   template <class T>
0268   struct has_fillView<BXVector<T> > {
0269     static bool const value = true;
0270   };
0271 }  // namespace edm
0272 
0273 // edm::Ptr support
0274 template <class T>
0275 inline void BXVector<T>::setPtr(std::type_info const& toType, unsigned long index, void const*& ptr) const {
0276   edm::detail::reallySetPtr<BXVector<T> >(*this, toType, index, ptr);
0277 }
0278 template <class T>
0279 inline void BXVector<T>::fillPtrVector(std::type_info const& toType,
0280                                        std::vector<unsigned long> const& indices,
0281                                        std::vector<void const*>& ptrs) const {
0282   edm::detail::reallyfillPtrVector(*this, toType, indices, ptrs);
0283 }
0284 //
0285 template <class T>
0286 inline void setPtr(BXVector<T> const& obj, std::type_info const& toType, unsigned long index, void const*& ptr) {
0287   obj.setPtr(toType, index, ptr);
0288 }
0289 template <class T>
0290 inline void fillPtrVector(BXVector<T> const& obj,
0291                           std::type_info const& toType,
0292                           std::vector<unsigned long> const& indices,
0293                           std::vector<void const*>& ptrs) {
0294   obj.fillPtrVector(toType, indices, ptrs);
0295 }
0296 namespace edm {
0297   template <class T>
0298   struct has_setPtr<BXVector<T> > {
0299     static bool const value = true;
0300   };
0301 }  // namespace edm
0302 
0303 // Local Variables:
0304 // mode: c++
0305 // End: