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 "RooAbsNumGenerator.h"
29#include "RooAbsReal.h"
30#include "RooCategory.h"
31#include "RooRealVar.h"
32#include "RooDataSet.h"
33#include "RooErrorHandler.h"
34
35#include "RooMsgService.h"
36#include "RooRealBinding.h"
37
38#include <cassert>
39#include <ostream>
40
41using std::endl;
42
43RooAbsNumGenerator::RooAbsNumGenerator() = default;
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) :
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," << std::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() << "\"" << std::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" << std::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" << std::endl;
107 _isValid= false;
108 }
109 }
110 if(!_isValid) {
111 oocoutE(nullptr, Generation) << func.GetName() << "::RooAbsNumGenerator" << ": constructor failed with errors" << std::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
120 cacheArgs.add(_realVars);
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
140RooAbsNumGenerator::~RooAbsNumGenerator() = default;
141
142
143////////////////////////////////////////////////////////////////////////////////
144/// Reattach original parameters to function clone
145
146void RooAbsNumGenerator::attachParameters(const RooArgSet& vars)
147{
148 RooArgSet newParams(vars) ;
149 newParams.remove(*_cache->get(),true,true) ;
150 _funcClone->recursiveRedirectServers(newParams) ;
151}
152
153/// \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:76
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:63
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