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 unsigned int nDim = getNDim();
58
59 if (context->_cfg.useGradient && funct->hasGradient()) {
61 _multiGenFcn = std::make_unique<ROOT::Math::GradFunctor>(this, &RooMinimizerFcn::operator(),
63 } else {
64 _multiGenFcn = std::make_unique<ROOT::Math::Functor>(std::cref(*this), getNDim());
65 }
66}
67
69{
70 _funct->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
71}
72
73/// Evaluate function given the parameters in `x`.
74double RooMinimizerFcn::operator()(const double *x) const
75{
76 // Set the parameter values for this iteration
77 for (unsigned index = 0; index < getNDim(); index++) {
78 if (_logfile)
79 (*_logfile) << x[index] << " ";
81 }
82
83 // Calculate the function for these parameters
85 double fvalue = _funct->getVal();
87
88 fvalue = applyEvalErrorHandling(fvalue);
89
90 // Optional logging
91 if (_logfile)
92 (*_logfile) << setprecision(15) << fvalue << setprecision(4) << endl;
93 if (cfg().verbose) {
94 cout << "\nprevFCN" << (_funct->isOffsetting() ? "-offset" : "") << " = " << setprecision(10) << fvalue
95 << setprecision(4) << " ";
96 cout.flush();
97 }
98
100
101 return fvalue;
102}
103
104void RooMinimizerFcn::evaluateGradient(const double *x, double *out) const
105{
106 // Set the parameter values for this iteration
107 for (unsigned index = 0; index < getNDim(); index++) {
108 if (_logfile)
109 (*_logfile) << x[index] << " ";
111 }
112
114
115 std::size_t iAll = 0;
116 std::size_t iFloating = 0;
117 for (RooAbsArg *param : _allParamsInit) {
118 if (!treatAsConstant(*param)) {
119 out[iFloating] = _gradientOutput[iAll];
120 ++iFloating;
121 }
122 ++iAll;
123 }
124
125 // Optional logging
126 if (cfg().verbose) {
127 std::cout << "\n gradient = ";
128 for (std::size_t i = 0; i < getNDim(); ++i) {
129 std::cout << out[i] << ", ";
130 }
131 }
132}
133
135{
136 return _funct->GetName();
137}
138
140{
141 return _funct->GetTitle();
142}
143
145{
147}
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:79
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.
Storage_t::size_type size() const
static bool treatAsConstant(RooAbsArg const &arg)
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
std::vector< double > _gradientOutput
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