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
40 return (*this)(par, gra);
41}
42
44operator()(const MinimumParameters &par, const FunctionGradient &Gradient) const
45{
46 // calculate numerical gradient from MinimumParameters object
47 // the algorithm takes correctly care when the gradient is approximately zero
48
50
51 // std::cout<<"########### Numerical2PDerivative"<<std::endl;
52 // std::cout<<"initial grd: "<<Gradient.Grad()<<std::endl;
53 // std::cout<<"position: "<<par.Vec()<<std::endl;
54 MnPrint print("Numerical2PGradientCalculator");
55
56 assert(par.IsValid());
57
58 double fcnmin = par.Fval();
59 // std::cout<<"fval: "<<fcnmin<<std::endl;
60
61 double eps2 = trafo.Precision().Eps2();
62 double eps = trafo.Precision().Eps();
63
64 //print.Trace("Assumed precision eps", eps, "eps2", eps2);
65
66 double dfmin = 8. * eps2 * (std::fabs(fcnmin) + fFcn.Up());
67 double vrysml = 8. * eps * eps;
68 // double vrysml = std::max(1.e-4, eps2);
69 // std::cout<<"dfmin= "<<dfmin<<std::endl;
70 // std::cout<<"vrysml= "<<vrysml<<std::endl;
71 // std::cout << " ncycle " << Ncycle() << std::endl;
72
73 unsigned int n = (par.Vec()).size();
74 unsigned int ncycle = fStrategy.GradientNCycles();
75 // MnAlgebraicVector vgrd(n), vgrd2(n), vgstp(n);
76 MnAlgebraicVector grd = Gradient.Grad();
77 MnAlgebraicVector g2 = Gradient.G2();
78 MnAlgebraicVector gstep = Gradient.Gstep();
79
80 print.Debug("Calculating gradient around function value", fcnmin, "\n\t at point", par.Vec());
81
82#ifndef _OPENMP
83
85
86 // for serial execution this can be outside the loop
87 MnAlgebraicVector x = par.Vec();
89
90 unsigned int startElementIndex = mpiproc.StartElementIndex();
91 unsigned int endElementIndex = mpiproc.EndElementIndex();
92
93 for (unsigned int i = startElementIndex; i < endElementIndex; i++) {
94
95#else
96
97 // parallelize this loop using OpenMP
98//#define N_PARALLEL_PAR 5
99#pragma omp parallel for if (fDoParallelOMP)
100 //#pragma omp for schedule (static, N_PARALLEL_PAR)
101
102 // Note: the MSVC compiler enforces that the index variable in OpenMP 'for'
103 // statements must have signed int type!
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.
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
FunctionGradient calculateInitialGradient(const MinimumParameters &, const MnUserTransformation &, double errorDef)
Initial rough estimate of the gradient using the parameter step size.