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 std::endl;
34
35
36
37////////////////////////////////////////////////////////////////////////////////
38/// Constructor taking function binding as input
39
41 _function(&function), _valid(function.isValid()),
42 _tol(2.2204460492503131e-16)
43{
44 if(_function->getDimension() != 1) {
45 oocoutE(nullptr,Eval) << "RooBrentRootFinder:: cannot find roots for function of dimension "
46 << _function->getDimension() << std::endl;
47 _valid= false;
48 }
49}
50
51
52
53////////////////////////////////////////////////////////////////////////////////
54/// Do the root finding using the Brent-Decker method. Returns a boolean status and
55/// loads 'result' with our best guess at the root if true.
56/// Prints a warning if the initial interval does not bracket a single
57/// root or if the root is not found after a fixed number of iterations.
58
59bool RooBrentRootFinder::findRoot(double &result, double xlo, double xhi, double value) const
60{
61 _function->saveXVec() ;
62
63 double a(xlo);
64 double 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 << std::endl;
70 return false;
71 }
72
73 bool ac_equal(false);
74 double fc= fb;
75 double c(0);
76 double d(0);
77 double e(0);
78 for(Int_t iter= 0; iter <= MaxIterations; iter++) {
79
80 if ((fb < 0 && fc < 0) || (fb > 0 && fc > 0)) {
81 // Rename a,b,c and adjust bounding interval d
82 ac_equal = true;
83 c = a;
84 fc = fa;
85 d = b - a;
86 e = b - a;
87 }
88
89 if (std::abs(fc) < std::abs(fb)) {
90 ac_equal = true;
91 a = b;
92 b = c;
93 c = a;
94 fa = fb;
95 fb = fc;
96 fc = fa;
97 }
98
99 double tol = 0.5 * _tol * std::abs(b);
100 double m = 0.5 * (c - b);
101
102
103 if (fb == 0 || std::abs(m) <= tol) {
104 //cout << "RooBrentRootFinder: iter = " << iter << " m = " << m << " tol = " << tol << std::endl ;
105 result= b;
106 _function->restoreXVec() ;
107 return true;
108 }
109
110 if (std::abs(e) < tol || std::abs(fa) <= std::abs(fb)) {
111 // Bounds decreasing too slowly: use bisection
112 d = m;
113 e = m;
114 }
115 else {
116 // Attempt inverse cubic interpolation
117 double p;
118 double q;
119 double r;
120 double s = fb / fa;
121
122 if (ac_equal) {
123 p = 2 * m * s;
124 q = 1 - s;
125 }
126 else {
127 q = fa / fc;
128 r = fb / fc;
129 p = s * (2 * m * q * (q - r) - (b - a) * (r - 1));
130 q = (q - 1) * (r - 1) * (s - 1);
131 }
132 // Check whether we are in bounds
133 if (p > 0) {
134 q = -q;
135 }
136 else {
137 p = -p;
138 }
139
140 double min1= 3 * m * q - std::abs(tol * q);
141 double min2= std::abs(e * q);
142 if (2 * p < (min1 < min2 ? min1 : min2)) {
143 // Accept the interpolation
144 e = d;
145 d = p / q;
146 }
147 else {
148 // Interpolation failed: use bisection.
149 d = m;
150 e = m;
151 }
152 }
153 // Move last best guess to a
154 a = b;
155 fa = fb;
156 // Evaluate new trial root
157 if (std::abs(d) > tol) {
158 b += d;
159 }
160 else {
161 b += (m > 0 ? +tol : -tol);
162 }
163 fb= (*_function)(&b) - value;
164
165 }
166 // Return our best guess if we run out of iterations
167 oocoutE(nullptr,Eval) << "RooBrentRootFinder::findRoot(" << _function->getName() << "): maximum iterations exceeded." << std::endl;
168 result= b;
169
170 _function->restoreXVec() ;
171
172 return false;
173}
#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)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
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