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
22A 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 "RooPolyVar.h"
30#include "RooArgList.h"
31#include "RooMsgService.h"
32#include "RooBatchCompute.h"
33
36
37#include "TError.h"
38
39#include <algorithm>
40#include <array>
41#include <cmath>
42
44
45////////////////////////////////////////////////////////////////////////////////
46/// Construct polynomial in x with coefficients in coefList. If
47/// lowestOrder is not zero, then the first element in coefList is
48/// interpreted as as the 'lowestOrder' coefficients and all
49/// subsequent coefficient elements are shifted by a similar amount.
50RooPolyVar::RooPolyVar(const char *name, const char *title, RooAbsReal &x, const RooArgList &coefList,
51 Int_t lowestOrder)
52 : RooAbsReal(name, title),
53 _x("x", "Dependent", this, x),
54 _coefList("coefList", "List of coefficients", this),
55 _lowestOrder(lowestOrder)
56{
57 // Check lowest order
58 if (_lowestOrder < 0) {
59 coutE(InputArguments) << "RooPolyVar::ctor(" << GetName()
60 << ") WARNING: lowestOrder must be >=0, setting value to 0" << std::endl;
61 _lowestOrder = 0;
62 }
63
65}
66
67////////////////////////////////////////////////////////////////////////////////
68/// Constructor of flat polynomial function
69
70RooPolyVar::RooPolyVar(const char *name, const char *title, RooAbsReal &x)
71 : RooAbsReal(name, title),
72 _x("x", "Dependent", this, x),
73 _coefList("coefList", "List of coefficients", this),
74 _lowestOrder(1)
75{
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// Copy constructor
80
81RooPolyVar::RooPolyVar(const RooPolyVar &other, const char *name)
82 : RooAbsReal(other, name),
83 _x("x", this, other._x),
84 _coefList("coefList", this, other._coefList),
85 _lowestOrder(other._lowestOrder)
86{
87}
88
89void RooPolyVar::fillCoeffValues(std::vector<double> &wksp, RooListProxy const &coefList)
90{
91 wksp.clear();
92 wksp.reserve(coefList.size());
93 {
94 const RooArgSet *nset = coefList.nset();
95 for (const auto arg : coefList) {
96 const auto c = static_cast<RooAbsReal *>(arg);
97 wksp.push_back(c->getVal(nset));
98 }
99 }
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// Calculate and return value of polynomial
104
106{
107 const unsigned sz = _coefList.size();
108 if (!sz)
109 return _lowestOrder ? 1. : 0.;
110
112
114}
115
117{
118 const unsigned sz = _coefList.size();
119 if (!sz) {
120 ctx.addResult(this, std::to_string((_lowestOrder ? 1. : 0.)));
121 return;
122 }
123
125 ctx.buildCall("RooFit::Detail::EvaluateFuncs::polynomialEvaluate", _coefList, sz, _lowestOrder, _x));
126}
127
129 RooArgList const &coefs, int lowestOrder)
130{
131 std::span<double> output = ctx.output();
132 if (coefs.empty()) {
133 output[0] = lowestOrder ? 1.0 : 0.0;
134 return;
135 }
136
137 std::vector<std::span<const double>> vars;
138 vars.reserve(coefs.size() + 2);
139
140 // Fill the coefficients for the skipped orders. By a conventions started in
141 // RooPolynomial, if the zero-th order is skipped, it implies a coefficient
142 // for the constant term of one.
143 std::array<double, RooBatchCompute::bufferSize> zeros;
144 std::array<double, RooBatchCompute::bufferSize> ones;
145 std::fill_n(zeros.data(), zeros.size(), 0.0);
146 std::fill_n(ones.data(), ones.size(), 1.0);
147 std::span<const double> zerosSpan{zeros.data(), 1};
148 std::span<const double> onesSpan{ones.data(), 1};
149 for (int i = lowestOrder - 1; i >= 0; --i) {
150 vars.push_back(i == 0 ? onesSpan : zerosSpan);
151 }
152
153 for (RooAbsArg *coef : coefs) {
154 vars.push_back(ctx.at(coef));
155 }
156 vars.push_back(ctx.at(&x));
157 std::array<double, 1> extraArgs{double(vars.size() - 1)};
158 RooBatchCompute::compute(ctx.config(caller), RooBatchCompute::Polynomial, ctx.output(), vars, extraArgs);
159}
160
161/// Compute multiple values of Polynomial.
163{
164 doEvalImpl(this, ctx, _x.arg(), _coefList, _lowestOrder);
165}
166
167////////////////////////////////////////////////////////////////////////////////
168/// Advertise that we can internally integrate over x
169
170Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
171{
172 if (matchArgs(allVars, analVars, _x))
173 return 1;
174 return 0;
175}
176
177////////////////////////////////////////////////////////////////////////////////
178/// Calculate and return analytical integral over x
179
180double RooPolyVar::analyticalIntegral(Int_t code, const char *rangeName) const
181{
182 R__ASSERT(code == 1);
183
184 const double xmin = _x.min(rangeName);
185 const double xmax = _x.max(rangeName);
186 const unsigned sz = _coefList.size();
187 if (!sz)
188 return _lowestOrder ? xmax - xmin : 0.0;
189
191
193}
194
195std::string RooPolyVar::buildCallToAnalyticIntegral(Int_t /* code */, const char *rangeName,
197{
198 const double xmin = _x.min(rangeName);
199 const double xmax = _x.max(rangeName);
200 const unsigned sz = _coefList.size();
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
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
Storage_t::size_type size() const
bool addTyped(const RooAbsCollection &list, bool silent=false)
Adds elements of a given RooAbsCollection to the container if they match the specified type.
const RooArgSet * nset() const
Definition RooAbsProxy.h:52
Abstract base class for objects that represent a real value and implements functionality common to al...
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
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.
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
std::span< double > output()
RooBatchCompute::Config config(RooAbsArg const *arg) const
A RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficients.
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.
void doEval(RooFit::EvalContext &) const override
Compute multiple values of Polynomial.
double evaluate() const override
Calculate and return value 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
static void doEvalImpl(RooAbsArg const *caller, RooFit::EvalContext &, RooAbsReal const &x, RooArgList const &coefs, int lowestOrder)
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
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
void compute(Config cfg, Computer comp, std::span< double > output, VarSpan vars, ArgSpan 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()