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
26
27namespace {
28 template <class T> inline void assign(RooFit::SuperFloat &var, const T &val) {
29 #ifdef USE_UBLAS
30 var.assign(val);
31 #else
32 var = val;
33 #endif
34 }
35} // namespace
36
38 : _actualVars("actualVars", "Variables used by formula expression", this),
39 _nset(nullptr) {
40 // constructor
41}
42
45 _actualVars("actualVars", "Variables used by formula expression", this),
46 _nset(nullptr) {
47 // constructor
48}
49
51 const char *name)
53 _actualVars("actualVars", this, other._actualVars),
54 _coefficients(other._coefficients), _nset(nullptr) {
55 // copy constructor
56}
57
58void RooLinearCombination::printArgs(std::ostream &os) const {
59 // detailed printing method
60 os << "[";
61 const std::size_t n(_actualVars.size());
62 for (std::size_t i = 0; i < n; ++i) {
63 const RooAbsReal *r =
64 static_cast<const RooAbsReal *>(_actualVars.at(i));
65 double c(_coefficients[i]);
66 if (c > 0 && i > 0)
67 os << "+";
68 os << c << "*" << r->GetTitle();
69 }
70 os << "]";
71}
72
76
78 // create a clone (deep copy) of this object
80 const std::size_t n(_actualVars.size());
81 for (std::size_t i = 0; i < n; ++i) {
82 const RooAbsReal *r =
83 static_cast<const RooAbsReal *>(_actualVars.at(i));
84 retval->add(_coefficients[i], static_cast<RooAbsReal *>(r->clone()));
85 }
86 return retval;
87}
88
90 // add a new term
91 _actualVars.add(*t);
92 _coefficients.push_back(c);
93}
94
96 // set the coefficient with the given index
97 _coefficients[idx] = c;
98}
99
101 // get the coefficient with the given index
102 return _coefficients[idx];
103}
104
106 // call the evaluation
107#ifdef USE_UBLAS
109 result.assign(0.0);
110 const std::size_t n(_actualVars.size());
111 for (std::size_t i = 0; i < n; ++i) {
113 tmp.assign(static_cast<const RooAbsReal *>(_actualVars.at(i))->getVal());
114 result += _coefficients[i] * tmp;
115 }
116 return result.convert_to<double>();
117#else
118 const std::size_t n(_actualVars.size());
119 std::vector<double> values(n);
120 for (std::size_t i = 0; i < n; ++i) {
121 values[i] = _coefficients[i] * static_cast<const RooAbsReal *>(_actualVars.at(i))->getVal();
122 }
123 // the values might span multiple orders of magnitudes, and to minimize
124 // precision loss, we sum up the values from the smallest to the largest
125 // absolute value.
126 std::sort(values.begin(), values.end(), [](double const& x, double const& y){ return std::abs(x) < std::abs(y); });
127 return ROOT::Math::KahanSum<double>::Accumulate(values.begin(), values.end()).Sum();
128#endif
129}
130
132 double xlo,
133 double xhi) const {
134 // Forward the plot sampling hint from the p.d.f. that defines the observable
135 // obs
136 for(auto const& func : _actualVars) {
137 auto binb = static_cast<RooAbsReal*>(func)->binBoundaries(obs, xlo, xhi);
138 if (binb) {
139 return binb;
140 }
141 }
142 return nullptr;
143}
144
146 double xlo,
147 double xhi) const {
148 // Forward the plot sampling hint from the p.d.f. that defines the observable
149 // obs
150 for(auto const& func : _actualVars) {
151 auto hint = static_cast<RooAbsReal*>(func)->plotSamplingHint(obs, xlo, xhi);
152 if (hint) {
153 return hint;
154 }
155 }
156 return nullptr;
157}
#define c(i)
Definition RSha256.hxx:101
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:225
Storage_t::size_type size() const
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
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
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 SuperFloat
Definition Floats.h:29