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