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