Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JHistogram1D.hh
Go to the documentation of this file.
1#ifndef __JHISTOGRAM1D__
2#define __JHISTOGRAM1D__
3
5#include "JTools/JDistance.hh"
9#include "JTools/JElement.hh"
10#include "JMath/JMath.hh"
11
12
13/**
14 * \author mdejong
15 */
16
17namespace JTOOLS {}
18namespace JPP { using namespace JTOOLS; }
19
20namespace 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 * Histogram in 1D.
104 *
105 * This class implements the JHistogram interface.
106 */
107 template<class JElement_t,
108 template<class, class> class JContainer_t,
111 public JContainer_t<JElement_t, JDistance_t>,
112 public JHistogram<typename JElement_t::abscissa_type, typename JElement_t::ordinate_type>,
113 public JMath< JHistogram1D<JElement_t, JContainer_t, JDistance_t> >
114 {
115 public:
116
118
119 typedef JContainer_t<JElement_t, JDistance_t> collection_type;
120
121 typedef typename collection_type::collection_type base_collection_type;
122
123 typedef typename collection_type::abscissa_type abscissa_type;
124 typedef typename collection_type::ordinate_type ordinate_type;
125 typedef typename collection_type::value_type value_type;
126
127 typedef typename collection_type::const_iterator const_iterator;
128 typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
129 typedef typename collection_type::iterator iterator;
130 typedef typename collection_type::reverse_iterator reverse_iterator;
131
133
135
136 typedef JTOOLS::JRebin <value_type> JRebin;
138
139
140 /**
141 * Default constructor.
142 */
144 {}
145
146
147 /**
148 * Constructor.
149 *
150 * \param bounds bounds
151 */
153 {
154 this->configure(bounds);
155 }
156
157
158 /**
159 * Constructor.
160 *
161 * \param bounds bounds
162 */
164 {
165 this->configure(bounds);
166 }
167
168
169 /**
170 * Reset.
171 */
172 void reset()
173 {
175
176 for (iterator i = this->begin(); i != this->end(); ++i) {
177 i->getY() = JMATH::zero;
178 }
179 }
180
181
182 /**
183 * Fill histogram.
184 *
185 * \param pX pointer to abscissa values
186 * \param w weight
187 */
188 virtual void evaluate(const abscissa_type* pX,
190 {
191 this->fill(*pX, w);
192 }
193
194
195 /**
196 * Fill histogram.
197 *
198 * \param x abscissa value
199 * \param w weight
200 */
203 {
204 this->integral += w;
205
206 iterator p = this->lower_bound(x);
207
208 if (p == this->begin())
209 this->underflow += w;
210 else if (p == this->end())
211 this->overflow += w;
212 else
213 (--p)->getY() += w;
214 }
215
216
217 /**
218 * Rebin histogram.
219 *
220 * \param merge rebin evaluator
221 */
222 template<class JRebin_t>
223 void rebin(JRebin_t merge)
224 {
225 if (this->size() > 1u) {
226
227 iterator out = this->begin();
228
229 for (const_iterator i = this->begin(); i != this->end(); ) {
230
231 *out = *i;
232
233 while (++i != this->end() && merge(*out,*i)) {
234 out->getY() += i->getY();
235 }
236
237 ++out;
238 }
239
240 const_reverse_iterator __rbegin(out);
241
242 if (this->getDistance(__rbegin->getX(), this->rbegin()->getX()) > 0.0) {
243
244 *out = *(this->rbegin());
245
246 ++out;
247 }
248
249 this->resize(std::distance(this->begin(), out));
250 }
251 }
252
253
254 /**
255 * Add histogram.
256 *
257 * \param histogram histogram
258 * \return this histogram
259 */
260 JHistogram1D& add(const JHistogram1D& histogram)
261 {
262 collection_type::add(static_cast<const base_collection_type&>(histogram));
263 histogram_type ::add(static_cast<const histogram_type&> (histogram));
264
265 return *this;
266 }
267
268
269 /**
270 * Subtract histogram.
271 *
272 * \param histogram histogram
273 * \return this histogram
274 */
275 JHistogram1D& sub(const JHistogram1D& histogram)
276 {
277 collection_type::sub(static_cast<const base_collection_type&>(histogram));
278 histogram_type ::sub(static_cast<const histogram_type&> (histogram));
279
280 return *this;
281 }
282
283
284 /**
285 * Scale contents.
286 *
287 * \param value multiplication factor
288 * \return this histogram
289 */
290 JHistogram1D& mul(const double value)
291 {
292 collection_type::mul(value);
293 histogram_type ::mul(value);
294
295 return *this;
296 }
297
298
299 /**
300 * Scale contents.
301 *
302 * \param value division factor
303 * \return this histogram
304 */
305 JHistogram1D& div(const double value)
306 {
307 collection_type::div(value);
308 histogram_type ::div(value);
309
310 return *this;
311 }
312
313
314 /**
315 * Read histogram from input.
316 *
317 * \param in reader
318 * \param object histogram
319 * \return reader
320 */
321 friend inline JReader& operator>>(JReader& in, JHistogram1D& object)
322 {
323 in >> static_cast<histogram_type&> (object);
324 in >> static_cast<collection_type&>(object);
325
326 return in;
327 }
328
329
330 /**
331 * Write histogram to output.
332 *
333 * \param out writer
334 * \param object histogram
335 * \return writer
336 */
337 friend inline JWriter& operator<<(JWriter& out, const JHistogram1D& object)
338 {
339 out << static_cast<const histogram_type&> (object);
340 out << static_cast<const collection_type&>(object);
341
342 return out;
343 }
344 };
345
346
347 /**
348 * Template specialisation if JHistogram1D class with bin centering.
349 *
350 * This class implements the JHistogram interface.
351 */
352 template<class JAbscissa_t,
353 class JContents_t,
354 template<class, class> class JContainer_t,
355 class JDistance_t>
356 class JHistogram1D<JBin1D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t> :
357 public JContainer_t<JBin1D<JAbscissa_t, JContents_t>, JDistance_t>,
358 public JHistogram<JAbscissa_t, JContents_t>,
359 public JMath< JHistogram1D<JBin1D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t> >
360 {
361 public:
362
364
366 typedef JContainer_t<element_type, JDistance_t> collection_type;
367
368 typedef typename collection_type::collection_type base_collection_type;
369
370 typedef typename collection_type::abscissa_type abscissa_type;
371 typedef typename collection_type::ordinate_type ordinate_type;
372 typedef typename collection_type::value_type value_type;
373
374 typedef typename collection_type::const_iterator const_iterator;
375 typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
376 typedef typename collection_type::iterator iterator;
377 typedef typename collection_type::reverse_iterator reverse_iterator;
378
380
382
383 typedef JTOOLS::JRebin <value_type> JRebin;
385
386
387 /**
388 * Default constructor.
389 */
391 {}
392
393
394 /**
395 * Constructor.
396 *
397 * \param bounds bounds
398 */
400 {
401 this->configure(bounds);
402 }
403
404
405 /**
406 * Constructor.
407 *
408 * \param bounds bounds
409 */
411 {
412 this->configure(bounds);
413 }
414
415
416 /**
417 * Reset.
418 */
419 void reset()
420 {
422
423 for (iterator i = this->begin(); i != this->end(); ++i) {
424 *i = element_type(i->getX(), JMATH::zero);
425 }
426 }
427
428
429 /**
430 * Fill histogram.
431 *
432 * \param pX pointer to abscissa values
433 * \param w weight
434 */
435 virtual void evaluate(const abscissa_type* pX,
437 {
438 this->fill(*pX, w);
439 }
440
441
442 /**
443 * Fill histogram.
444 *
445 * \param x abscissa value
446 * \param w weight
447 */
450 {
451 this->integral += w;
452
453 iterator p = this->lower_bound(x);
454
455 if (p == this->begin())
456 this->underflow += w;
457 else if (p == this->end())
458 this->overflow += w;
459 else
460 (--p)->fill(x, w);
461 }
462
463
464 /**
465 * Rebin histogram.
466 *
467 * \param merge rebin evaluator
468 */
469 template<class JRebin_t>
470 void rebin(JRebin_t merge)
471 {
472 if (this->size() > 1u) {
473
474 iterator out = this->begin();
475
476 for (const_iterator i = this->begin(); i != this->end(); ) {
477
478 *out = *i;
479
480 while (++i != this->end() && merge(*out,*i)) {
481 out->add(*i);
482 }
483
484 ++out;
485 }
486
487 const_reverse_iterator __rbegin(out);
488
489 if (getDistance(__rbegin->getX(), this->rbegin()->getX()) > 0.0) {
490
491 *out = *(this->rbegin());
492
493 ++out;
494 }
495
496 this->resize(std::distance(this->begin(), out));
497 }
498 }
499
500
501 /**
502 * Scale contents.
503 *
504 * \param value multiplication factor
505 */
506 JHistogram1D& mul(const double value)
507 {
508 for (iterator i = this->begin(); i != this->end(); ++i) {
509 i->mul(value);
510 }
511
512 histogram_type::mul(value);
513
514 return *this;
515 }
516
517
518 /**
519 * Scale contents.
520 *
521 * \param value division factor
522 */
523 JHistogram1D& div(const double value)
524 {
525 for (iterator i = this->begin(); i != this->end(); ++i) {
526 i->div(value);
527 }
528
529 histogram_type::div(value);
530
531 return *this;
532 }
533
534
535 /**
536 * Read histogram from input.
537 *
538 * \param in reader
539 * \param object histogram
540 * \return reader
541 */
542 friend inline JReader& operator>>(JReader& in, JHistogram1D& object)
543 {
544 in >> static_cast<histogram_type&> (object);
545 in >> static_cast<collection_type&>(object);
546
547 return in;
548 }
549
550
551 /**
552 * Write histogram to output.
553 *
554 * \param out writer
555 * \param object histogram
556 * \return writer
557 */
558 friend inline JWriter& operator<<(JWriter& out, const JHistogram1D& object)
559 {
560 out << static_cast<const histogram_type&> (object);
561 out << static_cast<const collection_type&>(object);
562
563 return out;
564 }
565 };
566
567
568 /**
569 * Conversion of histogram to probability density function (PDF).
570 *
571 * The PDF abscissa and contents are set to the bin center and contents divided the bin width, respectively.
572 *
573 * \param input histogram
574 * \param output mappable collection
575 */
576 template<class JElement_t,
577 template<class, class> class JContainer_t,
578 class JDistance_t>
580 typename JMappable<JElement_t>::map_type& output)
581 {
582 typedef typename JElement_t::abscissa_type abscissa_type;
583 typedef typename JElement_t::ordinate_type ordinate_type;
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 = 0.5 * (i->getX() + j->getX());
591 const ordinate_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 histogram to probability density function (PDF).
602 *
603 * The PDF abscissa and contents are set to the bin center and contents divided the bin width, respectively.
604 *
605 * \param input histogram
606 * \param output mappable collection
607 */
608 template<class JAbscissa_t,
609 class JContents_t,
610 template<class, class> class JContainer_t,
611 class JDistance_t>
612 inline void makePDF(const JHistogram1D<JBin1D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t>& input,
614 {
615 typedef JAbscissa_t abscissa_type;
616 typedef JContents_t contents_type;
617 typedef typename JHistogram1D<JBin1D<JAbscissa_t, JContents_t>, JContainer_t, JDistance_t>::const_iterator const_iterator;
618
619 if (input.getSize() > 1) {
620
621 for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
622
623 const abscissa_type x = i->getBinCenter();
624 const contents_type y = i->getY();
625 const double w = input.getDistance(i->getX(), j->getX());
626
627 output.put(x, y/w);
628 }
629 }
630 }
631
632
633 /**
634 * Conversion of data points to integral values.
635 *
636 * The integration is based on the sum of bin contents of the input data points.
637 *
638 * \param input histogram
639 * \param output mappable collection
640 * \return integral
641 */
642 template<class JElement_t,
643 template<class, class> class JContainer_t,
644 class JDistance_t>
645 inline typename JElement_t::ordinate_type
647 typename JMappable<JElement_t>::map_type& output)
648 {
649 typedef typename JElement_t::ordinate_type ordinate_type;
651
652 ordinate_type V(JMATH::zero);
653
654 if (input.getSize() > 1) {
655
656 output.put(input.begin()->getX(), V);
657
658 for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
659
660 V += i->getY();
661
662 output.put(j->getX(), V);
663 }
664 }
665
666 return V;
667 }
668}
669
670#endif
General purpose class for a collection of sorted elements.
The elements in a collection are sorted according to their abscissa values and a given distance opera...
Base class for data structures with artithmetic capabilities.
Interface for binary input.
Interface for binary output.
virtual void evaluate(const abscissa_type *pX, typename JClass< contents_type >::argument_type w)
Fill histogram.
JHistogram1D(const JAbstractHistogram< abscissa_type > &bounds)
Constructor.
friend JWriter & operator<<(JWriter &out, const JHistogram1D &object)
Write histogram to output.
friend JReader & operator>>(JReader &in, JHistogram1D &object)
Read histogram from input.
void fill(typename JClass< abscissa_type >::argument_type x, typename JClass< contents_type >::argument_type w)
Fill histogram.
JHistogram1D(const JAbstractCollection< abscissa_type > &bounds)
Constructor.
Histogram in 1D.
void rebin(JRebin_t merge)
Rebin histogram.
JTOOLS::JRebin< value_type > JRebin
JHistogram1D & mul(const double value)
Scale contents.
histogram_type::contents_type contents_type
JHistogram1D(const JAbstractCollection< abscissa_type > &bounds)
Constructor.
JHistogram1D()
Default constructor.
JTOOLS::JContent< value_type > JContent
JHistogram< abscissa_type, ordinate_type > histogram_type
collection_type::const_reverse_iterator const_reverse_iterator
collection_type::collection_type base_collection_type
void fill(typename JClass< abscissa_type >::argument_type x, typename JClass< contents_type >::argument_type w)
Fill histogram.
collection_type::ordinate_type ordinate_type
friend JWriter & operator<<(JWriter &out, const JHistogram1D &object)
Write histogram to output.
collection_type::abscissa_type abscissa_type
friend JReader & operator>>(JReader &in, JHistogram1D &object)
Read histogram from input.
collection_type::iterator iterator
JHistogram1D & add(const JHistogram1D &histogram)
Add histogram.
collection_type::value_type value_type
collection_type::const_iterator const_iterator
JHistogram1D & div(const double value)
Scale contents.
JHistogram1D & sub(const JHistogram1D &histogram)
Subtract histogram.
collection_type::reverse_iterator reverse_iterator
JHistogram1D(const JAbstractHistogram< abscissa_type > &bounds)
Constructor.
virtual void evaluate(const abscissa_type *pX, typename JClass< contents_type >::argument_type w)
Fill histogram.
JContainer_t< JElement_t, JDistance_t > collection_type
Template definition of histogram object interface.
Definition JHistogram.hh:36
JHistogram & mul(const double value)
Scale histogram.
JHistogram & div(double value)
Scale histogram.
JContents_t contents_type
Definition JHistogram.hh:51
void reset()
Reset.
Definition JHistogram.hh:64
static const JZero zero
Function object to assign zero value.
Definition JZero.hh:105
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
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.
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 configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
const int n
Definition JPolint.hh:791
int j
Definition JPolint.hh:801
Template for generic class types.
Definition JClass.hh:80
JArgument< T >::argument_type argument_type
Definition JClass.hh:82
Auxiliary base class for aritmetic operations of derived class types.
Definition JMath.hh:347
Abstract interface for abscissa values of a collection of elements.
Simple data structure for histogram binning.
1D Binned element with bin centering.
Definition JElement.hh:355
Auxiliary class for merging of consecutive bins until minimal content is reached.
const contents_type __y
JElement_t::ordinate_type contents_type
bool operator()(const value_type &first, const value_type &second) const
Test whether bins should be merged.
JElement_t value_type
JContent(const contents_type y)
Constructor.
Template class for distance evaluation.
Definition JDistance.hh:24
Template interface definition for associative collection of 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.
Auxiliary class for merging of fixed number of consecutive bins.
JElement_t value_type
JRebin(const int n)
Constructor.
bool operator()(const value_type &first, const value_type &second) const
Test whether bins should be merged.
JElement_t::ordinate_type contents_type