ROOT logo
// @(#)root/unuran:$Id: TUnuranContDist.cxx 35517 2010-09-21 09:43:15Z moneta $
// Authors: L. Moneta, J. Leydold Wed Feb 28 2007

/**********************************************************************
 *                                                                    *
 * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
 *                                                                    *
 *                                                                    *
 **********************************************************************/

// Implementation file for class TUnuranContDist

#include "TUnuranContDist.h"
#include "Math/RichardsonDerivator.h"
#include "Math/WrappedTF1.h"

#include "Math/Integrator.h"

#include "TF1.h"
#include <cassert>
#include <cmath>

ClassImp(TUnuranContDist)

TUnuranContDist::TUnuranContDist (const ROOT::Math::IGenFunction & pdf, const ROOT::Math::IGenFunction * deriv, bool isLogPdf, bool copyFunc  ) : 
   fPdf(&pdf),
   fDPdf(deriv),
   fCdf(0), 
   fXmin(1.), 
   fXmax(-1.), 
   fMode(0), 
   fArea(0),
   fIsLogPdf(isLogPdf),
   fHasDomain(0),
   fHasMode(0),
   fHasArea(0), 
   fOwnFunc(copyFunc)
{
   // Constructor from generic function interfaces
   // manage the functions and clone them if flag copyFunc is true
   if (fOwnFunc) { 
      fPdf = fPdf->Clone(); 
      if (fDPdf) fDPdf->Clone(); 
   }
} 


TUnuranContDist::TUnuranContDist (TF1 * pdf, TF1 * deriv, bool isLogPdf  ) : 
   fPdf(  (pdf) ? new ROOT::Math::WrappedTF1 ( *pdf) : 0 ), 
   fDPdf( (deriv) ?  new ROOT::Math::WrappedTF1 ( *deriv) : 0 ),
   fCdf(0), 
   fXmin(1.), 
   fXmax(-1.), 
   fMode(0), 
   fArea(0),
   fIsLogPdf(isLogPdf),
   fHasDomain(0),
   fHasMode(0),
   fHasArea(0), 
   fOwnFunc(true)
{
   // Constructor from a TF1 objects
   // function pointers are managed by class 
} 


TUnuranContDist::TUnuranContDist(const TUnuranContDist & rhs) : 
   TUnuranBaseDist(), 
   fPdf(0), 
   fDPdf(0),
   fCdf(0)
{
   // Implementation of copy constructor 
   operator=(rhs);
}

TUnuranContDist & TUnuranContDist::operator = (const TUnuranContDist &rhs) 
{
   // Implementation of assignment operator.
   if (this == &rhs) return *this;  // time saving self-test
   fXmin  = rhs.fXmin;  
   fXmax  = rhs.fXmax;  
   fMode  = rhs.fMode; 
   fArea  = rhs.fArea;
   fIsLogPdf  = rhs.fIsLogPdf;
   fHasDomain = rhs.fHasDomain;
   fHasMode   = rhs.fHasMode;
   fHasArea   = rhs.fHasArea;
   fOwnFunc   = rhs.fOwnFunc;
   if (!fOwnFunc) { 
      fPdf   = rhs.fPdf;
      fDPdf  = rhs.fDPdf;
      fCdf   = rhs.fCdf; 
   }
   else {
      if (fPdf) delete fPdf;
      if (fDPdf) delete fDPdf; 
      if (fCdf) delete fCdf; 
      fPdf  = (rhs.fPdf)  ? rhs.fPdf->Clone()  : 0;  
      fDPdf = (rhs.fDPdf) ? rhs.fDPdf->Clone() : 0;  
      fCdf  = (rhs.fCdf)  ? rhs.fCdf->Clone()  : 0;  
   }

   return *this;
}

TUnuranContDist::~TUnuranContDist() { 
   // destructor implementation
   if (fOwnFunc) { 
      if (fPdf) delete fPdf; 
      if (fDPdf) delete fDPdf; 
      if (fCdf) delete fCdf; 
   }
}

void TUnuranContDist::SetCdf(const ROOT::Math::IGenFunction & cdf) {  
   //  set cdf distribution using a generic function interface
   fCdf = (fOwnFunc) ? cdf.Clone() : &cdf; 
}


