Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooPowerSum.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 RooPowerSum
12 \ingroup Roofit
13
14RooPowerSum 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 RooPowerSum.png
18**/
19
20#include <RooPowerSum.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 <array>
30#include <cassert>
31#include <cmath>
32#include <sstream>
33
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/// RooPowerSum 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
55RooPowerSum::RooPowerSum(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) << "RooPowerSum::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
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.
86{
87 std::vector<std::span<const double>> vars;
88 vars.reserve(2 * _coefList.size() + 1);
89 vars.push_back(ctx.at(_x));
90
92
93 for (std::size_t i = 0; i < _coefList.size(); ++i) {
94 vars.push_back(ctx.at(&_coefList[i]));
95 vars.push_back(ctx.at(&_expList[i]));
96 }
97
98 std::array<double, 1> args{static_cast<double>(_coefList.size())};
99
100 RooBatchCompute::compute(ctx.config(this), RooBatchCompute::Power, ctx.output(), vars, args);
101}
102
103////////////////////////////////////////////////////////////////////////////////
104
106{
107 // Calculate and return value of polynomial
108
109 const unsigned sz = _coefList.size();
110 if (!sz) {
111 return 0.;
112 }
113
114 std::vector<double> coefs;
115 std::vector<double> exps;
116 coefs.reserve(sz);
117 exps.reserve(sz);
118 const RooArgSet *nset = _coefList.nset();
119 for (auto c : _coefList) {
120 coefs.push_back(static_cast<RooAbsReal *>(c)->getVal(nset));
121 }
122 for (auto c : _expList) {
123 exps.push_back(static_cast<RooAbsReal *>(c)->getVal(nset));
124 }
125 double x = this->_x;
126 double retval = 0;
127 for (unsigned int i = 0; i < sz; ++i) {
128 retval += coefs[i] * std::pow(x, exps[i]);
129 }
130 return retval;
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// Advertise to RooFit that this function can be analytically integrated.
135int RooPowerSum::getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char * /*rangeName*/) const
136{
137 if (matchArgs(allVars, analVars, _x))
138 return 1;
139 return 0;
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
144double RooPowerSum::analyticalIntegral(int /*code*/, const char *rangeName) const
145{
146 const double xmin = _x.min(rangeName);
147 const double xmax = _x.max(rangeName);
148 const unsigned sz = _coefList.size();
149 if (!sz) {
150 return xmax - xmin;
151 }
152
153 std::vector<double> coefs;
154 std::vector<double> exps;
155 coefs.reserve(sz);
156 exps.reserve(sz);
157 const RooArgSet *nset = _coefList.nset();
158 for (auto c : _coefList) {
159 coefs.push_back(static_cast<RooAbsReal *>(c)->getVal(nset));
160 }
161 for (auto c : _expList) {
162 exps.push_back(static_cast<RooAbsReal *>(c)->getVal(nset));
163 }
164
165 double retval = 0;
166 for (unsigned int i = 0; i < sz; ++i) {
167 if (exps[i] == -1) {
168 retval += coefs[i] * (log(xmax) - log(xmin));
169 } else {
170 retval += coefs[i] / (exps[i] + 1) * (pow(xmax, (exps[i] + 1)) - pow(xmin, (exps[i] + 1)));
171 }
172 }
173 return retval;
174}
175
177{
178 std::stringstream ss;
179 for (std::size_t i = 0; i < _coefList.size(); ++i) {
180 if (i != 0)
181 ss << "+";
182 if (expand) {
184 } else {
185 ss << _coefList.at(i)->GetName();
186 }
187 ss << "*pow(" << _x.GetName() << ",";
188 if (expand) {
190 } else {
191 ss << _expList.at(i)->GetName();
192 }
193 ss << ")";
194 }
195 return ss.str();
196}
#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.
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:24
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
std::span< double > output()
RooBatchCompute::Config config(RooAbsArg const *arg) const
RooPowerSum implements a power law PDF of the form.
Definition RooPowerSum.h:20
RooArgList const & coefList() const
Get the list of coefficients.
Definition RooPowerSum.h:32
double evaluate() const override
Evaluation.
int getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Advertise to RooFit that this function can be analytically integrated.
double analyticalIntegral(int code, const char *rangeName=nullptr) const override
Do the analytical integral according to the code that was returned by getAnalyticalIntegral().
RooRealProxy _x
Definition RooPowerSum.h:43
void doEval(RooFit::EvalContext &) const override
do not persist
RooListProxy _expList
Definition RooPowerSum.h:45
RooArgList const & expList() const
Get the list of exponents.
Definition RooPowerSum.h:35
std::string getFormulaExpression(bool expand) const
RooListProxy _coefList
Definition RooPowerSum.h:44
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:49
Double_t x[n]
Definition legend1.C:17
void compute(Config cfg, Computer comp, std::span< double > output, VarSpan vars, ArgSpan extraArgs={})