ROOT   Reference Guide
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 *
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(_actualVars.getSize());
63 for (std::size_t i = 0; i < n; ++i) {
64 const RooAbsReal *r =
65 static_cast<const RooAbsReal *>(_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(_actualVars.getSize());
82 for (std::size_t i = 0; i < n; ++i) {
83 const RooAbsReal *r =
84 static_cast<const RooAbsReal *>(_actualVars.at(i));
85 retval->add(_coefficients[i], static_cast<RooAbsReal *>(r->clone()));
86 }
87 return retval;
88}
89
91 // add a new term
93 _coefficients.push_back(c);
94}
95
97 // set the coefficient with the given index
98 _coefficients[idx] = c;
99}
100
102 // get the coefficient with the given index
103 return _coefficients[idx];
104}
105
107 // call the evaluation
108#ifdef USE_UBLAS
110 result.assign(0.0);
111 const std::size_t n(_actualVars.getSize());
112 for (std::size_t i = 0; i < n; ++i) {
114 tmp.assign(static_cast<const RooAbsReal *>(_actualVars.at(i))->getVal());
115 result += _coefficients[i] * tmp;
116 }
117 return result.convert_to<double>();
118#else
119 const std::size_t n(_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 *>(_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 xlo,
134 double 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 xlo,
148 double 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}
#define c(i)
Definition: RSha256.hxx:101
#define ClassImp(name)
Definition: Rtypes.h:375
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
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
Return the number of elements in the collection.
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:60
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:104
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition: RooArgList.h:110
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...
RooLinearCombination is a class that helps perform linear combination of floating point numbers and p...
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
std::list< double > * plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const override
Interface for returning an optional hint for initial sampling points when constructing a curve projec...
TObject * clone(const char *newname) const override
void add(RooFit::SuperFloat c, RooAbsReal *t)
void setCoefficient(size_t idx, RooFit::SuperFloat c)
RooFit::SuperFloat getCoefficient(size_t idx)
void printArgs(std::ostream &os) const override
Print object arguments, ie its proxies.
std::list< double > * binBoundaries(RooAbsRealLValue &obs, double xlo, double xhi) const override
Retrieve bin boundaries if this distribution is binned in obs.
std::vector< RooFit::SuperFloat > _coefficients
Mother of all ROOT objects.
Definition: TObject.h:41
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 T(double x)
Definition: ChebyshevPol.h:34
double SuperFloat
Definition: Floats.h:29