Logo ROOT   6.10/09
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 
20 RooPolynomial implements a polynomial p.d.f of the form
21 \f[ f(x) = \sum_{i} a_{i} * x^i \f]
22 By default coefficient a_0 is chosen to be 1, as polynomial
23 probability density functions have one degree of freedom
24 less than polynomial functions due to the normalization condition
25 **/
26 
27 #include <cmath>
28 #include <cassert>
29 
30 #include "RooPolynomial.h"
31 #include "RooAbsReal.h"
32 #include "RooArgList.h"
33 #include "RooMsgService.h"
34 
35 #include "TError.h"
36 
37 using namespace std;
38 
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// coverity[UNINIT_CTOR]
43 
45 {
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Constructor
50 
51 RooPolynomial::RooPolynomial(const char* name, const char* title,
52  RooAbsReal& x, const RooArgList& coefList, Int_t lowestOrder) :
53  RooAbsPdf(name, title),
54  _x("x", "Dependent", this, x),
55  _coefList("coefList","List of coefficients",this),
56  _lowestOrder(lowestOrder)
57 {
58  // Check lowest order
59  if (_lowestOrder<0) {
60  coutE(InputArguments) << "RooPolynomial::ctor(" << GetName()
61  << ") WARNING: lowestOrder must be >=0, setting value to 0" << endl ;
62  _lowestOrder=0 ;
63  }
64 
65  RooFIter coefIter = coefList.fwdIterator() ;
66  RooAbsArg* coef ;
67  while((coef = (RooAbsArg*)coefIter.next())) {
68  if (!dynamic_cast<RooAbsReal*>(coef)) {
69  coutE(InputArguments) << "RooPolynomial::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
70  << " is not of type RooAbsReal" << endl ;
71  R__ASSERT(0) ;
72  }
73  _coefList.add(*coef) ;
74  }
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 
79 RooPolynomial::RooPolynomial(const char* name, const char* title,
80  RooAbsReal& x) :
81  RooAbsPdf(name, title),
82  _x("x", "Dependent", this, x),
83  _coefList("coefList","List of coefficients",this),
84  _lowestOrder(1)
85 { }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Copy constructor
89 
90 RooPolynomial::RooPolynomial(const RooPolynomial& other, const char* name) :
91  RooAbsPdf(other, name),
92  _x("x", this, other._x),
93  _coefList("coefList",this,other._coefList),
95 { }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Destructor
99 
101 { }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 
106 {
107  // Calculate and return value of polynomial
108 
109  const unsigned sz = _coefList.getSize();
110  const int lowestOrder = _lowestOrder;
111  if (!sz) return lowestOrder ? 1. : 0.;
112  _wksp.clear();
113  _wksp.reserve(sz);
114  {
115  const RooArgSet* nset = _coefList.nset();
117  RooAbsReal* c;
118  while ((c = (RooAbsReal*) it.next())) _wksp.push_back(c->getVal(nset));
119  }
120  const Double_t x = _x;
121  Double_t retVal = _wksp[sz - 1];
122  for (unsigned i = sz - 1; i--; ) retVal = _wksp[i] + x * retVal;
123  return retVal * std::pow(x, lowestOrder) + (lowestOrder ? 1.0 : 0.0);
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 
128 Int_t RooPolynomial::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
129 {
130  if (matchArgs(allVars, analVars, _x)) return 1;
131  return 0;
132 }
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 
136 Double_t RooPolynomial::analyticalIntegral(Int_t code, const char* rangeName) const
137 {
138  R__ASSERT(code==1) ;
139 
140  const Double_t xmin = _x.min(rangeName), xmax = _x.max(rangeName);
141  const int lowestOrder = _lowestOrder;
142  const unsigned sz = _coefList.getSize();
143  if (!sz) return xmax - xmin;
144  _wksp.clear();
145  _wksp.reserve(sz);
146  {
147  const RooArgSet* nset = _coefList.nset();
149  unsigned i = 1 + lowestOrder;
150  RooAbsReal* c;
151  while ((c = (RooAbsReal*) it.next())) {
152  _wksp.push_back(c->getVal(nset) / Double_t(i));
153  ++i;
154  }
155  }
156  Double_t min = _wksp[sz - 1], max = _wksp[sz - 1];
157  for (unsigned i = sz - 1; i--; )
158  min = _wksp[i] + xmin * min, max = _wksp[i] + xmax * max;
159  return max * std::pow(xmax, 1 + lowestOrder) - min * std::pow(xmin, 1 + lowestOrder) +
160  (lowestOrder ? (xmax - xmin) : 0.);
161 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
#define coutE(a)
Definition: RooMsgService.h:34
RooListProxy _coefList
Definition: RooPolynomial.h:46
float xmin
Definition: THbookFile.cxx:93
RooRealProxy _x
Definition: RooPolynomial.h:45
Bool_t matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
virtual ~RooPolynomial()
Destructor.
#define R__ASSERT(e)
Definition: TError.h:96
RooPolynomial()
coverity[UNINIT_CTOR]
int Int_t
Definition: RtypesCore.h:41
STL namespace.
Double_t x[n]
Definition: legend1.C:17
Double_t evaluate() const
do not persist
Double_t analyticalIntegral(Int_t code, const char *rangeName=0) const
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral. ...
double pow(double, double)
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=0) const
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported...
const RooArgSet * nset() const
Definition: RooAbsProxy.h:46
Int_t getSize() const
std::vector< Double_t > _wksp
Definition: RooPolynomial.h:49
float xmax
Definition: THbookFile.cxx:93
RooAbsArg * next()
#define ClassImp(name)
Definition: Rtypes.h:336
Double_t min(const char *rname=0) const
Definition: RooRealProxy.h:56
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
RooFIter fwdIterator() const
Double_t max(const char *rname=0) const
Definition: RooRealProxy.h:57
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
Int_t _lowestOrder
Definition: RooPolynomial.h:47
RooPolynomial implements a polynomial p.d.f of the form By default coefficient a_0 is chosen to be 1...
Definition: RooPolynomial.h:28
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66