Jpp  17.3.2
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  * Reset.
170  */
171  void reset()
172  {
174 
175  for (iterator i = this->begin(); i != this->end(); ++i) {
176  i->getY() = JMATH::zero;
177  }
178  }
179 
180 
181  /**
182  * Fill histogram.
183  *
184  * \param pX pointer to abscissa values
185  * \param w weight
186  */
187  virtual void evaluate(const abscissa_type* pX,
189  {
190  this->fill(*pX, w);
191  }
192 
193 
194  /**
195  * Fill histogram.
196  *
197  * \param x abscissa value
198  * \param w weight
199  */
202  {
203  this->integral += w;
204 
205  iterator p = this->lower_bound(x);
206 
207  if (p == this->begin())
208  this->underflow += w;
209  else if (p == this->end())
210  this->overflow += w;
211  else
212  (--p)->getY() += w;
213  }
214 
215 
216  /**
217  * Rebin histogram.
218  *
219  * \param merge rebin evaluator
220  */
221  template<class JRebin_t>
222  void rebin(JRebin_t merge)
223  {
224  if (this->size() > 1u) {
225 
226  iterator out = this->begin();
227 
228  for (const_iterator i = this->begin(); i != this->end(); ) {
229 
230  *out = *i;
231 
232  while (++i != this->end() && merge(*out,*i)) {
233  out->getY() += i->getY();
234  }
235 
236  ++out;
237  }
238 
239  const_reverse_iterator __rbegin(out);
240 
241  if (this->getDistance(__rbegin->getX(), this->rbegin()->getX()) > 0.0) {
242 
243  *out = *(this->rbegin());
244 
245  ++out;
246  }
247 
248  this->resize(std::distance(this->begin(), out));
249  }
250  }
251 
252 
253  /**
254  * Add histogram.
255  *
256  * \param histogram histogram
257  * \return this histogram
258  */
259  JHistogram1D& add(const JHistogram1D& histogram)
260  {
261  collection_type::add(static_cast<const collection_type&>(histogram));
262  histogram_type ::add(static_cast<const histogram_type&> (histogram));
263 
264  return *this;
265  }
266 
267 
268  /**
269  * Subtract histogram.
270  *
271  * \param histogram histogram
272  * \return this histogram
273  */
274  JHistogram1D& sub(const JHistogram1D& histogram)
275  {
276  collection_type::sub(static_cast<const collection_type&>(histogram));
277  histogram_type ::sub(static_cast<const histogram_type&> (histogram));
278 
279  return *this;
280  }
281 
282 
283  /**
284  * Scale contents.
285  *
286  * \param value multiplication factor
287  * \return this histogram
288  */
289  JHistogram1D& mul(const double value)
290  {
291  collection_type::mul(value);
292  histogram_type ::mul(value);
293 
294  return *this;
295  }
296 
297 
298  /**
299  * Scale contents.
300  *
301  * \param value division factor
302  * \return this histogram
303  */
304  JHistogram1D& div(const double value)
305  {
306  collection_type::div(value);
307  histogram_type ::div(value);
308 
309  return *this;
310  }
311 
312 
313  /**
314  * Read histogram from input.
315  *
316  * \param in reader
317  * \param object histogram
318  * \return reader
319  */
320  friend inline JReader& operator>>(JReader& in, JHistogram1D& object)
321  {
322  in >> static_cast<histogram_type&> (object);
323  in >> static_cast<collection_type&>(object);
324 
325  return in;
326  }
327 
328 
329  /**
330  * Write histogram to output.
331  *
332  * \param out writer
333  * \param object histogram
334  * \return writer
335  */
336  friend inline JWriter& operator<<(JWriter& out, const JHistogram1D& object)
337  {
338  out << static_cast<const histogram_type&> (object);
339  out << static_cast<const collection_type&>(object);
340 
341  return out;
342  }
343  };
344 
345 
346  /**
347  * Template specialisation if JHistogram1D class with bin centering.
348  *
349  * This class implements the JHistogram interface.
350  */
351  template<class JAbscissa_t,
352  class JContents_t,
353  template<class, class> class JContainer_t,
354  class JDistance_t>
355  class JHistogram1D<JBin2D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t> :
356  public JContainer_t<JBin2D<JAbscissa_t, JContents_t>, JDistance_t>,
357  public JHistogram<JAbscissa_t, JContents_t>,
358  public JMath< JHistogram1D<JBin2D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t> >
359  {
360  public:
361 
362  enum { NUMBER_OF_DIMENSIONS = 1 };
363 
365  typedef JContainer_t<element_type, JDistance_t> collection_type;
366 
367  typedef typename collection_type::abscissa_type abscissa_type;
368  typedef typename collection_type::ordinate_type ordinate_type;
369  typedef typename collection_type::value_type value_type;
370 
371  typedef typename collection_type::const_iterator const_iterator;
372  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
373  typedef typename collection_type::iterator iterator;
374  typedef typename collection_type::reverse_iterator reverse_iterator;
375 
377 
379 
382 
383 
384  /**
385  * Default constructor.
386  */
388  {}
389 
390 
391  /**
392  * Constructor.
393  *
394  * \param bounds bounds
395  */
397  {
398  this->set(bounds);
399  }
400 
401 
402  /**
403  * Fill histogram.
404  *
405  * \param pX pointer to abscissa values
406  * \param w weight
407  */
408  virtual void evaluate(const abscissa_type* pX,
410  {
411  this->fill(*pX, w);
412  }
413 
414 
415  /**
416  * Fill histogram.
417  *
418  * \param x abscissa value
419  * \param w weight
420  */
423  {
424  this->integral += w;
425 
426  iterator p = this->lower_bound(x);
427 
428  if (p == this->begin())
429  this->underflow += w;
430  else if (p == this->end())
431  this->overflow += w;
432  else
433  (--p)->fill(x, w);
434  }
435 
436 
437  /**
438  * Rebin histogram.
439  *
440  * \param merge rebin evaluator
441  */
442  template<class JRebin_t>
443  void rebin(JRebin_t merge)
444  {
445  if (this->size() > 1u) {
446 
447  iterator out = this->begin();
448 
449  for (const_iterator i = this->begin(); i != this->end(); ) {
450 
451  *out = *i;
452 
453  while (++i != this->end() && merge(*out,*i)) {
454  out->add(*i);
455  }
456 
457  ++out;
458  }
459 
460  const_reverse_iterator __rbegin(out);
461 
462  if (getDistance(__rbegin->getX(), this->rbegin()->getX()) > 0.0) {
463 
464  *out = *(this->rbegin());
465 
466  ++out;
467  }
468 
469  this->resize(std::distance(this->begin(), out));
470  }
471  }
472 
473 
474  /**
475  * Scale contents.
476  *
477  * \param value multiplication factor
478  */
479  JHistogram1D& mul(const double value)
480  {
481  for (iterator i = this->begin(); i != this->end(); ++i) {
482  i->mul(value);
483  }
484 
485  histogram_type::mul(value);
486 
487  return *this;
488  }
489 
490 
491  /**
492  * Scale contents.
493  *
494  * \param value division factor
495  */
496  JHistogram1D& div(const double value)
497  {
498  for (iterator i = this->begin(); i != this->end(); ++i) {
499  i->div(value);
500  }
501 
502  histogram_type::div(value);
503 
504  return *this;
505  }
506 
507 
508  /**
509  * Read histogram from input.
510  *
511  * \param in reader
512  * \param object histogram
513  * \return reader
514  */
515  friend inline JReader& operator>>(JReader& in, JHistogram1D& object)
516  {
517  in >> static_cast<histogram_type&> (object);
518  in >> static_cast<collection_type&>(object);
519 
520  return in;
521  }
522 
523 
524  /**
525  * Write histogram to output.
526  *
527  * \param out writer
528  * \param object histogram
529  * \return writer
530  */
531  friend inline JWriter& operator<<(JWriter& out, const JHistogram1D& object)
532  {
533  out << static_cast<const histogram_type&> (object);
534  out << static_cast<const collection_type&>(object);
535 
536  return out;
537  }
538 
539  private:
540  /**
541  * Make methods inaccessible.
542  */
543  JHistogram1D& add(const JHistogram1D& histogram); //!< addition not allowed with bin centering
544  JHistogram1D& sub(const JHistogram1D& histogram); //!< subtraction not allowed with bin centering
545  };
546 
547 
548  /**
549  * Conversion of histogram to probability density function (PDF).
550  *
551  * The PDF abscissa and contents are set to the bin center and contents divided the bin width, respectively.
552  *
553  * \param input histogram
554  * \param output mappable collection
555  */
556  template<class JElement_t,
557  template<class, class> class JContainer_t,
558  class JDistance_t>
560  typename JMappable<JElement_t>::map_type& output)
561  {
562  typedef typename JElement_t::abscissa_type abscissa_type;
563  typedef typename JElement_t::ordinate_type ordinate_type;
565 
566  if (input.getSize() > 1) {
567 
568  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
569 
570  const abscissa_type x = 0.5 * (i->getX() + j->getX());
571  const ordinate_type y = i->getY();
572  const double w = input.getDistance(i->getX(), j->getX());
573 
574  output.put(x, y/w);
575  }
576  }
577  }
578 
579 
580  /**
581  * Conversion of histogram to probability density function (PDF).
582  *
583  * The PDF abscissa and contents are set to the bin center and contents divided the bin width, respectively.
584  *
585  * \param input histogram
586  * \param output mappable collection
587  */
588  template<class JAbscissa_t,
589  class JContents_t,
590  template<class, class> class JContainer_t,
591  class JDistance_t>
592  inline void makePDF(const JHistogram1D<JBin2D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t>& input,
594  {
595  typedef JAbscissa_t abscissa_type;
596  typedef JContents_t contents_type;
597  typedef typename JHistogram1D<JBin2D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t>::const_iterator const_iterator;
598 
599  if (input.getSize() > 1) {
600 
601  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
602 
603  const abscissa_type x = i->getBinCenter();
604  const contents_type y = i->getY();
605  const double w = input.getDistance(i->getX(), j->getX());
606 
607  output.put(x, y/w);
608  }
609  }
610  }
611 
612 
613  /**
614  * Conversion of data points to integral values.
615  *
616  * The integration is based on the sum of bin contents of the input data points.
617  *
618  * \param input histogram
619  * \param output mappable collection
620  * \return integral
621  */
622  template<class JElement_t,
623  template<class, class> class JContainer_t,
624  class JDistance_t>
625  inline typename JElement_t::ordinate_type
627  typename JMappable<JElement_t>::map_type& output)
628  {
629  typedef typename JElement_t::ordinate_type ordinate_type;
631 
632  ordinate_type V(JMATH::zero);
633 
634  if (input.getSize() > 1) {
635 
636  output.put(input.begin()->getX(), V);
637 
638  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
639 
640  V += i->getY();
641 
642  output.put(j->getX(), V);
643  }
644  }
645 
646  return V;
647  }
648 }
649 
650 #endif
JHistogram & div(double value)
Scale histogram.
Definition: JHistogram.hh:128
JElement_t value_type
Definition: JHistogram1D.hh:32
collection_type::ordinate_type ordinate_type
data_type w[N+1][M+1]
Definition: JPolint.hh:778
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:109
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
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
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:697
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:80
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:96
JElement_t::ordinate_type contents_type
Definition: JHistogram1D.hh:72
JHistogram & mul(const double value)
Scale histogram.
Definition: JHistogram.hh:112
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 reset()
Reset.
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:703
void reset()
Reset.
Definition: JHistogram.hh:56
double u[N+1]
Definition: JPolint.hh:776
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
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:812
const contents_type __y
Definition: JHistogram1D.hh:98
Template definition of histogram object interface.
Definition: JHistogram.hh:27