ROOT logo
// @(#)root/unuran:$Id: TUnuranContDist.h 33181 2010-04-25 10:15:08Z moneta $
// Authors: L. Moneta, J. Leydold Wed Feb 28 2007

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

// Header file for class TUnuranContDist


#ifndef ROOT_Math_TUnuranContDist
#define ROOT_Math_TUnuranContDist

#ifndef ROOT_Math_TUnuranBaseDist
#include "TUnuranBaseDist.h"
#endif

#ifndef ROOT_Math_IFunctionfwd
#include "Math/IFunctionfwd.h"
#endif

class TF1;



//______________________________________________________________
/** 
   TUnuranContDist class describing one dimensional continous distribution. 
   It is used by TUnuran to generate random numbers according to this distribution via 
   TUnuran::Sample()
   
   The class can be constructed from a function (TF1) representing the probability density 
   function of the distribution. Optionally the derivative of the pdf can also be passed. 

   It provides a method to set the domain of the distribution ( SetDomain ) which will correspond to the range 
   of the generated random numbers. By default the domain is (-inf, + inf), indipendently of the 
   range set in the TF1 class used to construct the distribution. 

   In addition, some UNURAN methods requires extra information (cdf function, distribution mode, 
   area of pdf, etc...). This information can as well be set. 
   Some methods require instead of the pdf the log of the pdf. 
   This can also be controlled by setting a flag when constructing this class. 
*/ 
///////////////////////////////////////////////////////////////////////
class TUnuranContDist : public TUnuranBaseDist {

public: 


   /** 
      Constructor from a TF1 objects specifying the pdf and optionally from another function 
      representing the derivative of the pdf. The flag isLogPdf can be used to pass instead of the pdf 
      (and its derivative) the log (and the derivative of the log) of the pdf. 
      By default the distribution has not domain set (it is defined between [-inf,+inf], no mode, no pdf area and no 
      cdf explicity defined. UnuRan, if needed, can compute some of this quantities, but the user if he knows them can 
      set them in order to speed up the algorithm. For example in case of the Cdf, if the user has not set it, a numerical 
      integration algorithm is used to estimate the Cdf from the Pdf. 
      In case an algorithm requires only the Cdf (no Pdf), an empty distribution can be constructed and then the user must 
      set afterwards the Cdf. 
   */ 
   explicit TUnuranContDist (TF1 * pdf = 0, TF1 * deriv = 0, bool isLogPdf = false );

   /** 
      Constructor as before but from a generic function object interface for one-dim functions
   */ 
   explicit TUnuranContDist (const ROOT::Math::IGenFunction & pdf, const ROOT::Math::IGenFunction * dpdf = 0, bool isLogPdf = false, bool copyFunc = false); 


   /** 
      Destructor 
   */ 
   virtual ~TUnuranContDist ();


   /** 
      Copy constructor
   */ 
   TUnuranContDist(const TUnuranContDist &); 

   /** 
      Assignment operator
   */ 
   TUnuranContDist & operator = (const TUnuranContDist & rhs); 

   /**
      Clone (required by base class)
    */
   virtual TUnuranContDist * Clone() const { return new TUnuranContDist(*this); } 


   /**
      set cdf distribution. If a method requires it 
      and is not set it is then estimated using numerical 
      integration from the pdf
   */
   void SetCdf(TF1 *  cdf); 

   /**
      set cdf distribution using a generic function interface
   */
   void SetCdf(const ROOT::Math::IGenFunction & cdf);

   /**
      Set the distribution domain. If min < max a domain is defined otherwise is undefined
    */
   void SetDomain(double xmin, double xmax)  { 
      fXmin = xmin; 
      fXmax = xmax; 
      if (fXmin < fXmax) 
         fHasDomain = true;
      else 
         fHasDomain = false;
   }

   /**
      set the distribution mode (x position of its maximum)
   */
   void SetMode(double mode) { fMode = mode; fHasMode=true;}

   /**
      set the area below the pdf
    */
   void SetPdfArea(double area) { fArea = area; fHasArea=true;}

   /**
      check if distribution has a defined domain and return in case its domain
   */
   bool GetDomain(double & xmin, double & xmax) const { 
      xmin = fXmin; 
      xmax = fXmax; 
      return fHasDomain; 
   }

   /**
      check if a cdf function is provided for the distribution 
    */
   bool HasCdf() const { return fCdf != 0; } 

   /**
      check if distribution has a pre-computed mode
    */
   bool HasMode() const { return fHasMode; } 

   
   /**
      check if distribution has a pre-computed area below the Pdf
    */
   bool HasPdfArea() const { return fHasArea; }   

   /**
      return the mode   (x location of  maximum of the pdf)  
   */
   double Mode() const { return fMode; }

