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

Perform MINUIT fits in one or two dimensions. More...

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

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

Classes

class  MinuitFitSummary
 Concrete instance of IFitSummary for use in MinuitFitter. More...
 

Public Types

enum  FitOpts {
  kNormal = 0, kCareful = 1, kFast = 2, kGradDesc = 3,
  kPrecisionMask = 3, kPrefitSysts = 4
}
 
enum  Verbosity { kQuiet, kVerbose }
 

Public Member Functions

void SetFitOpts (FitOpts opts)
 
 MinuitFitter (const IExperiment *expt, std::vector< const IFitVar * > vars, std::vector< const ISyst * > systs={}, FitOpts opts=kNormal)
 
 ~MinuitFitter ()
 
virtual double operator() (const std::vector< double > &pars) const override
 Evaluate the log-likelihood, as required by MINUT interface. More...
 
std::vector< double > Gradient (const std::vector< double > &pars) const override
 
bool CheckGradient () const override
 
TMatrixDSym GetCovariance () const
 
virtual double Up () const override
 Definition of one-sigma, required by MINUIT. More...
 
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

int GetMINUITPrecision () const
 
void DecodePars (const std::vector< double > &pars, osc::IOscCalcAdjustable *calc) const
 Stuff the parameters into the calculator and/or syst shifts object. More...
 
std::unique_ptr< IFitSummaryFitHelperSeeded (osc::IOscCalcAdjustable *seed, SystShifts &systSeed, Verbosity verb) const override
 Helper for FitHelper. More...
 
bool SupportsDerivatives () const
 
std::vector< SeedPtExpandSeeds (const SeedList &seedPts, const std::vector< SystShifts > &systSeedPts) const
 
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

osc::IOscCalcAdjustablefCalc
 
const IExperimentfExpt
 
FitOpts fFitOpts
 
bool fSupportsDerivatives
 
int fNEval = 0
 
int fNEvalGrad = 0
 
int fNEvalFiniteDiff = 0
 
TMatrixDSym fCovariance
 
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

Perform MINUIT fits in one or two dimensions.

Definition at line 17 of file MinuitFitter.h.

Member Enumeration Documentation

Enumerator
kNormal 

default if un-specified

kCareful 
kFast 
kGradDesc 
kPrecisionMask 
kPrefitSysts 

Definition at line 21 of file MinuitFitter.h.

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,
30  kPrecisionMask = 3,
31  // This option is independent, you may xor it in with one of the above
32  kPrefitSysts = 4
33  };
default if un-specified
Definition: MinuitFitter.h:25
enum ana::IFitter::Verbosity
inherited
Enumerator
kQuiet 
kVerbose 

Definition at line 22 of file IFitter.h.

Constructor & Destructor Documentation

ana::MinuitFitter::MinuitFitter ( const IExperiment expt,
std::vector< const IFitVar * >  vars,
std::vector< const ISyst * >  systs = {},
FitOpts  opts = kNormal 
)

Definition at line 64 of file MinuitFitter.cxx.

68  : IFitter(vars, systs),
69  fExpt(expt),
70  fFitOpts(opts),
72  {
73  }
std::vector< SystGroupDef > systs
Definition: syst_header.h:385
bool SupportsDerivatives() const
const IExperiment * fExpt
Definition: MinuitFitter.h:89
expt
Definition: demo5.py:34
const std::map< std::pair< std::string, std::string >, Variable > vars
IFitter(const std::vector< const IFitVar * > &vars, const std::vector< const ISyst * > &systs={})
Definition: IFitter.cxx:21
ana::MinuitFitter::~MinuitFitter ( )

Definition at line 76 of file MinuitFitter.cxx.

77  {
78  }

Member Function Documentation

bool ana::MinuitFitter::CheckGradient ( ) const
inlineoverride
void ana::MinuitFitter::DecodePars ( const std::vector< double > &  pars,
osc::IOscCalcAdjustable calc 
) const
protected

Stuff the parameters into the calculator and/or syst shifts object.

Definition at line 283 of file MinuitFitter.cxx.

References ana::assert(), ana::IFitter::fShifts, ana::IFitter::fSysts, ana::IFitter::fVars, MECModelEnuComparisons::i, calib::j, and febshutoff_auto::val.

