forked from GooFit/GooFit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FitManagerMinuit1.cc
130 lines (114 loc) · 3.85 KB
/
FitManagerMinuit1.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
PdfBase* pdfPointer;
FitManager* currGlue = 0;
int numPars = 0;
vector<Variable*> vars;
void specialTddpPrint (double fun);
FitManager::FitManager (PdfBase* dat)
: minuit(0)
, overrideCallLimit(-1)
{
pdfPointer = dat;
currGlue = this;
}
FitManager::~FitManager () {
if (minuit) delete minuit;
}
void FitManager::setupMinuit () {
vars.clear();
pdfPointer->getParameters(vars);
numPars = vars.size();
if (minuit) delete minuit;
minuit = new TMinuit(numPars);
int maxIndex = 0;
int counter = 0;
for (std::vector<Variable*>::iterator i = vars.begin(); i != vars.end(); ++i) {
minuit->DefineParameter(counter, (*i)->name.c_str(), (*i)->value, (*i)->error, (*i)->lowerlimit, (*i)->upperlimit);
if ((*i)->fixed) minuit->FixParameter(counter);
counter++;
if (maxIndex < (*i)->getIndex()) maxIndex = (*i)->getIndex();
}
numPars = maxIndex+1;
pdfPointer->copyParams();
minuit->SetFCN(FitFun);
}
void FitManager::fit () {
setupMinuit();
runMigrad();
}
void FitManager::runMigrad () {
assert(minuit);
host_callnumber = 0;
if (0 < overrideCallLimit) {
std::cout << "Calling MIGRAD with call limit " << overrideCallLimit << std::endl;
double plist[1];
plist[0] = overrideCallLimit;
int err = 0;
minuit->mnexcm("MIGRAD", plist, 1, err);
}
else minuit->Migrad();
}
void FitManager::getMinuitValues () const {
int counter = 0;
for (std::vector<Variable*>::iterator i = vars.begin(); i != vars.end(); ++i) {
minuit->GetParameter(counter++, (*i)->value, (*i)->error);
}
}
void FitFun(int &npar, double *gin, double &fun, double *fp, int iflag) {
vector<double> pars;
// Notice that npar is number of variable parameters, not total.
pars.resize(numPars);
int counter = 0;
for (std::vector<Variable*>::iterator i = vars.begin(); i != vars.end(); ++i) {
if (isnan(fp[counter])) cout << "Variable " << (*i)->name << " " << (*i)->index << " is NaN\n";
pars[(*i)->getIndex()] = fp[counter++] + (*i)->blind;
}
pdfPointer->copyParams(pars);
fun = pdfPointer->calculateNLL();
host_callnumber++;
#ifdef PRINTCALLS
specialTddpPrint(fun);
#endif
}
#ifdef PRINTCALLS
void specialTddpPrint (double fun) {
// Stupid amplitude-fit debugging method.
cout << "Function call " << host_callnumber << ": " << fun << "\n";
currGlue->getMinuitValues();
int varCount = 1;
for (std::vector<Variable*>::iterator v = vars.begin(); v != vars.end(); ++v) {
if (!(*v)) cout << "Null!" << endl;
if ((*v)->fixed) continue;
const fptype _mD0 = 1.86484;
const fptype _mD02 = _mD0 *_mD0;
const fptype _mD02inv = 1./_mD02;
double stupidSpecialModifier = 1; // Mikhail interprets some of the weights differently.
if (((*v)->name == "f0_980_amp_real") ||
((*v)->name == "f0_980_amp_imag") ||
((*v)->name == "f0_1370_amp_real") ||
((*v)->name == "f0_1370_amp_imag") ||
((*v)->name == "f0_1500_amp_real") ||
((*v)->name == "f0_1500_amp_imag") ||
((*v)->name == "f0_1710_amp_real") ||
((*v)->name == "f0_1710_amp_imag") ||
((*v)->name == "f0_600_amp_real") ||
((*v)->name == "f0_600_amp_imag")) stupidSpecialModifier = -_mD02;
else if (((*v)->name == "f2_1270_amp_real") ||
((*v)->name == "f2_1270_amp_imag")) stupidSpecialModifier = -_mD02inv;
else if (((*v)->name == "nonr_amp_real") ||
((*v)->name == "nonr_amp_imag")) stupidSpecialModifier = -1;
cout.width(20);
cout << (*v)->name;
cout.setf(ios_base::right,ios_base::adjustfield);
cout.width(3);
cout << varCount++;
cout.setf(ios_base::right,ios_base::adjustfield); cout.precision(8);
cout << " "; cout.width(12);
cout << (*v)->value / stupidSpecialModifier;
cout.setf(ios_base::right,ios_base::adjustfield); cout.precision(8);
cout << " "; cout.width(12);
cout << (*v)->error;
cout << endl;
}
cout << endl;
}
#endif