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