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
26class RooMinimizer;
27
28namespace RooFit {
29namespace TestStatistics {
30
32public:
33 MinuitFcnGrad(const std::shared_ptr<RooFit::TestStatistics::RooAbsL> &absL, RooMinimizer *context,
34 std::vector<ROOT::Fit::ParameterSettings> &parameters, LikelihoodMode likelihoodMode,
35 LikelihoodGradientMode likelihoodGradientMode);
36
37 /// Overridden from RooAbsMinimizerFcn to include gradient strategy synchronization.
38 bool Synchronize(std::vector<ROOT::Fit::ParameterSettings> &parameter_settings) override;
39
40 // used inside Minuit:
41 inline bool returnsInMinuit2ParameterSpace() const { return _gradient->usesMinuitInternalValues(); }
42
43 inline void setOptimizeConstOnFunction(RooAbsArg::ConstOpCode opcode, bool doAlsoTrackingOpt) override
44 {
45 applyToLikelihood([&](auto &l) { l.constOptimizeTestStatistic(opcode, doAlsoTrackingOpt); });
46 }
47
49
50 double operator()(const double *x) const;
51
52 /// IMultiGradFunction overrides necessary for Minuit
53 void Gradient(const double *x, double *grad) const;
54 void GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2,
55 double *previous_gstep) const;
56
57 inline std::string getFunctionName() const override { return _likelihood->GetName(); }
58
59 inline std::string getFunctionTitle() const override { return _likelihood->GetTitle(); }
60
61 inline void setOffsetting(bool flag) override
62 {
63 applyToLikelihood([&](auto &l) { l.enableOffsetting(flag); });
64 if (!flag) {
65 offsets_reset_ = true;
66 }
67 }
68
69private:
70 bool syncParameterValuesFromMinuitCalls(const double *x, bool minuit_internal) const;
71
72 template <class Func>
73 void applyToLikelihood(Func &&func) const
74 {
75 func(*_likelihood);
78 }
79 }
80
81 // members
82 // the likelihoods are shared_ptrs because they may point to the same object
83 std::shared_ptr<LikelihoodWrapper> _likelihood;
84 std::shared_ptr<LikelihoodWrapper> _likelihoodInGradient;
85 std::unique_ptr<LikelihoodGradientWrapper> _gradient;
86 mutable bool _calculatingGradient = false;
87
88 mutable std::shared_ptr<WrapperCalculationCleanFlags> _calculationIsClean;
89
90 mutable std::vector<double> _minuitInternalX;
91 mutable std::vector<double> _minuitExternalX;
92 // offsets_reset_ should be reset also when applyWeightSquared is activated in LikelihoodWrappers;
93 // currently setting this is not supported, so it doesn't happen.
94 mutable bool offsets_reset_ = false;
95 void syncOffsets() const;
96
97 std::unique_ptr<ROOT::Math::IMultiGradFunction> _multiGenFcn;
98
99 mutable bool _minuitInternalRooFitXMismatch = false;
100};
101
102} // namespace TestStatistics
103} // namespace RooFit
104
105#endif // ROOT_ROOFIT_TESTSTATISTICS_MinuitFcnGrad
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:61
Minuit-RooMinimizer interface which synchronizes parameter data and coordinates evaluation of likelih...
void GradientWithPrevResult(const double *x, double *grad, double *previous_grad, double *previous_g2, double *previous_gstep) const
double operator()(const double *x) const
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::string getFunctionName() const override
RooMinimizer sometimes needs the name of the minimized function. Implement this in the derived class.
void Gradient(const double *x, double *grad) const
IMultiGradFunction overrides necessary for Minuit.
std::vector< double > _minuitInternalX
std::shared_ptr< LikelihoodWrapper > _likelihood
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::unique_ptr< LikelihoodGradientWrapper > _gradient
void syncOffsets() const
Make sure the offsets are up to date.
std::string getFunctionTitle() const override
RooMinimizer sometimes needs the title of the minimized function. Implement this in the derived class...
std::shared_ptr< WrapperCalculationCleanFlags > _calculationIsClean
std::vector< double > _minuitExternalX
std::shared_ptr< LikelihoodWrapper > _likelihoodInGradient
void applyToLikelihood(Func &&func) const
std::unique_ptr< ROOT::Math::IMultiGradFunction > _multiGenFcn
Wrapper class around ROOT::Math::Minimizer that provides a seamless interface between the minimizer f...
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 CodegenImpl.h:64
TLine l
Definition textangle.C:4