Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooNumIntFactory.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17/**
18\file RooNumIntFactory.cxx
19\class RooNumIntFactory
20\ingroup Roofitcore
21
22%Factory to instantiate numeric integrators
23from a given function binding and a given configuration. The factory
24searches for a numeric integrator registered with the factory that
25has the ability to perform the numeric integration. The choice of
26method may depend on the number of dimensions integrated,
27the nature of the integration limits (closed or open ended) and
28the preference of the caller as encoded in the configuration object.
29**/
30
31#include "TClass.h"
32#include "TSystem.h"
33#include "Riostream.h"
34
35#include "RooNumIntFactory.h"
36#include "RooArgSet.h"
37#include "RooAbsFunc.h"
38#include "RooNumIntConfig.h"
39#include "RooNumber.h"
40
42#include "RooBinIntegrator.h"
44#include "RooMCIntegrator.h"
46
47#include "RooMsgService.h"
48
50
51////////////////////////////////////////////////////////////////////////////////
52/// Register all known integrators by calling
53/// their static registration functions
54void RooNumIntFactory::init() {
59 // GSL integrator is now in RooFitMore and it register itself
60 //RooAdaptiveGaussKronrodIntegrator1D::registerIntegrator(*this) ;
61 //RooGaussKronrodIntegrator1D::registerIntegrator(*this) ;
63
64 RooNumIntConfig::defaultConfig().method1D().setLabel("RooIntegrator1D") ;
65 RooNumIntConfig::defaultConfig().method1DOpen().setLabel("RooImproperIntegrator1D") ;
66 RooNumIntConfig::defaultConfig().method2D().setLabel("RooAdaptiveIntegratorND") ;
67 RooNumIntConfig::defaultConfig().methodND().setLabel("RooAdaptiveIntegratorND") ;
68
69 //if GSL is available load (and register GSL integrator)
70#ifdef R__HAS_MATHMORE
71 int iret = gSystem->Load("libRooFitMore");
72 if (iret < 0) {
73 oocoutE(nullptr, Integration) << " RooNumIntFactory::Init : libRooFitMore cannot be loaded. GSL integrators will not beavailable ! " << std::endl;
74 }
75#endif
76}
77
78
79////////////////////////////////////////////////////////////////////////////////
80/// Static method returning reference to singleton instance of factory
81
83{
84 static std::unique_ptr<RooNumIntFactory> instance;
85
86 if (!instance) {
87 // This is needed to break a deadlock. During init(),
88 // other functions may call back to this one here. So we need to construct first,
89 // and ensure that we can return an instance while we are waiting for init()
90 // to finish.
91 instance.reset(new RooNumIntFactory);
92 instance->init();
93 }
94
95 return *instance;
96}
97
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Method accepting registration of a prototype numeric integrator along with a RooArgSet of its
102/// default configuration options and an optional list of names of other numeric integrators
103/// on which this integrator depends. Returns true if integrator was previously registered
104
105bool RooNumIntFactory::registerPlugin(std::string const &name, Creator const &creator, const RooArgSet &defConfig,
106 bool canIntegrate1D, bool canIntegrate2D, bool canIntegrateND,
107 bool canIntegrateOpenEnded, const char *depName)
108{
109 if (_map.find(name) != _map.end()) {
110 //cout << "RooNumIntFactory::storeIntegrator() ERROR: integrator '" << name << "' already registered" << std::endl ;
111 return true ;
112 }
113
114 // Add to factory
115 auto& info = _map[name];
116 info.creator = creator;
117 info.canIntegrate1D = canIntegrate1D;
118 info.canIntegrate2D = canIntegrate2D;
119 info.canIntegrateND = canIntegrateND;
120 info.canIntegrateOpenEnded = canIntegrateOpenEnded;
121 info.depName = depName;
122
123 // Add default config to master config
124 RooNumIntConfig::defaultConfig().addConfigSection(name,defConfig, canIntegrate1D, canIntegrate2D, canIntegrateND, canIntegrateOpenEnded) ;
125
126 return false ;
127}
128
129std::string RooNumIntFactory::getIntegratorName(RooAbsFunc& func, const RooNumIntConfig& config, int ndimPreset, bool isBinned) const
130{
131 // First determine dimensionality and domain of integrand
132 int ndim = ndimPreset>0 ? ndimPreset : ((int)func.getDimension()) ;
133
134 bool openEnded = false ;
135 int i ;
136 for (i=0 ; i<ndim ; i++) {
139 openEnded = true ;
140 }
141 }
142
143 // Find method defined configuration
144 std::string method ;
145 switch(ndim) {
146 case 1:
147 method = openEnded ? config.method1DOpen().getCurrentLabel() : config.method1D().getCurrentLabel() ;
148 break ;
149
150 case 2:
151 method = openEnded ? config.method2DOpen().getCurrentLabel() : config.method2D().getCurrentLabel() ;
152 break ;
153
154 default:
155 method = openEnded ? config.methodNDOpen().getCurrentLabel() : config.methodND().getCurrentLabel() ;
156 break ;
157 }
158
159 // If distribution is binned and not open-ended override with bin integrator
160 if (isBinned & !openEnded) {
161 method = "RooBinIntegrator" ;
162 }
163
164 // Check that a method was defined for this case
165 if (method == "N/A") {
166 oocoutE(nullptr,Integration) << "RooNumIntFactory: No integration method has been defined for "
167 << (openEnded?"an open ended ":"a ") << ndim << "-dimensional integral" << std::endl;
168 return {};
169 }
170
171 return method;
172}
173
174////////////////////////////////////////////////////////////////////////////////
175/// Construct a numeric integrator instance that operates on function 'func' and is configured
176/// with 'config'. If ndimPreset is greater than zero that number is taken as the dimensionality
177/// of the integration, otherwise it is queried from 'func'. This function iterators over list
178/// of available prototype integrators and returns an clone attached to the given function of
179/// the first class that matches the specifications of the requested integration considering
180/// the number of dimensions, the nature of the limits (open ended vs closed) and the user
181/// preference stated in 'config'
182
183std::unique_ptr<RooAbsIntegrator> RooNumIntFactory::createIntegrator(RooAbsFunc& func, const RooNumIntConfig& config, int ndimPreset, bool isBinned) const
184{
185 std::string method = getIntegratorName(func, config, ndimPreset, isBinned);
186
187 if(method.empty()) {
188 return nullptr;
189 }
190
191 // Retrieve proto integrator and return clone configured for the requested integration task
192 std::unique_ptr<RooAbsIntegrator> engine = getPluginInfo(method)->creator(func,config) ;
193 if (config.printEvalCounter()) {
194 engine->setPrintEvalCounter(true) ;
195 }
196 return engine ;
197}
#define oocoutE(o, a)
#define ClassImp(name)
Definition Rtypes.h:382
char name[80]
Definition TGX11.cxx:110
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
virtual const char * getCurrentLabel() const
Return label string of current state.
Abstract interface for evaluating a real-valued function of one real variable and performing numerica...
Definition RooAbsFunc.h:27
virtual double getMaxLimit(UInt_t dimension) const =0
virtual double getMinLimit(UInt_t dimension) const =0
UInt_t getDimension() const
Definition RooAbsFunc.h:33
static void registerIntegrator(RooNumIntFactory &fact)
Register RooAdaptiveIntegratorND, its parameters, dependencies and capabilities with RooNumIntFactory...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
static void registerIntegrator(RooNumIntFactory &fact)
Register RooBinIntegrator, is parameters and capabilities with RooNumIntFactory.
bool setLabel(const char *label, bool printError=true) override
Set value by specifying the name of the desired state.
static void registerIntegrator(RooNumIntFactory &fact)
Register RooImproperIntegrator1D, its parameters and capabilities with RooNumIntFactory.
static void registerIntegrator(RooNumIntFactory &fact)
This function registers class RooMCIntegrator, its configuration options and its capabilities with Ro...
Holds the configuration parameters of the various numeric integrators used by RooRealIntegral.
RooCategory & method2DOpen()
RooCategory & method2D()
RooCategory & methodND()
RooCategory & methodNDOpen()
bool addConfigSection(std::string const &name, const RooArgSet &inDefaultConfig, bool canIntegrate1D, bool canIntegrate2D, bool canIntegrateND, bool canIntegrateOpenEnded)
Add a configuration section for a particular integrator.
RooCategory & method1D()
RooCategory & method1DOpen()
bool printEvalCounter() const
static RooNumIntConfig & defaultConfig()
Return reference to instance of default numeric integrator configuration object.
Factory to instantiate numeric integrators from a given function binding and a given configuration.
PluginInfo const * getPluginInfo(std::string const &name) const
std::unique_ptr< RooAbsIntegrator > createIntegrator(RooAbsFunc &func, const RooNumIntConfig &config, int ndim=0, bool isBinned=false) const
Construct a numeric integrator instance that operates on function 'func' and is configured with 'conf...
void init()
Register all known integrators by calling their static registration functions.
std::string getIntegratorName(RooAbsFunc &func, const RooNumIntConfig &config, int ndim=0, bool isBinned=false) const
std::function< std::unique_ptr< RooAbsIntegrator >(RooAbsFunc const &function, const RooNumIntConfig &config)> Creator
std::map< std::string, PluginInfo > _map
static RooNumIntFactory & instance()
Static method returning reference to singleton instance of factory.
bool registerPlugin(std::string const &name, Creator const &creator, const RooArgSet &defConfig, bool canIntegrate1D, bool canIntegrate2D, bool canIntegrateND, bool canIntegrateOpenEnded, const char *depName="")
Method accepting registration of a prototype numeric integrator along with a RooArgSet of its default...
static constexpr int isInfinite(double x)
Return true if x is infinite by RooNumber internal specification.
Definition RooNumber.h:27
static void registerIntegrator(RooNumIntFactory &fact)
Register integrator plugins, their parameters and capabilities with RooNumIntFactory.
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition TSystem.cxx:1857