Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 23:31:14

0001 #ifndef CROSSING_FRAME_H
0002 #define CROSSING_FRAME_H
0003 
0004 /** \class CrossingFrame
0005  *
0006  * CrossingFrame is the result of the Sim Mixing Module
0007  *
0008  * \author Ursula Berthon, Claude Charlot,  LLR Palaiseau
0009  *
0010  * \version   1st Version July 2005
0011  * \version   2nd Version Sep 2005
0012  * \version   3rd Version Nov 2007
0013  *
0014  ************************************************************/
0015 
0016 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0017 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0018 #include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
0019 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
0020 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0021 
0022 #include "DataFormats/Provenance/interface/EventID.h"
0023 #include "DataFormats/Common/interface/Wrapper.h"
0024 #include "SimDataFormats/EncodedEventId/interface/EncodedEventId.h"
0025 
0026 template <class T>
0027 class PCrossingFrame;
0028 
0029 #include <vector>
0030 #include <string>
0031 #include <iostream>
0032 #include <utility>
0033 #include <algorithm>
0034 #include <memory>
0035 
0036 template <class T>
0037 class CrossingFrame {
0038 public:
0039   // con- and destructors
0040 
0041   CrossingFrame() : firstCrossing_(0), lastCrossing_(0), bunchSpace_(75), subdet_(""), maxNbSources_(0) {}
0042   CrossingFrame(int minb, int maxb, int bunchsp, std::string subdet, unsigned int maxNbSources);
0043 
0044   ~CrossingFrame() { ; }
0045 
0046   void swap(CrossingFrame& other);
0047 
0048   CrossingFrame& operator=(CrossingFrame const& rhs);
0049 
0050   //standard version
0051   void addSignals(const std::vector<T>* vec, edm::EventID id);
0052   // version for HepMCProduct
0053   void addSignals(const T* vec, edm::EventID id);
0054 
0055   // standard version
0056   void addPileups(std::vector<T> const& vec);
0057   // version for HepMCProduct
0058   void addPileups(T const& product);
0059 
0060   void setTof();
0061 
0062   // we keep the shared pointer in the object that will be only destroyed at the end of the event (transient object!)
0063   // because of HepMCProduct, we need 2 versions...
0064   /*   void setPileupPtr(std::shared_ptr<edm::Wrapper<std::vector<T> > const> shPtr) {shPtrPileups_=shPtr;} */
0065   /*   void setPileupPtr(std::shared_ptr<edm::Wrapper<T> const> shPtr) {shPtrPileups2_=shPtr;} */
0066   void setPileupPtr(std::shared_ptr<edm::Wrapper<std::vector<T> > const> shPtr) { shPtrPileups_.push_back(shPtr); }
0067   void setPileupPtr(std::shared_ptr<edm::Wrapper<T> const> shPtr) { shPtrPileups2_.push_back(shPtr); }
0068   // used in the Step2 to set the PCrossingFrame
0069   void setPileupPtr(std::shared_ptr<edm::Wrapper<PCrossingFrame<T> > const> shPtr);
0070 
0071   void print(int level = 0) const;
0072 
0073   void setBcrOffset() { pileupOffsetsBcr_.push_back(pileups_.size()); }
0074 
0075   void setSourceOffset(const unsigned int s) { pileupOffsetsSource_[s].push_back(pileups_.size()); }
0076 
0077   //getters
0078   edm::EventID getEventID() const { return id_; }
0079   std::pair<int, int> getBunchRange() const { return std::pair<int, int>(firstCrossing_, lastCrossing_); }
0080   int getBunchSpace() const { return bunchSpace_; }
0081   unsigned int getMaxNbSources() const { return maxNbSources_; }
0082   std::string getSubDet() const { return subdet_; }
0083   unsigned int getPileupFileNr() const { return pileupFileNr_; }
0084   edm::EventID getIdFirstPileup() const { return idFirstPileup_; }
0085   const std::vector<unsigned int>& getPileupOffsetsBcr() const { return pileupOffsetsBcr_; }
0086   const std::vector<std::vector<unsigned int> >& getPileupOffsetsSource() const {
0087     return pileupOffsetsSource_;
0088   }  //one per source
0089   const std::vector<const T*>& getPileups() const { return pileups_; }
0090   const std::vector<const T*>& getSignal() const { return signals_; }
0091 
0092   void getSignal(typename std::vector<const T*>::const_iterator& first,
0093                  typename std::vector<const T*>::const_iterator& last) const {
0094     first = signals_.begin();
0095     last = signals_.end();
0096   }
0097   void getPileups(typename std::vector<const T*>::const_iterator& first,
0098                   typename std::vector<const T*>::const_iterator& last) const;
0099   unsigned int getNrSignals() const { return signals_.size(); }
0100   unsigned int getNrPileups() const { return pileups_.size(); }
0101   unsigned int getNrPileups(int bcr) const {
0102     return bcr == lastCrossing_ ? pileups_.size() - pileupOffsetsBcr_[lastCrossing_ - firstCrossing_]
0103                                 : pileupOffsetsBcr_[bcr - firstCrossing_ + 1] - pileupOffsetsBcr_[bcr - firstCrossing_];
0104   }
0105 
0106   // get pileup information in dependency from internal pointer
0107   int getBunchCrossing(unsigned int ip) const;
0108 
0109   int getSourceType(unsigned int ip) const;
0110 
0111   // get object in pileup when position in the vector is known (for DigiSimLink typically)
0112 
0113   const T& getObject(unsigned int ip) const {
0114     //ip is position in the MixCollection (i.e. signal + pileup)
0115     if (ip > getNrSignals() + getNrPileups())
0116       throw cms::Exception("BadIndex") << "CrossingFrame::getObject called with an invalid index- index was " << ip
0117                                        << "!";  // ip >=0, since ip is unsigned
0118     if (ip < getNrSignals()) {
0119       return *(signals_[ip]);
0120     } else {
0121       return *(pileups_[ip - getNrSignals()]);
0122     }
0123   }
0124 
0125   // setters needed for step2 when using mixed secondary source
0126   void setEventID(edm::EventID evId) { id_ = evId; }
0127   void setPileups(const std::vector<const T*>& p) { pileups_ = p; }
0128   void setBunchSpace(int bSpace) { bunchSpace_ = bSpace; }
0129   void setMaxNbSources(unsigned int mNbS) { maxNbSources_ = mNbS; }
0130   void setSubDet(std::string det) { subdet_ = det; }
0131   void setPileupFileNr(unsigned int pFileNr) { pileupFileNr_ = pFileNr; }
0132   void setIdFirstPileup(edm::EventID idFP) { idFirstPileup_ = idFP; }
0133   void setPileupOffsetsBcr(const std::vector<unsigned int>& pOffsetsBcr) { pileupOffsetsBcr_ = pOffsetsBcr; }
0134   void setPileupOffsetsSource(const std::vector<std::vector<unsigned int> >& pOffsetsS) {
0135     pileupOffsetsSource_ = pOffsetsS;
0136   }  //one per source
0137   void setBunchRange(std::pair<int, int> bunchRange) {
0138     firstCrossing_ = bunchRange.first;
0139     lastCrossing_ = bunchRange.second;
0140   }
0141 
0142 private:
0143   // please update the swap() function below if any data members are added.
0144   // general information
0145   int firstCrossing_;
0146   int lastCrossing_;
0147   int bunchSpace_;      //in nsec
0148   std::string subdet_;  // for PSimHits/PCaloHits
0149   edm::EventID id_;     // event id of the signal event
0150 
0151   // for playback option
0152   edm::EventID idFirstPileup_;  // EventId fof the first pileup event used for this signal event
0153   unsigned int pileupFileNr_;   // ordinal number of the pileup file this event was in
0154 
0155   unsigned int maxNbSources_;
0156 
0157   // signal
0158   std::vector<const T*> signals_;
0159 
0160   //pileup
0161   std::vector<const T*> pileups_;
0162   std::vector<std::shared_ptr<edm::Wrapper<std::vector<T> > const> > shPtrPileups_;
0163   std::vector<std::shared_ptr<edm::Wrapper<T> const> > shPtrPileups2_;  // fore HepMCProduct
0164   /*   std::shared_ptr<edm::Wrapper<std::vector<T> > const> shPtrPileups_;  */
0165   /*   std::shared_ptr<edm::Wrapper<T> const> shPtrPileups2_;   // fore HepMCProduct */
0166   std::shared_ptr<edm::Wrapper<PCrossingFrame<T> > const> shPtrPileupsPCF_;
0167   //  std::shared_ptr<edm::Wrapper<PCrossingFrame<edm::HepMCProduct> const> shPtrPileupsHepMCProductPCF_;
0168 
0169   // these are informations stored in order to be able to have information
0170   // as a function of the position of an object in the pileups_ vector
0171   std::vector<unsigned int> pileupOffsetsBcr_;
0172   std::vector<std::vector<unsigned int> > pileupOffsetsSource_;  //one per source
0173 };
0174 
0175 //==============================================================================
0176 //                              implementations
0177 //==============================================================================
0178 
0179 template <class T>
0180 CrossingFrame<T>::CrossingFrame(int minb, int maxb, int bunchsp, std::string subdet, unsigned int maxNbSources)
0181     : firstCrossing_(minb), lastCrossing_(maxb), bunchSpace_(bunchsp), subdet_(subdet), maxNbSources_(maxNbSources) {
0182   pileupOffsetsSource_.resize(maxNbSources_);
0183   for (unsigned int i = 0; i < maxNbSources_; ++i)
0184     pileupOffsetsSource_[i].reserve(-firstCrossing_ + lastCrossing_ + 1);
0185 
0186   //FIXME: should we force around 0 or so??
0187   pileupOffsetsBcr_.reserve(-firstCrossing_ + lastCrossing_ + 1);
0188 }
0189 
0190 template <typename T>
0191 inline void CrossingFrame<T>::swap(CrossingFrame<T>& other) {
0192   std::swap(firstCrossing_, other.firstCrossing_);
0193   std::swap(lastCrossing_, other.lastCrossing_);
0194   std::swap(bunchSpace_, other.bunchSpace_);
0195   subdet_.swap(other.subdet_);
0196   std::swap(id_, other.id_);
0197   std::swap(idFirstPileup_, other.idFirstPileup_);
0198   std::swap(pileupFileNr_, other.pileupFileNr_);
0199   std::swap(maxNbSources_, other.maxNbSources_);
0200   signals_.swap(other.signals_);
0201   pileups_.swap(other.pileups_);
0202   shPtrPileups_.swap(other.shPtrPileups_);
0203   shPtrPileups2_.swap(other.shPtrPileups2_);
0204   shPtrPileupsPCF_.swap(other.shPtrPileupsPCF_);
0205   pileupOffsetsBcr_.swap(other.pileupOffsetsBcr_);
0206   pileupOffsetsSource_.resize(maxNbSources_);
0207   for (unsigned int i = 0; i < pileupOffsetsSource_.size(); ++i) {
0208     pileupOffsetsSource_[i].swap(other.pileupOffsetsSource_[i]);
0209   }
0210 }
0211 
0212 template <typename T>
0213 inline CrossingFrame<T>& CrossingFrame<T>::operator=(CrossingFrame<T> const& rhs) {
0214   CrossingFrame<T> temp(rhs);
0215   this->swap(temp);
0216   return *this;
0217 }
0218 
0219 template <class T>
0220 void CrossingFrame<T>::getPileups(typename std::vector<const T*>::const_iterator& first,
0221                                   typename std::vector<const T*>::const_iterator& last) const {
0222   first = pileups_.begin();
0223   last = pileups_.end();
0224 }
0225 
0226 template <class T>
0227 void CrossingFrame<T>::print(int level) const {}
0228 
0229 template <class T>
0230 int CrossingFrame<T>::getSourceType(unsigned int ip) const {
0231   // ip is position in the pileup vector
0232   // decide to which source belongs object with index ip in the pileup vector
0233   // pileup=0, cosmics=1, beam halo+ =2, beam halo- =3 forward =4
0234   unsigned int bcr = getBunchCrossing(ip) - firstCrossing_;  //starts at 0
0235   for (unsigned int i = 0; i < pileupOffsetsSource_.size() - 1; ++i) {
0236     if (ip >= (pileupOffsetsSource_[i])[bcr] && ip < (pileupOffsetsSource_[i + 1])[bcr])
0237       return i;
0238   }
0239   return pileupOffsetsSource_.size() - 1;
0240 }
0241 
0242 template <class T>
0243 int CrossingFrame<T>::getBunchCrossing(unsigned int ip) const {
0244   // return the bcr for a certain position in the pileup vector
0245   for (unsigned int ii = 1; ii < pileupOffsetsBcr_.size(); ii++) {
0246     if (ip >= pileupOffsetsBcr_[ii - 1] && ip < pileupOffsetsBcr_[ii])
0247       return ii + firstCrossing_ - 1;
0248   }
0249   if (ip < pileups_.size())
0250     return lastCrossing_;
0251   else
0252     return 999;
0253 }
0254 
0255 // Free swap function
0256 template <typename T>
0257 inline void swap(CrossingFrame<T>& lhs, CrossingFrame<T>& rhs) {
0258   lhs.swap(rhs);
0259 }
0260 
0261 #include <iosfwd>
0262 #include <iostream>
0263 
0264 template <class T>
0265 std::ostream& operator<<(std::ostream& o, const CrossingFrame<T>& cf) {
0266   std::pair<int, int> range = cf.getBunchRange();
0267   o << "\nCrossingFrame for subdet " << cf.getEventID() << ",  bunchrange = " << range.first << "," << range.second
0268     << ", bunchSpace " << cf.getBunchSpace();
0269 
0270   return o;
0271 }
0272 
0273 #include "SimDataFormats/CrossingFrame/interface/PCrossingFrame.h"
0274 template <class T>
0275 void CrossingFrame<T>::setPileupPtr(std::shared_ptr<edm::Wrapper<PCrossingFrame<T> > const> shPtr) {
0276   shPtrPileupsPCF_ = shPtr;
0277 }
0278 
0279 template <class T>
0280 void CrossingFrame<T>::addPileups(T const& product) {
0281   // default, valid for HepMCProduct
0282   pileups_.push_back(&product);
0283 }
0284 
0285 #ifndef __GCCXML__
0286 template <class T>
0287 void CrossingFrame<T>::addPileups(std::vector<T> const& product) {
0288   for (auto const& item : product) {
0289     pileups_.push_back(&item);
0290   }
0291 }
0292 #endif
0293 
0294 template <class T>
0295 void CrossingFrame<T>::addSignals(const std::vector<T>* vec, edm::EventID id) {
0296   // valid (called) for all except HepMCProduct
0297   id_ = id;
0298   for (unsigned int i = 0; i < vec->size(); ++i) {
0299     signals_.push_back(&((*vec)[i]));
0300   }
0301 }
0302 
0303 template <class T>
0304 void CrossingFrame<T>::addSignals(const T* product, edm::EventID id) {
0305   // valid (called) for all except HepMCProduct
0306   id_ = id;
0307   signals_.push_back(product);
0308 }
0309 
0310 template <class T>
0311 void CrossingFrame<T>::setTof() {
0312   ;
0313 }
0314 
0315 #endif