00001 #ifndef CROSSING_FRAME_H
00002 #define CROSSING_FRAME_H
00003
00016 #include "DataFormats/Provenance/interface/EventID.h"
00017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00018
00019 #include <vector>
00020 #include <string>
00021 #include <iostream>
00022 #include <utility>
00023 #include <algorithm>
00024
00025 template <class T>
00026 class CrossingFrame
00027 {
00028
00029 public:
00030
00031
00032 CrossingFrame(): firstCrossing_(0), lastCrossing_(0), bunchSpace_(75),subdet_(""),maxNbSources_(0) {;}
00033 CrossingFrame(int minb, int maxb, int bunchsp, std::string subdet ,unsigned int maxNbSources);
00034
00035 ~CrossingFrame() {;}
00036
00037 void swap(CrossingFrame& other);
00038
00039 CrossingFrame& operator=(CrossingFrame const& rhs);
00040
00041 void addSignals(const std::vector<T> * vec,edm::EventID id);
00042
00043 void addPileups(const int bcr,const std::vector<T> * vec, unsigned int evtId,int vertexoffset=0,bool checkTof=false,bool high=false);
00044
00045 void print(int level=0) const ;
00046 void setBcrOffset() {
00047 pileupOffsetsBcr_.push_back(pileups_.size());
00048 }
00049 void setSourceOffset(const unsigned int s) {
00050 pileupOffsetsSource_[s].push_back(pileups_.size());
00051 }
00052
00053
00054 edm::EventID getEventID() const {return id_;}
00055 std::pair<int,int> getBunchRange() const {return std::pair<int,int>(firstCrossing_,lastCrossing_);}
00056 int getBunchSpace() const {return bunchSpace_;}
00057 int getBunchCrossing(unsigned int ip) const;
00058 int getSourceType(unsigned int ip) const;
00059 void getSignal(typename std::vector<T>::const_iterator &first,typename std::vector<T>::const_iterator &last) const {
00060 first=signals_.begin();
00061 last=signals_.end();
00062 }
00063 void getPileups(typename std::vector<T>::const_iterator &first, typename std::vector<T>::const_iterator &last) const;
00064 unsigned int getNrSignals() const {return signals_.size();}
00065 unsigned int getNrPileups() const {return pileups_.size();}
00066 unsigned int getNrPileups(int bcr) const {
00067 return bcr==lastCrossing_ ? pileups_.size()-pileupOffsetsBcr_[lastCrossing_-firstCrossing_] :pileupOffsetsBcr_[bcr-firstCrossing_+1]- pileupOffsetsBcr_[bcr-firstCrossing_];}
00068
00069
00070
00071 const T & getObject(unsigned int ip) const {
00072 if (ip<0 || ip>getNrSignals()+getNrPileups()) throw cms::Exception("BadIndex")<<"CrossingFrame::getObject called with an invalid index!";
00073 if (ip<getNrSignals()) {
00074 return signals_[ip];
00075 }
00076 else {
00077 return pileups_[ip-getNrSignals()];
00078 }
00079 }
00080
00081
00082
00083 static const int lowTrackTof;
00084 static const int highTrackTof;
00085 static const int minLowTof;
00086 static const int limHighLowTof;
00087
00088 private:
00089
00090
00091 int firstCrossing_;
00092 int lastCrossing_;
00093 int bunchSpace_;
00094 std::string subdet_;
00095 edm::EventID id_;
00096
00097
00098 edm::EventID idFirstPileup_;
00099 unsigned int pileupFileNr_;
00100
00101 unsigned int maxNbSources_;
00102
00103
00104 std::vector<T> signals_;
00105
00106
00107 std::vector<T> pileups_;
00108 std::vector<unsigned int> pileupOffsetsBcr_;
00109 std::vector< std::vector<unsigned int> > pileupOffsetsSource_;
00110 };
00111
00112
00113
00114
00115
00116 template <class T>
00117 CrossingFrame<T>::CrossingFrame(int minb, int maxb, int bunchsp, std::string subdet ,unsigned int maxNbSources):firstCrossing_(minb), lastCrossing_(maxb), bunchSpace_(bunchsp),subdet_(subdet),maxNbSources_(maxNbSources) {
00118 pileupOffsetsSource_.resize(maxNbSources_);
00119 for (unsigned int i=0;i<maxNbSources_;++i)
00120 pileupOffsetsSource_[i].reserve(-firstCrossing_+lastCrossing_+1);
00121
00122
00123 pileupOffsetsBcr_.reserve(-firstCrossing_+lastCrossing_+1);
00124 }
00125
00126 template <typename T>
00127 inline
00128 void
00129 CrossingFrame<T>::swap(CrossingFrame<T>& other) {
00130 std::swap(firstCrossing_, other.firstCrossing_);
00131 std::swap(lastCrossing_, other.lastCrossing_);
00132 std::swap(bunchSpace_, other.bunchSpace_);
00133 subdet_.swap(other.subdet_);
00134 std::swap(id_, other.id_);
00135 std::swap(idFirstPileup_, other.idFirstPileup_);
00136 std::swap(pileupFileNr_, other.pileupFileNr_);
00137 std::swap(maxNbSources_, other.maxNbSources_);
00138 signals_.swap(other.signals_);
00139 pileups_.swap(other.pileups_);
00140 pileupOffsetsBcr_.swap(other.pileupOffsetsBcr_);
00141
00142
00143
00144
00145
00146
00147 pileupOffsetsSource_.resize(maxNbSources_);
00148 for (unsigned int i=0;i<pileupOffsetsSource_.size();++i) {
00149 pileupOffsetsSource_[i].swap(other.pileupOffsetsSource_[i]);
00150 }
00151 }
00152
00153 template <typename T>
00154 inline
00155 CrossingFrame<T>&
00156 CrossingFrame<T>::operator=(CrossingFrame<T> const& rhs) {
00157 CrossingFrame<T> temp(rhs);
00158 this->swap(temp);
00159 return *this;
00160 }
00161
00162 template <class T>
00163 void CrossingFrame<T>::addSignals(const std::vector<T> * vec,edm::EventID id){
00164 id_=id;
00165 signals_=*vec;
00166 }
00167
00168 template <class T>
00169 void CrossingFrame<T>::getPileups(typename std::vector<T>::const_iterator &first,typename std::vector<T>::const_iterator &last) const {
00170 first=pileups_.begin();
00171 last=pileups_.end();
00172 }
00173
00174 template <class T>
00175 void CrossingFrame<T>::print(int level) const {
00176 }
00177
00178 template <class T>
00179 int CrossingFrame<T>::getSourceType(unsigned int ip) const {
00180
00181
00182 unsigned int bcr= getBunchCrossing(ip)-firstCrossing_;
00183 for (unsigned int i=0;i<pileupOffsetsSource_.size()-1;++i) {
00184 if (ip>=(pileupOffsetsSource_[i])[bcr] && ip <(pileupOffsetsSource_[i+1])[bcr]) return i;
00185 }
00186 return pileupOffsetsSource_.size()-1;
00187 }
00188
00189 template <class T>
00190 int CrossingFrame<T>::getBunchCrossing(unsigned int ip) const {
00191
00192 for (unsigned int ii=1;ii<pileupOffsetsBcr_.size();ii++){
00193 if (ip>=pileupOffsetsBcr_[ii-1] && ip<pileupOffsetsBcr_[ii]) return ii+firstCrossing_-1;
00194 }
00195 if (ip<pileups_.size()) return lastCrossing_;
00196 else return 999;
00197 }
00198
00199
00200
00201 template <typename T>
00202 inline
00203 void
00204 swap(CrossingFrame<T>& lhs, CrossingFrame<T>& rhs) {
00205 lhs.swap(rhs);
00206 }
00207
00208 #include<iosfwd>
00209 #include<iostream>
00210
00211 template <class T>
00212 std::ostream &operator<<(std::ostream& o, const CrossingFrame<T>& cf)
00213 {
00214 std::pair<int,int> range=cf.getBunchRange();
00215 o <<"\nCrossingFrame for subdet "<<cf.getEventID()<<", bunchrange = "<<range.first<<","<<range.second
00216 <<", bunchSpace "<<cf.getBunchSpace();
00217
00218 return o;
00219 }
00220
00221 #endif