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#include "Riostream.h"
27
28#include "RooAbsNumGenerator.h"
29#include "RooAbsReal.h"
30#include "RooCategory.h"
31#include "RooRealVar.h"
32#include "RooDataSet.h"
33#include "RooRandom.h"
34#include "RooErrorHandler.h"
35
36#include "RooMsgService.h"
37#include "RooRealBinding.h"
38
39#include <assert.h>
40
41using namespace std;
42
44 ;
45
46
47////////////////////////////////////////////////////////////////////////////////
48/// Initialize an accept-reject generator for the specified distribution function,
49/// which must be non-negative but does not need to be normalized over the
50/// variables to be generated, genVars. The function and its dependents are
51/// cloned and so will not be disturbed during the generation process.
52
53RooAbsNumGenerator::RooAbsNumGenerator(const RooAbsReal &func, const RooArgSet &genVars, bool verbose, const RooAbsReal* maxFuncVal) :
54 TNamed(func), _cloneSet(0), _funcClone(0), _funcMaxVal(maxFuncVal), _verbose(verbose), _funcValStore(0), _funcValPtr(0), _cache(0)
55{
56 // Clone the function and all nodes that it depends on so that this generator
57 // is independent of any existing objects.
58 RooArgSet nodes(func,func.GetName());
59 _cloneSet= (RooArgSet*) nodes.snapshot(true);
60 if (!_cloneSet) {
61 coutE(Generation) << "RooAbsNumGenerator::RooAbsNumGenerator(" << GetName() << ") Couldn't deep-clone function, abort," << endl ;
63 }
64
65 // Find the clone in the snapshot list
67
68
69 // Check that each argument is fundamental, and separate them into
70 // sets of categories and reals. Check that the area of the generating
71 // space is finite.
72 _isValid= true;
73 const RooAbsArg *found = 0;
74 for (RooAbsArg const* arg : genVars) {
75 if(!arg->isFundamental()) {
76 coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for derived \""
77 << arg->GetName() << "\"" << endl;
78 _isValid= false;
79 continue;
80 }
81 // look for this argument in the generating function's dependents
82 found= (const RooAbsArg*)_cloneSet->find(arg->GetName());
83 if(found) {
84 arg= found;
85 } else {
86 // clone any variables we generate that we haven't cloned already
87 arg= _cloneSet->addClone(*arg);
88 }
89 assert(0 != arg);
90 // is this argument a category or a real?
91 const RooCategory *catVar= dynamic_cast<const RooCategory*>(arg);
92 const RooRealVar *realVar= dynamic_cast<const RooRealVar*>(arg);
93 if(0 != catVar) {
94 _catVars.add(*catVar);
95 }
96 else if(0 != realVar) {
97 if(realVar->hasMin() && realVar->hasMax()) {
98 _realVars.add(*realVar);
99 }
100 else {
101 coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for \""
102 << realVar->GetName() << "\" with unbound range" << endl;
103 _isValid= false;
104 }
105 }
106 else {
107 coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for \""
108 << arg->GetName() << "\" with unexpected type" << endl;
109 _isValid= false;
110 }
111 }
112 if(!_isValid) {
113 coutE(Generation) << fName << "::" << ClassName() << ": constructor failed with errors" << endl;
114 return;
115 }
116
117 // create a fundamental type for storing function values
119 assert(0 != _funcValStore);
120
121 // create a new dataset to cache trial events and function values
122 RooArgSet cacheArgs(_catVars);
123 cacheArgs.add(_realVars);
124 cacheArgs.add(*_funcValStore);
125 _cache= new RooDataSet("cache","Accept-Reject Event Cache",cacheArgs);
126 assert(0 != _cache);
127
128 // attach our function clone to the cache dataset
129 const RooArgSet *cacheVars= _cache->get();
130 assert(0 != cacheVars);
131 _funcClone->recursiveRedirectServers(*cacheVars,false);
132
133 // update ours sets of category and real args to refer to the cache dataset
134 const RooArgSet *dataVars= _cache->get();
135 _catVars.replace(*dataVars);
136 _realVars.replace(*dataVars);
137
138 // find the function value in the dataset
140
141}
142
143
144
145////////////////////////////////////////////////////////////////////////////////
146/// Destructor
147
149{
150 delete _cloneSet;
151 delete _cache ;
152 delete _funcValStore ;
153}
154
155
156
157////////////////////////////////////////////////////////////////////////////////
158/// Reattach original parameters to function clone
159
161{
162 RooArgSet newParams(vars) ;
163 newParams.remove(*_cache->get(),true,true) ;
165}
166
167
168
169
170
171////////////////////////////////////////////////////////////////////////////////
172/// Print name of the generator
173
174void RooAbsNumGenerator::printName(ostream& os) const
175{
176 os << GetName() ;
177}
178
179
180
181////////////////////////////////////////////////////////////////////////////////
182/// Print the title of the generator
183
184void RooAbsNumGenerator::printTitle(ostream& os) const
185{
186 os << GetTitle() ;
187}
188
189
190
191////////////////////////////////////////////////////////////////////////////////
192/// Print the class name of the generator
193
195{
196 os << ClassName() ;
197}
198
199
200
201////////////////////////////////////////////////////////////////////////////////
202/// Print the arguments of the generator
203
204void RooAbsNumGenerator::printArgs(ostream& os) const
205{
206 os << "[ function=" << _funcClone->GetName() << " catobs=" << _catVars << " realobs=" << _realVars << " ]" ;
207}
208
#define coutE(a)
Definition: RooMsgService.h:37
#define ClassImp(name)
Definition: Rtypes.h:375
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:71
bool recursiveRedirectServers(const RooAbsCollection &newServerList, bool mustReplaceAll=false, bool nameChange=false, bool recurseInNewSet=true)
Recursively replace all servers with the new servers in newSet.
Definition: RooAbsArg.cxx:1165
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
virtual bool replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return true for success.
virtual RooAbsArg * addClone(const RooAbsArg &var, bool silent=false)
Add a clone of the specified argument to list.
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...
void printName(std::ostream &os) const override
Print name of the generator.
RooArgSet _realVars
Sets of discrete and real valued observabeles.
RooDataSet * _cache
Dataset holding generared values of observables.
bool _isValid
Verbose and valid flag.
RooRealVar * _funcValPtr
RRVs storing function value in context and in output dataset.
~RooAbsNumGenerator() override
Destructor.
RooAbsReal * _funcClone
Pointer to top level node of cloned function.
RooArgSet * _cloneSet
Set owning clone of input function.
void attachParameters(const RooArgSet &vars)
Reattach original parameters to function clone.
void printTitle(std::ostream &os) const override
Print the title of the generator.
void printArgs(std::ostream &os) const override
Print the arguments of the generator.
void printClassName(std::ostream &os) const override
Print the class name of the generator.
RooRealVar * _funcValStore
bool hasMax(const char *name=nullptr) const
Check if variable has an upper bound.
bool hasMin(const char *name=nullptr) 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:62
RooAbsArg * createFundamental(const char *newname=nullptr) const override
Create a RooRealVar fundamental object with our properties.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:179
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:28
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
static void softAbort()
Soft abort function that interrupts macro execution but doesn't kill ROOT.
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition: TNamed.h:48
TString fName
Definition: TNamed.h:32
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:207
@ Generation
Definition: RooGlobalFunc.h:61