GooFit  v2.1.3
FitManagerMinuit1.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <goofit/Log.h>
3 #include <goofit/PDFs/GooPdf.h>
4 #include <goofit/PdfBase.h>
5 #include <goofit/Variable.h>
7 #include <limits>
8 #include <set>
9 #include <typeinfo>
10 
11 namespace GooFit {
12 
14  : TMinuit(max_index(pdfPointer->getParameters()) + 1)
15  , pdfPointer(pdfPointer)
16  , vars(pdfPointer->getParameters()) {
17  int counter = 0;
18 
19  for(Variable &var : vars) {
20  var.setFitterIndex(counter);
21 
22  Int_t err = DefineParameter(
23  counter, var.getName().c_str(), var.getValue(), var.getError(), var.getLowerLimit(), var.getUpperLimit());
24 
25  if(GetNumPars() != counter + 1)
27  "Error when implementing param {} (possibly invalid error/lowerlimit/upperlimit values)!",
28  var.getName());
29 
30  if(err != 0)
31  throw GooFit::GeneralError("Was not able to implement param {} (error {})", var.getName(), err);
32 
33  if(var.IsFixed())
34  FixParameter(counter);
35 
36  counter++;
37  }
38 
39  pdfPointer->copyParams();
40 }
41 
42 Int_t Minuit1::Eval(int npar, double *gin, double &fun, double *fp, int iflag) {
43  std::vector<double> pars{fp, fp + GetNumPars()};
44 
45  std::vector<double> gooPars;
46  gooPars.resize(max_index(vars) + 1);
47 
48  for(Variable &var : vars) {
49  if(std::isnan(pars.at(var.getFitterIndex())))
50  GOOFIT_WARN("Variable {} at {} is NaN", var.getName(), var.getIndex());
51 
52  var.setChanged(var.getValue() != pars.at(var.getFitterIndex()));
53  var.setValue(pars.at(var.getFitterIndex()));
54  gooPars.at(var.getIndex()) = var.getValue() - var.getBlind(Variable::Key());
55  }
56 
57  pdfPointer->copyParams(gooPars);
58 
59  GOOFIT_TRACE("Calculating NLL");
60  fun = pdfPointer->calculateNLL();
62  return 0;
63 }
64 
66  host_callnumber = 0;
67 
68  for(Variable &var : minuit_.getVaraibles())
69  var.setChanged(true);
70 
71  std::cout << GooFit::gray << GooFit::bold;
72 
73  if(0 < overrideCallLimit) {
74  std::cout << "Calling MIGRAD with call limit " << overrideCallLimit << std::endl;
75  double plist[1] = {overrideCallLimit};
76  int err = 0;
77 
78  if(_useHesseBefore)
79  minuit_.mnexcm("HESSE", plist, 1, err);
80 
81  minuit_.mnexcm("MIGRAD", plist, 1, err);
82 
83  if(_useHesse)
84  minuit_.mnexcm("HESSE", plist, 1, err);
85 
86  if(_useMinos)
87  minuit_.mnexcm("MINOS", plist, 1, err);
88 
89  if(_useImprove)
90  minuit_.mnexcm("IMPROVE", plist, 1, err);
91  } else
92  minuit_.Migrad();
93 
94  std::cout << GooFit::reset;
95 
96  double tmp_value, tmp_error;
97  for(Variable &var : minuit_.getVaraibles()) {
98  minuit_.GetParameter(var.getFitterIndex(), tmp_value, tmp_error);
99  var.setValue(tmp_value);
100  var.setError(tmp_error);
101  }
102 }
103 
105  double &fmin, double &fedm, double &errdef, int &npari, int &nparx, int &istat) {
106  minuit_.mnstat(fmin, fedm, errdef, npari, nparx, istat);
107  std::cout << "mnstat(fmin = " << fmin << ", fedm = " << fedm << ", errdef = " << errdef << ", npari = " << npari
108  << ", nparx = " << nparx << ", istat = " << istat << ")" << std::endl;
109 }
110 } // namespace GooFit
Thrown when a general error is encountered.
Definition: Error.h:10
virtual __host__ double calculateNLL() const =0
Int_t Eval(Int_t npar, Double_t *grad, Double_t &fval, Double_t *par, Int_t flag) override
Fit function for Minuit.
#define GOOFIT_TRACE(...)
Definition: Log.h:126
Minuit1(PdfBase *pdfPointer)
void getMinuitStatus(double &fmin, double &fedm, double &errdef, int &npari, int &nparx, int &istat)
constexpr rang::style const bold
Definition: Color.h:17
int max_index(const std::vector< Variable > &vars)
Get the max index of a variable from a list.
Definition: Variable.cpp:8
constexpr rang::style const reset
Definition: Color.h:16
constexpr rang::fg const gray
Definition: Color.h:15
#define GOOFIT_WARN(...)
Definition: Log.h:46
__host__ void copyParams(const std::vector< double > &pars) const
int host_callnumber
Definition: PdfBase.cpp:33
This provides a key for some special classes to access blind info (passkey)
Definition: Variable.h:154