CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MixCollection.h
Go to the documentation of this file.
1 #ifndef MIX_COLLECTION_H
2 #define MIX_COLLECTION_H
3 #include <utility>
4 #include <string>
5 #include <vector>
6 
8 
9 template <class T>
11 private:
12 public:
13  typedef std::pair<int, int> range;
14  MixCollection();
15  MixCollection(const CrossingFrame<T> *cf, const range bunchRange = range(-999, 999));
16  MixCollection(const std::vector<const CrossingFrame<T> *> &cfs, const range bunchRange = range(-999, 999));
17 
18  range bunchrange() const { return bunchRange_; }
19  int size() const { return sizeSignal() + sizePileup(); }
20  int sizePileup() const;
21  int sizeSignal() const;
22  // false if at least one of the subdetectors was not found in registry
23  bool inRegistry() const { return inRegistry_; }
24 
25  // get object the index of which -in the whole collection- is known
26  const T &getObject(unsigned int ip) const {
27  if (ip >= (unsigned int)size())
28  throw cms::Exception("BadIndex")
29  << "MixCollection::getObject called with an invalid index!"; // ip >= 0, since ip is unsigned
30  int n = ip;
31  /*
32 - int iframe=0;
33 - for (unsigned int ii=0;ii<crossingFrames_.size();++ii) {
34 - iframe=ii;
35 - int s=crossingFrames_[iframe]->getNrSignals()+crossingFrames_[iframe]->getNrPileups();
36 - if (n<s) break;
37 */
38  for (unsigned int iframe = 0; iframe < crossingFrames_.size(); ++iframe) {
39  int s = crossingFrames_[iframe]->getNrSignals();
40  if (n < s)
41  return crossingFrames_[iframe]->getObject(n);
42  n = n - s;
43  }
44  /*
45  return crossingFrames_[iframe]->getObject(n);
46 */
47  for (unsigned int iframe = 0; iframe < crossingFrames_.size(); ++iframe) {
48  int s = crossingFrames_[iframe]->getNrSignals();
49  int p = crossingFrames_[iframe]->getNrPileups();
50  if (n < p)
51  return crossingFrames_[iframe]->getObject(s + n);
52  n = n - p;
53  }
54  throw cms::Exception("InternalError") << "MixCollection::getObject reached impossible condition";
55  }
56 
57  class MixItr;
58  friend class MixItr;
59 
60  // nested class for iterator
61  class MixItr {
62  public:
63  struct end_tag {};
64 
66  MixItr() : first_(true), internalCtr_(0) { ; }
67  MixItr(const MixCollection *shc, int nrDets, end_tag) : internalCtr2_(0) {
68  for (int i = 0; i < nrDets; ++i) {
69  const auto &cf = shc->crossingFrames_[i];
70  internalCtr2_ += cf->getSignal().size() + cf->getPileups().size();
71  }
72  }
73  MixItr(const MixCollection *shc, int nrDets)
74  : mixCol_(shc), nrDets_(nrDets), first_(true), iSignal_(0), iPileup_(0), internalCtr_(0) {
75  ;
76  }
77 
79  virtual ~MixItr() { ; }
80 
82  // default version valid for HepMCProduct
83  const T *operator->() const { return *(pMixItr_.operator->()); }
84  const T &operator*() const { return *(pMixItr_.operator*()); }
85  const MixItr operator++() { return next(); }
86  const MixItr operator++(int) { return next(); }
87  bool operator!=(const MixItr &itr) { return internalCtr2_ != itr.internalCtr2_; }
88 
90  int bunch() const {
91  if (trigger_)
92  return 0;
93  int bcr = myCF_->getBunchCrossing(internalCtr_);
94  return bcr;
95  }
96 
97  bool getTrigger() const { return trigger_; }
98 
99  int getSourceType() const { return (getTrigger() ? -1 : myCF_->getSourceType(internalCtr_)); }
100  int getPileupEventNr() const { return (getTrigger() ? 0 : myCF_->getPileupEventNr(internalCtr_)); }
101 
102  private:
103  typename std::vector<const T *>::const_iterator pMixItr_;
104  typename std::vector<const T *>::const_iterator pMixItrEnd_;
105 
108  int nrDets_;
109  bool first_;
111  bool trigger_;
112  unsigned int internalCtr_; //this is the internal counter pointing into the vector of piled up objects
113  unsigned int internalCtr2_ =
114  0; // this is the internal counter for the number of iterated elements, needed for operator!=
115 
116  const MixItr next();
117  void reset() { ; }
118  bool getNewSignal(typename std::vector<const T *>::const_iterator &first,
119  typename std::vector<const T *>::const_iterator &last);
120 
121  bool getNewPileups(typename std::vector<const T *>::const_iterator &first,
122  typename std::vector<const T *>::const_iterator &last);
123  };
124 
125  typedef MixItr iterator;
126  iterator begin() const;
127  iterator end() const;
128 
129 private:
130  void init(const range bunchRange);
131 
134  int nrDets_;
135 
136  std::vector<const CrossingFrame<T> *> crossingFrames_;
137 };
138 
140 //
141 // Exceptions
142 //
144 template <class T>
145 MixCollection<T>::MixCollection() : bunchRange_(0, 0), inRegistry_(false), nrDets_(0) {
146  crossingFrames_.push_back(NULL);
147 }
148 
149 template <class T>
150 MixCollection<T>::MixCollection(const CrossingFrame<T> *cf, const std::pair<int, int> bunchRange)
151  : inRegistry_(false), nrDets_(0) {
152  nrDets_ = 1;
153  inRegistry_ = true;
154  if (cf) {
155  crossingFrames_.push_back(cf);
156  init(bunchRange);
157  } else
158  throw cms::Exception("InvalidPtr") << "Could not construct MixCollection for " << typeid(T).name()
159  << ", pointer to CrossingFrame invalid!";
160 }
161 
162 template <class T>
163 MixCollection<T>::MixCollection(const std::vector<const CrossingFrame<T> *> &cfs, const std::pair<int, int> bunchRange)
164  : inRegistry_(false), nrDets_(0) {
165  // first, verify that all CrossingFrames have the same bunchrange
166  range bR = cfs[0]->getBunchRange();
167  for (unsigned int i = 1; i < cfs.size(); ++i) {
168  if (bR != cfs[i]->getBunchRange())
169  throw cms::Exception("Incompatible CrossingFrames")
170  << "You gave as input CrossingFrames with different bunchRanges!";
171  }
172 
173  //set necessary variables
174  for (unsigned int i = 0; i < cfs.size(); ++i) {
175  nrDets_++;
176  crossingFrames_.push_back(cfs[i]);
177  inRegistry_ = true; // true if at least one is present
178  }
179 
180  init(bunchRange);
181 }
182 
183 template <class T>
184 void MixCollection<T>::init(const std::pair<int, int> bunchRange) {
185  bunchRange_ = bunchRange;
186 
187  //verify whether bunchrange is ok
188  // in case of several crossingFrames, we have verified before that they have the same bunchrange
189  range defaultrange = crossingFrames_[0]->getBunchRange();
190  if (bunchRange_ == range(-999, 999))
191  bunchRange_ = defaultrange;
192  else if (bunchRange_ != defaultrange) {
193  int first = defaultrange.first;
194  int last = defaultrange.second;
195  if (bunchRange_.first < defaultrange.first || bunchRange_.second > defaultrange.second)
196  throw cms::Exception("BadRunRange")
197  << " You are asking for a runrange (" << bunchRange_.first << "," << bunchRange_.second
198  << "), outside of the existing runrange (" << defaultrange.first << ", " << defaultrange.second << ")\n";
199  bunchRange_ = range(first, last);
200  }
201 }
202 
203 template <class T>
205  // get size cumulated for all subdetectors
206  int s = 0;
207  for (int i = 0; i < nrDets_; ++i) {
208  s += crossingFrames_[i]->getNrPileups();
209  }
210  return s;
211 }
212 
213 template <class T>
215  int s = 0;
216  for (int i = 0; i < nrDets_; ++i) {
217  s += crossingFrames_[i]->getNrSignals();
218  }
219  return s;
220 }
221 
222 template <class T>
223 bool MixCollection<T>::MixItr::getNewSignal(typename std::vector<const T *>::const_iterator &first,
224  typename std::vector<const T *>::const_iterator &last) {
225  // gets the next signal collection with non-zero size
226 
227  while (iSignal_ < nrDets_) {
228  mixCol_->crossingFrames_[iSignal_]->getSignal(first, last);
230  iSignal_++;
231  if (first != last)
232  return true;
233  }
234  return false;
235 }
236 
237 template <class T>
238 bool MixCollection<T>::MixItr::getNewPileups(typename std::vector<const T *>::const_iterator &first,
239  typename std::vector<const T *>::const_iterator &last) {
240  // gets the next pileup collection , changing subdet if necessary
241  while (iPileup_ < nrDets_) {
242  mixCol_->crossingFrames_[iPileup_]->getPileups(first, last);
243  int s = 0;
244  for (typename std::vector<const T *>::const_iterator it = first; it != last; it++) {
245  s++;
246  }
247  myCF_ = mixCol_->crossingFrames_[iPileup_];
248  iPileup_++;
249  if (first != last)
250  return true;
251  }
252  return false;
253 }
254 
255 template <class T>
257  // initialisation
258  if (first_) {
259  first_ = false;
260  trigger_ = true;
261  } else {
262  ++internalCtr2_;
263  if (!trigger_)
264  internalCtr_++;
265  if (++pMixItr_ != pMixItrEnd_)
266  return *this;
267  }
268 
269  // we have an end condition, look whether there are more collections
270  bool ok;
271  if (trigger_) {
272  ok = this->getNewSignal(pMixItr_, pMixItrEnd_);
273  if (ok)
274  return *this;
275  trigger_ = false;
276  }
277  ok = this->getNewPileups(pMixItr_, pMixItrEnd_);
278  if (ok) {
279  // debug print start
280  // for (auto dbIt=pMixItr_;dbIt!=pMixItrEnd_;++dbIt) printf("Found pointer %p\n",(*dbIt));fflush(stdout);
281  // debug print end
282  internalCtr_ = 0;
283  }
284  return *this; // with internalCtr2_ we can always return *this
285 }
286 
287 template <class T>
289  return MixItr(this, nrDets_)++;
290 }
291 
292 template <class T>
294  return MixItr(this, nrDets_, typename MixItr::end_tag());
295 }
296 
297 #include <iosfwd>
298 #include <iostream>
299 template <class T>
300 std::ostream &operator<<(std::ostream &o, const MixCollection<T> &col) {
301  o << "MixCollection with bunchRange: " << (col.bunchrange()).first << "," << (col.bunchrange()).second
302  << " size of signal: " << col.sizeSignal() << " ,size of pileup: " << col.sizePileup();
303 
304  return o;
305 }
306 
307 #endif
const CrossingFrame< T > * myCF_
const T * operator->() const
Definition: MixCollection.h:83
const MixCollection * mixCol_
std::pair< int, int > range
Definition: MixCollection.h:13
const MixItr operator++(int)
Definition: MixCollection.h:86
bool inRegistry() const
Definition: MixCollection.h:23
#define NULL
Definition: scimark2.h:8
MixItr(const MixCollection *shc, int nrDets, end_tag)
Definition: MixCollection.h:67
const T & operator*() const
Definition: MixCollection.h:84
int size() const
Definition: MixCollection.h:19
bool getTrigger() const
Definition: MixCollection.h:97
const uint16_t range(const Frame &aFrame)
MixItr(const MixCollection *shc, int nrDets)
Definition: MixCollection.h:73
U second(std::pair< T, U > const &p)
std::vector< const CrossingFrame< T > * > crossingFrames_
bool operator!=(const MixItr &itr)
Definition: MixCollection.h:87
int sizeSignal() const
bool getNewSignal(typename std::vector< const T * >::const_iterator &first, typename std::vector< const T * >::const_iterator &last)
unsigned int internalCtr2_
iterator end() const
bool getNewPileups(typename std::vector< const T * >::const_iterator &first, typename std::vector< const T * >::const_iterator &last)
std::vector< const T * >::const_iterator pMixItr_
const T & getObject(unsigned int ip) const
Definition: MixCollection.h:26
std::vector< const T * >::const_iterator pMixItrEnd_
const std::vector< const T * > & getSignal() const
Definition: CrossingFrame.h:90
const std::vector< const T * > & getPileups() const
Definition: CrossingFrame.h:89
int getPileupEventNr() const
int getSourceType() const
Definition: MixCollection.h:99
int sizePileup() const
tuple last
Definition: dqmdumpme.py:56
iterator begin() const
int col
Definition: cuy.py:1009
long double T
void init(const range bunchRange)
unsigned int internalCtr_
const MixItr operator++()
Definition: MixCollection.h:85
range bunchrange() const
Definition: MixCollection.h:18
friend class MixItr
Definition: MixCollection.h:57
const MixItr next()