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 <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 _coefList.addTyped<RooAbsReal>(coefList);
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// Constructor of flat polynomial function
67
68RooPolyVar::RooPolyVar(const char *name, const char *title, RooAbsReal &x)
69 : RooAbsReal(name, title),
70 _x("x", "Dependent", this, x),
71 _coefList("coefList", "List of coefficients", this),
72 _lowestOrder(1)
73{
74}
75
76////////////////////////////////////////////////////////////////////////////////
77/// Copy constructor
78
79RooPolyVar::RooPolyVar(const RooPolyVar &other, const char *name)
80 : RooAbsReal(other, name),
81 _x("x", this, other._x),
82 _coefList("coefList", this, other._coefList),
83 _lowestOrder(other._lowestOrder)
84{
85}
86
87void RooPolyVar::fillCoeffValues(std::vector<double> &wksp, RooListProxy const &coefList)
88{
89 wksp.clear();
90 wksp.reserve(coefList.size());
91 {
92 const RooArgSet *nset = coefList.nset();
93 for (const auto arg : coefList) {
94 const auto c = static_cast<RooAbsReal *>(arg);
95 wksp.push_back(c->getVal(nset));
96 }
97 }
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// Calculate and return value of polynomial
102
104{
105 const unsigned sz = _coefList.size();
106 if (!sz)
107 return _lowestOrder ? 1. : 0.;
108
110
112}
113
115{
116 const unsigned sz = _coefList.size();
117 if (!sz) {
118 ctx.addResult(this, std::to_string((_lowestOrder ? 1. : 0.)));
119 return;
120 }
121
122 ctx.addResult(this,
123 ctx.buildCall("RooFit::Detail::EvaluateFuncs::polynomialEvaluate", _coefList, sz, _lowestOrder, _x));
124}
125
126void RooPolyVar::computeBatchImpl(RooAbsArg const* caller, double *output, size_t nEvents,
127 RooFit::Detail::DataMap const &dataMap, RooAbsReal const &x, RooArgList const &coefs,
128 int lowestOrder)
129{
130 if (coefs.empty()) {
131 output[0] = lowestOrder ? 1.0 : 0.0;
132 return;
133 }
134
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 const double zero = 1.0;
142 const double one = 1.0;
143 for (int i = lowestOrder - 1; i >= 0; --i) {
144 vars.push_back(i == 0 ? std::span<const double>{&one, 1} : std::span<const double>{&zero, 1});
145 }
146
147 for (RooAbsArg *coef : coefs) {
148 vars.push_back(dataMap.at(coef));
149 }
150 vars.push_back(dataMap.at(&x));
151 RooBatchCompute::ArgVector extraArgs{double(vars.size() - 1)};
152 RooBatchCompute::compute(dataMap.config(caller), RooBatchCompute::Polynomial, output, nEvents, vars, extraArgs);
153}
154
155/// Compute multiple values of Polynomial.
156void RooPolyVar::computeBatch(double *output, size_t nEvents,
157 RooFit::Detail::DataMap const &dataMap) const
158{
159 computeBatchImpl(this, output, nEvents, dataMap, _x.arg(), _coefList, _lowestOrder);
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// Advertise that we can internally integrate over x
164
165Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
166{
167 if (matchArgs(allVars, analVars, _x))
168 return 1;
169 return 0;
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Calculate and return analytical integral over x
174
175double RooPolyVar::analyticalIntegral(Int_t code, const char *rangeName) const
176{
177 R__ASSERT(code == 1);
178
179 const double xmin = _x.min(rangeName);
180 const double xmax = _x.max(rangeName);
181 const unsigned sz = _coefList.size();
182 if (!sz)
183 return _lowestOrder ? xmax - xmin : 0.0;
184
186
188}
189
190std::string RooPolyVar::buildCallToAnalyticIntegral(Int_t /* code */, const char *rangeName,
192{
193 const double xmin = _x.min(rangeName);
194 const double xmax = _x.max(rangeName);
195 const unsigned sz = _coefList.size();
196 if (!sz)
197 return std::to_string(_lowestOrder ? xmax - xmin : 0.0);
198
199 return ctx.buildCall("RooFit::Detail::AnalyticalIntegrals::polynomialIntegral", _coefList, sz, _lowestOrder, xmin,
200 xmax);
201}
#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:40
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
Definition DataMap.cxx:22
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
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()