Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooAbsNumGenerator.cxx
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*****************************************************************************
4 * Project: RooFit *
5 * Package: RooFitCore *
6 * @(#)root/roofitcore:$Id$
7 * Authors: *
8 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
9 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
10 * *
11 * Copyright (c) 2000-2005, Regents of the University of California *
12 * and Stanford University. All rights reserved. *
13 * *
14 * Redistribution and use in source and binary forms, *
15 * with or without modification, are permitted according to the terms *
16 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
17 *****************************************************************************/
18
19/**
20\file RooAbsNumGenerator.cxx
21\class RooAbsNumGenerator
22\ingroup Roofitcore
23
24Abstract base class for MC event generator
25implementations like RooAcceptReject and RooFoam
26**/
27
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 "RooMsgService.h"
39#include "RooRealBinding.h"
40
41#include <cassert>
42
43using std::endl;
44
45RooAbsNumGenerator::RooAbsNumGenerator() = default;
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Initialize an accept-reject generator for the specified distribution function,
50/// which must be non-negative but does not need to be normalized over the
51/// variables to be generated, genVars. The function and its dependents are
52/// cloned and so will not be disturbed during the generation process.
53
54RooAbsNumGenerator::RooAbsNumGenerator(const RooAbsReal &func, const RooArgSet &genVars, bool verbose, const RooAbsReal* maxFuncVal) :
56{
57 // Clone the function and all nodes that it depends on so that this generator
58 // is independent of any existing objects.
59 RooArgSet nodes(func,func.GetName());
60 if (nodes.snapshot(_cloneSet, true)) {
61 oocoutE(nullptr, Generation) << "RooAbsNumGenerator::RooAbsNumGenerator(" << func.GetName() << ") Couldn't deep-clone function, abort," << std::endl ;
63 }
64
65 // Find the clone in the snapshot list
66 _funcClone = static_cast<RooAbsReal*>(_cloneSet.find(func.GetName()));
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 = nullptr;
74 for (RooAbsArg const* arg : genVars) {
75 if(!arg->isFundamental()) {
76 oocoutE(nullptr, Generation) << func.GetName() << "::RooAbsNumGenerator: cannot generate values for derived \""
77 << arg->GetName() << "\"" << std::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(nullptr != 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(nullptr != catVar) {
94 _catVars.add(*catVar);
95 }
96 else if(nullptr != realVar) {
97 if(realVar->hasMin() && realVar->hasMax()) {
98 _realVars.add(*realVar);
99 }
100 else {
101 oocoutE(nullptr, Generation) << func.GetName() << "::RooAbsNumGenerator: cannot generate values for \""
102 << realVar->GetName() << "\" with unbound range" << std::endl;
103 _isValid= false;
104 }
105 }
106 else {
107 oocoutE(nullptr, Generation) << func.GetName() << "::RooAbsNumGenerator" << ": cannot generate values for \""
108 << arg->GetName() << "\" with unexpected type" << std::endl;
109 _isValid= false;
110 }
111 }
112 if(!_isValid) {
113 oocoutE(nullptr, Generation) << func.GetName() << "::RooAbsNumGenerator" << ": constructor failed with errors" << std::endl;
114 return;
115 }
116
117 // create a fundamental type for storing function values
118 _funcValStore= std::unique_ptr<RooAbsArg>{_funcClone->createFundamental()};
119
120 // create a new dataset to cache trial events and function values
121 RooArgSet cacheArgs(_catVars);
122 cacheArgs.add(_realVars);
123 cacheArgs.add(*_funcValStore);
124 _cache= std::make_unique<RooDataSet>("cache","Accept-Reject Event Cache",cacheArgs);
125
126 // attach our function clone to the cache dataset
127 const RooArgSet *cacheVars= _cache->get();
128 assert(nullptr != 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
137 _funcValPtr= static_cast<RooRealVar*>(dataVars->find(_funcValStore->GetName()));
138
139}
140
141
142RooAbsNumGenerator::~RooAbsNumGenerator() = default;
143
144
145////////////////////////////////////////////////////////////////////////////////
146/// Reattach original parameters to function clone
147
148void RooAbsNumGenerator::attachParameters(const RooArgSet& vars)
149{
150 RooArgSet newParams(vars) ;
151 newParams.remove(*_cache->get(),true,true) ;
152 _funcClone->recursiveRedirectServers(newParams) ;
153}
154
155/// \endcond
#define oocoutE(o, a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
Storage_t const & get() const
Const access to the underlying stl container.
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
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:49