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
22Class RooAbsNumGenerator is the abstract base class for MC event generator
23implementations 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
46using 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
58RooAbsNumGenerator::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());
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.
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;
111 }
112 }
113 else {
114 coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for \""
115 << arg->GetName() << "\" with unexpected type" << endl;
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
182void RooAbsNumGenerator::printName(ostream& os) const
183{
184 os << GetName() ;
185}
186
187
188
189////////////////////////////////////////////////////////////////////////////////
190/// Print the title of the generator
191
192void RooAbsNumGenerator::printTitle(ostream& os) const
193{
194 os << GetTitle() ;
195}
196
197
198
199////////////////////////////////////////////////////////////////////////////////
200/// Print the class name of the generator
201
203{
204 os << IsA()->GetName() ;
205}
206
207
208
209////////////////////////////////////////////////////////////////////////////////
210/// Print the arguments of the generator
211
212void RooAbsNumGenerator::printArgs(ostream& os) const
213{
214 os << "[ function=" << _funcClone->GetName() << " catobs=" << _catVars << " realobs=" << _realVars << " ]" ;
215}
216
#define coutE(a)
Definition: RooMsgService.h:33
const Bool_t kFALSE
Definition: RtypesCore.h:90
bool Bool_t
Definition: RtypesCore.h:61
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:73
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
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Definition: RooAbsArg.cxx:1064
virtual Bool_t replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return kTRUE for success.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
RooAbsArg * find(const char *name) const
Find object with given name in list.
Class RooAbsNumGenerator is the abstract base class for MC event generator implementations like RooAc...
virtual void printName(std::ostream &os) const
Print name of the generator.
virtual void printClassName(std::ostream &os) const
Print the class name of the generator.
RooRealVar * _funcValPtr
virtual ~RooAbsNumGenerator()
Destructor.
void attachParameters(const RooArgSet &vars)
Reattach original parameters to function clone.
virtual void printArgs(std::ostream &os) const
Print the arguments of the generator.
virtual void printTitle(std::ostream &os) const
Print the title of the generator.
RooRealVar * _funcValStore
Bool_t hasMax(const char *name=0) const
Check if variable has an upper bound.
Bool_t hasMin(const char *name=0) const
Check if variable has a lower bound.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:60
RooAbsArg * createFundamental(const char *newname=0) const
Create a RooRealVar fundamental object with our properties.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:126
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
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
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:23
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
virtual const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
static void softAbort()
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
Iterator abstract base class.
Definition: TIterator.h:30
virtual TObject * Next()=0
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TString fName
Definition: TNamed.h:32
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:128
@ Generation
Definition: RooGlobalFunc.h:67