Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TUnuranContDist.cxx
Go to the documentation of this file.
1// @(#)root/unuran:$Id$
2// Authors: L. Moneta, J. Leydold Wed Feb 28 2007
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Implementation file for class TUnuranContDist
12
13#include "TUnuranContDist.h"
15#include "Math/WrappedTF1.h"
16
17#include "Math/Integrator.h"
18
19#include "TF1.h"
20#include <cassert>
21#include <cmath>
22
24
25TUnuranContDist::TUnuranContDist (const ROOT::Math::IGenFunction & pdf, const ROOT::Math::IGenFunction * deriv, bool isLogPdf, bool copyFunc ) :
26 fPdf(&pdf),
27 fDPdf(deriv),
28 fCdf(nullptr),
29 fXmin(1.),
30 fXmax(-1.),
31 fMode(0),
32 fArea(0),
33 fIsLogPdf(isLogPdf),
34 fHasDomain(0),
35 fHasMode(0),
36 fHasArea(0),
37 fOwnFunc(copyFunc)
38{
39 // Constructor from generic function interfaces
40 // manage the functions and clone them if flag copyFunc is true
41 if (fOwnFunc) {
42 fPdf = fPdf->Clone();
43 if (fDPdf) fDPdf = fDPdf->Clone();
44 }
45}
46
47
48TUnuranContDist::TUnuranContDist (TF1 * pdf, TF1 * deriv, bool isLogPdf ) :
49 fPdf( (pdf) ? new ROOT::Math::WrappedTF1 ( *pdf) : nullptr ),
50 fDPdf( (deriv) ? new ROOT::Math::WrappedTF1 ( *deriv) : nullptr ),
51 fCdf(nullptr),
52 fXmin(1.),
53 fXmax(-1.),
54 fMode(0),
55 fArea(0),
56 fIsLogPdf(isLogPdf),
57 fHasDomain(0),
58 fHasMode(0),
59 fHasArea(0),
60 fOwnFunc(true)
61{
62 // Constructor from a TF1 objects
63 // function pointers are managed by class
64}
65
66
69 fPdf(nullptr),
70 fDPdf(nullptr),
71 fCdf(nullptr)
72{
73 // Implementation of copy constructor
74 operator=(rhs);
75}
76
78{
79 // Implementation of assignment operator.
80 if (this == &rhs) return *this; // time saving self-test
81 fXmin = rhs.fXmin;
82 fXmax = rhs.fXmax;
83 fMode = rhs.fMode;
84 fArea = rhs.fArea;
85 fIsLogPdf = rhs.fIsLogPdf;
87 fHasMode = rhs.fHasMode;
88 fHasArea = rhs.fHasArea;
89 fOwnFunc = rhs.fOwnFunc;
90 if (!fOwnFunc) {
91 fPdf = rhs.fPdf;
92 fDPdf = rhs.fDPdf;
93 fCdf = rhs.fCdf;
94 }
95 else {
96 if (fPdf) delete fPdf;
97 if (fDPdf) delete fDPdf;
98 if (fCdf) delete fCdf;
99 fPdf = (rhs.fPdf) ? rhs.fPdf->Clone() : nullptr;
100 fDPdf = (rhs.fDPdf) ? rhs.fDPdf->Clone() : nullptr;
101 fCdf = (rhs.fCdf) ? rhs.fCdf->Clone() : nullptr;
102 }
103
104 return *this;
105}
106
108 // destructor implementation
109 if (fOwnFunc) {
110 if (fPdf) delete fPdf;
111 if (fDPdf) delete fDPdf;
112 if (fCdf) delete fCdf;
113 }
114}
115
117 // set cdf distribution using a generic function interface
118 fCdf = (fOwnFunc) ? cdf.Clone() : &cdf;
119}
120
121
123 // set cumulative distribution function from a TF1
124 if (!fOwnFunc) {
125 // need to manage all functions now
126 if (fPdf) fPdf = fPdf->Clone();
127 if (fDPdf) fDPdf->Clone();
128 }
129 else
130 if (fCdf) delete fCdf;
131
132 fCdf = (cdf) ? new ROOT::Math::WrappedTF1 ( *cdf) : nullptr;
133 fOwnFunc = true;
134}
135
136double TUnuranContDist::Pdf ( double x) const {
137 // evaluate the pdf of the distribution. Return NaN if pdf is not available
138 return (fPdf) ? (*fPdf)(x) : TMath::QuietNaN();
139}
140
141double TUnuranContDist::DPdf( double x) const {
142 // evaluate the derivative of the pdf
143 // if derivative function is not given is evaluated numerically
144 if (fDPdf) {
145 return (*fDPdf)(x);
146 }
147 // do numerical derivation using numerical derivation
149 static double gEps = 0.001;
150 double h = ( std::abs(x) > 0 ) ? gEps * std::abs(x) : gEps;
151 assert (fPdf != 0);
152 return rd.Derivative1( *fPdf, x, h);
153}
154
155double TUnuranContDist::Cdf(double x) const {
156 // evaluate the integral (cdf) on the domain
157 if (fCdf) {
158 return (*fCdf)(x);
159 }
160 // do numerical integration
162 if (fXmin > fXmax) return ig.Integral( *fPdf );
163 else
164 return ig.Integral( *fPdf, fXmin, fXmax );
165
166}
167
#define h(i)
Definition RSha256.hxx:106
#define ClassImp(name)
Definition Rtypes.h:364
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:135
virtual IBaseFunctionOneDim * Clone() const =0
Clone a function.
User Class for performing numerical integration of a function in one dimension.
Definition Integrator.h:98
double Integral(Function &f, double a, double b)
evaluate the Integral of a function f over the defined interval (a,b)
Definition Integrator.h:500
User class for calculating the derivatives of a function.
double Derivative1(double x)
Returns the first derivative of the function at point x, computed by Richardson's extrapolation metho...
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface of one dimensions to be ...
Definition WrappedTF1.h:39
1-Dim function class
Definition TF1.h:213
TUnuranBaseDist, base class for Unuran distribution classees such as TUnuranContDist (for one-dimensi...
TUnuranContDist class describing one dimensional continuous distribution.
const ROOT::Math::IGenFunction * fDPdf
double DPdf(double x) const
evaluate the derivative of the pdf.
const ROOT::Math::IGenFunction * fPdf
TUnuranContDist(TF1 *pdf=0, TF1 *deriv=0, bool isLogPdf=false)
Constructor from a TF1 objects specifying the pdf and optionally from another function representing t...
double Pdf(double x) const
evaluate the Probability Density function.
const ROOT::Math::IGenFunction * fCdf
double Cdf(double x) const
evaluate the integral (cdf) on the domain.
void SetCdf(TF1 *cdf)
set cdf distribution.
virtual ~TUnuranContDist()
Destructor.
TUnuranContDist & operator=(const TUnuranContDist &rhs)
Assignment operator.
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Double_t QuietNaN()
Returns a quiet NaN as defined by IEEE 754
Definition TMath.h:901