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 namespace std;
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////////////////////////////////////////////////////////////////////////////////
138void RooGenericPdf::computeBatch(double* output, size_t nEvents, RooFit::Detail::DataMap const& dataMap) const
139{
140 formula().computeBatch(output, nEvents, dataMap);
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{
207 // If the number of elements to sum is less than 3, just build a sum expression.
208 // Otherwise build a loop to sum over the values.
209 unsigned int eleSize = _actualVars.size();
210 std::string className = GetName();
211 std::string varName = "elements" + className;
212 std::string sumName = "sum" + className;
213 std::string code;
214 std::string decl = "double " + varName + "[" + std::to_string(eleSize) + "]{";
215 int idx = 0;
216 for (RooAbsArg *it : _actualVars) {
217 decl += ctx.getResult(*it) + ",";
218 ctx.addResult(it, varName + "[" + std::to_string(idx) + "]");
219 idx++;
220 }
221 decl.back() = '}';
222 code += decl + ";\n";
223
224 ctx.addResult(this, (_formula->getTFormula()->GetUniqueFuncName() + "(" + varName + ")").Data());
225 ctx.addToCodeBody(this, code);
226}
227
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition TGX11.cxx:110
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
Abstract container object that can hold multiple RooAbsArg objects.
Storage_t::size_type size() const
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:543
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...
A class to maintain the context for squashing of RooFit models into code.
void addResult(RooAbsArg const *key, std::string const &value)
A function to save an expression that includes/depends on the result of the input node.
void addToCodeBody(RooAbsArg const *klass, std::string const &in)
Adds the input string to the squashed code body.
std::string const & getResult(RooAbsArg const &arg)
Gets the result for the given node using the node name.
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 computeBatch(double *output, size_t nEvents, RooFit::Detail::DataMap const &) const
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.
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.
RooListProxy _actualVars
void writeToStream(std::ostream &os, bool compact) const override
Write object contents to given stream.
void translate(RooFit::Detail::CodeSquashContext &ctx) const override
This function defines a translation for each RooAbsReal based object that can be used to express the ...
void computeBatch(double *output, size_t nEvents, RooFit::Detail::DataMap const &) const override
Base function for computing multiple values of a RooAbsReal.
RooFormula * _formula
! Formula engine
RooFormula & formula() const
TString _formExpr
Formula expression string.
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:378
static void output()