00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "BAT/BCModel.h"
00011
00012 #include "BAT/BCDataPoint.h"
00013 #include "BAT/BCDataSet.h"
00014 #include "BAT/BCParameter.h"
00015 #include "BAT/BCH1D.h"
00016 #include "BAT/BCH2D.h"
00017 #include "BAT/BCGoFTest.h"
00018 #include "BAT/BCLog.h"
00019 #include "BAT/BCErrorCodes.h"
00020 #include "BAT/BCMath.h"
00021
00022 #include <TCanvas.h>
00023 #include <TPostScript.h>
00024 #include <TDirectory.h>
00025 #include <TFile.h>
00026 #include <TKey.h>
00027 #include <TTree.h>
00028 #include <TMath.h>
00029 #include <TGraph.h>
00030 #include <TH2D.h>
00031
00032 #include <fstream>
00033 #include <iomanip>
00034
00035
00036
00037 BCModel::BCModel(const char * name) : BCIntegrate()
00038 {
00039 fNormalization = -1.0;
00040 fDataSet = 0;
00041 fParameterSet = new BCParameterSet;
00042
00043 fIndex = -1;
00044 fPValue = -1;
00045
00046 fName = (char *) name;
00047 flag_ConditionalProbabilityEntry = true;
00048
00049 fDataPointUpperBoundaries = 0;
00050 fDataPointLowerBoundaries = 0;
00051
00052 fErrorBandXY = 0;
00053
00054 fGoFNChains = 5;
00055 fGoFNIterationsMax = 100000;
00056 fGoFNIterationsRun = 2000;
00057 }
00058
00059
00060
00061 BCModel::BCModel() : BCIntegrate()
00062 {
00063 fNormalization = -1.0;
00064 fDataSet = 0;
00065 fParameterSet = new BCParameterSet();
00066
00067 fIndex = -1;
00068 fPValue = -1;
00069
00070 fName = "model";
00071 fDataPointUpperBoundaries = 0;
00072 fDataPointLowerBoundaries = 0;
00073
00074 flag_ConditionalProbabilityEntry = true;
00075
00076 fGoFNChains = 5;
00077 fGoFNIterationsMax = 100000;
00078 fGoFNIterationsRun = 2000;
00079 }
00080
00081
00082
00083 BCModel::~BCModel()
00084 {
00085 delete fParameterSet;
00086
00087 if (fDataPointLowerBoundaries)
00088 delete fDataPointLowerBoundaries;
00089
00090 if (fDataPointUpperBoundaries)
00091 delete fDataPointUpperBoundaries;
00092 }
00093
00094
00095
00096 int BCModel::GetNDataPoints()
00097 {
00098 int npoints = 0;
00099 if (fDataSet)
00100 npoints = fDataSet -> GetNDataPoints();
00101 else
00102 {
00103 BCLog::Out(BCLog::warning, BCLog::warning,"BCModel::GetNDataPoints(). No data set defined.");
00104 return ERROR_NOEVENTS;
00105 }
00106
00107 return npoints;
00108 }
00109
00110
00111
00112 BCDataPoint * BCModel::GetDataPoint(unsigned int index)
00113 {
00114 if (fDataSet)
00115 return fDataSet -> GetDataPoint(index);
00116
00117 BCLog::Out(BCLog::warning, BCLog::warning,"BCModel::GetDataPoint. No data set defined.");
00118 return 0;
00119 }
00120
00121
00122
00123 BCParameter * BCModel::GetParameter(int index)
00124 {
00125 if (!fParameterSet)
00126 return 0;
00127
00128 if (index < 0 || index >= (int)this -> GetNParameters())
00129 {
00130 BCLog::Out(BCLog::warning, BCLog::warning,
00131 Form("BCModel::GetParameter. Parameter index %d not within range.", index));
00132 return 0;
00133 }
00134
00135 return fParameterSet -> at(index);
00136 }
00137
00138
00139
00140 BCParameter * BCModel::GetParameter(const char * name)
00141 {
00142 if (!fParameterSet)
00143 return 0;
00144
00145 int index = -1;
00146 for (unsigned int i = 0; i < this->GetNParameters(); i++)
00147 if (name == this -> GetParameter(i) -> GetName())
00148 index = i;
00149
00150 if (index<0)
00151 {
00152 BCLog::Out(BCLog::warning, BCLog::warning,
00153 Form("BCModel::GetParameter. Model %s has no parameter named %s.", (this -> GetName()).data(), name));
00154 return 0;
00155 }
00156
00157 return this->GetParameter(index);
00158 }
00159
00160
00161
00162 std::vector <double> BCModel::GetErrorBand(double level)
00163 {
00164 std::vector <double> errorband;
00165
00166 if (!fErrorBandXY)
00167 return errorband;
00168
00169 int nx = fErrorBandXY -> GetNbinsX();
00170 errorband.assign(nx, 0.0);
00171
00172
00173 for (int ix = 1; ix <= nx; ix++)
00174 {
00175 TH1D * temphist = fErrorBandXY -> ProjectionY("temphist", ix, ix);
00176
00177 int nprobSum = 1;
00178 double q[1];
00179 double probSum[1];
00180 probSum[0] = level;
00181
00182 temphist -> GetQuantiles(nprobSum, q, probSum);
00183
00184 errorband[ix-1] = q[0];
00185 }
00186
00187 return errorband;
00188 }
00189
00190
00191
00192 TGraph * BCModel::GetErrorBandGraph(double level1, double level2)
00193 {
00194 if (!fErrorBandXY)
00195 return 0;
00196
00197
00198 int nx = fErrorBandXY -> GetNbinsX();
00199
00200 TGraph * graph = new TGraph(2 * nx);
00201 graph -> SetFillStyle(1001);
00202 graph -> SetFillColor(kYellow);
00203
00204
00205 std::vector <double> ymin = this -> GetErrorBand(level1);
00206 std::vector <double> ymax = this -> GetErrorBand(level2);
00207
00208 for (int i = 0; i < nx; i++)
00209 {
00210 graph -> SetPoint(i, fErrorBandXY -> GetXaxis() -> GetBinCenter(i + 1), ymin.at(i));
00211 graph -> SetPoint(nx + i, fErrorBandXY -> GetXaxis() -> GetBinCenter(nx - i), ymax.at(nx - i - 1));
00212 }
00213
00214 return graph;
00215 }
00216
00217
00218
00219 TH2D * BCModel::GetErrorBandXY_yellow(double level, int nsmooth)
00220 {
00221 if (!fErrorBandXY)
00222 return 0;
00223
00224 int nx = fErrorBandXY -> GetNbinsX();
00225 int ny = fErrorBandXY -> GetNbinsY();
00226
00227
00228 TH2D * hist_tempxy = (TH2D*) fErrorBandXY -> Clone(TString::Format("%s_sub_%f.2",fErrorBandXY->GetName(),level));
00229 hist_tempxy -> Reset();
00230 hist_tempxy -> SetFillColor(kYellow);
00231
00232
00233 for (int ix = 1; ix < nx; ix++)
00234 {
00235 BCH1D * hist_temp = new BCH1D();
00236
00237 TH1D * hproj = fErrorBandXY -> ProjectionY("temphist", ix, ix);
00238 if(nsmooth>0)
00239 hproj->Smooth(nsmooth);
00240
00241 hist_temp -> SetHistogram(hproj);
00242
00243 TH1D * hist_temp_yellow = hist_temp -> GetSmallestIntervalHistogram(level);
00244
00245 for (int iy = 1; iy <= ny; ++iy)
00246 hist_tempxy -> SetBinContent(ix, iy, hist_temp_yellow -> GetBinContent(iy));
00247
00248 delete hist_temp_yellow;
00249 delete hist_temp;
00250 }
00251
00252 return hist_tempxy;
00253 }
00254
00255
00256
00257 TGraph * BCModel::GetFitFunctionGraph(std::vector <double> parameters)
00258 {
00259 if (!fErrorBandXY)
00260 return 0;
00261
00262
00263 int nx = fErrorBandXY -> GetNbinsX();
00264 TGraph * graph = new TGraph(nx);
00265
00266
00267 for (int i = 0; i < nx; i++)
00268 {
00269 double x = fErrorBandXY -> GetXaxis() -> GetBinCenter(i + 1);
00270
00271 std::vector <double> xvec;
00272 xvec.push_back(x);
00273 double y = this -> FitFunction(xvec, parameters);
00274 xvec.clear();
00275
00276 graph -> SetPoint(i, x, y);
00277 }
00278
00279 return graph;
00280 }
00281
00282
00283
00284 TGraph * BCModel::GetFitFunctionGraph(std::vector <double> parameters, double xmin, double xmax, int n)
00285 {
00286
00287 TGraph * graph = new TGraph(n+1);
00288
00289 double dx = (xmax-xmin)/(double)n;
00290
00291
00292 for (int i = 0; i <= n; i++)
00293 {
00294 double x = (double)i*dx+xmin;
00295 std::vector <double> xvec;
00296 xvec.push_back(x);
00297 double y = this -> FitFunction(xvec, parameters);
00298
00299 xvec.clear();
00300
00301 graph -> SetPoint(i, x, y);
00302 }
00303
00304 return graph;
00305 }
00306
00307
00308
00309 bool BCModel::GetFlagBoundaries()
00310 {
00311 if (!fDataPointLowerBoundaries)
00312 return false;
00313
00314 if (!fDataPointUpperBoundaries)
00315 return false;
00316
00317 if (fDataPointLowerBoundaries -> GetNValues() != fDataSet -> GetDataPoint(0) -> GetNValues())
00318 return false;
00319
00320 if (fDataPointUpperBoundaries -> GetNValues() != fDataSet -> GetDataPoint(0) -> GetNValues())
00321 return false;
00322
00323 return true;
00324 }
00325
00326
00327
00328 void BCModel::SetSingleDataPoint(BCDataPoint * datapoint)
00329 {
00330
00331 BCDataSet * dataset = new BCDataSet();
00332
00333
00334 dataset -> AddDataPoint(datapoint);
00335
00336
00337 this -> SetDataSet(dataset);
00338
00339 }
00340
00341
00342
00343 void BCModel::SetSingleDataPoint(BCDataSet * dataset, unsigned int index)
00344 {
00345 if (index < 0 || index > dataset -> GetNDataPoints())
00346 return;
00347
00348 this -> SetSingleDataPoint(dataset -> GetDataPoint(index));
00349 }
00350
00351
00352
00353 void BCModel::SetDataBoundaries(unsigned int index, double lowerboundary, double upperboundary, bool fixed)
00354 {
00355
00356 if (!fDataSet)
00357 {
00358 BCLog::Out(BCLog::error, BCLog::error,
00359 "BCModel::SetDataBoundaries : Need to define data set first.");
00360 return;
00361 }
00362
00363
00364 if (index < 0 || index > fDataSet -> GetDataPoint(0) -> GetNValues())
00365 {
00366 BCLog::Out(BCLog::error, BCLog::error,
00367 "BCModel::SetDataBoundaries : Index out of range.");
00368 return;
00369 }
00370
00371
00372 if (!fDataPointLowerBoundaries)
00373 fDataPointLowerBoundaries = new BCDataPoint(fDataSet -> GetDataPoint(0) -> GetNValues());
00374
00375 if (!fDataPointUpperBoundaries)
00376 fDataPointUpperBoundaries = new BCDataPoint(fDataSet -> GetDataPoint(0) -> GetNValues());
00377
00378 if (fDataFixedValues.size() == 0)
00379 fDataFixedValues.assign(fDataSet -> GetDataPoint(0) -> GetNValues(), false);
00380
00381
00382 fDataPointLowerBoundaries -> SetValue(index, lowerboundary);
00383 fDataPointUpperBoundaries -> SetValue(index, upperboundary);
00384 fDataFixedValues[index] = fixed;
00385 }
00386
00387
00388
00389 void BCModel::SetErrorBandContinuous(bool flag)
00390 {
00391 fErrorBandContinuous = flag;
00392
00393 if (flag)
00394 return;
00395
00396
00397 fErrorBandX.clear();
00398
00399
00400 for (unsigned int i = 0; i < fDataSet -> GetNDataPoints(); ++i)
00401 fErrorBandX.push_back(fDataSet -> GetDataPoint(i) -> GetValue(fFitFunctionIndexX));
00402 }
00403
00404
00405
00406 int BCModel::AddParameter(const char * name, double lowerlimit, double upperlimit)
00407 {
00408
00409 BCParameter * parameter = new BCParameter(name, lowerlimit, upperlimit);
00410
00411 int flag_ok = this -> AddParameter(parameter);
00412 if (flag_ok)
00413 delete parameter;
00414
00415 return flag_ok;
00416 }
00417
00418
00419
00420 int BCModel::AddParameter(BCParameter * parameter)
00421 {
00422
00423 if (!fParameterSet)
00424 {
00425 BCLog::Out(BCLog::error, BCLog::error,
00426 "BCModel::AddParameter : Parameter set does not exist");
00427 return ERROR_PARAMETERSETDOESNOTEXIST;
00428 }
00429
00430
00431 int flag_exists = 0;
00432 for (unsigned int i = 0; i < this -> GetNParameters(); i++)
00433 if (this -> CompareStrings(parameter -> GetName().data(), this -> GetParameter(i) -> GetName().data()) == 0)
00434 flag_exists = -1;
00435
00436 if (flag_exists < 0)
00437 {
00438 BCLog::Out(BCLog::error, BCLog::error,
00439 Form("BCModel::AddParameter : Parameter with name %s exists already. ", parameter -> GetName().data()));
00440 return ERROR_PARAMETEREXISTSALREADY;
00441 }
00442
00443
00444 unsigned int index = fParameterSet -> size();
00445 parameter -> SetIndex(index);
00446
00447
00448 fParameterSet -> push_back(parameter);
00449
00450
00451 this -> SetParameters(fParameterSet);
00452
00453
00454
00455
00456
00457
00458
00459 return 0;
00460 }
00461
00462
00463
00464 double BCModel::LogProbabilityNN(std::vector <double> parameters)
00465 {
00466
00467 double logprob = this -> LogLikelihood(parameters);
00468
00469
00470 logprob += this -> LogAPrioriProbability(parameters);
00471
00472 return logprob;
00473 }
00474
00475
00476
00477 double BCModel::LogProbability(std::vector <double> parameters)
00478 {
00479
00480 if (fNormalization<0. || fNormalization==0.)
00481 {
00482 BCLog::Out(BCLog::warning, BCLog::warning,
00483 "BCModel::LogProbability. Normalization not available or zero.");
00484 return 0.;
00485 }
00486
00487 return this -> LogProbabilityNN(parameters) - log(fNormalization);
00488 }
00489
00490
00491
00492 double BCModel::LogLikelihood(std::vector <double> parameters)
00493 {
00494 double logprob = 0.;
00495
00496
00497 for (unsigned int i=0;i<fDataSet -> GetNDataPoints();i++)
00498 {
00499 BCDataPoint * datapoint = this -> GetDataPoint(i);
00500 logprob += this -> LogConditionalProbabilityEntry(datapoint, parameters);
00501 }
00502
00503 return logprob;
00504 }
00505
00506
00507
00508 double BCModel::LogEval(std::vector <double> parameters)
00509 {
00510 return this -> LogProbabilityNN(parameters);
00511 }
00512
00513
00514
00515 double BCModel::EvalSampling(std::vector <double> parameters)
00516 {
00517 return this -> SamplingFunction(parameters);
00518 }
00519
00520
00521
00522 double BCModel::SamplingFunction(std::vector <double> parameters)
00523 {
00524 double probability = 1.0;
00525 for (std::vector<BCParameter*>::const_iterator it = fParameterSet -> begin(); it != fParameterSet -> end(); ++it)
00526 probability *= 1.0 / ((*it) -> GetUpperLimit() - (*it) -> GetLowerLimit());
00527 return probability;
00528 }
00529
00530
00531
00532 double BCModel::Normalize()
00533 {
00534 BCLog::Out(BCLog::summary, BCLog::summary, Form("Model \'%s\': Normalizing probability",this->GetName().data()));
00535
00536 unsigned int n = this -> GetNvar();
00537
00538
00539 if (n == 0)
00540 {
00541 this->SetParameters(fParameterSet);
00542 n = this->GetNvar();
00543 }
00544
00545
00546
00547 fNormalization = this -> Integrate();
00548
00549 BCLog::Out(BCLog::detail, BCLog::detail, Form(" --> Normalization factor : %.6g", fNormalization));
00550
00551 return fNormalization;
00552 }
00553
00554
00555
00556 int BCModel::CheckParameters(std::vector <double> parameters)
00557 {
00558
00559 if (!parameters.size() == fParameterSet -> size())
00560 return ERROR_INVALIDNUMBEROFPARAMETERS;
00561
00562
00563 for (unsigned int i = 0; i < fParameterSet -> size(); i++)
00564 {
00565 BCParameter * modelparameter = fParameterSet -> at(i);
00566
00567 if (modelparameter -> GetLowerLimit() > parameters.at(i) ||
00568 modelparameter -> GetUpperLimit() < parameters.at(i))
00569 {
00570 BCLog::Out(BCLog::error, BCLog::error,
00571 Form("BCModel::CheckParameters : Parameter %s not within limits.", fParameterSet -> at(i) -> GetName().data()));
00572 return ERROR_PARAMETERNOTWITHINRANGE;
00573 }
00574 }
00575
00576 return 0;
00577 }
00578
00579
00580
00581 void BCModel::FindMode()
00582 {
00583
00584
00585 BCLog::Out(BCLog::summary, BCLog::summary,
00586 Form("Model \'%s\': Finding mode", this -> GetName().data()));
00587
00588
00589 this -> SetParameters(fParameterSet);
00590
00591 switch(this -> GetOptimizationMethod())
00592 {
00593 case BCIntegrate::kOptSimulatedAnnealing:
00594
00595 BCLog::Out(BCLog::warning, BCLog::warning, "BCModel::FindMode. Simulated annaeling not yet implemented.");
00596 return;
00597 case BCIntegrate::kOptMinuit:
00598 this -> FindModeMinuit();
00599 return;
00600 case BCIntegrate::kOptMetropolis:
00601 this -> MarginalizeAll();
00602 return;
00603 }
00604
00605 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::FindMode. Invalid mode finding method: %d. Return.",
00606 this->GetOptimizationMethod()));
00607
00608 return;
00609 }
00610
00611
00612
00613 void BCModel::WriteMode(const char * file)
00614 {
00615 ofstream ofi(file);
00616 if(!ofi.is_open())
00617 {
00618 std::cerr<<"Couldn't open file "<<file<<std::endl;
00619 return;
00620 }
00621
00622 int npar = fParameterSet -> size();
00623 for (int i=0; i<npar; i++)
00624 ofi<<fBestFitParameters.at(i)<<std::endl;
00625
00626 ofi<<std::endl;
00627 ofi<<"#######################################################################"<<std::endl;
00628 ofi<<"#"<<std::endl;
00629 ofi<<"# This file was created automatically by BCModel::WriteMode() call."<<std::endl;
00630 ofi<<"# It can be read in by call to BCModel::ReadMode()."<<std::endl;
00631 ofi<<"# Do not modify it unless you know what you're doing."<<std::endl;
00632 ofi<<"#"<<std::endl;
00633 ofi<<"#######################################################################"<<std::endl;
00634 ofi<<"#"<<std::endl;
00635 ofi<<"# Best fit parameters (mode) for model:"<<std::endl;
00636 ofi<<"# \'"<<fName.data()<<"\'"<<std::endl;
00637 ofi<<"#"<<std::endl;
00638 ofi<<"# Number of parameters: "<<npar<<std::endl;
00639 ofi<<"# Parameters ordered as above:"<<std::endl;
00640
00641 for (int i=0; i<npar; i++)
00642 {
00643 ofi<<"# "<<i<<": ";
00644 ofi<<fParameterSet->at(i)->GetName().data()<<" = ";
00645 ofi<<fBestFitParameters.at(i)<<std::endl;
00646 }
00647
00648 ofi<<"#"<<std::endl;
00649 ofi<<"########################################################################"<<std::endl;
00650 }
00651
00652
00653
00654 int BCModel::ReadMode(const char * file)
00655 {
00656 ifstream ifi(file);
00657 if(!ifi.is_open())
00658 {
00659 BCLog::Out(BCLog::error,BCLog::error,
00660 Form("BCModel::ReadMode : Couldn't open file %s.",file));
00661 return 0;
00662 }
00663
00664 int npar = fParameterSet -> size();
00665 std::vector <double> mode;
00666
00667 int i=0;
00668 while (i<npar && !ifi.eof())
00669 {
00670 double a;
00671 ifi>>a;
00672 mode.push_back(a);
00673 i++;
00674 }
00675
00676 if(i<npar)
00677 {
00678 BCLog::Out(BCLog::error,BCLog::error,
00679 Form("BCModel::ReadMode : Couldn't read mode from file %s.",file));
00680 BCLog::Out(BCLog::error,BCLog::error,
00681 Form("BCModel::ReadMode : Expected %d parameters, found %d.",npar,i));
00682 return 0;
00683 }
00684
00685 BCLog::Out(BCLog::summary,BCLog::summary,
00686 Form("# Read in best fit parameters (mode) for model \'%s\' from file %s:",fName.data(),file));
00687 this->SetMode(mode);
00688 for(int j=0 ; j<npar; j++)
00689 BCLog::Out(BCLog::summary,BCLog::summary,
00690 Form("# -> Parameter %d : %s = %e", j, fParameterSet->at(j)->GetName().data(), fBestFitParameters[j]));
00691
00692 BCLog::Out(BCLog::warning,BCLog::warning,
00693 "# ! Best fit values obtained before this call will be overwritten !");
00694
00695 return npar;
00696 }
00697
00698
00699
00700 int BCModel::MarginalizeAll()
00701 {
00702 BCLog::Out(BCLog::summary,BCLog::summary,
00703 Form("Running MCMC for model \'%s\'",this->GetName().data()));
00704
00705
00706 double dx = 0.0;
00707 double dy = 0.0;
00708
00709 if (fFitFunctionIndexX >= 0)
00710 {
00711 dx = (fDataPointUpperBoundaries -> GetValue(fFitFunctionIndexX) - fDataPointLowerBoundaries -> GetValue(fFitFunctionIndexX))
00712 / (double)fErrorBandNbinsX;
00713
00714 dy = (fDataPointUpperBoundaries -> GetValue(fFitFunctionIndexY) - fDataPointLowerBoundaries -> GetValue(fFitFunctionIndexY))
00715 / (double)fErrorBandNbinsY;
00716
00717 fErrorBandXY = new TH2D(
00718 TString::Format("errorbandxy_%d",BCLog::GetHIndex()), "",
00719 fErrorBandNbinsX,
00720 fDataPointLowerBoundaries -> GetValue(fFitFunctionIndexX) - .5 * dx,
00721 fDataPointUpperBoundaries -> GetValue(fFitFunctionIndexX) + .5 * dx,
00722 fErrorBandNbinsY,
00723 fDataPointLowerBoundaries -> GetValue(fFitFunctionIndexY) - .5 * dy,
00724 fDataPointUpperBoundaries -> GetValue(fFitFunctionIndexY) + .5 * dy);
00725 fErrorBandXY -> SetStats(kFALSE);
00726
00727 for (int ix = 1; ix <= fErrorBandNbinsX; ++ix)
00728 for (int iy = 1; iy <= fErrorBandNbinsX; ++iy)
00729 fErrorBandXY -> SetBinContent(ix, iy, 0.0);
00730 }
00731
00732 this -> MCMCMetropolis();
00733 this -> FindModeMCMC();
00734
00735
00736
00737 return 1;
00738 }
00739
00740
00741
00742
00743 BCH1D * BCModel::GetMarginalized(BCParameter * parameter)
00744 {
00745
00746
00747
00748
00749
00750
00751
00752 int index = parameter -> GetIndex();
00753
00754
00755 TH1D * hist = this -> MCMCGetH1Marginalized(index);
00756 if(!hist)
00757 return 0;
00758
00759 BCH1D * hprob = new BCH1D();
00760
00761
00762 hist -> SetName(Form("hist_%s_%s", this -> GetName().data(), parameter -> GetName().data()));
00763 hist -> SetXTitle(parameter -> GetName().data());
00764 hist -> SetYTitle(Form("p(%s|data)", parameter -> GetName().data()));
00765 hist -> SetStats(kFALSE);
00766
00767
00768 hprob -> SetHistogram(hist);
00769
00770
00771 double bestfit = hprob -> GetMode();
00772
00773 if (fBestFitParametersMarginalized.size() == 0)
00774 for (unsigned int i = 0; i < this -> GetNParameters(); i++)
00775 fBestFitParametersMarginalized.push_back(0.0);
00776
00777 fBestFitParametersMarginalized[index] = bestfit;
00778
00779 hprob->SetGlobalMode(fBestFitParameters.at(index));
00780
00781 return hprob;
00782 }
00783
00784
00785
00786 int BCModel::ReadMarginalizedFromFile(const char * file)
00787 {
00788 TFile * froot = new TFile(file);
00789 if(!froot->IsOpen())
00790 {
00791 BCLog::Out(BCLog::error, BCLog::error,
00792 Form("BCModel::ReadMarginalizedFromFile : Couldn't open file %s.",file));
00793 return 0;
00794 }
00795
00796
00797
00798
00799
00800
00801
00802 this -> MCMCInitialize();
00803
00804 int k=0;
00805 int n=this->GetNParameters();
00806 for(int i=0;i<n;i++)
00807 {
00808 BCParameter * a = this -> GetParameter(i);
00809 TKey * key = froot -> GetKey(TString::Format("hist_%s_%s", this -> GetName().data(), a -> GetName().data()));
00810 if(key)
00811 {
00812 TH1D * h1 = (TH1D*) key -> ReadObjectAny(TH1D::Class());
00813 h1->SetDirectory(0);
00814 if(this->SetMarginalized(i,h1))
00815 k++;
00816 }
00817 else
00818 BCLog::Out(BCLog::warning, BCLog::warning,
00819 Form("BCModel::ReadMarginalizedFromFile. Couldn't read histogram \"hist_%s_%s\" from file %s.",
00820 this -> GetName().data(), a -> GetName().data(), file));
00821 }
00822
00823 for(int i=0;i<n-1;i++)
00824 {
00825 for(int j=i+1;j<n;j++)
00826 {
00827 BCParameter * a = this -> GetParameter(i);
00828 BCParameter * b = this -> GetParameter(j);
00829 TKey * key = froot -> GetKey(TString::Format("hist_%s_%s_%s", this -> GetName().data(), a -> GetName().data(), b -> GetName().data()));
00830 if(key)
00831 {
00832 TH2D * h2 = (TH2D*) key -> ReadObjectAny(TH2D::Class());
00833 h2->SetDirectory(0);
00834 if(this->SetMarginalized(i,j,h2))
00835 k++;
00836 }
00837 else
00838 BCLog::Out(BCLog::warning, BCLog::warning,
00839 Form("BCModel::ReadMarginalizedFromFile. Couldn't read histogram \"hist_%s_%s_%s\" from file %s.",
00840 this -> GetName().data(), a -> GetName().data(), b -> GetName().data(), file));
00841 }
00842 }
00843
00844 froot->Close();
00845
00846 return k;
00847 }
00848
00849
00850
00851 int BCModel::ReadErrorBandFromFile(const char * file)
00852 {
00853 TFile * froot = new TFile(file);
00854 if(!froot->IsOpen())
00855 {
00856 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::ReadErrorBandFromFile. Couldn't open file %s.",file));
00857 return 0;
00858 }
00859
00860 int r=0;
00861
00862 TH2D * h2 = (TH2D*)froot->Get("errorbandxy");
00863 if(h2)
00864 {
00865 h2->SetDirectory(0);
00866 h2->SetName(TString::Format("errorbandxy_%d",BCLog::GetHIndex()));
00867 this->SetErrorBandHisto(h2);
00868 r=1;
00869 }
00870 else
00871 BCLog::Out(BCLog::warning, BCLog::warning,
00872 Form("BCModel::ReadErrorBandFromFile : Couldn't read histogram \"errorbandxy\" from file %s.", file));
00873
00874 froot->Close();
00875
00876 return r;
00877 }
00878
00879
00880
00881 int BCModel::PrintAllMarginalized1D(const char * filebase)
00882 {
00883 if(fMCMCH1Marginalized.size()==0)
00884 {
00885 BCLog::Out(BCLog::warning, BCLog::warning,
00886 "BCModel::PrintAllMarginalized1D : MarginalizeAll() has to be run prior to this to fill the distributions.");
00887 return 0;
00888 }
00889
00890 int n=this->GetNParameters();
00891 for(int i=0;i<n;i++)
00892 {
00893 BCParameter * a = this->GetParameter(i);
00894 this -> GetMarginalized(a) -> Print(Form("%s_1D_%s.ps",filebase,a->GetName().data()));
00895 }
00896
00897 return n;
00898 }
00899
00900
00901
00902 int BCModel::PrintAllMarginalized2D(const char * filebase)
00903 {
00904 if(fMCMCH2Marginalized.size()==0)
00905 {
00906 BCLog::Out(BCLog::warning, BCLog::warning,
00907 "BCModel::PrintAllMarginalized2D : MarginalizeAll() has to be run prior to this to fill the distributions.");
00908 return 0;
00909 }
00910
00911 int k=0;
00912 int n=this->GetNParameters();
00913 for(int i=0;i<n-1;i++)
00914 {
00915 for(int j=i+1;j<n;j++)
00916 {
00917 BCParameter * a = this->GetParameter(i);
00918 BCParameter * b = this->GetParameter(j);
00919
00920 double meana = (a -> GetLowerLimit() + a -> GetUpperLimit()) / 2.0;
00921 double deltaa = (a -> GetUpperLimit() - a -> GetLowerLimit());
00922 if (deltaa <= 1e-7 * meana)
00923 continue;
00924
00925 double meanb = (b -> GetLowerLimit() + b -> GetUpperLimit()) / 2.0;
00926 double deltab = (b -> GetUpperLimit() - b -> GetLowerLimit());
00927 if (deltab <= 1e-7 * meanb)
00928 continue;
00929
00930 this -> GetMarginalized(a,b) -> Print(Form("%s_2D_%s_%s.ps",filebase,a->GetName().data(),b->GetName().data()));
00931 k++;
00932 }
00933 }
00934
00935 return k;
00936 }
00937
00938
00939
00940 int BCModel::PrintAllMarginalized(const char * file, unsigned int hdiv, unsigned int vdiv)
00941 {
00942 if(fMCMCH1Marginalized.size()==0 || (fMCMCH2Marginalized.size()==0 && this -> GetNParameters() > 1))
00943 {
00944 BCLog::Out(BCLog::error, BCLog::error,
00945 "BCModel::PrintAllMarginalized : Marginalized distributions not available.");
00946 return 0;
00947 }
00948
00949
00950 if (fMCMCH1Marginalized.size()==1 && fMCMCH2Marginalized.size()==0)
00951 {
00952 BCParameter * a = this->GetParameter(0);
00953 this -> GetMarginalized(a) -> Print(file);
00954 return 1;
00955 }
00956
00957 int c_width=600;
00958 int c_height=600;
00959
00960 int type = 112;
00961
00962 if (hdiv > vdiv)
00963 {
00964 if(hdiv>3)
00965 {
00966 c_width=1000;
00967 c_height=700;
00968 }
00969 else
00970 {
00971 c_width=800;
00972 c_height=600;
00973 }
00974 }
00975 else if(hdiv < vdiv)
00976 {
00977 if(hdiv>3)
00978 {
00979 c_height=1000;
00980 c_width=700;
00981 }
00982 else
00983 {
00984 c_height=800;
00985 c_width=600;
00986 }
00987 type=111;
00988 }
00989
00990
00991 int npar = this -> GetNParameters();
00992 int nplots2d = npar * (npar-1)/2;
00993 int nplots = npar + nplots2d;
00994
00995
00996 BCLog::Out(BCLog::summary,BCLog::summary,
00997 Form("Printing all marginalized distributions (%d x 1D + %d x 2D = %d) into file %s",
00998 npar,nplots2d,nplots,file));
00999 if(nplots>100)
01000 BCLog::Out(BCLog::detail,BCLog::detail,"This can take a while...");
01001
01002
01003 TCanvas * c = new TCanvas( "c","canvas",c_width,c_height);
01004
01005 TPostScript * ps = new TPostScript(file,type);
01006
01007 if(type==112)
01008 ps->Range(24,16);
01009 else
01010 ps->Range(16,24);
01011
01012
01013 ps->NewPage();
01014 c->cd();
01015 c->Clear();
01016 c->Divide(hdiv,vdiv);
01017
01018 int n=0;
01019 for(int i=0;i<npar;i++)
01020 {
01021
01022 if(i!=0 && i%(hdiv*vdiv)==0)
01023 {
01024 c->Update();
01025 ps->NewPage();
01026 c->cd();
01027 c->Clear();
01028 c->Divide(hdiv,vdiv);
01029 }
01030
01031
01032 c->cd(i%(hdiv*vdiv)+1);
01033
01034 BCParameter * a = this->GetParameter(i);
01035 this -> GetMarginalized(a) -> Draw();
01036 n++;
01037
01038 if(n%100==0)
01039 BCLog::Out(BCLog::detail,BCLog::detail,Form(" --> %d plots done",n));
01040 }
01041
01042 c->Update();
01043
01044
01045 ps->NewPage();
01046 c->cd();
01047 c->Clear();
01048 c->Divide(hdiv,vdiv);
01049
01050 int k=0;
01051 for(int i=0;i<npar-1;i++)
01052 {
01053 for(int j=i+1;j<npar;j++)
01054 {
01055
01056 if(k!=0 && k%(hdiv*vdiv)==0)
01057 {
01058 c->Update();
01059 ps->NewPage();
01060 c->cd();
01061 c->Clear();
01062 c->Divide(hdiv,vdiv);
01063 }
01064
01065
01066 c->cd(k%(hdiv*vdiv)+1);
01067
01068 BCParameter * a = this->GetParameter(i);
01069 BCParameter * b = this->GetParameter(j);
01070
01071 double meana = (a -> GetLowerLimit() + a -> GetUpperLimit()) / 2.0;
01072 double deltaa = (a -> GetUpperLimit() - a -> GetLowerLimit());
01073 if (deltaa <= 1e-7 * meana)
01074 continue;
01075
01076 double meanb = (b -> GetLowerLimit() + b -> GetUpperLimit()) / 2.0;
01077 double deltab = (b -> GetUpperLimit() - b -> GetLowerLimit());
01078 if (deltab <= 1e-7 * meanb)
01079 continue;
01080
01081 this -> GetMarginalized(a,b) -> Draw(52);
01082 k++;
01083
01084 if((n+k)%100==0)
01085 BCLog::Out(BCLog::detail,BCLog::detail,Form(" --> %d plots done",n+k));
01086 }
01087 }
01088
01089 if( (n+k)>100 && (n+k)%100 != 0 )
01090 BCLog::Out(BCLog::detail,BCLog::detail,Form(" --> %d plots done",n+k));
01091
01092 c->Update();
01093 ps->Close();
01094
01095 delete c;
01096 delete ps;
01097
01098
01099 return n+k;
01100 }
01101
01102
01103
01104 BCH2D * BCModel::GetMarginalized(BCParameter * parameter1, BCParameter * parameter2)
01105 {
01106
01107
01108
01109
01110
01111
01112
01113 int index1 = parameter1 -> GetIndex();
01114 int index2 = parameter2 -> GetIndex();
01115
01116 if (index1 > index2)
01117 {
01118 BCLog::Out(BCLog::warning, BCLog::warning,
01119 "BCModel::GetMarginalized. Index 1 has to be smaller than index 2.");
01120 return 0;
01121 }
01122
01123
01124 TH2D * hist = this -> MCMCGetH2Marginalized(index1, index2);
01125
01126 if(hist==0)
01127 return 0;
01128
01129 BCH2D * hprob = new BCH2D();
01130
01131
01132 hist -> SetName(Form("hist_%s_%s_%s", this -> GetName().data(), parameter1 -> GetName().data(), parameter2 -> GetName().data()));
01133 hist -> SetXTitle(Form("%s", parameter1 -> GetName().data()));
01134 hist -> SetYTitle(Form("%s", parameter2 -> GetName().data()));
01135 hist -> SetStats(kFALSE);
01136
01137 double gmode[] = { fBestFitParameters.at(index1), fBestFitParameters.at(index2) };
01138 hprob->SetGlobalMode(gmode);
01139
01140
01141 hprob -> SetHistogram(hist);
01142
01143 return hprob;
01144 }
01145
01146
01147
01148 double BCModel::GetPvalueFromChi2(std::vector<double> par, int sigma_index)
01149 {
01150 double ll = this -> LogLikelihood(par);
01151 int n = this -> GetNDataPoints();
01152
01153 double sum_sigma=0;
01154 for (int i=0;i<n;i++)
01155 sum_sigma += log(this -> GetDataPoint(i) -> GetValue(sigma_index));
01156
01157 double chi2 = -2.*(ll + (double)n/2. * log(2.*M_PI) + sum_sigma);
01158
01159 fPValue = TMath::Prob(chi2,n);
01160
01161 return fPValue;
01162 }
01163
01164
01165
01166 BCH1D * BCModel::CalculatePValue(std::vector<double> par, bool flag_histogram)
01167 {
01168 BCH1D * hist = 0;
01169
01170
01171 BCLog::Out(BCLog::summary, BCLog::summary, "Do goodness-of-fit-test");
01172
01173
01174 BCGoFTest * goftest = new BCGoFTest("modeltest");
01175
01176
01177 goftest -> SetTestModel(this);
01178
01179
01180
01181 if (!goftest -> SetTestPoint(par))
01182 return 0;
01183
01184
01185 goftest -> MCMCSetNChains(fGoFNChains);
01186 goftest -> MCMCSetNIterationsMax(fGoFNIterationsMax);
01187 goftest -> MCMCSetNIterationsRun(fGoFNIterationsRun);
01188
01189
01190 fPValue = goftest -> GetCalculatedPValue(flag_histogram);
01191
01192
01193 if (flag_histogram)
01194 {
01195 hist = new BCH1D();
01196 hist -> SetHistogram(goftest -> GetHistogramLogProb());
01197 }
01198
01199
01200 delete goftest;
01201
01202
01203 return hist;
01204 }
01205
01206
01207
01208 void BCModel::CorrelateDataPointValues(std::vector<double> &x)
01209 {
01210
01211 }
01212
01213
01214
01215 double BCModel::HessianMatrixElement(BCParameter * parameter1, BCParameter * parameter2, std::vector<double> point)
01216 {
01217
01218 if (point.size() != this -> GetNParameters())
01219 {
01220 BCLog::Out(BCLog::warning, BCLog::warning, Form("BCModel::HessianMatrixElement. Invalid number of entries in the vector."));
01221 return -1;
01222 }
01223
01224
01225 double nsteps = 1e5;
01226 double dx1 = (parameter1 -> GetUpperLimit() - parameter1 -> GetLowerLimit()) / nsteps;
01227 double dx2 = (parameter2 -> GetUpperLimit() - parameter2 -> GetLowerLimit()) / nsteps ;
01228
01229
01230 std::vector<double> xpp = point;
01231 std::vector<double> xpm = point;
01232 std::vector<double> xmp = point;
01233 std::vector<double> xmm = point;
01234
01235 xpp.at(parameter1 -> GetIndex()) = xpp.at(parameter1 -> GetIndex()) + dx1;
01236 xpp.at(parameter2 -> GetIndex()) = xpp.at(parameter2 -> GetIndex()) + dx2;
01237
01238 xpm.at(parameter1 -> GetIndex()) = xpm.at(parameter1 -> GetIndex()) + dx1;
01239 xpm.at(parameter2 -> GetIndex()) = xpm.at(parameter2 -> GetIndex()) - dx2;
01240
01241 xmp.at(parameter1 -> GetIndex()) = xmp.at(parameter1 -> GetIndex()) - dx1;
01242 xmp.at(parameter2 -> GetIndex()) = xmp.at(parameter2 -> GetIndex()) + dx2;
01243
01244 xmm.at(parameter1 -> GetIndex()) = xmm.at(parameter1 -> GetIndex()) - dx1;
01245 xmm.at(parameter2 -> GetIndex()) = xmm.at(parameter2 -> GetIndex()) - dx2;
01246
01247
01248 double ppp = this -> Likelihood(xpp);
01249 double ppm = this -> Likelihood(xpm);
01250 double pmp = this -> Likelihood(xmp);
01251 double pmm = this -> Likelihood(xmm);
01252
01253
01254 double derivative = (ppp + pmm - ppm - pmp) / (4.0 * dx1 * dx2);
01255
01256 return derivative;
01257 }
01258
01259
01260
01261 void BCModel::FixDataAxis(unsigned int index, bool fixed)
01262 {
01263
01264 if (index < 0 || index > fDataSet -> GetDataPoint(0) -> GetNValues())
01265 {
01266 BCLog::Out(BCLog::warning, BCLog::warning, "BCModel::FixDataAxis. Index out of range.");
01267 return;
01268 }
01269
01270 if (fDataFixedValues.size() == 0)
01271 fDataFixedValues.assign(fDataSet -> GetDataPoint(0) -> GetNValues(), false);
01272
01273 fDataFixedValues[index] = fixed;
01274 }
01275
01276
01277
01278 bool BCModel::GetFixedDataAxis(unsigned int index)
01279 {
01280
01281 if (index < 0 || index > fDataSet -> GetDataPoint(0) -> GetNValues())
01282 {
01283 BCLog::Out(BCLog::warning, BCLog::warning, "BCModel::GetFixedDataAxis. Index out of range.");
01284 return false;
01285 }
01286
01287 return fDataFixedValues.at(index);
01288 }
01289
01290
01291
01292 void BCModel::PrintSummary()
01293 {
01294 int nparameters = this -> GetNParameters();
01295
01296
01297 std::cout
01298 << std::endl
01299 << " ---------------------------------" << std::endl
01300 << " Model : " << fName.data() << std::endl
01301 << " ---------------------------------"<< std::endl
01302 << " Index : " << fIndex << std::endl
01303 << " Number of parameters : " << nparameters << std::endl
01304 << std::endl
01305 << " - Parameters : " << std::endl
01306 << std::endl;
01307
01308
01309 for (int i=0; i<nparameters; i++)
01310 fParameterSet -> at(i) -> PrintSummary();
01311
01312
01313 if (this -> GetBestFitParameters().size() > 0)
01314 {
01315 std::cout
01316 << std::endl
01317 << " - Best fit parameters :" << std::endl
01318 << std::endl;
01319
01320 for (int i=0; i<nparameters; i++)
01321 {
01322 std::cout
01323 << " " << fParameterSet -> at(i) -> GetName().data()
01324 << " = " << this -> GetBestFitParameter(i)
01325 << " (overall)" << std::endl;
01326 if ((int)fBestFitParametersMarginalized.size() == nparameters)
01327 std::cout
01328 << " " << fParameterSet -> at(i) -> GetName().data()
01329 << " = " << this -> GetBestFitParameterMarginalized(i)
01330 << " (marginalized)" << std::endl;
01331 }
01332 }
01333
01334 std::cout << std::endl;
01335
01336
01337 if (fPValue >= 0)
01338 {
01339 double likelihood = this -> Likelihood(this -> GetBestFitParameters());
01340 std::cout
01341 << " - Model testing:" << std::endl
01342 << std::endl
01343 << " p(data|lambda*) = " << likelihood << std::endl
01344 << " p-value = " << fPValue << std::endl
01345 << std::endl;
01346 }
01347
01348
01349 if (fNormalization > 0)
01350 std::cout << " Normalization : " << fNormalization << std::endl;
01351 }
01352
01353
01354
01355 void BCModel::PrintResults(const char * file)
01356 {
01357
01358
01359
01360 ofstream ofi(file);
01361
01362
01363 if(!ofi.is_open())
01364 {
01365 std::cerr << "Couldn't open file " << file <<std::endl;
01366 return;
01367 }
01368
01369
01370 int npar = fParameterSet -> size();
01371
01372
01373 bool flag_conv = ((this -> MCMCGetNIterationsConvergenceGlobal() > 0)?1:0);
01374
01375 ofi
01376 << std::endl
01377 << " -----------------------------------------------------" << std::endl
01378 << " Summary of the Markov Chain Monte Carlo run" << std::endl
01379 << " -----------------------------------------------------" << std::endl
01380 << std::endl;
01381
01382 if (!flag_conv)
01383 {
01384 ofi
01385 << " WARNING: the Markov Chain did not converge! Be" << std::endl
01386 << " cautious using the following results!" << std::endl
01387 << std::endl;
01388 }
01389
01390 ofi
01391 << " Model summary" << std::endl
01392 << " =============" << std::endl
01393 << " Model: " << fName.data() << std::endl
01394 << " Number of parameters: " << npar << std::endl
01395 << " List of Parameters and ranges:" << std::endl;
01396 for (int i = 0; i < npar; ++i)
01397 {
01398 ofi
01399 << " (" << i << ") Parameter \""
01400 << fParameterSet -> at(i) -> GetName().data() << "\""
01401 << ": " << fParameterSet -> at(i) -> GetLowerLimit()
01402 << " - "
01403 << fParameterSet -> at(i) -> GetUpperLimit() << std::endl;
01404 }
01405 ofi << std::endl;
01406
01407 if (flag_conv)
01408 {
01409 ofi
01410 << " Results of the marginalization" << std::endl
01411 << " ==============================" << std::endl
01412 << " List of parameters and properties of the marginalized" << std::endl
01413 << " distributions:" << std::endl;
01414 for (int i = 0; i < npar; ++i)
01415 {
01416 BCH1D * bch1d = this -> GetMarginalized(fParameterSet -> at(i));
01417
01418 ofi
01419 << " (" << i << ") Parameter \""
01420 << fParameterSet -> at(i) -> GetName().data() << "\"" << std::endl
01421 << " Mean +- RMS: "
01422 << std::setprecision(4) << bch1d -> GetMean()
01423 << " +- "
01424 << std::setprecision(4) << bch1d -> GetRMS() << std::endl
01425 << " Median +- sigma: "
01426 << std::setprecision(4) << bch1d -> GetMedian()
01427 << " + " << std::setprecision(4) << bch1d -> GetQuantile(0.84) - bch1d -> GetMedian()
01428 << " - " << std::setprecision(4) << bch1d -> GetMedian() - bch1d -> GetQuantile(0.16) << std::endl
01429 << " (Marginalized) mode: " << bch1d -> GetMode() << std::endl
01430 << " Smallest interval(s) containing 68% and local modes:" << std::endl;
01431
01432 std::vector <double> v;
01433 v = bch1d -> GetSmallestIntervals(0.68);
01434 int ninter = int(v.size());
01435
01436 for (int j = 0; j < ninter; j+=5)
01437 ofi << " " << v.at(j) << " - " << v.at(j+1) << " (local mode at " << v.at(j+3) << " with rel. height " << v.at(j+2) << "; rel. area " << v.at(j+4) << ")" << std::endl;
01438 }
01439 ofi << std::endl;
01440 }
01441
01442 ofi
01443 << " Results of the optimization" << std::endl
01444 << " ===========================" << std::endl
01445 << " Optimization algorithm used: ";
01446 switch(this -> GetOptimizationMethod())
01447 {
01448 case BCIntegrate::kOptSimulatedAnnealing:
01449 ofi << " Simulated Annealing" << std::endl;
01450 break;
01451 case BCIntegrate::kOptMinuit:
01452 ofi << " Minuit" << std::endl;
01453 break;
01454 case BCIntegrate::kOptMetropolis:
01455 ofi << " MCMC " << std::endl;
01456 break;
01457 }
01458
01459 ofi << " List of parameters and global mode:" << std::endl;
01460 for (int i = 0; i < npar; ++i)
01461 ofi
01462 << " (" << i << ") Parameter \""
01463 << fParameterSet -> at(i) -> GetName().data() << "\": "
01464 << fBestFitParameters.at(i) << std::endl;
01465 ofi << std::endl;
01466
01467 if (fPValue >= 0.)
01468 {
01469 ofi
01470 << " Results of the model test" << std::endl
01471 << " =========================" << std::endl
01472 << " p-value at global mode: " << fPValue << std::endl
01473 << std::endl;
01474 }
01475
01476 ofi
01477 << " Status of the MCMC" << std::endl
01478 << " ==================" << std::endl
01479 << " Convergence reached: " << ((flag_conv)?"yes":"no") << std::endl;
01480
01481 if (flag_conv)
01482 ofi << " Number of iterations until convergence: " << this -> MCMCGetNIterationsConvergenceGlobal() << std::endl;
01483 else
01484 ofi
01485 << " WARNING: the Markov Chain did not converge! Be\n"
01486 << " cautious using the following results!" << std::endl
01487 << std::endl;
01488 ofi
01489 << " Number of chains: " << this -> MCMCGetNChains() << std::endl
01490 << " Number of iterations of each chain: " << this -> MCMCGetNIterationsMax() << std::endl
01491 << std::endl;
01492
01493 ofi
01494 << " -----------------------------------------------------" << std::endl
01495 << std::endl
01496 << " Notes" << std::endl
01497 << " =====" << std::endl
01498 << " (i) Median +- sigma denotes the median, m, of the" << std::endl
01499 << " marginalized distribution and the intervals from" << std::endl
01500 << " m to the 16% and 84% quantiles." << std::endl
01501 << " -----------------------------------------------------" << std::endl;
01502
01503
01504
01505 }
01506
01507
01508
01509 void BCModel::PrintShortFitSummary()
01510 {
01511 BCLog::Out(BCLog::summary, BCLog::summary, "---------------------------------------------------");
01512 BCLog::Out(BCLog::summary, BCLog::summary, Form("Fit summary for model \'%s\':",this -> GetName().data()));
01513 BCLog::Out(BCLog::summary, BCLog::summary, Form(" Number of parameters = %i", this -> GetNParameters()));
01514
01515 BCLog::Out(BCLog::summary, BCLog::summary, " Best fit parameters (global):");
01516 for (unsigned int i = 0; i < this -> GetNParameters(); ++i)
01517 BCLog::Out(BCLog::summary, BCLog::summary, Form(" %s = %.2lf", this -> GetParameter(i) -> GetName().data(), this -> GetBestFitParameter(i)));
01518
01519 BCLog::Out(BCLog::summary, BCLog::summary, " Goodness-of-fit test:");
01520 BCLog::Out(BCLog::summary, BCLog::summary, Form(" p-value = %.2lf", this -> GetPValue()));
01521 BCLog::Out(BCLog::summary, BCLog::summary, "---------------------------------------------------");
01522 }
01523
01524
01525
01526 void BCModel::PrintHessianMatrix(std::vector<double> parameters)
01527 {
01528
01529 if (parameters.size() != this -> GetNParameters())
01530 {
01531 BCLog::Out(BCLog::warning, BCLog::warning,
01532 Form("BCModel::PrintHessianMatrix. Invalid number of entries in the vector."));
01533 return;
01534 }
01535
01536
01537 std::cout
01538 << std::endl
01539 << " Hessian matrix elements: " << std::endl
01540 << " Point: ";
01541
01542 for (int i = 0; i < int(parameters.size()); i++)
01543 std::cout << parameters.at(i) << " ";
01544 std::cout << std::endl;
01545
01546
01547 for (unsigned int i = 0; i < this -> GetNParameters(); i++)
01548 for (unsigned int j = 0; j < i; j++)
01549 {
01550
01551 double hessianmatrixelement = this -> HessianMatrixElement(fParameterSet -> at(i),
01552 fParameterSet -> at(j), parameters);
01553
01554
01555 std::cout << " " << i << " " << j << " : " << hessianmatrixelement << std::endl;
01556 }
01557 }
01558
01559
01560
01561 BCDataPoint * BCModel::VectorToDataPoint(std::vector<double> data)
01562 {
01563 int sizeofvector = int(data.size());
01564 BCDataPoint * datapoint = new BCDataPoint(sizeofvector);
01565 datapoint -> SetValues(data);
01566 return datapoint;
01567 }
01568
01569
01570
01571 int BCModel::CompareStrings(const char * string1, const char * string2)
01572 {
01573 int flag_same = 0;
01574
01575 if (strlen(string1) != strlen(string2))
01576 return -1;
01577
01578 for (int i = 0; i < int(strlen(string1)); i++)
01579 if (string1[i] != string2[i])
01580 flag_same = -1;
01581
01582 return flag_same;
01583 }
01584
01585
01586