ROOT logo
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id: RooNumIntFactory.cxx 28259 2009-04-16 16:21:16Z wouter $
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *                                                                           *
 * Copyright (c) 2000-2005, Regents of the University of California          *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 * Redistribution and use in source and binary forms,                        *
 * with or without modification, are permitted according to the terms        *
 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
 *****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// RooNumIntFactory is a factory to instantiate numeric integrators
// from a given function binding and a given configuration. The factory
// searches for a numeric integrator registered with the factory that
// has the ability to perform the numeric integration. The choice of
// method may depend on the number of dimensions integrated,
// the nature of the integration limits (closed or open ended) and
// the preference of the caller as encoded in the configuration object.
// END_HTML
//

#include "TClass.h"
#include "Riostream.h"

#include "RooFit.h"

#include "RooNumIntFactory.h"
#include "RooArgSet.h"
#include "RooAbsFunc.h"
#include "RooNumIntConfig.h"
#include "RooNumber.h"

#include "RooIntegrator1D.h"
#include "RooIntegrator2D.h"
#include "RooSegmentedIntegrator1D.h"
#include "RooSegmentedIntegrator2D.h"
#include "RooImproperIntegrator1D.h"
#include "RooMCIntegrator.h"
#include "RooGaussKronrodIntegrator1D.h"
#include "RooAdaptiveGaussKronrodIntegrator1D.h"
#include "RooAdaptiveIntegratorND.h"
#include "RooSentinel.h"

#include "RooMsgService.h"

using namespace std ;

ClassImp(RooNumIntFactory)
;

RooNumIntFactory* RooNumIntFactory::_instance = 0 ;



//_____________________________________________________________________________
RooNumIntFactory::RooNumIntFactory()
{
  // Constructor. Register all known integrators by calling
  // their static registration functions

  _instance = this ;

  RooIntegrator1D::registerIntegrator(*this) ;
  RooIntegrator2D::registerIntegrator(*this) ;
  RooSegmentedIntegrator1D::registerIntegrator(*this) ;
  RooSegmentedIntegrator2D::registerIntegrator(*this) ;
  RooImproperIntegrator1D::registerIntegrator(*this) ;
  RooMCIntegrator::registerIntegrator(*this) ;
  RooAdaptiveGaussKronrodIntegrator1D::registerIntegrator(*this) ;
  RooGaussKronrodIntegrator1D::registerIntegrator(*this) ;  
  RooAdaptiveIntegratorND::registerIntegrator(*this) ;

  RooNumIntConfig::defaultConfig().method1D().setLabel("RooIntegrator1D") ;
  RooNumIntConfig::defaultConfig().method1DOpen().setLabel("RooImproperIntegrator1D") ;
  RooNumIntConfig::defaultConfig().method2D().setLabel("RooAdaptiveIntegratorND") ;
  RooNumIntConfig::defaultConfig().methodND().setLabel("RooAdaptiveIntegratorND") ;
  
}



//_____________________________________________________________________________
RooNumIntFactory::~RooNumIntFactory()
{
  // Destructor

  std::map<std::string,pair<RooAbsIntegrator*,std::string> >::iterator iter = _map.begin() ;
  while (iter != _map.end()) {
    delete iter->second.first ;
    ++iter ;
  }  
}


//_____________________________________________________________________________
RooNumIntFactory::RooNumIntFactory(const RooNumIntFactory& other) : TObject(other)
{
  // Copy constructor
}



//_____________________________________________________________________________
RooNumIntFactory& RooNumIntFactory::instance()
{
  // Static method returning reference to singleton instance of factory

  if (_instance==0) {
    new RooNumIntFactory ;
    RooSentinel::activate() ;
  } 
  return *_instance ;
}


//_____________________________________________________________________________
void RooNumIntFactory::cleanup()
{
  // Cleanup routine called by atexit() handler installed by RooSentinel

  if (_instance) {
    delete _instance ;
    _instance = 0 ;
  }
}



//_____________________________________________________________________________
Bool_t RooNumIntFactory::storeProtoIntegrator(RooAbsIntegrator* proto, const RooArgSet& defConfig, const char* depName) 
{
  // Method accepting registration of a prototype numeric integrator along with a RooArgSet of its
  // default configuration options and an optional list of names of other numeric integrators
  // on which this integrator depends. Returns true if integrator was previously registered

  TString name = proto->IsA()->GetName() ;

  if (getProtoIntegrator(name)) {
    //cout << "RooNumIntFactory::storeIntegrator() ERROR: integrator '" << name << "' already registered" << endl ;
    return kTRUE ;
  }

  // Add to factory 
  _map[name.Data()] = make_pair<RooAbsIntegrator*,std::string>(proto,depName) ;

  // Add default config to master config
  RooNumIntConfig::defaultConfig().addConfigSection(proto,defConfig) ;
  
  return kFALSE ;
}



