Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
ana::IFitter Class Referenceabstract

Base class for fitters. More...

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N20-10-23/CAFAna/Fit/IFitter.h"

Inheritance diagram for ana::IFitter:
ana::MinuitFitter ana::StanFitter

Classes

class  IFitSummary
 
struct  SeedPt
 

Public Types

enum  Verbosity { kQuiet, kVerbose }
 

Public Member Functions

 IFitter (const std::vector< const IFitVar * > &vars, const std::vector< const ISyst * > &systs={})
 
 IFitter (const IFitter &f)
 
virtual ~IFitter ()
 
virtual std::unique_ptr< IFitSummaryFit (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. More...
 
virtual std::unique_ptr< IFitSummaryFit (osc::IOscCalcAdjustable *seed, SystShifts &systSeed, Verbosity verb) const
 Variant with no seedPts. More...
 
virtual std::unique_ptr< IFitSummaryFit (osc::IOscCalcAdjustable *seed, Verbosity verb) const
 Variant with no seedPts and no systematics result returned. More...
 
virtual std::unique_ptr< IFitSummaryFit (SystShifts &systSeed, Verbosity verb=kVerbose) const
 Variant with no oscillations - useful for ND fits. More...
 
std::unique_ptr< SystShiftsGetSystShifts () const
 

Protected Member Functions

std::vector< SeedPtExpandSeeds (const SeedList &seedPts, const std::vector< SystShifts > &systSeedPts) const
 
virtual std::unique_ptr< IFitSummaryFitHelperSeeded (osc::IOscCalcAdjustable *seed, SystShifts &systSeed, Verbosity verb) const =0
 Helper for FitHelper – actually does fitting. Reimplement in derived classes. More...
 
virtual std::unique_ptr< IFitSummaryFitHelper (osc::IOscCalcAdjustable *seed, SystShifts &bestSysts, const SeedList &seedPts, const std::vector< SystShifts > &systSeedPts, Verbosity verb) const
 Helper for Fit. More...
 
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. More...
 

Protected Attributes

std::vector< const IFitVar * > fVars
 
std::vector< const ISyst * > fSysts
 
std::unique_ptr< SystShiftsfShifts
 
Verbosity fVerb
 

Static Protected Attributes

static SystShifts junkShifts = SystShifts()
 

Detailed Description

Base class for fitters.

Definition at line 16 of file IFitter.h.

Member Enumeration Documentation

Enumerator
kQuiet 
kVerbose 

Definition at line 22 of file IFitter.h.

Constructor & Destructor Documentation

ana::IFitter::IFitter ( const std::vector< const IFitVar * > &  vars,
const std::vector< const ISyst * > &  systs = {} 
)

Definition at line 21 of file IFitter.cxx.

23  : fVars(vars), fSysts(systs)
24  {
25  }
std::vector< SystGroupDef > systs
Definition: syst_header.h:385
std::vector< const ISyst * > fSysts
Definition: IFitter.h:115
const std::map< std::pair< std::string, std::string >, Variable > vars
std::vector< const IFitVar * > fVars
Definition: IFitter.h:114
ana::IFitter::IFitter ( const IFitter f)

Definition at line 28 of file IFitter.cxx.

29  : fVars(other.fVars),
30  fSysts(other.fSysts),
31  fShifts(std::make_unique<SystShifts>(*other.fShifts))
32  {}
std::vector< const ISyst * > fSysts
Definition: IFitter.h:115
std::vector< const IFitVar * > fVars
Definition: IFitter.h:114
std::unique_ptr< SystShifts > fShifts
Definition: IFitter.h:116
ana::IFitter::~IFitter ( )
virtual

Definition at line 35 of file IFitter.cxx.

36  {
37  }

Member Function Documentation

std::vector< IFitter::SeedPt > ana::IFitter::ExpandSeeds ( const SeedList seedPts,
const std::vector< SystShifts > &  systSeedPts 
) const
protected

Definition at line 46 of file IFitter.cxx.

References ana::SeedList::GetSeeds(), ana::SystShifts::Nominal(), gen_flatrecord::pt, runNovaSAM::ret, and seed.

Referenced by FitHelper().

48  {
49  std::vector<SeedPt> ret;
50  for(Seed seed: seedPts.GetSeeds()) ret.emplace_back(seed, SystShifts::Nominal());
51 
52  // Now duplicate as many times as required for the syst seeds
53  if(!systSeedPts.empty()){
54  std::vector<SeedPt> newret;
55  for(const SystShifts& s: systSeedPts){
56  for(SeedPt pt: ret){
57  pt.shift = s;
58  newret.push_back(pt);
59  }
60  }
61  ret = newret;
62  }
63 
64  return ret;
65  }
static SystShifts Nominal()
Definition: SystShifts.h:34
const XML_Char * s
Definition: expat.h:262
unsigned int seed
Definition: runWimpSim.h:102
std::unique_ptr< IFitter::IFitSummary > ana::IFitter::Fit ( osc::IOscCalcAdjustable seed,
SystShifts bestSysts = junkShifts,
const SeedList seedPts = SeedList(),
const std::vector< SystShifts > &  systSeedPts = {},
Verbosity  verb = kVerbose 
) const
virtual

Master fitting method. Depends on FitHelper and FitHelperSeeded.

Parameters
[out]seedSeed parameter and output best-fit point
[out]bestSystsBest systematics result returned here
seedPtsList of oscillation parameter seeds
systSeedPtsIf non-empty, try fit starting at each of these
verbIf quiet, no printout
Returns
-2x the log-likelihood of the best-fit point

Reimplemented in ana::StanFitter.

Definition at line 69 of file IFitter.cxx.

References om::cout, allTimeWatchdog::endl, FitHelper(), fSysts, fVars, fVerb, ana::SystShifts::GetShift(), kVerbose, registry_explorer::v, and ValidateSeeds().

Referenced by BlessedPlotsAna(), BlessedPlotsAnaByPeriod(), cc(), ana::CovMxSurface::CovMxSurface(), fake_future_data(), FCContour(), FCTutorial2020(), fill_col(), ana::FrequentistSurface::FillSurfacePoint(), ana::FrequentistSurface::FindMinimum(), ana::FindValley(), Fit(), ana::StanFitter::Fit(), ana::MinuitFitter::FitHelperSeeded(), getBestFit(), joint_fit_2017_contours(), joint_fit_2017_make_fc_slice(), joint_fit_2017_make_fc_surf(), joint_fit_2017_slices(), joint_fit_2018_contours(), joint_fit_2018_slices(), joint_fit_future_bestfit_univ(), joint_fit_future_contour_univ(), make_fc_mass_and_oct_nersc_2018(), make_fc_mh_nersc_2018(), make_fc_nus_surfs_nersc_2018(), make_fc_nus_surfs_nersc_2019(), make_fc_oct_nersc_2018(), make_fc_slices_nersc_2018(), make_fc_slices_nersc_2018_stats(), make_fc_surfaces_2020(), make_fc_surfaces_2020_validation(), make_fc_surfaces_nersc_2018(), make_fc_surfaces_nersc_2018_stats(), make_nus17_fc_surfs(), make_nus_fc_surfs(), make_starplots(), MakeCAFSensitivities_for_FNEX(), numu_sig_nonmax(), plot_shifts(), PlotNus17Prediction(), ana::Profile(), reach_2018_dCPfractions(), ana::RefineSeeds(), run_joint_fit_2020_bestfit(), run_joint_fit_2020_contours(), run_joint_fit_2020_slices(), RunFitter(), sensitivity2018(), sensitivity2020(), starPlot(), syst_test(), and test_ana().

74  {
75  fVerb = verb;
76  ValidateSeeds(seed, seedPts, systSeedPts);
77 
78  if (fVerb == kVerbose)
79  {
80  std::cout << "Finding best fit for";
81  for (const auto *v: fVars) std::cout << " " << v->ShortName();
82  for (const auto *s: fSysts) std::cout << " " << s->ShortName();
83 // if (fSupportsDerivatives) std::cout << " using analytic derivatives";
84  std::cout << "..." << std::endl;
85  }
86 
87  // Do all the actual work. This wrapper function is just so we can have
88  // better control of printing.
89  auto fitSummary = FitHelper(seed, bestSysts, seedPts, systSeedPts, verb);
90 
91  if (fVerb == kVerbose)
92  {
93  std::cout << "Best fit";
94  for (const auto *v: fVars)
95  {
96  std::cout << ", " << v->ShortName() << " = " << v->GetValue(seed);
97  }
98  for (const auto *s: fSysts)
99  {
100  std::cout << ", " << s->ShortName() << " = " << bestSysts.GetShift(s);
101  }
102  std::cout << ", eval metric = " << fitSummary->EvalMetricVal() << std::endl;
103 
104 // std::cout << " found with " << fNEval << " evaluations of the likelihood";
105 // if (fNEvalFiniteDiff > 0)
106 // std::cout << " (" << fNEvalFiniteDiff << " to evaluate gradients numerically)";
107 // if (fNEvalGrad > 0)
108 // std::cout << " and " << fNEvalGrad << " evaluations of the gradient";
109 // std::cout << std::endl;
110  }
111 
112  return fitSummary;
113  }
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
OStream cout
Definition: OStream.cxx:6
std::vector< const IFitVar * > fVars
Definition: IFitter.h:114
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
Verbosity fVerb
Definition: IFitter.h:117
virtual std::unique_ptr<IFitSummary> ana::IFitter::Fit ( osc::IOscCalcAdjustable seed,
SystShifts systSeed,
Verbosity  verb 
) const
inlinevirtual

Variant with no seedPts.

Definition at line 61 of file IFitter.h.

References Fit().

64  {
65  return Fit(seed, systSeed, {}, std::vector<SystShifts>(1, systSeed), verb);
66  }
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
virtual std::unique_ptr<IFitSummary> ana::IFitter::Fit ( osc::IOscCalcAdjustable seed,
Verbosity  verb 
) const
inlinevirtual

Variant with no seedPts and no systematics result returned.

Definition at line 69 of file IFitter.h.

References Fit().

71  {
72  return Fit(seed, junkShifts, {}, {}, verb);
73  }
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
static SystShifts junkShifts
Definition: IFitter.h:19
virtual std::unique_ptr<IFitSummary> ana::IFitter::Fit ( SystShifts systSeed,
Verbosity  verb = kVerbose 
) const
inlinevirtual

Variant with no oscillations - useful for ND fits.

Definition at line 76 of file IFitter.h.

References Fit().

77  {
78  return Fit(nullptr, systSeed, {}, std::vector<SystShifts>(1, systSeed), verb);
79  }
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
std::unique_ptr< IFitter::IFitSummary > ana::IFitter::FitHelper ( osc::IOscCalcAdjustable seed,
SystShifts bestSysts,
const SeedList seedPts,
const std::vector< SystShifts > &  systSeedPts,
Verbosity  verb 
) const
protectedvirtual

Helper for Fit.

Definition at line 117 of file IFitter.cxx.

References ana::assert(), ana::SystShifts::Copy(), osc::_IOscCalcAdjustable< T >::Copy(), ExpandSeeds(), FitHelperSeeded(), fShifts, fSysts, fVars, MECModelEnuComparisons::i, gen_flatrecord::pt, seed, ana::SystShifts::SetShift(), and registry_explorer::v.

Referenced by Fit().

122  {
123  // if user passed a derived kind of SystShifts, this preserves it
124  fShifts = bestSysts.Copy();
125  fShifts->ResetToNominal();
126 
127  const std::vector<SeedPt> pts = ExpandSeeds(seedPts, systSeedPts);
128 
129  std::unique_ptr<IFitSummary> bestFitSummary;
130  std::vector<double> bestFitPars, bestSystPars;
131 
132  for (const SeedPt &pt: pts)
133  {
134  osc::IOscCalcAdjustable* seed = nullptr;
135  if(initseed) seed = initseed->Copy();
136 
137  pt.fitvars.ResetCalc(seed);
138 
139  // be sure to keep any derived class stuff around
140  auto shift = fShifts->Copy();
141  *shift = pt.shift;
142  auto fitSummary = FitHelperSeeded(seed, *shift, verb);
143  if (fitSummary->IsBetterThan(bestFitSummary.get()))
144  {
145  bestFitSummary = std::move(fitSummary);
146  // Store the best fit values of all the parameters we know are being
147  // varied.
148  bestFitPars.clear();
149  bestSystPars.clear();
150  for (const auto *v: fVars)
151  bestFitPars.push_back(v->GetValue(seed));
152  for (const auto *s: fSysts)
153  bestSystPars.push_back(shift->GetShift<double>(s));
154  }
155 
156  delete seed;
157  } // end for pt
158 
159  assert(bestFitSummary);
160  // Stuff the results of the actual best fit back into the seeds
161  for (unsigned int i = 0; i < fVars.size(); ++i)
162  fVars[i]->SetValue(initseed, bestFitPars[i]);
163  for (unsigned int i = 0; i < fSysts.size(); ++i)
164  bestSysts.SetShift(fSysts[i], bestSystPars[i]);
165 
166  return std::move(bestFitSummary);
167  }
virtual _IOscCalcAdjustable< T > * Copy() const =0
std::vector< const ISyst * > fSysts
Definition: IFitter.h:115
const XML_Char * s
Definition: expat.h:262
unsigned int seed
Definition: runWimpSim.h:102
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.
std::vector< const IFitVar * > fVars
Definition: IFitter.h:114
assert(nhit_max >=nhit_nbins)
std::vector< SeedPt > ExpandSeeds(const SeedList &seedPts, const std::vector< SystShifts > &systSeedPts) const
Definition: IFitter.cxx:46
std::unique_ptr< SystShifts > fShifts
Definition: IFitter.h:116
virtual std::unique_ptr<IFitSummary> ana::IFitter::FitHelperSeeded ( osc::IOscCalcAdjustable seed,
SystShifts systSeed,
Verbosity  verb 
) const
protectedpure virtual

Helper for FitHelper – actually does fitting. Reimplement in derived classes.

Implemented in ana::StanFitter, and ana::MinuitFitter.

Referenced by FitHelper().

std::unique_ptr<SystShifts> ana::IFitter::GetSystShifts ( ) const
inline

Definition at line 82 of file IFitter.h.

References fShifts.

Referenced by plot_shifts().

82 {return fShifts->Copy();}
std::unique_ptr< SystShifts > fShifts
Definition: IFitter.h:116
void ana::IFitter::ValidateSeeds ( osc::IOscCalcAdjustable seed,
const SeedList seedPts,
const std::vector< SystShifts > &  systSeedPts 
) const
protected

Check that the seeds that were specified are compatible with the vars being fitted.

Definition at line 170 of file IFitter.cxx.

References ana::SeedList::ActiveFitVars(), om::cout, allTimeWatchdog::endl, fSysts, fVars, gen_flatrecord::pt, and registry_explorer::v.

Referenced by Fit().

173  {
174  if(!seed && !fVars.empty()){
175  std::cout << "ERROR: MinuitFitter::Fit() trying to fit oscillation parameters without an oscillation calculator" << std::endl;
176  abort();
177  }
178 
179  for(const IFitVar* v: seedPts.ActiveFitVars()){
180  if (std::find(fVars.begin(), fVars.end(), v) == fVars.end()){
181  std::cout << "ERROR MinuitFitter::Fit() trying to seed '"
182  << v->ShortName()
183  << "' which is not part of the fit." << std::endl;
184  abort();
185  }
186  }
187 
188  for(const SystShifts& pt: systSeedPts){
189  for(const ISyst* s: pt.ActiveSysts()){
190  if(std::find(fSysts.begin(), fSysts.end(), s) == fSysts.end()){
191  std::cout << "ERROR MinuitFitter::Fit() trying to seed '"
192  << s->ShortName()
193  << "' which is not part of the fit." << std::endl;
194  abort();
195  }
196  }
197  }
198  }
std::vector< const ISyst * > fSysts
Definition: IFitter.h:115
const XML_Char * s
Definition: expat.h:262
OStream cout
Definition: OStream.cxx:6
std::vector< const IFitVar * > fVars
Definition: IFitter.h:114

Member Data Documentation

std::unique_ptr<SystShifts> ana::IFitter::fShifts
mutableprotected
std::vector<const ISyst*> ana::IFitter::fSysts
protected
std::vector<const IFitVar*> ana::IFitter::fVars
protected
Verbosity ana::IFitter::fVerb
mutableprotected

Definition at line 117 of file IFitter.h.

Referenced by Fit(), and ana::MinuitFitter::FitHelperSeeded().

SystShifts ana::IFitter::junkShifts = SystShifts()
staticprotected

Definition at line 19 of file IFitter.h.


The documentation for this class was generated from the following files: