Logo ROOT  
Reference Guide
RooAbsNumGenerator.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 RooAbsNumGenerator.cxx
19 \class RooAbsNumGenerator
20 \ingroup Roofitcore
21 
22 Class RooAbsNumGenerator is the abstract base class for MC event generator
23 implementations like RooAcceptReject and RooFoam
24 **/
25 
26 
27 #include "RooFit.h"
28 #include "Riostream.h"
29 
30 #include "RooAbsNumGenerator.h"
31 #include "RooAbsReal.h"
32 #include "RooCategory.h"
33 #include "RooRealVar.h"
34 #include "RooDataSet.h"
35 #include "RooRandom.h"
36 #include "RooErrorHandler.h"
37 
38 #include "TString.h"
39 #include "TIterator.h"
40 #include "RooMsgService.h"
41 #include "TClass.h"
42 #include "RooRealBinding.h"
43 
44 #include <assert.h>
45 
46 using namespace std;
47 
49  ;
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Initialize an accept-reject generator for the specified distribution function,
54 /// which must be non-negative but does not need to be normalized over the
55 /// variables to be generated, genVars. The function and its dependents are
56 /// cloned and so will not be disturbed during the generation process.
57 
58 RooAbsNumGenerator::RooAbsNumGenerator(const RooAbsReal &func, const RooArgSet &genVars, Bool_t verbose, const RooAbsReal* maxFuncVal) :
59  TNamed(func), _cloneSet(0), _funcClone(0), _funcMaxVal(maxFuncVal), _verbose(verbose), _funcValStore(0), _funcValPtr(0), _cache(0)
60 {
61  // Clone the function and all nodes that it depends on so that this generator
62  // is independent of any existing objects.
63  RooArgSet nodes(func,func.GetName());
64  _cloneSet= (RooArgSet*) nodes.snapshot(kTRUE);
65  if (!_cloneSet) {
66  coutE(Generation) << "RooAbsNumGenerator::RooAbsNumGenerator(" << GetName() << ") Couldn't deep-clone function, abort," << endl ;
68  }
69 
70  // Find the clone in the snapshot list
72 
73 
74  // Check that each argument is fundamental, and separate them into
75  // sets of categories and reals. Check that the area of the generating
76  // space is finite.
77  _isValid= kTRUE;
78  TIterator *iterator= genVars.createIterator();
79  const RooAbsArg *found = 0;
80  const RooAbsArg *arg = 0;
81  while((arg= (const RooAbsArg*)iterator->Next())) {
82  if(!arg->isFundamental()) {
83  coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for derived \""
84  << arg->GetName() << "\"" << endl;
86  continue;
87  }
88  // look for this argument in the generating function's dependents
89  found= (const RooAbsArg*)_cloneSet->find(arg->GetName());
90  if(found) {
91  arg= found;
92  } else {
93  // clone any variables we generate that we haven't cloned already
94  arg= _cloneSet->addClone(*arg);
95  }
96  assert(0 != arg);
97  // is this argument a category or a real?
98  const RooCategory *catVar= dynamic_cast<const RooCategory*>(arg);
99  const RooRealVar *realVar= dynamic_cast<const RooRealVar*>(arg);
100  if(0 != catVar) {
101  _catVars.add(*catVar);
102  }
103  else if(0 != realVar) {
104  if(realVar->hasMin() && realVar->hasMax()) {
105  _realVars.add(*realVar);
106  }
107  else {
108  coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for \""
109  << realVar->GetName() << "\" with unbound range" << endl;
110  _isValid= kFALSE;
111  }
112  }
113  else {
114  coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for \""
115  << arg->GetName() << "\" with unexpected type" << endl;
116  _isValid= kFALSE;
117  }
118  }
119  delete iterator;
120  if(!_isValid) {
121  coutE(Generation) << fName << "::" << ClassName() << ": constructor failed with errors" << endl;
122  return;
123  }
124 
125  // create a fundamental type for storing function values
127  assert(0 != _funcValStore);
128 
129  // create a new dataset to cache trial events and function values
130  RooArgSet cacheArgs(_catVars);
131  cacheArgs.add(_realVars);
132  cacheArgs.add(*_funcValStore);
133  _cache= new RooDataSet("cache","Accept-Reject Event Cache",cacheArgs);
134  assert(0 != _cache);
135 
136  // attach our function clone to the cache dataset
137  const RooArgSet *cacheVars= _cache->get();
138  assert(0 != cacheVars);
140 
141  // update ours sets of category and real args to refer to the cache dataset
142  const RooArgSet *dataVars= _cache->get();
143  _catVars.replace(*dataVars);
144  _realVars.replace(*dataVars);
145 
146  // find the function value in the dataset
148 
149 }
150 
151 
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Destructor
155 
157 {
158  delete _cloneSet;
159  delete _cache ;
160  delete _funcValStore ;
161 }
162 
163 
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// Reattach original parameters to function clone
167 
169 {
170  RooArgSet newParams(vars) ;
171  newParams.remove(*_cache->get(),kTRUE,kTRUE) ;
173 }
174 
175 
176 
177 
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Print name of the generator
181 
182 void RooAbsNumGenerator::printName(ostream& os) const
183 {
184  os << GetName() ;
185 }
186 
187 
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Print the title of the generator
191 
192 void RooAbsNumGenerator::printTitle(ostream& os) const
193 {
194  os << GetTitle() ;
195 }
196 
197 
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// Print the class name of the generator
201 
202 void RooAbsNumGenerator::printClassName(ostream& os) const
203 {
204  os << IsA()->GetName() ;
205 }
206 
207 
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 /// Print the arguments of the generator
211 
212 void RooAbsNumGenerator::printArgs(ostream& os) const
213 {
214  os << "[ function=" << _funcClone->GetName() << " catobs=" << _catVars << " realobs=" << _realVars << " ]" ;
215 }
216 
RooErrorHandler::softAbort
static void softAbort()
Definition: RooErrorHandler.h:44
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooAbsNumGenerator::printArgs
virtual void printArgs(std::ostream &os) const
Print the arguments of the generator.
Definition: RooAbsNumGenerator.cxx:212
RooAbsReal.h
RooMsgService.h
RooFit.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:54
RooAbsNumGenerator::RooAbsNumGenerator
RooAbsNumGenerator()
Definition: RooAbsNumGenerator.h:32
coutE
#define coutE(a)
Definition: RooMsgService.h:33
RooAbsNumGenerator::printTitle
virtual void printTitle(std::ostream &os) const
Print the title of the generator.
Definition: RooAbsNumGenerator.cxx:192
RooAbsNumGenerator::printName
virtual void printName(std::ostream &os) const
Print name of the generator.
Definition: RooAbsNumGenerator.cxx:182
RooAbsNumGenerator::_cache
RooDataSet * _cache
Definition: RooAbsNumGenerator.h:80
RooAbsCollection::remove
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
Definition: RooAbsCollection.cxx:584
TNamed::fName
TString fName
Definition: TNamed.h:38
RooAbsCollection::find
RooAbsArg * find(const char *name) const
Find object with given name in list.
Definition: RooAbsCollection.cxx:813
TClass.h
RooArgSet::add
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:88
RooAbsReal
Definition: RooAbsReal.h:61
RooAbsArg::recursiveRedirectServers
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Definition: RooAbsArg.cxx:1066
RooAbsRealLValue::hasMax
Bool_t hasMax(const char *name=0) const
Check if variable has an upper bound.
Definition: RooAbsRealLValue.h:94
RooAbsNumGenerator::_catVars
RooArgSet _catVars
Definition: RooAbsNumGenerator.h:76
RooAbsNumGenerator::_funcValStore
RooRealVar * _funcValStore
Definition: RooAbsNumGenerator.h:78
RooDataSet.h
TString.h
bool
TIterator
Definition: TIterator.h:30
RooDataSet::get
virtual const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
Definition: RooDataSet.cxx:1038
RooAbsNumGenerator::_funcClone
RooAbsReal * _funcClone
Definition: RooAbsNumGenerator.h:74
RooArgSet::snapshot
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:134
RooAbsNumGenerator::_funcValPtr
RooRealVar * _funcValPtr
Definition: RooAbsNumGenerator.h:78
RooAbsNumGenerator::printClassName
virtual void printClassName(std::ostream &os) const
Print the class name of the generator.
Definition: RooAbsNumGenerator.cxx:202
TIterator.h
TNamed
Definition: TNamed.h:29
RooFit::Generation
@ Generation
Definition: RooGlobalFunc.h:67
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooRandom.h
RooAbsCollection::createIterator
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
Definition: RooAbsCollection.h:118
RooAbsCollection::replace
virtual Bool_t replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return kTRUE for success.
Definition: RooAbsCollection.cxx:533
RooAbsNumGenerator::_cloneSet
RooArgSet * _cloneSet
Definition: RooAbsNumGenerator.h:73
RooAbsNumGenerator
Definition: RooAbsNumGenerator.h:30
RooCategory.h
RooRealVar.h
TIterator::Next
virtual TObject * Next()=0
RooAbsArg::isFundamental
virtual Bool_t isFundamental() const
Is this object a fundamental type that can be added to a dataset? Fundamental-type subclasses overrid...
Definition: RooAbsArg.h:243
RooAbsNumGenerator::_isValid
Bool_t _isValid
Definition: RooAbsNumGenerator.h:77
RooAbsRealLValue::hasMin
Bool_t hasMin(const char *name=0) const
Check if variable has a lower bound.
Definition: RooAbsRealLValue.h:92
RooAbsNumGenerator::_realVars
RooArgSet _realVars
Definition: RooAbsNumGenerator.h:76
RooCategory
Definition: RooCategory.h:27
RooAbsReal::createFundamental
RooAbsArg * createFundamental(const char *newname=0) const
Create a RooRealVar fundamental object with our properties.
Definition: RooAbsReal.cxx:3384
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooErrorHandler.h
RooDataSet
Definition: RooDataSet.h:33
RooRealBinding.h
RooAbsArg
Definition: RooAbsArg.h:73
RooArgSet::addClone
virtual void addClone(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling addOwned() for each element in the source...
Definition: RooArgSet.h:96
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
RooAbsNumGenerator::~RooAbsNumGenerator
virtual ~RooAbsNumGenerator()
Destructor.
Definition: RooAbsNumGenerator.cxx:156
RooRealVar
Definition: RooRealVar.h:35
Riostream.h
TObject::ClassName
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:130
RooAbsNumGenerator::attachParameters
void attachParameters(const RooArgSet &vars)
Reattach original parameters to function clone.
Definition: RooAbsNumGenerator.cxx:168
RooArgSet
Definition: RooArgSet.h:28
RooAbsNumGenerator.h