Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooBrentRootFinder.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 RooBrentRootFinder.cxx
19\class RooBrentRootFinder
20\ingroup Roofitcore
21
22Implement the abstract 1-dimensional root finding interface using
23the Brent-Decker method. This implementation is based on the one
24in the GNU scientific library (v0.99).
25**/
26
27#include "RooBrentRootFinder.h"
28#include "RooAbsFunc.h"
29#include <cmath>
30#include "Riostream.h"
31#include "RooMsgService.h"
32
33using namespace std;
34
36
37
38////////////////////////////////////////////////////////////////////////////////
39/// Constructor taking function binding as input
40
42 _function(&function), _valid(function.isValid()),
43 _tol(2.2204460492503131e-16)
44{
45 if(_function->getDimension() != 1) {
46 oocoutE(nullptr,Eval) << "RooBrentRootFinder:: cannot find roots for function of dimension "
47 << _function->getDimension() << endl;
48 _valid= false;
49 }
50}
51
52
53
54////////////////////////////////////////////////////////////////////////////////
55/// Do the root finding using the Brent-Decker method. Returns a boolean status and
56/// loads 'result' with our best guess at the root if true.
57/// Prints a warning if the initial interval does not bracket a single
58/// root or if the root is not found after a fixed number of iterations.
59
60bool RooBrentRootFinder::findRoot(double &result, double xlo, double xhi, double value) const
61{
63
64 double a(xlo),b(xhi);
65 double fa= (*_function)(&a) - value;
66 double fb= (*_function)(&b) - value;
67 if(fb*fa > 0) {
68 oocxcoutD((TObject*)nullptr,Eval) << "RooBrentRootFinder::findRoot(" << _function->getName() << "): initial interval does not bracket a root: ("
69 << a << "," << b << "), value = " << value << " f[xlo] = " << fa << " f[xhi] = " << fb << endl;
70 return false;
71 }
72
73 bool ac_equal(false);
74 double fc= fb;
75 double c(0),d(0),e(0);
76 for(Int_t iter= 0; iter <= MaxIterations; iter++) {
77
78 if ((fb < 0 && fc < 0) || (fb > 0 && fc > 0)) {
79 // Rename a,b,c and adjust bounding interval d
80 ac_equal = true;
81 c = a;
82 fc = fa;
83 d = b - a;
84 e = b - a;
85 }
86
87 if (std::abs(fc) < std::abs(fb)) {
88 ac_equal = true;
89 a = b;
90 b = c;
91 c = a;
92 fa = fb;
93 fb = fc;
94 fc = fa;
95 }
96
97 double tol = 0.5 * _tol * std::abs(b);
98 double m = 0.5 * (c - b);
99
100
101 if (fb == 0 || std::abs(m) <= tol) {
102 //cout << "RooBrentRootFinder: iter = " << iter << " m = " << m << " tol = " << tol << endl ;
103 result= b;
105 return true;
106 }
107
108 if (std::abs(e) < tol || std::abs(fa) <= std::abs(fb)) {
109 // Bounds decreasing too slowly: use bisection
110 d = m;
111 e = m;
112 }
113 else {
114 // Attempt inverse cubic interpolation
115 double p, q, r;
116 double s = fb / fa;
117
118 if (ac_equal) {
119 p = 2 * m * s;
120 q = 1 - s;
121 }
122 else {
123 q = fa / fc;
124 r = fb / fc;
125 p = s * (2 * m * q * (q - r) - (b - a) * (r - 1));
126 q = (q - 1) * (r - 1) * (s - 1);
127 }
128 // Check whether we are in bounds
129 if (p > 0) {
130 q = -q;
131 }
132 else {
133 p = -p;
134 }
135
136 double min1= 3 * m * q - std::abs(tol * q);
137 double min2= std::abs(e * q);
138 if (2 * p < (min1 < min2 ? min1 : min2)) {
139 // Accept the interpolation
140 e = d;
141 d = p / q;
142 }
143 else {
144 // Interpolation failed: use bisection.
145 d = m;
146 e = m;
147 }
148 }
149 // Move last best guess to a
150 a = b;
151 fa = fb;
152 // Evaluate new trial root
153 if (std::abs(d) > tol) {
154 b += d;
155 }
156 else {
157 b += (m > 0 ? +tol : -tol);
158 }
159 fb= (*_function)(&b) - value;
160
161 }
162 // Return our best guess if we run out of iterations
163 oocoutE(nullptr,Eval) << "RooBrentRootFinder::findRoot(" << _function->getName() << "): maximum iterations exceeded." << endl;
164 result= b;
165
167
168 return false;
169}
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
#define oocxcoutD(o, a)
#define oocoutE(o, a)
#define ClassImp(name)
Definition Rtypes.h:377
winID h TVirtualViewer3D TVirtualGLPainter p
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
float * q
Abstract interface for evaluating a real-valued function of one real variable and performing numerica...
Definition RooAbsFunc.h:27
virtual void saveXVec() const
Definition RooAbsFunc.h:56
virtual void restoreXVec() const
Definition RooAbsFunc.h:59
UInt_t getDimension() const
Definition RooAbsFunc.h:33
virtual const char * getName() const
Name of function binding.
Definition RooAbsFunc.h:65
Implement the abstract 1-dimensional root finding interface using the Brent-Decker method.
const RooAbsFunc * _function
Pointer to input function.
bool findRoot(double &result, double xlo, double xhi, double value=0) const
Do the root finding using the Brent-Decker method.
static constexpr int MaxIterations
RooBrentRootFinder(const RooAbsFunc &function)
Constructor taking function binding as input.
bool _valid
True if current state is valid.
Mother of all ROOT objects.
Definition TObject.h:41
TMarker m
Definition textangle.C:8