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