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
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
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
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 RooArgList const &coefs, int lowestOrder)
116{
117 std::span<double> output = ctx.output();
118 if (coefs.empty()) {
119 output[0] = lowestOrder ? 1.0 : 0.0;
120 return;
121 }
122
123 std::vector<std::span<const double>> vars;
124 vars.reserve(coefs.size() + 2);
125
126 // Fill the coefficients for the skipped orders. By a conventions started in
127 // RooPolynomial, if the zero-th order is skipped, it implies a coefficient
128 // for the constant term of one.
129 std::array<double, RooBatchCompute::bufferSize> zeros;
130 std::array<double, RooBatchCompute::bufferSize> ones;
131 std::fill_n(zeros.data(), zeros.size(), 0.0);
132 std::fill_n(ones.data(), ones.size(), 1.0);
133 std::span<const double> zerosSpan{zeros.data(), 1};
134 std::span<const double> onesSpan{ones.data(), 1};
135 for (int i = lowestOrder - 1; i >= 0; --i) {
136 vars.push_back(i == 0 ? onesSpan : zerosSpan);
137 }
138
139 for (RooAbsArg *coef : coefs) {
140 vars.push_back(ctx.at(coef));
141 }
142 vars.push_back(ctx.at(&x));
143 std::array<double, 1> extraArgs{double(vars.size() - 1)};
145}
146
147/// Compute multiple values of Polynomial.
149{
150 doEvalImpl(this, ctx, _x.arg(), _coefList, _lowestOrder);
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Advertise that we can internally integrate over x
155
156Int_t RooPolyVar::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
157{
158 return matchArgs(allVars, analVars, _x) ? 1 : 0;
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Calculate and return analytical integral over x
163
164double RooPolyVar::analyticalIntegral(Int_t code, const char *rangeName) const
165{
166 R__ASSERT(code == 1);
167
168 const double xmin = _x.min(rangeName);
169 const double xmax = _x.max(rangeName);
170 const unsigned sz = _coefList.size();
171 if (!sz)
172 return _lowestOrder ? xmax - xmin : 0.0;
173
175
177}
#define c(i)
Definition RSha256.hxx:101
#define coutE(a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#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: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.
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
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)
void doEval(RooFit::EvalContext &) const override
Compute multiple values of Polynomial.
double evaluate() const override
Calculate and return value of polynomial.
RooRealProxy const & x() const
Definition RooPolyVar.h:37
RooArgList const & coefList() const
Definition RooPolyVar.h:38
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Advertise that we can internally integrate over x.
int lowestOrder() const
Definition RooPolyVar.h:39
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
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:49
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:129
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:508
static void output()