Logo ROOT  
Reference Guide
RootFinder.cxx
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Andreas Hoecker, Joerg Stelzer, Helge Voss
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : RootFinder *
8 * Web : http://tmva.sourceforge.net *
9 * *
10 * Description: *
11 * Implementation (see header file for description) *
12 * *
13 * Authors (alphabetical): *
14 * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15 * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
16 * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
17 * *
18 * Copyright (c) 2005: *
19 * CERN, Switzerland *
20 * U. of Victoria, Canada *
21 * MPI-K Heidelberg, Germany *
22 * *
23 * Redistribution and use in source and binary forms, with or without *
24 * modification, are permitted according to the terms listed in LICENSE *
25 * (http://tmva.sourceforge.net/LICENSE) *
26 **********************************************************************************/
27
28/*! \class TMVA::RootFinder
29\ingroup TMVA
30Root finding using Brents algorithm (translated from CERNLIB function RZERO)
31*/
32
33#include "TMVA/RootFinder.h"
34
35#include "TMVA/MethodBase.h"
36#include "TMVA/MsgLogger.h"
37#include "TMVA/Types.h"
38
39#include "TMath.h"
40#include "TObject.h"
41
43
44////////////////////////////////////////////////////////////////////////////////
45/// constructor
46
48 Double_t rootMin,
49 Double_t rootMax,
50 Int_t maxIterations,
51 Double_t absTolerance )
52: fRootMin( rootMin ),
53 fRootMax( rootMax ),
54 fMaxIter( maxIterations ),
55 fAbsTol ( absTolerance ),
56 fLogger ( new MsgLogger("RootFinder") )
57{
58 fMethod=method;
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// destructor
63
65{
66 delete fLogger;
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Root finding using Brents algorithm; taken from CERNLIB function RZERO
71
73{
74 Double_t a = fRootMin, b = fRootMax;
75 Double_t fa = fMethod->GetValueForRoot( a ) - refValue;
76 Double_t fb = fMethod->GetValueForRoot( b ) - refValue;
77 if (fb*fa > 0) {
78 Log() << kWARNING << "<Root> initial interval w/o root: "
79 << "(a=" << a << ", b=" << b << "),"
80 << " (Eff_a=" << fMethod->GetValueForRoot( a )
81 << ", Eff_b=" << fMethod->GetValueForRoot( b ) << "), "
82 << "(fa=" << fa << ", fb=" << fb << "), "
83 << "refValue = " << refValue << Endl;
84 return 1;
85 }
86
87 Bool_t ac_equal(kFALSE);
88 Double_t fc = fb;
89 Double_t c = 0, d = 0, e = 0;
90 for (Int_t iter= 0; iter <= fMaxIter; iter++) {
91 if ((fb < 0 && fc < 0) || (fb > 0 && fc > 0)) {
92
93 // Rename a,b,c and adjust bounding interval d
94 ac_equal = kTRUE;
95 c = a; fc = fa;
96 d = b - a; e = b - a;
97 }
98
99 if (TMath::Abs(fc) < TMath::Abs(fb)) {
100 ac_equal = kTRUE;
101 a = b; b = c; c = a;
102 fa = fb; fb = fc; fc = fa;
103 }
104
105 Double_t tol = 0.5 * 2.2204460492503131e-16 * TMath::Abs(b);
106 Double_t m = 0.5 * (c - b);
107 if (fb == 0 || TMath::Abs(m) <= tol || TMath::Abs(fb) < fAbsTol) return b;
108
109 // Bounds decreasing too slowly: use bisection
110 if (TMath::Abs (e) < tol || TMath::Abs (fa) <= TMath::Abs (fb)) { d = m; e = m; }
111 else {
112 // Attempt inverse cubic interpolation
113 Double_t p, q, r;
114 Double_t s = fb / fa;
115
116 if (ac_equal) { p = 2 * m * s; q = 1 - s; }
117 else {
118 q = fa / fc; r = fb / fc;
119 p = s * (2 * m * q * (q - r) - (b - a) * (r - 1));
120 q = (q - 1) * (r - 1) * (s - 1);
121 }
122 // Check whether we are in bounds
123 if (p > 0) q = -q;
124 else p = -p;
125
126 Double_t min1 = 3 * m * q - TMath::Abs (tol * q);
127 Double_t min2 = TMath::Abs (e * q);
128 if (2 * p < (min1 < min2 ? min1 : min2)) {
129 // Accept the interpolation
130 e = d; d = p / q;
131 }
132 else { d = m; e = m; } // Interpolation failed: use bisection.
133 }
134 // Move last best guess to a
135 a = b; fa = fb;
136 // Evaluate new trial root
137 if (TMath::Abs(d) > tol) b += d;
138 else b += (m > 0 ? +tol : -tol);
139
140 fb = fMethod->GetValueForRoot( b ) - refValue;
141
142 }
143
144 // Return our best guess if we run out of iterations
145 Log() << kWARNING << "<Root> maximum iterations (" << fMaxIter
146 << ") reached before convergence" << Endl;
147
148 return b;
149}
ROOT::R::TRInterface & r
Definition: Object.C:4
#define d(i)
Definition: RSha256.hxx:102
#define b(i)
Definition: RSha256.hxx:100
#define c(i)
Definition: RSha256.hxx:101
#define e(i)
Definition: RSha256.hxx:103
const Bool_t kFALSE
Definition: RtypesCore.h:90
double Double_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
float * q
Definition: THbookFile.cxx:87
static struct mg_connection * fc(struct mg_context *ctx)
Definition: civetweb.c:3728
Virtual base Class for all MVA method.
Definition: MethodBase.h:111
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
Root finding using Brents algorithm (translated from CERNLIB function RZERO)
Definition: RootFinder.h:48
virtual ~RootFinder(void)
destructor
Definition: RootFinder.cxx:64
Double_t Root(Double_t refValue)
Root finding using Brents algorithm; taken from CERNLIB function RZERO.
Definition: RootFinder.cxx:72
MethodBase * fMethod
Definition: RootFinder.h:69
RootFinder(MethodBase *method, Double_t rootMin, Double_t rootMax, Int_t maxIterations=100, Double_t absTolerance=0.0)
constructor
Definition: RootFinder.cxx:47
static constexpr double s
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
Double_t Log(Double_t x)
Definition: TMath.h:750
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
auto * m
Definition: textangle.C:8
auto * a
Definition: textangle.C:12