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. The expression syntax is the same as for RooFormulaVar; see its
27class documentation.
28**/
29
30#include "RooGenericPdf.h"
31#include "RooMsgService.h"
32#include "RooArgList.h"
33#include "RooFormulaUtils.h"
34#include "RooAbsRealLValue.h"
35
36#include "TFormula.h"
37
38#include <iostream>
39using std::istream, std::ostream, std::endl;
40
41
43
45
46////////////////////////////////////////////////////////////////////////////////
47/// Constructor with formula expression and list of input variables
48
49RooGenericPdf::RooGenericPdf(const char *name, const char *title, const RooArgList &dependents)
50 : RooGenericPdf(name, title, title, dependents)
51{
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Constructor with a name, title, formula expression and a list of variables
56
57RooGenericPdf::RooGenericPdf(const char *name, const char *title,
58 const char* inFormula, const RooArgList& dependents) :
59 RooAbsPdf(name,title),
60 _actualVars("actualVars","Variables used by PDF expression",this),
61 _formExpr(inFormula)
62{
63 if (dependents.empty()) {
64 _value = traceEval(nullptr);
65 } else {
66 RooFormulaUtils::initFormula(_evaluator, _formExpr, _actualVars, dependents, GetName());
67 }
68}
69
70
71
72////////////////////////////////////////////////////////////////////////////////
73/// Copy constructor
74
77 _actualVars("actualVars",this,other._actualVars),
78 _formExpr(other._formExpr)
79{
80 _binnings = RooFormulaUtils::cloneBinnings(other._binnings);
81 if (other._evaluator) {
82 _evaluator = RooFormulaUtils::cloneEvaluator(*other._evaluator, GetName());
83 }
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Return reference to the formula evaluation engine.
88/// If it doesn't exist, create it on the fly. Throws if the formula is invalid.
89
91{
92 return RooFormulaUtils::ensureEvaluator(_evaluator, const_cast<TString &>(_formExpr), _actualVars, GetName());
93}
94
95////////////////////////////////////////////////////////////////////////////////
96/// Declare that this pdf is piecewise constant (flat) within the bins of the
97/// given `binning` of the observable `obs`, which must be one of the formula
98/// variables. The method can be called several times to set a binning for more
99/// than one observable. Use a RooUniformBinning to describe many uniform bins
100/// compactly.
101///
102/// Once set, integrals over `obs` use the fast bin integrator (which sums the
103/// central value of each bin times the bin width) instead of the generic
104/// numeric integrator, and plotting samples the step shape exactly.
105///
106/// If `checkFlatness` is true (the default), the function is sampled at several
107/// points inside each bin to verify that it is indeed flat; if it is not, an
108/// error is issued and the binning is not stored.
109
111{
112 RooFormulaUtils::setBinning(_binnings, *this, _actualVars, _formExpr.Data(), obs, binning, checkFlatness);
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Return the binning previously declared with setBinning() for observable
117/// `obs`, or nullptr if no binning was declared. The observable is matched to a
118/// formula variable by name, consistently with setBinning().
119
121{
122 return RooFormulaUtils::getBinning(_binnings, _actualVars, obs);
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// Remove a binning previously declared with setBinning() for observable `obs`,
127/// reverting to the generic numeric integrator for it. Returns true if a binning
128/// was removed, false if none was set for `obs`.
129
131{
132 return _binnings.erase(_actualVars.index(obs.GetName())) > 0;
133}
134
135////////////////////////////////////////////////////////////////////////////////
136/// Return true if a binning was set with setBinning() for every
137/// observable in the integration set `obs`.
138
140{
141 return RooFormulaUtils::isBinnedDistribution(_binnings, _actualVars, obs);
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Return the boundaries of the binning set with setBinning() that fall
146/// within [xlo, xhi], or a null pointer if no binning was set for this observable.
147
148std::list<double> *RooGenericPdf::binBoundaries(RooAbsRealLValue &obs, double xlo, double xhi) const
149{
150 return RooFormulaUtils::binBoundaries(_binnings, _actualVars, obs, xlo, xhi);
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Return sampling hints that draw the piecewise-flat shape exactly (a pair of
155/// points just left and right of every bin boundary), or a null pointer if no
156/// binning was set for this observable.
157
158std::list<double> *RooGenericPdf::plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const
159{
160 return RooFormulaUtils::plotSamplingHint(_binnings, _actualVars, obs, xlo, xhi);
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Calculate current value of this object
165
167{
168 return RooFormulaUtils::evalFormula(evaluator(), _actualVars, _actualVars.nset());
169}
170
171
172////////////////////////////////////////////////////////////////////////////////
174{
175 RooFormulaUtils::doEvalFormula(evaluator(), _actualVars, ctx);
176}
177
178////////////////////////////////////////////////////////////////////////////////
179/// Print info about this object to the specified stream.
180
181void RooGenericPdf::printMultiline(ostream& os, Int_t content, bool verbose, TString indent) const
182{
184 if (verbose) {
185 os << " --- RooGenericPdf --- " << std::endl ;
186 indent.Append(" ");
187 os << indent ;
188 RooFormulaUtils::printFormula(os, indent, _formExpr.Data(), _actualVars);
189 }
190}
191
192
193
194////////////////////////////////////////////////////////////////////////////////
195/// Add formula expression as meta argument in printing interface
196
197void RooGenericPdf::printMetaArgs(ostream& os) const
198{
199 os << "formula=\"" << _formExpr << "\" " ;
200}
201
203{
204 RooFormulaUtils::printFormula(std::cout, "", _formExpr.Data(), _actualVars);
205}
206
207////////////////////////////////////////////////////////////////////////////////
208/// Read object contents from given stream
209
210bool RooGenericPdf::readFromStream(istream& /*is*/, bool /*compact*/, bool /*verbose*/)
211{
212 coutE(InputArguments) << "RooGenericPdf::readFromStream(" << GetName() << "): can't read" << std::endl;
213 return true;
214}
215
216
217////////////////////////////////////////////////////////////////////////////////
218/// Write object contents to given stream
219
220void RooGenericPdf::writeToStream(ostream& os, bool compact) const
221{
222 if (compact) {
223 os << getVal() << std::endl ;
224 } else {
225 os << GetTitle() ;
226 }
227}
228
230{
231 return evaluator().getTFormula()->GetUniqueFuncName().Data();
232}
#define coutE(a)
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition TGX11.cxx:142
Abstract base class for RooRealVar binning definitions.
Int_t index(const RooAbsArg *arg) const
Returns index of given arg, or -1 if arg is not in the collection.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
void printMultiline(std::ostream &os, Int_t contents, bool verbose=false, TString indent="") const override
Print multi line detailed information of this RooAbsPdf.
const RooArgSet * nset() const
Definition RooAbsProxy.h:52
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:107
double _value
Cache for current value of object.
Definition RooAbsReal.h:541
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:24
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
const RooAbsBinning * getBinning(const RooAbsRealLValue &obs) const
Return the binning previously declared with setBinning() for observable obs, or nullptr if no binning...
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.
bool isBinnedDistribution(const RooArgSet &obs) const override
Return true if a binning was set with setBinning() for every observable in the integration set obs.
std::string getUniqueFuncName() const
RooListProxy _actualVars
void writeToStream(std::ostream &os, bool compact) const override
Write object contents to given stream.
std::map< int, std::unique_ptr< RooAbsBinning > > _binnings
User-defined binnings, keyed by the observable's index in _actualVars, for a piecewise-flat distribut...
std::list< double > * plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const override
Return sampling hints that draw the piecewise-flat shape exactly (a pair of points just left and righ...
std::list< double > * binBoundaries(RooAbsRealLValue &obs, double xlo, double xhi) const override
Return the boundaries of the binning set with setBinning() that fall within [xlo, xhi],...
bool removeBinning(const RooAbsRealLValue &obs)
Remove a binning previously declared with setBinning() for observable obs, reverting to the generic n...
RooFormulaEvaluator & evaluator() const
Return reference to the formula evaluation engine.
std::unique_ptr< RooFormulaEvaluator > _evaluator
! Formula evaluation engine
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.
void setBinning(const RooAbsRealLValue &obs, const RooAbsBinning &binning, bool checkFlatness=true)
Declare that this pdf is piecewise constant (flat) within the bins of the given binning of the observ...
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
Basic string class.
Definition TString.h:137
const char * Data() const
Definition TString.h:385