IFitter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 
6 #include "CAFAna/Core/IFitVar.h"
8 #include "CAFAna/Fit/SeedList.h"
9 
10 namespace ana
11 {
12  class IExperiment;
13  class ISyst;
14 
15  /// Base class for fitters.
16  class IFitter
17  {
18  protected:
20 
21  public:
23 
24  IFitter(const std::vector<const IFitVar *>& vars,
25  const std::vector<const ISyst *>& systs = {});
26  IFitter(const IFitter & f);
27 
28  virtual ~IFitter();
29  /// Minimization and MCMC fits return very different information,
30  /// but various places need to know certain minimal things about them.
31  /// MinuitFitter and BayesianFitter both contain derived versions of this interface.
33  {
34  public:
35  // Need a virtual destructor to avoid leaking members of derived classes
36  virtual ~IFitSummary();
37 
38  /// Is this fit better than \a other ?
39  virtual bool IsBetterThan(const IFitSummary* other) const = 0;
40 
41  /// What's the value of the thing being minimized or maximized? (LL, chi2, etc.)
42  /// Interpretation depends on the derived class...
43  virtual double EvalMetricVal() const = 0;
44  };
45 
46  /// \brief Master fitting method. Depends on FitHelper and FitHelperSeeded.
47  ///
48  /// \param[out] seed Seed parameter and output best-fit point
49  /// \param[out] bestSysts Best systematics result returned here
50  /// \param seedPts List of oscillation parameter seeds
51  /// \param systSeedPts If non-empty, try fit starting at each of these
52  /// \param verb If quiet, no printout
53  /// \return -2x the log-likelihood of the best-fit point
54  virtual std::unique_ptr<IFitSummary> Fit(osc::IOscCalcAdjustable *seed,
55  SystShifts &bestSysts = junkShifts,
56  const SeedList& seedPts = SeedList(),
57  const std::vector<SystShifts>& systSeedPts = {},
58  Verbosity verb = kVerbose) const;
59 
60  /// Variant with no seedPts
61  virtual std::unique_ptr<IFitSummary> Fit(osc::IOscCalcAdjustable* seed,
62  SystShifts& systSeed,
63  Verbosity verb) const
64  {
65  return Fit(seed, systSeed, {}, std::vector<SystShifts>(1, systSeed), verb);
66  }
67 
68  /// Variant with no seedPts and no systematics result returned
69  virtual std::unique_ptr<IFitSummary> Fit(osc::IOscCalcAdjustable* seed,
70  Verbosity verb) const
71  {
72  return Fit(seed, junkShifts, {}, {}, verb);
73  }
74 
75  /// Variant with no oscillations - useful for ND fits
76  virtual std::unique_ptr<IFitSummary> Fit(SystShifts& systSeed, Verbosity verb = kVerbose) const
77  {
78  return Fit(nullptr, systSeed, {}, std::vector<SystShifts>(1, systSeed), verb);
79  }
80 
81 
82  std::unique_ptr<SystShifts> GetSystShifts() const {return fShifts->Copy();}
83 
84  protected:
85  struct SeedPt
86  {
87  SeedPt(const Seed& f, const SystShifts& s) : fitvars(f), shift(s) {}
90  };
91  std::vector<SeedPt> ExpandSeeds(const SeedList& seedPts,
92  const std::vector<SystShifts>& systSeedPts) const;
93 
94  /// Helper for \ref FitHelper -- actually does fitting. Reimplement in derived classes
95  virtual std::unique_ptr<IFitSummary>
97  SystShifts &systSeed,
98  Verbosity verb) const = 0;
99 
100  /// Helper for \ref Fit
101  virtual std::unique_ptr<IFitSummary>
103  SystShifts& bestSysts,
104  const SeedList& seedPts,
105  const std::vector<SystShifts>& systSeedPts,
106  Verbosity verb) const;
107 
108 
109  /// Check that the seeds that were specified are compatible with the vars being fitted
111  const SeedList& seedPts,
112  const std::vector<SystShifts>& systSeedPts) const;
113 
114  std::vector<const IFitVar*> fVars;
115  std::vector<const ISyst*> fSysts;
116  mutable std::unique_ptr<SystShifts> fShifts;
117  mutable Verbosity fVerb;
118 
119  };
120 
121 }
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
std::vector< SystGroupDef > systs
Definition: syst_header.h:385
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
virtual std::unique_ptr< IFitSummary > Fit(SystShifts &systSeed, Verbosity verb=kVerbose) const
Variant with no oscillations - useful for ND fits.
Definition: IFitter.h:76
void ValidateSeeds(osc::IOscCalcAdjustable *seed, const SeedList &seedPts, const std::vector< SystShifts > &systSeedPts) const
Check that the seeds that were specified are compatible with the vars being fitted.
Definition: IFitter.cxx:170
std::vector< const ISyst * > fSysts
Definition: IFitter.h:115
const XML_Char * s
Definition: expat.h:262
virtual ~IFitter()
Definition: IFitter.cxx:35
unsigned int seed
Definition: runWimpSim.h:102
virtual std::unique_ptr< IFitSummary > Fit(osc::IOscCalcAdjustable *seed, SystShifts &bestSysts=junkShifts, const SeedList &seedPts=SeedList(), const std::vector< SystShifts > &systSeedPts={}, Verbosity verb=kVerbose) const
Master fitting method. Depends on FitHelper and FitHelperSeeded.
Definition: IFitter.cxx:69
SeedPt(const Seed &f, const SystShifts &s)
Definition: IFitter.h:87
virtual std::unique_ptr< IFitSummary > FitHelperSeeded(osc::IOscCalcAdjustable *seed, SystShifts &systSeed, Verbosity verb) const =0
Helper for FitHelper – actually does fitting. Reimplement in derived classes.
const std::map< std::pair< std::string, std::string >, Variable > vars
Base class for fitters.
Definition: IFitter.h:16
std::vector< const IFitVar * > fVars
Definition: IFitter.h:114
virtual double EvalMetricVal() const =0
static SystShifts junkShifts
Definition: IFitter.h:19
virtual bool IsBetterThan(const IFitSummary *other) const =0
Is this fit better than other ?
std::vector< SeedPt > ExpandSeeds(const SeedList &seedPts, const std::vector< SystShifts > &systSeedPts) const
Definition: IFitter.cxx:46
std::unique_ptr< SystShifts > GetSystShifts() const
Definition: IFitter.h:82
std::unique_ptr< SystShifts > fShifts
Definition: IFitter.h:116
SystShifts shift
Definition: IFitter.h:89
virtual std::unique_ptr< IFitSummary > Fit(osc::IOscCalcAdjustable *seed, SystShifts &systSeed, Verbosity verb) const
Variant with no seedPts.
Definition: IFitter.h:61
virtual std::unique_ptr< IFitSummary > FitHelper(osc::IOscCalcAdjustable *seed, SystShifts &bestSysts, const SeedList &seedPts, const std::vector< SystShifts > &systSeedPts, Verbosity verb) const
Helper for Fit.
Definition: IFitter.cxx:117
virtual std::unique_ptr< IFitSummary > Fit(osc::IOscCalcAdjustable *seed, Verbosity verb) const
Variant with no seedPts and no systematics result returned.
Definition: IFitter.h:69
Verbosity fVerb
Definition: IFitter.h:117
IFitter(const std::vector< const IFitVar * > &vars, const std::vector< const ISyst * > &systs={})
Definition: IFitter.cxx:21