Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooFoamGenerator.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 RooFoamGenerator.cxx
19 \class RooFoamGenerator
20 \ingroup Roofitcore
21 
22 Class RooFoamGenerator is a generic toy monte carlo generator that implement
23 the TFOAM sampling technique on any positively valued function.
24 The RooFoamGenerator generator is used by the various generator context
25 classes to take care of generation of observables for which p.d.fs
26 do not define internal methods
27 **/
28 
29 
30 #include "RooFit.h"
31 #include "Riostream.h"
32 
33 #include "RooFoamGenerator.h"
34 #include "RooAbsReal.h"
35 #include "RooCategory.h"
36 #include "RooRealVar.h"
37 #include "RooDataSet.h"
38 #include "RooRandom.h"
39 #include "RooErrorHandler.h"
40 
41 #include "TString.h"
42 #include "TIterator.h"
43 #include "RooMsgService.h"
44 #include "TClass.h"
45 #include "TFoam.h"
46 #include "RooTFoamBinding.h"
47 #include "RooNumGenFactory.h"
48 #include "RooNumGenConfig.h"
49 
50 #include <assert.h>
51 
52 using namespace std;
53 
54 ClassImp(RooFoamGenerator);
55  ;
56 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
60 
61 void RooFoamGenerator::registerSampler(RooNumGenFactory& fact)
62 {
63  // Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
64  RooRealVar nSample("nSample","Number of samples per cell",200,0,1e6) ;
65  RooRealVar nCell1D("nCell1D","Number of cells for 1-dim generation",30,0,1e6) ;
66  RooRealVar nCell2D("nCell2D","Number of cells for 2-dim generation",500,0,1e6) ;
67  RooRealVar nCell3D("nCell3D","Number of cells for 3-dim generation",5000,0,1e6) ;
68  RooRealVar nCellND("nCellND","Number of cells for N-dim generation",10000,0,1e6) ;
69  RooRealVar chatLevel("chatLevel","TFOAM 'chat level' (verbosity)",0,0,2) ;
70 
71  RooFoamGenerator* proto = new RooFoamGenerator ;
72  fact.storeProtoSampler(proto,RooArgSet(nSample,nCell1D,nCell2D,nCell3D,nCellND,chatLevel)) ;
73 }
74 
75 
76 
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 
80 RooFoamGenerator::RooFoamGenerator(const RooAbsReal &func, const RooArgSet &genVars, const RooNumGenConfig& config, Bool_t verbose, const RooAbsReal* maxFuncVal) :
81  RooAbsNumGenerator(func,genVars,verbose,maxFuncVal)
82 {
83  _rvIter = _realVars.createIterator() ;
84  _binding = new RooTFoamBinding(*_funcClone,_realVars) ;
85 
86  _tfoam = new TFoam("TFOAM") ;
87  _tfoam->SetkDim(_realVars.getSize()) ;
88  _tfoam->SetRho(_binding) ;
89  _tfoam->SetPseRan(RooRandom::randomGenerator()) ;
90  switch(_realVars.getSize()) {
91  case 1:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell1D")) ; break ;
92  case 2:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell2D")) ; break ;
93  case 3:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell3D")) ; break ;
94  default:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCellND")) ; break ;
95  }
96  _tfoam->SetnSampl((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nSample")) ;
97  _tfoam->SetPseRan(RooRandom::randomGenerator()) ;
98  _tfoam->SetChat((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("chatLevel")) ;
99  _tfoam->Initialize() ;
100 
101  _vec = new Double_t[_realVars.getSize()] ;
102  _xmin = new Double_t[_realVars.getSize()] ;
103  _range = new Double_t[_realVars.getSize()] ;
104 
105  TIterator* iter = _realVars.createIterator() ;
106  RooRealVar* var ;
107  Int_t i(0) ;
108  while((var=(RooRealVar*)iter->Next())) {
109  _xmin[i] = var->getMin() ;
110  _range[i] = var->getMax() - var->getMin() ;
111  i++ ;
112  }
113  delete iter ;
114 
115 }
116 
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Destructor
120 
121 RooFoamGenerator::~RooFoamGenerator()
122 {
123  delete[] _vec ;
124  delete[] _xmin ;
125  delete[] _range ;
126  delete _tfoam ;
127  delete _binding ;
128  delete _rvIter ;
129 }
130 
131 
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// are we actually generating anything? (the cache always contains at least our function value)
135 
136 const RooArgSet *RooFoamGenerator::generateEvent(UInt_t /*remaining*/, Double_t& /*resampleRatio*/)
137 {
138  const RooArgSet *event= _cache->get();
139  if(event->getSize() == 1) return event;
140 
141  _tfoam->MakeEvent() ;
142  _tfoam->GetMCvect(_vec) ;
143 
144  // Transfer contents to dataset
145  RooRealVar* var ;
146  _rvIter->Reset() ;
147  Int_t i(0) ;
148  while((var=(RooRealVar*)_rvIter->Next())) {
149  var->setVal(_xmin[i] + _range[i]*_vec[i]) ;
150  i++ ;
151  }
152  return &_realVars ;
153 }