Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooGenericPdf.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 RooGenericPdf.cxx
19\class RooGenericPdf
20\ingroup Roofitcore
21
22Implementation of a probability density function
23that takes a RooArgList of servers and a C++ expression string defining how
24its value should be calculated from the given list of servers.
25A fully numerical integration is automatically performed to normalize the given
26expression. RooGenericPdf uses a RooFormula object to perform the expression evaluation.
27
28The string expression can be any valid TFormula expression referring to the
29listed servers either by name or by their ordinal list position. These three are
30equivalent:
31```
32 RooFormulaVar("gen", "x*y", RooArgList(x,y)) // reference by name
33 RooFormulaVar("gen", "@0*@1", RooArgList(x,y)) // reference by ordinal with @
34 RooFormulaVar("gen", "x[0]*x[1]", RooArgList(x,y)) // TFormula-builtin reference by ordinal
35```
36Note that `x[i]` is an expression reserved for TFormula. All variable references
37are automatically converted to the TFormula-native format. If a variable with
38the name `x` is given, the RooFormula interprets `x[i]` as a list position,
39but `x` without brackets as the name of a RooFit object.
40
41The last two versions, while slightly less readable, are more versatile because
42the names of the arguments are not hard coded.
43**/
44
45#include "RooGenericPdf.h"
46#include "Riostream.h"
47#include "RooStreamParser.h"
48#include "RooMsgService.h"
49#include "RooArgList.h"
50#include "RooFormula.h"
51
52using std::istream, std::ostream, std::endl;
53
55
57
59{
60 if(_formula) delete _formula;
61}
62
63
64////////////////////////////////////////////////////////////////////////////////
65/// Constructor with formula expression and list of input variables
66
67RooGenericPdf::RooGenericPdf(const char *name, const char *title, const RooArgList& dependents) :
68 RooAbsPdf(name,title),
69 _actualVars("actualVars","Variables used by PDF expression",this),
70 _formExpr(title)
71{
72 if (dependents.empty()) {
73 _value = traceEval(nullptr);
74 } else {
76 _formExpr = _formula->formulaString().c_str();
78 }
79}
80
81
82
83////////////////////////////////////////////////////////////////////////////////
84/// Constructor with a name, title, formula expression and a list of variables
85
86RooGenericPdf::RooGenericPdf(const char *name, const char *title,
87 const char* inFormula, const RooArgList& dependents) :
88 RooAbsPdf(name,title),
89 _actualVars("actualVars","Variables used by PDF expression",this),
90 _formExpr(inFormula)
91{
92 if (dependents.empty()) {
93 _value = traceEval(nullptr);
94 } else {
96 _formExpr = _formula->formulaString().c_str();
98 }
99}
100
101
102
103////////////////////////////////////////////////////////////////////////////////
104/// Copy constructor
105
107 RooAbsPdf(other, name),
108 _actualVars("actualVars",this,other._actualVars),
109 _formExpr(other._formExpr)
110{
111 formula();
112}
113
114
115////////////////////////////////////////////////////////////////////////////////
116
118{
119 if (!_formula) {
121 const_cast<TString&>(_formExpr) = _formula->formulaString().c_str();
122 }
123 return *_formula ;
124}
125
126
127
128////////////////////////////////////////////////////////////////////////////////
129/// Calculate current value of this object
130
132{
133 return formula().eval(_actualVars.nset()) ;
134}
135
136
137////////////////////////////////////////////////////////////////////////////////
139{
140 formula().doEval(ctx);
141}
142
143
144////////////////////////////////////////////////////////////////////////////////
145/// Propagate server changes to embedded formula object
146
147bool RooGenericPdf::redirectServersHook(const RooAbsCollection& newServerList, bool mustReplaceAll, bool nameChange, bool isRecursive)
148{
149 bool error = _formula ? _formula->changeDependents(newServerList,mustReplaceAll,nameChange) : true;
150 return error || RooAbsPdf::redirectServersHook(newServerList, mustReplaceAll, nameChange, isRecursive);
151}
152
153
154
155////////////////////////////////////////////////////////////////////////////////
156/// Print info about this object to the specified stream.
157
158void RooGenericPdf::printMultiline(ostream& os, Int_t content, bool verbose, TString indent) const
159{
160 RooAbsPdf::printMultiline(os,content,verbose,indent);
161 if (verbose) {
162 os << " --- RooGenericPdf --- " << endl ;
163 indent.Append(" ");
164 os << indent ;
165 formula().printMultiline(os,content,verbose,indent);
166 }
167}
168
169
170
171////////////////////////////////////////////////////////////////////////////////
172/// Add formula expression as meta argument in printing interface
173
174void RooGenericPdf::printMetaArgs(ostream& os) const
175{
176 os << "formula=\"" << _formExpr << "\" " ;
177}
178
179
181
182
183////////////////////////////////////////////////////////////////////////////////
184/// Read object contents from given stream
185
186bool RooGenericPdf::readFromStream(istream& /*is*/, bool /*compact*/, bool /*verbose*/)
187{
188 coutE(InputArguments) << "RooGenericPdf::readFromStream(" << GetName() << "): can't read" << std::endl;
189 return true;
190}
191
192
193////////////////////////////////////////////////////////////////////////////////
194/// Write object contents to given stream
195
196void RooGenericPdf::writeToStream(ostream& os, bool compact) const
197{
198 if (compact) {
199 os << getVal() << endl ;
200 } else {
201 os << GetTitle() ;
202 }
203}
204
206{
208}
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:382
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition TGX11.cxx:110
Abstract container object that can hold multiple RooAbsArg objects.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
Print multi line detailed information of this RooAbsPdf.
bool redirectServersHook(const RooAbsCollection &newServerList, bool mustReplaceAll, bool nameChange, bool isRecursiveStep) override
The cache manager.
const RooArgSet * nset() const
Definition RooAbsProxy.h:52
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
double _value
Cache for current value of object.
Definition RooAbsReal.h:535
double traceEval(const RooArgSet *set) const
Calculate current value of object, with error tracing wrapper.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
Internally uses ROOT's TFormula to compute user-defined expressions of RooAbsArgs.
Definition RooFormula.h:27
TFormula * getTFormula() const
Definition RooFormula.h:72
std::string formulaString() const
Definition RooFormula.h:71
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
Printing interface.
RooArgSet actualDependents() const
Return list of arguments which are used in the formula.
Definition RooFormula.h:39
void dump() const
DEBUG: Dump state information.
double eval(const RooArgSet *nset=nullptr) const
Evaluate all parameters/observables, and then evaluate formula.
bool changeDependents(const RooAbsCollection &newDeps, bool mustReplaceAll, bool nameChange)
Change used variables to those with the same name in given list.
void doEval(RooFit::EvalContext &) const
Implementation of a probability density function that takes a RooArgList of servers and a C++ express...
double evaluate() const override
Calculate current value of this object.
~RooGenericPdf() override
bool readFromStream(std::istream &is, bool compact, bool verbose=false) override
Read object contents from given stream.
const RooArgList & dependents() const
void printMetaArgs(std::ostream &os) const override
Add formula expression as meta argument in printing interface.
std::string getUniqueFuncName() const
RooListProxy _actualVars
void writeToStream(std::ostream &os, bool compact) const override
Write object contents to given stream.
RooFormula * _formula
! Formula engine
RooFormula & formula() const
TString _formExpr
Formula expression string.
void doEval(RooFit::EvalContext &) const override
Base function for computing multiple values of a RooAbsReal.
void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const override
Print info about this object to the specified stream.
bool redirectServersHook(const RooAbsCollection &newServerList, bool mustReplaceAll, bool nameChange, bool isRecursive) override
Propagate server changes to embedded formula object.
TString GetUniqueFuncName() const
Definition TFormula.h:253
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376