ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 \file RooAbsNumGenerator.cxx
19 \class RooAbsNumGenerator
20 \ingroup Roofitcore
21 
22 Class RooAbsNumGenerator is the abstract base class for MC event generator
23 implementations like RooAcceptReject and RooFoam
24 **/
25 
26 
27 #include "RooFit.h"
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 "TString.h"
39 #include "TIterator.h"
40 #include "RooMsgService.h"
41 #include "TClass.h"
42 #include "RooRealBinding.h"
43 
44 #include <assert.h>
45 
46 using namespace std;
47 
49  ;
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Initialize an accept-reject generator for the specified distribution function,
54 /// which must be non-negative but does not need to be normalized over the
55 /// variables to be generated, genVars. The function and its dependents are
56 /// cloned and so will not be disturbed during the generation process.
57 
59  TNamed(func), _cloneSet(0), _funcClone(0), _funcMaxVal(maxFuncVal), _verbose(verbose), _funcValStore(0), _funcValPtr(0), _cache(0)
60 {
61  // Clone the function and all nodes that it depends on so that this generator
62  // is independent of any existing objects.
63  RooArgSet nodes(func,func.GetName());
64  _cloneSet= (RooArgSet*) nodes.snapshot(kTRUE);
65  if (!_cloneSet) {
66  coutE(Generation) << "RooAbsNumGenerator::RooAbsNumGenerator(" << GetName() << ") Couldn't deep-clone function, abort," << endl ;
68  }
69 
70  // Find the clone in the snapshot list
71  _funcClone = (RooAbsReal*)_cloneSet->find(func.GetName());
72 
73 
74  // Check that each argument is fundamental, and separate them into
75  // sets of categories and reals. Check that the area of the generating
76  // space is finite.
77  _isValid= kTRUE;
78  TIterator *iterator= genVars.createIterator();
79  const RooAbsArg *found = 0;
80  const RooAbsArg *arg = 0;
81  while((arg= (const RooAbsArg*)iterator->Next())) {
82  if(!arg->isFundamental()) {
83  coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for derived \""
84  << arg->GetName() << "\"" << endl;
86  continue;
87  }
88  // look for this argument in the generating function's dependents
89  found= (const RooAbsArg*)_cloneSet->find(arg->GetName());
90  if(found) {
91  arg= found;
92  } else {
93  // clone any variables we generate that we haven't cloned already
94  arg= _cloneSet->addClone(*arg);
95  }
96  assert(0 != arg);
97  // is this argument a category or a real?
98  const RooCategory *catVar= dynamic_cast<const RooCategory*>(arg);
99  const RooRealVar *realVar= dynamic_cast<const RooRealVar*>(arg);
100  if(0 != catVar) {
101  _catVars.add(*catVar);
102  }
103  else if(0 != realVar) {
104  if(realVar->hasMin() && realVar->hasMax()) {
105  _realVars.add(*realVar);
106  }
107  else {
108  coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for \""
109  << realVar->GetName() << "\" with unbound range" << endl;
110  _isValid= kFALSE;
111  }
112  }
113  else {
114  coutE(Generation) << fName << "::" << ClassName() << ": cannot generate values for \""
115  << arg->GetName() << "\" with unexpected type" << endl;
116  _isValid= kFALSE;
117  }
118  }
119  delete iterator;
120  if(!_isValid) {
121  coutE(Generation) << fName << "::" << ClassName() << ": constructor failed with errors" << endl;
122  return;
123  }
124 
125  // create a fundamental type for storing function values
127  assert(0 != _funcValStore);
128 
129  // create a new dataset to cache trial events and function values
130  RooArgSet cacheArgs(_catVars);
131  cacheArgs.add(_realVars);
132  cacheArgs.add(*_funcValStore);
133  _cache= new RooDataSet("cache","Accept-Reject Event Cache",cacheArgs);
134  assert(0 != _cache);
135 
136  // attach our function clone to the cache dataset
137  const RooArgSet *cacheVars= _cache->get();
138  assert(0 != cacheVars);
140 
141  // update ours sets of category and real args to refer to the cache dataset
142  const RooArgSet *dataVars= _cache->get();
143  _catVars.replace(*dataVars);
144  _realVars.replace(*dataVars);
145 
146  // find the function value in the dataset
148 
149 }
150 
151 
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Destructor
155 
157 {
158  delete _cloneSet;
159  delete _cache ;
160  delete _funcValStore ;
161 }
162 
163 
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// Reattach original parameters to function clone
167 
169 {
170  RooArgSet newParams(vars) ;
171  newParams.remove(*_cache->get(),kTRUE,kTRUE) ;
173 }
174 
175 
176 
177 
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Print name of the generator
181 
182 void RooAbsNumGenerator::printName(ostream& os) const
183 {
184  os << GetName() ;
185 }
186 
187 
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Print the title of the generator
191 
192 void RooAbsNumGenerator::printTitle(ostream& os) const
193 {
194  os << GetTitle() ;
195 }
196 
197 
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// Print the class name of the generator
201 
202 void RooAbsNumGenerator::printClassName(ostream& os) const
203 {
204  os << IsA()->GetName() ;
205 }
206 
207 
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 /// Print the arguments of the generator
211 
212 void RooAbsNumGenerator::printArgs(ostream& os) const
213 {
214  os << "[ function=" << _funcClone->GetName() << " catobs=" << _catVars << " realobs=" << _realVars << " ]" ;
215 }
216 
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
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 represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
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
Class RooAbsNumGenerator is the abstract base class for MC event generator implementations like RooAc...
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
TString fName
Definition: TNamed.h:36
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:25
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:1088
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