Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
43
44
45////////////////////////////////////////////////////////////////////////////////
46/// Initialize an accept-reject generator for the specified distribution function,
47/// which must be non-negative but does not need to be normalized over the
48/// variables to be generated, genVars. The function and its dependents are
49/// cloned and so will not be disturbed during the generation process.
50
51RooAbsNumGenerator::RooAbsNumGenerator(const RooAbsReal &func, const RooArgSet &genVars, bool verbose, const RooAbsReal* maxFuncVal) :
52 _cloneSet(0), _funcClone(0), _funcMaxVal(maxFuncVal), _verbose(verbose), _funcValStore(0), _funcValPtr(0), _cache(0)
53{
54 // Clone the function and all nodes that it depends on so that this generator
55 // is independent of any existing objects.
56 RooArgSet nodes(func,func.GetName());
57 _cloneSet= (RooArgSet*) nodes.snapshot(true);
58 if (!_cloneSet) {
59 oocoutE(nullptr, Generation) << "RooAbsNumGenerator::RooAbsNumGenerator(" << func.GetName() << ") Couldn't deep-clone function, abort," << endl ;
61 }
62
63 // Find the clone in the snapshot list
65
66
67 // Check that each argument is fundamental, and separate them into
68 // sets of categories and reals. Check that the area of the generating
69 // space is finite.
70 _isValid= true;
71 const RooAbsArg *found = 0;
72 for (RooAbsArg const* arg : genVars) {
73 if(!arg->isFundamental()) {
74 oocoutE(nullptr, Generation) << func.GetName() << "::RooAbsNumGenerator: cannot generate values for derived \""
75 << arg->GetName() << "\"" << endl;
76 _isValid= false;
77 continue;
78 }
79 // look for this argument in the generating function's dependents
80 found= (const RooAbsArg*)_cloneSet->find(arg->GetName());
81 if(found) {
82 arg= found;
83 } else {
84 // clone any variables we generate that we haven't cloned already
85 arg= _cloneSet->addClone(*arg);
86 }
87 assert(0 != arg);
88 // is this argument a category or a real?
89 const RooCategory *catVar= dynamic_cast<const RooCategory*>(arg);
90 const RooRealVar *realVar= dynamic_cast<const RooRealVar*>(arg);
91 if(0 != catVar) {
92 _catVars.add(*catVar);
93 }
94 else if(0 != realVar) {
95 if(realVar->hasMin() && realVar->hasMax()) {
96 _realVars.add(*realVar);
97 }
98 else {
99 oocoutE(nullptr, Generation) << func.GetName() << "::RooAbsNumGenerator: cannot generate values for \""
100 << realVar->GetName() << "\" with unbound range" << endl;
101 _isValid= false;
102 }
103 }
104 else {
105 oocoutE(nullptr, Generation) << func.GetName() << "::RooAbsNumGenerator" << ": cannot generate values for \""
106 << arg->GetName() << "\" with unexpected type" << endl;
107 _isValid= false;
108 }
109 }
110 if(!_isValid) {
111 oocoutE(nullptr, Generation) << func.GetName() << "::RooAbsNumGenerator" << ": constructor failed with errors" << endl;
112 return;
113 }
114
115 // create a fundamental type for storing function values
117 assert(0 != _funcValStore);
118
119 // create a new dataset to cache trial events and function values
120 RooArgSet cacheArgs(_catVars);
121 cacheArgs.add(_realVars);
122 cacheArgs.add(*_funcValStore);
123 _cache= new RooDataSet("cache","Accept-Reject Event Cache",cacheArgs);
124 assert(0 != _cache);
125
126 // attach our function clone to the cache dataset
127 const RooArgSet *cacheVars= _cache->get();
128 assert(0 != cacheVars);
129 _funcClone->recursiveRedirectServers(*cacheVars,false);
130
131 // update ours sets of category and real args to refer to the cache dataset
132 const RooArgSet *dataVars= _cache->get();
133 _catVars.replace(*dataVars);
134 _realVars.replace(*dataVars);
135
136 // find the function value in the dataset
138
139}
140
141
142
143////////////////////////////////////////////////////////////////////////////////
144/// Destructor
145
147{
148 delete _cloneSet;
149 delete _cache ;
150 delete _funcValStore ;
151}
152
153
154
155////////////////////////////////////////////////////////////////////////////////
156/// Reattach original parameters to function clone
157
159{
160 RooArgSet newParams(vars) ;
161 newParams.remove(*_cache->get(),true,true) ;
163}
bool _verbose
Definition RooMinuit.h:94
#define oocoutE(o, a)
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
bool recursiveRedirectServers(const RooAbsCollection &newServerList, bool mustReplaceAll=false, bool nameChange=false, bool recurseInNewSet=true)
Recursively replace all servers with the new servers in newSet.
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.
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.
RooAbsReal * _funcClone
Pointer to top level node of cloned function.
RooArgSet * _cloneSet
Set owning clone of input function.
virtual ~RooAbsNumGenerator()
Destructor.
void attachParameters(const RooArgSet &vars)
Reattach original parameters to function clone.
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:55
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:178
RooCategory is an object to represent discrete states.
Definition RooCategory.h:28
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:57
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
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47