ROOT   Reference Guide
Searching...
No Matches
RooBernstein.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 *
4 * Copyright (c) 2024, CERN
5 *
6 * Redistribution and use in source and binary forms,
7 * with or without modification, are permitted according to the terms
9 */
10
11/** \class RooBernstein
12 \ingroup Roofit
13
14Bernstein basis polynomials are positive-definite in the range [0,1].
15In this implementation, we extend [0,1] to be the range of the parameter.
16There are n+1 Bernstein basis polynomials of degree n:
17\f[
18 B_{i,n}(x) = \begin{pmatrix}n \\\ i \end{pmatrix} x^i \cdot (1-x)^{n-i}
19\f]
20Thus, by providing n coefficients that are positive-definite, there
21is a natural way to have well-behaved polynomial PDFs. For any n, the n+1 polynomials
22'form a partition of unity', i.e., they sum to one for all values of x.
23They can be used as a basis to span the space of polynomials with degree n or less:
24\f[
25 PDF(x, c_0, ..., c_n) = \mathcal{N} \cdot \sum_{i=0}^{n} c_i \cdot B_{i,n}(x).
26\f]
27By giving n+1 coefficients in the constructor, this class constructs the n+1
28polynomials of degree n, and sums them to form an element of the space of polynomials
29of degree n. \f$\mathcal{N} \f$ is a normalisation constant that takes care of the
30cases where the \f$c_i \f$ are not all equal to one.
31
33http://www.idav.ucdavis.edu/education/CAGDNotes/Bernstein-Polynomials.pdf
34**/
35
36#include <RooBernstein.h>
37#include <RooRealVar.h>
38#include <RooBatchCompute.h>
39
41
43
44RooBernstein::RooBernstein(const char *name, const char *title, RooAbsRealLValue &x, const RooArgList &coefList)
45 : RooAbsPdf(name, title), _x("x", "Dependent", this, x), _coefList("coefficients", "List of coefficients", this)
46{
48}
49
51 : RooAbsPdf(other, name),
52 _x("x", this, other._x),
53 _coefList("coefList", this, other._coefList),
54 _refRangeName{other._refRangeName},
55 _buffer{other._buffer}
56{
57}
58
59/// Force use of a given normalisation range.
60/// Needed for functions or PDFs (e.g. RooAddPdf) whose shape depends on the choice of normalisation.
61void RooBernstein::selectNormalizationRange(const char *rangeName, bool force)
62{
63 if (rangeName && (force || !_refRangeName.empty())) {
64 _refRangeName = rangeName;
65 }
66}
67
69{
70 _buffer.resize(_coefList.size() + 2); // will usually be a no-op because size stays the same
71 std::size_t n = _coefList.size();
72 for (std::size_t i = 0; i < n; ++i) {
73 _buffer[i] = static_cast<RooAbsReal &>(_coefList[i]).getVal();
74 }
75 std::tie(_buffer[n], _buffer[n + 1]) = _x->getRange(_refRangeName.empty() ? nullptr : _refRangeName.c_str());
76}
77
79{
80 fillBuffer();
82}
83
85{
86 fillBuffer();
87 ctx.addResult(this, ctx.buildCall("RooFit::Detail::MathFuncs::bernstein", _x, xmin(), xmax(), _coefList,
88 _coefList.size()));
89}
90
91/// Compute multiple values of Bernstein distribution.
93{
94 fillBuffer();
96}
97
98Int_t RooBernstein::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
99{
100 return matchArgs(allVars, analVars, _x) ? 1 : 0;
101}
102
103double RooBernstein::analyticalIntegral(Int_t /*code*/, const char *rangeName) const
104{
105 fillBuffer();
106 return RooFit::Detail::MathFuncs::bernsteinIntegral(_x.min(rangeName), _x.max(rangeName), xmin(), xmax(),
107 _buffer.data(), _coefList.size());
108}
109
110std::string RooBernstein::buildCallToAnalyticIntegral(Int_t /*code*/, const char *rangeName,
112{
113 fillBuffer(); // to get the right xmin() and xmax()
114 return ctx.buildCall("RooFit::Detail::MathFuncs::bernsteinIntegral", _x.min(rangeName), _x.max(rangeName),
115 xmin(), xmax(), _coefList, _coefList.size());
116}
#define ClassImp(name)
Definition Rtypes.h:377
char name[80]
Definition TGX11.cxx:110
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 that may appear on the left hand side of ...
std::pair< double, double > getRange(const char *name=nullptr) const
Get low and high bound of the variable.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
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:55
Bernstein basis polynomials are positive-definite in the range [0,1].
void selectNormalizationRange(const char *rangeName=nullptr, bool force=false) override
Force use of a given normalisation range.
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported.
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.
void fillBuffer() const
RooBernstein()=default
std::string _refRangeName
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral.
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
void doEval(RooFit::EvalContext &) const override
Compute multiple values of Bernstein distribution.
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 ...
double xmax() const
std::vector< double > _buffer
!
RooTemplateProxy< RooAbsRealLValue > _x
double xmin() const
RooListProxy _coefList
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.
std::span< double > output()
RooBatchCompute::Config config(RooAbsArg const *arg) const
double max(const char *rname=nullptr) const
Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
double min(const char *rname=nullptr) const
Query lower limit of range. This requires the payload to be RooAbsRealLValue or derived.
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
void compute(Config cfg, Computer comp, std::span< double > output, VarSpan vars, ArgSpan extraArgs={})
double bernsteinIntegral(double xlo, double xhi, double xmin, double xmax, double *coefs, int nCoefs)
Definition MathFuncs.h:673
double bernstein(double x, double xmin, double xmax, double *coefs, int nCoefs)
The caller needs to make sure that there is at least one coefficient.
Definition MathFuncs.h:48