Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Numerical2PGradientCalculator.cxx
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7 * *
8 **********************************************************************/
9
12#include "Minuit2/MnFcn.h"
17#include "Minuit2/MnStrategy.h"
18#include "Minuit2/MnPrint.h"
19
20#include "./MPIProcess.h"
21
22#ifdef _OPENMP
23#include <omp.h>
24#endif
25
26#include <cmath>
27#include <cassert>
28#include <iomanip>
29
30namespace ROOT {
31
32namespace Minuit2 {
33
35{
36 // calculate gradient using Initial gradient calculator and from MinimumParameters object
37
39 FunctionGradient gra = gc(par);
40
41 return (*this)(par, gra);
42}
43
45operator()(const MinimumParameters &par, const FunctionGradient &Gradient) const
46{
47 // calculate numerical gradient from MinimumParameters object
48 // the algorithm takes correctly care when the gradient is approximately zero
49
51
52 // std::cout<<"########### Numerical2PDerivative"<<std::endl;
53 // std::cout<<"initial grd: "<<Gradient.Grad()<<std::endl;
54 // std::cout<<"position: "<<par.Vec()<<std::endl;
55 MnPrint print("Numerical2PGradientCalculator");
56
57 assert(par.IsValid());
58
59 double fcnmin = par.Fval();
60 // std::cout<<"fval: "<<fcnmin<<std::endl;
61
62 double eps2 = trafo.Precision().Eps2();
63 double eps = trafo.Precision().Eps();
64
65 //print.Trace("Assumed precision eps", eps, "eps2", eps2);
66
67 double dfmin = 8. * eps2 * (std::fabs(fcnmin) + fFcn.Up());
68 double vrysml = 8. * eps * eps;
69 // double vrysml = std::max(1.e-4, eps2);
70 // std::cout<<"dfmin= "<<dfmin<<std::endl;
71 // std::cout<<"vrysml= "<<vrysml<<std::endl;
72 // std::cout << " ncycle " << Ncycle() << std::endl;
73
74 unsigned int n = (par.Vec()).size();
75 unsigned int ncycle = fStrategy.GradientNCycles();
76 // MnAlgebraicVector vgrd(n), vgrd2(n), vgstp(n);
77 MnAlgebraicVector grd = Gradient.Grad();
78 MnAlgebraicVector g2 = Gradient.G2();
79 MnAlgebraicVector gstep = Gradient.Gstep();
80
81 print.Debug("Calculating gradient around function value", fcnmin, "\n\t at point", par.Vec());
82
83#ifndef _OPENMP
84
86
87 // for serial execution this can be outside the loop
88 MnAlgebraicVector x = par.Vec();
90
91 unsigned int startElementIndex = mpiproc.StartElementIndex();
92 unsigned int endElementIndex = mpiproc.EndElementIndex();
93
94 for (unsigned int i = startElementIndex; i < endElementIndex; i++) {
95
96#else
97
98 // parallelize this loop using OpenMP
99//#define N_PARALLEL_PAR 5
100#pragma omp parallel
101#pragma omp for
102 //#pragma omp for schedule (static, N_PARALLEL_PAR)
103
104 for (int i = 0; i < int(n); i++) {
105
106#endif
107
108#ifdef _OPENMP
109 // create in loop since each thread will use its own copy
110 MnAlgebraicVector x = par.Vec();
112#endif
113
114 double xtf = x(i);
115 double epspri = eps2 + std::fabs(grd(i) * eps2);
116 double stepb4 = 0.;
117 for (unsigned int j = 0; j < ncycle; j++) {
118 double optstp = std::sqrt(dfmin / (std::fabs(g2(i)) + epspri));
119 double step = std::max(optstp, std::fabs(0.1 * gstep(i)));
120 // std::cout<<"step: "<<step;
121 if (trafo.Parameter(trafo.ExtOfInt(i)).HasLimits()) {
122 if (step > 0.5)
123 step = 0.5;
124 }
125 double stpmax = 10. * std::fabs(gstep(i));
126 if (step > stpmax)
127 step = stpmax;
128 // std::cout<<" "<<step;
129 double stpmin = std::max(vrysml, 8. * std::fabs(eps2 * x(i)));
130 if (step < stpmin)
131 step = stpmin;
132 // std::cout<<" "<<step<<std::endl;
133 // std::cout<<"step: "<<step<<std::endl;
134 if (std::fabs((step - stepb4) / step) < fStrategy.GradientStepTolerance()) {
135 // std::cout<<"(step-stepb4)/step"<<std::endl;
136 // std::cout<<"j= "<<j<<std::endl;
137 // std::cout<<"step= "<<step<<std::endl;
138 break;
139 }
140 gstep(i) = step;
141 stepb4 = step;
142
143 x(i) = xtf + step;
144 double fs1 = mfcnCaller(x);
145 x(i) = xtf - step;
146 double fs2 = mfcnCaller(x);
147 x(i) = xtf;
148
149 double grdb4 = grd(i);
150 grd(i) = 0.5 * (fs1 - fs2) / step;
151 g2(i) = (fs1 + fs2 - 2. * fcnmin) / step / step;
152
153#ifdef _OPENMP
154#pragma omp critical
155#endif
156 {
157#ifdef _OPENMP
158 // must create thread-local MnPrint instances when printing inside threads
159 MnPrint printtl("Numerical2PGradientCalculator[OpenMP]");
160#endif
161 if (i == 0 && j == 0) {
162#ifdef _OPENMP
163 printtl.Trace([&](std::ostream &os) {
164#else
165 print.Trace([&](std::ostream &os) {
166#endif
167 os << std::setw(10) << "parameter" << std::setw(6) << "cycle" << std::setw(15) << "x" << std::setw(15)
168 << "step" << std::setw(15) << "f1" << std::setw(15) << "f2" << std::setw(15) << "grd"
169 << std::setw(15) << "g2" << std::endl;
170 });
171 }
172#ifdef _OPENMP
173 printtl.Trace([&](std::ostream &os) {
174#else
175 print.Trace([&](std::ostream &os) {
176#endif
177 const int pr = os.precision(13);
178 const int iext = trafo.ExtOfInt(i);
179 os << std::setw(10) << trafo.Name(iext) << std::setw(5) << j << " " << x(i) << " " << step << " "
180 << fs1 << " " << fs2 << " " << grd(i) << " " << g2(i) << std::endl;
181 os.precision(pr);
182 });
183 }
184
185 if (std::fabs(grdb4 - grd(i)) / (std::fabs(grd(i)) + dfmin / step) < fStrategy.GradientTolerance()) {
186 // std::cout<<"j= "<<j<<std::endl;
187 // std::cout<<"step= "<<step<<std::endl;
188 // std::cout<<"fs1, fs2: "<<fs1<<" "<<fs2<<std::endl;
189 // std::cout<<"fs1-fs2: "<<fs1-fs2<<std::endl;
190 break;
191 }
192 }
193
194 // vgrd(i) = grd;
195 // vgrd2(i) = g2;
196 // vgstp(i) = gstep;
197 }
198
199#ifndef _OPENMP
200 mpiproc.SyncVector(grd);
201 mpiproc.SyncVector(g2);
202 mpiproc.SyncVector(gstep);
203#endif
204
205 // print after parallel processing to avoid synchronization issues
206 print.Debug([&](std::ostream &os) {
207 const int pr = os.precision(13);
208 os << std::endl;
209 os << std::setw(14) << "Parameter" << std::setw(14) << "Gradient" << std::setw(14) << "g2 " << std::setw(14)
210 << "step" << std::endl;
211 for (int i = 0; i < int(n); i++) {
212 const int iext = trafo.ExtOfInt(i);
213 os << std::setw(14) << trafo.Name(iext) << " " << grd(i) << " " << g2(i) << " " << gstep(i) << std::endl;
214 }
215 os.precision(pr);
216 });
217
218 return FunctionGradient(grd, g2, gstep);
219}
220
221} // namespace Minuit2
222
223} // namespace ROOT
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
Class to calculate an initial estimate of the gradient.
const MnAlgebraicVector & Vec() const
void Debug(const Ts &... args)
Definition MnPrint.h:135
void Trace(const Ts &... args)
Definition MnPrint.h:141
class dealing with the transformation between user specified parameters (external) and internal param...
FunctionGradient operator()(const MinimumParameters &) const override
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...