#ifndef _utl_RandomSamplerFromCDF_h_ #define _utl_RandomSamplerFromCDF_h_ #include #include #include #include #include namespace utl { class RandomSamplerFromPDF; class TabulatedFunction; /** \class RandomSamplerFromCDF RandomSamplerFromCDF.h "utl/RandomSamplerFromCDF.h" Input requirements: CDF has to start with zero, does not have to be normalized. \author Darko Veberic \date 27 Aug 2009 \version $Id: RandomSamplerFromCDF.h 15948 2010-03-21 21:58:33Z darko $ \ingroup math */ class RandomSamplerFromCDF : public VRandomSampler { public: /// Construct using a CDF defined by X and Y vectors RandomSamplerFromCDF(const std::vector& x, const std::vector& y) : fX(x), fY(y) { CheckAndNormalize(); } /// Construct using a CDF defined by a Tabulated Function RandomSamplerFromCDF(const TabulatedFunction& cdf); /// Construct using a CDF defined by an std::vector, the old CLHEP behaviour RandomSamplerFromCDF(const std::vector& cdf); RandomSamplerFromCDF(const std::map& cdf); /// Construct the CDF from a function defined by a string RandomSamplerFromCDF(const std::string& function, const std::string& independentVariableName, const double min, const double max, const int bins) { SetFunction(function, independentVariableName, min, max, bins); } RandomSamplerFromCDF(const std::string& function, const double min, const double max, const int bins) { SetFunction(function, "x", min, max, bins); } virtual ~RandomSamplerFromCDF() { } double GetInverseCDF(const double y) const; double GetMinX() const { return fX.front(); } double GetMaxX() const { return fX.back(); } protected: virtual double MapRandom(const double rand) const { return GetInverseCDF(rand); } const std::vector& GetCDFX() const { return fX; } const std::vector& GetCDFY() const { return fY; } private: void SetFunction(const std::string& function, const std::string& independentVariableName, const double min, const double max, const int bins); int GetIndex(const double y) const { return std::distance(fY.begin(), std::lower_bound(fY.begin(), fY.end(), y)); } void CheckAndNormalize(); void Dump() const; std::vector fX; std::vector fY; friend class RandomSamplerFromPDF; }; } #endif