Logo 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 *
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 
22 #include "RooLinearCombination.h"
23 #include "Floats.h"
24 
25 #include "Math/Util.h"
26 
28 
29 namespace {
30  template <class T> inline void assign(SuperFloat &var, const T &val) {
31  #ifdef USE_UBLAS
32  var.assign(val);
33  #else
34  var = val;
35  #endif
36  }
37 } // namespace
38 
40  : _actualVars("actualVars", "Variables used by formula expression", this),
41  _nset(0) {
42  // constructor
43 }
44 
46  : RooAbsReal(name, name),
47  _actualVars("actualVars", "Variables used by formula expression", this),
48  _nset(0) {
49  // constructor
50 }
51 
53  const char *name)
54  : RooAbsReal(other, name),
55  _actualVars("actualVars", this, other._actualVars),
56  _coefficients(other._coefficients), _nset(0) {
57  // copy constructor
58 }
59 
60 void RooLinearCombination::printArgs(std::ostream &os) const {
61  // detailed printing method
62  os << "[";
63  const std::size_t n(this->_actualVars.getSize());
64  for (std::size_t i = 0; i < n; ++i) {
65  const RooAbsReal *r =
66  static_cast<const RooAbsReal *>(this->_actualVars.at(i));
67  double c(_coefficients[i]);
68  if (c > 0 && i > 0)
69  os << "+";
70  os << c << "*" << r->GetTitle();
71  }
72  os << "]";
73 }
74 
76  // destructor
77 }
78 
79 TObject *RooLinearCombination::clone(const char *newname) const {
80  // create a clone (deep copy) of this object
81  RooLinearCombination *retval = new RooLinearCombination(newname);
82  const std::size_t n(this->_actualVars.getSize());
83  for (std::size_t i = 0; i < n; ++i) {
84  const RooAbsReal *r =
85  static_cast<const RooAbsReal *>(this->_actualVars.at(i));
86  retval->add(this->_coefficients[i], static_cast<RooAbsReal *>(r->clone()));
87  }
88  return retval;
89 }
90 
92  // add a new term
93  _actualVars.add(*t);
94  _coefficients.push_back(c);
95 }
96 
98  // set the coefficient with the given index
99  this->_coefficients[idx] = c;
100 }
101 
103  // get the coefficient with the given index
104  return this->_coefficients[idx];
105 }
106 
108  // call the evaluation
109 #ifdef USE_UBLAS
110  SuperFloat result;
111  result.assign(0.0);
112  const std::size_t n(this->_actualVars.getSize());
113  for (std::size_t i = 0; i < n; ++i) {
114  SuperFloat tmp;
115  tmp.assign(static_cast<const RooAbsReal *>(this->_actualVars.at(i))->getVal());
116  result += this->_coefficients[i] * tmp;
117  }
118  return result.convert_to<double>();
119 #else
120  const std::size_t n(this->_actualVars.getSize());
121  std::vector<double> values(n);
122  for (std::size_t i = 0; i < n; ++i) {
123  values[i] = _coefficients[i] * static_cast<const RooAbsReal *>(this->_actualVars.at(i))->getVal();
124  }
125  // the values might span multiple orders of magnitudes, and to minimize
126  // precision loss, we sum up the values from the smallest to the largest
127  // absolute value.
128  std::sort(values.begin(), values.end(), [](double const& x, double const& y){ return std::abs(x) < std::abs(y); });
129  return ROOT::Math::KahanSum<double>::Accumulate(values.begin(), values.end()).Sum();
130 #endif
131 }
132 
134  Double_t xlo,
135  Double_t xhi) const {
136  // Forward the plot sampling hint from the p.d.f. that defines the observable
137  // obs
138  for(auto const& func : _actualVars) {
139  auto binb = static_cast<RooAbsReal*>(func)->binBoundaries(obs, xlo, xhi);
140  if (binb) {
141  return binb;
142  }
143  }
144  return 0;
145 }
146 
148  Double_t xlo,
149  Double_t xhi) const {
150  // Forward the plot sampling hint from the p.d.f. that defines the observable
151  // obs
152  for(auto const& func : _actualVars) {
153  auto hint = static_cast<RooAbsReal*>(func)->plotSamplingHint(obs, xlo, xhi);
154  if (hint) {
155  return hint;
156  }
157  }
158  return 0;
159 }
c
#define c(i)
Definition: RSha256.hxx:101
Util.h
n
const Int_t n
Definition: legend1.C:16
RooLinearCombination::printArgs
virtual void printArgs(std::ostream &os) const override
Print object arguments, ie its proxies.
Definition: RooLinearCombination.cxx:60
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
r
ROOT::R::TRInterface & r
Definition: Object.C:4
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
x
Double_t x[n]
Definition: legend1.C:17
RooAbsReal
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:61
Floats.h
RooArgList::at
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition: RooArgList.h:72
RooLinearCombination::RooLinearCombination
RooLinearCombination()
Definition: RooLinearCombination.cxx:39
TObject::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:403
ROOT::Math::KahanSum::Accumulate
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:188
RooLinearCombination::binBoundaries
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.
Definition: RooLinearCombination.cxx:133
RooLinearCombination::add
void add(SuperFloat c, RooAbsReal *t)
Definition: RooLinearCombination.cxx:91
RooLinearCombination::plotSamplingHint
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...
Definition: RooLinearCombination.cxx:147
RooLinearCombination::clone
virtual TObject * clone(const char *newname) const override
Definition: RooLinearCombination.cxx:79
y
Double_t y[n]
Definition: legend1.C:17
RooLinearCombination::_coefficients
std::vector< SuperFloat > _coefficients
Definition: RooLinearCombination.h:32
RooLinearCombination.h
RooLinearCombination::evaluate
virtual Double_t evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
Definition: RooLinearCombination.cxx:107
Double_t
double Double_t
Definition: RtypesCore.h:59
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
RooLinearCombination::setCoefficient
void setCoefficient(size_t idx, SuperFloat c)
Definition: RooLinearCombination.cxx:97
name
char name[80]
Definition: TGX11.cxx:110
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
RooLinearCombination::~RooLinearCombination
~RooLinearCombination()
Definition: RooLinearCombination.cxx:75
RooListProxy::add
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Reimplementation of standard RooArgList::add()
Definition: RooListProxy.cxx:104
SuperFloat
double SuperFloat
Definition: Floats.h:22
RooLinearCombination
RooLinearCombination is a class that helps perform linear combination of floating point numbers and p...
Definition: RooLinearCombination.h:30
RooAbsRealLValue
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
Definition: RooAbsRealLValue.h:31
RooLinearCombination::_actualVars
RooListProxy _actualVars
Definition: RooLinearCombination.h:31
RooLinearCombination::getCoefficient
SuperFloat getCoefficient(size_t idx)
Definition: RooLinearCombination.cxx:102
RooAbsCollection::getSize
Int_t getSize() const
Definition: RooAbsCollection.h:231