Logo ROOT  
Reference Guide
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 "RooAbsReal.h"
34#include "RooArgList.h"
35#include "RooMsgService.h"
36#include "RooBatchCompute.h"
37
38#include "TError.h"
39#include <vector>
40using namespace std;
41
43
44////////////////////////////////////////////////////////////////////////////////
45/// coverity[UNINIT_CTOR]
46
48{
49}
50
51////////////////////////////////////////////////////////////////////////////////
52/// Create a polynomial in the variable `x`.
53/// \param[in] name Name of the PDF
54/// \param[in] title Title for plotting the PDF
55/// \param[in] x The variable of the polynomial
56/// \param[in] coefList The coefficients \f$ a_i \f$
57/// \param[in] lowestOrder [optional] Truncate the sum such that it skips the lower orders:
58/// \f[
59/// 1. + \sum_{i=0}^{\mathrm{coefList.size()}} a_{i} * x^{(i + \mathrm{lowestOrder})}
60/// \f]
61///
62/// This means that
63/// \code{.cpp}
64/// RooPolynomial pol("pol", "pol", x, RooArgList(a, b), lowestOrder = 2)
65/// \endcode
66/// computes
67/// \f[
68/// \mathrm{pol}(x) = 1 * x^0 + (0 * x^{\ldots}) + a * x^2 + b * x^3.
69/// \f]
70
71
72RooPolynomial::RooPolynomial(const char* name, const char* title,
73 RooAbsReal& x, const RooArgList& coefList, Int_t lowestOrder) :
74 RooAbsPdf(name, title),
75 _x("x", "Dependent", this, x),
76 _coefList("coefList","List of coefficients",this),
77 _lowestOrder(lowestOrder)
78{
79 // Check lowest order
80 if (_lowestOrder<0) {
81 coutE(InputArguments) << "RooPolynomial::ctor(" << GetName()
82 << ") WARNING: lowestOrder must be >=0, setting value to 0" << endl ;
83 _lowestOrder=0 ;
84 }
85
86 for (auto *coef : coefList) {
87 if (!dynamic_cast<RooAbsReal*>(coef)) {
88 coutE(InputArguments) << "RooPolynomial::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
89 << " is not of type RooAbsReal" << endl ;
90 R__ASSERT(0) ;
91 }
92 _coefList.add(*coef) ;
93 }
94}
95
96////////////////////////////////////////////////////////////////////////////////
97
98RooPolynomial::RooPolynomial(const char* name, const char* title,
99 RooAbsReal& x) :
100 RooAbsPdf(name, title),
101 _x("x", "Dependent", this, x),
102 _coefList("coefList","List of coefficients",this),
103 _lowestOrder(1)
104{ }
105
106////////////////////////////////////////////////////////////////////////////////
107/// Copy constructor
108
110 RooAbsPdf(other, name),
111 _x("x", this, other._x),
112 _coefList("coefList",this,other._coefList),
113 _lowestOrder(other._lowestOrder)
114{ }
115
116////////////////////////////////////////////////////////////////////////////////
117/// Destructor
118
120{ }
121
122////////////////////////////////////////////////////////////////////////////////
123
125{
126 // Calculate and return value of polynomial
127
128 const unsigned sz = _coefList.getSize();
129 const int lowestOrder = _lowestOrder;
130 if (!sz) return lowestOrder ? 1. : 0.;
131 _wksp.clear();
132 _wksp.reserve(sz);
133 {
134 const RooArgSet* nset = _coefList.nset();
135 for (auto *c : static_range_cast<RooAbsReal *>(_coefList)) {
136 _wksp.push_back(c->getVal(nset));
137 }
138 }
139 const double x = _x;
140 double retVal = _wksp[sz - 1];
141 for (unsigned i = sz - 1; i--; ) retVal = _wksp[i] + x * retVal;
142 return retVal * std::pow(x, lowestOrder) + (lowestOrder ? 1.0 : 0.0);
143}
144
145// The batch mode support for RooPolynomial was commented out, because that
146// implementation can't deal with observables used as polynomial coefficients
147// yet.
148
149//////////////////////////////////////////////////////////////////////////////////
150///// Compute multiple values of Polynomial.
151//void RooPolynomial::computeBatch(cudaStream_t* stream, double* output, size_t nEvents, RooFit::DataMap& dataMap) const
152//{
153 //RooBatchCompute::ArgVector extraArgs;
154 //for (auto* coef:_coefList)
155 //extraArgs.push_back( static_cast<const RooAbsReal*>(coef)->getVal() );
156 //extraArgs.push_back(_lowestOrder);
157 //auto dispatch = stream ? RooBatchCompute::dispatchCUDA : RooBatchCompute::dispatchCPU;
158 //dispatch->compute(stream, RooBatchCompute::Polynomial, output, nEvents, dataMap, {&*_x,&*_norm}, extraArgs);
159//}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Advertise to RooFit that this function can be analytically integrated.
163Int_t RooPolynomial::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
164{
165 if (matchArgs(allVars, analVars, _x)) return 1;
166 return 0;
167}
168
169////////////////////////////////////////////////////////////////////////////////
170/// Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
171double RooPolynomial::analyticalIntegral(Int_t code, const char* rangeName) const
172{
173 R__ASSERT(code==1) ;
174
175 const double xmin = _x.min(rangeName), xmax = _x.max(rangeName);
176 const int lowestOrder = _lowestOrder;
177 const unsigned sz = _coefList.getSize();
178 if (!sz) return xmax - xmin;
179 _wksp.clear();
180 _wksp.reserve(sz);
181 {
182 const RooArgSet* nset = _coefList.nset();
183 unsigned i = 1 + lowestOrder;
184 for (auto *c : static_range_cast<RooAbsReal *>(_coefList)) {
185 _wksp.push_back(c->getVal(nset) / double(i));
186 ++i;
187 }
188 }
189 double min = _wksp[sz - 1], max = _wksp[sz - 1];
190 for (unsigned i = sz - 1; i--; )
191 min = _wksp[i] + xmin * min, max = _wksp[i] + xmax * max;
192 return max * std::pow(xmax, 1 + lowestOrder) - min * std::pow(xmin, 1 + lowestOrder) +
193 (lowestOrder ? (xmax - xmin) : 0.);
194}
#define c(i)
Definition: RSha256.hxx:101
#define coutE(a)
Definition: RooMsgService.h:37
#define ClassImp(name)
Definition: Rtypes.h:375
#define R__ASSERT(e)
Definition: TError.h:118
char name[80]
Definition: TGX11.cxx:110
float xmin
Definition: THbookFile.cxx:95
float xmax
Definition: THbookFile.cxx:95
Int_t getSize() const
Return the number of elements in the collection.
const RooArgSet * nset() const
Definition: RooAbsProxy.h:45
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:64
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:57
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...
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
double evaluate() const override
do not persist
RooRealProxy _x
Definition: RooPolynomial.h:45
double analyticalIntegral(Int_t code, const char *rangeName=0) const override
Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
std::vector< double > _wksp
Definition: RooPolynomial.h:49
Int_t _lowestOrder
Definition: RooPolynomial.h:47
RooPolynomial()
coverity[UNINIT_CTOR]
~RooPolynomial() override
Destructor.
RooListProxy _coefList
Definition: RooPolynomial.h:46
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=0) const override
Advertise to RooFit that this function can be analytically integrated.
double min(const char *rname=0) const
Query lower limit of range. This requires the payload to be RooAbsRealLValue or derived.
double max(const char *rname=0) const
Query upper 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
RVec< PromoteTypes< T0, T1 > > pow(const T0 &x, const RVec< T1 > &v)
Definition: RVec.hxx:1753
Double_t x[n]
Definition: legend1.C:17
@ InputArguments
Definition: RooGlobalFunc.h:64