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
22Abstract 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 <cassert>
40
41using std::endl;
42
44
45
46////////////////////////////////////////////////////////////////////////////////
47/// Initialize an accept-reject generator for the specified distribution function,
48/// which must be non-negative but does not need to be normalized over the
49/// variables to be generated, genVars. The function and its dependents are
50/// cloned and so will not be disturbed during the generation process.
51
52RooAbsNumGenerator::RooAbsNumGenerator(const RooAbsReal &func, const RooArgSet &genVars, bool verbose, const RooAbsReal* maxFuncVal) :
53 _funcMaxVal(maxFuncVal), _verbose(verbose)
54{
55 // Clone the function and all nodes that it depends on so that this generator
56 // is independent of any existing objects.
57 RooArgSet nodes(func,func.GetName());
58 if (nodes.snapshot(_cloneSet, true)) {
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
64 _funcClone = static_cast<RooAbsReal*>(_cloneSet.find(func.GetName()));
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 = nullptr;
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(nullptr != 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(nullptr != catVar) {
92 _catVars.add(*catVar);
93 }
94 else if(nullptr != 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
116 _funcValStore= std::unique_ptr<RooAbsArg>{_funcClone->createFundamental()};
117
118 // create a new dataset to cache trial events and function values
119 RooArgSet cacheArgs(_catVars);
120 cacheArgs.add(_realVars);
121 cacheArgs.add(*_funcValStore);
122 _cache= std::make_unique<RooDataSet>("cache","Accept-Reject Event Cache",cacheArgs);
123
124 // attach our function clone to the cache dataset
125 const RooArgSet *cacheVars= _cache->get();
126 assert(nullptr != cacheVars);
127 _funcClone->recursiveRedirectServers(*cacheVars,false);
128
129 // update ours sets of category and real args to refer to the cache dataset
130 const RooArgSet *dataVars= _cache->get();
131 _catVars.replace(*dataVars);
132 _realVars.replace(*dataVars);
133
134 // find the function value in the dataset
135 _funcValPtr= static_cast<RooRealVar*>(dataVars->find(_funcValStore->GetName()));
136
137}
138
139
141
142
143////////////////////////////////////////////////////////////////////////////////
144/// Reattach original parameters to function clone
145
147{
148 RooArgSet newParams(vars) ;
149 newParams.remove(*_cache->get(),true,true) ;
151}
bool _verbose
Verbose messaging if true.
#define oocoutE(o, a)
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
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.
Storage_t const & get() const
Const access to the underlying stl container.
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 _catVars
Set of discrete observabeles.
RooArgSet _realVars
Set of real valued observabeles.
std::unique_ptr< RooAbsArg > _funcValStore
RRV storing function value in context.
bool _isValid
Valid flag.
RooArgSet _cloneSet
Set owning clone of input function.
RooAbsReal * _funcClone
Pointer to top level node of cloned function.
virtual ~RooAbsNumGenerator()
RooRealVar * _funcValPtr
RRV storing function value in output dataset.
void attachParameters(const RooArgSet &vars)
Reattach original parameters to function clone.
std::unique_ptr< RooDataSet > _cache
Dataset holding generared values of observables.
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.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
RooFit::OwningPtr< 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
Object to represent discrete states.
Definition RooCategory.h:28
static void softAbort()
Soft abort function that interrupts macro execution but doesn't kill ROOT.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47