Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TUnuranSampler.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/mathcore:$Id$
0002 // Author: L. Moneta Fri Sep 22 15:06:47 2006
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 // Header file for class TUnuranSampler
0011 
0012 #ifndef ROOT_TUnuranSampler
0013 #define ROOT_TUnuranSampler
0014 
0015 
0016 #include "Math/DistSampler.h"
0017 
0018 // needed by the ClassDef
0019 #include "Rtypes.h"
0020 
0021 namespace ROOT {
0022 
0023    namespace Fit {
0024 
0025       class DataRange;
0026       class BinData;
0027       class UnBinData;
0028    }
0029 
0030    namespace Math {
0031    }
0032 }
0033 
0034 
0035 
0036 //_______________________________________________________________________________
0037 /**
0038    \class TUnuranSampler
0039    \ingroup Unuran
0040 
0041    TUnuranSampler class
0042    class implementing  the ROOT::Math::DistSampler interface using the UNU.RAN
0043    package for sampling distributions.
0044 
0045 */
0046 
0047 class TRandom;
0048 class TF1;
0049 class TUnuran;
0050 
0051 class TUnuranSampler : public ROOT::Math::DistSampler {
0052 
0053 public:
0054 
0055    /// default constructor
0056    TUnuranSampler();
0057 
0058 
0059    /// virtual destructor
0060    ~TUnuranSampler() override;
0061 
0062 
0063    using DistSampler::SetFunction;
0064 
0065    /// Set the parent function distribution to use for random sampling (one dim case).
0066    void SetFunction(const ROOT::Math::IGenFunction & func) override  {
0067       fFunc1D = &func;
0068       SetFunction<const ROOT::Math::IGenFunction>(func, 1);
0069    }
0070 
0071    /// Set the Function using a TF1 pointer.
0072    void SetFunction(TF1 * pdf);
0073 
0074    /// set the cumulative distribution function of the PDF used for random sampling (one dim case)
0075    void SetCdf(const ROOT::Math::IGenFunction &cdf) override;
0076 
0077    /// set the Derivative of the PDF used for random sampling (one dim continuous case)
0078    void SetDPdf(const ROOT::Math::IGenFunction &dpdf) override;
0079 
0080    /**
0081       initialize the generators with the given algorithm
0082       If no algorithm is passed used the default one for the type of distribution
0083    */
0084    bool Init(const char * algo ="") override;
0085 
0086 
0087    /**
0088       initialize the generators with the given algorithm
0089       If no algorithm is passed used the default one for the type of distribution
0090    */
0091    bool Init(const ROOT::Math::DistSamplerOptions & opt ) override;
0092 
0093    /**
0094        Set the random engine to be used
0095        Needs to be called before Init to have effect
0096    */
0097    void SetRandom(TRandom * r) override;
0098 
0099    /**
0100        Set the random seed for the TRandom instances used by the sampler
0101        classes
0102        Needs to be called before Init to have effect
0103    */
0104    void SetSeed(unsigned int seed) override;
0105 
0106    /**
0107       Set the print level
0108       (if level=-1 use default)
0109     */
0110    void SetPrintLevel(int level) {fLevel = level;}
0111 
0112    /*
0113       set the mode (1D distribution)
0114     */
0115    void SetMode(double mode) override {
0116       fMode = mode;
0117       fHasMode = true;
0118    }
0119 
0120    /*
0121       set the mode (Multidim distribution)
0122    */
0123    void SetMode(const std::vector<double> &modes) override;
0124 
0125 
0126    /*
0127      set the area
0128     */
0129    void SetArea(double area) override {
0130       fArea = area;
0131       fHasArea = true;
0132    }
0133 
0134    /// Set using of logarithm of PDF (only for 1D continuous case)
0135    void SetUseLogPdf(bool on = true) override { fUseLogPdf = on; }
0136 
0137    /**
0138       Get the random engine used by the sampler
0139     */
0140    TRandom * GetRandom() override;
0141 
0142    /**
0143       sample one event in one dimension
0144       better implementation could be provided by the derived classes
0145    */
0146    double Sample1D() override;//  {
0147 //       return fUnuran->Sample();
0148 //    }
0149 
0150    /**
0151       sample one event in multi-dimension by filling the given array
0152       return false if sampling failed
0153    */
0154    bool Sample(double * x) override;
0155 //  {
0156 //       if (!fOneDim) return fUnuran->SampleMulti(x);
0157 //       x[0] = Sample1D();
0158 //       return true;
0159 //    }
0160 
0161    /**
0162       sample one bin given an estimated of the pdf in the bin
0163       (this can be function value at the center or its integral in the bin
0164       divided by the bin width)
0165       By default do not do random sample, just return the function values
0166     */
0167    bool SampleBin(double prob, double & value, double *error = nullptr) override;
0168 
0169 
0170 
0171 protected:
0172 
0173    /// Initialization for 1D distributions.
0174    bool DoInit1D(const char * algo);
0175    /// Initialization for 1D discrete distributions.
0176    bool DoInitDiscrete1D(const char * algo);
0177    /// Initialization for multi-dim distributions.
0178    bool DoInitND(const char * algo);
0179 
0180 
0181 private:
0182 
0183    // private member
0184    bool                              fOneDim = false;      ///< flag to indicate if the function is 1 dimension
0185    bool                              fDiscrete = false;    ///< flag to indicate if the function is discrete
0186    bool                              fHasMode = false;     ///< flag to indicate if a mode is set
0187    bool                              fHasArea = false;     ///< flag to indicate if a area is set
0188    bool                              fUseLogPdf = false;   ///< flag to indicate if we use the log of the PDF
0189    int fLevel;                                     ///< debug level
0190    double                            fMode;        ///< mode of dist (1D)
0191    double                            fArea;        ///< area of dist
0192    std::vector<double>               fNDMode;      ///< mode of the multi-dim distribution
0193    const ROOT::Math::IGenFunction *  fFunc1D = nullptr;      ///< 1D function pointer (pdf)
0194    const ROOT::Math::IGenFunction *  fCDF    = nullptr;      ///< CDF function pointer
0195    const ROOT::Math::IGenFunction *  fDPDF   = nullptr;      ///< 1D Derivative function pointer
0196    TUnuran *                         fUnuran = nullptr;      ///< unuran engine class
0197 
0198    ClassDef(TUnuranSampler, 2);                    // Distribution sampler class based on UNU.RAN
0199 };
0200 
0201 
0202 
0203 #endif /* ROOT_TUnuranSampler */