CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BoxND.h
Go to the documentation of this file.
1 #ifndef NPSTAT_BOXND_HH_
2 #define NPSTAT_BOXND_HH_
3 
14 #include <vector>
15 
16 #include "Alignment/Geners/interface/ClassId.hh"
18 
19 namespace npstat {
23  template <typename Numeric>
24  struct BoxND : public std::vector<Interval<Numeric> >
25  {
27  inline BoxND() {}
28 
30  inline explicit BoxND(const unsigned long dim) :
31  std::vector<Interval<Numeric> >(dim) {}
32 
34  inline BoxND(const unsigned long dim, const Interval<Numeric>& v) :
35  std::vector<Interval<Numeric> >(dim, v) {}
36 
42  template <typename Num2>
43  explicit BoxND(const std::vector<Num2>& limits);
44 
46  template <typename Num2>
47  explicit BoxND(const BoxND<Num2>& r);
48 
53  template <typename Num2>
54  BoxND& copyFrom(const BoxND<Num2>& r);
55 
57  inline unsigned long dim() const {return this->size();}
58 
60  Numeric volume() const;
61 
66  void getMidpoint(Numeric* coord, unsigned long coordLen) const;
67 
69 
74  template <typename Num2>
75  bool isInsideLower(const Num2* coord, unsigned long coordLen) const;
76  template <typename Num2>
77  bool isInsideUpper(const Num2* coord, unsigned long coordLen) const;
78  template <typename Num2>
79  bool isInsideWithBounds(const Num2* coord, unsigned long coordLen) const;
80  template <typename Num2>
81  bool isInside(const Num2* coord, unsigned long coordLen) const;
83 
85 
86  BoxND& operator*=(double r);
87  BoxND& operator/=(double r);
89 
91 
92  BoxND& operator*=(const std::vector<double>& scales);
93  BoxND& operator/=(const std::vector<double>& scales);
95 
100  BoxND& expand(double r);
101 
103 
108  BoxND& expand(const std::vector<double>& scales);
109  BoxND& expand(const double* scales, unsigned long lenScales);
111 
113 
114  template <typename Num2>
115  BoxND& operator+=(const std::vector<Num2>& shifts);
116  template <typename Num2>
117  BoxND& operator-=(const std::vector<Num2>& shifts);
118  template <typename Num2>
119  BoxND& shift(const Num2* shifts, unsigned long lenShifts);
121 
123  BoxND& moveToOrigin();
124 
126  Numeric overlapVolume(const BoxND& r) const;
127 
129  double overlapFraction(const BoxND& r) const;
130 
132  static BoxND unitBox(unsigned long ndim);
133 
139  static BoxND sizeTwoBox(unsigned long ndim);
140 
146  static BoxND allSpace(unsigned long ndim);
147 
149 
150  inline gs::ClassId classId() const {return gs::ClassId(*this);}
151  bool write(std::ostream& of) const;
153 
154  static const char* classname();
155  static inline unsigned version() {return 1;}
156  static void restore(const gs::ClassId& id, std::istream& in, BoxND* box);
157  };
158 }
159 
161 
162 template <typename Numeric>
164 
165 template <typename Numeric>
168 
169 #include <limits>
170 #include <cassert>
172 
173 #include "Alignment/Geners/interface/GenericIO.hh"
174 
175 namespace npstat {
176  template <typename Numeric>
177  template <typename Num2>
179  {
180  const unsigned long dim = r.size();
181  if (dim)
182  {
183  this->reserve(dim);
184  for (unsigned long i=0; i<dim; ++i)
185  {
186  const Interval<Num2>& ri(r[i]);
187  this->push_back(Interval<Numeric>(ri.min(), ri.max()));
188  }
189  }
190  }
191 
192  template <typename Numeric>
193  template <typename Num2>
194  BoxND<Numeric>::BoxND(const std::vector<Num2>& limits)
195  {
196  const unsigned long dim = limits.size();
197  if (dim)
198  {
199  this->reserve(dim);
200  Numeric zero = Numeric();
201  for (unsigned long i=0; i<dim; ++i)
202  {
203  const Numeric value(static_cast<Numeric>(limits[i]));
204  if (value >= zero)
205  this->push_back(Interval<Numeric>(zero, value));
206  else
207  this->push_back(Interval<Numeric>(value, zero));
208  }
209  }
210  }
211 
212  template <typename Numeric>
213  template <typename Num2>
215  {
216  if ((void *)this == (void *)(&r))
217  return *this;
218  const unsigned long n = r.size();
219  this->clear();
220  this->reserve(n);
221  for (unsigned long i=0; i<n; ++i)
222  {
223  const Interval<Num2>& ir(r[i]);
224  this->push_back(Interval<Numeric>(ir.min(), ir.max()));
225  }
226  return *this;
227  }
228 
229  template <typename Numeric>
230  Numeric BoxND<Numeric>::volume() const
231  {
232  Numeric v(static_cast<Numeric>(1));
233  const unsigned long mydim = this->size();
234  for (unsigned long i=0U; i<mydim; ++i)
235  v *= (*this)[i].length();
236  return v;
237  }
238 
239  template <typename Numeric>
240  Numeric BoxND<Numeric>::overlapVolume(const BoxND& r) const
241  {
242  const unsigned long mydim = this->size();
243  if (mydim == r.size())
244  {
245  Numeric v(static_cast<Numeric>(1));
246  for (unsigned long i=0U; i<mydim; ++i)
247  v *= (*this)[i].overlapLength(r[i]);
248  return v;
249  }
250  else
251  return static_cast<Numeric>(0);
252  }
253 
254  template <typename Numeric>
256  {
257  const unsigned long mydim = this->size();
258  if (mydim == r.size())
259  {
260  double f = 1.0;
261  for (unsigned long i=0U; i<mydim; ++i)
262  f *= (*this)[i].overlapFraction(r[i]);
263  return f;
264  }
265  else
266  return 0.0;
267  }
268 
269  template <typename Numeric>
270  void BoxND<Numeric>::getMidpoint(Numeric* coord,
271  const unsigned long coordLen) const
272  {
273  const unsigned long mydim = this->size();
274  if (coordLen < mydim) throw npstat::NpstatInvalidArgument(
275  "In npstat::BoxND::getMidpoint: insufficient output buffer length");
276  if (mydim)
277  {
278  assert(coord);
279  for (unsigned long i=0U; i<mydim; ++i)
280  coord[i] = (*this)[i].midpoint();
281  }
282  }
283 
284  template <typename Numeric>
285  template <typename Num2>
286  bool BoxND<Numeric>::isInsideLower(const Num2* coords,
287  const unsigned long coordLen) const
288  {
289  if (coordLen != this->size()) throw npstat::NpstatInvalidArgument(
290  "In npstat::BoxND::isInsideLower: "
291  "incompatible point dimensionality");
292  const Interval<Numeric>* myptr = &(*this)[0];
293  for (unsigned long i=0; i<coordLen; ++i)
294  if (!myptr[i].isInsideLower(coords[i]))
295  return false;
296  return true;
297  }
298 
299  template <typename Numeric>
300  template <typename Num2>
301  bool BoxND<Numeric>::isInsideUpper(const Num2* coords,
302  const unsigned long coordLen) const
303  {
304  if (coordLen != this->size()) throw npstat::NpstatInvalidArgument(
305  "In npstat::BoxND::isInsideUpper: "
306  "incompatible point dimensionality");
307  const Interval<Numeric>* myptr = &(*this)[0];
308  for (unsigned long i=0; i<coordLen; ++i)
309  if (!myptr[i].isInsideUpper(coords[i]))
310  return false;
311  return true;
312  }
313 
314  template <typename Numeric>
315  template <typename Num2>
316  bool BoxND<Numeric>::isInsideWithBounds(const Num2* coords,
317  const unsigned long coordLen) const
318  {
319  if (coordLen != this->size()) throw npstat::NpstatInvalidArgument(
320  "In npstat::BoxND::isInsideWithBounds: "
321  "incompatible point dimensionality");
322  const Interval<Numeric>* myptr = &(*this)[0];
323  for (unsigned long i=0; i<coordLen; ++i)
324  if (!myptr[i].isInsideWithBounds(coords[i]))
325  return false;
326  return true;
327  }
328 
329  template <typename Numeric>
330  template <typename Num2>
331  bool BoxND<Numeric>::isInside(const Num2* coords,
332  const unsigned long coordLen) const
333  {
334  if (coordLen != this->size()) throw npstat::NpstatInvalidArgument(
335  "In npstat::BoxND::isInside: incompatible point dimensionality");
336  const Interval<Numeric>* myptr = &(*this)[0];
337  for (unsigned long i=0; i<coordLen; ++i)
338  if (!myptr[i].isInside(coords[i]))
339  return false;
340  return true;
341  }
342 
343  template <typename Numeric>
345  {
346  const unsigned long mydim = this->size();
347  for (unsigned long i=0; i<mydim; ++i)
348  (*this)[i] *= r;
349  return *this;
350  }
351 
352  template <typename Numeric>
354  {
355  const unsigned long mydim = this->size();
356  for (unsigned long i=0; i<mydim; ++i)
357  (*this)[i].moveMidpointTo0();
358  return *this;
359  }
360 
361  template <typename Numeric>
363  {
364  const unsigned long mydim = this->size();
365  for (unsigned long i=0; i<mydim; ++i)
366  (*this)[i].expand(r);
367  return *this;
368  }
369 
370  template <typename Numeric>
372  const std::vector<double>& scales)
373  {
374  const unsigned long mydim = this->size();
375  if (mydim != scales.size()) throw npstat::NpstatInvalidArgument(
376  "In npstat::BoxND::operator*=: "
377  "incompatible argument dimensionality");
378  for (unsigned long i=0; i<mydim; ++i)
379  (*this)[i] *= scales[i];
380  return *this;
381  }
382 
383  template <typename Numeric>
385  const std::vector<double>& scales)
386  {
387  const unsigned long mydim = this->size();
388  if (mydim != scales.size()) throw npstat::NpstatInvalidArgument(
389  "In npstat::BoxND::expand: incompatible argument dimensionality");
390  for (unsigned long i=0; i<mydim; ++i)
391  (*this)[i].expand(scales[i]);
392  return *this;
393  }
394 
395  template <typename Numeric>
397  const double* scales, const unsigned long lenScales)
398  {
399  const unsigned long mydim = this->size();
400  if (mydim != lenScales) throw npstat::NpstatInvalidArgument(
401  "In npstat::BoxND::expand: incompatible argument dimensionality");
402  if (mydim)
403  {
404  assert(scales);
405  for (unsigned long i=0; i<mydim; ++i)
406  (*this)[i].expand(scales[i]);
407  }
408  return *this;
409  }
410 
411  template <typename Numeric>
413  {
414  const unsigned long mydim = this->size();
415  for (unsigned long i=0; i<mydim; ++i)
416  (*this)[i] /= r;
417  return *this;
418  }
419 
420  template <typename Numeric>
422  const std::vector<double>& scales)
423  {
424  const unsigned long mydim = this->size();
425  if (mydim != scales.size()) throw npstat::NpstatInvalidArgument(
426  "In npstat::BoxND::operator/=: "
427  "incompatible argument dimensionality");
428  for (unsigned long i=0; i<mydim; ++i)
429  (*this)[i] /= scales[i];
430  return *this;
431  }
432 
433  template <typename Numeric>
434  template <typename Num2>
435  BoxND<Numeric>& BoxND<Numeric>::operator+=(const std::vector<Num2>& shifts)
436  {
437  const unsigned long mydim = this->size();
438  if (mydim != shifts.size()) throw npstat::NpstatInvalidArgument(
439  "In npstat::BoxND::operator+=: "
440  "incompatible argument dimensionality");
441  for (unsigned long i=0; i<mydim; ++i)
442  (*this)[i] += static_cast<Numeric>(shifts[i]);
443  return *this;
444  }
445 
446  template <typename Numeric>
447  template <typename Num2>
449  const Num2* shifts, const unsigned long shiftsLen)
450  {
451  const unsigned long mydim = this->size();
452  if (mydim != shiftsLen) throw npstat::NpstatInvalidArgument(
453  "In npstat::BoxND::shift: incompatible argument dimensionality");
454  if (mydim)
455  {
456  assert(shifts);
457  for (unsigned long i=0; i<mydim; ++i)
458  (*this)[i] += static_cast<Numeric>(shifts[i]);
459  }
460  return *this;
461  }
462 
463  template <typename Numeric>
464  template <typename Num2>
465  BoxND<Numeric>& BoxND<Numeric>::operator-=(const std::vector<Num2>& shifts)
466  {
467  const unsigned long mydim = this->size();
468  if (mydim != shifts.size()) throw npstat::NpstatInvalidArgument(
469  "In npstat::BoxND::operator-=: "
470  "incompatible argument dimensionality");
471  for (unsigned long i=0; i<mydim; ++i)
472  (*this)[i] -= static_cast<Numeric>(shifts[i]);
473  return *this;
474  }
475 
476  template <typename Numeric>
477  BoxND<Numeric> BoxND<Numeric>::unitBox(const unsigned long ndim)
478  {
479  Interval<Numeric> unit(static_cast<Numeric>(0),
480  static_cast<Numeric>(1));
481  return BoxND<Numeric>(ndim, unit);
482  }
483 
484  template <typename Numeric>
485  BoxND<Numeric> BoxND<Numeric>::sizeTwoBox(const unsigned long ndim)
486  {
487  const Numeric one = static_cast<Numeric>(1);
488  Interval<Numeric> i(-one, one);
489  return BoxND<Numeric>(ndim, i);
490  }
491 
492  template <typename Numeric>
493  BoxND<Numeric> BoxND<Numeric>::allSpace(const unsigned long ndim)
494  {
495  const Numeric maxval = std::numeric_limits<Numeric>::max();
496  Interval<Numeric> i(-maxval, maxval);
497  return BoxND<Numeric>(ndim, i);
498  }
499 
500  template<typename Numeric>
502  {
503  static const std::string na(gs::template_class_name<Numeric>("npstat::BoxND"));
504  return na.c_str();
505  }
506 
507  template<typename Numeric>
508  bool BoxND<Numeric>::write(std::ostream& of) const
509  {
510  const unsigned long mydim = this->size();
511  std::vector<Numeric> limits;
512  limits.reserve(2UL*mydim);
513  for (unsigned long i=0; i<mydim; ++i)
514  {
515  limits.push_back((*this)[i].min());
516  limits.push_back((*this)[i].max());
517  }
518  return gs::write_item(of, limits);
519  }
520 
521  template<typename Numeric>
522  void BoxND<Numeric>::restore(const gs::ClassId& id, std::istream& in, BoxND* b)
523  {
524  static const gs::ClassId current(gs::ClassId::makeId<BoxND<Numeric> >());
525  current.ensureSameId(id);
526 
527  std::vector<Numeric> limits;
528  gs::restore_item(in, &limits);
529  if (in.fail())
530  throw gs::IOReadFailure("In npstat::BoxND::restore: input stream failure");
531  const unsigned long nlimits = limits.size();
532  if (nlimits % 2UL)
533  throw gs::IOInvalidData("In npstat::BoxND::restore: bad limits");
534  assert(b);
535  b->clear();
536  b->reserve(nlimits/2UL);
537  for (unsigned long i=0; i<nlimits/2UL; ++i)
538  b->push_back(npstat::Interval<Numeric>(limits[2U*i], limits[2U*i+1U]));
539  }
540 }
541 
542 template <typename Numeric>
544 {
545  const unsigned long dim = l.size();
546  if (dim != r.size())
547  return false;
548  for (unsigned long i=0; i<dim; ++i)
549  if (l[i] != r[i])
550  return false;
551  return true;
552 }
553 
554 template <typename Numeric>
556 {
557  return !(l == r);
558 }
559 
560 
561 #endif // NPSTAT_BOXND_HH_
562 
BoxND(const unsigned long dim)
Definition: BoxND.h:30
int i
Definition: DBlmapReader.cc:9
bool isInside(const Num2 *coord, unsigned long coordLen) const
Definition: BoxND.h:331
static BoxND unitBox(unsigned long ndim)
Definition: BoxND.h:477
bool write(std::ostream &of) const
Definition: BoxND.h:508
static void restore(const gs::ClassId &id, std::istream &in, BoxND *box)
Definition: BoxND.h:522
BoxND & operator*=(double r)
Definition: BoxND.h:344
void getMidpoint(Numeric *coord, unsigned long coordLen) const
Definition: BoxND.h:270
bool isInsideWithBounds(const Num2 *coord, unsigned long coordLen) const
Definition: BoxND.h:316
BoxND & expand(double r)
Definition: BoxND.h:362
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
bool operator==(const CaloTower &t1, const CaloTower &t2)
Definition: CaloTower.h:209
const Numeric max() const
Definition: Interval.h:94
unsigned long dim() const
Definition: BoxND.h:57
BoxND & moveToOrigin()
Definition: BoxND.h:353
Exceptions for the npstat namespace.
BoxND & operator+=(const std::vector< Num2 > &shifts)
const T & max(const T &a, const T &b)
static unsigned version()
Definition: BoxND.h:155
string unit
Definition: csvLumiCalc.py:46
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
bool isInsideLower(const Num2 *coord, unsigned long coordLen) const
Definition: BoxND.h:286
static BoxND allSpace(unsigned long ndim)
Definition: BoxND.h:493
static const char * classname()
Definition: BoxND.h:501
static BoxND sizeTwoBox(unsigned long ndim)
Definition: BoxND.h:485
double f[11][100]
Numeric overlapVolume(const BoxND &r) const
Definition: BoxND.h:240
BoxND & operator-=(const std::vector< Num2 > &shifts)
Numeric volume() const
Definition: BoxND.h:230
double overlapFraction(const BoxND &r) const
Definition: BoxND.h:255
BoxND & shift(const Num2 *shifts, unsigned long lenShifts)
double b
Definition: hdecay.h:120
BoxND(const unsigned long dim, const Interval< Numeric > &v)
Definition: BoxND.h:34
BoxND & operator/=(double r)
Definition: BoxND.h:412
const Numeric min() const
Definition: Interval.h:91
gs::ClassId classId() const
Definition: BoxND.h:150
BoxND & copyFrom(const BoxND< Num2 > &r)
bool isInsideUpper(const Num2 *coord, unsigned long coordLen) const
Definition: BoxND.h:301
tuple size
Write out results.