ROOT   Reference Guide
Searching...
No Matches
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 *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
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 "RooBatchCompute.h"
35
38
39#include "TError.h"
40
42
43////////////////////////////////////////////////////////////////////////////////
44/// Construct polynomial in x with coefficients in coefList. If
45/// lowestOrder is not zero, then the first element in coefList is
46/// interpreted as as the 'lowestOrder' coefficients and all
47/// subsequent coefficient elements are shifted by a similar amount.
48RooPolyVar::RooPolyVar(const char *name, const char *title, RooAbsReal &x, const RooArgList &coefList,
49 Int_t lowestOrder)
50 : RooAbsReal(name, title),
51 _x("x", "Dependent", this, x),
52 _coefList("coefList", "List of coefficients", this),
53 _lowestOrder(lowestOrder)
54{
55 // Check lowest order
56 if (_lowestOrder < 0) {
57 coutE(InputArguments) << "RooPolyVar::ctor(" << GetName()
58 << ") WARNING: lowestOrder must be >=0, setting value to 0" << std::endl;
59 _lowestOrder = 0;
60 }
61
62 for (RooAbsArg *coef : coefList) {
63 if (!dynamic_cast<RooAbsReal *>(coef)) {
64 coutE(InputArguments) << "RooPolyVar::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
65 << " is not of type RooAbsReal" << std::endl;
66 R__ASSERT(0);
67 }
69 }
70}
71
72////////////////////////////////////////////////////////////////////////////////
73/// Constructor of flat polynomial function
74
75RooPolyVar::RooPolyVar(const char *name, const char *title, RooAbsReal &x)
76 : RooAbsReal(name, title),
77 _x("x", "Dependent", this, x),
78 _coefList("coefList", "List of coefficients", this),
79 _lowestOrder(1)
80{
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Copy constructor
85
86RooPolyVar::RooPolyVar(const RooPolyVar &other, const char *name)
87 : RooAbsReal(other, name),
88 _x("x", this, other._x),
89 _coefList("coefList", this, other._coefList),
90 _lowestOrder(other._lowestOrder)
91{
92}
93
94void RooPolyVar::fillCoeffValues(std::vector<double> &wksp, RooListProxy const &coefList)
95{
96 wksp.clear();
97 wksp.reserve(coefList.size());
98 {
99 const RooArgSet *nset = coefList.nset();
100 for (const auto arg : coefList) {
101 const auto c = static_cast<RooAbsReal *>(arg);
102 wksp.push_back(c->getVal(nset));
103 }
104 }
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Calculate and return value of polynomial
109
111{
112 const unsigned sz = _coefList.getSize();
113 if (!sz)
114 return _lowestOrder ? 1. : 0.;
115
117
119}
120
122{
123 const unsigned sz = _coefList.size();
124 if (!sz) {
125 ctx.addResult(this, std::to_string((_lowestOrder ? 1. : 0.)));
126 return;
127 }
128
130 ctx.buildCall("RooFit::Detail::EvaluateFuncs::polynomialEvaluate", _coefList, sz, _lowestOrder, _x));
131}
132
133void RooPolyVar::computeBatchImpl(RooAbsArg const* caller, double *output, size_t nEvents,
134 RooFit::Detail::DataMap const &dataMap, RooAbsReal const &x, RooArgList const &coefs,
135 int lowestOrder)
136{
137 if (coefs.empty()) {
138 output[0] = lowestOrder ? 1.0 : 0.0;
139 return;
140 }
141
143 vars.reserve(coefs.size() + 2);
144
145 // Fill the coefficients for the skipped orders. By a conventions started in
146 // RooPolynomial, if the zero-th order is skipped, it implies a coefficient
147 // for the constant term of one.
148 const double zero = 1.0;
149 const double one = 1.0;
150 for (int i = lowestOrder - 1; i >= 0; --i) {
151 vars.push_back(i == 0 ? std::span<const double>{&one, 1} : std::span<const double>{&zero, 1});
152 }
153
154 for (RooAbsArg *coef : coefs) {
155 vars.push_back(dataMap.at(coef));
156 }
157 vars.push_back(dataMap.at(&x));
158 RooBatchCompute::ArgVector extraArgs{double(vars.size() - 1)};
159 RooBatchCompute::compute(dataMap.config(caller), RooBatchCompute::Polynomial, output, nEvents, vars, extraArgs);
160}
161
162/// Compute multiple values of Polynomial.
163void RooPolyVar::computeBatch(double *output, size_t nEvents,
164 RooFit::Detail::DataMap const &dataMap) const
165{
166 computeBatchImpl(this, output, nEvents, dataMap, _x.arg(), _coefList, _lowestOrder);
167}
168
169////////////////////////////////////////////////////////////////////////////////
170/// Advertise that we can internally integrate over x
171
172Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
173{
174 if (matchArgs(allVars, analVars, _x))
175 return 1;
176 return 0;
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// Calculate and return analytical integral over x
181
182double RooPolyVar::analyticalIntegral(Int_t code, const char *rangeName) const
183{
184 R__ASSERT(code == 1);
185
186 const double xmin = _x.min(rangeName), xmax = _x.max(rangeName);
187 const unsigned sz = _coefList.getSize();
188 if (!sz)
189 return _lowestOrder ? xmax - xmin : 0.0;
190
192
194}
195
196std::string RooPolyVar::buildCallToAnalyticIntegral(Int_t /* code */, const char *rangeName,
198{
199 const double xmin = _x.min(rangeName), xmax = _x.max(rangeName);
200 const unsigned sz = _coefList.getSize();
201 if (!sz)
202 return std::to_string(_lowestOrder ? xmax - xmin : 0.0);
203
204 return ctx.buildCall("RooFit::Detail::AnalyticalIntegrals::polynomialIntegral", _coefList, sz, _lowestOrder, xmin,
205 xmax);
206}
#define c(i)
Definition RSha256.hxx:101
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
#define R__ASSERT(e)
Definition TError.h:118
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:80
Int_t getSize() const
Return the number of elements in the collection.
Storage_t::size_type size() const
const RooArgSet * nset() const
Definition RooAbsProxy.h:52
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:59
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:55
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...
A class to maintain the context for squashing of RooFit models into code.
std::string buildCall(std::string const &funcname, Args_t const &...args)
Build the code to call the function with name funcname, passing some arguments.
void addResult(RooAbsArg const *key, std::string const &value)
A function to save an expression that includes/depends on the result of the input node.
RooBatchCompute::Config config(RooAbsArg const *arg) const
Definition DataMap.cxx:40
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
Definition DataMap.cxx:22
Class RooPolyVar is a RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficie...
Definition RooPolyVar.h:25
static void fillCoeffValues(std::vector< double > &wksp, RooListProxy const &coefList)
std::string buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override
This function defines the analytical integral translation for the class.
double evaluate() const override
Calculate and return value of polynomial.
void computeBatch(double *output, size_t nEvents, RooFit::Detail::DataMap const &) const override
Compute multiple values of Polynomial.
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Advertise that we can internally integrate over x.
std::vector< double > _wksp
! do not persist
Definition RooPolyVar.h:46
Int_t _lowestOrder
Definition RooPolyVar.h:44
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Calculate and return analytical integral over x.
RooListProxy _coefList
Definition RooPolyVar.h:43
void translate(RooFit::Detail::CodeSquashContext &ctx) const override
This function defines a translation for each RooAbsReal based object that can be used to express the ...
RooRealProxy _x
Definition RooPolyVar.h:42
static void computeBatchImpl(RooAbsArg const *caller, double *output, size_t nEvents, RooFit::Detail::DataMap const &, RooAbsReal const &x, RooArgList const &coefs, int lowestOrder)
double max(const char *rname=nullptr) const
Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
const T & arg() const
Return reference to object held in proxy.
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
Double_t x[n]
Definition legend1.C:17
std::vector< std::span< const double > > VarVector
std::vector< double > ArgVector
void compute(Config cfg, Computer comp, RestrictArr output, size_t size, const VarVector &vars, ArgVector &extraArgs)
double polynomialIntegral(double const *coeffs, int nCoeffs, int lowestOrder, double xMin, double xMax)
In pdfMode, a coefficient for the constant term of 1.0 is implied if lowestOrder > 0.
double polynomialEvaluate(double const *coeffs, int nCoeffs, int lowestOrder, double x)
In pdfMode, a coefficient for the constant term of 1.0 is implied if lowestOrder > 0.
static void output()