Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooDerivative.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17/**
18\file RooDerivative.cxx
19\class RooDerivative
20\ingroup Roofitcore
21
22Represents the first, second, or third order derivative
23of any RooAbsReal as calculated (numerically) by the MathCore Richardson
24derivator class.
25**/
26
27#include <cmath>
28
29#include "RooDerivative.h"
30#include "RooAbsReal.h"
31#include "RooAbsPdf.h"
32#include "RooArgSet.h"
33#include "RooMsgService.h"
34#include "RooRealVar.h"
35#include "RooFunctor.h"
36
39
40#include <ostream>
41
42////////////////////////////////////////////////////////////////////////////////
43/// Default constructor
44
46
47////////////////////////////////////////////////////////////////////////////////
48
49RooDerivative::RooDerivative(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, Int_t orderIn, double epsIn) :
50 RooAbsReal(name, title),
51 _order(orderIn),
52 _eps(epsIn),
53 _nset("nset","nset",this,false,false),
54 _func("function","function",this,func),
55 _x("x","x",this,x)
56{
57 if (_order<0 || _order>3 ) {
58 throw std::runtime_error(Form("RooDerivative::ctor(%s) ERROR, derivation order must be 1,2 or 3",name)) ;
59 }
60}
61
62////////////////////////////////////////////////////////////////////////////////
63
64RooDerivative::RooDerivative(const char *name, const char *title, RooAbsReal &func, RooRealVar &x,
65 const RooArgSet &nset, Int_t orderIn, double epsIn)
66 : RooDerivative(name, title, func, x, orderIn, epsIn)
67{
68 _nset.add(nset);
69}
70
71////////////////////////////////////////////////////////////////////////////////
72
75 _order(other._order),
76 _eps(other._eps),
77 _nset("nset",this,other._nset),
78 _func("function",this,other._func),
79 _x("x",this,other._x)
80{
81}
82
84
85////////////////////////////////////////////////////////////////////////////////
86/// Calculate value
87
89{
90 if (!_ftor) {
91 _ftor = std::unique_ptr<RooFunctor>{_func.arg().functor(_x.arg(), RooArgSet(), _nset)};
93 _rd = std::make_unique<ROOT::Math::RichardsonDerivator>(wf, _eps, true);
94 }
95
96 // Figure out if we are close to the variable boundaries
97 double val = _x;
98 auto &xVar = static_cast<RooRealVar &>(*_x);
99 double valMin = xVar.getMin();
100 double valMax = xVar.getMax();
101 bool isCloseLo = val - valMin < _eps;
102 bool isCloseHi = valMax - val < _eps;
103
104 // If we hit the boundary left and right, there is obviously a mistake when setting epsilon
105 if (isCloseLo && isCloseHi) {
106 std::stringstream errMsg;
107 errMsg << "error in numerical derivator: 2 * epsilon is larger than the variable range!";
108 coutE(Eval) << errMsg.str() << std::endl;
109 throw std::runtime_error(errMsg.str());
110 }
111
112 // If we are close to the variable boundary on either side
113 if (isCloseLo || isCloseHi) {
114 // For first-order derivatives we are good: the RichardsonDerivator can
115 // also calculate the derivative using only forward or backward
116 // variations:
117 if (_order == 1) {
118 return isCloseLo ? _rd->DerivativeForward(val) : _rd->DerivativeBackward(val);
119 }
120 // If the function is constant within floating point precision anyway,
121 // we don't have a problem.
122 const double eps = std::numeric_limits<double>::epsilon();
123 const double yval1 = _ftor->eval(val);
124 const double yval2 = isCloseLo ? _ftor->eval(val + _eps) : _ftor->eval(val - _eps);
125 if (std::abs(yval2 - yval1) <= eps) {
126 return 0.0;
127 }
128
129 // Give up
130 std::stringstream errMsg;
131 errMsg << "error in numerical derivator: variable value is to close to limits to compute finite differences";
132 coutE(Eval) << errMsg.str() << std::endl;
133 throw std::runtime_error(errMsg.str());
134 }
135
136 switch (_order) {
137 case 1: return _rd->Derivative1(val);
138 case 2: return _rd->Derivative2(val);
139 case 3: return _rd->Derivative3(val);
140 }
141 return 0;
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Zap functor and derivator ;
146
#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:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2571
Template class to wrap any C++ callable object which takes one argument i.e.
Abstract container object that can hold multiple RooAbsArg objects.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
bool redirectServersHook(const RooAbsCollection &newServerList, bool mustReplaceAll, bool nameChange, bool isRecursiveStep) override
Function that is called at the end of redirectServers().
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
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...
Represents the first, second, or third order derivative of any RooAbsReal as calculated (numerically)...
double eps() const
std::unique_ptr< ROOT::Math::RichardsonDerivator > _rd
! Derivator
~RooDerivative() override
RooDerivative()
Default constructor.
double _eps
Precision.
std::unique_ptr< RooFunctor > _ftor
! Functor binding of RooAbsReal
RooSetProxy _nset
Normalization set (optional)
bool redirectServersHook(const RooAbsCollection &, bool, bool, bool) override
Zap functor and derivator ;.
Int_t _order
Derivation order.
double evaluate() const override
Calculate value.
RooRealProxy _func
Input function.
RooRealProxy _x
Observable.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
const T & arg() const
Return reference to object held in proxy.
Double_t x[n]
Definition legend1.C:17