test
CMS 3D CMS Logo

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