MinuitFitter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "CAFAna/Fit/IFitter.h"
4 
5 #include "CAFAna/Core/IFitVar.h"
8 
9 #include "Minuit2/FCNGradientBase.h"
10 #include "Minuit2/FunctionMinimum.h"
11 #include "TMatrixDSym.h"
12 
13 namespace ana
14 {
15 
16 /// Perform MINUIT fits in one or two dimensions
18  : public IFitter, public ROOT::Minuit2::FCNGradientBase
19  {
20  public:
21  enum FitOpts
22  {
23  // These first three correspond to MIGRAD options (in a different
24  // order)
25  kNormal = 0, ///< default if un-specified
26  kCareful = 1,
27  kFast = 2,
28  // A simple alternate fitter, exclusive with the options above
29  kGradDesc = 3,
31  // This option is independent, you may xor it in with one of the above
33  };
34 
35  void SetFitOpts(FitOpts opts);
36 
38  std::vector<const IFitVar *> vars,
39  std::vector<const ISyst *> systs = {},
40  FitOpts opts = kNormal);
41 
42  ~MinuitFitter();
43 
44  /// Evaluate the log-likelihood, as required by MINUT interface
45  virtual double operator()(const std::vector<double> &pars) const override;
46 
47  std::vector<double> Gradient(const std::vector<double> &pars) const override;
48 
49  bool CheckGradient() const override { return (fFitOpts & kPrecisionMask) != kFast; }
50  TMatrixDSym GetCovariance() const {return fCovariance;}
51 
52  /// Definition of one-sigma, required by MINUIT
53  virtual double Up() const override { return 1; }
54 
55  protected:
56  /// Convert from our enum order to MINUIT's codes (0=fast, 1=normal,
57  /// 2=careful)
58  int GetMINUITPrecision() const {return ((fFitOpts & kPrecisionMask)+1)%3;}
59 
60  /// Stuff the parameters into the calculator and/or syst shifts object
61  void DecodePars(const std::vector<double>& pars, osc::IOscCalcAdjustable * calc) const;
62 
63  /// Concrete instance of IFitSummary for use in MinuitFitter
65  {
66  public:
67  MinuitFitSummary(std::unique_ptr<ROOT::Minuit2::FunctionMinimum> && minimum);
68 
69  const ROOT::Minuit2::FunctionMinimum * GetMinimizer() const { return fMinimum.get(); }
70 
71  double EvalMetricVal() const override; ///< The chi^2 passed to the fitter
72  bool IsBetterThan(const IFitSummary* other) const override; ///< Is the current fit better than \a other?
73 
74  private:
75  /// the minimizer instance
76  std::unique_ptr<ROOT::Minuit2::FunctionMinimum> fMinimum;
77  };
78 
79  /// Helper for \ref FitHelper
80  std::unique_ptr<IFitSummary> FitHelperSeeded(osc::IOscCalcAdjustable *seed,
81  SystShifts &systSeed,
82  Verbosity verb) const override;
83 
84  /// Intended to be called only once (from constructor) to initialize
85  /// fSupportsDerivatives
86  bool SupportsDerivatives() const;
87 
90 
92 
94 
95  mutable int fNEval = 0;
96  mutable int fNEvalGrad = 0;
97  mutable int fNEvalFiniteDiff = 0;
98  mutable TMatrixDSym fCovariance;
99  };
100 
101 }
TMatrixDSym GetCovariance() const
Definition: MinuitFitter.h:50
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
Concrete instance of IFitSummary for use in MinuitFitter.
Definition: MinuitFitter.h:64
std::vector< SystGroupDef > systs
Definition: syst_header.h:385
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
bool CheckGradient() const override
Definition: MinuitFitter.h:49
const ROOT::Minuit2::FunctionMinimum * GetMinimizer() const
Definition: MinuitFitter.h:69
bool SupportsDerivatives() const
MinuitFitSummary(std::unique_ptr< ROOT::Minuit2::FunctionMinimum > &&minimum)
osc::OscCalcDumb calc
osc::IOscCalcAdjustable * fCalc
Definition: MinuitFitter.h:88
const IExperiment * fExpt
Definition: MinuitFitter.h:89
default if un-specified
Definition: MinuitFitter.h:25
expt
Definition: demo5.py:34
unsigned int seed
Definition: runWimpSim.h:102
std::vector< double > Gradient(const std::vector< double > &pars) const override
bool IsBetterThan(const IFitSummary *other) const override
Is the current fit better than other?
virtual double Up() const override
Definition of one-sigma, required by MINUIT.
Definition: MinuitFitter.h:53
int GetMINUITPrecision() const
Definition: MinuitFitter.h:58
void DecodePars(const std::vector< double > &pars, osc::IOscCalcAdjustable *calc) const
Stuff the parameters into the calculator and/or syst shifts object.
const std::map< std::pair< std::string, std::string >, Variable > vars
Base class for fitters.
Definition: IFitter.h:16
TMatrixDSym fCovariance
Definition: MinuitFitter.h:98
std::unique_ptr< IFitSummary > FitHelperSeeded(osc::IOscCalcAdjustable *seed, SystShifts &systSeed, Verbosity verb) const override
Helper for FitHelper.
double EvalMetricVal() const override
The chi^2 passed to the fitter.
std::string pars("Th23Dmsq32")
MinuitFitter(const IExperiment *expt, std::vector< const IFitVar * > vars, std::vector< const ISyst * > systs={}, FitOpts opts=kNormal)
Base class defining interface for experiments.
Definition: IExperiment.h:14
virtual double operator()(const std::vector< double > &pars) const override
Evaluate the log-likelihood, as required by MINUT interface.
std::unique_ptr< ROOT::Minuit2::FunctionMinimum > fMinimum
the minimizer instance
Definition: MinuitFitter.h:76
void SetFitOpts(FitOpts opts)
Perform MINUIT fits in one or two dimensions.
Definition: MinuitFitter.h:17