Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooPolyVar.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$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 \file RooPolyVar.cxx
19 \class RooPolyVar
20 \ingroup Roofitcore
21 
22 Class RooPolyVar is a RooAbsReal implementing a polynomial in terms
23 of a list of RooAbsReal coefficients
24 \f[f(x) = \sum_{i} a_{i} \cdot x^i \f]
25 Class RooPolyvar implements analytical integrals of all polynomials
26 it can define.
27 **/
28 
29 #include <cmath>
30 
31 #include "RooPolyVar.h"
32 #include "RooArgList.h"
33 #include "RooMsgService.h"
34 //#include "Riostream.h"
35 
36 #include "TError.h"
37 
38 using namespace std;
39 
40 ClassImp(RooPolyVar);
41 ;
42 
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Default constructor
46 
47 RooPolyVar::RooPolyVar() : _lowestOrder(0)
48 { }
49 
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Construct polynomial in x with coefficients in coefList. If
53 /// lowestOrder is not zero, then the first element in coefList is
54 /// interpreted as as the 'lowestOrder' coefficients and all
55 /// subsequent coeffient elements are shifted by a similar amount.
56 RooPolyVar::RooPolyVar(const char* name, const char* title,
57  RooAbsReal& x, const RooArgList& coefList, Int_t lowestOrder) :
58  RooAbsReal(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) << "RooPolyVar::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) << "RooPolyVar::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 /// Constructor of flat polynomial function
85 
86 RooPolyVar::RooPolyVar(const char* name, const char* title,
87  RooAbsReal& x) :
88  RooAbsReal(name, title),
89  _x("x", "Dependent", this, x),
90  _coefList("coefList","List of coefficients",this),
91  _lowestOrder(1)
92 { }
93 
94 
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Copy constructor
98 
99 RooPolyVar::RooPolyVar(const RooPolyVar& other, const char* name) :
100  RooAbsReal(other, name),
101  _x("x", this, other._x),
102  _coefList("coefList",this,other._coefList),
103  _lowestOrder(other._lowestOrder)
104 { }
105 
106 
107 
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Destructor
111 
112 RooPolyVar::~RooPolyVar()
113 { }
114 
115 
116 
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Calculate and return value of polynomial
120 
121 Double_t RooPolyVar::evaluate() const
122 {
123  const unsigned sz = _coefList.getSize();
124  const int lowestOrder = _lowestOrder;
125  if (!sz) return lowestOrder ? 1. : 0.;
126  _wksp.clear();
127  _wksp.reserve(sz);
128  {
129  const RooArgSet* nset = _coefList.nset();
130  for (const auto arg : _coefList) {
131  const auto c = static_cast<RooAbsReal*>(arg);
132  _wksp.push_back(c->getVal(nset));
133  }
134  }
135  const Double_t x = _x;
136  Double_t retVal = _wksp[sz - 1];
137  for (unsigned i = sz - 1; i--; ) retVal = _wksp[i] + x * retVal;
138  return retVal * std::pow(x, lowestOrder);
139 }
140 
141 
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Advertise that we can internally integrate over x
145 
146 Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
147 {
148  if (matchArgs(allVars, analVars, _x)) return 1;
149  return 0;
150 }
151 
152 
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Calculate and return analytical integral over x
156 
157 Double_t RooPolyVar::analyticalIntegral(Int_t code, const char* rangeName) const
158 {
159  R__ASSERT(code==1) ;
160 
161  const Double_t xmin = _x.min(rangeName), xmax = _x.max(rangeName);
162  const int lowestOrder = _lowestOrder;
163  const unsigned sz = _coefList.getSize();
164  if (!sz) return xmax - xmin;
165  _wksp.clear();
166  _wksp.reserve(sz);
167  {
168  const RooArgSet* nset = _coefList.nset();
169  RooFIter it = _coefList.fwdIterator();
170  unsigned i = 1 + lowestOrder;
171  RooAbsReal* c;
172  while ((c = (RooAbsReal*) it.next())) {
173  _wksp.push_back(c->getVal(nset) / Double_t(i));
174  ++i;
175  }
176  }
177  Double_t min = _wksp[sz - 1], max = _wksp[sz - 1];
178  for (unsigned i = sz - 1; i--; )
179  min = _wksp[i] + xmin * min, max = _wksp[i] + xmax * max;
180  return max * std::pow(xmax, 1 + lowestOrder) - min * std::pow(xmin, 1 + lowestOrder);
181 }