45 #include "gsl/gsl_monte_vegas.h"
46 #include "gsl/gsl_monte_miser.h"
47 #include "gsl/gsl_monte_plain.h"
73 GSLMCIntegrator::GSLMCIntegrator(MCIntegration::Type type,
double absTol,
double relTol,
unsigned int calls):
76 fCalls((calls > 0) ? calls : IntegratorMultiDimOptions::DefaultNCalls()),
77 fAbsTol((absTol >= 0) ? absTol : IntegratorMultiDimOptions::DefaultAbsTolerance() ),
78 fRelTol((relTol >= 0) ? relTol : IntegratorMultiDimOptions::DefaultRelTolerance() ),
79 fResult(0),fError(0),fStatus(-1),
87 fRng =
new GSLRngWrapper();
91 if (fType == MCIntegration::kVEGAS) {
92 IOptions * opts = IntegratorMultiDimOptions::FindDefault(
"VEGAS");
93 if (opts != 0) SetParameters( VegasParameters(*opts) );
95 else if (fType == MCIntegration::kMISER) {
96 IOptions * opts = IntegratorMultiDimOptions::FindDefault(
"MISER");
97 if (opts != 0) SetParameters( MiserParameters(*opts) );
102 GSLMCIntegrator::GSLMCIntegrator(
const char * type,
double absTol,
double relTol,
unsigned int calls):
103 fType(MCIntegration::kDEFAULT),
108 fResult(0),fError(0),fStatus(-1),
117 fRng =
new GSLRngWrapper();
120 if (fType == MCIntegration::kVEGAS) {
121 IOptions * opts = IntegratorMultiDimOptions::FindDefault(
"VEGAS");
122 if (opts != 0) SetParameters( VegasParameters(*opts) );
124 else if (fType == MCIntegration::kMISER) {
125 IOptions * opts = IntegratorMultiDimOptions::FindDefault(
"MISER");
126 if (opts != 0) SetParameters( MiserParameters(*opts) );
133 GSLMCIntegrator::~GSLMCIntegrator()
136 if (fWorkspace)
delete fWorkspace;
137 if (fRng != 0 && !fExtGen)
delete fRng;
138 if (fFunction != 0)
delete fFunction;
147 GSLMCIntegrator::GSLMCIntegrator(
const GSLMCIntegrator &) :
148 VirtualIntegratorMultiDim()
151 GSLMCIntegrator & GSLMCIntegrator::operator=(
const GSLMCIntegrator &) {
return *
this; }
157 void GSLMCIntegrator::SetFunction(
const IMultiGenFunction &f)
160 if(fFunction == 0) fFunction =
new GSLMonteFunctionWrapper();
161 fFunction->SetFunction(f);
165 void GSLMCIntegrator::SetFunction( GSLMonteFuncPointer f,
unsigned int dim,
void * p )
168 if(fFunction == 0) fFunction =
new GSLMonteFunctionWrapper();
169 fFunction->SetFuncPointer( f );
170 fFunction->SetParams ( p );
176 double GSLMCIntegrator::Integral(
const double* a,
const double* b)
180 gsl_rng* fr = fRng->Rng();
182 if (!CheckFunction())
return 0;
188 if ( fType == MCIntegration::kVEGAS)
190 GSLVegasIntegrationWorkspace * ws =
dynamic_cast<GSLVegasIntegrationWorkspace *
>(fWorkspace);
192 fStatus = gsl_monte_vegas_integrate( fFunction->GetFunc(), (
double *) a, (
double*) b , fDim, fCalls, fr, ws->GetWS(), &fResult, &fError);
194 else if (fType == MCIntegration::kMISER)
196 GSLMiserIntegrationWorkspace * ws =
dynamic_cast<GSLMiserIntegrationWorkspace *
>(fWorkspace);
198 fStatus = gsl_monte_miser_integrate( fFunction->GetFunc(), (
double *) a, (
double *) b , fDim, fCalls, fr, ws->GetWS(), &fResult, &fError);
200 else if (fType == MCIntegration::kPLAIN)
202 GSLPlainIntegrationWorkspace * ws =
dynamic_cast<GSLPlainIntegrationWorkspace *
>(fWorkspace);
204 fStatus = gsl_monte_plain_integrate( fFunction->GetFunc(), (
double *) a, (
double *) b , fDim, fCalls, fr, ws->GetWS(), &fResult, &fError);
213 std::cerr <<
"GSLIntegrator - Error: Unknown integration type" << std::endl;
214 throw std::exception();
222 double GSLMCIntegrator::Integral(
const GSLMonteFuncPointer & f,
unsigned int dim,
double* a,
double* b,
void * p )
225 SetFunction(f,dim,p);
226 return Integral(a,b);
242 double GSLMCIntegrator::Result()
const {
return fResult; }
247 double GSLMCIntegrator::Error()
const {
return fError; }
252 int GSLMCIntegrator::Status()
const {
return fStatus; }
260 void GSLMCIntegrator::SetRelTolerance(
double relTol){ this->fRelTol = relTol; }
265 void GSLMCIntegrator::SetAbsTolerance(
double absTol){ this->fAbsTol = absTol; }
267 void GSLMCIntegrator::SetGenerator(GSLRandomEngine & r){
269 if (fRng && !fExtGen)
delete fRng;
274 void GSLMCIntegrator::SetType (MCIntegration::Type type)
278 if (fWorkspace != 0) {
279 if (type == fWorkspace->Type() )
return;
284 if (type == MCIntegration::kPLAIN) {
285 fWorkspace =
new GSLPlainIntegrationWorkspace();
287 else if (type == MCIntegration::kMISER) {
288 fWorkspace =
new GSLMiserIntegrationWorkspace();
292 if (type != MCIntegration::kVEGAS) {
293 MATH_WARN_MSG(
"GSLMCIntegration",
"Invalid integration type : use Vegas as default");
294 fType = MCIntegration::kVEGAS;
296 fWorkspace =
new GSLVegasIntegrationWorkspace();
300 void GSLMCIntegrator::SetTypeName(
const char * type)
303 std::string typeName = (type!=0) ? type :
"VEGAS";
304 if (type == 0) MATH_INFO_MSG(
"GSLMCIntegration::SetTypeName",
"use default Vegas integrator method");
305 std::transform(typeName.begin(), typeName.end(), typeName.begin(), (int(*)(int)) toupper );
307 MCIntegration::Type integType = MCIntegration::kVEGAS;
309 if (typeName ==
"PLAIN") {
310 integType = MCIntegration::kPLAIN;
312 else if (typeName ==
"MISER") {
313 integType = MCIntegration::kMISER;
315 else if (typeName !=
"VEGAS") {
316 MATH_WARN_MSG(
"GSLMCIntegration::SetTypeName",
"Invalid integration type : use Vegas as default");
325 void GSLMCIntegrator::SetMode(MCIntegration::Mode mode)
328 if(fType == ROOT::Math::MCIntegration::kVEGAS)
330 GSLVegasIntegrationWorkspace * ws =
dynamic_cast<GSLVegasIntegrationWorkspace *
>(fWorkspace);
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;
337 else std::cerr <<
"Mode not matching integration type";
340 void GSLMCIntegrator::SetOptions(
const ROOT::Math::IntegratorMultiDimOptions & opt)
343 SetTypeName(opt.Integrator().c_str() );
344 SetAbsTolerance( opt.AbsTolerance() );
345 SetRelTolerance( opt.RelTolerance() );
346 fCalls = opt.NCalls();
351 ROOT::Math::IOptions * extraOpt = opt.ExtraOptions();
353 if (fType == MCIntegration::kVEGAS ) {
354 VegasParameters p(*extraOpt);
357 else if (fType == MCIntegration::kMISER ) {
358 MiserParameters p(fDim);
363 MATH_WARN_MSG(
"GSLMCIntegrator::SetOptions",
"Invalid options set for the chosen integration type - ignore them");
369 void GSLMCIntegrator::SetParameters(
const VegasParameters &p)
372 if (fType == MCIntegration::kVEGAS)
374 GSLVegasIntegrationWorkspace * ws =
dynamic_cast<GSLVegasIntegrationWorkspace *
>(fWorkspace);
376 ws->SetParameters(p);
379 MATH_ERROR_MSG(
"GSLIntegrator::SetParameters",
" Parameters not matching integration type");
382 void GSLMCIntegrator::SetParameters(
const MiserParameters &p)
385 if (fType == MCIntegration::kMISER)
387 GSLMiserIntegrationWorkspace * ws =
dynamic_cast<GSLMiserIntegrationWorkspace *
>(fWorkspace);
389 ws->SetParameters(p);
392 MATH_ERROR_MSG(
"GSLIntegrator::SetParameters",
" Parameters not matching integration type");
396 void GSLMCIntegrator::DoInitialize ( )
400 if (fWorkspace == 0)
return;
401 if (fDim == fWorkspace->NDim() && fType == fWorkspace->Type() )
407 fWorkspace->Init(fDim);
414 double GSLMCIntegrator::Sigma()
417 if(fType == MCIntegration::kVEGAS)
419 GSLVegasIntegrationWorkspace * ws =
dynamic_cast<GSLVegasIntegrationWorkspace *
>(fWorkspace);
421 return ws->GetWS()->sigma;
425 std::cerr <<
"Parameter not mathcing integration type";
434 double GSLMCIntegrator::ChiSqr()
437 if(fType == MCIntegration::kVEGAS)
439 GSLVegasIntegrationWorkspace * ws =
dynamic_cast<GSLVegasIntegrationWorkspace *
>(fWorkspace);
441 return ws->GetWS()->chisq;
445 std::cerr <<
"Parameter not mathcing integration type";
452 bool GSLMCIntegrator::CheckFunction()
456 if (fFunction && fFunction->GetFunc() )
return true;
457 MATH_ERROR_MSG(
"GSLMCIntegrator::CheckFunction",
"Function has not been specified");
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";
468 ROOT::Math::IntegratorMultiDimOptions GSLMCIntegrator::Options()
const {
469 IOptions * extraOpts = ExtraOptions();
470 ROOT::Math::IntegratorMultiDimOptions opt(extraOpts);
471 opt.SetAbsTolerance(fAbsTol);
472 opt.SetRelTolerance(fRelTol);
473 opt.SetNCalls(fCalls);
475 opt.SetIntegrator(GetTypeName() );
479 ROOT::Math::IOptions * GSLMCIntegrator::ExtraOptions()
const {
480 if (!fWorkspace)
return 0;
481 return fWorkspace->Options();