Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooLinearCombination.cxx
Go to the documentation of this file.
1// Author: Rahul Balasubramanian, Nikhef 08 Apr 2021
2/*****************************************************************************
3 * RooFit
4 * Authors: *
5 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
6 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
7 * *
8 * Copyright (c) 2000-2019, Regents of the University of California *
9 * and Stanford University. All rights reserved. *
10 * *
11 * Redistribution and use in source and binary forms, *
12 * with or without modification, are permitted according to the terms *
13 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
14 *****************************************************************************/
15
16//////////////////////////////////////////////////////////////////////////////
17/// \class RooLinearCombination
18/// RooLinearCombination is a class that helps perform linear combination of
19/// floating point numbers and permits handling them as multiprecision
20///
21
23
24#include "Math/Util.h"
25
27
28namespace {
29 template <class T> inline void assign(RooFit::SuperFloat &var, const T &val) {
30 #ifdef USE_UBLAS
31 var.assign(val);
32 #else
33 var = val;
34 #endif
35 }
36} // namespace
37
39 : _actualVars("actualVars", "Variables used by formula expression", this),
40 _nset(0) {
41 // constructor
42}
43
46 _actualVars("actualVars", "Variables used by formula expression", this),
47 _nset(0) {
48 // constructor
49}
50
52 const char *name)
53 : RooAbsReal(other, name),
54 _actualVars("actualVars", this, other._actualVars),
55 _coefficients(other._coefficients), _nset(0) {
56 // copy constructor
57}
58
59void RooLinearCombination::printArgs(std::ostream &os) const {
60 // detailed printing method
61 os << "[";
62 const std::size_t n(this->_actualVars.getSize());
63 for (std::size_t i = 0; i < n; ++i) {
64 const RooAbsReal *r =
65 static_cast<const RooAbsReal *>(this->_actualVars.at(i));
66 double c(_coefficients[i]);
67 if (c > 0 && i > 0)
68 os << "+";
69 os << c << "*" << r->GetTitle();
70 }
71 os << "]";
72}
73
75 // destructor
76}
77
78TObject *RooLinearCombination::clone(const char *newname) const {
79 // create a clone (deep copy) of this object
80 RooLinearCombination *retval = new RooLinearCombination(newname);
81 const std::size_t n(this->_actualVars.getSize());
82 for (std::size_t i = 0; i < n; ++i) {
83 const RooAbsReal *r =
84 static_cast<const RooAbsReal *>(this->_actualVars.at(i));
85 retval->add(this->_coefficients[i], static_cast<RooAbsReal *>(r->clone()));
86 }
87 return retval;
88}
89
91 // add a new term
92 _actualVars.add(*t);
93 _coefficients.push_back(c);
94}
95
97 // set the coefficient with the given index
98 this->_coefficients[idx] = c;
99}
100
102 // get the coefficient with the given index
103 return this->_coefficients[idx];
104}
105
107 // call the evaluation
108#ifdef USE_UBLAS
109 RooFit::SuperFloat result;
110 result.assign(0.0);
111 const std::size_t n(this->_actualVars.getSize());
112 for (std::size_t i = 0; i < n; ++i) {
114 tmp.assign(static_cast<const RooAbsReal *>(this->_actualVars.at(i))->getVal());
115 result += this->_coefficients[i] * tmp;
116 }
117 return result.convert_to<double>();
118#else
119 const std::size_t n(this->_actualVars.getSize());
120 std::vector<double> values(n);
121 for (std::size_t i = 0; i < n; ++i) {
122 values[i] = _coefficients[i] * static_cast<const RooAbsReal *>(this->_actualVars.at(i))->getVal();
123 }
124 // the values might span multiple orders of magnitudes, and to minimize
125 // precision loss, we sum up the values from the smallest to the largest
126 // absolute value.
127 std::sort(values.begin(), values.end(), [](double const& x, double const& y){ return std::abs(x) < std::abs(y); });
128 return ROOT::Math::KahanSum<double>::Accumulate(values.begin(), values.end()).Sum();
129#endif
130}
131
133 Double_t xlo,
134 Double_t xhi) const {
135 // Forward the plot sampling hint from the p.d.f. that defines the observable
136 // obs
137 for(auto const& func : _actualVars) {
138 auto binb = static_cast<RooAbsReal*>(func)->binBoundaries(obs, xlo, xhi);
139 if (binb) {
140 return binb;
141 }
142 }
143 return 0;
144}
145
147 Double_t xlo,
148 Double_t xhi) const {
149 // Forward the plot sampling hint from the p.d.f. that defines the observable
150 // obs
151 for(auto const& func : _actualVars) {
152 auto hint = static_cast<RooAbsReal*>(func)->plotSamplingHint(obs, xlo, xhi);
153 if (hint) {
154 return hint;
155 }
156 }
157 return 0;
158}
ROOT::R::TRInterface & r
Definition Object.C:4
#define c(i)
Definition RSha256.hxx:101
double Double_t
Definition RtypesCore.h:59
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
static KahanSum< T, N > Accumulate(Iterator begin, Iterator end, T initialValue=T{})
Iterate over a range and return an instance of a KahanSum.
Definition Util.h:211
Int_t getSize() const
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:64
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:94
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooLinearCombination is a class that helps perform linear combination of floating point numbers and p...
virtual std::list< Double_t > * binBoundaries(RooAbsRealLValue &obs, Double_t xlo, Double_t xhi) const override
Retrieve bin boundaries if this distribution is binned in obs.
virtual TObject * clone(const char *newname) const override
void add(RooFit::SuperFloat c, RooAbsReal *t)
void setCoefficient(size_t idx, RooFit::SuperFloat c)
virtual std::list< Double_t > * plotSamplingHint(RooAbsRealLValue &obs, Double_t xlo, Double_t xhi) const override
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
RooFit::SuperFloat getCoefficient(size_t idx)
virtual Double_t evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
virtual void printArgs(std::ostream &os) const override
Print object arguments, ie its proxies.
std::vector< RooFit::SuperFloat > _coefficients
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Reimplementation of standard RooArgList::add()
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:473
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
double SuperFloat
Definition Floats.h:29