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