ROOT  6.06/09
Reference Guide
TUnuranDiscrDist.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 TUnuranDiscrDist
12 
13 #include "TUnuranDiscrDist.h"
14 
15 #include "Math/IFunction.h"
16 #include "TF1.h"
17 #include "Math/WrappedTF1.h"
18 
19 
20 #include <cassert>
21 
22 
24  fPmf(&func),
25  fCdf(0),
26  fXmin(1),
27  fXmax(-1),
28  fMode(0),
29  fSum(0),
30  fHasDomain(0),
31  fHasMode(0),
32  fHasSum(0),
33  fOwnFunc(copyFunc)
34 {
35  //Constructor from a generic function object
36  if (fOwnFunc) {
37  fPmf = fPmf->Clone();
38  //if (fCdf) fCdf->Clone();
39  }
40 }
41 
42 
44  fPmf( (func) ? new ROOT::Math::WrappedTF1 ( *func) : 0 ),
45  fCdf(0),
46  fXmin(1),
47  fXmax(-1),
48  fMode(0),
49  fSum(0),
50  fHasDomain(0),
51  fHasMode(0),
52  fHasSum(0),
53  fOwnFunc(true)
54 {
55  //Constructor from a TF1 objects
56 }
57 
58 
61  fPmf(0),
62  fCdf(0)
63 {
64  // Implementation of copy ctor using aassignment operator
65  operator=(rhs);
66 }
67 
69 {
70  // Implementation of assignment operator (copy only the function pointer not the function itself)
71  if (this == &rhs) return *this; // time saving self-test
72  fPVec = rhs.fPVec;
73  fPVecSum = rhs.fPVecSum;
74  fXmin = rhs.fXmin;
75  fXmax = rhs.fXmax;
76  fMode = rhs.fMode;
77  fSum = rhs.fSum;
78  fHasDomain = rhs.fHasDomain;
79  fHasMode = rhs.fHasMode;
80  fHasSum = rhs.fHasSum;
81  fOwnFunc = rhs.fOwnFunc;
82  if (!fOwnFunc) {
83  fPmf = rhs.fPmf;
84  fCdf = rhs.fCdf;
85  }
86  else {
87  if (fPmf) delete fPmf;
88  if (fCdf) delete fCdf;
89  fPmf = (rhs.fPmf) ? rhs.fPmf->Clone() : 0;
90  fCdf = (rhs.fCdf) ? rhs.fCdf->Clone() : 0;
91  }
92 
93  return *this;
94 }
95 
97  // destructor implementation
98  if (fOwnFunc) {
99  if (fPmf) delete fPmf;
100  if (fCdf) delete fCdf;
101  }
102 }
103 
105  // set cdf distribution using a generic function interface
106  fCdf = (fOwnFunc) ? cdf.Clone() : &cdf;
107 }
108 
110  // set cumulative distribution function from a TF1
111  if (!fOwnFunc && fPmf) {
112  // need to manage also the pmf
113  fPmf = fPmf->Clone();
114  }
115  else
116  if (fCdf) delete fCdf;
117 
118  fCdf = (cdf) ? new ROOT::Math::WrappedTF1 ( *cdf) : 0;
119  fOwnFunc = true;
120 }
121 
122 double TUnuranDiscrDist::Pmf ( int x) const {
123  // evaluate the distribution
124  if (!fPmf) {
125  if (x < static_cast<int>(fPVec.size()) || x >= static_cast<int>(fPVec.size()) ) return 0;
126  return fPVec[x];
127  }
128  return (*fPmf)(double(x));
129 }
130 
131 double TUnuranDiscrDist::Cdf ( int x) const {
132  // evaluate the cumulative distribution
133  // otherwise evaluate from the sum of the probabilities
134  if (fHasDomain && x < fXmin) return 0;
135 
136  if (fCdf) {
137  return (*fCdf)(double(x));
138  }
139 
140  //estimation from sum of probability
141  int vsize = fPVecSum.size();
142  if ( x < vsize )
143  return fPVecSum[x];
144 
145  // calculate the sum
146  int x0 = ( fHasDomain) ? fXmin : 0;
147  int i0 = vsize; // starting index
148  int iN = x - x0 + 1; // maximum index
149  fPVecSum.resize(iN);
150  double sum = ( i0 > 0 ) ? fPVecSum.back() : 0;
151  for (int i = i0; i < iN; ++i) {
152  sum += Pmf(i + x0);
153  fPVecSum[i] = sum;
154  }
155 
156  return fPVecSum.back();
157 
158 }
159 
160 
161 
162 
163 
TUnuranBaseDist, base class for Unuran distribution classees such as TUnuranContDist (for one-dimensi...
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
const ROOT::Math::IGenFunction * fCdf
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
const ROOT::Math::IGenFunction * fPmf
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface of one dimensions to be ...
Definition: WrappedTF1.h:41
double Pmf(int x) const
evaluate the distribution (probability mesh function) at the integer value x.
Double_t x[n]
Definition: legend1.C:17
double cdf(double *x, double *p)
Definition: unuranDistr.cxx:44
TUnuranDiscrDist(const ROOT::Math::IGenFunction &func, bool copyFunc=false)
Constructor from a generic function object specifying the pdf.
TUnuranDiscrDist class for one dimensional discrete distribution.
void SetCdf(const ROOT::Math::IGenFunction &cdf)
set cdf distribution from a generic function interface.
TUnuranDiscrDist & operator=(const TUnuranDiscrDist &rhs)
Assignment operator.
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Namespace for new Math classes and functions.
1-Dim function class
Definition: TF1.h:149
double Cdf(int x) const
evaluate the integral (cdf) on the given domain
virtual IBaseFunctionOneDim * Clone() const =0
Clone a function.
virtual ~TUnuranDiscrDist()
Destructor.
std::vector< double > fPVecSum
std::vector< double > fPVec