Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooPower.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 *
4 * Copyright (c) 2022, CERN
5 *
6 * Redistribution and use in source and binary forms,
7 * with or without modification, are permitted according to the terms
8 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
9 */
10
11/** \class RooPower
12 \ingroup Roofit
13
14RooPower implements a power law PDF of the form
15\f[ f(x) = \mathcal{N} \cdot \sum_{i} a_{i} * x^{b_i} \f]
16
17\image html RooPower.png
18**/
19
20#include <RooPower.h>
21
22#include <RooAbsReal.h>
23#include <RooArgList.h>
24#include <RooMsgService.h>
25#include "RooBatchCompute.h"
26
27#include <TError.h>
28
29#include <cmath>
30#include <cassert>
31#include <sstream>
32
34
35////////////////////////////////////////////////////////////////////////////////
36/// Create a power law in the variable `x`.
37/// \param[in] name Name of the PDF
38/// \param[in] title Title for plotting the PDF
39/// \param[in] x The variable of the polynomial
40/// \param[in] coefList The coefficients \f$ a_i \f$
41/// \param[in] expList The exponentials \f$ b_i \f$
42/// \f[
43/// \sum_{i=0}^{n} a_{i} * x^{b_{i}}
44/// \f]
45///
46/// This means that
47/// \code{.cpp}
48/// RooPower powl("pow", "pow", x, RooArgList(a1, a2), RooArgList(b1,b2))
49/// \endcode
50/// computes
51/// \f[
52/// \mathrm{pol}(x) = a1 * x^b1 + a2 * x^b2
53/// \f]
54
55RooPower::RooPower(const char *name, const char *title, RooAbsReal &x, const RooArgList &coefList,
56 const RooArgList &expList)
57 : RooAbsPdf(name, title),
58 _x("x", "Dependent", this, x),
59 _coefList("coefList", "List of coefficients", this),
60 _expList("expList", "List of exponents", this)
61{
62 if (coefList.size() != expList.size()) {
63 coutE(InputArguments) << "RooPower::ctor(" << GetName()
64 << ") ERROR: coefficient list and exponent list must be of same length" << std::endl;
65 return;
66 }
69}
70
71////////////////////////////////////////////////////////////////////////////////
72/// Copy constructor
73
74RooPower::RooPower(const RooPower &other, const char *name)
75 : RooAbsPdf(other, name),
76 _x("x", this, other._x),
77 _coefList("coefList", this, other._coefList),
78 _expList("expList", this, other._expList)
79{
80}
81
82////////////////////////////////////////////////////////////////////////////////
83
84/// Compute multiple values of Power distribution.
85void RooPower::computeBatch(double *output, size_t nEvents, RooFit::Detail::DataMap const &dataMap) const
86{
88 vars.reserve(2 * _coefList.size() + 1);
89 vars.push_back(dataMap.at(_x));
90
91 assert(_coefList.size() == _expList.size());
92
93 for (std::size_t i = 0; i < _coefList.size(); ++i) {
94 vars.push_back(dataMap.at(&_coefList[i]));
95 vars.push_back(dataMap.at(&_expList[i]));
96 }
97
99 args.push_back(_coefList.size());
100
101 RooBatchCompute::compute(dataMap.config(this), RooBatchCompute::Power, output, nEvents, vars, args);
102}
103
104////////////////////////////////////////////////////////////////////////////////
105
106double RooPower::evaluate() const
107{
108 // Calculate and return value of polynomial
109
110 const unsigned sz = _coefList.size();
111 if (!sz) {
112 return 0.;
113 }
114
115 std::vector<double> coefs;
116 std::vector<double> exps;
117 coefs.reserve(sz);
118 exps.reserve(sz);
119 const RooArgSet *nset = _coefList.nset();
120 for (auto c : _coefList) {
121 coefs.push_back(static_cast<RooAbsReal *>(c)->getVal(nset));
122 }
123 for (auto c : _expList) {
124 exps.push_back(static_cast<RooAbsReal *>(c)->getVal(nset));
125 }
126 double x = this->_x;
127 double retval = 0;
128 for (unsigned int i = 0; i < sz; ++i) {
129 retval += coefs[i] * std::pow(x, exps[i]);
130 }
131 return retval;
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// Advertise to RooFit that this function can be analytically integrated.
136int RooPower::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
137{
138 if (matchArgs(allVars, analVars, _x))
139 return 1;
140 return 0;
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
145double RooPower::analyticalIntegral(int /*code*/, const char *rangeName) const
146{
147 const double xmin = _x.min(rangeName);
148 const double xmax = _x.max(rangeName);
149 const unsigned sz = _coefList.size();
150 if (!sz) {
151 return xmax - xmin;
152 }
153
154 std::vector<double> coefs;
155 std::vector<double> exps;
156 coefs.reserve(sz);
157 exps.reserve(sz);
158 const RooArgSet *nset = _coefList.nset();
159 for (auto c : _coefList) {
160 coefs.push_back(static_cast<RooAbsReal *>(c)->getVal(nset));
161 }
162 for (auto c : _expList) {
163 exps.push_back(static_cast<RooAbsReal *>(c)->getVal(nset));
164 }
165
166 double retval = 0;
167 for (unsigned int i = 0; i < sz; ++i) {
168 if (exps[i] == -1) {
169 retval += coefs[i] * (log(xmax) - log(xmin));
170 } else {
171 retval += coefs[i] / (exps[i] + 1) * (pow(xmax, (exps[i] + 1)) - pow(xmin, (exps[i] + 1)));
172 }
173 }
174 return retval;
175}
176
177std::string RooPower::getFormulaExpression(bool expand) const
178{
179 std::stringstream ss;
180 for (std::size_t i = 0; i < _coefList.size(); ++i) {
181 if (i != 0)
182 ss << "+";
183 if (expand) {
184 ss << static_cast<RooAbsReal *>(_coefList.at(i))->getVal();
185 } else {
186 ss << _coefList.at(i)->GetName();
187 }
188 ss << "*pow(" << _x.GetName() << ",";
189 if (expand) {
190 ss << static_cast<RooAbsReal *>(_expList.at(i))->getVal();
191 } else {
192 ss << _expList.at(i)->GetName();
193 }
194 ss << ")";
195 }
196 return ss.str();
197}
#define c(i)
Definition RSha256.hxx:101
#define coutE(a)
#define ClassImp(name)
Definition Rtypes.h:377
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
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 interface for all probability density functions.
Definition RooAbsPdf.h:40
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
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
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
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
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
RooPower implements a power law PDF of the form.
Definition RooPower.h:20
RooListProxy _expList
Definition RooPower.h:45
std::string getFormulaExpression(bool expand) const
Definition RooPower.cxx:177
void computeBatch(double *output, size_t size, RooFit::Detail::DataMap const &) const override
do not persist
Definition RooPower.cxx:85
RooPower()
Definition RooPower.h:22
RooArgList const & coefList() const
Get the list of coefficients.
Definition RooPower.h:32
RooListProxy _coefList
Definition RooPower.h:44
RooRealProxy _x
Definition RooPower.h:43
int getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Advertise to RooFit that this function can be analytically integrated.
Definition RooPower.cxx:136
double analyticalIntegral(int code, const char *rangeName=nullptr) const override
Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
Definition RooPower.cxx:145
double evaluate() const override
Evaluation.
Definition RooPower.cxx:106
RooArgList const & expList() const
Get the list of exponents.
Definition RooPower.h:35
double max(const char *rname=nullptr) const
Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
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)
static void output()