Referenced by GetMINUITPrecision(), and operator()().

284  {
285  assert(pars.size() == fVars.size()+fSysts.size());
286 
287  if (fVars.size() > 0)
288  {
289  assert(calc);
290  for(unsigned int i = 0; i < fVars.size(); ++i){
291  auto val = pars[i];
292  fVars[i]->SetValue(calc, val);
293  }
294  }
295 
296  for(unsigned int j = 0; j < fSysts.size(); ++j){
297  auto val = pars[fVars.size()+j];
298  fShifts->SetShift(fSysts[j], val);
299  }
300  }
std::vector< const ISyst * > fSysts
Definition: IFitter.h:115
const double j
Definition: BetheBloch.cxx:29
std::vector< const IFitVar * > fVars
Definition: IFitter.h:114
std::string pars("Th23Dmsq32")
assert(nhit_max >=nhit_nbins)
std::unique_ptr< SystShifts > fShifts
Definition: IFitter.h:116
std::vector< IFitter::SeedPt > ana::IFitter::ExpandSeeds ( const SeedList seedPts,
const std::vector< SystShifts > &  systSeedPts 
) const
protectedinherited

Definition at line 46 of file IFitter.cxx.

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

Referenced by ana::IFitter::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
virtualinherited

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, ana::IFitter::FitHelper(), ana::IFitter::fSysts, ana::IFitter::fVars, ana::IFitter::fVerb, ana::SystShifts::GetShift(), ana::IFitter::kVerbose, registry_explorer::v, and ana::IFitter::ValidateSeeds().

Referenced by BlessedPlotsAna(), BlessedPlotsAnaByPeriod(), cc(), ana::CovMxSurface::CovMxSurface(), fake_future_data(), FCContour(), FCTutorial2020(), fill_col(), ana::FrequentistSurface::FillSurfacePoint(), ana::FrequentistSurface::FindMinimum(), ana::FindValley(), ana::IFitter::Fit(), ana::StanFitter::Fit(), 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
inlinevirtualinherited

Variant with no seedPts.

Definition at line 61 of file IFitter.h.

References ana::IFitter::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
inlinevirtualinherited

Variant with no seedPts and no systematics result returned.

Definition at line 69 of file IFitter.h.

References ana::IFitter::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
inlinevirtualinherited

Variant with no oscillations - useful for ND fits.

Definition at line 76 of file IFitter.h.

References ana::IFitter::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
protectedvirtualinherited

Helper for Fit.

Definition at line 117 of file IFitter.cxx.

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

Referenced by ana::IFitter::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
std::unique_ptr< IFitter::IFitSummary > ana::MinuitFitter::FitHelperSeeded ( osc::IOscCalcAdjustable seed,
SystShifts systSeed,
Verbosity  verb 
) const
overrideprotectedvirtual

Helper for FitHelper.

Implements ana::IFitter.

Definition at line 103 of file MinuitFitter.cxx.

References ana::assert(), om::cout, allTimeWatchdog::endl, fCalc, fCovariance, fExpt, fFitOpts, ana::IFitter::Fit(), ana::IFitter::fShifts, fSupportsDerivatives, ana::IFitter::fSysts, ana::IFitter::fVars, ana::IFitter::fVerb, runNovaSAM::gErrorIgnoreLevel, GetMINUITPrecision(), ana::SystShifts::GetShift(), MECModelEnuComparisons::i, calib::j, kGradDesc, kPrecisionMask, kPrefitSysts, elec2geo::pos, seed, ana::SystShifts::SetShift(), registry_explorer::v, and febshutoff_auto::val.