//_____________________________________________________________________________
const RooAbsIntegrator* RooNumIntFactory::getProtoIntegrator(const char* name) 
{
  // Return prototype integrator with given (class) name

  if (_map.count(name)==0) {
    return 0 ;
  } 
  
  return _map[name].first ;
}



//_____________________________________________________________________________
const char* RooNumIntFactory::getDepIntegratorName(const char* name) 
{
  // Get list of class names of integrators needed by integrator named 'name'
  if (_map.count(name)==0) {
    return 0 ;
  }

  return _map[name].second.c_str() ;
}



//_____________________________________________________________________________
RooAbsIntegrator* RooNumIntFactory::createIntegrator(RooAbsFunc& func, const RooNumIntConfig& config, Int_t ndimPreset) 
{
  // Construct a numeric integrator instance that operates on function 'func' and is configured
  // with 'config'. If ndimPreset is greater than zero that number is taken as the dimensionality
  // of the integration, otherwise it is queried from 'func'. This function iterators over list
  // of available prototype integrators and returns an clone attached to the given function of
  // the first class that matches the specifications of the requested integration considering
  // the number of dimensions, the nature of the limits (open ended vs closed) and the user
  // preference stated in 'config'

  // First determine dimensionality and domain of integrand  
  Int_t ndim = ndimPreset>0 ? ndimPreset : func.getDimension() ;

  Bool_t openEnded = kFALSE ;
  Int_t i ;
  for (i=0 ; i<ndim ; i++) {
    if(RooNumber::isInfinite(func.getMinLimit(i)) ||
       RooNumber::isInfinite(func.getMaxLimit(i))) {
      openEnded = kTRUE ;
    }
  }

  // Find method defined configuration
  TString method ;
  switch(ndim) {
  case 1:
    method = openEnded ? config.method1DOpen().getLabel() : config.method1D().getLabel() ;
    break ;

  case 2:
    method = openEnded ? config.method2DOpen().getLabel() : config.method2D().getLabel() ;
    break ;

  default:
    method = openEnded ? config.methodNDOpen().getLabel() : config.methodND().getLabel() ;
    break ;
  }

  // Check that a method was defined for this case
  if (!method.CompareTo("N/A")) {
    oocoutE((TObject*)0,Integration) << "RooNumIntFactory::createIntegrator: No integration method has been defined for " 
				     << (openEnded?"an open ended ":"a ") << ndim << "-dimensional integral" << endl ;
    return 0 ;    
  }

  // Retrieve proto integrator and return clone configured for the requested integration task
  const RooAbsIntegrator* proto = getProtoIntegrator(method) ;  
  RooAbsIntegrator* engine =  proto->clone(func,config) ;
  if (config.printEvalCounter()) {
    engine->setPrintEvalCounter(kTRUE) ;
  }
  return engine ;
}
 RooNumIntFactory.cxx:1
 RooNumIntFactory.cxx:2
 RooNumIntFactory.cxx:3
 RooNumIntFactory.cxx:4
 RooNumIntFactory.cxx:5
 RooNumIntFactory.cxx:6
 RooNumIntFactory.cxx:7
 RooNumIntFactory.cxx:8
 RooNumIntFactory.cxx:9
 RooNumIntFactory.cxx:10
 RooNumIntFactory.cxx:11
 RooNumIntFactory.cxx:12
 RooNumIntFactory.cxx:13
 RooNumIntFactory.cxx:14
 RooNumIntFactory.cxx:15
 RooNumIntFactory.cxx:16
 RooNumIntFactory.cxx:17
 RooNumIntFactory.cxx:18
 RooNumIntFactory.cxx:19
 RooNumIntFactory.cxx:20
 RooNumIntFactory.cxx:21
 RooNumIntFactory.cxx:22
 RooNumIntFactory.cxx:23
 RooNumIntFactory.cxx:24
 RooNumIntFactory.cxx:25
 RooNumIntFactory.cxx:26
 RooNumIntFactory.cxx:27
 RooNumIntFactory.cxx:28
 RooNumIntFactory.cxx:29
 RooNumIntFactory.cxx:30
 RooNumIntFactory.cxx:31
 RooNumIntFactory.cxx:32
 RooNumIntFactory.cxx:33
 RooNumIntFactory.cxx:34
 RooNumIntFactory.cxx:35
 RooNumIntFactory.cxx:36
 RooNumIntFactory.cxx:37
 RooNumIntFactory.cxx:38
 RooNumIntFactory.cxx:39
 RooNumIntFactory.cxx:40
 RooNumIntFactory.cxx:41
 RooNumIntFactory.cxx:42
 RooNumIntFactory.cxx:43
 RooNumIntFactory.cxx:44
 RooNumIntFactory.cxx:45
 RooNumIntFactory.cxx:46
 RooNumIntFactory.cxx:47
 RooNumIntFactory.cxx:48
 RooNumIntFactory.cxx:49
 RooNumIntFactory.cxx:50
 RooNumIntFactory.cxx:51
 RooNumIntFactory.cxx:52
 RooNumIntFactory.cxx:53
 RooNumIntFactory.cxx:54
 RooNumIntFactory.cxx:55
 RooNumIntFactory.cxx:56
 RooNumIntFactory.cxx:57
 RooNumIntFactory.cxx:58
 RooNumIntFactory.cxx:59
 RooNumIntFactory.cxx:60
 RooNumIntFactory.cxx:61
 RooNumIntFactory.cxx:62
 RooNumIntFactory.cxx:63
 RooNumIntFactory.cxx:64
 RooNumIntFactory.cxx:65
 RooNumIntFactory.cxx:66
 RooNumIntFactory.cxx:67
 RooNumIntFactory.cxx:68
 RooNumIntFactory.cxx:69
 RooNumIntFactory.cxx:70
 RooNumIntFactory.cxx:71
 RooNumIntFactory.cxx:72
 RooNumIntFactory.cxx:73
 RooNumIntFactory.cxx:74
 RooNumIntFactory.cxx:75
 RooNumIntFactory.cxx:76
 RooNumIntFactory.cxx:77
 RooNumIntFactory.cxx:78
 RooNumIntFactory.cxx:79
 RooNumIntFactory.cxx:80
 RooNumIntFactory.cxx:81
 RooNumIntFactory.cxx:82
 RooNumIntFactory.cxx:83
 RooNumIntFactory.cxx:84
 RooNumIntFactory.cxx:85
 RooNumIntFactory.cxx:86
 RooNumIntFactory.cxx:87
 RooNumIntFactory.cxx:88
 RooNumIntFactory.cxx:89
 RooNumIntFactory.cxx:90
 RooNumIntFactory.cxx:91
 RooNumIntFactory.cxx:92
 RooNumIntFactory.cxx:93
 RooNumIntFactory.cxx:94
 RooNumIntFactory.cxx:95
 RooNumIntFactory.cxx:96
 RooNumIntFactory.cxx:97
 RooNumIntFactory.cxx:98
 RooNumIntFactory.cxx:99
 RooNumIntFactory.cxx:100
 RooNumIntFactory.cxx:101
 RooNumIntFactory.cxx:102
 RooNumIntFactory.cxx:103
 RooNumIntFactory.cxx:104
 RooNumIntFactory.cxx:105
 RooNumIntFactory.cxx:106
 RooNumIntFactory.cxx:107
 RooNumIntFactory.cxx:108
 RooNumIntFactory.cxx:109
 RooNumIntFactory.cxx:110
 RooNumIntFactory.cxx:111
 RooNumIntFactory.cxx:112
 RooNumIntFactory.cxx:113
 RooNumIntFactory.cxx:114
 RooNumIntFactory.cxx:115
 RooNumIntFactory.cxx:116
 RooNumIntFactory.cxx:117
 RooNumIntFactory.cxx:118
 RooNumIntFactory.cxx:119
 RooNumIntFactory.cxx:120
 RooNumIntFactory.cxx:121
 RooNumIntFactory.cxx:122
 RooNumIntFactory.cxx:123
 RooNumIntFactory.cxx:124
 RooNumIntFactory.cxx:125
 RooNumIntFactory.cxx:126
 RooNumIntFactory.cxx:127
 RooNumIntFactory.cxx:128
 RooNumIntFactory.cxx:129
 RooNumIntFactory.cxx:130
 RooNumIntFactory.cxx:131
 RooNumIntFactory.cxx:132
 RooNumIntFactory.cxx:133
 RooNumIntFactory.cxx:134
 RooNumIntFactory.cxx:135
 RooNumIntFactory.cxx:136
 RooNumIntFactory.cxx:137
 RooNumIntFactory.cxx:138
 RooNumIntFactory.cxx:139
 RooNumIntFactory.cxx:140
 RooNumIntFactory.cxx:141
 RooNumIntFactory.cxx:142
 RooNumIntFactory.cxx:143
 RooNumIntFactory.cxx:144
 RooNumIntFactory.cxx:145
 RooNumIntFactory.cxx:146
 RooNumIntFactory.cxx:147
 RooNumIntFactory.cxx:148
 RooNumIntFactory.cxx:149
 RooNumIntFactory.cxx:150
 RooNumIntFactory.cxx:151
 RooNumIntFactory.cxx:152
 RooNumIntFactory.cxx:153
 RooNumIntFactory.cxx:154
 RooNumIntFactory.cxx:155
 RooNumIntFactory.cxx:156
 RooNumIntFactory.cxx:157
 RooNumIntFactory.cxx:158
 RooNumIntFactory.cxx:159
 RooNumIntFactory.cxx:160
 RooNumIntFactory.cxx:161
 RooNumIntFactory.cxx:162
 RooNumIntFactory.cxx:163
 RooNumIntFactory.cxx:164
 RooNumIntFactory.cxx:165
 RooNumIntFactory.cxx:166
 RooNumIntFactory.cxx:167
 RooNumIntFactory.cxx:168
 RooNumIntFactory.cxx:169
 RooNumIntFactory.cxx:170
 RooNumIntFactory.cxx:171
 RooNumIntFactory.cxx:172
 RooNumIntFactory.cxx:173
 RooNumIntFactory.cxx:174
 RooNumIntFactory.cxx:175
 RooNumIntFactory.cxx:176
 RooNumIntFactory.cxx:177
 RooNumIntFactory.cxx:178
 RooNumIntFactory.cxx:179
 RooNumIntFactory.cxx:180
 RooNumIntFactory.cxx:181
 RooNumIntFactory.cxx:182
 RooNumIntFactory.cxx:183
 RooNumIntFactory.cxx:184
 RooNumIntFactory.cxx:185
 RooNumIntFactory.cxx:186
 RooNumIntFactory.cxx:187
 RooNumIntFactory.cxx:188
 RooNumIntFactory.cxx:189
 RooNumIntFactory.cxx:190
 RooNumIntFactory.cxx:191
 RooNumIntFactory.cxx:192
 RooNumIntFactory.cxx:193
 RooNumIntFactory.cxx:194
 RooNumIntFactory.cxx:195
 RooNumIntFactory.cxx:196
 RooNumIntFactory.cxx:197
 RooNumIntFactory.cxx:198
 RooNumIntFactory.cxx:199
 RooNumIntFactory.cxx:200
 RooNumIntFactory.cxx:201
 RooNumIntFactory.cxx:202
 RooNumIntFactory.cxx:203
 RooNumIntFactory.cxx:204
 RooNumIntFactory.cxx:205
 RooNumIntFactory.cxx:206
 RooNumIntFactory.cxx:207
 RooNumIntFactory.cxx:208
 RooNumIntFactory.cxx:209
 RooNumIntFactory.cxx:210
 RooNumIntFactory.cxx:211
 RooNumIntFactory.cxx:212
 RooNumIntFactory.cxx:213
 RooNumIntFactory.cxx:214
 RooNumIntFactory.cxx:215
 RooNumIntFactory.cxx:216
 RooNumIntFactory.cxx:217
 RooNumIntFactory.cxx:218
 RooNumIntFactory.cxx:219
 RooNumIntFactory.cxx:220
 RooNumIntFactory.cxx:221
 RooNumIntFactory.cxx:222
 RooNumIntFactory.cxx:223
 RooNumIntFactory.cxx:224
 RooNumIntFactory.cxx:225
 RooNumIntFactory.cxx:226
 RooNumIntFactory.cxx:227
 RooNumIntFactory.cxx:228
 RooNumIntFactory.cxx:229
 RooNumIntFactory.cxx:230
 RooNumIntFactory.cxx:231
 RooNumIntFactory.cxx:232
 RooNumIntFactory.cxx:233
 RooNumIntFactory.cxx:234
 RooNumIntFactory.cxx:235
 RooNumIntFactory.cxx:236
 RooNumIntFactory.cxx:237
 RooNumIntFactory.cxx:238
 RooNumIntFactory.cxx:239
 RooNumIntFactory.cxx:240
 RooNumIntFactory.cxx:241
 RooNumIntFactory.cxx:242