void TUnuranContDist::SetCdf(TF1 *  cdf) { 
   // set cumulative distribution function from a TF1
   if (!fOwnFunc) { 
      // need to manage all functions now
      assert (fPdf != 0); 
      fPdf = fPdf->Clone(); 
      if (fDPdf) fDPdf->Clone(); 
   }
   else 
      if (fOwnFunc && fCdf) delete fCdf;

   fCdf = (cdf) ? new ROOT::Math::WrappedTF1 ( *cdf) : 0;    
   fOwnFunc = true; 
}

double TUnuranContDist::Pdf ( double x) const { 
   // evaluate the pdf of the distribution    
   assert(fPdf != 0);
   //fX[0] = x; 
   return (*fPdf)(x); 
}

double TUnuranContDist::DPdf( double x) const { 
   // evaluate the derivative of the pdf
   // if derivative function is not given is evaluated numerically
   if (fDPdf != 0) { 
      //fX[0] = x; 
      return (*fDPdf)(x); 
   }
   // do numerical derivation using numerical derivation
   ROOT::Math::RichardsonDerivator rd;
   static double gEps = 0.001;
   double h = ( std::abs(x) > 0 ) ?  gEps * std::abs(x) : gEps; 
   assert (fPdf != 0);
   return rd.Derivative1( *fPdf, x, h);
}

