ROOT  6.06/09
Reference Guide
RooNumGenFactory.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 //
19 // BEGIN_HTML
20 // RooNumGenFactory is a factory to instantiate numeric integrators
21 // from a given function binding and a given configuration. The factory
22 // searches for a numeric integrator registered with the factory that
23 // has the ability to perform the numeric integration. The choice of
24 // method may depend on the number of dimensions integrated,
25 // the nature of the integration limits (closed or open ended) and
26 // the preference of the caller as encoded in the configuration object.
27 // END_HTML
28 //
29 
30 #include "TClass.h"
31 #include "Riostream.h"
32 
33 #include "RooFit.h"
34 
35 #include "RooNumGenFactory.h"
36 #include "RooArgSet.h"
37 #include "RooAbsFunc.h"
38 #include "RooNumGenConfig.h"
39 #include "RooNumber.h"
40 
41 #include "RooAcceptReject.h"
42 #include "RooFoamGenerator.h"
43 #include "RooSentinel.h"
44 
45 
46 #include "RooMsgService.h"
47 
48 using namespace std ;
49 
51 ;
52 
53 RooNumGenFactory* RooNumGenFactory::_instance = 0 ;
54 
55 
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Constructor. Register all known integrators by calling
59 /// their static registration functions
60 
62 {
63  _instance = this ;
64 
67 
68  // Prepare default
73 
78 
83 
84 }
85 
86 
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// Destructor
90 
92 {
93  std::map<std::string,RooAbsNumGenerator*>::iterator iter = _map.begin() ;
94  while (iter != _map.end()) {
95  delete iter->second ;
96  ++iter ;
97  }
98 }
99 
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Copy constructor
103 
105 {
106 }
107 
108 
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Static method returning reference to singleton instance of factory
112 
114 {
115  if (_instance==0) {
116  new RooNumGenFactory ;
118  }
119  return *_instance ;
120 }
121 
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Cleanup routine called by atexit() handler installed by RooSentinel
125 
127 {
128  if (_instance) {
129  delete _instance ;
130  _instance = 0 ;
131  }
132 }
133 
134 
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Method accepting registration of a prototype numeric integrator along with a RooArgSet of its
138 /// default configuration options and an optional list of names of other numeric integrators
139 /// on which this integrator depends. Returns true if integrator was previously registered
140 
142 {
143  TString name = proto->IsA()->GetName() ;
144 
145  if (getProtoSampler(name)) {
146  //cout << "RooNumGenFactory::storeSampler() ERROR: integrator '" << name << "' already registered" << endl ;
147  return kTRUE ;
148  }
149 
150  // Add to factory
151  _map[name.Data()] = proto ;
152 
153  // Add default config to master config
155 
156  return kFALSE ;
157 }
158 
159 
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Return prototype integrator with given (class) name
163 
165 {
166  if (_map.count(name)==0) {
167  return 0 ;
168  }
169 
170  return _map[name] ;
171 }
172 
173 
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 /// Construct a numeric integrator instance that operates on function 'func' and is configured
177 /// with 'config'. If ndimPreset is greater than zero that number is taken as the dimensionality
178 /// of the integration, otherwise it is queried from 'func'. This function iterators over list
179 /// of available prototype integrators and returns an clone attached to the given function of
180 /// the first class that matches the specifications of the requested integration considering
181 /// the number of dimensions, the nature of the limits (open ended vs closed) and the user
182 /// preference stated in 'config'
183 
185 {
186  // Find method defined configuration
187  Int_t ndim = genVars.getSize() ;
188  Bool_t cond = (condVars.getSize() > 0) ? kTRUE : kFALSE ;
189 
190  Bool_t hasCat(kFALSE) ;
191  TIterator* iter = genVars.createIterator() ;
192  RooAbsArg* arg ;
193  while ((arg=(RooAbsArg*)iter->Next())) {
194  if (arg->IsA()==RooCategory::Class()) {
195  hasCat=kTRUE ;
196  break ;
197  }
198  }
199  delete iter ;
200 
201 
202  TString method ;
203  switch(ndim) {
204  case 1:
205  method = config.method1D(cond,hasCat).getLabel() ;
206  break ;
207 
208  case 2:
209  method = config.method2D(cond,hasCat).getLabel() ;
210  break ;
211 
212  default:
213  method = config.methodND(cond,hasCat).getLabel() ;
214  break ;
215  }
216 
217  // Check that a method was defined for this case
218  if (!method.CompareTo("N/A")) {
219  oocoutE((TObject*)0,Integration) << "RooNumGenFactory::createSampler: No sampler method has been defined for "
220  << (cond?"a conditional ":"a ") << ndim << "-dimensional p.d.f" << endl ;
221  return 0 ;
222  }
223 
224  // Retrieve proto integrator and return clone configured for the requested integration task
225  const RooAbsNumGenerator* proto = getProtoSampler(method) ;
226  RooAbsNumGenerator* engine = proto->clone(func,genVars,condVars,config,verbose,maxFuncVal) ;
227  return engine ;
228 }
RooCategory & methodND(Bool_t cond, Bool_t cat)
const RooAbsNumGenerator * getProtoSampler(const char *name)
Return prototype integrator with given (class) name.
virtual RooAbsNumGenerator * clone(const RooAbsReal &, const RooArgSet &genVars, const RooArgSet &condVars, const RooNumGenConfig &config, Bool_t verbose=kFALSE, const RooAbsReal *maxFuncVal=0) const =0
virtual ~RooNumGenFactory()
Destructor.
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
RooAbsNumGenerator * createSampler(RooAbsReal &func, const RooArgSet &genVars, const RooArgSet &condVars, const RooNumGenConfig &config, Bool_t verbose=kFALSE, RooAbsReal *maxFuncVal=0)
Construct a numeric integrator instance that operates on function 'func' and is configured with 'conf...
static void cleanup()
Cleanup routine called by atexit() handler installed by RooSentinel.
Iterator abstract base class.
Definition: TIterator.h:32
virtual Bool_t setLabel(const char *label, Bool_t printError=kTRUE)
Set value by specifying the name of the desired state If printError is set, a message will be printed...
const char * Data() const
Definition: TString.h:349
ClassImp(RooNumGenFactory)
void Class()
Definition: Class.C:29
#define oocoutE(o, a)
Definition: RooMsgService.h:48
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
RooNumGenFactory()
Constructor.
TIterator * createIterator(Bool_t dir=kIterForward) const
static RooNumGenConfig & defaultConfig()
Return reference to instance of default numeric integrator configuration object.
bool verbose
Bool_t storeProtoSampler(RooAbsNumGenerator *proto, const RooArgSet &defConfig)
Method accepting registration of a prototype numeric integrator along with a RooArgSet of its default...
static void registerSampler(RooNumGenFactory &fact)
Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
std::map< Int_t, const RooCatType * > _map
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:70
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Bool_t addConfigSection(const RooAbsNumGenerator *proto, const RooArgSet &defaultConfig)
Add a configuration section for a particular integrator.
std::map< std::string, RooAbsNumGenerator * > _map
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
virtual TObject * Next()=0
static RooNumGenFactory & instance()
Static method returning reference to singleton instance of factory.
static void registerSampler(RooNumGenFactory &fact)
Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory.
Int_t getSize() const
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: Rtypes.h:91
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:385
static RooNumGenFactory * _instance
RooCategory & method2D(Bool_t cond, Bool_t cat)
virtual const char * getLabel() const
Return label string of current state.
Definition: RooCategory.h:40
RooCategory & method1D(Bool_t cond, Bool_t cat)