ROOT  6.06/09
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 #include "TMath.h"
29 
30 #include "TMVA/RootFinder.h"
31 #include "TMVA/MsgLogger.h"
32 
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// constructor
37 
38 TMVA::RootFinder::RootFinder( Double_t (*rootVal)( Double_t ),
39  Double_t rootMin,
40  Double_t rootMax,
41  Int_t maxIterations,
42  Double_t absTolerance )
43  : fRootMin( rootMin ),
44  fRootMax( rootMax ),
45  fMaxIter( maxIterations ),
46  fAbsTol ( absTolerance ),
47  fLogger ( new MsgLogger("RootFinder") )
48 {
49  fGetRootVal = rootVal;
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// destructor
54 
56 {
57  delete fLogger;
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Root finding using Brents algorithm; taken from CERNLIB function RZERO
62 
64 {
65  Double_t a = fRootMin, b = fRootMax;
66  Double_t fa = (*fGetRootVal)( a ) - refValue;
67  Double_t fb = (*fGetRootVal)( b ) - refValue;
68  if (fb*fa > 0) {
69  Log() << kWARNING << "<Root> initial interval w/o root: "
70  << "(a=" << a << ", b=" << b << "),"
71  << " (Eff_a=" << (*fGetRootVal)( a )
72  << ", Eff_b=" << (*fGetRootVal)( b ) << "), "
73  << "(fa=" << fa << ", fb=" << fb << "), "
74  << "refValue = " << refValue << Endl;
75  return 1;
76  }
77 
78  Bool_t ac_equal(kFALSE);
79  Double_t fc = fb;
80  Double_t c = 0, d = 0, e = 0;
81  for (Int_t iter= 0; iter <= fMaxIter; iter++) {
82  if ((fb < 0 && fc < 0) || (fb > 0 && fc > 0)) {
83 
84  // Rename a,b,c and adjust bounding interval d
85  ac_equal = kTRUE;
86  c = a; fc = fa;
87  d = b - a; e = b - a;
88  }
89 
90  if (TMath::Abs(fc) < TMath::Abs(fb)) {
91  ac_equal = kTRUE;
92  a = b; b = c; c = a;
93  fa = fb; fb = fc; fc = fa;
94  }
95 
96  Double_t tol = 0.5 * 2.2204460492503131e-16 * TMath::Abs(b);
97  Double_t m = 0.5 * (c - b);
98  if (fb == 0 || TMath::Abs(m) <= tol || TMath::Abs(fb) < fAbsTol) return b;
99 
100  // Bounds decreasing too slowly: use bisection
101  if (TMath::Abs (e) < tol || TMath::Abs (fa) <= TMath::Abs (fb)) { d = m; e = m; }
102  else {
103  // Attempt inverse cubic interpolation
104  Double_t p, q, r;
105  Double_t s = fb / fa;
106 
107  if (ac_equal) { p = 2 * m * s; q = 1 - s; }
108  else {
109  q = fa / fc; r = fb / fc;
110  p = s * (2 * m * q * (q - r) - (b - a) * (r - 1));
111  q = (q - 1) * (r - 1) * (s - 1);
112  }
113  // Check whether we are in bounds
114  if (p > 0) q = -q;
115  else p = -p;
116 
117  Double_t min1 = 3 * m * q - TMath::Abs (tol * q);
118  Double_t min2 = TMath::Abs (e * q);
119  if (2 * p < (min1 < min2 ? min1 : min2)) {
120  // Accept the interpolation
121  e = d; d = p / q;
122  }
123  else { d = m; e = m; } // Interpolation failed: use bisection.
124  }
125  // Move last best guess to a
126  a = b; fa = fb;
127  // Evaluate new trial root
128  if (TMath::Abs(d) > tol) b += d;
129  else b += (m > 0 ? +tol : -tol);
130 
131  fb = (*fGetRootVal)( b ) - refValue;
132 
133  }
134 
135  // Return our best guess if we run out of iterations
136  Log() << kWARNING << "<Root> maximum iterations (" << fMaxIter
137  << ") reached before convergence" << Endl;
138 
139  return b;
140 }
141 
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
const Bool_t kFALSE
Definition: Rtypes.h:92
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
static struct mg_connection * fc(struct mg_context *ctx)
Definition: civetweb.c:839
MsgLogger * fLogger
Definition: RootFinder.h:72
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
Double_t Root(Double_t refValue)
Root finding using Brents algorithm; taken from CERNLIB function RZERO.
Definition: RootFinder.cxx:63
const double tol
ROOT::R::TRInterface & r
Definition: Object.C:4
TMarker * m
Definition: textangle.C:8
ClassImp(TMVA::RootFinder) TMVA
constructor
Definition: RootFinder.cxx:33
double Double_t
Definition: RtypesCore.h:55
virtual ~RootFinder(void)
destructor
Definition: RootFinder.cxx:55
Abstract ClassifierFactory template that handles arbitrary types.
const Bool_t kTRUE
Definition: Rtypes.h:91
float * q
Definition: THbookFile.cxx:87
Definition: math.cpp:60