CMS 3D CMS Logo

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