ROOT   Reference Guide
Searching...
No Matches
RooPolynomial.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitModels *
4 * @(#)root/roofit:$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 *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
15 *****************************************************************************/
16
17/** \class RooPolynomial
18 \ingroup Roofit
19
20RooPolynomial implements a polynomial p.d.f of the form
21\f[ f(x) = \mathcal{N} \cdot \sum_{i} a_{i} * x^i \f]
22By default, the coefficient \f$a_0 \f$ is chosen to be 1, as polynomial
23probability density functions have one degree of freedom
24less than polynomial functions due to the normalisation condition. \f$\mathcal{N} \f$
25is a normalisation constant that is automatically calculated when the polynomial is used
26in computations.
27
28The sum can be truncated at the low end. See the main constructor
29RooPolynomial::RooPolynomial(const char*, const char*, RooAbsReal&, const RooArgList&, Int_t)
30**/
31
32#include "RooPolynomial.h"
33#include "RooArgList.h"
34#include "RooMsgService.h"
35#include "RooPolyVar.h"
36
38
39#include "TError.h"
40#include <vector>
41
43
44////////////////////////////////////////////////////////////////////////////////
45/// Create a polynomial in the variable x.
46/// \param[in] name Name of the PDF
47/// \param[in] title Title for plotting the PDF
48/// \param[in] x The variable of the polynomial
49/// \param[in] coefList The coefficients \f$a_i \f$
50/// \param[in] lowestOrder [optional] Truncate the sum such that it skips the lower orders:
51/// \f[
52/// 1. + \sum_{i=0}^{\mathrm{coefList.size()}} a_{i} * x^{(i + \mathrm{lowestOrder})}
53/// \f]
54///
55/// This means that
56/// \code{.cpp}
57/// RooPolynomial pol("pol", "pol", x, RooArgList(a, b), lowestOrder = 2)
58/// \endcode
59/// computes
60/// \f[
61/// \mathrm{pol}(x) = 1 * x^0 + (0 * x^{\ldots}) + a * x^2 + b * x^3.
62/// \f]
63
64RooPolynomial::RooPolynomial(const char *name, const char *title, RooAbsReal &x, const RooArgList &coefList,
65 Int_t lowestOrder)
66 : RooAbsPdf(name, title),
67 _x("x", "Dependent", this, x),
68 _coefList("coefList", "List of coefficients", this),
69 _lowestOrder(lowestOrder)
70{
71 // Check lowest order
72 if (_lowestOrder < 0) {
73 coutE(InputArguments) << "RooPolynomial::ctor(" << GetName()
74 << ") WARNING: lowestOrder must be >=0, setting value to 0" << std::endl;
75 _lowestOrder = 0;
76 }
77
79}
80
81////////////////////////////////////////////////////////////////////////////////
82
83RooPolynomial::RooPolynomial(const char *name, const char *title, RooAbsReal &x)
84 : RooAbsPdf(name, title),
85 _x("x", "Dependent", this, x),
86 _coefList("coefList", "List of coefficients", this)
87{
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Copy constructor
92
94 : RooAbsPdf(other, name),
95 _x("x", this, other._x),
96 _coefList("coefList", this, other._coefList),
97 _lowestOrder(other._lowestOrder)
98{
99}
100
101////////////////////////////////////////////////////////////////////////////////
102
104{
105 const unsigned sz = _coefList.size();
106 if (!sz)
107 return _lowestOrder ? 1. : 0.;
108
110
111 return RooFit::Detail::MathFuncs::polynomial<true>(_wksp.data(), sz, _lowestOrder, _x);
112}
113
115{
116 const unsigned sz = _coefList.size();
117 if (!sz) {
118 ctx.addResult(this, std::to_string((_lowestOrder ? 1. : 0.)));
119 return;
120 }
121
122 ctx.addResult(this, ctx.buildCall("RooFit::Detail::MathFuncs::polynomial<true>", _coefList, sz, _lowestOrder, _x));
123}
124
125/// Compute multiple values of Polynomial.
127{
128 return RooPolyVar::doEvalImpl(this, ctx, _x.arg(), _coefList, _lowestOrder);
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// Advertise to RooFit that this function can be analytically integrated.
133Int_t RooPolynomial::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
134{
135 return matchArgs(allVars, analVars, _x);
136}
137
138////////////////////////////////////////////////////////////////////////////////
139/// Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
140double RooPolynomial::analyticalIntegral(Int_t code, const char *rangeName) const
141{
142 R__ASSERT(code == 1);
143
144 const double xmin = _x.min(rangeName);
145 const double xmax = _x.max(rangeName);
146 const unsigned sz = _coefList.size();
147 if (!sz)
148 return _lowestOrder ? xmax - xmin : 0.0;
149
151
152 return RooFit::Detail::MathFuncs::polynomialIntegral<true>(_wksp.data(), sz, _lowestOrder, xmin, xmax);
153}
154
155std::string RooPolynomial::buildCallToAnalyticIntegral(Int_t /* code */, const char *rangeName,
157{
158 const double xmin = _x.min(rangeName);
159 const double xmax = _x.max(rangeName);
160 const unsigned sz = _coefList.size();
161 if (!sz)
162 return std::to_string(_lowestOrder ? xmax - xmin : 0.0);
163
164 return ctx.buildCall("RooFit::Detail::MathFuncs::polynomialIntegral<true>", _coefList, sz, _lowestOrder,
165 xmin, xmax);
166}
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
Storage_t::size_type size() const
bool addTyped(const RooAbsCollection &list, bool silent=false)
Adds elements of a given RooAbsCollection to the container if they match the specified type.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
bool matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
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
A class to maintain the context for squashing of RooFit models into code.
std::string buildCall(std::string const &funcname, Args_t const &...args)
Build the code to call the function with name funcname, passing some arguments.
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.
static void fillCoeffValues(std::vector< double > &wksp, RooListProxy const &coefList)
static void doEvalImpl(RooAbsArg const *caller, RooFit::EvalContext &, RooAbsReal const &x, RooArgList const &coefs, int lowestOrder)
RooPolynomial implements a polynomial p.d.f of the form.
double evaluate() const override
do not persist
RooRealProxy _x
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 ...
std::string buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override
This function defines the analytical integral translation for the class.
std::vector< double > _wksp
RooListProxy _coefList
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Advertise to RooFit that this function can be analytically integrated.
void doEval(RooFit::EvalContext &) const override
Compute multiple values of Polynomial.
RooArgList const & coefList() const
Get the coefficient list.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Double_t x[n]
Definition legend1.C:17