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 #include "Alignment/Geners/interface/IOIsUnsigned.hh"
175 
176 namespace npstat {
177  template <typename Numeric>
178  template <typename Num2>
180  {
181  const unsigned long dim = r.size();
182  if (dim)
183  {
184  this->reserve(dim);
185  for (unsigned long i=0; i<dim; ++i)
186  {
187  const Interval<Num2>& ri(r[i]);
188  this->push_back(Interval<Numeric>(ri.min(), ri.max()));
189  }
190  }
191  }
192 
193  template <typename Numeric>
194  template <typename Num2>
195  BoxND<Numeric>::BoxND(const std::vector<Num2>& limits)
196  {
197  const unsigned long dim = limits.size();
198  if (dim)
199  {
200  this->reserve(dim);
201  Numeric zero = Numeric();
202  for (unsigned long i=0; i<dim; ++i)
203  {
204  const Numeric value(static_cast<Numeric>(limits[i]));
205  if (value >= zero)
206  this->push_back(Interval<Numeric>(zero, value));
207  else
208  this->push_back(Interval<Numeric>(value, zero));
209  }
210  }
211  }
212 
213  template <typename Numeric>
214  template <typename Num2>
216  {
217  if ((void *)this == (void *)(&r))
218  return *this;
219  const unsigned long n = r.size();
220  this->clear();
221  this->reserve(n);
222  for (unsigned long i=0; i<n; ++i)
223  {
224  const Interval<Num2>& ir(r[i]);
225  this->push_back(Interval<Numeric>(ir.min(), ir.max()));
226  }
227  return *this;
228  }
229 
230  template <typename Numeric>
231  Numeric BoxND<Numeric>::volume() const
232  {
233  Numeric v(static_cast<Numeric>(1));
234  const unsigned long mydim = this->size();
235  for (unsigned long i=0U; i<mydim; ++i)
236  v *= (*this)[i].length();
237  return v;
238  }
239 
240  template <typename Numeric>
241  Numeric BoxND<Numeric>::overlapVolume(const BoxND& r) const
242  {
243  const unsigned long mydim = this->size();
244  if (mydim == r.size())
245  {
246  Numeric v(static_cast<Numeric>(1));
247  for (unsigned long i=0U; i<mydim; ++i)
248  v *= (*this)[i].overlapLength(r[i]);
249  return v;
250  }
251  else
252  return static_cast<Numeric>(0);
253  }
254 
255  template <typename Numeric>
257  {
258  const unsigned long mydim = this->size();
259  if (mydim == r.size())
260  {
261  double f = 1.0;
262  for (unsigned long i=0U; i<mydim; ++i)
263  f *= (*this)[i].overlapFraction(r[i]);
264  return f;
265  }
266  else
267  return 0.0;
268  }
269 
270  template <typename Numeric>
271  void BoxND<Numeric>::getMidpoint(Numeric* coord,
272  const unsigned long coordLen) const
273  {
274  const unsigned long mydim = this->size();
275  if (coordLen < mydim) throw npstat::NpstatInvalidArgument(
276  "In npstat::BoxND::getMidpoint: insufficient output buffer length");
277  if (mydim)
278  {
279  assert(coord);
280  for (unsigned long i=0U; i<mydim; ++i)
281  coord[i] = (*this)[i].midpoint();
282  }
283  }
284 
285  template <typename Numeric>
286  template <typename Num2>
287  bool BoxND<Numeric>::isInsideLower(const Num2* coords,
288  const unsigned long coordLen) const
289  {
290  if (coordLen != this->size()) throw npstat::NpstatInvalidArgument(
291  "In npstat::BoxND::isInsideLower: "
292  "incompatible point dimensionality");
293  const Interval<Numeric>* myptr = &(*this)[0];
294  for (unsigned long i=0; i<coordLen; ++i)
295  if (!myptr[i].isInsideLower(coords[i]))
296  return false;
297  return true;
298  }
299 
300  template <typename Numeric>
301  template <typename Num2>
302  bool BoxND<Numeric>::isInsideUpper(const Num2* coords,
303  const unsigned long coordLen) const
304  {
305  if (coordLen != this->size()) throw npstat::NpstatInvalidArgument(
306  "In npstat::BoxND::isInsideUpper: "
307  "incompatible point dimensionality");
308  const Interval<Numeric>* myptr = &(*this)[0];
309  for (unsigned long i=0; i<coordLen; ++i)
310  if (!myptr[i].isInsideUpper(coords[i]))
311  return false;
312  return true;
313  }
314 
315  template <typename Numeric>
316  template <typename Num2>
317  bool BoxND<Numeric>::isInsideWithBounds(const Num2* coords,
318  const unsigned long coordLen) const
319  {
320  if (coordLen != this->size()) throw npstat::NpstatInvalidArgument(
321  "In npstat::BoxND::isInsideWithBounds: "
322  "incompatible point dimensionality");
323  const Interval<Numeric>* myptr = &(*this)[0];
324  for (unsigned long i=0; i<coordLen; ++i)
325  if (!myptr[i].isInsideWithBounds(coords[i]))
326  return false;
327  return true;
328  }
329 
330  template <typename Numeric>
331  template <typename Num2>
332  bool BoxND<Numeric>::isInside(const Num2* coords,
333  const unsigned long coordLen) const
334  {
335  if (coordLen != this->size()) throw npstat::NpstatInvalidArgument(
336  "In npstat::BoxND::isInside: incompatible point dimensionality");
337  const Interval<Numeric>* myptr = &(*this)[0];
338  for (unsigned long i=0; i<coordLen; ++i)
339  if (!myptr[i].isInside(coords[i]))
340  return false;
341  return true;
342  }
343 
344  template <typename Numeric>
346  {
347  const unsigned long mydim = this->size();
348  for (unsigned long i=0; i<mydim; ++i)
349  (*this)[i] *= r;
350  return *this;
351  }
352 
353  template <typename Numeric>
355  {
356  const unsigned long mydim = this->size();
357  for (unsigned long i=0; i<mydim; ++i)
358  (*this)[i].moveMidpointTo0();
359  return *this;
360  }
361 
362  template <typename Numeric>
364  {
365  const unsigned long mydim = this->size();
366  for (unsigned long i=0; i<mydim; ++i)
367  (*this)[i].expand(r);
368  return *this;
369  }
370 
371  template <typename Numeric>
373  const std::vector<double>& scales)
374  {
375  const unsigned long mydim = this->size();
376  if (mydim != scales.size()) throw npstat::NpstatInvalidArgument(
377  "In npstat::BoxND::operator*=: "
378  "incompatible argument dimensionality");
379  for (unsigned long i=0; i<mydim; ++i)
380  (*this)[i] *= scales[i];
381  return *this;
382  }
383 
384  template <typename Numeric>
386  const std::vector<double>& scales)
387  {
388  const unsigned long mydim = this->size();
389  if (mydim != scales.size()) throw npstat::NpstatInvalidArgument(
390  "In npstat::BoxND::expand: incompatible argument dimensionality");
391  for (unsigned long i=0; i<mydim; ++i)
392  (*this)[i].expand(scales[i]);
393  return *this;
394  }
395 
396  template <typename Numeric>
398  const double* scales, const unsigned long lenScales)
399  {
400  const unsigned long mydim = this->size();
401  if (mydim != lenScales) throw npstat::NpstatInvalidArgument(
402  "In npstat::BoxND::expand: incompatible argument dimensionality");
403  if (mydim)
404  {
405  assert(scales);
406  for (unsigned long i=0; i<mydim; ++i)
407  (*this)[i].expand(scales[i]);
408  }
409  return *this;
410  }
411 
412  template <typename Numeric>
414  {
415  const unsigned long mydim = this->size();
416  for (unsigned long i=0; i<mydim; ++i)
417  (*this)[i] /= r;
418  return *this;
419  }
420 
421  template <typename Numeric>
423  const std::vector<double>& scales)
424  {
425  const unsigned long mydim = this->size();
426  if (mydim != scales.size()) throw npstat::NpstatInvalidArgument(
427  "In npstat::BoxND::operator/=: "
428  "incompatible argument dimensionality");
429  for (unsigned long i=0; i<mydim; ++i)
430  (*this)[i] /= scales[i];
431  return *this;
432  }
433 
434  template <typename Numeric>
435  template <typename Num2>
436  BoxND<Numeric>& BoxND<Numeric>::operator+=(const std::vector<Num2>& shifts)
437  {
438  const unsigned long mydim = this->size();
439  if (mydim != shifts.size()) throw npstat::NpstatInvalidArgument(
440  "In npstat::BoxND::operator+=: "
441  "incompatible argument dimensionality");
442  for (unsigned long i=0; i<mydim; ++i)
443  (*this)[i] += static_cast<Numeric>(shifts[i]);
444  return *this;
445  }
446 
447  template <typename Numeric>
448  template <typename Num2>
450  const Num2* shifts, const unsigned long shiftsLen)
451  {
452  const unsigned long mydim = this->size();
453  if (mydim != shiftsLen) throw npstat::NpstatInvalidArgument(
454  "In npstat::BoxND::shift: incompatible argument dimensionality");
455  if (mydim)
456  {
457  assert(shifts);
458  for (unsigned long i=0; i<mydim; ++i)
459  (*this)[i] += static_cast<Numeric>(shifts[i]);
460  }
461  return *this;
462  }
463 
464  template <typename Numeric>
465  template <typename Num2>
466  BoxND<Numeric>& BoxND<Numeric>::operator-=(const std::vector<Num2>& shifts)
467  {
468  const unsigned long mydim = this->size();
469  if (mydim != shifts.size()) throw npstat::NpstatInvalidArgument(
470  "In npstat::BoxND::operator-=: "
471  "incompatible argument dimensionality");
472  for (unsigned long i=0; i<mydim; ++i)
473  (*this)[i] -= static_cast<Numeric>(shifts[i]);
474  return *this;
475  }
476 
477  template <typename Numeric>
478  BoxND<Numeric> BoxND<Numeric>::unitBox(const unsigned long ndim)
479  {
480  Interval<Numeric> unit(static_cast<Numeric>(0),
481  static_cast<Numeric>(1));
482  return BoxND<Numeric>(ndim, unit);
483  }
484 
485  template <typename Numeric>
486  BoxND<Numeric> BoxND<Numeric>::sizeTwoBox(const unsigned long ndim)
487  {
488  const Numeric one = static_cast<Numeric>(1);
489  Interval<Numeric> i(-one, one);
490  return BoxND<Numeric>(ndim, i);
491  }
492 
493  template <typename Numeric>
494  BoxND<Numeric> BoxND<Numeric>::allSpace(const unsigned long ndim)
495  {
496  const Numeric maxval = std::numeric_limits<Numeric>::max();
497  Interval<Numeric> i(static_cast<Numeric>(0), maxval);
499  i.setMin(-maxval);
500  return BoxND<Numeric>(ndim, i);
501  }
502 
503  template<typename Numeric>
505  {
506  static const std::string na(gs::template_class_name<Numeric>("npstat::BoxND"));
507  return na.c_str();
508  }
509 
510  template<typename Numeric>
511  bool BoxND<Numeric>::write(std::ostream& of) const
512  {
513  const unsigned long mydim = this->size();
514  std::vector<Numeric> limits;
515  limits.reserve(2UL*mydim);
516  for (unsigned long i=0; i<mydim; ++i)
517  {
518  limits.push_back((*this)[i].min());
519  limits.push_back((*this)[i].max());
520  }
521  return gs::write_item(of, limits);
522  }
523 
524  template<typename Numeric>
525  void BoxND<Numeric>::restore(const gs::ClassId& id, std::istream& in, BoxND* b)
526  {
527  static const gs::ClassId current(gs::ClassId::makeId<BoxND<Numeric> >());
528  current.ensureSameId(id);
529 
530  std::vector<Numeric> limits;
531  gs::restore_item(in, &limits);
532  if (in.fail())
533  throw gs::IOReadFailure("In npstat::BoxND::restore: input stream failure");
534  const unsigned long nlimits = limits.size();
535  if (nlimits % 2UL)
536  throw gs::IOInvalidData("In npstat::BoxND::restore: bad limits");
537  assert(b);
538  b->clear();
539  b->reserve(nlimits/2UL);
540  for (unsigned long i=0; i<nlimits/2UL; ++i)
541  b->push_back(npstat::Interval<Numeric>(limits[2U*i], limits[2U*i+1U]));
542  }
543 }
544 
545 template <typename Numeric>
547 {
548  const unsigned long dim = l.size();
549  if (dim != r.size())
550  return false;
551  for (unsigned long i=0; i<dim; ++i)
552  if (l[i] != r[i])
553  return false;
554  return true;
555 }
556 
557 template <typename Numeric>
559 {
560  return !(l == r);
561 }
562 
563 
564 #endif // NPSTAT_BOXND_HH_
565 
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:332
static BoxND unitBox(unsigned long ndim)
Definition: BoxND.h:478
bool write(std::ostream &of) const
Definition: BoxND.h:511
static void restore(const gs::ClassId &id, std::istream &in, BoxND *box)
Definition: BoxND.h:525
BoxND & operator*=(double r)
Definition: BoxND.h:345
void getMidpoint(Numeric *coord, unsigned long coordLen) const
Definition: BoxND.h:271
bool isInsideWithBounds(const Num2 *coord, unsigned long coordLen) const
Definition: BoxND.h:317
BoxND & expand(double r)
Definition: BoxND.h:363
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
const Numeric max() const
Definition: Interval.h:94
unsigned long dim() const
Definition: BoxND.h:57
BoxND & moveToOrigin()
Definition: BoxND.h:354
Exceptions for the npstat namespace.
BoxND & operator+=(const std::vector< Num2 > &shifts)
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:287
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
static BoxND allSpace(unsigned long ndim)
Definition: BoxND.h:494
static const char * classname()
Definition: BoxND.h:504
static BoxND sizeTwoBox(unsigned long ndim)
Definition: BoxND.h:486
double f[11][100]
T min(T a, T b)
Definition: MathUtil.h:58
Numeric overlapVolume(const BoxND &r) const
Definition: BoxND.h:241
BoxND & operator-=(const std::vector< Num2 > &shifts)
void setMin(const Numeric value)
Definition: Interval.h:62
Numeric volume() const
Definition: BoxND.h:231
double overlapFraction(const BoxND &r) const
Definition: BoxND.h:256
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:413
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:302
tuple size
Write out results.