Logo ROOT  
Reference Guide
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 
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 
113 { }
114 
115 
116 
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Calculate and return value of polynomial
120 
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();
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 }
c
#define c(i)
Definition: RSha256.hxx:119
RooMsgService.h
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
xmax
float xmax
Definition: THbookFile.cxx:95
RooAbsCollection::fwdIterator
RooFIter fwdIterator() const
One-time forward iterator.
Definition: RooAbsCollection.h:133
coutE
#define coutE(a)
Definition: RooMsgService.h:33
RooArgList
Definition: RooArgList.h:21
RooPolyVar::_lowestOrder
Int_t _lowestOrder
Definition: RooPolyVar.h:47
x
Double_t x[n]
Definition: legend1.C:17
RooAbsReal
Definition: RooAbsReal.h:61
RooPolyVar::evaluate
Double_t evaluate() const
do not persist
Definition: RooPolyVar.cxx:121
RooPolyVar::_wksp
std::vector< Double_t > _wksp
Definition: RooPolyVar.h:49
RooPolyVar::RooPolyVar
RooPolyVar()
Default constructor.
Definition: RooPolyVar.cxx:47
RooPolyVar::_coefList
RooListProxy _coefList
Definition: RooPolyVar.h:46
RooPolyVar.h
RooPolyVar::analyticalIntegral
Double_t analyticalIntegral(Int_t code, const char *rangeName=0) const
Calculate and return analytical integral over x.
Definition: RooPolyVar.cxx:157
RooPolyVar::_x
RooRealProxy _x
Definition: RooPolyVar.h:45
xmin
float xmin
Definition: THbookFile.cxx:95
RooPolyVar::~RooPolyVar
virtual ~RooPolyVar()
Destructor.
Definition: RooPolyVar.cxx:112
RooTemplateProxy::min
double min(const char *rname=0) const
Query lower limit of range. This requires the payload to be RooAbsRealLValue or derived.
Definition: RooTemplateProxy.h:287
RooFIter
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
Definition: RooLinkedListIter.h:39
RooFIter::next
RooAbsArg * next()
Return next element or nullptr if at end.
Definition: RooLinkedListIter.h:49
RooListProxy::add
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
Definition: RooListProxy.cxx:104
RooAbsProxy::nset
const RooArgSet * nset() const
Definition: RooAbsProxy.h:59
RooPolyVar
Definition: RooPolyVar.h:28
RooPolyVar::getAnalyticalIntegral
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=0) const
Advertise that we can internally integrate over x.
Definition: RooPolyVar.cxx:146
Double_t
double Double_t
Definition: RtypesCore.h:59
R__ASSERT
#define R__ASSERT(e)
Definition: TError.h:120
name
char name[80]
Definition: TGX11.cxx:110
RooTemplateProxy::max
double max(const char *rname=0) const
Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
Definition: RooTemplateProxy.h:289
RooAbsArg
Definition: RooAbsArg.h:73
RooAbsReal::matchArgs
Bool_t matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
Definition: RooAbsReal.cxx:3400
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
pow
double pow(double, double)
RooArgList.h
RooAbsCollection::getSize
Int_t getSize() const
Definition: RooAbsCollection.h:171
RooArgSet
Definition: RooArgSet.h:28
int
TError.h