Logo ROOT  
Reference Guide
 
Loading...
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 *
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/** \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
39
40#include "TError.h"
41#include <vector>
42
44
45////////////////////////////////////////////////////////////////////////////////
46/// Create a polynomial in the variable `x`.
47/// \param[in] name Name of the PDF
48/// \param[in] title Title for plotting the PDF
49/// \param[in] x The variable of the polynomial
50/// \param[in] coefList The coefficients \f$ a_i \f$
51/// \param[in] lowestOrder [optional] Truncate the sum such that it skips the lower orders:
52/// \f[
53/// 1. + \sum_{i=0}^{\mathrm{coefList.size()}} a_{i} * x^{(i + \mathrm{lowestOrder})}
54/// \f]
55///
56/// This means that
57/// \code{.cpp}
58/// RooPolynomial pol("pol", "pol", x, RooArgList(a, b), lowestOrder = 2)
59/// \endcode
60/// computes
61/// \f[
62/// \mathrm{pol}(x) = 1 * x^0 + (0 * x^{\ldots}) + a * x^2 + b * x^3.
63/// \f]
64
65RooPolynomial::RooPolynomial(const char *name, const char *title, RooAbsReal &x, const RooArgList &coefList,
66 Int_t lowestOrder)
67 : RooAbsPdf(name, title),
68 _x("x", "Dependent", this, x),
69 _coefList("coefList", "List of coefficients", this),
70 _lowestOrder(lowestOrder)
71{
72 // Check lowest order
73 if (_lowestOrder < 0) {
74 coutE(InputArguments) << "RooPolynomial::ctor(" << GetName()
75 << ") WARNING: lowestOrder must be >=0, setting value to 0" << std::endl;
76 _lowestOrder = 0;
77 }
78
79 for (auto *coef : coefList) {
80 if (!dynamic_cast<RooAbsReal *>(coef)) {
81 coutE(InputArguments) << "RooPolynomial::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
82 << " is not of type RooAbsReal" << std::endl;
83 R__ASSERT(0);
84 }
85 _coefList.add(*coef);
86 }
87}
88
89////////////////////////////////////////////////////////////////////////////////
90
91RooPolynomial::RooPolynomial(const char *name, const char *title, RooAbsReal &x)
92 : RooAbsPdf(name, title),
93 _x("x", "Dependent", this, x),
94 _coefList("coefList", "List of coefficients", this),
95 _lowestOrder(1)
96{
97}
98
99////////////////////////////////////////////////////////////////////////////////
100/// Copy constructor
101
103 : RooAbsPdf(other, name),
104 _x("x", this, other._x),
105 _coefList("coefList", this, other._coefList),
106 _lowestOrder(other._lowestOrder)
107{
108}
109
110////////////////////////////////////////////////////////////////////////////////
111
113{
114 const unsigned sz = _coefList.getSize();
115 if (!sz)
116 return _lowestOrder ? 1. : 0.;
117
119
120 return RooFit::Detail::EvaluateFuncs::polynomialEvaluate<true>(_wksp.data(), sz, _lowestOrder, _x);
121}
122
124{
125 const unsigned sz = _coefList.size();
126 if (!sz) {
127 ctx.addResult(this, std::to_string((_lowestOrder ? 1. : 0.)));
128 return;
129 }
130
131 ctx.addResult(
132 this, ctx.buildCall("RooFit::Detail::EvaluateFuncs::polynomialEvaluate<true>", _coefList, sz, _lowestOrder, _x));
133}
134
135/// Compute multiple values of Polynomial.
136void RooPolynomial::computeBatch(double *output, size_t nEvents,
137 RooFit::Detail::DataMap const &dataMap) const
138{
139 return RooPolyVar::computeBatchImpl(this, output, nEvents, dataMap, _x.arg(), _coefList, _lowestOrder);
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Advertise to RooFit that this function can be analytically integrated.
144Int_t RooPolynomial::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
145{
146 if (matchArgs(allVars, analVars, _x))
147 return 1;
148 return 0;
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/// Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
153double RooPolynomial::analyticalIntegral(Int_t code, const char *rangeName) const
154{
155 R__ASSERT(code == 1);
156
157 const double xmin = _x.min(rangeName), xmax = _x.max(rangeName);
158 const unsigned sz = _coefList.getSize();
159 if (!sz)
160 return _lowestOrder ? xmax - xmin : 0.0;
161
163
164 return RooFit::Detail::AnalyticalIntegrals::polynomialIntegral<true>(_wksp.data(), sz, _lowestOrder, xmin, xmax);
165}
166
167std::string RooPolynomial::buildCallToAnalyticIntegral(Int_t /* code */, const char *rangeName,
169{
170 const double xmin = _x.min(rangeName), xmax = _x.max(rangeName);
171 const unsigned sz = _coefList.getSize();
172 if (!sz)
173 return std::to_string(_lowestOrder ? xmax - xmin : 0.0);
174
175 return ctx.buildCall("RooFit::Detail::AnalyticalIntegrals::polynomialIntegral<true>", _coefList, sz, _lowestOrder,
176 xmin, xmax);
177}
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
#define R__ASSERT(e)
Definition TError.h:118
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
Int_t getSize() const
Return the number of elements in the collection.
Storage_t::size_type size() const
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: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...
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 computeBatchImpl(RooAbsArg const *caller, double *output, size_t nEvents, RooFit::Detail::DataMap const &, RooAbsReal const &x, RooArgList const &coefs, int lowestOrder)
RooPolynomial implements a polynomial p.d.f of the form.
void computeBatch(double *output, size_t nEvents, RooFit::Detail::DataMap const &) const override
Compute multiple values of Polynomial.
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.
RooArgList const & coefList() const
Get the coefficient list.
double max(const char *rname=nullptr) const
Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
const T & arg() const
Return reference to object held in proxy.
double min(const char *rname=nullptr) const
Query lower limit of range. This requires the payload to be RooAbsRealLValue or derived.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Double_t x[n]
Definition legend1.C:17
static void output()