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