Logo ROOT  
Reference Guide
MinuitFcnGrad.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl
5 *
6 * Copyright (c) 2021, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef ROOT_ROOFIT_TESTSTATISTICS_MinuitFcnGrad
14#define ROOT_ROOFIT_TESTSTATISTICS_MinuitFcnGrad
15
16#include "RooArgList.h"
17#include "RooRealVar.h"
21#include "../RooAbsMinimizerFcn.h"
22
24#include "Math/IFunction.h" // ROOT::Math::IMultiGradFunction
25
26// forward declaration
27class RooAbsReal;
28class RooMinimizer;
29
30namespace RooFit {
31namespace TestStatistics {
32
34public:
35 MinuitFcnGrad(const std::shared_ptr<RooFit::TestStatistics::RooAbsL> &_likelihood, RooMinimizer *context,
36 std::vector<ROOT::Fit::ParameterSettings> &parameters, LikelihoodMode likelihoodMode,
37 LikelihoodGradientMode likelihoodGradientMode, bool verbose = false);
38
39 inline ROOT::Math::IMultiGradFunction *Clone() const override { return new MinuitFcnGrad(*this); }
40
41 /// Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.
42 bool Synchronize(std::vector<ROOT::Fit::ParameterSettings> &parameter_settings, bool optConst,
43 bool verbose = false) override;
44
45 // used inside Minuit:
46 inline bool returnsInMinuit2ParameterSpace() const override { return gradient->usesMinuitInternalValues(); }
47
48 inline void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) override
49 {
50 likelihood->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
51 }
52
53 bool fit(ROOT::Fit::Fitter& fitter) const override { return fitter.FitFCN(*this); };
54 ROOT::Math::IMultiGenFunction* getMultiGenFcn() override { return this; };
55
56private:
57 /// IMultiGradFunction override necessary for Minuit
58 double DoEval(const double *x) const override;
59
60public:
61 /// IMultiGradFunction overrides necessary for Minuit
62 void Gradient(const double *x, double *grad) const override;
63 void GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2,
64 double *previous_gstep) const override;
65
66 /// Part of IMultiGradFunction interface, used widely both in Minuit and in RooFit.
67 inline unsigned int NDim() const override { return _nDim; }
68
69 inline std::string getFunctionName() const override { return likelihood->GetName(); }
70
71 inline std::string getFunctionTitle() const override { return likelihood->GetTitle(); }
72
73 inline void setOffsetting(bool flag) override { likelihood->enableOffsetting(flag); }
74
75private:
76 /// This override should not be used in this class, so it throws.
77 double DoDerivative(const double *x, unsigned int icoord) const override;
78
79 bool syncParameterValuesFromMinuitCalls(const double *x, bool minuit_internal) const;
80
81 // members
82 std::shared_ptr<LikelihoodWrapper> likelihood;
83 std::shared_ptr<LikelihoodWrapper> likelihood_in_gradient;
84 std::shared_ptr<LikelihoodGradientWrapper> gradient;
85 mutable bool calculating_gradient_ = false;
86
87public:
88 mutable std::shared_ptr<WrapperCalculationCleanFlags> calculation_is_clean;
89
90private:
91 mutable std::vector<double> minuit_internal_x_;
92 mutable std::vector<double> minuit_external_x_;
93
94public:
96};
97
98} // namespace TestStatistics
99} // namespace RooFit
100
101#endif // ROOT_ROOFIT_TESTSTATISTICS_MinuitFcnGrad
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:77
bool FitFCN(unsigned int npar, Function &fcn, const double *params=0, unsigned int dataSize=0, bool chi2fit=false)
Fit using the a generic FCN function as a C++ callable object implementing double () (const double *)...
Definition: Fitter.h:653
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:62
Interface (abstract class) for multi-dimensional functions providing a gradient calculation.
Definition: IFunction.h:343
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:62
Minuit-RooMinimizer interface which synchronizes parameter data and coordinates evaluation of likelih...
Definition: MinuitFcnGrad.h:33
void setOffsetting(bool flag) override
Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
Definition: MinuitFcnGrad.h:73
void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) override
This function must be overridden in the derived class to pass on constant term optimization configura...
Definition: MinuitFcnGrad.h:48
bool Synchronize(std::vector< ROOT::Fit::ParameterSettings > &parameter_settings, bool optConst, bool verbose=false) override
Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.
std::vector< double > minuit_internal_x_
Definition: MinuitFcnGrad.h:91
std::string getFunctionName() const override
RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
Definition: MinuitFcnGrad.h:69
double DoDerivative(const double *x, unsigned int icoord) const override
This override should not be used in this class, so it throws.
bool fit(ROOT::Fit::Fitter &fitter) const override
Definition: MinuitFcnGrad.h:53
double DoEval(const double *x) const override
IMultiGradFunction override necessary for Minuit.
ROOT::Math::IMultiGenFunction * getMultiGenFcn() override
Definition: MinuitFcnGrad.h:54
bool syncParameterValuesFromMinuitCalls(const double *x, bool minuit_internal) const
Minuit calls (via FcnAdapters etc) DoEval or Gradient with a set of parameters x.
std::shared_ptr< WrapperCalculationCleanFlags > calculation_is_clean
Definition: MinuitFcnGrad.h:88
bool returnsInMinuit2ParameterSpace() const override
Definition: MinuitFcnGrad.h:46
std::string getFunctionTitle() const override
RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class...
Definition: MinuitFcnGrad.h:71
std::shared_ptr< LikelihoodWrapper > likelihood_in_gradient
Definition: MinuitFcnGrad.h:83
void GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2, double *previous_gstep) const override
void Gradient(const double *x, double *grad) const override
IMultiGradFunction overrides necessary for Minuit.
ROOT::Math::IMultiGradFunction * Clone() const override
Clone a function.
Definition: MinuitFcnGrad.h:39
std::shared_ptr< LikelihoodWrapper > likelihood
Definition: MinuitFcnGrad.h:82
std::vector< double > minuit_external_x_
Definition: MinuitFcnGrad.h:92
std::shared_ptr< LikelihoodGradientWrapper > gradient
Definition: MinuitFcnGrad.h:84
unsigned int NDim() const override
Part of IMultiGradFunction interface, used widely both in Minuit and in RooFit.
Definition: MinuitFcnGrad.h:67
MinuitFcnGrad(const std::shared_ptr< RooFit::TestStatistics::RooAbsL > &_likelihood, RooMinimizer *context, std::vector< ROOT::Fit::ParameterSettings > &parameters, LikelihoodMode likelihoodMode, LikelihoodGradientMode likelihoodGradientMode, bool verbose=false)
RooMinimizer is a wrapper class around ROOT::Fit:Fitter that provides a seamless interface between th...
Definition: RooMinimizer.h:43
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: Common.h:18