BAT  0.9.4
The Bayesian analysis toolkit
 All Classes Namespaces Functions Variables Enumerations
BCMVCombination.cxx
1 /*
2  * Copyright (C) 2007-2014, the BAT core developer team
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  * For documentation see http://mpp.mpg.de/bat
7  */
8 
9 // ---------------------------------------------------------
10 
11 #include "BCMVCombination.h"
12 
13 #include "BCMVCMeasurement.h"
14 #include "BCMVCObservable.h"
15 #include "BCMVCUncertainty.h"
16 
17 #include "../../BAT/BCLog.h"
18 #include "../../BAT/BCMath.h"
19 #include "../../BAT/BCParameter.h"
20 
21 #include <fstream>
22 #include <iomanip>
23 #include <iostream>
24 
25 // ---------------------------------------------------------
26 BCMVCombination::BCMVCombination()
27  : BCModel("BCMVCombination")
28  , fDetCovariance(0)
29  , fNObservables(0)
30  , fNNuisanceCorrelation(0)
31 {
32 }
33 
34 // ---------------------------------------------------------
35 BCMVCombination::~BCMVCombination()
36 {
37  int nuncertainties = GetNUncertainties();
38  int nmeasurements = GetNMeasurements();
39 
40  for (int i = 0; i < nuncertainties; ++i) {
41  BCMVCUncertainty* u = GetUncertainty(i);
42  delete u;
43  }
44  fUncertainties.clear();
45 
46  for (int i = 0; i < nmeasurements; ++i) {
47  BCMVCMeasurement* m = GetMeasurement(i);
48  delete m;
49  }
50  fMeasurements.clear();
51 }
52 
53 // ---------------------------------------------------------
54 void BCMVCombination::AddObservable(std::string name, double min, double max)
55 {
56  // check if observable exists already
57  int index = GetIndexObservable(name);
58 
59  if (index >= 0)
60  return;
61 
62  BCMVCObservable* obs = new BCMVCObservable();
63  obs->SetName(name);
64  obs->SetMinMax(min, max);
65  fObservables.push_back(obs);
66 
67  fNObservables++;
68 
69  AddParameter(name.c_str(), min, max);
70 
71  SetPriorConstant(name.c_str());
72 }
73 
74 // ---------------------------------------------------------
75 void BCMVCombination::AddUncertainty(std::string name)
76 {
77  BCMVCUncertainty* u = new BCMVCUncertainty(name);
78 
79  fUncertainties.push_back(u);
80 }
81 
82 // ---------------------------------------------------------
83 void BCMVCombination::AddMeasurement(std::string name, std::string observable, double value, std::vector<double> uncertainties)
84 {
85  // get index of the corresponding observable
86  int index = GetIndexObservable(observable);
87 
88  // check if observable exists
89  if (index < 0) {
90  BCLog::OutWarning(Form("BCMVCombination::AddMeasurement. Observable \"%s\" does not exist. Measurement was not added.", observable.c_str()));
91  return;
92  }
93 
94  BCMVCMeasurement* m = new BCMVCMeasurement(name);
95  m->SetObservable(index);
96  m->SetCentralValue(value);
97  m->SetUncertainties(uncertainties);
98 
99  fMeasurements.push_back(m);
100 
101  fVectorObservable.push_back(index);
102 
103  int n = GetNMeasurements();
104  fVectorMeasurements.ResizeTo(n);
105  fVectorMeasurements[n-1]=value;
106 }
107 
108 // ---------------------------------------------------------
109 double BCMVCombination::LogLikelihood(const std::vector<double> &parameters)
110 {
111  double logprob = 0.;
112 
113  if (fNNuisanceCorrelation > 0) {
114  CalculateCovarianceMatrix(parameters);
115  if (!PositiveDefinite())
116  return -1e90;
117  }
118 
119  int nmeas = GetNActiveMeasurements();
120 
121  // copy parameters into a vector
122  TVectorD observables(nmeas);
123 
124  for (int i = 0; i < nmeas; ++i) {
125  observables[i] = parameters[fVectorActiveObservable[i]];
126  }
127 
128  TVectorD prod1 = observables - fVectorActiveMeasurements;
129  TVectorD prod2 = fInvCovarianceMatrix * prod1;
130  double prod = prod1 * prod2;
131 
132  logprob = -0.5 * prod - log(TMath::Power(2*TMath::Pi(), nmeas/2.) * sqrt(fabs(fDetCovariance)));
133 
134  return logprob;
135 }
136 
137 // ---------------------------------------------------------
138 int BCMVCombination::ReadInput(std::string filename)
139 {
140  // open input file
141  std::ifstream infile;
142  infile.open(filename.c_str(), std::ifstream::in);
143 
144  // check if file is open
145  if (!infile.is_open()) {
146  BCLog::OutWarning(Form("BCMVCombination::ReadInput. Could not open input file %s.", filename.c_str()));
147  return 0;
148  }
149 
150  int nobservables;
151  int nmeasurements;
152  int nuncertainties;
153  int nnuisance;
154 
155  infile >> nobservables >> nmeasurements >> nuncertainties >> nnuisance;
156 
157  std::vector<std::string> observable_names;
158 
159  for (int i = 0; i < nobservables; ++i) {
160  std::string name;
161  double min;
162  double max;
163  infile >> name >> min >> max;
164 
165  // add observable
166  AddObservable(name.c_str(), min, max);
167  }
168 
169  for (int i = 0; i < nuncertainties; ++i) {
170  std::string name;
171  infile >> name;
172 
173  // add uncertainty
174  AddUncertainty(name);
175  }
176 
177  for (int i = 0; i < nmeasurements; ++i) {
178  std::string name;
179  std::string observable;
180  double central;
181  std::vector<double> uncertainties(0);
182 
183  infile >> name;
184  infile >> observable;
185  infile >> central;
186 
187  for (int j = 0; j < nuncertainties; ++j) {
188  double uncertainty;
189  infile >> uncertainty;
190  uncertainties.push_back(uncertainty);
191  }
192 
193  // add measurement
194  AddMeasurement(name, observable, central, uncertainties);
195  }
196 
197  for (int i = 0; i < nuncertainties; ++i) {
198  TMatrixD mat(nmeasurements, nmeasurements);
199 
200  for (int j = 0; j < nmeasurements; ++j)
201  for (int k = 0; k < nmeasurements; ++k) {
202  double corr;
203  infile >> corr;
204  mat[j][k] = corr;
205  }
206 
207  // set correlation matrix
208  GetUncertainty(i)->SetCorrelationMatrix(mat);
209  }
210 
211  for (int i = 0; i < nnuisance; ++i) {
212  std::string uncertainty;
213  std::string measurement1;
214  std::string observable1;
215  std::string measurement2;
216  std::string observable2;
217  std::string parname;
218  double min;
219  double max;
220  double pre;
221  std::string priorshape;
222 
223  infile >> uncertainty >> measurement1 >> observable1 >> measurement2 >> observable2 >> parname;
224 
225  // check if parameter exists already
226  int index = -1;
227 
228  for (unsigned int i = 0; i < GetNParameters(); i++)
229  if (parname.c_str() == GetParameter(i)->GetName())
230  index = i;
231 
232  if (index >= 0)
233  infile >> pre;
234 
235  else {
236  // read properties of parameter
237  infile >> min >> max >> priorshape;
238 
239  // add nuisance parameter
240  AddParameter(parname.c_str(), min, max);
241 
242  // set pre-factor
243  pre = 1;
244 
245  // set index
246  index = GetNParameters()-1;
247 
248  if (priorshape == "flat") {
249  SetPriorConstant(parname.c_str());
250  }
251  else if (priorshape == "gauss") {
252  double mean;
253  double std;
254 
255  infile >> mean >> std;
256 
257  SetPriorGauss(parname.c_str(), mean, std);
258  }
259  else {
260  BCLog::OutWarning(Form("BCMVCombination::ReadInput. Unknown prior shape %s.", priorshape.c_str()));
261  }
262  }
263 
264  // increase counter of nuisance parametera
265  fNNuisanceCorrelation++;
266 
267  NuisanceParameter p;
268  p.index_uncertainty = GetIndexUncertainty(uncertainty);
269  p.index_measurement1 = GetIndexMeasurement(measurement1, observable1);
270  p.index_measurement2 = GetIndexMeasurement(measurement2, observable2);
271  p.index_rhoparameter = index;
272  p.pre = pre;
273 
274  fNuisanceCorrelation.push_back(p);
275  }
276 
277  // close input file
278  infile.close();
279 
280  PrepareAnalysis();
281 
282  // no error
283  return 1;
284 }
285 
286 // ---------------------------------------------------------
287 void BCMVCombination::PrepareAnalysis()
288 {
289  int nuncertainties = GetNUncertainties();
290 
291  // prepare analysis
292  for (int i = 0; i < nuncertainties; ++i)
293  CalculateCorrelationMatrix(i);
294 
295  CalculateCovarianceMatrix();
296 
297  if (!PositiveDefinite()) {
298  BCLog::OutWarning("BCMVCombination::PrepareAnalysis. Covariance matrix is not positive definite.");
299  }
300 
301  CalculateHelperVectors();
302 
303 }
304 
305 // ---------------------------------------------------------
306 int BCMVCombination::GetNActiveMeasurements()
307 {
308  int n = GetNMeasurements();
309 
310  int counter = 0;
311 
312  for (int i = 0; i < n; ++i) {
313  BCMVCMeasurement* m = GetMeasurement(i);
314  if (m->GetFlagActive())
315  counter++;
316  }
317  return counter;
318 }
319 
320 // ---------------------------------------------------------
321 void BCMVCombination::CalculateHelperVectors()
322 {
323  int nmeasurements = GetNMeasurements();
324 
325  fVectorMeasurements.Clear();
326  fVectorMeasurements.ResizeTo(nmeasurements);
327  fVectorObservable.clear();
328 
329  for (int i = 0; i < nmeasurements; ++i) {
330  BCMVCMeasurement* m = GetMeasurement(i);
331  fVectorMeasurements[i] = m->GetCentralValue();
332  fVectorObservable.push_back(m->GetObservable());
333  }
334 
335  int nactive = GetNActiveMeasurements();
336 
337  fVectorActiveMeasurements.Clear();
338  fVectorActiveMeasurements.ResizeTo(nactive);
339  fVectorActiveObservable.clear();
340 
341  int counter = 0;
342  for (int i = 0; i < nmeasurements; ++i) {
343  BCMVCMeasurement* m = GetMeasurement(i);
344  if (m->GetFlagActive()) {
345  fVectorActiveMeasurements[counter] = m->GetCentralValue();
346  fVectorActiveObservable.push_back(m->GetObservable());
347  counter++;
348  }
349  }
350 
351 }
352 
353 // ---------------------------------------------------------
354 void BCMVCombination::CalculateCorrelationMatrix(int index)
355 {
356  BCMVCUncertainty* u = fUncertainties.at(index);
357 
358  int n = GetNMeasurements();
359  int nactive = GetNActiveMeasurements();
360 
361  TMatrixD cov(nactive, nactive);
362 
363  TMatrixD corr = u->GetCorrelationMatrix();
364 
365  int counteri = 0;
366  for (int i = 0; i < n; ++i) {
367  BCMVCMeasurement* mi = GetMeasurement(i);
368  double sigma_i = mi->GetUncertainty(index);
369 
370  // skip line if not active
371  if (!mi->GetFlagActive())
372  continue;
373 
374  int counterj = 0;
375  for (int j = 0; j < n; ++j) {
376  BCMVCMeasurement* mj = GetMeasurement(j);
377  double sigma_j = mj->GetUncertainty(index);
378 
379  if (mj->GetFlagActive()) {
380  cov[counteri][counterj] = corr[i][j]*sigma_i*sigma_j;
381  counterj++;
382  }
383  }
384  counteri++;
385  }
386 
387  u->SetCovarianceMatrix(cov);
388 }
389 
390 // ---------------------------------------------------------
391 void BCMVCombination::CalculateCovarianceMatrix(std::vector<double> parameters)
392 {
393  int n = GetNUncertainties();
394  int nmeasurements = GetNMeasurements();
395  int nnuisance = fNNuisanceCorrelation;
396 
397  fCovarianceMatrix.Clear();
398  fCovarianceMatrix.ResizeTo(GetNActiveMeasurements(), GetNActiveMeasurements());
399 
400  // loop over all uncertainties
401  for (int unc_i = 0; unc_i < n; ++unc_i) {
402  BCMVCUncertainty* u = fUncertainties.at(unc_i);
403 
404  // shrink covariance matrix such that it fits only active measurements
405  TMatrixD mat_small = u->GetCovarianceMatrix();
406 
407  // loop over all measurements (i)
408  int counteri = 0;
409  for (int meas_i = 0; meas_i < nmeasurements; ++meas_i) {
410  BCMVCMeasurement* mi = GetMeasurement(meas_i);
411 
412  // skip line if not active
413  if (!mi->GetFlagActive())
414  continue;
415 
416  // loop over all measurements (j)
417  int counterj = 0;
418  for (int meas_j = 0; meas_j < nmeasurements; ++meas_j) {
419  BCMVCMeasurement* mj = GetMeasurement(meas_j);
420 
421  // skip line if not active
422  if (!mj->GetFlagActive())
423  continue;
424 
425  // modify matrix if nuisance parameter present
426  if (parameters.size() > 0) {
427 
428  // loop over all nuisance parameters
429  for (int nuis_k = 0; nuis_k < nnuisance; ++nuis_k) {
430 
431  // get nuisance parameter
432  NuisanceParameter p = fNuisanceCorrelation.at(nuis_k);
433 
434  // compare
435  if (p.index_uncertainty == unc_i) {
436  double sigma_i = GetMeasurement(p.index_measurement1)->GetUncertainty(unc_i);
437  double sigma_j = GetMeasurement(p.index_measurement2)->GetUncertainty(unc_i);
438  double pre = p.pre;
439 
440  // check if indices agree
441  if (meas_i == p.index_measurement1 && meas_j == p.index_measurement2) {
442  mat_small[counteri][counterj] = pre * parameters.at(p.index_rhoparameter) * sigma_i*sigma_j;
443  mat_small[counterj][counteri] = mat_small[counteri][counterj];
444  }
445  }
446  } // end loop: nuisance parameters
447  }
448 
449  // increase counter of active measurements
450  counterj++;
451  } // end loop: measurements j
452 
453  // increase counter of active measurements
454  counteri++;
455  } // end loop: measurements i
456 
457  // add matrix if active
458  if (u->GetFlagActive())
459  fCovarianceMatrix += mat_small;
460  }
461  fInvCovarianceMatrix.Clear();
462  fInvCovarianceMatrix.ResizeTo(fCovarianceMatrix);
463  fInvCovarianceMatrix = fCovarianceMatrix;
464  fInvCovarianceMatrix.Invert();
465 
466  fDetCovariance = fCovarianceMatrix.Determinant();
467 }
468 
469 // ---------------------------------------------------------
470 bool BCMVCombination::PositiveDefinite()
471 {
472  TMatrixDEigen m(fCovarianceMatrix);
473 
474  // calculate real part of all eigenvalues
475  TVectorD eigen_re = m.GetEigenValuesRe();
476 
477  int n_eigen = eigen_re.GetNoElements();
478 
479  bool flag_ispositive = true;
480 
481  // check if eigenvalues are positive
482  for (int i = 0; i < n_eigen; ++i) {
483  if (eigen_re[i] < 0)
484  flag_ispositive = false;
485  }
486 
487  // true if all eigenvalues are positive
488  return flag_ispositive;
489 }
490 
491 // ---------------------------------------------------------
492 void BCMVCombination::CalculateBLUE()
493 {
494  int nmeas = GetNMeasurements();
495  int nactivemeas = GetNActiveMeasurements();
496  int nobs = GetNObservables();
497  int nunc = GetNUncertainties();
498 
499  // calculate U matrix
500  // TMatrixD u(nmeas, nobs);
501  TMatrixD u(nactivemeas, nobs);
502 
503  int counter = 0;
504  for (int i = 0; i < nmeas; ++i) {
505  BCMVCMeasurement* m = GetMeasurement(i);
506 
507  // if measurement is active fill matrix u
508  if (m->GetFlagActive()) {
509  for (int j = 0; j < nobs; ++j) {
510  if (m->GetObservable() == j)
511  u[counter][j] = 1;
512  else
513  u[counter][j] = 0;
514  }
515  counter++;
516  }
517  }
518 
519  // calculate weight matrix
520  TMatrixD ut = u;
521  ut.Transpose(ut);
522 
523  TMatrixD m1 = ut * fInvCovarianceMatrix;
524  TMatrixD m2 = m1 * u;
525  m2.Invert();
526 
527  fBLUEWeights.Clear();
528  // fBLUEWeights.ResizeTo(nobs, nmeas);
529  fBLUEWeights.ResizeTo(nobs, nactivemeas);
530 
531  fBLUEWeights = m2*m1;
532 
533  // calculate central values
534  fBLUECentral.Clear();
535  fBLUECentral.ResizeTo(nobs);
536 
537  // fBLUECentral = fBLUEWeights * fVectorMeasurements;
538  fBLUECentral = fBLUEWeights * fVectorActiveMeasurements;
539 
540  // calculate covariance matrix
541  fBLUECovarianceMatrix.Clear();
542  fBLUECovarianceMatrix.ResizeTo(nobs, nobs);
543 
544  TMatrixD weightt = fBLUEWeights;
545  weightt.Transpose(weightt);
546 
547  fBLUECovarianceMatrix = fBLUEWeights * fCovarianceMatrix * weightt;
548 
549  // calculate uncertainties, covariance and correlation matrices for each uncertainty
550  for (int i = 0; i < nunc; ++i) {
551 
552  // calculate covariance matrix
553  BCMVCUncertainty* u = GetUncertainty(i);
554  if (!u->GetFlagActive())
555  continue;
556 
557  TMatrixD cov = u->GetCovarianceMatrix();
558  TMatrixD mat = fBLUEWeights * cov * weightt;
559  fBLUECovarianceMatrices.push_back(mat);
560 
561  // calculate uncertainties
562  TVectorD vec(nobs);
563  for (int j = 0; j < nobs; ++j) {
564  double sigma_j = sqrt(mat[j][j]);
565  vec[j] = sigma_j;
566  }
567  fBLUEUncertaintiesPerSource.push_back(vec);
568 
569  TMatrixD mat2(nobs, nobs);
570  for (int j = 0; j < nobs; ++j)
571  for (int k = 0; k < nobs; ++k) {
572  mat2[j][k] = mat[j][k]/vec[j]/vec[k];
573  }
574  fBLUECorrelationMatrices.push_back(mat2);
575  }
576 
577  // calculate uncertainties
578  fBLUEUncertainties.Clear();
579  fBLUEUncertainties.ResizeTo(nobs);
580 
581  for (int i = 0; i < nobs; ++i)
582  fBLUEUncertainties[i] = sqrt(fBLUECovarianceMatrix[i][i]);
583 
584  // calculate correlation matrix
585  fBLUECorrelationMatrix.Clear();
586  fBLUECorrelationMatrix.ResizeTo(nobs, nobs);
587 
588  for (int i = 0; i < nobs; ++i)
589  for (int j = 0; j < nobs; ++j)
590  fBLUECorrelationMatrix[i][j] = fBLUECovarianceMatrix[i][j] / fBLUEUncertainties[i] / fBLUEUncertainties[j];
591 }
592 
593 // ---------------------------------------------------------
594 void BCMVCombination::PrintBLUEResults(std::string filename)
595 {
596  // open file
597  std::ofstream ofi(filename.c_str());
598 
599  // check if file is open
600  if (!ofi.is_open()) {
601  std::cerr << "Couldn't open file " << filename << std::endl;
602  return;
603  }
604 
605  int nobs = GetNObservables();
606  int nmeas = GetNMeasurements();
607  int nunc = GetNUncertainties();
608 
609  ofi << std::endl;
610  ofi << "Summary of the combination" << std::endl;
611  ofi << "==========================" << std::endl << std::endl;
612 
613  ofi << "* Observables:" << std::endl;
614  ofi << " Observable (range): " << std::endl;
615  for (int i = 0; i < nobs; ++i)
616  ofi << " " << std::setiosflags(std::ios::left) << GetParameter(i)->GetName()
617  << " (" << GetParameter(i)->GetLowerLimit() << " - " << GetParameter(i)->GetUpperLimit() << ")" << std::endl;
618  ofi << std::endl;
619 
620  ofi << "* Measurements:" << std::endl;
621  ofi << " Measurement (observable): central value +- total uncertainty" << std::endl;
622  for (int i = 0; i < nmeas; ++i) {
623  BCMVCMeasurement* m = GetMeasurement(i);
624  if (m->GetFlagActive()) {
625  double total2 = 0;
626  for (int j = 0; j < nunc; ++j) {
627  if (GetUncertainty(j)->GetFlagActive())
628  total2+=m->GetUncertainty(j)*m->GetUncertainty(j);
629  }
630  ofi << " " << std::setiosflags(std::ios::left) << std::setw(20) << m->GetName()
631  << std::setiosflags(std::ios::left) << " (" << GetParameter(m->GetObservable())->GetName() << ")"
632  << ": " << std::setiosflags(std::ios::left) << std::setw(7) << std::setprecision(4) << m->GetCentralValue()
633  << " +- " << std::setiosflags(std::ios::left) << std::setw(7) << std::setprecision(4) << sqrt(total2) << std::endl;
634  }
635  }
636  ofi << std::endl;
637 
638  ofi << "* Uncertainties:" << std::endl;
639  ofi << " Measurement (observable): Uncertainty (";
640  for (int j = 0; j < nunc-1; ++j )
641  if (GetUncertainty(j)->GetFlagActive())
642  ofi << GetUncertainty(j)->GetName() << ", ";
643  if (GetUncertainty(nunc-1)->GetFlagActive())
644  ofi << GetUncertainty(nunc-1)->GetName() << ")" << std::endl;
645  else
646  ofi << ")" << std::endl;
647 
648  for (int i = 0; i < nmeas; ++i) {
649  BCMVCMeasurement* m = GetMeasurement(i);
650  if (m->GetFlagActive()) {
651  ofi << " " << std::setiosflags(std::ios::left) << std::setw(20) << m->GetName()
652  << std::setiosflags(std::ios::left) << " (" << GetParameter(m->GetObservable())->GetName() << "): ";
653  for (int j = 0; j < nunc; ++j )
654  if (GetUncertainty(j)->GetFlagActive())
655  ofi << std::setiosflags(std::ios::left) << std::setw(7) << m->GetUncertainty(j);
656  ofi << std::endl;
657  }
658  }
659  ofi << std::endl;
660 
661  for (int i = 0; i < nunc; ++i) {
662  BCMVCUncertainty* u = GetUncertainty(i);
663 
664  if (!u->GetFlagActive())
665  continue;
666 
667  ofi << " " << u->GetName() << " " << "(correlation matrix)" << std::endl;
668  TMatrixD mat = u->GetCorrelationMatrix();
669 
670  int counterk = 0;
671  for (int k = 0; k < nmeas; ++k) {
672  BCMVCMeasurement* mk = GetMeasurement(k);
673 
674  // skip line if not active
675  if (!mk->GetFlagActive())
676  continue;
677 
678  ofi << " ";
679 
680  int counterj = 0;
681  for (int j = 0; j < nmeas; ++j) {
682  BCMVCMeasurement* mj = GetMeasurement(j);
683 
684  if (mj->GetFlagActive()) {
685  ofi << std::setw(7) << std::showpos << mat[k][j] << " ";
686  counterj++;
687  }
688  }
689  ofi << std::noshowpos << std::endl;
690  counterk++;
691  }
692  ofi << std::endl;
693  }
694 
695  ofi << "* BLUE results: " << std::endl;
696  ofi << " Observable: estimate +- total uncertainty" << std::endl;
697  for (int i = 0; i < nobs; ++i) {
698  if (i < fBLUECentral.GetNoElements())
699  ofi << " " << GetParameter(i)->GetName() << ": " << fBLUECentral[i] << " +- " << std::setprecision(4) << fBLUEUncertainties[i] << std::endl;
700  }
701  ofi << std::endl;
702 
703  ofi << " Observable: Uncertainty (";
704  for (int j = 0; j < nunc-1; ++j )
705  if (GetUncertainty(j)->GetFlagActive())
706  ofi << GetUncertainty(j)->GetName() << ", ";
707  if (GetUncertainty(nunc-1)->GetFlagActive())
708  ofi << GetUncertainty(nunc-1)->GetName() << ")" << std::endl;
709  else
710  ofi << ")" << std::endl;
711 
712  for (int i = 0; i < nobs; ++i) {
713  ofi << " " << std::setiosflags(std::ios::left) << GetParameter(i)->GetName()<< ":";
714  int counterj = 0;
715  for (int j = 0; j < nunc; ++j )
716  if (GetUncertainty(j)->GetFlagActive()) {
717  ofi << " " << std::setiosflags(std::ios::left) << std::setw(7) << std::setprecision(4) << GetBLUEUncertainties(counterj)[i];
718  counterj++;
719  }
720  ofi << std::endl;
721  }
722  ofi << std::endl;
723 
724  if (nobs > 1) {
725  ofi << " Individual correlation matrices " << std::endl;
726  for (int i = 0; i < nunc; ++i) {
727  if (GetUncertainty(i)->GetFlagActive()) {
728  TMatrixD mat = GetBLUECorrelationMatrix(i);
729  ofi << " " << GetUncertainty(i)->GetName() << std::endl;
730  for (int j = 0; j < nobs; ++j) {
731  ofi << " ";
732  for (int k = 0; k < nobs; ++k) {
733  ofi << std::setw(7) << std::setprecision(4) << std::showpos << mat[j][k] << " ";
734  }
735  ofi << std::noshowpos << std::endl;
736  }
737  ofi << std::endl;
738  }
739  }
740  }
741 
742  if (nobs > 1) {
743  ofi << " Overall correlation matrix" << std::endl;
744  TMatrixD mat = fBLUECorrelationMatrix;
745  for (int j = 0; j < nobs; ++j) {
746  ofi << " ";
747  for (int k = 0; k < nobs; ++k)
748  ofi << std::setw(7) << std::setprecision(4) << std::showpos << mat[j][k] << " ";
749  ofi << std::noshowpos << std::endl;
750  }
751  ofi << std::endl;
752  }
753 
754  ofi << " Weights [%]:" <<std::endl;
755  int counter = 0;
756  for (int j = 0; j < nmeas; ++j) {
757  if (GetMeasurement(j)->GetFlagActive()) {
758  ofi << " " << std::setw(20) << GetMeasurement(j)->GetName() << " : ";
759  for (int k = 0; k < nobs; ++k)
760  ofi << std::setw(7) << std::setprecision(4) << std::showpos << fBLUEWeights[k][counter]*100. << " ";
761  ofi << std::endl;
762  counter++;
763  }
764  }
765  ofi << std::endl;
766 
767  // close file
768  ofi.close();
769 }
770 
771 // ---------------------------------------------------------
772 void BCMVCombination::PrintMatrix(TMatrixD &matrix, std::string name)
773 {
774  int nrows = matrix.GetNrows();
775  int ncols = matrix.GetNcols();
776 
777  std::cout << std::endl;
778  std::cout << name.c_str() << " (" << nrows << "x" << ncols << "):" << std::endl;
779 
780  for (int i = 0; i < nrows; ++i) {
781  for (int j = 0; j < ncols; ++j)
782  std::cout << std::setprecision(3) << std::setw(7) << matrix[i][j] << " ";
783  std::cout << std::endl;
784  }
785 }
786 
787 // ---------------------------------------------------------
788 void BCMVCombination::PrintVector(TVectorD &vector, std::string name)
789 {
790  int nrows = vector.GetNoElements();
791 
792  std::cout << std::endl;
793  std::cout << name.c_str() << " (" << nrows << "):" << std::endl;
794 
795  for (int i = 0; i < nrows; ++i) {
796  std::cout << std::setprecision(3) << std::setw(7) << vector[i] << " ";
797  std::cout << std::endl;
798  }
799 }
800 
801 // ---------------------------------------------------------
802 int BCMVCombination::GetIndexMeasurement(std::string measurement, std::string observable)
803 {
804  int index_observable = GetIndexObservable(observable);
805 
806  int nmeasurements = GetNMeasurements();
807 
808  for (int i = 0; i < nmeasurements; ++i) {
809  if ((measurement == GetMeasurement(i)->GetName()) && (index_observable == GetMeasurement(i)->GetObservable()))
810  return i;
811  }
812 
813  return -1;
814 }
815 
816 // ---------------------------------------------------------
817 int BCMVCombination::GetIndexUncertainty(std::string name)
818 {
819  int nuncertainties = GetNUncertainties();
820 
821  int index = -1;
822 
823  for (int i = 0; i < nuncertainties; ++i) {
824  if (name == GetUncertainty(i)->GetName())
825  index = i;
826  }
827 
828  return index;
829 }
830 
831 // ---------------------------------------------------------
832 int BCMVCombination::GetIndexObservable(std::string name)
833 {
834  int n = GetNObservables();
835 
836  // go through the list of parameters and compare strings
837  for (int i = 0; i < n; ++i) {
838  // if (name == std::string(GetParameter(i)->GetName()))
839  if (name == std::string(fObservables.at(i)->GetName()))
840  return i;
841  }
842 
843  // return -1 if not in the list
844  return -1;
845 }
846 
847 // ---------------------------------------------------------
int SetPriorGauss(int index, double mean, double sigma)
Definition: BCModel.cxx:610
The base class for all user-defined models.
Definition: BCModel.h:50
double LogLikelihood(const std::vector< double > &parameters)
virtual int AddParameter(BCParameter *parameter)
Definition: BCModel.cxx:225
int SetPriorConstant(int index)
Definition: BCModel.cxx:732
const std::string & GetName() const
Definition: BCModel.h:85