Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JResult.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JRESULT__
2 #define __JTOOLS__JRESULT__
3 
4 #include <cmath>
5 
6 #include "JTools/JRange.hh"
7 #include "JLang/JClass.hh"
8 #include "JMath/JMath.hh"
9 #include "JMath/JZero.hh"
10 
11 
12 /**
13  * \file
14  *
15  * This include file containes various data structures that
16  * can be used as specific return types for the interpolating methods.
17  * These data structures have standard arithmetic capabilities and
18  * are templated so that they can be expanded in higher dimensions.
19  * \author mdejong
20  */
21 
22 namespace JTOOLS {}
23 namespace JPP { using namespace JTOOLS; }
24 
25 namespace JTOOLS {
26 
27 
28  /**
29  * Data structure for result including value and first derivative of function.
30  *
31  * This data structure contains the following data mambers:
32  * <pre>
33  * JResultHesse::f = function value;
34  * JResultHesse::fp = first derivative;
35  * </pre>
36  *
37  * This class implements the JMATH::JMath interface.
38  */
39  template<class JResult_t>
40  struct JResultHesse :
41  public JMATH::JMath< JResultHesse<JResult_t> >
42  {
43 
45 
46 
47  /**
48  * Default constructor.
49  */
51  f (JMATH::zero),
52  fp(JMATH::zero)
53  {}
54 
55 
56  /**
57  * Constructor.
58  *
59  * \param __f function value
60  * \param __fp first derivative
61  */
63  argument_type __fp) :
64  f (__f ),
65  fp(__fp)
66  {}
67 
68 
69  /**
70  * Prefix unary minus for function value of PDF.
71  *
72  * \return function value of PDF
73  */
75  {
76  f = -f;
77  fp = -fp;
78 
79  return *this;
80  }
81 
82 
83  /**
84  * Addition operator for function value of PDF.
85  *
86  * \param value function value of PDF
87  * \return function value of PDF
88  */
89  JResultHesse& add(const JResultHesse& value)
90  {
91  f += value.f;
92  fp += value.fp;
93 
94  return *this;
95  }
96 
97 
98  /**
99  * Subtraction operator for function value of PDF.
100  *
101  * \param value function value of PDF
102  * \return function value of PDF
103  */
105  {
106  f -= value.f;
107  fp -= value.fp;
108 
109  return *this;
110  }
111 
112 
113  /**
114  * Multiplication operator for function value of PDF.
115  *
116  * \param value multiplication factor
117  * \return function value of PDF
118  */
119  JResultHesse& mul(const double value)
120  {
121  f *= value;
122  fp *= value;
123 
124  return *this;
125  }
126 
127 
128  /**
129  * Division operator for function value of PDF.
130  *
131  * \param value multiplication factor
132  * \return function value of PDF
133  */
134  JResultHesse& div(const double value)
135  {
136  f /= value;
137  fp /= value;
138 
139  return *this;
140  }
141 
142 
143  /**
144  * Get probability.
145  * If the given hit is false (true), the return value corresponds to the Poisson probability
146  * that zero (one or more) hits occur for the given expectation value JResultHesse::f.
147  *
148  * \param hit hit (or not)
149  * \return probability
150  */
151  double getP(const bool hit) const
152  {
153  if (!hit)
154  return exp(-f); // probability of 0 hits
155  else
156  return 1.0 - getP(false); // probability of 1 or more hits
157  }
158 
159 
160  /**
161  * Get chi2.
162  * The chi2 corresponds to <tt>-log(P)</tt>, where <tt>P</tt> is the probability JResultHesse::f.
163  *
164  * \param hit hit (or not)
165  * \return chi2
166  */
167  double getChi2(const bool hit) const
168  {
169  if (!hit)
170  return f; // = -log(getP(false))
171  else
172  return -log(getP(true));
173  }
174 
175 
176  /**
177  * Get derivative of chi2.
178  *
179  * \param hit hit (or not)
180  * \return derivative
181  */
182  double getDerivativeOfChi2(const bool hit) const
183  {
184  if (!hit)
185  return fp;
186  else
187  return -fp * getP(false) / getP(true);
188  }
189 
190 
191  JResult_t f; //!< function value
192  JResult_t fp; //!< first derivative
193  };
194 
195 
196  /**
197  * Data structure for result including value, first derivative and integrals of function.
198  *
199  * This data structure contains the following data mambers:
200  * <pre>
201  * JResultPDF::f = function value;
202  * JResultPDF::fp = first derivative;
203  * JResultPDF::v = partial integral;
204  * JResultPDF::V = complete integral.
205  * </pre>
206  * The partial and complete integrals are used to evaluate the probability of the first hit.
207  *
208  * This class implements the JMATH::JMath interface.
209  */
210  template<class JResult_t>
211  struct JResultPDF :
212  public JMATH::JMath< JResultPDF<JResult_t> >
213  {
214 
216 
217 
218  /**
219  * Default constructor.
220  */
222  f (JMATH::zero),
223  fp(JMATH::zero),
224  v (JMATH::zero),
225  V (JMATH::zero)
226  {}
227 
228 
229  /**
230  * Constructor.
231  *
232  * \param __f function value
233  * \param __fp first derivative
234  * \param __v integral <xmin,x]
235  * \param __V integral <xmin,xmax>
236  */
238  argument_type __fp,
239  argument_type __v,
240  argument_type __V) :
241  f (__f ),
242  fp(__fp),
243  v (__v),
244  V (__V)
245  {}
246 
247 
248  /**
249  * Constructor.
250  * This constructor refers to the result of a signal with a constant rate <tt>R</tt>
251  * to produce an event occuring at the given moment <tt>x</tt> within the fixed range <tt>X</tt>.
252  *
253  * \param R rate
254  * \param x abscissa value
255  * \param X abscissa range
256  */
258  argument_type x,
259  const JRange<JResult_t>& X) :
260  f (R),
261  fp(JMATH::zero),
262  v (R * (X.constrain(x) - X.getLowerLimit())),
263  V (R * (X.getUpperLimit() - X.getLowerLimit()))
264  {}
265 
266 
267  /**
268  * Prefix unary minus for function value of PDF.
269  *
270  * \return function value of PDF
271  */
273  {
274  f = -f;
275  fp = -fp;
276  v = -v;
277  V = -V;
278 
279  return *this;
280  }
281 
282 
283  /**
284  * Addition operator for function value of PDF.
285  *
286  * \param value function value of PDF
287  * \return function value of PDF
288  */
289  JResultPDF& add(const JResultPDF& value)
290  {
291  f += value.f;
292  fp += value.fp;
293  v += value.v;
294  V += value.V;
295 
296  return *this;
297  }
298 
299 
300  /**
301  * Subtraction operator for function value of PDF.
302  *
303  * \param value function value of PDF
304  * \return function value of PDF
305  */
306  JResultPDF& sub(const JResultPDF& value)
307  {
308  f -= value.f;
309  fp -= value.fp;
310  v -= value.v;
311  V -= value.V;
312 
313  return *this;
314  }
315 
316 
317  /**
318  * Multiplication operator for function value of PDF.
319  *
320  * \param value multiplication factor
321  * \return function value of PDF
322  */
323  JResultPDF& mul(const double value)
324  {
325  f *= value;
326  fp *= value;
327  v *= value;
328  V *= value;
329 
330  return *this;
331  }
332 
333 
334  /**
335  * Division operator for function value of PDF.
336  *
337  * \param value division factor
338  * \return function value of PDF
339  */
340  JResultPDF& div(const double value)
341  {
342  f /= value;
343  fp /= value;
344  v /= value;
345  V /= value;
346 
347  return *this;
348  }
349 
350 
351  /**
352  * Get probability of first hit.
353  * The probability is defined at the moment JResultPDF::f and JResultPDF::v have been evaluated
354  * and it is normalised to the total interval corresponding to JResultPDF::V.
355  *
356  * \return probability
357  */
358  double getP() const
359  {
360  return exp(-v) * f / (1.0 - exp(-V));
361  }
362 
363 
364  /**
365  * Get chi2 of first hit.
366  * The chi2 corresponds to <tt>-log(P)</tt>, where <tt>P</tt> is the probability JResultPDF::f.
367  *
368  * \return chi2
369  */
370  double getChi2() const
371  {
372  return -log(getP());
373  }
374 
375 
376  /**
377  * Get derivative of chi2 of first hit.
378  *
379  * \return derivative
380  */
381  double getDerivativeOfChi2() const
382  {
383  return fp/f - f;
384  }
385 
386 
387  JResult_t f; //!< function value
388  JResult_t fp; //!< first derivative
389  JResult_t v; //!< integral <xmin,x]
390  JResult_t V; //!< integral <xmin,xmax>
391  };
392 
393 
394  /**
395  * Data structure for result including value and <tt>N</tt> derivatives of function.
396  *
397  * This data structure contains the following data mambers:
398  * <pre>
399  * JResultPolynome::y[0] = function value;
400  * JResultPolynome::y[i] = ith derivative;
401  * JResultPolynome::y[N] = Nth derivative;
402  * </pre>
403  *
404  * This class implements the JMATH::JMath interface.
405  */
406  template<unsigned int N, class JResult_t>
408  public JMATH::JMath< JResultPolynome<N, JResult_t> >
409  {
410 
412 
413  static const int NUMBER_OF_POINTS = N + 1; // number of points (starting at 0)
414 
415 
416  /**
417  * Default constructor.
418  */
420  {
421  for (int i = 0; i != NUMBER_OF_POINTS; ++i) {
422  y[i] = JMATH::zero;
423  }
424  }
425 
426 
427  /**
428  * Prefix unary minus for function value of PDF.
429  *
430  * \return function value of PDF
431  */
433  {
434  for (int i = 0; i != NUMBER_OF_POINTS; ++i) {
435  y[i] = -y[i];
436  }
437 
438  return *this;
439  }
440 
441 
442  /**
443  * Addition operator for function value of PDF.
444  *
445  * \param value function value of PDF
446  * \return function value of PDF
447  */
449  {
450  for (int i = 0; i != NUMBER_OF_POINTS; ++i) {
451  y[i] += value.y[i];
452  }
453 
454  return *this;
455  }
456 
457 
458  /**
459  * Subtraction operator for function value of PDF.
460  *
461  * \param value function value of PDF
462  * \return function value of PDF
463  */
465  {
466  for (int i = 0; i != NUMBER_OF_POINTS; ++i) {
467  y[i] -= value.y[i];
468  }
469 
470  return *this;
471  }
472 
473 
474  /**
475  * Multiplication operator for function value of PDF.
476  *
477  * \param value multiplication factor
478  * \return function value of PDF
479  */
480  JResultPolynome& mul(const double value)
481  {
482  for (int i = 0; i != NUMBER_OF_POINTS; ++i) {
483  y[i] *= value;
484  }
485 
486  return *this;
487  }
488 
489 
490  /**
491  * Division operator for function value of PDF.
492  *
493  * \param value multiplication factor
494  * \return function value of PDF
495  */
496  JResultPolynome& div(const double value)
497  {
498  for (int i = 0; i != NUMBER_OF_POINTS; ++i) {
499  y[i] /= value;
500  }
501 
502  return *this;
503  }
504 
505 
506  /**
507  * Function value.
508  *
509  * \param x abscissa value
510  * \return function value
511  */
512  double getValue(const double x) const
513  {
514  double w = 0.0;
515  double z = 1.0;
516 
517  for (int i = 0; i != NUMBER_OF_POINTS; ++i, z *= x / i) {
518  w += y[i] * z;
519  }
520 
521  return w;
522  }
523 
524 
525  /**
526  * Function value.
527  *
528  * \param x abscissa value
529  * \return function value
530  */
531  double operator()(const double x) const
532  {
533  return getValue(x);
534  }
535 
536 
537  JResult_t y[NUMBER_OF_POINTS]; //!< values
538  };
539 
540 
541  /**
542  * Auxiliary class to recursively evaluate to a result.
543  */
544  template<class T>
546  {
547  typedef T result_type;
548 
549  /**
550  * Get function value.
551  *
552  * \return result
553  */
555  {
556  return value;
557  }
558 
559  /**
560  * Get derivative value.
561  *
562  * \return result
563  */
565  {
566  return value;
567  }
568 
569  /**
570  * Get partial integral value.
571  *
572  * \return result
573  */
575  {
576  return value;
577  }
578  };
579 
580 
581  /**
582  * Template specialisation of JResultEvaluator for JResultHesse.
583  */
584  template<class T>
586  {
588 
589  /**
590  * Get function value.
591  *
592  * \return result
593  */
594  static result_type get_value(const JResultHesse<T>& value)
595  {
596  return JResultEvaluator<T>::get_value(value.f);
597  }
598 
599  /**
600  * Get derivative value.
601  *
602  * \return result
603  */
605  {
606  return JResultEvaluator<T>::get_value(value.fp);
607  }
608 
609  /**
610  * Get partial integral value.
611  *
612  * \return result
613  */
615  {
616  return JMATH::zero;
617  }
618  };
619 
620 
621  /**
622  * Template specialisation of JResultEvaluator for JResultPDF.
623  */
624  template<class T>
626  {
628 
629  /**
630  * Get function value.
631  *
632  * \return result
633  */
634  static result_type get_value(const JResultPDF<T>& value)
635  {
636  return JResultEvaluator<T>::get_value(value.f);
637  }
638 
639  /**
640  * Get derivative value.
641  *
642  * \return result
643  */
645  {
646  return JResultEvaluator<T>::get_value(value.fp);
647  }
648 
649  /**
650  * Get partial integral value.
651  *
652  * \return result
653  */
655  {
656  return JResultEvaluator<T>::get_value(value.v);
657  }
658  };
659 
660 
661  /**
662  * Template specialisation of JResultEvaluator for JResultPolynome.
663  */
664  template<unsigned int N, class T>
666  {
668 
669  /**
670  * Get function value.
671  *
672  * \return result
673  */
675  {
676  return JResultEvaluator<T>::get_value(value.y[0]);
677  }
678 
679  /**
680  * Get derivative value.
681  *
682  * \return result
683  */
685  {
686  return JResultEvaluator<T>::get_value(value.y[1]);
687  }
688 
689  /**
690  * Get partial integral value.
691  *
692  * \return result
693  */
695  {
696  return JMATH::zero;
697  }
698  };
699 
700 
701  /**
702  * Template specialisation of JResultEvaluator for JResultPolynome.
703  */
704  template<class T>
706  {
708 
709  /**
710  * Get function value.
711  *
712  * \return result
713  */
715  {
716  return JResultEvaluator<T>::get_value(value.y[0]);
717  }
718 
719  /**
720  * Get derivative value.
721  *
722  * \return result
723  */
725  {
726  return JMATH::zero;
727  }
728 
729  /**
730  * Get partial integral value.
731  *
732  * \return result
733  */
735  {
736  return JMATH::zero;
737  }
738  };
739 
740 
741  /**
742  * Helper method to recursively evaluate a to function value.
743  *
744  * \param value result
745  * \return function value
746  */
747  template<class JResult_t>
748  inline typename JResultEvaluator<JResult_t>::result_type get_value(const JResult_t& value)
749  {
751  }
752 
753 
754  /**
755  * Helper method to convert function value to derivative value.
756  *
757  * \param value result
758  * \return derivative value
759  */
760  template<class JResult_t>
761  inline typename JResultEvaluator<JResult_t>::result_type get_derivative(const JResult_t& value)
762  {
764  }
765 
766 
767  /**
768  * Helper method to convert function value to partial integral value.
769  *
770  * \param value result
771  * \return partial integral value
772  */
773  template<class JResult_t>
774  inline typename JResultEvaluator<JResult_t>::result_type get_integral(const JResult_t& value)
775  {
777  }
778 }
779 
780 #endif
JLANG::JClass< JResult_t >::argument_type argument_type
Definition: JResult.hh:44
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
JResultPolynome & add(const JResultPolynome &value)
Addition operator for function value of PDF.
Definition: JResult.hh:448
JLANG::JClass< JResult_t >::argument_type argument_type
Definition: JResult.hh:411
Data structure for result including value and N derivatives of function.
Definition: JResult.hh:407
static result_type get_derivative(const JResultPDF< T > &value)
Get derivative value.
Definition: JResult.hh:644
static result_type get_integral(typename JLANG::JClass< T >::argument_type value)
Get partial integral value.
Definition: JResult.hh:574
JResultHesse(argument_type __f, argument_type __fp)
Constructor.
Definition: JResult.hh:62
JResultPDF()
Default constructor.
Definition: JResult.hh:221
JResultPolynome & sub(const JResultPolynome &value)
Subtraction operator for function value of PDF.
Definition: JResult.hh:464
static result_type get_derivative(typename JLANG::JClass< T >::argument_type value)
Get derivative value.
Definition: JResult.hh:564
JResult_t f
function value
Definition: JResult.hh:191
double getChi2() const
Get chi2 of first hit.
Definition: JResult.hh:370
JResultPolynome()
Default constructor.
Definition: JResult.hh:419
Auxiliary class to recursively evaluate to a result.
Definition: JResult.hh:545
static result_type get_integral(const JResultHesse< T > &value)
Get partial integral value.
Definition: JResult.hh:614
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:94
static result_type get_value(typename JLANG::JClass< T >::argument_type value)
Get function value.
Definition: JResult.hh:554
JResultPDF & mul(const double value)
Multiplication operator for function value of PDF.
Definition: JResult.hh:323
Definition of zero value for any class.
double getChi2(const bool hit) const
Get chi2.
Definition: JResult.hh:167
JResult_t fp
first derivative
Definition: JResult.hh:388
JResult_t V
integral &lt;xmin,xmax&gt;
Definition: JResult.hh:390
static result_type get_derivative(const JResultPolynome< N, T > &value)
Get derivative value.
Definition: JResult.hh:684
static result_type get_integral(const JResultPolynome< N, T > &value)
Get partial integral value.
Definition: JResult.hh:694
JLANG::JClass< JResult_t >::argument_type argument_type
Definition: JResult.hh:215
JResultEvaluator< T >::result_type result_type
Definition: JResult.hh:667
JArgument< T >::argument_type argument_type
Definition: JClass.hh:64
JResultHesse & sub(const JResultHesse &value)
Subtraction operator for function value of PDF.
Definition: JResult.hh:104
JResultPDF & negate()
Prefix unary minus for function value of PDF.
Definition: JResult.hh:272
JResultHesse & mul(const double value)
Multiplication operator for function value of PDF.
Definition: JResult.hh:119
static const int NUMBER_OF_POINTS
Definition: JResult.hh:413
Data structure for result including value, first derivative and integrals of function.
Definition: JResult.hh:211
static result_type get_derivative(const JResultPolynome< 0, T > &value)
Get derivative value.
Definition: JResult.hh:724
JResultPDF & add(const JResultPDF &value)
Addition operator for function value of PDF.
Definition: JResult.hh:289
JResult_t fp
first derivative
Definition: JResult.hh:192
double getP() const
Get probability of first hit.
Definition: JResult.hh:358
JResult_t f
function value
Definition: JResult.hh:387
JResultEvaluator< T >::result_type result_type
Definition: JResult.hh:627
static result_type get_value(const JResultHesse< T > &value)
Get function value.
Definition: JResult.hh:594
JResultPolynome & negate()
Prefix unary minus for function value of PDF.
Definition: JResult.hh:432
JResult_t v
integral &lt;xmin,x]
Definition: JResult.hh:389
Range of values.
Definition: JRange.hh:33
JResultPDF(argument_type __f, argument_type __fp, argument_type __v, argument_type __V)
Constructor.
Definition: JResult.hh:237
JResultEvaluator< T >::result_type result_type
Definition: JResult.hh:707
double getP(const bool hit) const
Get probability.
Definition: JResult.hh:151
double getValue(const double x) const
Function value.
Definition: JResult.hh:512
static result_type get_derivative(const JResultHesse< T > &value)
Get derivative value.
Definition: JResult.hh:604
JResultPDF & sub(const JResultPDF &value)
Subtraction operator for function value of PDF.
Definition: JResult.hh:306
JResult_t y[NUMBER_OF_POINTS]
values
Definition: JResult.hh:537
static result_type get_integral(const JResultPDF< T > &value)
Get partial integral value.
Definition: JResult.hh:654
JResultPolynome & mul(const double value)
Multiplication operator for function value of PDF.
Definition: JResult.hh:480
JResultPolynome & div(const double value)
Division operator for function value of PDF.
Definition: JResult.hh:496
static result_type get_integral(const JResultPolynome< 0, T > &value)
Get partial integral value.
Definition: JResult.hh:734
Auxiliary class to define a range between two values.
JResultEvaluator< JResult_t >::result_type get_derivative(const JResult_t &value)
Helper method to convert function value to derivative value.
Definition: JResult.hh:761
JResultHesse & div(const double value)
Division operator for function value of PDF.
Definition: JResult.hh:134
JResultEvaluator< T >::result_type result_type
Definition: JResult.hh:587
double getDerivativeOfChi2(const bool hit) const
Get derivative of chi2.
Definition: JResult.hh:182
double getDerivativeOfChi2() const
Get derivative of chi2 of first hit.
Definition: JResult.hh:381
JResultHesse & negate()
Prefix unary minus for function value of PDF.
Definition: JResult.hh:74
Base class for data structures with artithmetic capabilities.
JResultPDF(argument_type R, argument_type x, const JRange< JResult_t > &X)
Constructor.
Definition: JResult.hh:257
JResultHesse()
Default constructor.
Definition: JResult.hh:50
static result_type get_value(const JResultPDF< T > &value)
Get function value.
Definition: JResult.hh:634
JResultEvaluator< JResult_t >::result_type get_integral(const JResult_t &value)
Helper method to convert function value to partial integral value.
Definition: JResult.hh:774
Data structure for result including value and first derivative of function.
Definition: JResult.hh:40
JResultPDF & div(const double value)
Division operator for function value of PDF.
Definition: JResult.hh:340
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition: JResult.hh:748
double operator()(const double x) const
Function value.
Definition: JResult.hh:531
static result_type get_value(const JResultPolynome< N, T > &value)
Get function value.
Definition: JResult.hh:674
JResultHesse & add(const JResultHesse &value)
Addition operator for function value of PDF.
Definition: JResult.hh:89
static result_type get_value(const JResultPolynome< 0, T > &value)
Get function value.
Definition: JResult.hh:714