RooNumGenFactory.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooNumGenFactory.cxx 28259 2009-04-16 16:21:16Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 //
00019 // BEGIN_HTML
00020 // RooNumGenFactory is a factory to instantiate numeric integrators
00021 // from a given function binding and a given configuration. The factory
00022 // searches for a numeric integrator registered with the factory that
00023 // has the ability to perform the numeric integration. The choice of
00024 // method may depend on the number of dimensions integrated,
00025 // the nature of the integration limits (closed or open ended) and
00026 // the preference of the caller as encoded in the configuration object.
00027 // END_HTML
00028 //
00029 
00030 #include "TClass.h"
00031 #include "Riostream.h"
00032 
00033 #include "RooFit.h"
00034 
00035 #include "RooNumGenFactory.h"
00036 #include "RooArgSet.h"
00037 #include "RooAbsFunc.h"
00038 #include "RooNumGenConfig.h"
00039 #include "RooNumber.h"
00040 
00041 #include "RooAcceptReject.h"
00042 #include "RooFoamGenerator.h"
00043 #include "RooSentinel.h"
00044 
00045 
00046 #include "RooMsgService.h"
00047 
00048 using namespace std ;
00049 
00050 ClassImp(RooNumGenFactory)
00051 ;
00052 
00053 RooNumGenFactory* RooNumGenFactory::_instance = 0 ;
00054 
00055 
00056 
00057 //_____________________________________________________________________________
00058 RooNumGenFactory::RooNumGenFactory()
00059 {
00060   // Constructor. Register all known integrators by calling
00061   // their static registration functions
00062 
00063   _instance = this ;
00064 
00065   RooAcceptReject::registerSampler(*this) ;
00066   RooFoamGenerator::registerSampler(*this) ;
00067 
00068   // Prepare default
00069   RooNumGenConfig::defaultConfig().method1D(kFALSE,kFALSE).setLabel("RooFoamGenerator") ;
00070   RooNumGenConfig::defaultConfig().method1D(kTRUE ,kFALSE).setLabel("RooAcceptReject") ;
00071   RooNumGenConfig::defaultConfig().method1D(kFALSE,kTRUE ).setLabel("RooAcceptReject") ;
00072   RooNumGenConfig::defaultConfig().method1D(kTRUE, kTRUE ).setLabel("RooAcceptReject") ;
00073   
00074   RooNumGenConfig::defaultConfig().method2D(kFALSE,kFALSE).setLabel("RooFoamGenerator") ;
00075   RooNumGenConfig::defaultConfig().method2D(kTRUE ,kFALSE).setLabel("RooAcceptReject") ;
00076   RooNumGenConfig::defaultConfig().method2D(kFALSE,kTRUE ).setLabel("RooAcceptReject") ;
00077   RooNumGenConfig::defaultConfig().method2D(kTRUE, kTRUE ).setLabel("RooAcceptReject") ;
00078   
00079   RooNumGenConfig::defaultConfig().methodND(kFALSE,kFALSE).setLabel("RooFoamGenerator") ;
00080   RooNumGenConfig::defaultConfig().methodND(kTRUE ,kFALSE).setLabel("RooAcceptReject") ;
00081   RooNumGenConfig::defaultConfig().methodND(kFALSE,kTRUE ).setLabel("RooAcceptReject") ;
00082   RooNumGenConfig::defaultConfig().methodND(kTRUE, kTRUE ).setLabel("RooAcceptReject") ;
00083   
00084 }
00085 
00086 
00087 
00088 //_____________________________________________________________________________
00089 RooNumGenFactory::~RooNumGenFactory()
00090 {
00091   // Destructor
00092 
00093   std::map<std::string,RooAbsNumGenerator*>::iterator iter = _map.begin() ;
00094   while (iter != _map.end()) {
00095     delete iter->second ;
00096     ++iter ;
00097   }  
00098 }
00099 
00100 
00101 //_____________________________________________________________________________
00102 RooNumGenFactory::RooNumGenFactory(const RooNumGenFactory& other) : TObject(other)
00103 {
00104   // Copy constructor
00105 }
00106 
00107 
00108 
00109 //_____________________________________________________________________________
00110 RooNumGenFactory& RooNumGenFactory::instance()
00111 {
00112   // Static method returning reference to singleton instance of factory
00113 
00114   if (_instance==0) {
00115     new RooNumGenFactory ;
00116     RooSentinel::activate() ;
00117   } 
00118   return *_instance ;
00119 }
00120 
00121 
00122 //_____________________________________________________________________________
00123 void RooNumGenFactory::cleanup()
00124 {
00125   // Cleanup routine called by atexit() handler installed by RooSentinel
00126 
00127   if (_instance) {
00128     delete _instance ;
00129     _instance = 0 ;
00130   }
00131 }
00132 
00133 
00134 
00135 //_____________________________________________________________________________
00136 Bool_t RooNumGenFactory::storeProtoSampler(RooAbsNumGenerator* proto, const RooArgSet& defConfig) 
00137 {
00138   // Method accepting registration of a prototype numeric integrator along with a RooArgSet of its
00139   // default configuration options and an optional list of names of other numeric integrators
00140   // on which this integrator depends. Returns true if integrator was previously registered
00141 
00142   TString name = proto->IsA()->GetName() ;
00143 
00144   if (getProtoSampler(name)) {
00145     //cout << "RooNumGenFactory::storeSampler() ERROR: integrator '" << name << "' already registered" << endl ;
00146     return kTRUE ;
00147   }
00148 
00149   // Add to factory 
00150   _map[name.Data()] = proto ;
00151 
00152   // Add default config to master config
00153   RooNumGenConfig::defaultConfig().addConfigSection(proto,defConfig) ;
00154   
00155   return kFALSE ;
00156 }
00157 
00158 
00159 
00160 //_____________________________________________________________________________
00161 const RooAbsNumGenerator* RooNumGenFactory::getProtoSampler(const char* name) 
00162 {
00163   // Return prototype integrator with given (class) name
00164 
00165   if (_map.count(name)==0) {
00166     return 0 ;
00167   } 
00168   
00169   return _map[name] ;
00170 }
00171 
00172 
00173 
00174 //_____________________________________________________________________________
00175 RooAbsNumGenerator* RooNumGenFactory::createSampler(RooAbsReal& func, const RooArgSet& genVars, const RooArgSet& condVars, const RooNumGenConfig& config, Bool_t verbose, RooAbsReal* maxFuncVal) 
00176 {
00177   // Construct a numeric integrator instance that operates on function 'func' and is configured
00178   // with 'config'. If ndimPreset is greater than zero that number is taken as the dimensionality
00179   // of the integration, otherwise it is queried from 'func'. This function iterators over list
00180   // of available prototype integrators and returns an clone attached to the given function of
00181   // the first class that matches the specifications of the requested integration considering
00182   // the number of dimensions, the nature of the limits (open ended vs closed) and the user
00183   // preference stated in 'config'
00184 
00185   // Find method defined configuration
00186   Int_t ndim = genVars.getSize() ;
00187   Bool_t cond = (condVars.getSize() > 0) ? kTRUE : kFALSE ;
00188 
00189   Bool_t hasCat(kFALSE) ;
00190   TIterator* iter = genVars.createIterator() ;
00191   RooAbsArg* arg ;
00192   while ((arg=(RooAbsArg*)iter->Next())) {
00193     if (arg->IsA()==RooCategory::Class()) {
00194       hasCat=kTRUE ;
00195       break ;
00196     }
00197   }
00198   delete iter ;
00199 
00200 
00201   TString method ;
00202   switch(ndim) {
00203   case 1:
00204     method = config.method1D(cond,hasCat).getLabel() ;
00205     break ;
00206 
00207   case 2:
00208     method = config.method2D(cond,hasCat).getLabel() ;
00209     break ;
00210 
00211   default:
00212     method = config.methodND(cond,hasCat).getLabel() ;
00213     break ;
00214   }
00215 
00216   // Check that a method was defined for this case
00217   if (!method.CompareTo("N/A")) {
00218     oocoutE((TObject*)0,Integration) << "RooNumGenFactory::createSampler: No sampler method has been defined for " 
00219                                      << (cond?"a conditional ":"a ") << ndim << "-dimensional p.d.f" << endl ;
00220     return 0 ;    
00221   }
00222 
00223   // Retrieve proto integrator and return clone configured for the requested integration task
00224   const RooAbsNumGenerator* proto = getProtoSampler(method) ;  
00225   RooAbsNumGenerator* engine =  proto->clone(func,genVars,condVars,config,verbose,maxFuncVal) ;
00226   return engine ;
00227 }

Generated on Tue Jul 5 15:06:53 2011 for ROOT_528-00b_version by  doxygen 1.5.1