Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooMinimizerFcn.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * AL, Alfio Lazzaro, INFN Milan, alfio.lazzaro@mi.infn.it *
7 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl *
8 * *
9 * *
10 * Redistribution and use in source and binary forms, *
11 * with or without modification, are permitted according to the terms *
12 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
13 *****************************************************************************/
14
15//////////////////////////////////////////////////////////////////////////////
16/// \class RooMinimizerFcn
17/// RooMinimizerFcn is an interface to the ROOT::Math::IBaseFunctionMultiDim,
18/// a function that ROOT's minimisers use to carry out minimisations.
19///
20
21#include "RooMinimizerFcn.h"
22
23#include "RooAbsArg.h"
24#include "RooAbsPdf.h"
25#include "RooArgSet.h"
26#include "RooRealVar.h"
27#include "RooMsgService.h"
28#include "RooMinimizer.h"
29#include "RooNaNPacker.h"
30
31#include "Math/Functor.h"
32#include "TMatrixDSym.h"
33
34#include <fstream>
35#include <iomanip>
36
37using std::cout, std::endl, std::setprecision;
38
39namespace {
40
41// Helper function that wraps RooAbsArg::getParameters and directly returns the
42// output RooArgSet. To be used in the initializer list of the RooMinimizerFcn
43// constructor.
44RooArgSet getParameters(RooAbsReal const &funct)
45{
47 funct.getParameters(nullptr, out);
48 return out;
49}
50
51} // namespace
52
53// use reference wrapper for the Functor, such that the functor points to this RooMinimizerFcn by reference.
55 : RooAbsMinimizerFcn(getParameters(*funct), context), _funct(funct)
56{
57 if (context->_cfg.useGradient && funct->hasGradient()) {
58 _multiGenFcn = std::make_unique<ROOT::Math::GradFunctor>(this, &RooMinimizerFcn::operator(),
60 } else {
61 _multiGenFcn = std::make_unique<ROOT::Math::Functor>(std::cref(*this), getNDim());
62 }
63}
64
66{
67 _funct->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
68}
69
70/// Evaluate function given the parameters in `x`.
71double RooMinimizerFcn::operator()(const double *x) const
72{
73 // Set the parameter values for this iteration
74 for (unsigned index = 0; index < _nDim; index++) {
75 if (_logfile)
76 (*_logfile) << x[index] << " ";
78 }
79
80 // Calculate the function for these parameters
82 double fvalue = _funct->getVal();
84
85 fvalue = applyEvalErrorHandling(fvalue);
86
87 // Optional logging
88 if (_logfile)
89 (*_logfile) << setprecision(15) << fvalue << setprecision(4) << endl;
90 if (cfg().verbose) {
91 cout << "\nprevFCN" << (_funct->isOffsetting() ? "-offset" : "") << " = " << setprecision(10) << fvalue
92 << setprecision(4) << " ";
93 cout.flush();
94 }
95
97
98 return fvalue;
99}
100
101void RooMinimizerFcn::evaluateGradient(const double *x, double *out) const
102{
103 // Set the parameter values for this iteration
104 for (unsigned index = 0; index < _nDim; index++) {
105 if (_logfile)
106 (*_logfile) << x[index] << " ";
108 }
109
110 _funct->gradient(out);
111
112 // Optional logging
113 if (cfg().verbose) {
114 std::cout << "\n gradient = ";
115 for (std::size_t i = 0; i < getNDim(); ++i) {
116 std::cout << out[i] << ", ";
117 }
118 }
119}
120
122{
123 return _funct->GetName();
124}
125
127{
128 return _funct->GetTitle();
129}
130
132{
134}
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
RooFit::OwningPtr< RooArgSet > getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
virtual void constOptimizeTestStatistic(ConstOpCode opcode, bool doAlsoTrackingOpt=true)
Interface function signaling a request to perform constant term optimization.
double applyEvalErrorHandling(double fvalue) const
Apply corrections on the fvalue if errors were signaled.
RooMinimizer::Config const & cfg() const
std::ofstream * _logfile
bool SetPdfParamVal(int index, double value) const
Set value of parameter i.
unsigned int getNDim() const
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
virtual bool isOffsetting() const
Definition RooAbsReal.h:370
virtual bool hasGradient() const
Definition RooAbsReal.h:387
virtual void gradient(double *) const
Definition RooAbsReal.h:388
static void setHideOffset(bool flag)
virtual void enableOffsetting(bool)
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
void evaluateGradient(const double *x, double *out) const
std::string getFunctionName() const override
RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
void setOffsetting(bool flag) override
Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) override
This function must be overridden in the derived class to pass on constant term optimization configura...
std::unique_ptr< ROOT::Math::IBaseFunctionMultiDim > _multiGenFcn
RooAbsReal * _funct
double operator()(const double *x) const
Evaluate function given the parameters in x.
RooMinimizerFcn(RooAbsReal *funct, RooMinimizer *context)
std::string getFunctionTitle() const override
RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class...
Wrapper class around ROOT::Math::Minimizer that provides a seamless interface between the minimizer f...
RooMinimizer::Config _cfg
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
Double_t x[n]
Definition legend1.C:17