Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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);
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) override;
43
44 // used inside Minuit:
45 inline bool returnsInMinuit2ParameterSpace() const override { return gradient->usesMinuitInternalValues(); }
46
47 inline void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) override
48 {
49 likelihood->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
51 likelihood_in_gradient->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
52 }
53 }
54
55 bool fit(ROOT::Fit::Fitter &fitter) const override { return fitter.FitFCN(*this); };
56 ROOT::Math::IMultiGenFunction *getMultiGenFcn() override { return this; };
57
58private:
59 /// IMultiGradFunction override necessary for Minuit
60 double DoEval(const double *x) const override;
61
62public:
63 /// IMultiGradFunction overrides necessary for Minuit
64 void Gradient(const double *x, double *grad) const override;
65 void GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2,
66 double *previous_gstep) const override;
67
68 /// Part of IMultiGradFunction interface, used widely both in Minuit and in RooFit.
69 inline unsigned int NDim() const override { return _nDim; }
70
71 inline std::string getFunctionName() const override { return likelihood->GetName(); }
72
73 inline std::string getFunctionTitle() const override { return likelihood->GetTitle(); }
74
75 inline void setOffsetting(bool flag) override
76 {
77 likelihood->enableOffsetting(flag);
79 likelihood_in_gradient->enableOffsetting(flag);
80 }
81 }
82
83private:
84 /// This override should not be used in this class, so it throws.
85 double DoDerivative(const double *x, unsigned int icoord) const override;
86
87 bool syncParameterValuesFromMinuitCalls(const double *x, bool minuit_internal) const;
88
89 // members
90 std::shared_ptr<LikelihoodWrapper> likelihood;
91 std::shared_ptr<LikelihoodWrapper> likelihood_in_gradient;
92 std::shared_ptr<LikelihoodGradientWrapper> gradient;
93 mutable bool calculating_gradient_ = false;
94
95public:
96 mutable std::shared_ptr<WrapperCalculationCleanFlags> calculation_is_clean;
97
98private:
99 mutable std::vector<double> minuit_internal_x_;
100 mutable std::vector<double> minuit_external_x_;
101
102public:
104};
105
106} // namespace TestStatistics
107} // namespace RooFit
108
109#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:659
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...
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...
bool Synchronize(std::vector< ROOT::Fit::ParameterSettings > &parameter_settings) override
Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.
std::vector< double > minuit_internal_x_
std::string getFunctionName() const override
RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
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
double DoEval(const double *x) const override
IMultiGradFunction override necessary for Minuit.
ROOT::Math::IMultiGenFunction * getMultiGenFcn() override
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
bool returnsInMinuit2ParameterSpace() const override
std::string getFunctionTitle() const override
RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class...
std::shared_ptr< LikelihoodWrapper > likelihood_in_gradient
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.
std::shared_ptr< LikelihoodWrapper > likelihood
std::vector< double > minuit_external_x_
std::shared_ptr< LikelihoodGradientWrapper > gradient
unsigned int NDim() const override
Part of IMultiGradFunction interface, used widely both in Minuit and in RooFit.
RooMinimizer is a wrapper class around ROOT::Fit:Fitter that provides a seamless interface between th...
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