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
22Class RooPolyVar is a 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
36#include "TError.h"
37
39
40////////////////////////////////////////////////////////////////////////////////
41/// Construct polynomial in x with coefficients in coefList. If
42/// lowestOrder is not zero, then the first element in coefList is
43/// interpreted as as the 'lowestOrder' coefficients and all
44/// subsequent coeffient elements are shifted by a similar amount.
45RooPolyVar::RooPolyVar(const char* name, const char* title,
46 RooAbsReal& x, const RooArgList& coefList, Int_t lowestOrder) :
47 RooAbsReal(name, title),
48 _x("x", "Dependent", this, x),
49 _coefList("coefList","List of coefficients",this),
50 _lowestOrder(lowestOrder)
51{
52 // Check lowest order
53 if (_lowestOrder<0) {
54 coutE(InputArguments) << "RooPolyVar::ctor(" << GetName()
55 << ") WARNING: lowestOrder must be >=0, setting value to 0" << std::endl;
56 _lowestOrder=0 ;
57 }
58
59 for(RooAbsArg * coef : coefList) {
60 if (!dynamic_cast<RooAbsReal*>(coef)) {
61 coutE(InputArguments) << "RooPolyVar::ctor(" << GetName() << ") ERROR: coefficient " << coef->GetName()
62 << " is not of type RooAbsReal" << std::endl;
63 R__ASSERT(0) ;
64 }
65 _coefList.add(*coef) ;
66 }
67}
68
69
70////////////////////////////////////////////////////////////////////////////////
71/// Constructor of flat polynomial function
72
73RooPolyVar::RooPolyVar(const char* name, const char* title,
74 RooAbsReal& x) :
75 RooAbsReal(name, title),
76 _x("x", "Dependent", this, x),
77 _coefList("coefList","List of coefficients",this),
78 _lowestOrder(1)
79{ }
80
81
82
83////////////////////////////////////////////////////////////////////////////////
84/// Copy constructor
85
86RooPolyVar::RooPolyVar(const RooPolyVar& other, const char* name) :
87 RooAbsReal(other, name),
88 _x("x", this, other._x),
89 _coefList("coefList",this,other._coefList),
90 _lowestOrder(other._lowestOrder)
91{ }
92
93
94////////////////////////////////////////////////////////////////////////////////
95/// Calculate and return value of polynomial
96
98{
99 const unsigned sz = _coefList.getSize();
100 const int lowestOrder = _lowestOrder;
101 if (!sz) return lowestOrder ? 1. : 0.;
102 _wksp.clear();
103 _wksp.reserve(sz);
104 {
105 const RooArgSet* nset = _coefList.nset();
106 for (const auto arg : _coefList) {
107 const auto c = static_cast<RooAbsReal*>(arg);
108 _wksp.push_back(c->getVal(nset));
109 }
110 }
111 const double x = _x;
112 double retVal = _wksp[sz - 1];
113 for (unsigned i = sz - 1; i--; ) retVal = _wksp[i] + x * retVal;
114 return retVal * std::pow(x, lowestOrder);
115}
116
117void RooPolyVar::computeBatchImpl(cudaStream_t *stream, double *output, size_t nEvents,
118 RooFit::Detail::DataMap const &dataMap, RooAbsReal const &x, RooArgList const &coefs,
119 int lowestOrder)
120{
121 if (coefs.empty()) {
122 output[0] = lowestOrder ? 1.0 : 0.0;
123 return;
124 }
125
127 vars.reserve(coefs.size() + 2);
128
129 // Fill the coefficients for the skipped orders. By a conventions started in
130 // RooPolynomial, if the zero-th order is skipped, it implies a coefficient
131 // for the constant term of one.
132 const double zero = 1.0;
133 const double one = 1.0;
134 for(int i = lowestOrder - 1; i >= 0; --i) {
135 vars.push_back(i == 0 ? RooSpan<const double>{&one, 1} : RooSpan<const double>{&zero, 1});
136 }
137
138 for (RooAbsArg *coef : coefs) {
139 vars.push_back(dataMap.at(coef));
140 }
141 vars.push_back(dataMap.at(&x));
143 RooBatchCompute::ArgVector extraArgs{double(vars.size() - 1)};
144 dispatch->compute(stream, RooBatchCompute::Polynomial, output, nEvents, vars, extraArgs);
145}
146
147/// Compute multiple values of Polynomial.
148void RooPolyVar::computeBatch(cudaStream_t *stream, double *output, size_t nEvents,
149 RooFit::Detail::DataMap const &dataMap) const
150{
151 computeBatchImpl(stream, output, nEvents, dataMap, _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 if (matchArgs(allVars, analVars, _x)) return 1;
160 return 0;
161}
162
163
164
165////////////////////////////////////////////////////////////////////////////////
166/// Calculate and return analytical integral over x
167
168double RooPolyVar::analyticalIntegral(Int_t code, const char* rangeName) const
169{
170 R__ASSERT(code==1) ;
171
172 const double xmin = _x.min(rangeName), xmax = _x.max(rangeName);
173 const int lowestOrder = _lowestOrder;
174 const unsigned sz = _coefList.getSize();
175 if (!sz) return xmax - xmin;
176 _wksp.clear();
177 _wksp.reserve(sz);
178 {
179 const RooArgSet* nset = _coefList.nset();
180 unsigned i = 1 + lowestOrder;
181 for(auto * c : static_range_cast<RooAbsReal*>(_coefList)) {
182 _wksp.push_back(c->getVal(nset) / double(i));
183 ++i;
184 }
185 }
186 double min = _wksp[sz - 1], max = _wksp[sz - 1];
187 for (unsigned i = sz - 1; i--; )
188 min = _wksp[i] + xmin * min, max = _wksp[i] + xmax * max;
189 return max * std::pow(xmax, 1 + lowestOrder) - min * std::pow(xmin, 1 + lowestOrder);
190}
#define c(i)
Definition RSha256.hxx:101
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
#define R__ASSERT(e)
Definition TError.h:117
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
Int_t getSize() const
Return the number of elements in the collection.
Storage_t::size_type size() const
const RooArgSet * nset() const
Definition RooAbsProxy.h:52
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
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
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
RooSpan< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
Definition DataMap.cxx:21
Class RooPolyVar is a RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficie...
Definition RooPolyVar.h:25
double evaluate() const override
Calculate and return value of polynomial.
static void computeBatchImpl(cudaStream_t *, double *output, size_t nEvents, RooFit::Detail::DataMap const &, RooAbsReal const &x, RooArgList const &coefs, int lowestOrder)
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:45
Int_t _lowestOrder
Definition RooPolyVar.h:43
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Calculate and return analytical integral over x.
RooListProxy _coefList
Definition RooPolyVar.h:42
RooRealProxy _x
Definition RooPolyVar.h:41
void computeBatch(cudaStream_t *, double *output, size_t nEvents, RooFit::Detail::DataMap const &) const override
Compute multiple values of Polynomial.
A simple container to hold a batch of data values.
Definition RooSpan.h:34
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< RooSpan< const double > > VarVector
R__EXTERN RooBatchComputeInterface * dispatchCUDA
R__EXTERN RooBatchComputeInterface * dispatchCPU
This dispatch pointer points to an implementation of the compute library, provided one has been loade...
std::vector< double > ArgVector
static void output()