Logo ROOT   6.08/07
Reference Guide
GSLMCIntegrator.cxx
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: Magdalena Slawinska 08/2007
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2007 ROOT Foundation, CERN/PH-SFT *
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU General Public License *
10  * as published by the Free Software Foundation; either version 2 *
11  * of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library (see file COPYING); if not, write *
20  * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21  * 330, Boston, MA 02111-1307 USA, or contact the author. *
22  * *
23  **********************************************************************/
24 //
25 // implementation file for class GSLMCIntegrator
26 // Author: Magdalena Slawinska
27 //
28 //
29 
30 #include "Math/IFunctionfwd.h"
31 #include "Math/IFunction.h"
32 #include "Math/Error.h"
33 #include <vector>
34 
36 
37 #include "Math/GSLMCIntegrator.h"
38 #include "Math/GSLRndmEngines.h"
40 #include "GSLRngWrapper.h"
41 
42 #include <algorithm>
43 #include <functional>
44 #include <ctype.h> // need to use c version of tolower defined here
45 
46 
47 #include "gsl/gsl_monte_vegas.h"
48 #include "gsl/gsl_monte_miser.h"
49 #include "gsl/gsl_monte_plain.h"
50 
51 
52 
53 namespace ROOT {
54 namespace Math {
55 
56 
57 
58 // constructors
59 
60 // GSLMCIntegrator::GSLMCIntegrator():
61 // fResult(0),fError(0),fStatus(-1),
62 // fWorkspace(0),
63 // fFunction(0)
64 // {
65 // // constructor of GSL MCIntegrator.Vegas MC is set as default integration type
66 // //set random number generator
67 // fRng = new GSLRngWrapper();
68 // fRng->Allocate();
69 // // use the default options
70 // ROOT::Math::IntegratorMultiDimOptions opts; // this create the default options
71 // SetOptions(opts);
72 // }
73 
74 
75 GSLMCIntegrator::GSLMCIntegrator(MCIntegration::Type type, double absTol, double relTol, unsigned int calls):
76  fType(type),
77  fDim(0),
78  fCalls((calls > 0) ? calls : IntegratorMultiDimOptions::DefaultNCalls()),
79  fAbsTol((absTol >= 0) ? absTol : IntegratorMultiDimOptions::DefaultAbsTolerance() ),
80  fRelTol((relTol >= 0) ? relTol : IntegratorMultiDimOptions::DefaultRelTolerance() ),
81  fResult(0),fError(0),fStatus(-1),
82  fExtGen(false),
83  fWorkspace(0),
84  fFunction(0)
85 {
86  // constructor of GSL MCIntegrator using enumeration as type
87  SetType(type);
88  //set random number generator
89  fRng = new GSLRngWrapper();
90  fRng->Allocate();
91  // use the default options for the needed extra parameters
92  // use the default options for the needed extra parameters
95  if (opts != 0) SetParameters( VegasParameters(*opts) );
96  }
97  else if (fType == MCIntegration::kMISER) {
99  if (opts != 0) SetParameters( MiserParameters(*opts) );
100  }
101 
102 }
103 
104 GSLMCIntegrator::GSLMCIntegrator(const char * type, double absTol, double relTol, unsigned int calls):
105  fDim(0),
106  fCalls(calls),
107  fAbsTol(absTol),
108  fRelTol(relTol),
109  fResult(0),fError(0),fStatus(-1),
110  fExtGen(false),
111  fWorkspace(0),
112  fFunction(0)
113 {
114  // constructor of GSL MCIntegrator. Vegas MC is set as default integration type if type == 0
115  SetTypeName(type);
116 
117  //set random number generator
118  fRng = new GSLRngWrapper();
119  fRng->Allocate();
120  // use the default options for the needed extra parameters
121  if (fType == MCIntegration::kVEGAS) {
123  if (opts != 0) SetParameters( VegasParameters(*opts) );
124  }
125  else if (fType == MCIntegration::kMISER) {
127  if (opts != 0) SetParameters( MiserParameters(*opts) );
128  }
129 
130 }
131 
132 
133 
135 {
136  // delete workspace
137  if (fWorkspace) delete fWorkspace;
138  if (fRng != 0 && !fExtGen) delete fRng;
139  if (fFunction != 0) delete fFunction;
140  fRng = 0;
141 
142 }
143 
144 
145 // disable copy ctrs
146 
147 
150 {}
151 
153 
154 
155 
156 
157 
159 {
160  // method to set the a generic integration function
163  fDim = f.NDim();
164 }
165 
166 void GSLMCIntegrator::SetFunction( GSLMonteFuncPointer f, unsigned int dim, void * p )
167 {
168  // method to set the a generic integration function
171  fFunction->SetParams ( p );
172  fDim = dim;
173 }
174 
175 
176 
177 double GSLMCIntegrator::Integral(const double* a, const double* b)
178 {
179  // evaluate the Integral of a over the defined interval (a[],b[])
180  assert(fRng != 0);
181  gsl_rng* fr = fRng->Rng();
182  assert(fr != 0);
183  if (!CheckFunction()) return 0;
184 
185  // initialize by creating the right WS
186  // (if dimension and type are different than previous calculation)
187  DoInitialize();
188 
190  {
192  assert(ws != 0);
193  fStatus = gsl_monte_vegas_integrate( fFunction->GetFunc(), (double *) a, (double*) b , fDim, fCalls, fr, ws->GetWS(), &fResult, &fError);
194  }
195  else if (fType == MCIntegration::kMISER)
196  {
198  assert(ws != 0);
199  fStatus = gsl_monte_miser_integrate( fFunction->GetFunc(), (double *) a, (double *) b , fDim, fCalls, fr, ws->GetWS(), &fResult, &fError);
200  }
201  else if (fType == MCIntegration::kPLAIN)
202  {
204  assert(ws != 0);
205  fStatus = gsl_monte_plain_integrate( fFunction->GetFunc(), (double *) a, (double *) b , fDim, fCalls, fr, ws->GetWS(), &fResult, &fError);
206  }
207  /**/
208  else
209  {
210 
211  fResult = 0;
212  fError = 0;
213  fStatus = -1;
214  std::cerr << "GSLIntegrator - Error: Unknown integration type" << std::endl;
215  throw std::exception();
216  }
217 
218  return fResult;
219 
220 }
221 
222 
223 double GSLMCIntegrator::Integral(const GSLMonteFuncPointer & f, unsigned int dim, double* a, double* b, void * p )
224 {
225  // evaluate the Integral for a function f over the defined interval (a[],b[])
226  SetFunction(f,dim,p);
227  return Integral(a,b);
228 }
229 
230 
231 /* to be added later
232  double GSLMCIntegrator::Integral(GSLMonteFuncPointer f, void * p, double* a, double* b)
233  {
234 
235  }
236 
237 */
238 //MCIntegration::Type GSLMCIntegrator::MCType() const {return fType;}
239 
240 /**
241  return the Result of the last Integral calculation
242 */
243 double GSLMCIntegrator::Result() const { return fResult; }
244 
245 /**
246  return the estimate of the absolute Error of the last Integral calculation
247 */
248 double GSLMCIntegrator::Error() const { return fError; }
249 
250 /**
251  return the Error Status of the last Integral calculation
252 */
253 int GSLMCIntegrator::Status() const { return fStatus; }
254 
255 
256 // setter for control Parameters (getters are not needed so far )
257 
258 /**
259  set the desired relative Error
260 */
261 void GSLMCIntegrator::SetRelTolerance(double relTol){ this->fRelTol = relTol; }
262 
263 /**
264  set the desired absolute Error
265 */
267 
269  // delete previous exist generator
270  if (fRng && !fExtGen) delete fRng;
271  fRng = r.Engine();
272  fExtGen = true;
273 }
274 
276 {
277  // create workspace according to the type
278  fType=type;
279  if (fWorkspace != 0) {
280  if (type == fWorkspace->Type() ) return;
281  delete fWorkspace; // delete because is a different type
282  fWorkspace = 0;
283  }
284  //create Workspace according to type
285  if (type == MCIntegration::kPLAIN) {
287  }
288  else if (type == MCIntegration::kMISER) {
290  }
291  else {
292  // default: use VEGAS
293  if (type != MCIntegration::kVEGAS) {
294  MATH_WARN_MSG("GSLMCIntegration","Invalid integration type : use Vegas as default");
296  }
298  }
299 }
300 
302 {
303  // set the integration type using a string
304  std::string typeName = (type!=0) ? type : "VEGAS";
305  if (type == 0) MATH_INFO_MSG("GSLMCIntegration::SetTypeName","use default Vegas integrator method");
306  std::transform(typeName.begin(), typeName.end(), typeName.begin(), (int(*)(int)) toupper );
307 
308  MCIntegration::Type integType = MCIntegration::kVEGAS; // default
309 
310  if (typeName == "PLAIN") {
311  integType = MCIntegration::kPLAIN;
312  }
313  else if (typeName == "MISER") {
314  integType = MCIntegration::kMISER;
315  }
316  else if (typeName != "VEGAS") {
317  MATH_WARN_MSG("GSLMCIntegration::SetTypeName","Invalid integration type : use Vegas as default");
318  }
319 
320  // create the fWorkspace object
321  if (integType != fType) SetType(integType);
322 }
323 
324 
326 {
327  // set integration mode for VEGAS method
329  {
331  assert(ws != 0);
332  if(mode == MCIntegration::kIMPORTANCE) ws->GetWS()->mode = GSL_VEGAS_MODE_IMPORTANCE;
333  else if(mode == MCIntegration::kSTRATIFIED) ws->GetWS()->mode = GSL_VEGAS_MODE_STRATIFIED;
334  else if(mode == MCIntegration::kIMPORTANCE_ONLY) ws->GetWS()->mode = GSL_VEGAS_MODE_IMPORTANCE_ONLY;
335  }
336 
337  else std::cerr << "Mode not matching integration type";
338 }
339 
341 {
342  // set integration options
343  SetTypeName(opt.Integrator().c_str() );
344  SetAbsTolerance( opt.AbsTolerance() );
345  SetRelTolerance( opt.RelTolerance() );
346  fCalls = opt.NCalls();
347 
348  //std::cout << fType << " " << MCIntegration::kVEGAS << std::endl;
349 
350  // specific options
351  ROOT::Math::IOptions * extraOpt = opt.ExtraOptions();
352  if (extraOpt) {
353  if (fType == MCIntegration::kVEGAS ) {
354  VegasParameters p(*extraOpt);
355  SetParameters(p);
356  }
357  else if (fType == MCIntegration::kMISER ) {
358  MiserParameters p(fDim); // if possible pass dimension
359  p = (*extraOpt);
360  SetParameters(p);
361  }
362  else {
363  MATH_WARN_MSG("GSLMCIntegrator::SetOptions","Invalid options set for the chosen integration type - ignore them");
364  }
365  }
366 }
367 
368 
370 {
371  // set method parameters
373  {
375  assert(ws != 0);
376  ws->SetParameters(p);
377  }
378  else
379  MATH_ERROR_MSG("GSLIntegrator::SetParameters"," Parameters not matching integration type");
380 }
381 
383 {
384  // set method parameters
386  {
388  assert(ws != 0);
389  ws->SetParameters(p);
390  }
391  else
392  MATH_ERROR_MSG("GSLIntegrator::SetParameters"," Parameters not matching integration type");
393 }
394 
395 
397 {
398  // initialize by setting integration type
399 
400  if (fWorkspace == 0) return;
401  if (fDim == fWorkspace->NDim() && fType == fWorkspace->Type() )
402  return; // can use previously existing ws
403 
404  // otherwise clear workspace
405  fWorkspace->Clear();
406  // and create a new one
407  fWorkspace->Init(fDim);
408 }
409 
410 
411 
412 //----------- methods specific for VEGAS
413 
415 {
416  // returns the error sigma from the last iteration of the VEGAS algorithm
418  {
420  assert (ws != 0);
421  return ws->GetWS()->sigma;
422  }
423  else
424  {
425  std::cerr << "Parameter not mathcing integration type";
426  return 0;
427  }
428 
429 }
430 
431 
432 /**
433 */
435 {
436  // returns chi-squared per degree of freedom for the estimate of the integral
438  {
440  assert(ws != 0);
441  return ws->GetWS()->chisq;
442  }
443  else
444  {
445  std::cerr << "Parameter not mathcing integration type";
446  return 0;
447  }
448 }
449 
450 
451 
453 {
454  // internal method to check validity of GSL function pointer
455 
456  if (fFunction && fFunction->GetFunc() ) return true;
457  MATH_ERROR_MSG("GSLMCIntegrator::CheckFunction","Function has not been specified");
458  return false;
459 }
460 
461 const char * GSLMCIntegrator::GetTypeName() const {
462  if (fType == MCIntegration::kVEGAS) return "VEGAS";
463  if (fType == MCIntegration::kMISER) return "MISER";
464  if (fType == MCIntegration::kPLAIN) return "PLAIN";
465  return "UNDEFINED";
466 }
467 
469  IOptions * extraOpts = ExtraOptions();
473  opt.SetNCalls(fCalls);
474  opt.SetWKSize(0);
475  opt.SetIntegrator(GetTypeName() );
476  return opt;
477 }
478 
480  if (!fWorkspace) return 0;
481  return fWorkspace->Options();
482 }
483 
484 
485 } // namespace Math
486 } // namespace ROOT
487 
488 
489 
GSLMCIntegrator(MCIntegration::Type type=MCIntegration::kVEGAS, double absTol=-1, double relTol=-1, unsigned int calls=0)
constructor of GSL MCIntegrator using all the default options
double(* GSLMonteFuncPointer)(double *, size_t, void *)
unsigned int NCalls() const
maximum number of function calls
const double absTol
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
GSLMonteFunctionWrapper * fFunction
double Integral(const GSLMonteFuncPointer &f, unsigned int dim, double *a, double *b, void *p=0)
evaluate the Integral of a function f over the defined hypercube (a,b)
Type
enumeration specifying the integration types.
void SetWKSize(unsigned int size)
set workspace size
void SetFuncPointer(GSLMonteFuncPointer f)
int Status() const
return the Error Status of the last Integral calculation
void SetNCalls(unsigned int calls)
set maximum number of function calls
structures collecting parameters for MISER multidimensional integration
Definition: MCParameters.h:76
TArc * a
Definition: textangle.C:12
void SetFunction(const IMultiGenFunction &f)
method to set the a generic integration function
#define MATH_WARN_MSG(loc, str)
Definition: Error.h:47
double RelTolerance() const
absolute tolerance
structures collecting parameters for VEGAS multidimensional integration FOr implementation of default...
Definition: MCParameters.h:45
void SetRelTolerance(double relTolerance)
set the desired relative Error
double ChiSqr()
returns chi-squared per degree of freedom for the estimate of the integral in the Vegas algorithm ...
GSLRandomEngine Base class for all GSL random engines, normally user instantiate the derived classes ...
virtual ~GSLMCIntegrator()
destructor
GSLMCIntegrationWorkspace * fWorkspace
GSLRngWrapper class to wrap gsl_rng structure.
Definition: GSLRngWrapper.h:25
void SetParameters(const struct VegasParameters &p)
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:50
ROOT::Math::IntegratorMultiDimOptions Options() const
get the option used for the integration
std::string Integrator() const
name of multi-dim integrator
#define MATH_INFO_MSG(loc, str)
Definition: Error.h:44
Numerical multi dimensional integration options.
void SetFunction(const FuncType &f)
Fill gsl function structure from a C++ Function class.
TRandom2 r(17)
Interface (abstract) class for multi numerical integration It must be implemented by the concrete Int...
double AbsTolerance() const
non-static methods for retrivieng options
void SetIntegrator(const char *name)
set multi-dim integrator name
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
MCIntegration::Type fType
void SetParameters(const VegasParameters &p)
set default parameters for VEGAS method
GSLRngWrapper * Engine()
internal method to return the engine Used by class like GSLMCIntegrator to set the engine ...
void SetOptions(const ROOT::Math::IntegratorMultiDimOptions &opt)
set the integration options
ROOT::Math::IOptions * ExtraOptions() const
get the specific options (for Vegas or Miser) in term of string- name
PyObject * fType
void SetType(MCIntegration::Type type)
set integration method
double Result() const
return the type of the integration used
void SetTypeName(const char *typeName)
set integration method using a name instead of an enumeration
double f(double x)
GSLMCIntegrator & operator=(const GSLMCIntegrator &)
static ROOT::Math::IOptions * FindDefault(const char *name)
void SetGenerator(GSLRandomEngine &r)
set random number generator
const char * GetTypeName() const
return the name
int type
Definition: TGX11.cxx:120
double Sigma()
set parameters for PLAIN method
virtual ROOT::Math::IOptions * Options() const =0
retrieve option pointer corresponding to parameters create a new object to be managed by the user ...
Namespace for new Math classes and functions.
void SetMode(MCIntegration::Mode mode)
set integration mode for VEGAS method The possible MODE are : MCIntegration::kIMPORTANCE (default) : ...
Generic interface for defining configuration options of a numerical algorithm.
Definition: IOptions.h:32
wrapper to a multi-dim function withtout derivatives for Monte Carlo multi-dimensional integration al...
virtual MCIntegration::Type Type() const =0
void SetAbsTolerance(double absTolerance)
set the desired absolute Error
IOptions * ExtraOptions() const
return extra options
virtual void Clear()
free the workspace deleting the GSL pointer
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
virtual bool Init(size_t dim)=0
initialize the workspace creating the GSL pointer if it is not there
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
void SetRelTolerance(double tol)
set the relative tolerance
double Error() const
return the estimate of the absolute Error of the last Integral calculation
void SetAbsTolerance(double tol)
non-static methods for setting options