Logo ROOT  
Reference Guide
 
Loading...
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 *
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
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
35
36#include "TError.h"
37
38#include <algorithm>
39#include <array>
40#include <cmath>
41
43
44////////////////////////////////////////////////////////////////////////////////
45/// Construct polynomial in x with coefficients in coefList. If
46/// lowestOrder is not zero, then the first element in coefList is
47/// interpreted as as the 'lowestOrder' coefficients and all
48/// subsequent coefficient elements are shifted by a similar amount.
49RooPolyVar::RooPolyVar(const char *name, const char *title, RooAbsReal &x, const RooArgList &coefList,
50 Int_t lowestOrder)
51 : RooAbsReal(name, title),
52 _x("x", "Dependent", this, x),
53 _coefList("coefList", "List of coefficients", this),
54 _lowestOrder(lowestOrder)
55{
56 // Check lowest order
57 if (_lowestOrder < 0) {
58 coutE(InputArguments) << "RooPolyVar::ctor(" << GetName()
59 << ") WARNING: lowestOrder must be >=0, setting value to 0" << std::endl;
60 _lowestOrder = 0;
61 }
62
63 _coefList.addTyped<RooAbsReal>(coefList);
64}
65
66////////////////////////////////////////////////////////////////////////////////
67/// Constructor of flat polynomial function
68
69RooPolyVar::RooPolyVar(const char *name, const char *title, RooAbsReal &x)
70 : RooAbsReal(name, title),
71 _x("x", "Dependent", this, x),
72 _coefList("coefList", "List of coefficients", this),
73 _lowestOrder(1)
74{
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Copy constructor
79
80RooPolyVar::RooPolyVar(const RooPolyVar &other, const char *name)
81 : RooAbsReal(other, name),
82 _x("x", this, other._x),
83 _coefList("coefList", this, other._coefList),
84 _lowestOrder(other._lowestOrder)
85{
86}
87
88void RooPolyVar::fillCoeffValues(std::vector<double> &wksp, RooListProxy const &coefList)
89{
90 wksp.clear();
91 wksp.reserve(coefList.size());
92 {
93 const RooArgSet *nset = coefList.nset();
94 for (const auto arg : coefList) {
95 const auto c = static_cast<RooAbsReal *>(arg);
96 wksp.push_back(c->getVal(nset));
97 }
98 }
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Calculate and return value of polynomial
103
105{
106 const unsigned sz = _coefList.size();
107 if (!sz)
108 return _lowestOrder ? 1. : 0.;
109
111
113}
114
116{
117 const unsigned sz = _coefList.size();
118 if (!sz) {
119 ctx.addResult(this, std::to_string((_lowestOrder ? 1. : 0.)));
120 return;
121 }
122
123 ctx.addResult(this, ctx.buildCall("RooFit::Detail::MathFuncs::polynomial", _coefList, sz, _lowestOrder, _x));
124}
125
127 RooArgList const &coefs, int lowestOrder)
128{
129 std::span<double> output = ctx.output();
130 if (coefs.empty()) {
131 output[0] = lowestOrder ? 1.0 : 0.0;
132 return;
133 }
134
135 std::vector<std::span<const double>> vars;
136 vars.reserve(coefs.size() + 2);
137
138 // Fill the coefficients for the skipped orders. By a conventions started in
139 // RooPolynomial, if the zero-th order is skipped, it implies a coefficient
140 // for the constant term of one.
141 std::array<double, RooBatchCompute::bufferSize> zeros;
142 std::array<double, RooBatchCompute::bufferSize> ones;
143 std::fill_n(zeros.data(), zeros.size(), 0.0);
144 std::fill_n(ones.data(), ones.size(), 1.0);
145 std::span<const double> zerosSpan{zeros.data(), 1};
146 std::span<const double> onesSpan{ones.data(), 1};
147 for (int i = lowestOrder - 1; i >= 0; --i) {
148 vars.push_back(i == 0 ? onesSpan : zerosSpan);
149 }
150
151 for (RooAbsArg *coef : coefs) {
152 vars.push_back(ctx.at(coef));
153 }
154 vars.push_back(ctx.at(&x));
155 std::array<double, 1> extraArgs{double(vars.size() - 1)};
156 RooBatchCompute::compute(ctx.config(caller), RooBatchCompute::Polynomial, ctx.output(), vars, extraArgs);
157}
158
159/// Compute multiple values of Polynomial.
161{
162 doEvalImpl(this, ctx, _x.arg(), _coefList, _lowestOrder);
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Advertise that we can internally integrate over x
167
168Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
169{
170 return matchArgs(allVars, analVars, _x) ? 1 : 0;
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Calculate and return analytical integral over x
175
176double RooPolyVar::analyticalIntegral(Int_t code, const char *rangeName) const
177{
178 R__ASSERT(code == 1);
179
180 const double xmin = _x.min(rangeName);
181 const double xmax = _x.max(rangeName);
182 const unsigned sz = _coefList.size();
183 if (!sz)
184 return _lowestOrder ? xmax - xmin : 0.0;
185
187
189}
190
191std::string RooPolyVar::buildCallToAnalyticIntegral(Int_t /* code */, const char *rangeName,
193{
194 const double xmin = _x.min(rangeName);
195 const double xmax = _x.max(rangeName);
196 const unsigned sz = _coefList.size();
197 if (!sz)
198 return std::to_string(_lowestOrder ? xmax - xmin : 0.0);
199
200 return ctx.buildCall("RooFit::Detail::MathFuncs::polynomialIntegral", _coefList, sz, _lowestOrder, xmin, xmax);
201}
#define c(i)
Definition RSha256.hxx:101
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
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:79
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:24
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
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 polynomial(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.
Definition MathFuncs.h:130
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.
Definition MathFuncs.h:481
static void output()