106  {
107  fVerb = verb;
108  if((fFitOpts & kPrefitSysts) && !fVars.empty()){
109  // Update systSeed to the best syst values, while holding the oscillation
110  // parameters fixed
111  MinuitFitter prefit(fExpt, {}, fSysts, fFitOpts);
112  prefit.Fit(seed, systSeed, verb);
113  // Then continue with the full fit as usual
114  }
115 
116  fCalc = seed;
117  *fShifts = systSeed;
118 
119  ROOT::Minuit2::MnUserParameters mnPars;
120 
121  for (const IFitVar *v: fVars)
122  {
123  const double val = v->GetValue(seed);
124  // name, value, error
125  mnPars.Add(v->ShortName(), val, val ? val / 2 : .1);
126  }
127  // One way this can go wrong is if two variables have the same ShortName
128  assert(mnPars.Params().size() == fVars.size());
129  for (const ISyst *s: fSysts)
130  {
131  const double val = systSeed.GetShift(s);
132  // name, value, error
133  mnPars.Add(s->ShortName(), val, 1);
134  }
135  // One way this can go wrong is if two variables have the same ShortName
136  assert(mnPars.Params().size() == fVars.size() + fSysts.size());
137 
138  ROOT::Minuit2::MnApplication *mnApp = 0;
139 
140  if ((fFitOpts & kPrecisionMask) == kGradDesc)
141  {
142  mnApp = new GradientDescent(*this, mnPars);
143  } else
144  {
146  {
147  mnApp = new ROOT::Minuit2::MnMigrad(*this, mnPars, GetMINUITPrecision());
148  } else
149  {
150  mnApp = new ROOT::Minuit2::MnMigrad(*((ROOT::Minuit2::FCNBase *) this), mnPars, GetMINUITPrecision());
151  }
152  }
153 
154  // Minuit2 doesn't give a good way to control verbosity...
155  const int olderr = gErrorIgnoreLevel;
156  if (fVerb <= Verbosity::kQuiet) gErrorIgnoreLevel = 1001; // Ignore warnings
157  auto minpt = std::make_unique<ROOT::Minuit2::FunctionMinimum>((*mnApp)());
158 
159  gErrorIgnoreLevel = olderr;
160 
161  if(minpt->IsValid()){
162  const std::vector<double> cov = minpt->UserCovariance().Data();
163  const unsigned int nPars = mnPars.Params().size();
164 
165  assert(cov.size() == (nPars*(nPars+1))/2);
166 
167  fCovariance.ResizeTo(nPars, nPars);
168  int pos = 0;
169  for(unsigned int iRow = 0; iRow < nPars; iRow++){
170  for(unsigned int iCol = 0; iCol < iRow+1; iCol++){
171  fCovariance(iRow,iCol) = fCovariance(iCol, iRow) = cov[pos];
172  pos++;
173  }
174  }
175  }
176  else{
177  fCovariance.ResizeTo(0, 0);
178  if(fVerb > Verbosity::kQuiet)
179  std::cout << "*** ERROR: minimum is not valid ***" << std::endl;
180  }
181 
182  const std::vector<double> minvec = minpt->UserParameters().Params();
183 
184  // Store results back to the "seed" variable
185  for (unsigned int i = 0; i < fVars.size(); ++i)
186  {
187  fVars[i]->SetValue(seed, minvec[i]);
188  }
189  // Store systematic results back into "systSeed"
190  for (unsigned int j = 0; j < fSysts.size(); ++j)
191  {
192  systSeed.SetShift(fSysts[j], minvec[fVars.size() + j]);
193  }
194 
195  delete mnApp;
196 
197  return std::make_unique<MinuitFitSummary>(std::move(minpt));
198  }
osc::IOscCalcAdjustable * fCalc
Definition: MinuitFitter.h:88
std::vector< const ISyst * > fSysts
Definition: IFitter.h:115
const IExperiment * fExpt
Definition: MinuitFitter.h:89
const XML_Char * s
Definition: expat.h:262
unsigned int seed
Definition: runWimpSim.h:102
int GetMINUITPrecision() const
Definition: MinuitFitter.h:58
const double j
Definition: BetheBloch.cxx:29
OStream cout
Definition: OStream.cxx:6
TMatrixDSym fCovariance
Definition: MinuitFitter.h:98
std::vector< const IFitVar * > fVars
Definition: IFitter.h:114
MinuitFitter(const IExperiment *expt, std::vector< const IFitVar * > vars, std::vector< const ISyst * > systs={}, FitOpts opts=kNormal)
assert(nhit_max >=nhit_nbins)
std::unique_ptr< SystShifts > fShifts
Definition: IFitter.h:116
Verbosity fVerb
Definition: IFitter.h:117
TMatrixDSym ana::MinuitFitter::GetCovariance ( ) const
inline

Definition at line 50 of file MinuitFitter.h.

References fCovariance.

