Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
GSLRngROOTWrapper.h
Go to the documentation of this file.
1// wrapper class used to wrap ROOT random number engines with GSL interface
2
3// @(#)root/mathmore:$Id$
4// Author: L. Moneta Fri Aug 24 17:20:45 2007
5
6/**********************************************************************
7 * *
8 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
9 * *
10 * *
11 **********************************************************************/
12
13// Header file for class GSLRngWrapper
14
15#ifndef ROOT_Math_GSLRngROOTWrapper
16#define ROOT_Math_GSLRngROOTWrapper
17
18#include "gsl/gsl_rng.h"
19
20#include <string>
21
22namespace ROOT {
23
24 namespace Math {
25
26 /**
27 * class for wrapping ROOT Engines in gsl_rng types which can be used as extra
28 * GSL random number generators
29 * For this we need to implement functions which will be called by gsl_rng.
30 * The functions (Seed, Rndm, IntRndm) are passed in the gsl_rng_type and used to build a gsl_rng object.
31 * When gsl_rng is alloacated, only the memory state is allocated using calloc(1,size), which gives a memory
32 * block of the given bytes and it initializes to zero. Therefore no constructor of GSLRngROOTWrapper can be called
33 * and also we cannot call non-static member function of the class.
34 * The underlined ROOT engine is then built and deleted using the functions CreateEngine() and FreeEngine(),
35 * called by the specific GSLRandomEngine class that instantiates for the the generator (e.g. GSLRngMixMax)
36 *
37 **/
38 template <class Engine>
40
41 Engine *fEngine = nullptr;
42
43 // non need to have specific ctor/dtor since we cannot call them
44
45 // function called by the specific GSLRndmEngine to create the ROOT engine
46 static void CreateEngine(gsl_rng *r)
47 {
48 // the engine pointer is retrieved from r
49 GSLRngROOTWrapper *wrng = ((GSLRngROOTWrapper *)r->state);
50 //printf("calling create engine - engine pointer : %p\n", wrng->fEngine);
51 if (!wrng->fEngine)
52 wrng->fEngine = new Engine();
53 // do nothing in case it is already created (e.g. when calling with default_seed)
54 }
55
56 static double Rndm(void *p) { return ((GSLRngROOTWrapper *)p)->fEngine->operator()(); }
57 static unsigned long IntRndm(void *p) { return ((GSLRngROOTWrapper *)p)->fEngine->IntRndm(); }
58
59 static void Seed(void *p, unsigned long seed)
60 {
61 auto wrng = ((GSLRngROOTWrapper *)p);
62 // (GSL calls at the beginning with the default seed (typically zero))
63 //printf("calling the seed function with %d on %p and engine %p\n", seed, p, wrng->fEngine);
64 if (seed == gsl_rng_default_seed) {
65 seed = 111; // avoid using 0 that for ROOT means a specific seed
66 if (!wrng->fEngine) wrng->fEngine = new Engine();
67 }
68 assert(wrng->fEngine != nullptr);
69 wrng->fEngine->SetSeed(seed);
70 }
71 static void FreeEngine(gsl_rng *r)
72 {
73 auto wrng = ((GSLRngROOTWrapper *)r->state);
74 if (wrng->fEngine)
75 delete wrng->fEngine;
76 wrng->fEngine = nullptr;
77 }
78
79 static unsigned long Max() { return Engine::MaxInt(); }
80 static unsigned long Min() { return Engine::MinInt(); }
81 static size_t Size() { return sizeof(GSLRngROOTWrapper<Engine>); }
82 static std::string Name() { return std::string("GSL_") + Engine::Name(); }
83 };
84
85 } // end namespace Math
86} // end namespace ROOT
87
88#include "Math/MixMaxEngine.h"
89
90// now define and implement the specific types
91
93
94static const std::string gsl_mixmax_name = GSLMixMaxWrapper::Name();
95static const gsl_rng_type mixmax_type =
96{
97 gsl_mixmax_name.c_str(),
104};
105
106const gsl_rng_type *gsl_rng_mixmax = &mixmax_type;
107
108#endif
109
static const std::string gsl_mixmax_name
const gsl_rng_type * gsl_rng_mixmax
ROOT::Math::GSLRngROOTWrapper< ROOT::Math::MixMaxEngine< 17, 0 > > GSLMixMaxWrapper
static const gsl_rng_type mixmax_type
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Namespace for new Math classes and functions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
class for wrapping ROOT Engines in gsl_rng types which can be used as extra GSL random number generat...
static unsigned long IntRndm(void *p)
static void FreeEngine(gsl_rng *r)
static void Seed(void *p, unsigned long seed)
static void CreateEngine(gsl_rng *r)
static double Rndm(void *p)