Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 * *
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 * (see tmva/doc/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
41
42////////////////////////////////////////////////////////////////////////////////
43/// constructor
44
50: fRootMin( rootMin ),
51 fRootMax( rootMax ),
52 fMaxIter( maxIterations ),
53 fAbsTol ( absTolerance ),
54 fLogger ( new MsgLogger("RootFinder") )
55{
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// destructor
61
63{
64 delete fLogger;
65}
66
67////////////////////////////////////////////////////////////////////////////////
68/// Root finding using Brents algorithm; taken from CERNLIB function RZERO
69
71{
72 Double_t a = fRootMin, b = fRootMax;
73 Double_t fa = fMethod->GetValueForRoot( a ) - refValue;
74 Double_t fb = fMethod->GetValueForRoot( b ) - refValue;
75 if (fb*fa > 0) {
76 Log() << kWARNING << "<Root> initial interval w/o root: "
77 << "(a=" << a << ", b=" << b << "),"
78 << " (Eff_a=" << fMethod->GetValueForRoot( a )
79 << ", Eff_b=" << fMethod->GetValueForRoot( b ) << "), "
80 << "(fa=" << fa << ", fb=" << fb << "), "
81 << "refValue = " << refValue << Endl;
82 return 1;
83 }
84
86 Double_t fc = fb;
87 Double_t c = 0, d = 0, e = 0;
88 for (Int_t iter= 0; iter <= fMaxIter; iter++) {
89 if ((fb < 0 && fc < 0) || (fb > 0 && fc > 0)) {
90
91 // Rename a,b,c and adjust bounding interval d
93 c = a; fc = fa;
94 d = b - a; e = b - a;
95 }
96
97 if (TMath::Abs(fc) < TMath::Abs(fb)) {
99 a = b; b = c; c = a;
100 fa = fb; fb = fc; fc = fa;
101 }
102
103 Double_t tol = 0.5 * 2.2204460492503131e-16 * TMath::Abs(b);
104 Double_t m = 0.5 * (c - b);
105 if (fb == 0 || TMath::Abs(m) <= tol || TMath::Abs(fb) < fAbsTol) return b;
106
107 // Bounds decreasing too slowly: use bisection
108 if (TMath::Abs (e) < tol || TMath::Abs (fa) <= TMath::Abs (fb)) { d = m; e = m; }
109 else {
110 // Attempt inverse cubic interpolation
111 Double_t p, q, r;
112 Double_t s = fb / fa;
113
114 if (ac_equal) { p = 2 * m * s; q = 1 - s; }
115 else {
116 q = fa / fc; r = fb / fc;
117 p = s * (2 * m * q * (q - r) - (b - a) * (r - 1));
118 q = (q - 1) * (r - 1) * (s - 1);
119 }
120 // Check whether we are in bounds
121 if (p > 0) q = -q;
122 else p = -p;
123
124 Double_t min1 = 3 * m * q - TMath::Abs (tol * q);
125 Double_t min2 = TMath::Abs (e * q);
126 if (2 * p < (min1 < min2 ? min1 : min2)) {
127 // Accept the interpolation
128 e = d; d = p / q;
129 }
130 else { d = m; e = m; } // Interpolation failed: use bisection.
131 }
132 // Move last best guess to a
133 a = b; fa = fb;
134 // Evaluate new trial root
135 if (TMath::Abs(d) > tol) b += d;
136 else b += (m > 0 ? +tol : -tol);
137
138 fb = fMethod->GetValueForRoot( b ) - refValue;
139
140 }
141
142 // Return our best guess if we run out of iterations
143 Log() << kWARNING << "<Root> maximum iterations (" << fMaxIter
144 << ") reached before convergence" << Endl;
145
146 return b;
147}
#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
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
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
float * q
Virtual base Class for all MVA method.
Definition MethodBase.h:111
ostringstream derivative to redirect and format output
Definition MsgLogger.h:57
virtual ~RootFinder(void)
destructor
Double_t Root(Double_t refValue)
Root finding using Brents algorithm; taken from CERNLIB function RZERO.
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
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:124
TMarker m
Definition textangle.C:8