Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JHistogram1D.hh
Go to the documentation of this file.
1 #ifndef __JHISTOGRAM1D__
2 #define __JHISTOGRAM1D__
3 
4 #include "JTools/JHistogram.hh"
5 #include "JTools/JDistance.hh"
8 #include "JTools/JCollection.hh"
9 #include "JTools/JElement.hh"
10 #include "JMath/JMath.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 namespace JTOOLS {}
18 namespace JPP { using namespace JTOOLS; }
19 
20 namespace JTOOLS {
21 
22  using JLANG::JClass;
23 
24 
25  /**
26  * Auxiliary class for merging of fixed number of consecutive bins.
27  */
28  template<class JElement_t>
29  struct JRebin {
30 
31  typedef JElement_t value_type;
32  typedef typename JElement_t::ordinate_type contents_type;
33 
34 
35  /**
36  * Constructor.
37  *
38  * \param n number of bins to merge
39  */
40  JRebin(const int n) :
41  __n(n > 1 ? n : 1),
42  __i(0)
43  {}
44 
45 
46  /**
47  * Test whether bins should be merged.
48  *
49  * \param first first bin
50  * \param second second bin
51  * \return true if bins should be merged; else false
52  */
53  bool operator()(const value_type& first, const value_type& second) const
54  {
55  return (__n != 1 && ++__i%__n != 0);
56  }
57 
58  private:
59  const int __n;
60  mutable int __i;
61  };
62 
63 
64  /**
65  * Auxiliary class for merging of consecutive bins until minimal content is reached.
66  */
67  template<class JElement_t>
68  struct JContent {
69 
70  typedef JElement_t value_type;
71  typedef typename JElement_t::ordinate_type contents_type;
72 
73 
74  /**
75  * Constructor.
76  *
77  * \param y minimal content
78  */
80  __y(y)
81  {}
82 
83 
84  /**
85  * Test whether bins should be merged.
86  *
87  * \param first first bin
88  * \param second second bin
89  * \return true if bins should be merged; else false
90  */
91  bool operator()(const value_type& first, const value_type& second) const
92  {
93  return (first.getY() + second.getY() < __y);
94  }
95 
96  private:
98  };
99 
100 
101 
102  /**
103  * Histogram in 1D.
104  *
105  * This class implements the JHistogram interface.
106  */
107  template<class JElement_t,
108  template<class, class> class JContainer_t,
110  class JHistogram1D :
111  public JContainer_t<JElement_t, JDistance_t>,
112  public JHistogram<typename JElement_t::abscissa_type, typename JElement_t::ordinate_type>,
113  public JMATH::JMath< JHistogram1D<JElement_t, JContainer_t, JDistance_t> >
114  {
115  public:
116 
117  enum { NUMBER_OF_DIMENSIONS = 1 };
118 
119  typedef JContainer_t<JElement_t, JDistance_t> collection_type;
120 
121  typedef typename collection_type::abscissa_type abscissa_type;
122  typedef typename collection_type::ordinate_type ordinate_type;
123  typedef typename collection_type::value_type value_type;
124 
125  typedef typename collection_type::const_iterator const_iterator;
126  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
127  typedef typename collection_type::iterator iterator;
128  typedef typename collection_type::reverse_iterator reverse_iterator;
129 
131 
133 
136 
137 
138  /**
139  * Default constructor.
140  */
142  {}
143 
144 
145  /**
146  * Constructor.
147  *
148  * \param bounds bounds
149  */
151  {
152  this->configure(bounds);
153  }
154 
155 
156  /**
157  * Constructor.
158  *
159  * \param bounds bounds
160  */
162  {
163  this->configure(bounds);
164  }
165 
166 
167  /**
168  * Fill histogram.
169  *
170  * \param pX pointer to abscissa values
171  * \param w weight
172  */
173  virtual void evaluate(const abscissa_type* pX,
175  {
176  this->fill(*pX, w);
177  }
178 
179 
180  /**
181  * Fill histogram.
182  *
183  * \param x abscissa value
184  * \param w weight
185  */
188  {
189  this->integral += w;
190 
191  iterator p = this->lower_bound(x);
192 
193  if (p == this->begin())
194  this->underflow += w;
195  else if (p == this->end())
196  this->overflow += w;
197  else
198  (--p)->getY() += w;
199  }
200 
201 
202  /**
203  * Rebin histogram.
204  *
205  * \param merge rebin evaluator
206  */
207  template<class JRebin_t>
208  void rebin(JRebin_t merge)
209  {
210  if (this->size() > 1u) {
211 
212  iterator out = this->begin();
213 
214  for (const_iterator i = this->begin(); i != this->end(); ) {
215 
216  *out = *i;
217 
218  while (++i != this->end() && merge(*out,*i)) {
219  out->getY() += i->getY();
220  }
221 
222  ++out;
223  }
224 
225  const_reverse_iterator __rbegin(out);
226 
227  if (this->getDistance(__rbegin->getX(), this->rbegin()->getX()) > 0.0) {
228 
229  *out = *(this->rbegin());
230 
231  ++out;
232  }
233 
234  this->resize(std::distance(this->begin(), out));
235  }
236  }
237 
238 
239  /**
240  * Add histogram.
241  *
242  * \param histogram histogram
243  * \return this histogram
244  */
245  JHistogram1D& add(const JHistogram1D& histogram)
246  {
247  collection_type::add(static_cast<const collection_type&>(histogram));
248  histogram_type ::add(static_cast<const histogram_type&> (histogram));
249 
250  return *this;
251  }
252 
253 
254  /**
255  * Subtract histogram.
256  *
257  * \param histogram histogram
258  * \return this histogram
259  */
260  JHistogram1D& sub(const JHistogram1D& histogram)
261  {
262  collection_type::sub(static_cast<const collection_type&>(histogram));
263  histogram_type ::sub(static_cast<const histogram_type&> (histogram));
264 
265  return *this;
266  }
267 
268 
269  /**
270  * Scale contents.
271  *
272  * \param value multiplication factor
273  * \return this histogram
274  */
275  JHistogram1D& mul(const double value)
276  {
277  collection_type::mul(value);
278  histogram_type ::mul(value);
279 
280  return *this;
281  }
282 
283 
284  /**
285  * Scale contents.
286  *
287  * \param value division factor
288  * \return this histogram
289  */
290  JHistogram1D& div(const double value)
291  {
292  collection_type::div(value);
293  histogram_type ::div(value);
294 
295  return *this;
296  }
297 
298 
299  /**
300  * Read histogram from input.
301  *
302  * \param in reader
303  * \param object histogram
304  * \return reader
305  */
306  friend inline JReader& operator>>(JReader& in, JHistogram1D& object)
307  {
308  in >> static_cast<histogram_type&> (object);
309  in >> static_cast<collection_type&>(object);
310 
311  return in;
312  }
313 
314 
315  /**
316  * Write histogram to output.
317  *
318  * \param out writer
319  * \param object histogram
320  * \return writer
321  */
322  friend inline JWriter& operator<<(JWriter& out, const JHistogram1D& object)
323  {
324  out << static_cast<const histogram_type&> (object);
325  out << static_cast<const collection_type&>(object);
326 
327  return out;
328  }
329  };
330 
331 
332  /**
333  * Template specialisation if JHistogram1D class with bin centering.
334  *
335  * This class implements the JHistogram interface.
336  */
337  template<class JAbscissa_t,
338  class JContents_t,
339  template<class, class> class JContainer_t,
340  class JDistance_t>
341  class JHistogram1D<JBin2D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t> :
342  public JContainer_t<JBin2D<JAbscissa_t, JContents_t>, JDistance_t>,
343  public JHistogram<JAbscissa_t, JContents_t>,
344  public JMATH::JMath< JHistogram1D<JBin2D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t> >
345  {
346  public:
347 
348  enum { NUMBER_OF_DIMENSIONS = 1 };
349 
351  typedef JContainer_t<element_type, JDistance_t> collection_type;
352 
353  typedef typename collection_type::abscissa_type abscissa_type;
354  typedef typename collection_type::ordinate_type ordinate_type;
355  typedef typename collection_type::value_type value_type;
356 
357  typedef typename collection_type::const_iterator const_iterator;
358  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
359  typedef typename collection_type::iterator iterator;
360  typedef typename collection_type::reverse_iterator reverse_iterator;
361 
363 
365 
368 
369 
370  /**
371  * Default constructor.
372  */
374  {}
375 
376 
377  /**
378  * Constructor.
379  *
380  * \param bounds bounds
381  */
383  {
384  this->set(bounds);
385  }
386 
387 
388  /**
389  * Fill histogram.
390  *
391  * \param pX pointer to abscissa values
392  * \param w weight
393  */
394  virtual void evaluate(const abscissa_type* pX,
396  {
397  this->fill(*pX, w);
398  }
399 
400 
401  /**
402  * Fill histogram.
403  *
404  * \param x abscissa value
405  * \param w weight
406  */
409  {
410  this->integral += w;
411 
412  iterator p = this->lower_bound(x);
413 
414  if (p == this->begin())
415  this->underflow += w;
416  else if (p == this->end())
417  this->overflow += w;
418  else
419  (--p)->fill(x, w);
420  }
421 
422 
423  /**
424  * Rebin histogram.
425  *
426  * \param merge rebin evaluator
427  */
428  template<class JRebin_t>
429  void rebin(JRebin_t merge)
430  {
431  if (this->size() > 1u) {
432 
433  iterator out = this->begin();
434 
435  for (const_iterator i = this->begin(); i != this->end(); ) {
436 
437  *out = *i;
438 
439  while (++i != this->end() && merge(*out,*i)) {
440  out->add(*i);
441  }
442 
443  ++out;
444  }
445 
446  const_reverse_iterator __rbegin(out);
447 
448  if (getDistance(__rbegin->getX(), this->rbegin()->getX()) > 0.0) {
449 
450  *out = *(this->rbegin());
451 
452  ++out;
453  }
454 
455  this->resize(std::distance(this->begin(), out));
456  }
457  }
458 
459 
460  /**
461  * Scale contents.
462  *
463  * \param value multiplication factor
464  */
465  JHistogram1D& mul(const double value)
466  {
467  for (iterator i = this->begin(); i != this->end(); ++i) {
468  i->mul(value);
469  }
470 
471  histogram_type::mul(value);
472 
473  return *this;
474  }
475 
476 
477  /**
478  * Scale contents.
479  *
480  * \param value division factor
481  */
482  JHistogram1D& div(const double value)
483  {
484  for (iterator i = this->begin(); i != this->end(); ++i) {
485  i->div(value);
486  }
487 
488  histogram_type::div(value);
489 
490  return *this;
491  }
492 
493 
494  /**
495  * Read histogram from input.
496  *
497  * \param in reader
498  * \param object histogram
499  * \return reader
500  */
501  friend inline JReader& operator>>(JReader& in, JHistogram1D& object)
502  {
503  in >> static_cast<histogram_type&> (object);
504  in >> static_cast<collection_type&>(object);
505 
506  return in;
507  }
508 
509 
510  /**
511  * Write histogram to output.
512  *
513  * \param out writer
514  * \param object histogram
515  * \return writer
516  */
517  friend inline JWriter& operator<<(JWriter& out, const JHistogram1D& object)
518  {
519  out << static_cast<const histogram_type&> (object);
520  out << static_cast<const collection_type&>(object);
521 
522  return out;
523  }
524 
525  private:
526  /**
527  * Make methods inaccessible.
528  */
529  JHistogram1D& add(const JHistogram1D& histogram); //!< addition not allowed with bin centering
530  JHistogram1D& sub(const JHistogram1D& histogram); //!< subtraction not allowed with bin centering
531  };
532 
533 
534  /**
535  * Conversion of histogram to probability density function (PDF).
536  *
537  * The PDF abscissa and contents are set to the bin center and contents divided the bin width, respectively.
538  *
539  * \param input histogram
540  * \param output mappable collection
541  */
542  template<class JElement_t,
543  template<class, class> class JContainer_t,
544  class JDistance_t>
546  typename JMappable<JElement_t>::map_type& output)
547  {
548  typedef typename JElement_t::abscissa_type abscissa_type;
549  typedef typename JElement_t::ordinate_type ordinate_type;
551 
552  if (input.getSize() > 1) {
553 
554  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
555 
556  const abscissa_type x = 0.5 * (i->getX() + j->getX());
557  const ordinate_type y = i->getY();
558  const double w = input.getDistance(i->getX(), j->getX());
559 
560  output.put(x, y/w);
561  }
562  }
563  }
564 
565 
566  /**
567  * Conversion of histogram to probability density function (PDF).
568  *
569  * The PDF abscissa and contents are set to the bin center and contents divided the bin width, respectively.
570  *
571  * \param input histogram
572  * \param output mappable collection
573  */
574  template<class JAbscissa_t,
575  class JContents_t,
576  template<class, class> class JContainer_t,
577  class JDistance_t>
578  inline void makePDF(const JHistogram1D<JBin2D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t>& input,
580  {
581  typedef JAbscissa_t abscissa_type;
582  typedef JContents_t contents_type;
583  typedef typename JHistogram1D<JBin2D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t>::const_iterator const_iterator;
584 
585  if (input.getSize() > 1) {
586 
587  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
588 
589  const abscissa_type x = i->getBinCenter();
590  const contents_type y = i->getY();
591  const double w = input.getDistance(i->getX(), j->getX());
592 
593  output.put(x, y/w);
594  }
595  }
596  }
597 
598 
599  /**
600  * Conversion of data points to integral values.
601  *
602  * The integration is based on the sum of bin contents of the input data points.
603  *
604  * \param input histogram
605  * \param output mappable collection
606  * \return integral
607  */
608  template<class JElement_t,
609  template<class, class> class JContainer_t,
610  class JDistance_t>
611  inline typename JElement_t::ordinate_type
613  typename JMappable<JElement_t>::map_type& output)
614  {
615  typedef typename JElement_t::ordinate_type ordinate_type;
617 
618  ordinate_type V(JMATH::zero);
619 
620  if (input.getSize() > 1) {
621 
622  output.put(input.begin()->getX(), V);
623 
624  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
625 
626  V += i->getY();
627 
628  output.put(j->getX(), V);
629  }
630  }
631 
632  return V;
633  }
634 }
635 
636 #endif
JHistogram & div(double value)
Scale histogram.
Definition: JHistogram.hh:117
JElement_t value_type
Definition: JHistogram1D.hh:31
collection_type::ordinate_type ordinate_type
Interface for binary output.
JHistogram1D & mul(const double value)
Scale contents.
friend JReader & operator>>(JReader &in, JHistogram1D &object)
Read histogram from input.
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
JContent(const contents_type y)
Constructor.
Definition: JHistogram1D.hh:79
The elements in a collection are sorted according to their abscissa values and a given distance opera...
Abstract interface for abscissa values of a collection of elements.
JRebin(const int n)
Constructor.
Definition: JHistogram1D.hh:40
Template class for distance evaluation.
Definition: JDistance.hh:24
histogram_type::contents_type contents_type
2D Binned element.
Definition: JElement.hh:353
Template interface definition for associative collection of elements.
Auxiliary class for merging of fixed number of consecutive bins.
Definition: JHistogram1D.hh:29
JTOOLS::JContent< value_type > JContent
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:94
Simple data structure for histogram binning.
JElement_t::ordinate_type contents_type
Definition: JHistogram1D.hh:32
JHistogram1D(const JAbstractHistogram< abscissa_type > &bounds)
Constructor.
collection_type::reverse_iterator reverse_iterator
JContainer_t< JElement_t, JDistance_t > collection_type
JHistogram1D & sub(const JHistogram1D &histogram)
Subtract histogram.
friend JReader & operator>>(JReader &in, JHistogram1D &object)
Read histogram from input.
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
Definition: JMathToolkit.hh:98
JHistogram1D(const JAbstractCollection< abscissa_type > &bounds)
Constructor.
JElement_t value_type
Definition: JHistogram1D.hh:70
collection_type::const_iterator const_iterator
JHistogram1D(const JAbstractCollection< abscissa_type > &bounds)
Constructor.
JArgument< T >::argument_type argument_type
Definition: JClass.hh:64
collection_type::const_reverse_iterator const_reverse_iterator
JHistogram & add(const JHistogram &histogram)
Add histogram.
Definition: JHistogram.hh:69
collection_type::iterator iterator
void makePDF(const JHistogram1D< JElement_t, JContainer_t, JDistance_t > &input, typename JMappable< JElement_t >::map_type &output)
Conversion of histogram to probability density function (PDF).
void rebin(JRebin_t merge)
Rebin histogram.
JHistogram & sub(const JHistogram &histogram)
Subtract histogram.
Definition: JHistogram.hh:85
JElement_t::ordinate_type contents_type
Definition: JHistogram1D.hh:71
JHistogram & mul(const double value)
Scale histogram.
Definition: JHistogram.hh:101
JAbscissa_t abscissa_type
Definition: JHistogram.hh:42
JHistogram< abscissa_type, ordinate_type > histogram_type
virtual void evaluate(const abscissa_type *pX, typename JClass< contents_type >::argument_type w)
Fill histogram.
virtual void evaluate(const abscissa_type *pX, typename JClass< contents_type >::argument_type w)
Fill histogram.
General purpose class for a collection of sorted elements.
void put(typename JClass< key_type >::argument_type key, typename JClass< mapped_type >::argument_type value)
Put pair-wise element (key,value) into collection.
Interface for binary input.
JHistogram1D & div(const double value)
Scale contents.
JTOOLS::JRebin< value_type > JRebin
Auxiliary class for merging of consecutive bins until minimal content is reached. ...
Definition: JHistogram1D.hh:68
Template for generic class types.
Definition: JClass.hh:62
JHistogram1D & add(const JHistogram1D &histogram)
Add histogram.
collection_type::value_type value_type
const int __n
Definition: JHistogram1D.hh:59
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
void fill(typename JClass< abscissa_type >::argument_type x, typename JClass< contents_type >::argument_type w)
Fill histogram.
friend JWriter & operator<<(JWriter &out, const JHistogram1D &object)
Write histogram to output.
bool operator()(const value_type &first, const value_type &second) const
Test whether bins should be merged.
Definition: JHistogram1D.hh:53
Base class for data structures with artithmetic capabilities.
collection_type::abscissa_type abscissa_type
Histogram in 1D.
JHistogram1D()
Default constructor.
friend JWriter & operator<<(JWriter &out, const JHistogram1D &object)
Write histogram to output.
void fill(typename JClass< abscissa_type >::argument_type x, typename JClass< contents_type >::argument_type w)
Fill histogram.
bool operator()(const value_type &first, const value_type &second) const
Test whether bins should be merged.
Definition: JHistogram1D.hh:91
JContents_t contents_type
Definition: JHistogram.hh:43
JElement_t::ordinate_type integrate(const JCollection< JElement_t, JDistance_t > &input, typename JMappable< JElement_t >::map_type &output)
Conversion of data points to integral values.
Definition: JCollection.hh:793
const contents_type __y
Definition: JHistogram1D.hh:97
Template definition of histogram object interface.
Definition: JHistogram.hh:27