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
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
64 _coefList.addTyped<RooAbsReal>(coefList);
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
124 ctx.addResult(this,
125 ctx.buildCall("RooFit::Detail::EvaluateFuncs::polynomialEvaluate", _coefList, sz, _lowestOrder, _x));
126}
127
128void RooPolyVar::computeBatchImpl(RooAbsArg const* caller, double *output, size_t nEvents,
129 RooFit::Detail::DataMap const &dataMap, RooAbsReal const &x, RooArgList const &coefs,
130 int lowestOrder)
131{
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(dataMap.at(coef));
155 }
156 vars.push_back(dataMap.at(&x));
157 std::array<double, 1> extraArgs{double(vars.size() - 1)};
158 RooBatchCompute::compute(dataMap.config(caller), RooBatchCompute::Polynomial, output, nEvents, vars, extraArgs);
159}
160
161/// Compute multiple values of Polynomial.
162void RooPolyVar::computeBatch(double *output, size_t nEvents,
163 RooFit::Detail::DataMap const &dataMap) const
164{
165 computeBatchImpl(this, output, nEvents, dataMap, _x.arg(), _coefList, _lowestOrder);
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Advertise that we can internally integrate over x
170
171Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
172{
173 if (matchArgs(allVars, analVars, _x))
174 return 1;
175 return 0;
176}
177
178////////////////////////////////////////////////////////////////////////////////
179/// Calculate and return analytical integral over x
180
181double RooPolyVar::analyticalIntegral(Int_t code, const char *rangeName) const
182{
183 R__ASSERT(code == 1);
184
185 const double xmin = _x.min(rangeName);
186 const double xmax = _x.max(rangeName);
187 const unsigned sz = _coefList.size();
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);
200 const double xmax = _x.max(rangeName);
201 const unsigned sz = _coefList.size();
202 if (!sz)
203 return std::to_string(_lowestOrder ? xmax - xmin : 0.0);
204
205 return ctx.buildCall("RooFit::Detail::AnalyticalIntegrals::polynomialIntegral", _coefList, sz, _lowestOrder, xmin,
206 xmax);
207}
#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.
RooBatchCompute::Config config(RooAbsArg const *arg) const
Definition DataMap.cxx:73
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
Definition DataMap.cxx:35
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.
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
void compute(Config cfg, Computer comp, RestrictArr output, size_t size, 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()