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 <Fit/Fitter.h>
25#include "Math/IFunction.h" // ROOT::Math::IMultiGradFunction
26
27// forward declaration
28class RooAbsReal;
29class RooMinimizer;
30
31namespace RooFit {
32namespace TestStatistics {
33
34/// For communication with wrappers, an instance of this struct must be shared between them and MinuitFcnGrad. It keeps
35/// track of what has been evaluated for the current parameter set provided by Minuit.
37 // indicate whether that part has been calculated since the last parameter update
38 bool likelihood = false;
39 bool gradient = false;
40
41 void set_all(bool value)
42 {
43 likelihood = value;
44 gradient = value;
45 }
46};
47
49public:
50 enum class LikelihoodMode { serial, multiprocess };
51 enum class LikelihoodGradientMode { multiprocess };
52
53 MinuitFcnGrad(const std::shared_ptr<RooFit::TestStatistics::RooAbsL> &_likelihood, RooMinimizer *context,
54 std::vector<ROOT::Fit::ParameterSettings> &parameters,
55 LikelihoodMode likelihoodMode,
56 LikelihoodGradientMode likelihoodGradientMode, bool verbose = false);
57
58 inline ROOT::Math::IMultiGradFunction *Clone() const override { return new MinuitFcnGrad(*this); }
59
60 /// Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.
61 Bool_t Synchronize(std::vector<ROOT::Fit::ParameterSettings> &parameter_settings, Bool_t optConst,
62 Bool_t verbose = kFALSE) override;
63
64 // used inside Minuit:
65 inline bool returnsInMinuit2ParameterSpace() const override { return gradient->usesMinuitInternalValues(); }
66
67 inline void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, Bool_t doAlsoTrackingOpt) override
68 {
69 likelihood->constOptimizeTestStatistic(opcode, doAlsoTrackingOpt);
70 }
71
72private:
73 /// IMultiGradFunction override necessary for Minuit
74 double DoEval(const double *x) const override;
75
76public:
77 /// IMultiGradFunction overrides necessary for Minuit
78 void Gradient(const double *x, double *grad) const override;
79 void GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2,
80 double *previous_gstep) const override;
81
82 /// Part of IMultiGradFunction interface, used widely both in Minuit and in RooFit.
83 inline unsigned int NDim() const override { return _nDim; }
84
85 inline std::string getFunctionName() const override { return likelihood->GetName(); }
86
87 inline std::string getFunctionTitle() const override { return likelihood->GetTitle(); }
88
89 inline void setOffsetting(Bool_t flag) override { likelihood->enableOffsetting(flag); }
90
91private:
92 /// This override should not be used in this class, so it throws.
93 double DoDerivative(const double *x, unsigned int icoord) const override;
94
95 bool syncParameterValuesFromMinuitCalls(const double *x, bool minuit_internal) const;
96
97 // members
98 std::shared_ptr<LikelihoodWrapper> likelihood;
99 std::shared_ptr<LikelihoodGradientWrapper> gradient;
100
101public:
102 mutable std::shared_ptr<WrapperCalculationCleanFlags> calculation_is_clean;
103
104private:
105 mutable std::vector<double> minuit_internal_x_;
106 mutable std::vector<double> minuit_external_x_;
107
108public:
110};
111
112} // namespace TestStatistics
113} // namespace RooFit
114
115#endif // ROOT_ROOFIT_TESTSTATISTICS_MinuitFcnGrad
const Bool_t kFALSE
Definition: RtypesCore.h:101
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:63
Minuit-RooMinimizer interface which synchronizes parameter data and coordinates evaluation of likelih...
Definition: MinuitFcnGrad.h:48
void setOffsetting(Bool_t flag) override
Enable or disable offsetting on the function to be minimized, which enhances numerical precision.
Definition: MinuitFcnGrad.h:89
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.
Definition: MinuitFcnGrad.h:85
double DoDerivative(const double *x, unsigned int icoord) const override
This override should not be used in this class, so it throws.
Bool_t Synchronize(std::vector< ROOT::Fit::ParameterSettings > &parameter_settings, Bool_t optConst, Bool_t verbose=kFALSE) override
Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.
double DoEval(const double *x) const override
IMultiGradFunction override necessary for Minuit.
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
Definition: MinuitFcnGrad.h:65
std::string getFunctionTitle() const override
RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class...
Definition: MinuitFcnGrad.h:87
void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, Bool_t doAlsoTrackingOpt) override
This function must be overridden in the derived class to pass on constant term optimization configura...
Definition: MinuitFcnGrad.h:67
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:58
std::shared_ptr< LikelihoodWrapper > likelihood
Definition: MinuitFcnGrad.h:98
std::vector< double > minuit_external_x_
std::shared_ptr< LikelihoodGradientWrapper > gradient
Definition: MinuitFcnGrad.h:99
unsigned int NDim() const override
Part of IMultiGradFunction interface, used widely both in Minuit and in RooFit.
Definition: MinuitFcnGrad.h:83
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:49
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
For communication with wrappers, an instance of this struct must be shared between them and MinuitFcn...
Definition: MinuitFcnGrad.h:36