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
22RooGenericPdf is a concrete implementation of a probability density function,
23which 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 "RunContext.h"
51
52using namespace std;
53
55
56
57
58////////////////////////////////////////////////////////////////////////////////
59/// Constructor with formula expression and list of input variables
60
61RooGenericPdf::RooGenericPdf(const char *name, const char *title, const RooArgList& dependents) :
62 RooAbsPdf(name,title),
63 _actualVars("actualVars","Variables used by PDF expression",this),
64 _formExpr(title)
65{
67 formula();
68
69 if (_actualVars.empty()) _value = traceEval(0) ;
70}
71
72
73
74////////////////////////////////////////////////////////////////////////////////
75/// Constructor with a name, title, formula expression and a list of variables
76
77RooGenericPdf::RooGenericPdf(const char *name, const char *title,
78 const char* inFormula, const RooArgList& dependents) :
79 RooAbsPdf(name,title),
80 _actualVars("actualVars","Variables used by PDF expression",this),
81 _formExpr(inFormula)
82{
84 formula();
85
86 if (_actualVars.empty()) _value = traceEval(0) ;
87}
88
89
90
91////////////////////////////////////////////////////////////////////////////////
92/// Copy constructor
93
95 RooAbsPdf(other, name),
96 _actualVars("actualVars",this,other._actualVars),
97 _formExpr(other._formExpr)
98{
99 formula();
100}
101
102
103////////////////////////////////////////////////////////////////////////////////
104
106{
107 if (!_formula) {
108 const_cast<std::unique_ptr<RooFormula>&>(_formula).reset(
110 const_cast<TString&>(_formExpr) = _formula->formulaString().c_str();
111 }
112 return *_formula ;
113}
114
115
116
117////////////////////////////////////////////////////////////////////////////////
118/// Calculate current value of this object
119
121{
122 return formula().eval(_actualVars.nset()) ;
123}
124
125
126////////////////////////////////////////////////////////////////////////////////
127/// Evaluate this formula for values found in inputData.
129 if (normSet != nullptr && normSet != _normSet)
130 throw std::logic_error("Got conflicting normSets");
131
132 auto results = formula().evaluateSpan(this, inputData, _normSet);
133 inputData.spans[this] = results;
134
135 return results;
136}
137
138////////////////////////////////////////////////////////////////////////////////
139void RooGenericPdf::computeBatch(cudaStream_t* stream, double* output, size_t nEvents, RooFit::Detail::DataMap const& dataMap) const
140{
141 formula().computeBatch(stream, output, nEvents, dataMap);
142}
143
144
145////////////////////////////////////////////////////////////////////////////////
146/// Change formula expression to given expression
147
148bool RooGenericPdf::setFormula(const char* inFormula)
149{
150 if (formula().reCompile(inFormula)) return true ;
151
152 _formExpr = inFormula ;
153 setValueDirty() ;
154 return false ;
155}
156
157
158
159////////////////////////////////////////////////////////////////////////////////
160/// Check if given value is valid
161
162bool RooGenericPdf::isValidReal(double /*value*/, bool /*printError*/) const
163{
164 return true ;
165}
166
167
168
169////////////////////////////////////////////////////////////////////////////////
170/// Propagate server changes to embedded formula object
171
172bool RooGenericPdf::redirectServersHook(const RooAbsCollection& newServerList, bool mustReplaceAll, bool nameChange, bool isRecursive)
173{
174 bool error = _formula ? _formula->changeDependents(newServerList,mustReplaceAll,nameChange) : true;
175 return error || RooAbsPdf::redirectServersHook(newServerList, mustReplaceAll, nameChange, isRecursive);
176}
177
178
179
180////////////////////////////////////////////////////////////////////////////////
181/// Print info about this object to the specified stream.
182
183void RooGenericPdf::printMultiline(ostream& os, Int_t content, bool verbose, TString indent) const
184{
185 RooAbsPdf::printMultiline(os,content,verbose,indent);
186 if (verbose) {
187 os << " --- RooGenericPdf --- " << endl ;
188 indent.Append(" ");
189 os << indent ;
190 formula().printMultiline(os,content,verbose,indent);
191 }
192}
193
194
195
196////////////////////////////////////////////////////////////////////////////////
197/// Add formula expression as meta argument in printing interface
198
199void RooGenericPdf::printMetaArgs(ostream& os) const
200{
201 os << "formula=\"" << _formExpr << "\" " ;
202}
203
204
205
206////////////////////////////////////////////////////////////////////////////////
207/// Read object contents from given stream
208
209bool RooGenericPdf::readFromStream(istream& is, bool compact, bool /*verbose*/)
210{
211 if (compact) {
212 coutE(InputArguments) << "RooGenericPdf::readFromStream(" << GetName() << "): can't read in compact mode" << endl ;
213 return true ;
214 } else {
215 RooStreamParser parser(is) ;
216 return setFormula(parser.readLine()) ;
217 }
218}
219
220
221////////////////////////////////////////////////////////////////////////////////
222/// Write object contents to given stream
223
224void RooGenericPdf::writeToStream(ostream& os, bool compact) const
225{
226 if (compact) {
227 os << getVal() << endl ;
228 } else {
229 os << GetTitle() ;
230 }
231}
232
233
234
#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
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition RooAbsArg.h:487
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
RooArgSet const * _normSet
Normalization integral (owned by _normMgr)
Definition RooAbsPdf.h:377
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:91
double _value
Cache for current value of object.
Definition RooAbsReal.h:509
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
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
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...
RooFormula internally uses ROOT's TFormula to compute user-defined expressions of RooAbsArgs.
Definition RooFormula.h:33
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
Printing interface.
void computeBatch(cudaStream_t *, double *output, size_t nEvents, RooFit::Detail::DataMap const &) const
RooSpan< double > evaluateSpan(const RooAbsReal *dataOwner, RooBatchCompute::RunContext &inputData, const RooArgSet *nset=nullptr) const
double eval(const RooArgSet *nset=nullptr) const
Evalute all parameters/observables, and then evaluate formula.
RooGenericPdf is a concrete implementation of a probability density function, which takes a RooArgLis...
double evaluate() const override
Calculate current value of this object.
RooSpan< double > evaluateSpan(RooBatchCompute::RunContext &inputData, const RooArgSet *normSet) const override
Evaluate this formula for values found in inputData.
bool setFormula(const char *formula)
Change formula expression to given expression.
bool readFromStream(std::istream &is, bool compact, bool verbose=false) override
Read object contents from given stream.
const RooArgList & dependents() const
bool isValidReal(double value, bool printError) const override
Check if given value is valid.
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 computeBatch(cudaStream_t *, double *output, size_t nEvents, RooFit::Detail::DataMap const &) const override
Base function for computing multiple values of a RooAbsReal.
std::unique_ptr< 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.
A simple container to hold a batch of data values.
Definition RooSpan.h:34
TString readLine()
Read an entire line from the stream and return as TString This method recognizes the use of '\' in th...
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:380
This struct enables passing computation data around between elements of a computation graph.
Definition RunContext.h:32
std::map< RooFit::Detail::DataKey, RooSpan< const double > > spans
Once an object has computed its value(s), the span pointing to the results is registered here.
Definition RunContext.h:53
static void output()