Logo ROOT  
Reference Guide
 
Loading...
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
8 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
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
32See also
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
84/// Compute multiple values of Bernstein distribution.
86{
87 fillBuffer();
89}
90
91Int_t RooBernstein::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
92{
93 return matchArgs(allVars, analVars, _x) ? 1 : 0;
94}
95
96double RooBernstein::analyticalIntegral(Int_t /*code*/, const char *rangeName) const
97{
98 fillBuffer();
99 return RooFit::Detail::MathFuncs::bernsteinIntegral(_x.min(rangeName), _x.max(rangeName), xmin(), xmax(),
100 _buffer.data(), _coefList.size());
101}
#define ClassImp(name)
Definition Rtypes.h:382
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:24
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.
void fillBuffer() const
RooBernstein()=default
RooArgList const & coefList() const
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.
double xmax() const
std::vector< double > _buffer
!
RooTemplateProxy< RooAbsRealLValue > _x
double xmin() const
RooListProxy _coefList
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:710
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