   /**
      return area below the pdf
   */
   double PdfArea() const { return fArea; }


   /**
      flag to control if given function represent the log of a pdf
   */
   bool IsLogPdf() const {  return fIsLogPdf; }

   /**
      evaluate the Probability Density function. Used by the UnuRan algorithms 
   */
   double Pdf ( double x) const; 

   /**
      evaluate the derivative of the pdf. Used by  UnuRan 
   */
   double DPdf( double x) const; 

   /**
      evaluate the integral (cdf)  on the domain. Used by Unuran algorithm
   */
   double Cdf(double x) const;   


protected: 


private: 


   const ROOT::Math::IGenFunction * fPdf;         // pointer to the pdf 
   const ROOT::Math::IGenFunction * fDPdf;       //pointer to the derivative of the pdf
   const ROOT::Math::IGenFunction * fCdf;       //pointer to the cdf (cumulative dist.)

   double fXmin;            //lower value of the domain 
   double fXmax;            //upper value of the domain
   double fMode;            //mode of the distribution
   double fArea;            //area below pdf

   // flags
   bool  fIsLogPdf;         //flag to control if function pointer represent log of pdf
   bool  fHasDomain;        //flag to control if distribution has a defined domain (otherwise is [-inf,+inf]
   bool  fHasMode;          //flag to control if distribution has a pre-computed mode
   bool  fHasArea;          //flag to control if distribution has a pre-computed area below the pdf
   bool  fOwnFunc;          // flag to indicate if class manages the function pointers
   //mutable double fX[1];         //! cached vector for using TF1::EvalPar

   ClassDef(TUnuranContDist,1)  //Wrapper class for one dimensional continuous distribution


}; 



