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