Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:17

0001 // @(#)root/mathmore:$Id$
0002 // Author: L. Moneta, A. Zsenei   08/2005
0003 
0004  /**********************************************************************
0005   *                                                                    *
0006   * Copyright (c) 2004 ROOT Foundation,  CERN/PH-SFT                   *
0007   *                                                                    *
0008   * This library is free software; you can redistribute it and/or      *
0009   * modify it under the terms of the GNU General Public License        *
0010   * as published by the Free Software Foundation; either version 2     *
0011   * of the License, or (at your option) any later version.             *
0012   *                                                                    *
0013   * This library is distributed in the hope that it will be useful,    *
0014   * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
0015   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
0016   * General Public License for more details.                           *
0017   *                                                                    *
0018   * You should have received a copy of the GNU General Public License  *
0019   * along with this library (see file COPYING); if not, write          *
0020   * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
0021   * 330, Boston, MA 02111-1307 USA, or contact the author.             *
0022   *                                                                    *
0023   **********************************************************************/
0024 
0025 // Header file for class GSLRandom
0026 //
0027 // Created by: moneta  at Sun Nov 21 16:26:03 2004
0028 //
0029 // Last update: Sun Nov 21 16:26:03 2004
0030 //
0031 #ifndef ROOT_Math_GSLRndmEngines
0032 #define ROOT_Math_GSLRndmEngines
0033 
0034 #include <string>
0035 #include <vector>
0036 
0037 
0038 namespace ROOT {
0039 namespace Math {
0040 
0041 
0042    class GSLRngWrapper;
0043    class GSLMCIntegrator;
0044 
0045    //_________________________________________________________________
0046    /**
0047       GSLRandomEngine
0048       Base class for all GSL random engines,
0049       normally user instantiate the derived classes
0050       which creates internally the generator.
0051 
0052       The main GSL generators (see
0053       <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">
0054       here</A>) are available as derived classes
0055       In addition to generate uniform numbers it provides method for
0056       generating numbers according to pre-defined distributions
0057       using the GSL functions from
0058       <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html">
0059       GSL random number distributions</A>.
0060 
0061 
0062 
0063       @ingroup Random
0064    */
0065    class GSLRandomEngine {
0066 
0067       friend class GSLMCIntegrator;
0068 
0069    public:
0070 
0071      /**
0072          default constructor. No creation of rng is done.
0073          If then Initialize() is called an engine is created
0074          based on default GSL type (MT)
0075      */
0076       GSLRandomEngine();
0077 
0078       /**
0079           create from an existing rng.
0080           User manage the rng pointer which is then deleted only by calling Terminate()
0081       */
0082       GSLRandomEngine( GSLRngWrapper * rng);
0083 
0084       /**
0085          Copy constructor : clone the contained GSL generator
0086        */
0087       GSLRandomEngine(const GSLRandomEngine & eng);
0088 
0089       /**
0090          Assignment operator : make a deep copy of the contained GSL generator
0091        */
0092       GSLRandomEngine & operator=(const GSLRandomEngine & eng);
0093 
0094       /**
0095          initialize the generator
0096          If no rng is present the default one based on Mersenne and Twister is created
0097        */
0098       void Initialize();
0099 
0100       /**
0101          delete pointer to contained rng
0102        */
0103       void Terminate();
0104 
0105       /**
0106          call Terminate()
0107       */
0108       virtual ~GSLRandomEngine();
0109 
0110       /**
0111          Generate a  random number between ]0,1]
0112          0 is excluded and 1 is included
0113       */
0114       double operator() () const;
0115 
0116       /**
0117          Generate a  random number between ]0,1]
0118          0 is excluded and 1 is included
0119       */
0120       double Rndm() const { return (*this)(); }
0121 
0122       /**
0123           Generate an integer number between [0,max-1] (including 0 and max-1)
0124           if max is larger than available range of algorithm
0125           an error message is printed and zero is returned
0126       */
0127       unsigned long RndmInt(unsigned long max) const;
0128       /**
0129           Generate an integer number between [0,max_generator-1] (including 0 and max-1)
0130           if max is larger than available range of algorithm
0131           an error message is printed and zero is returned
0132       */
0133       unsigned long IntRndm() const {
0134          return RndmInt(MaxInt());   // max return the largest value the generator can give +1
0135       }
0136 
0137       /**
0138          Generate an array of random numbers.
0139          The iterators points to the random numbers
0140       */
0141       template<class Iterator>
0142       void RandomArray(Iterator begin, Iterator end) const {
0143          for ( Iterator itr = begin; itr != end; ++itr ) {
0144             *itr = this->operator()();
0145          }
0146       }
0147 
0148       /**
0149          Generate an array of random numbers
0150          The iterators points to the random numbers
0151       */
0152       void RandomArray(double * begin, double * end) const;
0153 
0154       /**
0155          return name of generator
0156       */
0157       std::string Name() const;
0158 
0159       /**
0160          return the state size of generator
0161       */
0162       unsigned int Size() const;
0163 
0164       /**
0165          return the minimum integer a generator can handle
0166          typically this value is 0
0167        */
0168       unsigned long MinInt() const;
0169 
0170       /**
0171          return the maximum integer +1 a generator can handle
0172 
0173        */
0174       unsigned long MaxInt() const;
0175 
0176       /**
0177           set the random generator seed
0178       */
0179       void SetSeed(unsigned int seed) const;
0180 
0181 
0182       /** @name Random Distributions
0183           Implemented using the
0184           <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html">
0185           GSL Random number Distributions</A>
0186       **/
0187       //@{
0188       /**
0189          Gaussian distribution - default method is Box-Muller (polar method)
0190       */
0191       double Gaussian(double sigma) const;
0192 
0193       /**
0194          Gaussian distribution - Ziggurat method
0195       */
0196       double GaussianZig(double sigma) const;
0197 
0198       /**
0199          Gaussian distribution - Ratio method
0200       */
0201       double GaussianRatio(double sigma) const;
0202       /**
0203          Gaussian Tail distribution
0204       */
0205       double GaussianTail(double a, double sigma) const;
0206 
0207       /**
0208          Bivariate Gaussian distribution with correlation
0209       */
0210       void Gaussian2D(double sigmaX, double sigmaY, double rho, double &x, double &y) const;
0211 
0212       /**
0213          Multivariate Gaussian distribution
0214       */
0215       void GaussianND(size_t dim, const double *pars, const double *covmat, double *genpars, double * lmat = nullptr) const;
0216 
0217 
0218       /**
0219          Exponential distribution
0220       */
0221       double Exponential(double mu) const;
0222 
0223       /**
0224          Cauchy distribution
0225       */
0226       double Cauchy(double a) const;
0227 
0228       /**
0229          Landau distribution
0230       */
0231       double Landau() const;
0232 
0233       /**
0234          Gamma distribution
0235       */
0236       double Gamma(double a, double b) const;
0237 
0238       /**
0239          Beta distribution
0240       */
0241       double Beta(double a, double b) const;
0242 
0243       /**
0244          Log Normal distribution
0245       */
0246       double LogNormal(double zeta, double sigma) const;
0247 
0248       /**
0249          Chi square distribution
0250       */
0251       double ChiSquare(double nu) const;
0252 
0253       /**
0254          F distribution
0255       */
0256       double FDist(double nu1, double nu2) const;
0257 
0258       /**
0259          t student distribution
0260       */
0261       double tDist(double nu) const;
0262 
0263       /**
0264          Rayleigh distribution
0265       */
0266       double Rayleigh(double sigma) const;
0267 
0268       /**
0269          Logistic distribution
0270       */
0271       double Logistic(double a) const;
0272 
0273       /**
0274          Pareto distribution
0275       */
0276       double Pareto(double a, double b) const;
0277 
0278       /**
0279          generate random numbers in a 2D circle of radious 1
0280       */
0281       void Dir2D(double &x, double &y) const;
0282 
0283       /**
0284          generate random numbers in a 3D sphere of radious 1
0285       */
0286       void Dir3D(double &x, double &y, double &z) const;
0287 
0288       /**
0289          Poisson distribution
0290       */
0291       unsigned int Poisson(double mu) const;
0292 
0293       /**
0294          Binomial distribution
0295       */
0296       unsigned int Binomial(double p, unsigned int n) const;
0297 
0298       /**
0299          Negative Binomial distribution
0300       */
0301       unsigned int NegativeBinomial(double p, double n) const;
0302 
0303       /**
0304          Multinomial distribution
0305       */
0306       std::vector<unsigned int> Multinomial( unsigned int ntot, const std::vector<double> & p ) const;
0307 
0308       //@}
0309 
0310 
0311 
0312    protected:
0313 
0314       /// internal method used by the derived class to set the type of generators
0315       void SetType(GSLRngWrapper * r) {
0316          fRng = r;
0317       }
0318 
0319       /// internal method to return the engine
0320       /// Used by class like GSLMCIntegrator to set the engine
0321       GSLRngWrapper * Engine() {
0322          return fRng;
0323       }
0324 
0325    private:
0326 
0327       GSLRngWrapper * fRng;                // pointer to GSL generator wrapper (managed by the class)
0328       mutable unsigned int  fCurTime;      // current time used to seed the generator
0329 
0330    };
0331 
0332    //_____________________________________________________________________________________
0333    /**
0334       Mersenne-Twister generator
0335       gsl_rng_mt19937 from
0336       <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0337 
0338 
0339       @ingroup Random
0340    */
0341    class GSLRngMT : public GSLRandomEngine {
0342    public:
0343       typedef GSLRandomEngine BaseType;
0344       GSLRngMT();
0345    };
0346 
0347    //_____________________________________________________________________________________
0348    /**
0349       Old Ranlux generator (James, Luscher) (default luxury level, p = 223)
0350       (This is eequivalent to TRandom1 with default luxury level)
0351       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0352 
0353       @ingroup Random
0354    */
0355    class GSLRngRanLux : public GSLRandomEngine {
0356    public:
0357       typedef GSLRandomEngine BaseType;
0358       GSLRngRanLux();
0359    };
0360 
0361    //_____________________________________________________________________________________
0362    /**
0363       Second generation of Ranlux generator for single precision with  luxury level of 1
0364       (It throws away 202 values for every 12 used)
0365       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0366 
0367       @ingroup Random
0368    */
0369    class GSLRngRanLuxS1 : public GSLRandomEngine {
0370    public:
0371       typedef GSLRandomEngine BaseType;
0372       GSLRngRanLuxS1();
0373    };
0374    typedef GSLRngRanLuxS1 GSLRngRanLux1; // for backward compatibility
0375 
0376    //_____________________________________________________________________________________
0377    /**
0378       Second generation of Ranlux generator for Single precision with  luxury level of 2
0379       (It throws away 397 value for every 12 used)
0380       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0381 
0382       @ingroup Random
0383    */
0384    class GSLRngRanLuxS2 : public GSLRandomEngine {
0385    public:
0386       typedef GSLRandomEngine BaseType;
0387       GSLRngRanLuxS2();
0388    };
0389    typedef GSLRngRanLuxS2 GSLRngRanLux2; // for backward compatibility
0390 
0391    //_____________________________________________________________________________________
0392    /**
0393       Double precision (48 bits) version of Second generation of Ranlux generator with  luxury level of 1
0394       (It throws away 202 value for every 12 used)
0395       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0396 
0397       @ingroup Random
0398    */
0399    class GSLRngRanLuxD1 : public GSLRandomEngine {
0400    public:
0401       typedef GSLRandomEngine BaseType;
0402       GSLRngRanLuxD1();
0403    };
0404 
0405    //_____________________________________________________________________________________
0406    /**
0407       Double precision (48 bits) version of Second generation of Ranlux generator with  luxury level of 2
0408       (It throws away 397 value for every 12 used)
0409       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0410 
0411       @ingroup Random
0412    */
0413    class GSLRngRanLuxD2 : public GSLRandomEngine {
0414    public:
0415       typedef GSLRandomEngine BaseType;
0416       GSLRngRanLuxD2();
0417    };
0418    typedef GSLRngRanLuxD2 GSLRngRanLux48; // for backward compatibility
0419 
0420 
0421    //_____________________________________________________________________________________
0422    /**
0423       Tausworthe generator by L'Ecuyer
0424       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0425 
0426       @ingroup Random
0427    */
0428    class GSLRngTaus : public GSLRandomEngine {
0429    public:
0430       typedef GSLRandomEngine BaseType;
0431       GSLRngTaus();
0432    };
0433 
0434    //_____________________________________________________________________________________
0435    /**
0436       Lagged Fibonacci generator by Ziff
0437       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0438 
0439       @ingroup Random
0440    */
0441    class GSLRngGFSR4 : public GSLRandomEngine {
0442    public:
0443       typedef GSLRandomEngine BaseType;
0444       GSLRngGFSR4();
0445    };
0446 
0447    //_____________________________________________________________________________________
0448    /**
0449       Combined multiple recursive  generator (L'Ecuyer)
0450       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0451 
0452       @ingroup Random
0453    */
0454    class GSLRngCMRG : public GSLRandomEngine {
0455    public:
0456       typedef GSLRandomEngine BaseType;
0457       GSLRngCMRG();
0458    };
0459 
0460    //_____________________________________________________________________________________
0461    /**
0462       5-th order multiple recursive  generator (L'Ecuyer, Blouin and Coutre)
0463       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-algorithms.html">here</A>
0464 
0465       @ingroup Random
0466    */
0467    class GSLRngMRG : public GSLRandomEngine {
0468    public:
0469       typedef GSLRandomEngine BaseType;
0470       GSLRngMRG();
0471    };
0472 
0473    //_____________________________________________________________________________________
0474    /**
0475       BSD rand() generator
0476       gsl_rmg_rand from
0477       <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A>
0478 
0479       @ingroup Random
0480    */
0481    class GSLRngRand : public GSLRandomEngine {
0482    public:
0483       typedef GSLRandomEngine BaseType;
0484       GSLRngRand();
0485    };
0486 
0487    //_____________________________________________________________________________________
0488    /**
0489       RANMAR generator
0490       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A>
0491 
0492       @ingroup Random
0493    */
0494    class GSLRngRanMar : public GSLRandomEngine {
0495    public:
0496       typedef GSLRandomEngine BaseType;
0497       GSLRngRanMar();
0498    };
0499 
0500    //_____________________________________________________________________________________
0501    /**
0502       MINSTD generator (Park and Miller)
0503       see <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Unix-random-number-generators.html">here</A>
0504 
0505       @ingroup Random
0506    */
0507    class GSLRngMinStd : public GSLRandomEngine {
0508    public:
0509       typedef GSLRandomEngine BaseType;
0510       GSLRngMinStd();
0511    };
0512 
0513    /** MixMax generator based on ROOT::Math::MixMaxEngine of N=240
0514 
0515        @ingroup Random
0516    */
0517    class GSLRngMixMax : public GSLRandomEngine {
0518    public:
0519       typedef GSLRandomEngine BaseType;
0520       GSLRngMixMax();
0521       ~GSLRngMixMax() override;  // we need a dtcor since is not a standard GSL engine
0522    };
0523 
0524 } // namespace Math
0525 } // namespace ROOT
0526 
0527 // random functions specialization for GSL
0528 // needs to be defined after defining GSLRandomEngine class
0529 
0530 #include "Math/GSLRandomFunctions.h"
0531 
0532 #endif /* ROOT_Math_GSLRndmEngines */
0533