forked from amissert/atmFitTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovBase.cxx
236 lines (208 loc) · 5.6 KB
/
covBase.cxx
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <math.h>
#include "TDecompChol.h"
#include "ThrowParms.h"
#include "covBase.h"
covBase::covBase() {}
covBase::covBase(std::string name, std::string file)
{
rnd = new TRandom3(1);
init(name, file);
}
covBase::covBase(std::string name, std::string file, unsigned int seed)
{
rnd = new TRandom3(seed);
init(name, file);
}
covBase::~covBase()
{
delete fParName;
delete fParInit;
delete fParSigma;
delete fParCurr;
delete fParProp;
delete fParUp;
delete fParLow;
delete fParEvalLikelihood;
delete fStepScale;
delete[] fPropKernel;
}
void covBase::init(std::string name, std::string file)
{
TFile infile(file.c_str(), "read");
TMatrixDSym *covMatrix = (TMatrixDSym*)(infile.Get(name.c_str()));
cov = (TMatrixDSym*)covMatrix->Clone(name.c_str());
invCov = (TMatrixDSym*)covMatrix->Clone("invCov");
invCov->Invert();
mName = name;
size = cov->GetNrows();
size_xsec = size;
fParName = new std::string[size];
fParInit = new double[size];
fParSigma = new double[size];
fParCurr = new double[size];
fParProp = new double[size];
fParUp = new double[size];
fParLow = new double[size];
fParEvalLikelihood = new bool[size];
fPropKernel = new TF1*[size];
fStepScale = new float[size];
for (int i = 0; i < size; ++i) {
nominal.push_back(1.);
fParEvalLikelihood[i] = true;
fStepScale[i] = 1.;
}
randPar.ResizeTo(size);
}
void covBase::throwNominal(bool nomValues, unsigned int seed)
{
TDecompChol chdcmp(*cov);
if (!chdcmp.Decompose()) {
std::cerr << "Cholesky decomposition failed for " << mName << std::endl;
exit(-1);
}
chel = new TMatrixD(chdcmp.GetU());
CholeskyDecomp(size, *chel);
TVectorD vec(size);
for (int i = 0; i < size; ++i) vec(i) = 1.;
ThrowParms *nom_throws = new ThrowParms(vec, (*cov));
nom_throws->SetSeed(seed);
nominal.clear();
nominal.resize(size);
if (!nomValues) {
bool throw_again = true;
while (throw_again) {
throw_again = false;
nom_throws->ThrowSet(nominal);
for (int i = 0; i < size; ++i) {
if (fParSigma[i] < 0) { // if parameter is fixed, do not throw
nominal.at(i) = 1.0;
continue;
}
if (nominal.at(i) < 0) {
nominal.at(i) = 0;
throw_again = true;
}
}
}
}
delete nom_throws;
}
void covBase::CholeskyDecomp(int npars, TMatrixD &chel_mat)
{
for(int i=0; i<npars; i++)
for(int j=0; j<npars; j++){
//if diagonal element
if(i==j){
chel_mat(i,i) = (*cov)(i,i);
for(int k=0; k<=i-1; k++) chel_mat(i,i) = chel_mat(i,i)-pow(chel_mat(i,k),2);
chel_mat(i,i) = sqrt(chel_mat(i,i));
//if lower half
} else if(j<i) {
chel_mat(i,j) = (*cov)(i,j);
for(int k=0; k<=j-1; k++) chel_mat(i,j) = chel_mat(i,j)-chel_mat(i,k)*chel_mat(j,k);
chel_mat(i,j) = chel_mat(i,j)/chel_mat(j,j);
} else chel_mat(i,j) = 0.;
}
}
void covBase::genPropKernels()
{
for (int i = 0; i < size; ++i) {
fPropKernel[i] = NULL;
if (fParSigma[i] > 0) {
double e = sqrt((*cov)(i,i));
fPropKernel[i] = new TF1(Form("%s_%03d", mName.c_str(), i), "gaus", -e*10., e*10.);
fPropKernel[i]->SetParameters(1./(e*sqrt(2*3.14159265)), 0., e);
}
}
}
void covBase::setPropFunc(int i, TF1 *func)
{
if (i >= size) {
std::cerr << "Parameter index out of range." << std::endl;
exit(-1);
}
fPropKernel[i] = func;
fParSigma[i] = func->GetParameter(1);
}
void covBase::proposeStep()
{
randomize();
correlateSteps();
}
void covBase::randomize()
{
for (int i = 0; i < size; ++i) {
if (fParSigma[i] > 0) {
//randPar(i) = rnd->Gaus(0,1);
randPar(i) = fPropKernel[i]->GetRandom();
} else {
randPar(i) = 0;
}
}
}
double covBase::getLikelihood()
{
double LogL = 0;
for (int i = 0; i < size; ++i) {
if (fParProp[i] < 0) return 99999.9;
for (int j = 0; j < size; ++j) {
if (fParEvalLikelihood[i] && fParEvalLikelihood[j]) LogL += (fParProp[i] - nominal[i]) * (fParProp[j] - nominal[j]) * (*invCov)(i,j) * 0.5;
}
}
return LogL;
}
void covBase::correlateSteps()
{
TVectorD corr_throw = (*chel)*randPar;
for (int i = 0; i < size; ++i) {
if (fParSigma[i] > 0) fParProp[i] = fParCurr[i] + corr_throw(i)*fStepScale[i];
}
}
void covBase::acceptStep()
{
for (int i = 0; i < size; ++i) fParCurr[i] = fParProp[i];
}
std::vector<double> covBase::getNominalArray()
{
return nominal;
}
std::vector<double> covBase::getProposedArray()
{
std::vector<double> prop;
for (int i = 0; i < size; ++i) prop.push_back(fParProp[i]);
return prop;
}
std::vector<double> covBase::getCurrentArray()
{
std::vector<double> curr;
for (int i = 0; i < size; ++i) curr.push_back(fParCurr[i]);
return curr;
}
void covBase::setPars(std::vector<double> pars)
{
if (pars.size() != (unsigned int)size) {
std::cerr << "Error with covBase::setPars(std::vector<double> pars): pars.size() != size."<<std::endl;
exit(-1);
}
for (int i = 0; i < size; ++i) fParCurr[i] = pars.at(i);
}
void covBase::setStepScales(float scale)
{
for (int i = 0; i < size; ++i) fStepScale[i] = scale;
}
void covBase::PrintNominal()
{
std::cout<<"----------- Nominal ------------\n";
for (int i = 0; i < size; ++i) {
std::cout << i << " " << fParName[i] << " = " << nominal.at(i) << ", sigma = " << fParSigma[i] << ", low = " << fParLow[i] << ", up = " << fParUp[i] << "\n";
}
std::cout<<"--------------------------------"<<std::endl;
}
void covBase::PrintPars()
{
std::cout<<"----------- Current ------------\n";
for (int i = 0; i < size; ++i) {
std::cout << i << " " << fParName[i] << " = " << fParCurr[i] << "\n";
}
std::cout<<"--------------------------------"<<std::endl;
}