CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/SimDataFormats/CrossingFrame/interface/CrossingFrame.h

Go to the documentation of this file.
00001 #ifndef CROSSING_FRAME_H
00002 #define CROSSING_FRAME_H
00003 
00016 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00017 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00018 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
00019 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00020 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00021 
00022 #include "DataFormats/Provenance/interface/EventID.h"
00023 #include "DataFormats/Common/interface/Wrapper.h"
00024 #include "SimDataFormats/EncodedEventId/interface/EncodedEventId.h"
00025 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00026 
00027 template <class T>
00028 class PCrossingFrame;
00029 
00030 #include "boost/shared_ptr.hpp"
00031 #include <vector>
00032 #include <string>
00033 #include <iostream>
00034 #include <utility>
00035 #include <algorithm>
00036 
00037 
00038 template <class T> 
00039 class CrossingFrame 
00040 { 
00041 
00042  public:
00043   // con- and destructors
00044 
00045   CrossingFrame():  firstCrossing_(0), lastCrossing_(0), bunchSpace_(75),subdet_(""),maxNbSources_(0) {
00046 }
00047   CrossingFrame(int minb, int maxb, int bunchsp, std::string subdet ,unsigned int maxNbSources);
00048 
00049   ~CrossingFrame() {;}
00050 
00051   void swap(CrossingFrame& other);
00052 
00053   CrossingFrame& operator=(CrossingFrame const& rhs);
00054 
00055   //standard version
00056   void addSignals(const std::vector<T> * vec,edm::EventID id);
00057   // version for HepMCProduct
00058   void addSignals(const T * vec,edm::EventID id);
00059  
00060   // standard version
00061   void addPileups(const int bcr, std::vector<T> * vec, unsigned int evtId,int vertexoffset=0);
00062   // version for HepMCProduct
00063   void addPileups(const int bcr, T * product, unsigned int evtId,int vertexoffset=0);
00064 
00065   void setTof( );
00066 
00067   // we keep the shared pointer in the object that will be only destroyed at the end of the event (transient object!)
00068   // because of HepMCProduct, we need 2 versions...
00069   void setPileupPtr(boost::shared_ptr<edm::Wrapper<std::vector<T> > const> shPtr) {shPtrPileups_=shPtr;}
00070   void setPileupPtr(boost::shared_ptr<edm::Wrapper<T> const> shPtr) {shPtrPileups2_=shPtr;}
00071   // used in the Step2 to set the PCrossingFrame
00072   void setPileupPtr(boost::shared_ptr<edm::Wrapper<PCrossingFrame<T> > const> shPtr);
00073   
00074   void print(int level=0) const ;
00075 
00076   void setBcrOffset() {
00077     pileupOffsetsBcr_.push_back(pileups_.size());
00078   }
00079 
00080   void setSourceOffset(const unsigned int s) {
00081     pileupOffsetsSource_[s].push_back(pileups_.size());
00082   }
00083 
00084   //getters
00085   edm::EventID getEventID() const {return id_;}
00086   std::pair<int,int> getBunchRange() const {return std::pair<int,int>(firstCrossing_,lastCrossing_);}
00087   int getBunchSpace() const {return bunchSpace_;}
00088   unsigned int getMaxNbSources() const {return maxNbSources_; }
00089   std::string getSubDet() const { return subdet_;}
00090   unsigned int getPileupFileNr() const {return pileupFileNr_;}
00091   edm::EventID getIdFirstPileup() const {return idFirstPileup_;}
00092   std::vector<unsigned int> getPileupOffsetsBcr() const {return pileupOffsetsBcr_;}   
00093   std::vector< std::vector<unsigned int> > getPileupOffsetsSource() const {return pileupOffsetsSource_;} //one per source
00094   std::vector<const T *> getPileups() const {return pileups_;}
00095   std::vector<const T *> getSignal() const {return signals_;}
00096   
00097   
00098   void getSignal(typename std::vector<const T *>::const_iterator &first,typename std::vector<const T*>::const_iterator &last) const {
00099     first=signals_.begin();
00100     last=signals_.end();
00101   }
00102   void getPileups(typename std::vector<const T*>::const_iterator &first, typename std::vector<const T*>::const_iterator &last) const;
00103   unsigned int getNrSignals() const {return signals_.size();} 
00104   unsigned int getNrPileups() const {return pileups_.size();} 
00105   unsigned int getNrPileups(int bcr) const {
00106     return bcr==lastCrossing_ ? pileups_.size()-pileupOffsetsBcr_[lastCrossing_-firstCrossing_] :pileupOffsetsBcr_[bcr-firstCrossing_+1]- pileupOffsetsBcr_[bcr-firstCrossing_];} 
00107 
00108   // get pileup information in dependency from internal pointer
00109   int getBunchCrossing(unsigned int ip) const;
00110 
00111   int getSourceType(unsigned int ip) const;
00112 
00113   // get object in pileup when position in the vector is known (for DigiSimLink typically)
00114 
00115   const T & getObject(unsigned int ip) const { 
00116     //ip is position in the MixCollection (i.e. signal + pileup)
00117     if (ip>getNrSignals()+getNrPileups()) throw cms::Exception("BadIndex")<<"CrossingFrame::getObject called with an invalid index- index was "<<ip<<"!"; // ip >=0, since ip is unsigned
00118     if (ip<getNrSignals()) {
00119       return *(signals_[ip]);
00120     }
00121     else  {
00122       return *(pileups_[ip-getNrSignals()]);
00123     }
00124   }  
00125 
00126 
00127   // setters needed for step2 when using mixed secondary source  
00128   void setEventID(edm::EventID evId) { id_ = evId; }
00129   void setPileups(std::vector<const T *> p) { pileups_ = p; } 
00130   void setBunchSpace(int bSpace) { bunchSpace_ = bSpace; } 
00131   void setMaxNbSources(unsigned int mNbS) { maxNbSources_ = mNbS; } 
00132   void setSubDet(std::string det) { subdet_ = det; } 
00133   void setPileupFileNr(unsigned int pFileNr) { pileupFileNr_ = pFileNr;} 
00134   void setIdFirstPileup(edm::EventID idFP) {idFirstPileup_ = idFP;}
00135   void setPileupOffsetsBcr(std::vector<unsigned int> pOffsetsBcr) { pileupOffsetsBcr_ = pOffsetsBcr;}  
00136   void setPileupOffsetsSource(std::vector< std::vector<unsigned int> > pOffsetsS) { pileupOffsetsSource_ = pOffsetsS;}  //one per source
00137   void setBunchRange(std::pair<int,int> bunchRange) { firstCrossing_ = bunchRange.first;
00138                                                       lastCrossing_ = bunchRange.second;} 
00139   
00140  private:                                           
00141   // please update the swap() function below if any data members are added.
00142   // general information
00143   int firstCrossing_;
00144   int lastCrossing_;
00145   int bunchSpace_;  //in nsec
00146   std::string subdet_;  // for PSimHits/PCaloHits
00147   edm::EventID id_; // event id of the signal event
00148 
00149   // for playback option
00150   edm::EventID idFirstPileup_;   // EventId fof the first pileup event used for this signal event
00151   unsigned int pileupFileNr_;    // ordinal number of the pileup file this event was in
00152 
00153   unsigned int maxNbSources_;
00154 
00155   // signal
00156   std::vector<const T * >  signals_; 
00157 
00158   //pileup
00159   std::vector<const T *>  pileups_; 
00160   boost::shared_ptr<edm::Wrapper<std::vector<T> > const> shPtrPileups_; 
00161   boost::shared_ptr<edm::Wrapper<T> const> shPtrPileups2_;   // fore HepMCProduct
00162   boost::shared_ptr<edm::Wrapper<PCrossingFrame<T> > const> shPtrPileupsPCF_;
00163 //  boost::shared_ptr<edm::Wrapper<PCrossingFrame<edm::HepMCProduct> const> shPtrPileupsHepMCProductPCF_;
00164   
00165   // these are informations stored in order to be able to have information
00166   // as a function of the position of an object in the pileups_ vector
00167   std::vector<unsigned int> pileupOffsetsBcr_;
00168   std::vector< std::vector<unsigned int> > pileupOffsetsSource_; //one per source
00169   
00170 };
00171 
00172 //==============================================================================
00173 //                              implementations
00174 //==============================================================================
00175 
00176 template <class T> 
00177 CrossingFrame<T>::CrossingFrame(int minb, int maxb, int bunchsp, std::string subdet ,unsigned int
00178 maxNbSources):firstCrossing_(minb), lastCrossing_(maxb),
00179 bunchSpace_(bunchsp),subdet_(subdet),maxNbSources_(maxNbSources) {
00180  pileupOffsetsSource_.resize(maxNbSources_);
00181  for (unsigned int i=0;i<maxNbSources_;++i)
00182    pileupOffsetsSource_[i].reserve(-firstCrossing_+lastCrossing_+1);
00183 
00184 //FIXME: should we force around 0 or so??
00185   pileupOffsetsBcr_.reserve(-firstCrossing_+lastCrossing_+1);
00186 }
00187 
00188 template <typename T>
00189 inline
00190 void
00191 CrossingFrame<T>::swap(CrossingFrame<T>& other) {
00192   std::swap(firstCrossing_, other.firstCrossing_);
00193   std::swap(lastCrossing_, other.lastCrossing_);
00194   std::swap(bunchSpace_, other.bunchSpace_);
00195   subdet_.swap(other.subdet_);
00196   std::swap(id_, other.id_);
00197   std::swap(idFirstPileup_, other.idFirstPileup_);
00198   std::swap(pileupFileNr_, other.pileupFileNr_);
00199   std::swap(maxNbSources_, other.maxNbSources_);
00200   signals_.swap(other.signals_);
00201   pileups_.swap(other.pileups_);
00202   pileupOffsetsBcr_.swap(other.pileupOffsetsBcr_);
00203   pileupOffsetsSource_.resize(maxNbSources_);
00204   for (unsigned int i=0;i<pileupOffsetsSource_.size();++i) { 
00205     pileupOffsetsSource_[i].swap(other.pileupOffsetsSource_[i]);
00206   }
00207 }
00208 
00209 template <typename T>
00210 inline
00211 CrossingFrame<T>&
00212 CrossingFrame<T>::operator=(CrossingFrame<T> const& rhs) {
00213   CrossingFrame<T> temp(rhs);
00214   this->swap(temp);
00215   return *this;
00216 }
00217 
00218 
00219 template <class T>  
00220 void  CrossingFrame<T>::getPileups(typename std::vector<const T *>::const_iterator &first,typename std::vector<const T *>::const_iterator &last) const {
00221   first=pileups_.begin();
00222   last=pileups_.end();
00223 }
00224 
00225 template <class T> 
00226 void CrossingFrame<T>::print(int level) const {
00227 }
00228 
00229 template <class T> 
00230 int  CrossingFrame<T>::getSourceType(unsigned int ip) const {
00231   // ip is position in the pileup vector
00232   // decide to which source belongs object with index ip in the pileup vector
00233   // pileup=0, cosmics=1, beam halo+ =2, beam halo- =3 forward =4
00234   unsigned int bcr= getBunchCrossing(ip)-firstCrossing_; //starts at 0
00235   for (unsigned int i=0;i<pileupOffsetsSource_.size()-1;++i) {
00236     if (ip>=(pileupOffsetsSource_[i])[bcr] && ip <(pileupOffsetsSource_[i+1])[bcr]) return i;
00237   }
00238   return pileupOffsetsSource_.size()-1;
00239 }
00240 
00241 template <class T>   
00242 int CrossingFrame<T>::getBunchCrossing(unsigned int ip) const {
00243   // return the bcr for a certain position in the pileup vector
00244     for (unsigned int ii=1;ii<pileupOffsetsBcr_.size();ii++){
00245       if (ip>=pileupOffsetsBcr_[ii-1] && ip<pileupOffsetsBcr_[ii]) return ii+firstCrossing_-1;
00246     }
00247     if (ip<pileups_.size()) return lastCrossing_;
00248     else return 999;
00249 }
00250 
00251 // Free swap function
00252 template <typename T>
00253 inline
00254 void
00255 swap(CrossingFrame<T>& lhs, CrossingFrame<T>& rhs) {
00256   lhs.swap(rhs);
00257 }
00258 
00259 #include<iosfwd>
00260 #include<iostream>
00261 
00262 template <class T>
00263 std::ostream &operator<<(std::ostream& o, const CrossingFrame<T>& cf)
00264 {
00265   std::pair<int,int> range=cf.getBunchRange();
00266   o <<"\nCrossingFrame for subdet "<<cf.getEventID()<<",  bunchrange = "<<range.first<<","<<range.second
00267     <<", bunchSpace "<<cf.getBunchSpace();
00268 
00269   return o;
00270 }
00271 
00272 #include "SimDataFormats/CrossingFrame/interface/PCrossingFrame.h"
00273 template <class T>
00274 void CrossingFrame<T>::setPileupPtr(boost::shared_ptr<edm::Wrapper<PCrossingFrame<T> > const> shPtr) {shPtrPileupsPCF_=shPtr;}
00275 
00276 
00277 //==================== template specializations  ===========================================
00278 template <class T>
00279 void CrossingFrame<T>::addPileups(const int bcr, T * product, unsigned int evtId,int vertexoffset) {
00280   // default, valid for HepMCProduct
00281   pileups_.push_back(product);
00282 }
00283 
00284 template <class T>
00285 void CrossingFrame<T>::addPileups(const int bcr, std::vector<T> * product, unsigned int evtId,int vertexoffset){
00286   // default, in fact never called since special implementations exist for all possible types
00287   // of this signature, i.e. PSimHit, PCaloHit, SimTrack, SimVertex
00288   // But needs to be present for HepMCProduct
00289 }
00290 
00291 template <>
00292 void CrossingFrame<SimTrack>::addPileups(const int bcr, std::vector<SimTrack> *, unsigned int evtId,int vertexoffset);
00293 
00294 template <>
00295 void CrossingFrame<SimVertex>::addPileups(const int bcr, std::vector<SimVertex> *, unsigned int evtId,int vertexoffset);
00296 
00297 template <>
00298 void CrossingFrame<PSimHit>::addPileups(const int bcr, std::vector<PSimHit> *, unsigned int evtId,int vertexoffset);
00299 
00300 template <>
00301 void CrossingFrame<PCaloHit>::addPileups(const int bcr, std::vector<PCaloHit> *, unsigned int evtId,int vertexoffset);
00302 
00303 template <class T> 
00304 void CrossingFrame<T>::addSignals(const std::vector<T> * vec,edm::EventID id){
00305   // valid (called) for all except HepMCProduct
00306   id_=id;
00307   for (unsigned int i=0;i<vec->size();++i) {
00308     signals_.push_back(&((*vec)[i]));
00309   }
00310 }
00311 
00312 template <class T> 
00313 void CrossingFrame<T>::addSignals(const T * product,edm::EventID id){
00314   // valid (called) for all except HepMCProduct
00315   id_=id;
00316   signals_.push_back(product);
00317 }
00318 
00319 template <class T>
00320 void CrossingFrame<T>::setTof() {;}
00321 
00322 #endif