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
26 const ROOT::Math::IGenFunction *cdf, bool isLogPdf, bool copyFunc)
27 : fPdf(pdf), fDPdf(dpdf), fCdf(cdf), fXmin(1.), fXmax(-1.), fMode(0), fArea(0), fIsLogPdf(isLogPdf), fHasDomain(0),
28 fHasMode(0), fHasArea(0), fOwnFunc(copyFunc)
29{
30 // Constructor from generic function interfaces
31 // manage the functions and clone them if flag copyFunc is true
32 if (fOwnFunc) {
33 if (fPdf)
34 fPdf = fPdf->Clone();
35 if (fDPdf)
36 fDPdf = fDPdf->Clone();
37 if (fCdf)
38 fCdf = fCdf->Clone();
39 }
40}
41
42TUnuranContDist::TUnuranContDist (const ROOT::Math::IGenFunction & pdf, const ROOT::Math::IGenFunction * deriv, bool isLogPdf, bool copyFunc ) :
43 TUnuranContDist(&pdf,deriv, nullptr, isLogPdf, copyFunc)
44{}
45
46TUnuranContDist::TUnuranContDist (TF1 * pdf, TF1 * deriv, TF1 * cdf, bool isLogPdf ) :
47 fPdf( (pdf) ? new ROOT::Math::WrappedTF1 ( *pdf) : nullptr ),
48 fDPdf( (deriv) ? new ROOT::Math::WrappedTF1 ( *deriv) : nullptr ),
49 fCdf( (cdf) ? new ROOT::Math::WrappedTF1 ( *cdf) : nullptr),
50 fXmin(1.),
51 fXmax(-1.),
52 fMode(0),
53 fArea(0),
54 fIsLogPdf(isLogPdf),
55 fHasDomain(0),
56 fHasMode(0),
57 fHasArea(0),
58 fOwnFunc(true)
59{
60 // Constructor from a TF1 objects
61 // function pointers are managed by class
62}
63
64TUnuranContDist::TUnuranContDist (TF1 * pdf, TF1 * deriv, bool isLogPdf ) :
65 TUnuranContDist(pdf,deriv, nullptr, isLogPdf)
66 {}
67
70 fPdf(nullptr),
71 fDPdf(nullptr),
72 fCdf(nullptr)
73{
74 // Implementation of copy constructor
75 operator=(rhs);
76}
77
79{
80 // Implementation of assignment operator.
81 if (this == &rhs) return *this; // time saving self-test
82 fXmin = rhs.fXmin;
83 fXmax = rhs.fXmax;
84 fMode = rhs.fMode;
85 fArea = rhs.fArea;
86 fIsLogPdf = rhs.fIsLogPdf;
88 fHasMode = rhs.fHasMode;
89 fHasArea = rhs.fHasArea;
90 fOwnFunc = rhs.fOwnFunc;
91 if (!fOwnFunc) {
92 fPdf = rhs.fPdf;
93 fDPdf = rhs.fDPdf;
94 fCdf = rhs.fCdf;
95 }
96 else {
97 if (fPdf) delete fPdf;
98 if (fDPdf) delete fDPdf;
99 if (fCdf) delete fCdf;
100 fPdf = (rhs.fPdf) ? rhs.fPdf->Clone() : nullptr;
101 fDPdf = (rhs.fDPdf) ? rhs.fDPdf->Clone() : nullptr;
102 fCdf = (rhs.fCdf) ? rhs.fCdf->Clone() : nullptr;
103 }
104
105 return *this;
106}
107
109 // destructor implementation
110 if (fOwnFunc) {
111 if (fPdf) delete fPdf;
112 if (fDPdf) delete fDPdf;
113 if (fCdf) delete fCdf;
114 }
115}
116
118 // set cdf distribution using a generic function interface
119 fCdf = (fOwnFunc) ? cdf.Clone() : &cdf;
120}
121
122
124 // set cumulative distribution function from a TF1
125 if (!fOwnFunc) {
126 // need to manage all functions now
127 if (fPdf) fPdf = fPdf->Clone();
128 if (fDPdf) fDPdf->Clone();
129 }
130 else
131 if (fCdf) delete fCdf;
132
133 fCdf = (cdf) ? new ROOT::Math::WrappedTF1 ( *cdf) : nullptr;
134 fOwnFunc = true;
135}
136
137double TUnuranContDist::Pdf ( double x) const {
138 // evaluate the pdf of the distribution. Return NaN if pdf is not available
139 return (fPdf) ? (*fPdf)(x) : TMath::QuietNaN();
140}
141
142double TUnuranContDist::DPdf( double x) const {
143 // evaluate the derivative of the pdf
144 // if derivative function is not given is evaluated numerically
145 // in case a pdf is available, otherwise a NaN is returned
146 if (fDPdf) {
147 return (*fDPdf)(x);
148 }
149 if (!fPdf) return TMath::QuietNaN();
150 // do numerical derivation using numerical derivation
152 static double gEps = 0.001;
153 double h = ( std::abs(x) > 0 ) ? gEps * std::abs(x) : gEps;
154 assert (fPdf != 0);
155 return rd.Derivative1( *fPdf, x, h);
156}
157
158double TUnuranContDist::Cdf(double x) const {
159 // evaluate the integral (cdf) on the domain
160 if (fCdf) {
161 return (*fCdf)(x);
162 }
163 // do numerical integration
164 if (!fPdf) return TMath::QuietNaN();
166 if (fXmin > fXmax) return ig.Integral( *fPdf );
167 else
168 return ig.Integral( *fPdf, fXmin, fXmax );
169
170}
171
#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:851