Loading [MathJax]/extensions/tex2jax.js
ROOT  6.06/09
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 //
19 // BEGIN_HTML
20 // Class RooAbsNumGenerator is the abstract base class for MC event generator
21 // implementations like RooAcceptReject and RooFoam
22 // END_HTML
23 //
24 
25 
26 #include "RooFit.h"
27 #include "Riostream.h"
28 
29 #include "RooAbsNumGenerator.h"
30 #include "RooAbsReal.h"
31 #include "RooCategory.h"
32 #include "RooRealVar.h"
33 #include "RooDataSet.h"
34 #include "RooRandom.h"
35 #include "RooErrorHandler.h"
36 
37 #include "TString.h"
38 #include "TIterator.h"
39 #include "RooMsgService.h"
40 #include "TClass.h"
41 #include "RooRealBinding.h"
42 
43 #include <assert.h>
44 
45 using namespace std;
46 
48  ;
49 
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Initialize an accept-reject generator for the specified distribution function,
53 /// which must be non-negative but does not need to be normalized over the
54 /// variables to be generated, genVars. The function and its dependents are
55 /// cloned and so will not be disturbed during the generation process.
56 
58  TNamed(func), _cloneSet(0), _funcClone(0), _funcMaxVal(maxFuncVal), _verbose(verbose), _funcValStore(0), _funcValPtr(0), _cache(0)
59 {
60  // Clone the function and all nodes that it depends on so that this generator
61  // is independent of any existing objects.
62  RooArgSet nodes(func,func.GetName());
63  _cloneSet= (RooArgSet*) nodes.snapshot(kTRUE);
64  if (!_cloneSet) {
65  coutE(Generation) << "RooAbsNumGenerator::RooAbsNumGenerator(" << GetName() << ") Couldn't deep-clone function, abort," << endl ;
67  }
68 
69  // Find the clone in the snapshot list
70  _funcClone = (RooAbsReal*)_cloneSet->find(func.GetName());
71 
72 
73  // Check that each argument is fundamental, and separate them into
74  // sets of categories and reals. Check that the area of the generating
75  // space is finite.
76  _isValid= kTRUE;
77  TIterator *iterator= genVars.createIterator();
78  const RooAbsArg *found = 0;
79  const RooAbsArg *arg = 0;
80  while((arg= (const RooAbsArg*)iterator->Next())) {
81  if(!arg->isFundamental()) {
82  coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for derived \""
83  << arg->GetName() << "\"" << endl;
85  continue;
86  }
87  // look for this argument in the generating function's dependents
88  found= (const RooAbsArg*)_cloneSet->find(arg->GetName());
89  if(found) {
90  arg= found;
91  } else {
92  // clone any variables we generate that we haven't cloned already
93  arg= _cloneSet->addClone(*arg);
94  }
95  assert(0 != arg);
96  // is this argument a category or a real?
97  const RooCategory *catVar= dynamic_cast<const RooCategory*>(arg);
98  const RooRealVar *realVar= dynamic_cast<const RooRealVar*>(arg);
99  if(0 != catVar) {
100  _catVars.add(*catVar);
101  }
102  else if(0 != realVar) {
103  if(realVar->hasMin() && realVar->hasMax()) {
104  _realVars.add(*realVar);
105  }
106  else {
107  coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for \""
108  << realVar->GetName() << "\" with unbound range" << endl;
109  _isValid= kFALSE;
110  }
111  }
112  else {
113  coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for \""
114  << arg->GetName() << "\" with unexpected type" << endl;
115  _isValid= kFALSE;
116  }
117  }
118  delete iterator;
119  if(!_isValid) {
120  coutE(Generation) << fName << "::" << ClassName() << ": constructor failed with errors" << endl;
121  return;
122  }
123 
124  // create a fundamental type for storing function values
126  assert(0 != _funcValStore);
127 
128  // create a new dataset to cache trial events and function values
129  RooArgSet cacheArgs(_catVars);
130  cacheArgs.add(_realVars);
131  cacheArgs.add(*_funcValStore);
132  _cache= new RooDataSet("cache","Accept-Reject Event Cache",cacheArgs);
133  assert(0 != _cache);
134 
135  // attach our function clone to the cache dataset
136  const RooArgSet *cacheVars= _cache->get();
137  assert(0 != cacheVars);
139 
140  // update ours sets of category and real args to refer to the cache dataset
141  const RooArgSet *dataVars= _cache->get();
142  _catVars.replace(*dataVars);
143  _realVars.replace(*dataVars);
144 
145  // find the function value in the dataset
147 
148 }
149 
150 
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// Destructor
154 
156 {
157  delete _cloneSet;
158  delete _cache ;
159  delete _funcValStore ;
160 }
161 
162 
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Reattach original parameters to function clone
166 
168 {
169  RooArgSet newParams(vars) ;
170  newParams.remove(*_cache->get(),kTRUE,kTRUE) ;
172 }
173 
174 
175 
176 
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Print name of the generator
180 
181 void RooAbsNumGenerator::printName(ostream& os) const
182 {
183  os << GetName() ;
184 }
185 
186 
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 /// Print the title of the generator
190 
191 void RooAbsNumGenerator::printTitle(ostream& os) const
192 {
193  os << GetTitle() ;
194 }
195 
196 
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Print the class name of the generator
200 
201 void RooAbsNumGenerator::printClassName(ostream& os) const
202 {
203  os << IsA()->GetName() ;
204 }
205 
206 
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Print the arguments of the generator
210 
211 void RooAbsNumGenerator::printArgs(ostream& os) const
212 {
213  os << "[ function=" << _funcClone->GetName() << " catobs=" << _catVars << " realobs=" << _realVars << " ]" ;
214 }
215 
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
virtual void printName(std::ostream &os) const
Print name of the generator.
#define coutE(a)
Definition: RooMsgService.h:35
virtual ~RooAbsNumGenerator()
Destructor.
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
static void softAbort()
virtual Bool_t replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return kTRUE for success.
#define assert(cond)
Definition: unittest.h:542
virtual Bool_t isFundamental() const
Definition: RooAbsArg.h:157
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
virtual void printArgs(std::ostream &os) const
Print the arguments of the generator.
ClassImp(RooAbsNumGenerator)
Iterator abstract base class.
Definition: TIterator.h:32
virtual void printTitle(std::ostream &os) const
Print the title of the generator.
RooAbsArg * createFundamental(const char *newname=0) const
Create a RooRealVar fundamental object with our properties.
Bool_t hasMax(const char *name=0) const
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
TIterator * createIterator(Bool_t dir=kIterForward) const
void attachParameters(const RooArgSet &vars)
Reattach original parameters to function clone.
RooRealVar * _funcValStore
RooAbsArg * find(const char *name) const
Find object with given name in list.
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:187
RooRealVar * _funcValPtr
TClass * IsA() const
bool verbose
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
TString fName
Definition: TNamed.h:36
Bool_t hasMin(const char *name=0) const
virtual void printClassName(std::ostream &os) const
Print the class name of the generator.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
double func(double *x, double *p)
Definition: stressTF1.cxx:213
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
virtual TObject * Next()=0
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Definition: RooAbsArg.cxx:1087
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event 'index'.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448