50 {return fCovariance;}
TMatrixDSym fCovariance
Definition: MinuitFitter.h:98
int ana::MinuitFitter::GetMINUITPrecision ( ) const
inlineprotected

Convert from our enum order to MINUIT's codes (0=fast, 1=normal, 2=careful)

Definition at line 58 of file MinuitFitter.h.

References calc, DecodePars(), fFitOpts, kPrecisionMask, and pars().

Referenced by FitHelperSeeded().

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

Definition at line 82 of file IFitter.h.

References ana::IFitter::fShifts.

Referenced by plot_shifts().

82 {return fShifts->Copy();}
std::unique_ptr< SystShifts > fShifts
Definition: IFitter.h:116
std::vector< double > ana::MinuitFitter::Gradient ( const std::vector< double > &  pars) const
override

Definition at line 237 of file MinuitFitter.cxx.

238  {
239  abort();
240  /*
241  ++fNEvalGrad;
242 
243  std::vector<double> ret(pars.size());
244 
245  // TODO handling of FitVars including penalty terms
246 
247  if (!fVars.empty())
248  {
249  // Have to use finite differences to calculate these derivatives
250  const double dx = 1e-9;
251  const double nom = (*this)(pars);
252  ++fNEvalFiniteDiff;
253  std::vector<double> parsCopy = pars;
254  for (unsigned int i = 0; i < fVars.size(); ++i)
255  {
256  parsCopy[i] += dx;
257  ret[i] = ((*this)(parsCopy) - nom) / dx;
258  ++fNEvalFiniteDiff;
259  parsCopy[i] = pars[i];
260  }
261  }
262 
263  // Get systematic parts analytically
264 
265  DecodePars(pars, fCalc); // Updates fCalc and fShifts
266 
267  std::unordered_map<const ISyst *, double> dchi;
268  for (const ISyst *s: fSysts) dchi[s] = 0;
269  fExpt->Derivative(fCalc, *fShifts, dchi);
270 
271  for (unsigned int i = 0; i < fSysts.size(); ++i)
272  {
273  // Include the derivative of the penalty terms too. TODO this is only
274  // right for quadratic penalties (ie all the currently existing ones)
275  ret[fVars.size() + i] = dchi[fSysts[i]] + 2 * fShifts->GetShift(fSysts[i]);
276  }
277 
278  return ret;
279  */
280  }
double ana::MinuitFitter::operator() ( const std::vector< double > &  pars) const
overridevirtual

Evaluate the log-likelihood, as required by MINUT interface.

Definition at line 217 of file MinuitFitter.cxx.

References ana::assert(), ana::IExperiment::ChiSq(), DecodePars(), fCalc, fExpt, fNEval, ana::IFitter::fShifts, ana::IFitter::fSysts, ana::IFitter::fVars, and MECModelEnuComparisons::i.

218  {
219  ++fNEval;
220 
221  assert(pars.size() == fVars.size() + fSysts.size());
222 
223  DecodePars(pars, fCalc); // Updates fCalc and fShifts
224 
225  // Have to re-fetch the FitVar values because DecodePars() will have
226  // truncated values to the physical range where necessary.
227  double penalty = 0;
228  for (unsigned int i = 0; i < fVars.size(); ++i)
229  {
230  penalty += fVars[i]->Penalty(pars[i], fCalc);
231  }
232 
233  return fExpt->ChiSq(fCalc, *fShifts) + penalty + fShifts->Penalty();
234  }
virtual double ChiSq(osc::IOscCalcAdjustable *osc, const SystShifts &syst=SystShifts::Nominal()) const
Definition: IExperiment.h:18
osc::IOscCalcAdjustable * fCalc
Definition: MinuitFitter.h:88
std::vector< const ISyst * > fSysts
Definition: IFitter.h:115
const IExperiment * fExpt
Definition: MinuitFitter.h:89
void DecodePars(const std::vector< double > &pars, osc::IOscCalcAdjustable *calc) const
Stuff the parameters into the calculator and/or syst shifts object.
std::vector< const IFitVar * > fVars
Definition: IFitter.h:114
std::string pars("Th23Dmsq32")
assert(nhit_max >=nhit_nbins)
std::unique_ptr< SystShifts > fShifts
Definition: IFitter.h:116
void ana::MinuitFitter::SetFitOpts ( FitOpts  opts)

