Logo ROOT   6.18/05
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 <cmath>
33#include <cassert>
34
35#include "RooPolynomial.h"
36#include "RooAbsReal.h"
37#include "RooArgList.h"
38#include "RooMsgService.h"
39
40#include "TError.h"
41
42using namespace std;
43
45
46////////////////////////////////////////////////////////////////////////////////
47/// coverity[UNINIT_CTOR]
48
50{
51}
52
53////////////////////////////////////////////////////////////////////////////////
54/// Create a polynomial in the variable `x`.
55/// \param[in] name Name of the PDF
56/// \param[in] title Title for plotting the PDF
57/// \param[in] x The variable of the polynomial
58/// \param[in] coefList The coefficients \f$ a_i \f$
59/// \param[in] lowestOrder [optional] Truncate the sum such that it skips the lower orders:
60/// \f[
61/// 1. + \sum_{i=0}^{\mathrm{coefList.size()}} a_{i} * x^{(i + \mathrm{lowestOrder})}
62/// \f]
63///
64/// This means that
65/// \code{.cpp}
66/// RooPolynomial pol("pol", "pol", x, RooArgList(a, b), lowestOrder = 2)
67/// \endcode
68/// computes
69/// \f[
70/// \mathrm{pol}(x) = 1 * x^0 + (0 * x^{\ldots}) + a * x^2 + b * x^3.
71/// \f]
72
73
74RooPolynomial::RooPolynomial(const char* name, const char* title,
75 RooAbsReal& x, const RooArgList& coefList, Int_t lowestOrder) :
76 RooAbsPdf(name, title),
77 _x("x", "Dependent", this, x),
78 _coefList("coefList","List of coefficients",this),
79 _lowestOrder(lowestOrder)
80{
81 // Check lowest order
82 if (_lowestOrder<0) {
83 coutE(InputArguments) << "RooPolynomial::ctor(" << GetName()
84 << ") WARNING: lowestOrder must be >=0, setting value to 0" << endl ;
85 _lowestOrder=0 ;
86 }
87
88 RooFIter coefIter = coefList.fwdIterator() ;
89 RooAbsArg* coef ;
90 while((coef = (RooAbsArg*)coefIter.next())) {
91 if (!dynamic_cast<RooAbsReal*>(coef)) {
92 coutE(InputArguments) << "RooPolynomial::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
93 << " is not of type RooAbsReal" << endl ;
94 R__ASSERT(0) ;
95 }
96 _coefList.add(*coef) ;
97 }
98}
99
100////////////////////////////////////////////////////////////////////////////////
101
102RooPolynomial::RooPolynomial(const char* name, const char* title,
103 RooAbsReal& x) :
104 RooAbsPdf(name, title),
105 _x("x", "Dependent", this, x),
106 _coefList("coefList","List of coefficients",this),
107 _lowestOrder(1)
108{ }
109
110////////////////////////////////////////////////////////////////////////////////
111/// Copy constructor
112
114 RooAbsPdf(other, name),
115 _x("x", this, other._x),
116 _coefList("coefList",this,other._coefList),
117 _lowestOrder(other._lowestOrder)
118{ }
119
120////////////////////////////////////////////////////////////////////////////////
121/// Destructor
122
124{ }
125
126////////////////////////////////////////////////////////////////////////////////
127
129{
130 // Calculate and return value of polynomial
131
132 const unsigned sz = _coefList.getSize();
133 const int lowestOrder = _lowestOrder;
134 if (!sz) return lowestOrder ? 1. : 0.;
135 _wksp.clear();
136 _wksp.reserve(sz);
137 {
138 const RooArgSet* nset = _coefList.nset();
140 RooAbsReal* c;
141 while ((c = (RooAbsReal*) it.next())) _wksp.push_back(c->getVal(nset));
142 }
143 const Double_t x = _x;
144 Double_t retVal = _wksp[sz - 1];
145 for (unsigned i = sz - 1; i--; ) retVal = _wksp[i] + x * retVal;
146 return retVal * std::pow(x, lowestOrder) + (lowestOrder ? 1.0 : 0.0);
147}
148
149////////////////////////////////////////////////////////////////////////////////
150/// Advertise to RooFit that this function can be analytically integrated.
151Int_t RooPolynomial::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
152{
153 if (matchArgs(allVars, analVars, _x)) return 1;
154 return 0;
155}
156
157////////////////////////////////////////////////////////////////////////////////
158/// Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
159Double_t RooPolynomial::analyticalIntegral(Int_t code, const char* rangeName) const
160{
161 R__ASSERT(code==1) ;
162
163 const Double_t xmin = _x.min(rangeName), xmax = _x.max(rangeName);
164 const int lowestOrder = _lowestOrder;
165 const unsigned sz = _coefList.getSize();
166 if (!sz) return xmax - xmin;
167 _wksp.clear();
168 _wksp.reserve(sz);
169 {
170 const RooArgSet* nset = _coefList.nset();
172 unsigned i = 1 + lowestOrder;
173 RooAbsReal* c;
174 while ((c = (RooAbsReal*) it.next())) {
175 _wksp.push_back(c->getVal(nset) / Double_t(i));
176 ++i;
177 }
178 }
179 Double_t min = _wksp[sz - 1], max = _wksp[sz - 1];
180 for (unsigned i = sz - 1; i--; )
181 min = _wksp[i] + xmin * min, max = _wksp[i] + xmax * max;
182 return max * std::pow(xmax, 1 + lowestOrder) - min * std::pow(xmin, 1 + lowestOrder) +
183 (lowestOrder ? (xmax - xmin) : 0.);
184}
#define c(i)
Definition: RSha256.hxx:101
#define coutE(a)
Definition: RooMsgService.h:34
int Int_t
Definition: RtypesCore.h:41
double Double_t
Definition: RtypesCore.h:55
#define ClassImp(name)
Definition: Rtypes.h:365
#define R__ASSERT(e)
Definition: TError.h:96
char name[80]
Definition: TGX11.cxx:109
float xmin
Definition: THbookFile.cxx:93
float xmax
Definition: THbookFile.cxx:93
double pow(double, double)
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:70
RooFIter fwdIterator() const R__SUGGEST_ALTERNATIVE("begin()
One-time forward iterator.
Int_t getSize() const
const RooArgSet * nset() const
Definition: RooAbsProxy.h:46
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
Bool_t 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:21
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
RooAbsArg * next()
Return next element or nullptr if at end.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=0) const
Advertise to RooFit that this function can be analytically integrated.
RooRealProxy _x
Definition: RooPolynomial.h:45
Double_t analyticalIntegral(Int_t code, const char *rangeName=0) const
Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
Int_t _lowestOrder
Definition: RooPolynomial.h:47
RooPolynomial()
coverity[UNINIT_CTOR]
Double_t evaluate() const
do not persist
RooListProxy _coefList
Definition: RooPolynomial.h:46
std::vector< Double_t > _wksp
Definition: RooPolynomial.h:49
virtual ~RooPolynomial()
Destructor.
Double_t min(const char *rname=0) const
Definition: RooRealProxy.h:56
Double_t max(const char *rname=0) const
Definition: RooRealProxy.h:57
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Double_t x[n]
Definition: legend1.C:17
@ InputArguments
Definition: RooGlobalFunc.h:58