```/*****************************************************************************
* Project: RooFit                                                           *
* Package: RooFitModels                                                     *
* @(#)root/roofit:\$Id\$
* Authors:                                                                  *
*   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
*   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
*                                                                           *
* Copyright (c) 2000-2005, Regents of the University of California          *
*                                                                           *
* Redistribution and use in source and binary forms,                        *
* with or without modification, are permitted according to the terms        *
*****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// RooPolynomial implements a polynomial p.d.f of the form
// <pre>
// f(x) = sum_i a_i * x^i
//</pre>
// By default coefficient a_0 is chosen to be 1, as polynomial
// probability density functions have one degree of freedome
// less than polynomial functions due to the normalization condition
// END_HTML
//

#include "RooFit.h"

#include "Riostream.h"
#include "Riostream.h"
#include "TMath.h"

#include "RooPolynomial.h"
#include "RooAbsReal.h"
#include "RooRealVar.h"
#include "RooArgList.h"

#include "TError.h"

using namespace std;

ClassImp(RooPolynomial)
;

//_____________________________________________________________________________
RooPolynomial::RooPolynomial()
{
// coverity[UNINIT_CTOR]
_coefIter = _coefList.createIterator() ;
}

//_____________________________________________________________________________
RooPolynomial::RooPolynomial(const char* name, const char* title,
RooAbsReal& x, const RooArgList& coefList, Int_t lowestOrder) :
RooAbsPdf(name, title),
_x("x", "Dependent", this, x),
_coefList("coefList","List of coefficients",this),
_lowestOrder(lowestOrder)
{
// Constructor
_coefIter = _coefList.createIterator() ;

// Check lowest order
if (_lowestOrder<0) {
cout << "RooPolynomial::ctor(" << GetName()
<< ") WARNING: lowestOrder must be >=0, setting value to 0" << endl ;
_lowestOrder=0 ;
}

TIterator* coefIter = coefList.createIterator() ;
RooAbsArg* coef ;
while((coef = (RooAbsArg*)coefIter->Next())) {
if (!dynamic_cast<RooAbsReal*>(coef)) {
cout << "RooPolynomial::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
<< " is not of type RooAbsReal" << endl ;
R__ASSERT(0) ;
}
}
delete coefIter ;
}

//_____________________________________________________________________________
RooPolynomial::RooPolynomial(const char* name, const char* title,
RooAbsReal& x) :
RooAbsPdf(name, title),
_x("x", "Dependent", this, x),
_coefList("coefList","List of coefficients",this),
_lowestOrder(1)
{
_coefIter = _coefList.createIterator() ;
}

//_____________________________________________________________________________
RooPolynomial::RooPolynomial(const RooPolynomial& other, const char* name) :
RooAbsPdf(other, name),
_x("x", this, other._x),
_coefList("coefList",this,other._coefList),
_lowestOrder(other._lowestOrder)
{
// Copy constructor
_coefIter = _coefList.createIterator() ;
}

//_____________________________________________________________________________
RooPolynomial::~RooPolynomial()
{
// Destructor
delete _coefIter ;
}

//_____________________________________________________________________________
Double_t RooPolynomial::evaluate() const
{
Int_t order(_lowestOrder) ;
Double_t sum(order<1 ? 0 : 1) ;

_coefIter->Reset() ;

RooAbsReal* coef ;
const RooArgSet* nset = _coefList.nset() ;
while((coef=(RooAbsReal*)_coefIter->Next())) {
sum += coef->getVal(nset)*TMath::Power(_x,order++) ;
}

//   if (sum<=0) {
//cout << "RooPolynomial sum = " << sum << endl ;
//   }
return sum;
}

//_____________________________________________________________________________
Int_t RooPolynomial::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
{
if (matchArgs(allVars, analVars, _x)) return 1;
return 0;
}

//_____________________________________________________________________________
Double_t RooPolynomial::analyticalIntegral(Int_t code, const char* rangeName) const
{
R__ASSERT(code==1) ;

Int_t order(_lowestOrder) ;

Double_t sum(order>0 ? _x.max(rangeName)-_x.min(rangeName) : 0) ;
//cout << "RooPolynomial::aI(" << GetName() << ") range = " << _x.min(rangeName) << " - " << _x.max(rangeName) << endl ;

const RooArgSet* nset = _coefList.nset() ;
_coefIter->Reset() ;
RooAbsReal* coef ;

// Primitive = sum(k) coef_k * 1/(k+1) x^(k+1)
while((coef=(RooAbsReal*)_coefIter->Next())) {
sum += coef->getVal(nset)*(TMath::Power(_x.max(rangeName),order+1)-TMath::Power(_x.min(rangeName),order+1))/(order+1) ;
order++ ;
}

return sum;

}
```
RooPolynomial.cxx:1
RooPolynomial.cxx:2
RooPolynomial.cxx:3
RooPolynomial.cxx:4
RooPolynomial.cxx:5
RooPolynomial.cxx:6
RooPolynomial.cxx:7
RooPolynomial.cxx:8
RooPolynomial.cxx:9
RooPolynomial.cxx:10
RooPolynomial.cxx:11
RooPolynomial.cxx:12
RooPolynomial.cxx:13
RooPolynomial.cxx:14
RooPolynomial.cxx:15
RooPolynomial.cxx:16
RooPolynomial.cxx:17
RooPolynomial.cxx:18
RooPolynomial.cxx:19
RooPolynomial.cxx:20
RooPolynomial.cxx:21
RooPolynomial.cxx:22
RooPolynomial.cxx:23
RooPolynomial.cxx:24
RooPolynomial.cxx:25
RooPolynomial.cxx:26
RooPolynomial.cxx:27
RooPolynomial.cxx:28
RooPolynomial.cxx:29
RooPolynomial.cxx:30
RooPolynomial.cxx:31
RooPolynomial.cxx:32
RooPolynomial.cxx:33
RooPolynomial.cxx:34
RooPolynomial.cxx:35
RooPolynomial.cxx:36
RooPolynomial.cxx:37
RooPolynomial.cxx:38
RooPolynomial.cxx:39
RooPolynomial.cxx:40
RooPolynomial.cxx:41
RooPolynomial.cxx:42
RooPolynomial.cxx:43
RooPolynomial.cxx:44
RooPolynomial.cxx:45
RooPolynomial.cxx:46
RooPolynomial.cxx:47
RooPolynomial.cxx:48
RooPolynomial.cxx:49
RooPolynomial.cxx:50
RooPolynomial.cxx:51
RooPolynomial.cxx:52
RooPolynomial.cxx:53
RooPolynomial.cxx:54
RooPolynomial.cxx:55
RooPolynomial.cxx:56
RooPolynomial.cxx:57
RooPolynomial.cxx:58
RooPolynomial.cxx:59
RooPolynomial.cxx:60
RooPolynomial.cxx:61
RooPolynomial.cxx:62
RooPolynomial.cxx:63
RooPolynomial.cxx:64
RooPolynomial.cxx:65
RooPolynomial.cxx:66
RooPolynomial.cxx:67
RooPolynomial.cxx:68
RooPolynomial.cxx:69
RooPolynomial.cxx:70
RooPolynomial.cxx:71
RooPolynomial.cxx:72
RooPolynomial.cxx:73
RooPolynomial.cxx:74
RooPolynomial.cxx:75
RooPolynomial.cxx:76
RooPolynomial.cxx:77
RooPolynomial.cxx:78
RooPolynomial.cxx:79
RooPolynomial.cxx:80
RooPolynomial.cxx:81
RooPolynomial.cxx:82
RooPolynomial.cxx:83
RooPolynomial.cxx:84
RooPolynomial.cxx:85
RooPolynomial.cxx:86
RooPolynomial.cxx:87
RooPolynomial.cxx:88
RooPolynomial.cxx:89
RooPolynomial.cxx:90
RooPolynomial.cxx:91
RooPolynomial.cxx:92
RooPolynomial.cxx:93
RooPolynomial.cxx:94
RooPolynomial.cxx:95
RooPolynomial.cxx:96
RooPolynomial.cxx:97
RooPolynomial.cxx:98
RooPolynomial.cxx:99
RooPolynomial.cxx:100
RooPolynomial.cxx:101
RooPolynomial.cxx:102
RooPolynomial.cxx:103
RooPolynomial.cxx:104
RooPolynomial.cxx:105
RooPolynomial.cxx:106
RooPolynomial.cxx:107
RooPolynomial.cxx:108
RooPolynomial.cxx:109
RooPolynomial.cxx:110
RooPolynomial.cxx:111
RooPolynomial.cxx:112
RooPolynomial.cxx:113
RooPolynomial.cxx:114
RooPolynomial.cxx:115
RooPolynomial.cxx:116
RooPolynomial.cxx:117
RooPolynomial.cxx:118
RooPolynomial.cxx:119
RooPolynomial.cxx:120
RooPolynomial.cxx:121
RooPolynomial.cxx:122
RooPolynomial.cxx:123
RooPolynomial.cxx:124
RooPolynomial.cxx:125
RooPolynomial.cxx:126
RooPolynomial.cxx:127
RooPolynomial.cxx:128
RooPolynomial.cxx:129
RooPolynomial.cxx:130
RooPolynomial.cxx:131
RooPolynomial.cxx:132
RooPolynomial.cxx:133
RooPolynomial.cxx:134
RooPolynomial.cxx:135
RooPolynomial.cxx:136
RooPolynomial.cxx:137
RooPolynomial.cxx:138
RooPolynomial.cxx:139
RooPolynomial.cxx:140
RooPolynomial.cxx:141
RooPolynomial.cxx:142
RooPolynomial.cxx:143
RooPolynomial.cxx:144
RooPolynomial.cxx:145
RooPolynomial.cxx:146
RooPolynomial.cxx:147
RooPolynomial.cxx:148
RooPolynomial.cxx:149
RooPolynomial.cxx:150
RooPolynomial.cxx:151
RooPolynomial.cxx:152
RooPolynomial.cxx:153
RooPolynomial.cxx:154
RooPolynomial.cxx:155
RooPolynomial.cxx:156
RooPolynomial.cxx:157
RooPolynomial.cxx:158
RooPolynomial.cxx:159
RooPolynomial.cxx:160
RooPolynomial.cxx:161
RooPolynomial.cxx:162
RooPolynomial.cxx:163
RooPolynomial.cxx:164
RooPolynomial.cxx:165
RooPolynomial.cxx:166
RooPolynomial.cxx:167
RooPolynomial.cxx:168
RooPolynomial.cxx:169
RooPolynomial.cxx:170
RooPolynomial.cxx:171
RooPolynomial.cxx:172
RooPolynomial.cxx:173
RooPolynomial.cxx:174
RooPolynomial.cxx:175
RooPolynomial.cxx:176
RooPolynomial.cxx:177
RooPolynomial.cxx:178
RooPolynomial.cxx:179
RooPolynomial.cxx:180