Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooMinimizerFcn.cxx
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*****************************************************************************
4 * Project: RooFit *
5 * Package: RooFitCore *
6 * @(#)root/roofitcore:$Id$
7 * Authors: *
8 * AL, Alfio Lazzaro, INFN Milan, alfio.lazzaro@mi.infn.it *
9 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl *
10 * *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17//////////////////////////////////////////////////////////////////////////////
18/// \class RooMinimizerFcn
19/// RooMinimizerFcn is an interface to the ROOT::Math::IBaseFunctionMultiDim,
20/// a function that ROOT's minimisers use to carry out minimisations.
21///
22
23#include "RooMinimizerFcn.h"
24
25#include "RooAbsArg.h"
26#include "RooAbsPdf.h"
27#include "RooArgSet.h"
28#include "RooRealVar.h"
29#include "RooMsgService.h"
30#include "RooMinimizer.h"
31#include "RooNaNPacker.h"
32
33#include "Math/Functor.h"
34#include "TMatrixDSym.h"
35
36#include <fstream>
37#include <iomanip>
38
39using std::setprecision;
40
41namespace {
42
43// Helper function that wraps RooAbsArg::getParameters and directly returns the
44// output RooArgSet. To be used in the initializer list of the RooMinimizerFcn
45// constructor.
46RooArgSet getParameters(RooAbsReal const &funct)
47{
48 RooArgSet out;
49 funct.getParameters(nullptr, out);
50 return out;
51}
52
53} // namespace
54
55// use reference wrapper for the Functor, such that the functor points to this RooMinimizerFcn by reference.
56RooMinimizerFcn::RooMinimizerFcn(RooAbsReal *funct, RooMinimizer *context)
57 : RooAbsMinimizerFcn(getParameters(*funct), context), _funct(funct)
58{
59 unsigned int nDim = getNDim();
60
61 if (context->_cfg.useGradient && funct->hasGradient()) {
62 _gradientOutput.resize(_allParams.size());
63 _multiGenFcn = std::make_unique<ROOT::Math::GradFunctor>(this, &RooMinimizerFcn::operator(),
64 &RooMinimizerFcn::evaluateGradient, nDim);
65 } else {
66 _multiGenFcn = std::make_unique<ROOT::Math::Functor>(std::cref(*this), getNDim());
67 }
68}
69
70void RooMinimizerFcn::setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt)
71{
72 _funct->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
73}
74
75/// Evaluate function given the parameters in `x`.
76double RooMinimizerFcn::operator()(const double *x) const
77{
78 // Set the parameter values for this iteration
79 for (unsigned index = 0; index < getNDim(); index++) {
80 if (_logfile)
81 (*_logfile) << x[index] << " ";
83 }
84
85 // Calculate the function for these parameters
87 double fvalue = _funct->getVal();
89
91
92 // Optional logging
93 if (_logfile)
94 (*_logfile) << setprecision(15) << fvalue << setprecision(4) << std::endl;
95 if (cfg().verbose) {
96 std::cout << "\nprevFCN" << (_funct->isOffsetting() ? "-offset" : "") << " = " << setprecision(10) << fvalue
97 << setprecision(4) << " ";
98 std::cout.flush();
99 }
100
101 finishDoEval();
102
103 return fvalue;
104}
105
106void RooMinimizerFcn::evaluateGradient(const double *x, double *out) const
107{
108 // Set the parameter values for this iteration
109 for (unsigned index = 0; index < getNDim(); index++) {
110 if (_logfile)
111 (*_logfile) << x[index] << " ";
113 }
114
115 _funct->gradient(_gradientOutput.data());
116
117 std::size_t iAll = 0;
118 std::size_t iFloating = 0;
119 for (RooAbsArg *param : _allParamsInit) {
120 if (!treatAsConstant(*param)) {
122 ++iFloating;
123 }
124 ++iAll;
125 }
126
127 // Optional logging
128 if (cfg().verbose) {
129 std::cout << "\n gradient = ";
130 for (std::size_t i = 0; i < getNDim(); ++i) {
131 std::cout << out[i] << ", ";
132 }
133 }
134}
135
136std::string RooMinimizerFcn::getFunctionName() const
137{
138 return _funct->GetName();
139}
140
141std::string RooMinimizerFcn::getFunctionTitle() const
142{
143 return _funct->GetTitle();
144}
145
146void RooMinimizerFcn::setOffsetting(bool flag)
147{
148 _funct->enableOffsetting(flag);
149}
150
151/// \endcond
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 char Point_t Rectangle_t WindowAttributes_t index
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
static void setHideOffset(bool flag)
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Wrapper class around ROOT::Math::Minimizer that provides a seamless interface between the minimizer f...
RooMinimizer::Config _cfg
Double_t x[n]
Definition legend1.C:17