CMS 3D CMS Logo

CrossingFrame.h
Go to the documentation of this file.
1 #ifndef CROSSING_FRAME_H
2 #define CROSSING_FRAME_H
3 
21 
25 
26 template <class T>
28 
29 #include <vector>
30 #include <string>
31 #include <iostream>
32 #include <utility>
33 #include <algorithm>
34 #include <memory>
35 
36 template <class T>
38 public:
39  // con- and destructors
40 
42  CrossingFrame(int minb, int maxb, int bunchsp, std::string subdet, unsigned int maxNbSources);
43 
45 
46  void swap(CrossingFrame& other);
47 
49 
50  //standard version
51  void addSignals(const std::vector<T>* vec, edm::EventID id);
52  // version for HepMCProduct
53  void addSignals(const T* vec, edm::EventID id);
54 
55  // standard version
56  void addPileups(std::vector<T> const& vec);
57  // version for HepMCProduct
58  void addPileups(T const& product);
59 
60  void setTof();
61 
62  // we keep the shared pointer in the object that will be only destroyed at the end of the event (transient object!)
63  // because of HepMCProduct, we need 2 versions...
64  /* void setPileupPtr(std::shared_ptr<edm::Wrapper<std::vector<T> > const> shPtr) {shPtrPileups_=shPtr;} */
65  /* void setPileupPtr(std::shared_ptr<edm::Wrapper<T> const> shPtr) {shPtrPileups2_=shPtr;} */
66  void setPileupPtr(std::shared_ptr<edm::Wrapper<std::vector<T> > const> shPtr) { shPtrPileups_.push_back(shPtr); }
67  void setPileupPtr(std::shared_ptr<edm::Wrapper<T> const> shPtr) { shPtrPileups2_.push_back(shPtr); }
68  // used in the Step2 to set the PCrossingFrame
69  void setPileupPtr(std::shared_ptr<edm::Wrapper<PCrossingFrame<T> > const> shPtr);
70 
71  void print(int level = 0) const;
72 
73  void setBcrOffset() { pileupOffsetsBcr_.push_back(pileups_.size()); }
74 
75  void setSourceOffset(const unsigned int s) { pileupOffsetsSource_[s].push_back(pileups_.size()); }
76 
77  //getters
78  edm::EventID getEventID() const { return id_; }
79  std::pair<int, int> getBunchRange() const { return std::pair<int, int>(firstCrossing_, lastCrossing_); }
80  int getBunchSpace() const { return bunchSpace_; }
81  unsigned int getMaxNbSources() const { return maxNbSources_; }
82  std::string getSubDet() const { return subdet_; }
83  unsigned int getPileupFileNr() const { return pileupFileNr_; }
85  const std::vector<unsigned int>& getPileupOffsetsBcr() const { return pileupOffsetsBcr_; }
86  const std::vector<std::vector<unsigned int> >& getPileupOffsetsSource() const {
87  return pileupOffsetsSource_;
88  } //one per source
89  const std::vector<const T*>& getPileups() const { return pileups_; }
90  const std::vector<const T*>& getSignal() const { return signals_; }
91 
92  void getSignal(typename std::vector<const T*>::const_iterator& first,
93  typename std::vector<const T*>::const_iterator& last) const {
94  first = signals_.begin();
95  last = signals_.end();
96  }
97  void getPileups(typename std::vector<const T*>::const_iterator& first,
98  typename std::vector<const T*>::const_iterator& last) const;
99  unsigned int getNrSignals() const { return signals_.size(); }
100  unsigned int getNrPileups() const { return pileups_.size(); }
101  unsigned int getNrPileups(int bcr) const {
104  }
105 
106  // get pileup information in dependency from internal pointer
107  int getBunchCrossing(unsigned int ip) const;
108 
109  int getSourceType(unsigned int ip) const;
110 
111  // get object in pileup when position in the vector is known (for DigiSimLink typically)
112 
113  const T& getObject(unsigned int ip) const {
114  //ip is position in the MixCollection (i.e. signal + pileup)
115  if (ip > getNrSignals() + getNrPileups())
116  throw cms::Exception("BadIndex") << "CrossingFrame::getObject called with an invalid index- index was " << ip
117  << "!"; // ip >=0, since ip is unsigned
118  if (ip < getNrSignals()) {
119  return *(signals_[ip]);
120  } else {
121  return *(pileups_[ip - getNrSignals()]);
122  }
123  }
124 
125  // setters needed for step2 when using mixed secondary source
126  void setEventID(edm::EventID evId) { id_ = evId; }
127  void setPileups(const std::vector<const T*>& p) { pileups_ = p; }
128  void setBunchSpace(int bSpace) { bunchSpace_ = bSpace; }
129  void setMaxNbSources(unsigned int mNbS) { maxNbSources_ = mNbS; }
130  void setSubDet(std::string det) { subdet_ = det; }
131  void setPileupFileNr(unsigned int pFileNr) { pileupFileNr_ = pFileNr; }
133  void setPileupOffsetsBcr(const std::vector<unsigned int>& pOffsetsBcr) { pileupOffsetsBcr_ = pOffsetsBcr; }
134  void setPileupOffsetsSource(const std::vector<std::vector<unsigned int> >& pOffsetsS) {
135  pileupOffsetsSource_ = pOffsetsS;
136  } //one per source
137  void setBunchRange(std::pair<int, int> bunchRange) {
138  firstCrossing_ = bunchRange.first;
139  lastCrossing_ = bunchRange.second;
140  }
141 
142 private:
143  // please update the swap() function below if any data members are added.
144  // general information
147  int bunchSpace_; //in nsec
148  std::string subdet_; // for PSimHits/PCaloHits
149  edm::EventID id_; // event id of the signal event
150 
151  // for playback option
152  edm::EventID idFirstPileup_; // EventId fof the first pileup event used for this signal event
153  unsigned int pileupFileNr_; // ordinal number of the pileup file this event was in
154 
155  unsigned int maxNbSources_;
156 
157  // signal
158  std::vector<const T*> signals_;
159 
160  //pileup
161  std::vector<const T*> pileups_;
162  std::vector<std::shared_ptr<edm::Wrapper<std::vector<T> > const> > shPtrPileups_;
163  std::vector<std::shared_ptr<edm::Wrapper<T> const> > shPtrPileups2_; // fore HepMCProduct
164  /* std::shared_ptr<edm::Wrapper<std::vector<T> > const> shPtrPileups_; */
165  /* std::shared_ptr<edm::Wrapper<T> const> shPtrPileups2_; // fore HepMCProduct */
166  std::shared_ptr<edm::Wrapper<PCrossingFrame<T> > const> shPtrPileupsPCF_;
167  // std::shared_ptr<edm::Wrapper<PCrossingFrame<edm::HepMCProduct> const> shPtrPileupsHepMCProductPCF_;
168 
169  // these are informations stored in order to be able to have information
170  // as a function of the position of an object in the pileups_ vector
171  std::vector<unsigned int> pileupOffsetsBcr_;
172  std::vector<std::vector<unsigned int> > pileupOffsetsSource_; //one per source
173 };
174 
175 //==============================================================================
176 // implementations
177 //==============================================================================
178 
179 template <class T>
180 CrossingFrame<T>::CrossingFrame(int minb, int maxb, int bunchsp, std::string subdet, unsigned int maxNbSources)
181  : firstCrossing_(minb), lastCrossing_(maxb), bunchSpace_(bunchsp), subdet_(subdet), maxNbSources_(maxNbSources) {
183  for (unsigned int i = 0; i < maxNbSources_; ++i)
185 
186  //FIXME: should we force around 0 or so??
188 }
189 
190 template <typename T>
192  std::swap(firstCrossing_, other.firstCrossing_);
193  std::swap(lastCrossing_, other.lastCrossing_);
194  std::swap(bunchSpace_, other.bunchSpace_);
195  subdet_.swap(other.subdet_);
196  std::swap(id_, other.id_);
197  std::swap(idFirstPileup_, other.idFirstPileup_);
198  std::swap(pileupFileNr_, other.pileupFileNr_);
199  std::swap(maxNbSources_, other.maxNbSources_);
200  signals_.swap(other.signals_);
201  pileups_.swap(other.pileups_);
202  shPtrPileups_.swap(other.shPtrPileups_);
203  shPtrPileups2_.swap(other.shPtrPileups2_);
204  shPtrPileupsPCF_.swap(other.shPtrPileupsPCF_);
205  pileupOffsetsBcr_.swap(other.pileupOffsetsBcr_);
206  pileupOffsetsSource_.resize(maxNbSources_);
207  for (unsigned int i = 0; i < pileupOffsetsSource_.size(); ++i) {
208  pileupOffsetsSource_[i].swap(other.pileupOffsetsSource_[i]);
209  }
210 }
211 
212 template <typename T>
214  CrossingFrame<T> temp(rhs);
215  this->swap(temp);
216  return *this;
217 }
218 
219 template <class T>
220 void CrossingFrame<T>::getPileups(typename std::vector<const T*>::const_iterator& first,
221  typename std::vector<const T*>::const_iterator& last) const {
222  first = pileups_.begin();
223  last = pileups_.end();
224 }
225 
226 template <class T>
227 void CrossingFrame<T>::print(int level) const {}
228 
229 template <class T>
230 int CrossingFrame<T>::getSourceType(unsigned int ip) const {
231  // ip is position in the pileup vector
232  // decide to which source belongs object with index ip in the pileup vector
233  // pileup=0, cosmics=1, beam halo+ =2, beam halo- =3 forward =4
234  unsigned int bcr = getBunchCrossing(ip) - firstCrossing_; //starts at 0
235  for (unsigned int i = 0; i < pileupOffsetsSource_.size() - 1; ++i) {
236  if (ip >= (pileupOffsetsSource_[i])[bcr] && ip < (pileupOffsetsSource_[i + 1])[bcr])
237  return i;
238  }
239  return pileupOffsetsSource_.size() - 1;
240 }
241 
242 template <class T>
243 int CrossingFrame<T>::getBunchCrossing(unsigned int ip) const {
244  // return the bcr for a certain position in the pileup vector
245  for (unsigned int ii = 1; ii < pileupOffsetsBcr_.size(); ii++) {
246  if (ip >= pileupOffsetsBcr_[ii - 1] && ip < pileupOffsetsBcr_[ii])
247  return ii + firstCrossing_ - 1;
248  }
249  if (ip < pileups_.size())
250  return lastCrossing_;
251  else
252  return 999;
253 }
254 
255 // Free swap function
256 template <typename T>
257 inline void swap(CrossingFrame<T>& lhs, CrossingFrame<T>& rhs) {
258  lhs.swap(rhs);
259 }
260 
261 #include <iosfwd>
262 #include <iostream>
263 
264 template <class T>
265 std::ostream& operator<<(std::ostream& o, const CrossingFrame<T>& cf) {
266  std::pair<int, int> range = cf.getBunchRange();
267  o << "\nCrossingFrame for subdet " << cf.getEventID() << ", bunchrange = " << range.first << "," << range.second
268  << ", bunchSpace " << cf.getBunchSpace();
269 
270  return o;
271 }
272 
274 template <class T>
275 void CrossingFrame<T>::setPileupPtr(std::shared_ptr<edm::Wrapper<PCrossingFrame<T> > const> shPtr) {
276  shPtrPileupsPCF_ = shPtr;
277 }
278 
279 template <class T>
280 void CrossingFrame<T>::addPileups(T const& product) {
281  // default, valid for HepMCProduct
282  pileups_.push_back(&product);
283 }
284 
285 #ifndef __GCCXML__
286 template <class T>
287 void CrossingFrame<T>::addPileups(std::vector<T> const& product) {
288  for (auto const& item : product) {
289  pileups_.push_back(&item);
290  }
291 }
292 #endif
293 
294 template <class T>
295 void CrossingFrame<T>::addSignals(const std::vector<T>* vec, edm::EventID id) {
296  // valid (called) for all except HepMCProduct
297  id_ = id;
298  for (unsigned int i = 0; i < vec->size(); ++i) {
299  signals_.push_back(&((*vec)[i]));
300  }
301 }
302 
303 template <class T>
304 void CrossingFrame<T>::addSignals(const T* product, edm::EventID id) {
305  // valid (called) for all except HepMCProduct
306  id_ = id;
307  signals_.push_back(product);
308 }
309 
310 template <class T>
312  ;
313 }
314 
315 #endif
void addSignals(const std::vector< T > *vec, edm::EventID id)
void addPileups(std::vector< T > const &vec)
void setEventID(edm::EventID evId)
void print(int level=0) const
const std::vector< const T * > & getPileups() const
Definition: CrossingFrame.h:89
unsigned int getNrPileups() const
int getSourceType(unsigned int ip) const
void setMaxNbSources(unsigned int mNbS)
unsigned int getPileupFileNr() const
Definition: CrossingFrame.h:83
void getSignal(typename std::vector< const T *>::const_iterator &first, typename std::vector< const T *>::const_iterator &last) const
Definition: CrossingFrame.h:92
void setPileupFileNr(unsigned int pFileNr)
void setIdFirstPileup(edm::EventID idFP)
unsigned int getNrPileups(int bcr) const
edm::EventID getEventID() const
Definition: CrossingFrame.h:78
void setSubDet(std::string det)
edm::EventID id_
unsigned int maxNbSources_
std::vector< std::shared_ptr< edm::Wrapper< T > const > > shPtrPileups2_
unsigned int getNrSignals() const
Definition: CrossingFrame.h:99
edm::EventID idFirstPileup_
int getBunchCrossing(unsigned int ip) const
void setBunchRange(std::pair< int, int > bunchRange)
std::vector< unsigned int > pileupOffsetsBcr_
void setPileupPtr(std::shared_ptr< edm::Wrapper< std::vector< T > > const > shPtr)
Definition: CrossingFrame.h:66
void setPileupOffsetsBcr(const std::vector< unsigned int > &pOffsetsBcr)
std::shared_ptr< edm::Wrapper< PCrossingFrame< T > > const > shPtrPileupsPCF_
void setBunchSpace(int bSpace)
void setPileupOffsetsSource(const std::vector< std::vector< unsigned int > > &pOffsetsS)
int getBunchSpace() const
Definition: CrossingFrame.h:80
void setBcrOffset()
Definition: CrossingFrame.h:73
std::vector< std::vector< unsigned int > > pileupOffsetsSource_
void setPileupPtr(std::shared_ptr< edm::Wrapper< T > const > shPtr)
Definition: CrossingFrame.h:67
std::string getSubDet() const
Definition: CrossingFrame.h:82
ii
Definition: cuy.py:589
const T & getObject(unsigned int ip) const
const std::vector< const T * > & getSignal() const
Definition: CrossingFrame.h:90
const std::vector< std::vector< unsigned int > > & getPileupOffsetsSource() const
Definition: CrossingFrame.h:86
std::string subdet_
edm::EventID getIdFirstPileup() const
Definition: CrossingFrame.h:84
const std::vector< unsigned int > & getPileupOffsetsBcr() const
Definition: CrossingFrame.h:85
void swap(CrossingFrame &other)
std::pair< int, int > getBunchRange() const
Definition: CrossingFrame.h:79
unsigned int getMaxNbSources() const
Definition: CrossingFrame.h:81
unsigned int pileupFileNr_
std::vector< const T * > pileups_
void setPileups(const std::vector< const T *> &p)
long double T
void setSourceOffset(const unsigned int s)
Definition: CrossingFrame.h:75
std::vector< const T * > signals_
CrossingFrame & operator=(CrossingFrame const &rhs)
std::vector< std::shared_ptr< edm::Wrapper< std::vector< T > > const > > shPtrPileups_
void swap(CrossingFrame< T > &lhs, CrossingFrame< T > &rhs)