Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JElement.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JELEMENT__
2#define __JTOOLS__JELEMENT__
3
4#include <cmath>
5
6#include "JMath/JZero.hh"
7#include "JMath/JMath.hh"
8#include "JLang/JClass.hh"
9#include "JLang/JException.hh"
10#include "JIO/JSerialisable.hh"
11
12
13/**
14 * \file
15 *
16 * The elements in a collection are sorted according to their abscissa values and a given distance operator.
17 * These elements should have the following type definitions and member methods:
18 * <pre>
19 * typedef <abscissa type> abscissa_type;
20 * typedef <ordinate type> ordinate_type;
21 *
22 * constructor();
23 * constructor(abscissa_type, ordinate_type);
24 *
25 * abscissa_type getX() const;
26 * const ordinate_type& getY() const;
27 * ordinate_type& getY();
28 * </pre>
29 * \author mdejong
30 */
31
32namespace JTOOLS {}
33namespace JPP { using namespace JTOOLS; }
34
35namespace JTOOLS {
36
37 using JIO::JReader;
38 using JIO::JWriter;
39 using JMATH::JMath;
40 using JMATH::getZero;
41
42
43 /**
44 * 2D Element.
45 */
46 template<class JAbscissa_t, class JOrdinate_t>
47 struct JElement2D {
48
49 typedef JAbscissa_t abscissa_type;
50 typedef JOrdinate_t ordinate_type;
51
52
53 /**
54 * Default constructor.
55 */
57 __x(getZero<abscissa_type>()),
58 __y(getZero<ordinate_type>())
59 {}
60
61
62 /**
63 * Constructor.
64 *
65 * \param x abscissa value
66 * \param y ordinate value
67 */
73
74
75 /**
76 * Get abscissa value.
77 *
78 * \return abscissa value
79 */
81 {
82 return __x;
83 }
84
85
86 /**
87 * Get ordinate value.
88 *
89 * \return ordinate value
90 */
91 const ordinate_type& getY() const
92 {
93 return __y;
94 }
95
96
97 /**
98 * Get ordinate value.
99 *
100 * \return ordinate value
101 */
103 {
104 return __y;
105 }
106
107
108 /**
109 * Read element from input.
110 *
111 * \param in reader
112 * \param element element
113 * \return reader
114 */
115 friend inline JReader& operator>>(JReader& in, JElement2D& element)
116 {
117 in >> element.__x;
118 in >> element.__y;
119
120 return in;
121 }
122
123
124 /**
125 * Write element to output.
126 *
127 * \param out writer
128 * \param element element
129 * \return writer
130 */
131 friend inline JWriter& operator<<(JWriter& out, const JElement2D& element)
132 {
133 out << element.__x;
134 out << element.__y;
135
136 return out;
137 }
138
139
140 protected:
143 };
144
145
146 /**
147 * 2D Element for spline interpolations.
148 *
149 * Note that the internal data members needed for the calculation
150 * of the 2nd derivatives are not subject to I/O, i.e.\ the I/O of
151 * this class is identical to that of the JElement2D class.
152 */
153 template<class JAbscissa_t, class JOrdinate_t>
154 struct JSplineElement2D :
155 public JElement2D<JAbscissa_t, JOrdinate_t>
156 {
157
161
162
163 /**
164 * Default constructor.
165 */
167 element_type(),
168 __u(getZero<ordinate_type>())
169 {}
170
171
172 /**
173 * Constructor.
174 *
175 * \param x abscissa value
176 * \param y ordinate value
177 */
183
184
185 /**
186 * Get derivative.
187 *
188 * \return derivative
189 */
191 {
192 return __u;
193 }
194
195
196 /**
197 * Set derivative.
198 *
199 * \param u derivative
200 */
202 {
203 __u= u;
204 }
205
206
207 protected:
209 };
210
211
212 /**
213 * 2D Element for spline interpolations.
214 *
215 * Note that the internal data members needed for the calculation
216 * of the integral values are not subject to I/O, i.e.\ the I/O of
217 * this class is identical to that of the JElement2D class.
218 */
219 template<class JAbscissa_t, class JOrdinate_t>
221 public JSplineElement2D<JAbscissa_t, JOrdinate_t>
222 {
223
227
228
229 /**
230 * Default constructor.
231 */
233 element_type(),
234 __v(getZero<ordinate_type>())
235 {}
236
237
238 /**
239 * Constructor.
240 *
241 * \param x abscissa value
242 * \param y ordinate value
243 */
249
250
251 /**
252 * Get integral.
253 *
254 * \return integral
255 */
257 {
258 return __v;
259 }
260
261
262 /**
263 * Set integral.
264 *
265 * \param v integral
266 */
268 {
269 __v = v;
270 }
271
272
273 protected:
275 };
276
277
278 /**
279 * 2D Element for polynomial interpolations.
280 *
281 * Note that the internal data members needed for the calculation
282 * of the integral values are not subject to I/O, i.e.\ the I/O of
283 * this class is identical to that of the JElement2D class.
284 */
285 template<class JAbscissa_t, class JOrdinate_t>
287 public JElement2D<JAbscissa_t, JOrdinate_t>
288 {
289
293
294
295 /**
296 * Default constructor.
297 */
299 element_type(),
300 __v(getZero<ordinate_type>())
301 {}
302
303
304 /**
305 * Constructor.
306 *
307 * \param x abscissa value
308 * \param y ordinate value
309 */
315
316
317 /**
318 * Get integral.
319 *
320 * \return integral
321 */
323 {
324 return __v;
325 }
326
327
328 /**
329 * Set integral.
330 *
331 * \param v integral
332 */
334 {
335 __v = v;
336 }
337
338
339 protected:
341 };
342
343
344 /**
345 * 1D Binned element with bin centering.
346 *
347 * Note that the internal data members needed for the calculation
348 * of the bin center are not subject to I/O, i.e.\ the I/O of
349 * this class is identical to that of the JElement2D class.
350 */
351 template<class JAbscissa_t, class JOrdinate_t>
352 struct JBin1D :
353 public JElement2D<JAbscissa_t, JOrdinate_t>,
354 public JMath< JBin1D<JAbscissa_t, JOrdinate_t> >
355 {
356
358
361
362
363 /**
364 * Default constructor.
365 */
367 element_type(),
368 __u (getZero<ordinate_type>()),
369 __w2(getZero<ordinate_type>())
370 {}
371
372
373 /**
374 * Constructor.
375 *
376 * \param x abscissa value
377 * \param y ordinate value (sum of weights)
378 * \param u sum of weighted abscissa values
379 * \param w2 sum of squared weights
380 */
385 element_type(x,y),
386 __u (u),
387 __w2(w2)
388 {
389 using namespace JPP;
390
391 if (w2 < getZero<ordinate_type>()) {
392 THROW(JValueOutOfRange, "JBin1D::JBin1D(): Invalid sum of squared weights " << w2);
393 }
394 }
395
396
397 /**
398 * Constructor.
399 *
400 * \param x abscissa value
401 * \param y ordinate value (sum of weights)
402 */
407
408
409 /**
410 * Add abscissa value.
411 *
412 * \param x abscissa value
413 * \param w weight
414 */
417 {
418 this->__y += w;
419 this->__u += w * x;
420 this->__w2 += w * w;
421 }
422
423
424 /**
425 * Get bin center.
426 *
427 * \return center
428 */
430 {
431 if (this->__y != getZero<ordinate_type>())
432 return this->__u / this->__y;
433 else
434 return this->__x;
435 }
436
437
438 /**
439 * Get bin content.
440 *
441 * \return content
442 */
443 ordinate_type getBinContent() const
444 {
445 return this->__y;
446 }
447
448
449 /**
450 * Get bin error.
451 *
452 * \return error
453 */
454 ordinate_type getBinError() const
455 {
456 return sqrt(this->__w2);
457 }
458
459
460 /**
461 * Get effective bin content.\n
462 * For a bin filled with identical weights the return value of this function\n
463 * corresponds to a Poisson count with equivalent relative error.
464 *
465 * \return effective bin content
466 */
467 ordinate_type getEffectiveBinContent() const
468 {
469 using namespace JPP;
470
471 const ordinate_type y = getBinContent();
472 const ordinate_type ey = getBinError();
473
474 if (ey > getZero<ordinate_type>()) {
475 return y*y/ey/ey;
476 } else {
477 THROW(JDivisionByZero, "JBin1D::getEffectiveBinContent(): Invalid effective bin content (bin content error is zero)");
478 }
479 }
480
481
482 /**
483 * Negate bin.\n
484 * Note: This method should only be used for binned elements which can hold negative weights.
485 *
486 * \return this bin
487 */
489 {
490 this->__y = -this->__y;
491
492 return *this;
493 }
494
495
496 /**
497 * Add bin.
498 *
499 * \param bin bin
500 * \return this bin
501 */
502 JBin1D& add(const JBin1D& bin)
503 {
504 this->__y += bin.__y;
505 this->__u += bin.__u;
506 this->__w2 += bin.__w2;
507
508 return *this;
509 }
510
511
512 /**
513 * Subtract bin.
514 *
515 * \param bin bin
516 * \return this bin
517 */
518 JBin1D& sub(const JBin1D& bin)
519 {
520 this->__y -= bin.__y;
521 this->__u -= bin.__u;
522 this->__w2 += bin.__w2;
523
524 return *this;
525 }
526
527
528 /**
529 * Scale contents.
530 *
531 * \param value multiplication factor
532 * \return this bin
533 */
534 JBin1D& mul(const double value)
535 {
536 this->__y *= value;
537 this->__u *= value;
538 this->__w2 *= value*value;
539
540 return *this;
541 }
542
543
544 /**
545 * Divide contents.
546 *
547 * \param value division factor
548 * \return this bin
549 */
550 JBin1D& div(const double value)
551 {
552 this->__y /= value;
553 this->__u /= value;
554 this->__w2 /= value*value;
555
556 return *this;
557 }
558
559
560 /**
561 * Multiply bins.
562 *
563 * \param bin bin
564 * \return this bin
565 */
566 JBin1D& mul(const JBin1D& bin)
567 {
568 const double y1 = this->getBinContent();
569 const double ey1 = this->getBinError();
570
571 const double y2 = bin.getBinContent();
572 const double ey2 = bin.getBinError();
573
574 this->__y *= y2;
575 this->__u *= y2;
576 this->__w2 = ((ey1 * ey1 * y2 * y2) +
577 (ey2 * ey2 * y1 * y1));
578
579 return *this;
580 }
581
582
583 protected:
584 abscissa_type __u; //!< Sum of weighted abscissa values
585 ordinate_type __w2; //!< Sum of squared weights
586 };
587
588
589 /**
590 * 3D Element.
591 */
592 template<class JAbscissa_t, class JOrdinate_t>
593 struct JElement3D {
594
595 typedef JAbscissa_t abscissa_type;
596 typedef JOrdinate_t ordinate_type;
597
598
599 /**
600 * Default constructor.
601 */
603 __x(getZero<abscissa_type>()),
604 __y(getZero<abscissa_type>()),
605 __z(getZero<ordinate_type>())
606 {}
607
608
609 /**
610 * Constructor.
611 *
612 * \param x abscissa value
613 * \param y abscissa value
614 * \param z ordinate value
615 */
623
624
625 /**
626 * Get abscissa value.
627 *
628 * \return abscissa value
629 */
631 {
632 return __x;
633 }
634
635
636 /**
637 * Get abscissa value.
638 *
639 * \return abscissa value
640 */
642 {
643 return __y;
644 }
645
646
647 /**
648 * Get ordinate value.
649 *
650 * \return ordinate value
651 */
652 const ordinate_type& getZ() const
653 {
654 return __y;
655 }
656
657
658 /**
659 * Get ordinate value.
660 *
661 * \return ordinate value
662 */
664 {
665 return __y;
666 }
667
668
669 /**
670 * Read element from input.
671 *
672 * \param in reader
673 * \param element element
674 * \return reader
675 */
676 friend inline JReader& operator>>(JReader& in, JElement3D& element)
677 {
678 in >> element.__x;
679 in >> element.__y;
680 in >> element.__z;
681
682 return in;
683 }
684
685
686 /**
687 * Write element to output.
688 *
689 * \param out writer
690 * \param element element
691 * \return writer
692 */
693 friend inline JWriter& operator<<(JWriter& out, const JElement3D& element)
694 {
695 out << element.__x;
696 out << element.__y;
697 out << element.__z;
698
699 return out;
700 }
701
702
703 protected:
707 };
708}
709
710#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Base class for data structures with artithmetic capabilities.
Definition of zero value for any class.
Interface for binary input.
Interface for binary output.
Exception for division by zero.
Exception for accessing a value in a collection that is outside of its range.
T getZero()
Get zero value for a given data type.
Definition JZero.hh:26
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
JArgument< T >::argument_type argument_type
Definition JClass.hh:82
Auxiliary base class for aritmetic operations of derived class types.
Definition JMath.hh:347
1D Binned element with bin centering.
Definition JElement.hh:355
JBin1D()
Default constructor.
Definition JElement.hh:366
JBin1D & negate()
Negate bin.
Definition JElement.hh:488
JBin1D(typename JLANG::JClass< abscissa_type >::argument_type x, typename JLANG::JClass< ordinate_type >::argument_type y)
Constructor.
Definition JElement.hh:403
JBin1D & mul(const double value)
Scale contents.
Definition JElement.hh:534
JBin1D(typename JLANG::JClass< abscissa_type >::argument_type x, typename JLANG::JClass< ordinate_type >::argument_type y, typename JLANG::JClass< abscissa_type >::argument_type u, typename JLANG::JClass< ordinate_type >::argument_type w2)
Constructor.
Definition JElement.hh:381
JBin1D & add(const JBin1D &bin)
Add bin.
Definition JElement.hh:502
abscissa_type getBinCenter() const
Get bin center.
Definition JElement.hh:429
JBin1D & mul(const JBin1D &bin)
Multiply bins.
Definition JElement.hh:566
ordinate_type getBinContent() const
Get bin content.
Definition JElement.hh:443
abscissa_type __u
Sum of weighted abscissa values.
Definition JElement.hh:584
element_type::ordinate_type ordinate_type
Definition JElement.hh:360
JBin1D & div(const double value)
Divide contents.
Definition JElement.hh:550
void fill(typename JLANG::JClass< abscissa_type >::argument_type x, typename JLANG::JClass< ordinate_type >::argument_type w)
Add abscissa value.
Definition JElement.hh:415
ordinate_type getEffectiveBinContent() const
Get effective bin content.
Definition JElement.hh:467
element_type::abscissa_type abscissa_type
Definition JElement.hh:359
ordinate_type __w2
Sum of squared weights.
Definition JElement.hh:585
JElement2D< JAbscissa_t, JOrdinate_t > element_type
Definition JElement.hh:357
JBin1D & sub(const JBin1D &bin)
Subtract bin.
Definition JElement.hh:518
ordinate_type getBinError() const
Get bin error.
Definition JElement.hh:454
2D Element.
Definition JPolint.hh:1131
friend JReader & operator>>(JReader &in, JElement2D &element)
Read element from input.
Definition JElement.hh:115
ordinate_type & getY()
Get ordinate value.
Definition JElement.hh:102
ordinate_type __y
Definition JElement.hh:142
abscissa_type getX() const
Get abscissa value.
Definition JElement.hh:80
JElement2D()
Default constructor.
Definition JElement.hh:56
friend JWriter & operator<<(JWriter &out, const JElement2D &element)
Write element to output.
Definition JElement.hh:131
JOrdinate_t ordinate_type
Definition JElement.hh:50
const ordinate_type & getY() const
Get ordinate value.
Definition JElement.hh:91
abscissa_type __x
Definition JElement.hh:141
JAbscissa_t abscissa_type
Definition JElement.hh:49
JElement2D(typename JLANG::JClass< abscissa_type >::argument_type x, typename JLANG::JClass< ordinate_type >::argument_type y)
Constructor.
Definition JElement.hh:68
3D Element.
Definition JElement.hh:593
JElement3D()
Default constructor.
Definition JElement.hh:602
friend JReader & operator>>(JReader &in, JElement3D &element)
Read element from input.
Definition JElement.hh:676
abscissa_type getY() const
Get abscissa value.
Definition JElement.hh:641
abscissa_type __x
Definition JElement.hh:704
friend JWriter & operator<<(JWriter &out, const JElement3D &element)
Write element to output.
Definition JElement.hh:693
ordinate_type & getZ()
Get ordinate value.
Definition JElement.hh:663
JElement3D(typename JLANG::JClass< abscissa_type >::argument_type x, typename JLANG::JClass< abscissa_type >::argument_type y, typename JLANG::JClass< ordinate_type >::argument_type z)
Constructor.
Definition JElement.hh:616
const ordinate_type & getZ() const
Get ordinate value.
Definition JElement.hh:652
abscissa_type __y
Definition JElement.hh:705
JAbscissa_t abscissa_type
Definition JElement.hh:595
ordinate_type __z
Definition JElement.hh:706
abscissa_type getX() const
Get abscissa value.
Definition JElement.hh:630
JOrdinate_t ordinate_type
Definition JElement.hh:596
2D Element for polynomial interpolations.
Definition JElement.hh:288
void setIntegral(typename JLANG::JClass< ordinate_type >::argument_type v)
Set integral.
Definition JElement.hh:333
element_type::abscissa_type abscissa_type
Definition JElement.hh:291
element_type::ordinate_type ordinate_type
Definition JElement.hh:292
JPolintElement2S()
Default constructor.
Definition JElement.hh:298
JPolintElement2S(typename JLANG::JClass< abscissa_type >::argument_type x, typename JLANG::JClass< ordinate_type >::argument_type y)
Constructor.
Definition JElement.hh:310
JElement2D< JAbscissa_t, JOrdinate_t > element_type
Definition JElement.hh:290
ordinate_type getIntegral() const
Get integral.
Definition JElement.hh:322
2D Element for spline interpolations.
Definition JSpline.hh:770
JElement2D< JAbscissa_t, JOrdinate_t > element_type
Definition JElement.hh:158
JSplineElement2D(typename JLANG::JClass< abscissa_type >::argument_type x, typename JLANG::JClass< ordinate_type >::argument_type y)
Constructor.
Definition JElement.hh:178
void setU(typename JLANG::JClass< ordinate_type >::argument_type u)
Set derivative.
Definition JElement.hh:201
JSplineElement2D()
Default constructor.
Definition JElement.hh:166
ordinate_type getU() const
Get derivative.
Definition JElement.hh:190
element_type::abscissa_type abscissa_type
Definition JElement.hh:159
element_type::ordinate_type ordinate_type
Definition JElement.hh:160
2D Element for spline interpolations.
Definition JElement.hh:222
ordinate_type getIntegral() const
Get integral.
Definition JElement.hh:256
element_type::ordinate_type ordinate_type
Definition JElement.hh:226
element_type::abscissa_type abscissa_type
Definition JElement.hh:225
JSplineElement2S(typename JLANG::JClass< abscissa_type >::argument_type x, typename JLANG::JClass< ordinate_type >::argument_type y)
Constructor.
Definition JElement.hh:244
void setIntegral(typename JLANG::JClass< ordinate_type >::argument_type v)
Set integral.
Definition JElement.hh:267
JSplineElement2S()
Default constructor.
Definition JElement.hh:232
JSplineElement2D< JAbscissa_t, JOrdinate_t > element_type
Definition JElement.hh:224