double TUnuranContDist::Cdf(double x) const {   
   // evaluate the integral (cdf)  on the domain
   if (fCdf != 0) {  
     // fX[0] = x; 
      return (*fCdf)(x);
   }
   // do numerical integration
   ROOT::Math::Integrator ig; 
   if (fXmin > fXmax) return ig.Integral( *fPdf ); 
   else 
      return ig.Integral( *fPdf, fXmin, fXmax );
   
}

 TUnuranContDist.cxx:1
 TUnuranContDist.cxx:2
 TUnuranContDist.cxx:3
 TUnuranContDist.cxx:4
 TUnuranContDist.cxx:5
 TUnuranContDist.cxx:6
 TUnuranContDist.cxx:7
 TUnuranContDist.cxx:8
 TUnuranContDist.cxx:9
 TUnuranContDist.cxx:10
 TUnuranContDist.cxx:11
 TUnuranContDist.cxx:12
 TUnuranContDist.cxx:13
 TUnuranContDist.cxx:14
 TUnuranContDist.cxx:15
 TUnuranContDist.cxx:16
 TUnuranContDist.cxx:17
 TUnuranContDist.cxx:18
 TUnuranContDist.cxx:19
 TUnuranContDist.cxx:20
 TUnuranContDist.cxx:21
 TUnuranContDist.cxx:22
 TUnuranContDist.cxx:23
 TUnuranContDist.cxx:24
 TUnuranContDist.cxx:25
 TUnuranContDist.cxx:26
 TUnuranContDist.cxx:27
 TUnuranContDist.cxx:28
 TUnuranContDist.cxx:29
 TUnuranContDist.cxx:30
 TUnuranContDist.cxx:31
 TUnuranContDist.cxx:32
 TUnuranContDist.cxx:33
 TUnuranContDist.cxx:34
 TUnuranContDist.cxx:35
 TUnuranContDist.cxx:36
 TUnuranContDist.cxx:37
 TUnuranContDist.cxx:38
 TUnuranContDist.cxx:39
 TUnuranContDist.cxx:40
 TUnuranContDist.cxx:41
 TUnuranContDist.cxx:42
 TUnuranContDist.cxx:43
 TUnuranContDist.cxx:44
 TUnuranContDist.cxx:45
 TUnuranContDist.cxx:46
 TUnuranContDist.cxx:47
 TUnuranContDist.cxx:48
 TUnuranContDist.cxx:49
 TUnuranContDist.cxx:50
 TUnuranContDist.cxx:51
 TUnuranContDist.cxx:52
 TUnuranContDist.cxx:53
 TUnuranContDist.cxx:54
 TUnuranContDist.cxx:55
 TUnuranContDist.cxx:56
 TUnuranContDist.cxx:57
 TUnuranContDist.cxx:58
 TUnuranContDist.cxx:59
 TUnuranContDist.cxx:60
 TUnuranContDist.cxx:61
 TUnuranContDist.cxx:62
 TUnuranContDist.cxx:63
 TUnuranContDist.cxx:64
 TUnuranContDist.cxx:65
 TUnuranContDist.cxx:66
 TUnuranContDist.cxx:67
 TUnuranContDist.cxx:68
 TUnuranContDist.cxx:69
 TUnuranContDist.cxx:70
 TUnuranContDist.cxx:71
 TUnuranContDist.cxx:72
 TUnuranContDist.cxx:73
 TUnuranContDist.cxx:74
 TUnuranContDist.cxx:75
 TUnuranContDist.cxx:76
 TUnuranContDist.cxx:77
 TUnuranContDist.cxx:78
 TUnuranContDist.cxx:79
 TUnuranContDist.cxx:80
 TUnuranContDist.cxx:81
 TUnuranContDist.cxx:82
 TUnuranContDist.cxx:83
 TUnuranContDist.cxx:84
 TUnuranContDist.cxx:85
 TUnuranContDist.cxx:86
 TUnuranContDist.cxx:87
 TUnuranContDist.cxx:88
 TUnuranContDist.cxx:89
 TUnuranContDist.cxx:90
 TUnuranContDist.cxx:91
 TUnuranContDist.cxx:92
 TUnuranContDist.cxx:93
 TUnuranContDist.cxx:94
 TUnuranContDist.cxx:95
 TUnuranContDist.cxx:96
 TUnuranContDist.cxx:97
 TUnuranContDist.cxx:98
 TUnuranContDist.cxx:99
 TUnuranContDist.cxx:100
 TUnuranContDist.cxx:101
 TUnuranContDist.cxx:102
 TUnuranContDist.cxx:103
 TUnuranContDist.cxx:104
 TUnuranContDist.cxx:105
 TUnuranContDist.cxx:106
 TUnuranContDist.cxx:107
 TUnuranContDist.cxx:108
 TUnuranContDist.cxx:109
 TUnuranContDist.cxx:110
 TUnuranContDist.cxx:111
 TUnuranContDist.cxx:112
 TUnuranContDist.cxx:113
 TUnuranContDist.cxx:114
 TUnuranContDist.cxx:115
 TUnuranContDist.cxx:116
 TUnuranContDist.cxx:117
 TUnuranContDist.cxx:118
 TUnuranContDist.cxx:119
 TUnuranContDist.cxx:120
 TUnuranContDist.cxx:121
 TUnuranContDist.cxx:122
 TUnuranContDist.cxx:123
 TUnuranContDist.cxx:124
 TUnuranContDist.cxx:125
 TUnuranContDist.cxx:126
 TUnuranContDist.cxx:127
 TUnuranContDist.cxx:128
 TUnuranContDist.cxx:129
 TUnuranContDist.cxx:130
 TUnuranContDist.cxx:131
 TUnuranContDist.cxx:132
 TUnuranContDist.cxx:133
 TUnuranContDist.cxx:134
 TUnuranContDist.cxx:135
 TUnuranContDist.cxx:136
 TUnuranContDist.cxx:137
 TUnuranContDist.cxx:138
 TUnuranContDist.cxx:139
 TUnuranContDist.cxx:140
 TUnuranContDist.cxx:141
 TUnuranContDist.cxx:142
 TUnuranContDist.cxx:143
 TUnuranContDist.cxx:144
 TUnuranContDist.cxx:145
 TUnuranContDist.cxx:146
 TUnuranContDist.cxx:147
 TUnuranContDist.cxx:148
 TUnuranContDist.cxx:149
 TUnuranContDist.cxx:150
 TUnuranContDist.cxx:151
 TUnuranContDist.cxx:152
 TUnuranContDist.cxx:153
 TUnuranContDist.cxx:154
 TUnuranContDist.cxx:155
 TUnuranContDist.cxx:156
 TUnuranContDist.cxx:157
 TUnuranContDist.cxx:158
 TUnuranContDist.cxx:159
 TUnuranContDist.cxx:160
 TUnuranContDist.cxx:161
 TUnuranContDist.cxx:162
 TUnuranContDist.cxx:163
 TUnuranContDist.cxx:164
 TUnuranContDist.cxx:165
 TUnuranContDist.cxx:166
 TUnuranContDist.cxx:167
 TUnuranContDist.cxx:168
 TUnuranContDist.cxx:169
 TUnuranContDist.cxx:170
 TUnuranContDist.cxx:171
 TUnuranContDist.cxx:172