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