Definition at line 201 of file MinuitFitter.cxx.

References om::cout, allTimeWatchdog::endl, fFitOpts, fSupportsDerivatives, ana::IFitter::fSysts, kGradDesc, kPrecisionMask, and plot_validation_datamc::opts.

Referenced by ana::FrequentistSurface::FillSurfacePoint(), and ana::FrequentistSurface::FindMinimum().

202  {
203  if ((opts & kPrecisionMask) == kGradDesc &&
204  !fSysts.empty() &&
206  {
207  std::cout
208  << "Warning - not setting precision to kGradDesc, since analytic gradients are not supported by this experiment"
209  << std::endl;
210  return;
211  }
212 
213  fFitOpts = opts;
214  }
std::vector< const ISyst * > fSysts
Definition: IFitter.h:115
OStream cout
Definition: OStream.cxx:6
bool ana::MinuitFitter::SupportsDerivatives ( ) const
protected

Intended to be called only once (from constructor) to initialize fSupportsDerivatives

Definition at line 81 of file MinuitFitter.cxx.

82  {
83  return false;
84  /*
85  // Make completely opt-in for now
86  if (getenv("CAFANA_ANALYTIC_DERIVATIVES") == 0) return false;
87 
88  // No point using derivatives for FitVars only, we do finite differences,
89  // probably worse than MINUIT would.
90  if (fSysts.empty()) return false;
91 
92  // Otherwise, do the minimal trial to see if the experiment will return a
93  // gradient.
94  std::unordered_map<const ISyst *, double> dchi = {{fSysts[0], 0}};
95  osc::NoOscillations calc;
96  fExpt->Derivative(&calc, SystShifts::Nominal(), dchi);
97  return !dchi.empty();
98  */
99  }
virtual double ana::MinuitFitter::Up ( ) const
inlineoverridevirtual

Definition of one-sigma, required by MINUIT.

Definition at line 53 of file MinuitFitter.h.

53 { return 1; }
void ana::IFitter::ValidateSeeds ( osc::IOscCalcAdjustable seed,
const SeedList seedPts,
const std::vector< SystShifts > &  systSeedPts 
) const
protectedinherited

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, ana::IFitter::fSysts, ana::IFitter::fVars, gen_flatrecord::pt, and registry_explorer::v.

Referenced by ana::IFitter::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

osc::IOscCalcAdjustable* ana::MinuitFitter::fCalc
mutableprotected

Definition at line 88 of file MinuitFitter.h.

Referenced by FitHelperSeeded(), and operator()().

TMatrixDSym ana::MinuitFitter::fCovariance
mutableprotected

Definition at line 98 of file MinuitFitter.h.

Referenced by FitHelperSeeded(), and GetCovariance().

const IExperiment* ana::MinuitFitter::fExpt
protected

Definition at line 89 of file MinuitFitter.h.

Referenced by FitHelperSeeded(), and operator()().

FitOpts ana::MinuitFitter::fFitOpts
protected

Definition at line 91 of file MinuitFitter.h.

Referenced by CheckGradient(), FitHelperSeeded(), GetMINUITPrecision(), and SetFitOpts().

int ana::MinuitFitter::fNEval = 0
mutableprotected

Definition at line 95 of file MinuitFitter.h.

Referenced by operator()().

int ana::MinuitFitter::fNEvalFiniteDiff = 0
mutableprotected

Definition at line 97 of file MinuitFitter.h.

int ana::MinuitFitter::fNEvalGrad = 0
mutableprotected

Definition at line 96 of file MinuitFitter.h.

std::unique_ptr<SystShifts> ana::IFitter::fShifts
mutableprotectedinherited
bool ana::MinuitFitter::fSupportsDerivatives
protected

Definition at line 93 of file MinuitFitter.h.

Referenced by FitHelperSeeded(), and SetFitOpts().

std::vector<const ISyst*> ana::IFitter::fSysts
protectedinherited
std::vector<const IFitVar*> ana::IFitter::fVars
protectedinherited
Verbosity ana::IFitter::fVerb
mutableprotectedinherited

Definition at line 117 of file IFitter.h.

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

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

Definition at line 19 of file IFitter.h.


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