Logo ROOT   6.08/07
Reference Guide
RooFormulaVar.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 // RooFormulaVar is a generic implementation of a real valued object
20 // which takes a RooArgList of servers and a C++ expression string defining how
21 // its value should be calculated from the given list of servers.
22 // RooFormulaVar uses a RooFormula object to perform the expression evaluation.
23 //
24 // If RooAbsPdf objects are supplied to RooFormulaVar as servers, their
25 // raw (unnormalized) values will be evaluated. Use RooGenericPdf, which
26 // constructs generic PDF functions, to access their properly normalized
27 // values.
28 //
29 // The string expression can be any valid TFormula expression referring to the
30 // listed servers either by name or by their ordinal list position:
31 //
32 // RooFormulaVar("gen","x*y",RooArgList(x,y)) or
33 // RooFormulaVar("gen","@0*@1",RooArgList(x,y))
34 //
35 // The latter form, while slightly less readable, is more versatile because it
36 // doesn't hardcode any of the variable names it expects
37 //
38 
39 
40 #include "RooFit.h"
41 #include "Riostream.h"
42 
43 #include "RooFormulaVar.h"
44 #include "RooFormulaVar.h"
45 #include "RooStreamParser.h"
46 #include "RooNLLVar.h"
47 #include "RooChi2Var.h"
48 #include "RooMsgService.h"
49 #include "RooTrace.h"
50 
51 
52 using namespace std;
53 
55 
56 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Constructor with formula expression and list of input variables
60 
61 RooFormulaVar::RooFormulaVar(const char *name, const char *title, const char* inFormula, const RooArgList& dependents) :
62  RooAbsReal(name,title),
63  _actualVars("actualVars","Variables used by formula expression",this),
64  _formula(0), _formExpr(inFormula)
65 {
66  _actualVars.add(dependents) ;
67 
68  if (_actualVars.getSize()==0) _value = traceEval(0) ;
69 }
70 
71 
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// Constructor with formula expression, title and list of input variables
75 
76 RooFormulaVar::RooFormulaVar(const char *name, const char *title, const RooArgList& dependents) :
77  RooAbsReal(name,title),
78  _actualVars("actualVars","Variables used by formula expression",this),
79  _formula(0), _formExpr(title)
80 {
81  _actualVars.add(dependents) ;
82 
83  if (_actualVars.getSize()==0) _value = traceEval(0) ;
84 }
85 
86 
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// Copy constructor
90 
91 RooFormulaVar::RooFormulaVar(const RooFormulaVar& other, const char* name) :
92  RooAbsReal(other, name),
93  _actualVars("actualVars",this,other._actualVars),
94  _formula(0), _formExpr(other._formExpr)
95 {
96 }
97 
98 
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Destructor
102 
104 {
105  if (_formula) delete _formula ;
106 }
107 
108 
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Return reference to internal RooFormula object
112 
114 {
115  if (!_formula) {
117  }
118  return *_formula ;
119 }
120 
121 
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Calculate current value of object from internal formula
125 
127 {
128  return formula().eval(_lastNSet) ;
129 }
130 
131 
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Check if given value is valid
135 
136 Bool_t RooFormulaVar::isValidReal(Double_t /*value*/, Bool_t /*printError*/) const
137 {
138  return kTRUE ;
139 }
140 
141 
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Propagate server change information to embedded RooFormula object
145 
146 Bool_t RooFormulaVar::redirectServersHook(const RooAbsCollection& newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t /*isRecursive*/)
147 {
148  return formula().changeDependents(newServerList,mustReplaceAll,nameChange) ;
149 }
150 
151 
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Print info about this object to the specified stream.
155 
156 void RooFormulaVar::printMultiline(ostream& os, Int_t contents, Bool_t verbose, TString indent) const
157 {
158  RooAbsReal::printMultiline(os,contents,verbose,indent);
159  if(verbose) {
160  indent.Append(" ");
161  os << indent;
162  formula().printMultiline(os,contents,verbose,indent);
163  }
164 }
165 
166 
167 
168 ////////////////////////////////////////////////////////////////////////////////
169 /// Add formula expression as meta argument in printing interface
170 
171 void RooFormulaVar::printMetaArgs(ostream& os) const
172 {
173  os << "formula=\"" << _formExpr << "\" " ;
174 }
175 
176 
177 
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Read object contents from given stream
181 
182 Bool_t RooFormulaVar::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/)
183 {
184  coutE(InputArguments) << "RooFormulaVar::readFromStream(" << GetName() << "): can't read" << endl ;
185  return kTRUE ;
186 }
187 
188 
189 
190 ////////////////////////////////////////////////////////////////////////////////
191 /// Write object contents to given stream
192 
193 void RooFormulaVar::writeToStream(ostream& os, Bool_t compact) const
194 {
195  if (compact) {
196  cout << getVal() << endl ;
197  } else {
198  os << GetTitle() ;
199  }
200 }
201 
202 
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Forward the plot sampling hint from the p.d.f. that defines the observable obs
206 
207 std::list<Double_t>* RooFormulaVar::binBoundaries(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
208 {
209  RooFIter iter = _actualVars.fwdIterator() ;
210  RooAbsReal* func ;
211  while((func=(RooAbsReal*)iter.next())) {
212  list<Double_t>* binb = func->binBoundaries(obs,xlo,xhi) ;
213  if (binb) {
214  return binb ;
215  }
216  }
217 
218  return 0 ;
219 }
220 
221 
222 
223 ////////////////////////////////////////////////////////////////////////////////
224 /// Forward the plot sampling hint from the p.d.f. that defines the observable obs
225 
226 std::list<Double_t>* RooFormulaVar::plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
227 {
228  RooFIter iter = _actualVars.fwdIterator() ;
229  RooAbsReal* func ;
230  while((func=(RooAbsReal*)iter.next())) {
231  list<Double_t>* hint = func->plotSamplingHint(obs,xlo,xhi) ;
232  if (hint) {
233  return hint ;
234  }
235  }
236 
237  return 0 ;
238 }
239 
240 
241 
242 ////////////////////////////////////////////////////////////////////////////////
243 /// Return the default error level for MINUIT error analysis
244 /// If the formula contains one or more RooNLLVars and
245 /// no RooChi2Vars, return the defaultErrorLevel() of
246 /// RooNLLVar. If the addition contains one ore more RooChi2Vars
247 /// and no RooNLLVars, return the defaultErrorLevel() of
248 /// RooChi2Var. If the addition contains neither or both
249 /// issue a warning message and return a value of 1
250 
252 {
253  RooAbsReal* nllArg(0) ;
254  RooAbsReal* chi2Arg(0) ;
255 
257  RooAbsArg* arg ;
258  while((arg=(RooAbsArg*)iter->Next())) {
259  if (dynamic_cast<RooNLLVar*>(arg)) {
260  nllArg = (RooAbsReal*)arg ;
261  }
262  if (dynamic_cast<RooChi2Var*>(arg)) {
263  chi2Arg = (RooAbsReal*)arg ;
264  }
265  }
266  delete iter ;
267 
268  if (nllArg && !chi2Arg) {
269  coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName()
270  << ") Formula contains a RooNLLVar, using its error level" << endl ;
271  return nllArg->defaultErrorLevel() ;
272  } else if (chi2Arg && !nllArg) {
273  coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName()
274  << ") Formula contains a RooChi2Var, using its error level" << endl ;
275  return chi2Arg->defaultErrorLevel() ;
276  } else if (!nllArg && !chi2Arg) {
277  coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName() << ") WARNING: "
278  << "Formula contains neither RooNLLVar nor RooChi2Var server, using default level of 1.0" << endl ;
279  } else {
280  coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName() << ") WARNING: "
281  << "Formula contains BOTH RooNLLVar and RooChi2Var server, using default level of 1.0" << endl ;
282  }
283 
284  return 1.0 ;
285 }
286 
287 
288 
289 
TString _formExpr
Normalization set to be passed along to contents.
Definition: RooFormulaVar.h:78
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &, Double_t, Double_t) const
Forward the plot sampling hint from the p.d.f. that defines the observable obs.
TIterator * createIterator(Bool_t dir=kIterForward) const
#define coutE(a)
Definition: RooMsgService.h:35
virtual Bool_t redirectServersHook(const RooAbsCollection &newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t isRecursive)
Propagate server change information to embedded RooFormula object.
RooFormula * _formula
Definition: RooFormulaVar.h:76
#define coutI(a)
Definition: RooMsgService.h:32
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
virtual std::list< Double_t > * binBoundaries(RooAbsRealLValue &, Double_t, Double_t) const
Definition: RooAbsReal.h:278
Double_t traceEval(const RooArgSet *set) const
Calculate current value of object, with error tracing wrapper.
Definition: RooAbsReal.cxx:288
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Structure printing.
Definition: RooAbsReal.cxx:424
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
RooArgSet * _lastNSet
Definition: RooAbsReal.h:481
STL namespace.
Iterator abstract base class.
Definition: TIterator.h:32
void printMetaArgs(std::ostream &os) const
Add formula expression as meta argument in printing interface.
void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Printing interface.
Definition: RooFormula.cxx:414
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to given stream.
virtual Double_t defaultErrorLevel() const
Return the default error level for MINUIT error analysis If the formula contains one or more RooNLLVa...
RooFormula an implementation of ROOT::v5::TFormula that interfaces it to RooAbsArg value objects...
Definition: RooFormula.h:27
Double_t eval(const RooArgSet *nset=0)
Evaluate ROOT::v5::TFormula using given normalization set to be used as observables definition passed...
Definition: RooFormula.cxx:234
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
Int_t getSize() const
virtual ~RooFormulaVar()
Destructor.
RooListProxy _actualVars
Definition: RooFormulaVar.h:75
bool verbose
RooFormula & formula() const
Return reference to internal RooFormula object.
static void indent(ostringstream &buf, int indent_level)
RooAbsArg * next()
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
RooFIter fwdIterator() const
virtual Bool_t isValidReal(Double_t value, Bool_t printError) const
Check if given value is valid.
double func(double *x, double *p)
Definition: stressTF1.cxx:213
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &, Double_t, Double_t) const
Definition: RooAbsReal.h:279
Double_t _value
Definition: RooAbsReal.h:389
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects...
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Print info about this object to the specified stream.
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
virtual Double_t defaultErrorLevel() const
Definition: RooAbsReal.h:189
virtual TObject * Next()=0
Bool_t changeDependents(const RooAbsCollection &newDeps, Bool_t mustReplaceAll, Bool_t nameChange)
Change used variables to those with the same name in given list If mustReplaceAll is true and error i...
Definition: RooFormula.cxx:191
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual Double_t evaluate() const
Calculate current value of object from internal formula.
virtual std::list< Double_t > * binBoundaries(RooAbsRealLValue &, Double_t, Double_t) const
Forward the plot sampling hint from the p.d.f. that defines the observable obs.
char name[80]
Definition: TGX11.cxx:109
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52