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
22Class RooPolyVar is a RooAbsReal implementing a polynomial in terms
23of a list of RooAbsReal coefficients
24\f[f(x) = \sum_{i} a_{i} \cdot x^i \f]
25Class RooPolyvar implements analytical integrals of all polynomials
26it 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
38using namespace std;
39
41;
42
43
44////////////////////////////////////////////////////////////////////////////////
45/// Default constructor
46
47RooPolyVar::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.
56RooPolyVar::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 for(RooAbsArg * coef : coefList) {
71 if (!dynamic_cast<RooAbsReal*>(coef)) {
72 coutE(InputArguments) << "RooPolyVar::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
73 << " is not of type RooAbsReal" << endl ;
74 R__ASSERT(0) ;
75 }
76 _coefList.add(*coef) ;
77 }
78}
79
80
81////////////////////////////////////////////////////////////////////////////////
82/// Constructor of flat polynomial function
83
84RooPolyVar::RooPolyVar(const char* name, const char* title,
85 RooAbsReal& x) :
86 RooAbsReal(name, title),
87 _x("x", "Dependent", this, x),
88 _coefList("coefList","List of coefficients",this),
89 _lowestOrder(1)
90{ }
91
92
93
94////////////////////////////////////////////////////////////////////////////////
95/// Copy constructor
96
97RooPolyVar::RooPolyVar(const RooPolyVar& other, const char* name) :
98 RooAbsReal(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/// Calculate and return value of polynomial
118
120{
121 const unsigned sz = _coefList.getSize();
122 const int lowestOrder = _lowestOrder;
123 if (!sz) return lowestOrder ? 1. : 0.;
124 _wksp.clear();
125 _wksp.reserve(sz);
126 {
127 const RooArgSet* nset = _coefList.nset();
128 for (const auto arg : _coefList) {
129 const auto c = static_cast<RooAbsReal*>(arg);
130 _wksp.push_back(c->getVal(nset));
131 }
132 }
133 const double x = _x;
134 double retVal = _wksp[sz - 1];
135 for (unsigned i = sz - 1; i--; ) retVal = _wksp[i] + x * retVal;
136 return retVal * std::pow(x, lowestOrder);
137}
138
139
140
141////////////////////////////////////////////////////////////////////////////////
142/// Advertise that we can internally integrate over x
143
144Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
145{
146 if (matchArgs(allVars, analVars, _x)) return 1;
147 return 0;
148}
149
150
151
152////////////////////////////////////////////////////////////////////////////////
153/// Calculate and return analytical integral over x
154
155double RooPolyVar::analyticalIntegral(Int_t code, const char* rangeName) const
156{
157 R__ASSERT(code==1) ;
158
159 const double xmin = _x.min(rangeName), xmax = _x.max(rangeName);
160 const int lowestOrder = _lowestOrder;
161 const unsigned sz = _coefList.getSize();
162 if (!sz) return xmax - xmin;
163 _wksp.clear();
164 _wksp.reserve(sz);
165 {
166 const RooArgSet* nset = _coefList.nset();
167 unsigned i = 1 + lowestOrder;
168 for(auto * c : static_range_cast<RooAbsReal*>(_coefList)) {
169 _wksp.push_back(c->getVal(nset) / double(i));
170 ++i;
171 }
172 }
173 double 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}
#define c(i)
Definition: RSha256.hxx:101
#define coutE(a)
Definition: RooMsgService.h:37
#define ClassImp(name)
Definition: Rtypes.h:375
#define R__ASSERT(e)
Definition: TError.h:118
char name[80]
Definition: TGX11.cxx:110
float xmin
Definition: THbookFile.cxx:95
float xmax
Definition: THbookFile.cxx:95
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:71
Int_t getSize() const
Return the number of elements in the collection.
const RooArgSet * nset() const
Definition: RooAbsProxy.h:48
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:62
bool matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
Class RooPolyVar is a RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficie...
Definition: RooPolyVar.h:28
RooPolyVar()
Default constructor.
Definition: RooPolyVar.cxx:47
double evaluate() const override
Calculate and return value of polynomial.
Definition: RooPolyVar.cxx:119
~RooPolyVar() override
Destructor.
Definition: RooPolyVar.cxx:110
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Advertise that we can internally integrate over x.
Definition: RooPolyVar.cxx:144
std::vector< double > _wksp
! do not persist
Definition: RooPolyVar.h:49
Int_t _lowestOrder
Definition: RooPolyVar.h:47
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Calculate and return analytical integral over x.
Definition: RooPolyVar.cxx:155
RooListProxy _coefList
Definition: RooPolyVar.h:46
RooRealProxy _x
Definition: RooPolyVar.h:45
double max(const char *rname=nullptr) const
Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
double min(const char *rname=nullptr) const
Query lower limit of range. This requires the payload to be RooAbsRealLValue or derived.
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
RVec< PromoteTypes< T0, T1 > > pow(const T0 &x, const RVec< T1 > &v)
Definition: RVec.hxx:1754
Double_t x[n]
Definition: legend1.C:17
@ InputArguments
Definition: RooGlobalFunc.h:64