Logo ROOT   6.10/09
Reference Guide
Factory.cxx
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Fri Dec 22 14:43:33 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Implementation file for class MinimizerFactory
12 
13 #include "Math/Factory.h"
14 #include "Math/Error.h"
15 
16 #include "RConfigure.h"
17 
18 #include "Math/Minimizer.h"
19 #include "Math/MinimizerOptions.h"
20 
21 #include "Math/DistSampler.h"
23 
24 // uncomment these if you dont want to use the plugin manager
25 // but you need to link also the needed minimization libraries (e.g Minuit and/or Minuit2)
26 // #define MATH_NO_PLUGIN_MANAGER
27 // #define HAS_MINUIT
28 // #define HAS_MINUIT2
29 
30 #ifndef MATH_NO_PLUGIN_MANAGER
31 // use ROOT Plug-in manager
32 #include "TPluginManager.h"
33 #include "TROOT.h"
34 #include "TVirtualMutex.h"
35 #else
36 // all the minimizer implementation classes
37 //#define HAS_MINUIT2
38 #ifdef HAS_MINUIT2
40 #endif
41 #ifdef HAS_MINUIT
42 #include "TMinuitMinimizer.h"
43 #endif
44 #ifdef R__HAS_MATHMORE
45 #include "Math/GSLMinimizer.h"
46 #include "Math/GSLNLSMinimizer.h"
47 #include "Math/GSLSimAnMinimizer.h"
48 #endif
49 
50 #endif
51 
52 #include <algorithm>
53 #include <cassert>
54 
55 //#define DEBUG
56 #ifdef DEBUG
57 #include <iostream>
58 #endif
59 
60 #ifndef MATH_NO_PLUGIN_MANAGER
61 // use ROOT Plugin Manager to create Minimizer concrete classes
62 
63 ROOT::Math::Minimizer * ROOT::Math::Factory::CreateMinimizer(const std::string & minimizerType,const std::string & algoType)
64 {
65  // create Minimizer using the plug-in manager given the type of Minimizer (MINUIT, MINUIT2, FUMILI, etc..) and
66  // algorithm (MIGRAD, SIMPLEX, etc..)
67 
68  const char * minim = minimizerType.c_str();
69  const char * algo = algoType.c_str();
70 
71  //case of fumili2
72  std::string s1,s2;
73  if (minimizerType == "Fumili2" ) {
74  s1 = "Minuit2";
75  s2 = "fumili";
76  minim = s1.c_str();
77  algo = s2.c_str();
78  }
79  if (minimizerType == "TMinuit") {
80  s1 = "Minuit";
81  minim = s1.c_str();
82  }
83 
84  if (minimizerType.empty() ) minim = ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
85 
87 
88  // create Minimizer using the PM
90  //gDebug = 3;
91  if ((h = gROOT->GetPluginManager()->FindHandler("ROOT::Math::Minimizer",minim ))) {
92  if (h->LoadPlugin() == -1) {
93 #ifdef DEBUG
94  std::cout << "Error Loading ROOT::Math::Minimizer " << minim << std::endl;
95 #endif
96  return 0;
97  }
98 
99  // create plug-in with required algorithm
100  ROOT::Math::Minimizer * min = reinterpret_cast<ROOT::Math::Minimizer *>( h->ExecPlugin(1,algo ) );
101 #ifdef DEBUG
102  if (min != 0)
103  std::cout << "Loaded Minimizer " << minimizerType << " " << algoType << std::endl;
104  else
105  std::cout << "Error creating Minimizer " << minimizerType << " " << algoType << std::endl;
106 #endif
107 
108  return min;
109  }
110  return 0;
111 
112 }
113 
114 #else
115 
116 // use directly classes instances
117 
118 ROOT::Math::Minimizer * ROOT::Math::Factory::CreateMinimizer(const std::string & minimizerType, const std::string & algoType)
119 {
120  // static method to create a minimizer .
121  // not using PM so direct dependency on all libraries (Minuit, Minuit2, MathMore, etc...)
122  // The default is the Minuit2 minimizer or GSL Minimizer
123 
124  // should use enumerations instead of string ?
125 
126  Minimizer * min = 0;
127  std::string algo = algoType;
128 
129 
130 #ifdef HAS_MINUIT2
131  if (minimizerType == "Minuit2")
132  min = new ROOT::Minuit2::Minuit2Minimizer(algoType.c_str());
133  if (minimizerType == "Fumili2")
134  min = new ROOT::Minuit2::Minuit2Minimizer("fumili");
135 #endif
136 
137 #ifdef HAS_MINUIT
138  // use TMinuit
139  if (minimizerType == "Minuit" || minimizerType == "TMinuit")
140  min = new TMinuitMinimizer(algoType.c_str());
141 #endif
142 
143 #ifdef R__HAS_MATHMORE
144  // use GSL minimizer
145  if (minimizerType == "GSL")
146  min = new ROOT::Math::GSLMinimizer(algoType.c_str());
147 
148  else if (minimizerType == "GSL_NLS")
149  min = new ROOT::Math::GSLNLSMinimizer();
150 
151  else if (minimizerType == "GSL_SIMAN")
152  min = new ROOT::Math::GSLSimAnMinimizer();
153 #endif
154 
155 
156 #ifdef HAS_MINUIT2
157  // DEFAULT IS MINUIT2 based on MIGRAD id minuit2 exists
158  else
160 #endif
161 
162  return min;
163 }
164 
165 #endif
166 
168 #ifdef MATH_NO_PLUGIN_MANAGER
169  MATH_ERROR_MSG("Factory::CreateDistSampler","ROOT plug-in manager not available");
170  return 0;
171 #else
172  // create a DistSampler class using the ROOT plug-in manager
173  const char * typeName = type.c_str();
174  if (type.empty() ) typeName = ROOT::Math::DistSamplerOptions::DefaultSampler().c_str();
175 
177 
178  TPluginManager *pm = gROOT->GetPluginManager();
179  assert(pm != 0);
180  TPluginHandler *h = pm->FindHandler("ROOT::Math::DistSampler", typeName );
181  if (h != 0) {
182  if (h->LoadPlugin() == -1) {
183  MATH_ERROR_MSG("Factory::CreateDistSampler","Error loading DistSampler plug-in");
184  return 0;
185  }
186 
187  ROOT::Math::DistSampler * smp = reinterpret_cast<ROOT::Math::DistSampler *>( h->ExecPlugin(0) );
188  assert(smp != 0);
189  return smp;
190  }
191  MATH_ERROR_MSGVAL("Factory::CreateDistSampler","Error finding DistSampler plug-in",typeName);
192  return 0;
193 #endif
194 }
195 
196 
GSLMinimizer class.
Definition: GSLMinimizer.h:86
TH1 * h
Definition: legend2.C:5
TPluginHandler * FindHandler(const char *base, const char *uri=0)
Returns the handler if there exists a handler for the specified URI.
#define gROOT
Definition: TROOT.h:375
Int_t LoadPlugin()
Load the plugin library for this handler.
static ROOT::Math::Minimizer * CreateMinimizer(const std::string &minimizerType="", const std::string &algoType="")
static method to create the corrisponding Minimizer given the string Supported Minimizers types are: ...
Definition: Factory.cxx:63
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:57
#define MATH_ERROR_MSGVAL(loc, str, x)
Definition: Error.h:69
GSLSimAnMinimizer class for minimization using simulated annealing using the algorithm from GSL...
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2, Minuit, GSL, etc..) Plug-in&#39;s exist in ROOT to be able to instantiate the derived classes like ROOT::Math::GSLMinimizer or ROOT::Math::Minuit2Minimizer via the plug-in manager.
Definition: Minimizer.h:78
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:50
static const std::string & DefaultMinimizerType()
Long_t ExecPlugin(int nargs, const T &... params)
This class implements a plugin library manager.
static ROOT::Math::DistSampler * CreateDistSampler(const std::string &samplerType="")
static method to create the distribution sampler class given a string specifying the type Supported s...
Definition: Factory.cxx:167
#define R__LOCKGUARD2(mutex)
Interface class for generic sampling of a distribution, i.e.
Definition: DistSampler.h:57
Minuit2Minimizer class implementing the ROOT::Math::Minimizer interface for Minuit2 minimization algo...
GSLNLSMinimizer class for Non Linear Least Square fitting It Uses the Levemberg-Marquardt algorithm f...
int type
Definition: TGX11.cxx:120
static const std::string & DefaultSampler()
TMinuitMinimizer class: ROOT::Math::Minimizer implementation based on TMinuit.