#endif /* ROOT_Math_TUnuranContDist */
 TUnuranContDist.h:1
 TUnuranContDist.h:2
 TUnuranContDist.h:3
 TUnuranContDist.h:4
 TUnuranContDist.h:5
 TUnuranContDist.h:6
 TUnuranContDist.h:7
 TUnuranContDist.h:8
 TUnuranContDist.h:9
 TUnuranContDist.h:10
 TUnuranContDist.h:11
 TUnuranContDist.h:12
 TUnuranContDist.h:13
 TUnuranContDist.h:14
 TUnuranContDist.h:15
 TUnuranContDist.h:16
 TUnuranContDist.h:17
 TUnuranContDist.h:18
 TUnuranContDist.h:19
 TUnuranContDist.h:20
 TUnuranContDist.h:21
 TUnuranContDist.h:22
 TUnuranContDist.h:23
 TUnuranContDist.h:24
 TUnuranContDist.h:25
 TUnuranContDist.h:26
 TUnuranContDist.h:27
 TUnuranContDist.h:28
 TUnuranContDist.h:29
 TUnuranContDist.h:30
 TUnuranContDist.h:31
 TUnuranContDist.h:32
 TUnuranContDist.h:33
 TUnuranContDist.h:34
 TUnuranContDist.h:35
 TUnuranContDist.h:36
 TUnuranContDist.h:37
 TUnuranContDist.h:38
 TUnuranContDist.h:39
 TUnuranContDist.h:40
 TUnuranContDist.h:41
 TUnuranContDist.h:42
 TUnuranContDist.h:43
 TUnuranContDist.h:44
 TUnuranContDist.h:45
 TUnuranContDist.h:46
 TUnuranContDist.h:47
 TUnuranContDist.h:48
 TUnuranContDist.h:49
 TUnuranContDist.h:50
 TUnuranContDist.h:51
 TUnuranContDist.h:52
 TUnuranContDist.h:53
 TUnuranContDist.h:54
 TUnuranContDist.h:55
 TUnuranContDist.h:56
 TUnuranContDist.h:57
 TUnuranContDist.h:58
 TUnuranContDist.h:59
 TUnuranContDist.h:60
 TUnuranContDist.h:61
 TUnuranContDist.h:62
 TUnuranContDist.h:63
 TUnuranContDist.h:64
 TUnuranContDist.h:65
 TUnuranContDist.h:66
 TUnuranContDist.h:67
 TUnuranContDist.h:68
 TUnuranContDist.h:69
 TUnuranContDist.h:70
 TUnuranContDist.h:71
 TUnuranContDist.h:72
 TUnuranContDist.h:73
 TUnuranContDist.h:74
 TUnuranContDist.h:75
 TUnuranContDist.h:76
 TUnuranContDist.h:77
 TUnuranContDist.h:78
 TUnuranContDist.h:79
 TUnuranContDist.h:80
 TUnuranContDist.h:81
 TUnuranContDist.h:82
 TUnuranContDist.h:83
 TUnuranContDist.h:84
 TUnuranContDist.h:85
 TUnuranContDist.h:86
 TUnuranContDist.h:87
 TUnuranContDist.h:88
 TUnuranContDist.h:89
 TUnuranContDist.h:90
 TUnuranContDist.h:91
 TUnuranContDist.h:92
 TUnuranContDist.h:93
 TUnuranContDist.h:94
 TUnuranContDist.h:95
 TUnuranContDist.h:96
 TUnuranContDist.h:97
 TUnuranContDist.h:98
 TUnuranContDist.h:99
 TUnuranContDist.h:100
 TUnuranContDist.h:101
 TUnuranContDist.h:102
 TUnuranContDist.h:103
 TUnuranContDist.h:104
 TUnuranContDist.h:105
 TUnuranContDist.h:106
 TUnuranContDist.h:107
 TUnuranContDist.h:108
 TUnuranContDist.h:109
 TUnuranContDist.h:110
 TUnuranContDist.h:111
 TUnuranContDist.h:112
 TUnuranContDist.h:113
 TUnuranContDist.h:114
 TUnuranContDist.h:115
 TUnuranContDist.h:116
 TUnuranContDist.h:117
 TUnuranContDist.h:118
 TUnuranContDist.h:119
 TUnuranContDist.h:120
 TUnuranContDist.h:121
 TUnuranContDist.h:122
 TUnuranContDist.h:123
 TUnuranContDist.h:124
 TUnuranContDist.h:125
 TUnuranContDist.h:126
 TUnuranContDist.h:127
 TUnuranContDist.h:128
 TUnuranContDist.h:129
 TUnuranContDist.h:130
 TUnuranContDist.h:131
 TUnuranContDist.h:132
 TUnuranContDist.h:133
 TUnuranContDist.h:134
 TUnuranContDist.h:135
 TUnuranContDist.h:136
 TUnuranContDist.h:137
 TUnuranContDist.h:138
 TUnuranContDist.h:139
 TUnuranContDist.h:140
 TUnuranContDist.h:141
 TUnuranContDist.h:142
 TUnuranContDist.h:143
 TUnuranContDist.h:144
 TUnuranContDist.h:145
 TUnuranContDist.h:146
 TUnuranContDist.h:147
 TUnuranContDist.h:148
 TUnuranContDist.h:149
 TUnuranContDist.h:150
 TUnuranContDist.h:151
 TUnuranContDist.h:152
 TUnuranContDist.h:153
 TUnuranContDist.h:154
 TUnuranContDist.h:155
 TUnuranContDist.h:156
 TUnuranContDist.h:157
 TUnuranContDist.h:158
 TUnuranContDist.h:159
 TUnuranContDist.h:160
 TUnuranContDist.h:161
 TUnuranContDist.h:162
 TUnuranContDist.h:163
 TUnuranContDist.h:164
 TUnuranContDist.h:165
 TUnuranContDist.h:166
 TUnuranContDist.h:167
 TUnuranContDist.h:168
 TUnuranContDist.h:169
 TUnuranContDist.h:170
 TUnuranContDist.h:171
 TUnuranContDist.h:172
 TUnuranContDist.h:173
 TUnuranContDist.h:174
 TUnuranContDist.h:175
 TUnuranContDist.h:176
 TUnuranContDist.h:177
 TUnuranContDist.h:178
 TUnuranContDist.h:179
 TUnuranContDist.h:180
 TUnuranContDist.h:181
 TUnuranContDist.h:182
 TUnuranContDist.h:183
 TUnuranContDist.h:184
 TUnuranContDist.h:185
 TUnuranContDist.h:186
 TUnuranContDist.h:187
 TUnuranContDist.h:188
 TUnuranContDist.h:189
 TUnuranContDist.h:190
 TUnuranContDist.h:191
 TUnuranContDist.h:192
 TUnuranContDist.h:193
 TUnuranContDist.h:194
 TUnuranContDist.h:195
 TUnuranContDist.h:196
 TUnuranContDist.h:197
 TUnuranContDist.h:198
 TUnuranContDist.h:199
 TUnuranContDist.h:200
 TUnuranContDist.h:201
 TUnuranContDist.h:202
 TUnuranContDist.h:203
 TUnuranContDist.h:204
 TUnuranContDist.h:205
 TUnuranContDist.h:206
 TUnuranContDist.h:207
 TUnuranContDist.h:208
 TUnuranContDist.h:209
 TUnuranContDist.h:210
 TUnuranContDist.h:211
 TUnuranContDist.h:212
 TUnuranContDist.h:213
 TUnuranContDist.h:214
 TUnuranContDist.h:215