00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "BAT/BCModelManager.h"
00011
00012 #include "BAT/BCModel.h"
00013 #include "BAT/BCDataSet.h"
00014 #include "BAT/BCDataPoint.h"
00015 #include "BAT/BCLog.h"
00016 #include "BAT/BCErrorCodes.h"
00017
00018 #include <fstream>
00019 #include <iostream>
00020
00021 #include <TString.h>
00022
00023
00024
00025 BCModelManager::BCModelManager()
00026 {
00027 fModelContainer = new BCModelContainer();
00028 fDataSet = 0;
00029 }
00030
00031
00032
00033 BCModelManager::~BCModelManager()
00034 {
00035 delete fModelContainer;
00036
00037 if (fDataSet)
00038 delete fDataSet;
00039 }
00040
00041
00042
00043 BCModelManager::BCModelManager(const BCModelManager & modelmanager)
00044 {
00045 modelmanager.Copy(*this);
00046 }
00047
00048
00049
00050 BCModelManager & BCModelManager::operator = (const BCModelManager & modelmanager)
00051 {
00052 if (this != &modelmanager)
00053 modelmanager.Copy(* this);
00054
00055 return * this;
00056 }
00057
00058
00059
00060 void BCModelManager::SetDataSet(BCDataSet * dataset)
00061 {
00062
00063 fDataSet = dataset;
00064
00065
00066 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00067 this -> GetModel(i) -> SetDataSet(fDataSet);
00068 }
00069
00070
00071
00072 void BCModelManager::SetSingleDataPoint(BCDataPoint * datapoint)
00073 {
00074
00075 BCDataSet * dataset = new BCDataSet();
00076
00077
00078 dataset -> AddDataPoint(datapoint);
00079
00080
00081 this -> SetDataSet(dataset);
00082 }
00083
00084
00085
00086 void BCModelManager::SetSingleDataPoint(BCDataSet * dataset, unsigned int index)
00087 {
00088 if (index < 0 || index > dataset -> GetNDataPoints())
00089 return;
00090
00091 this -> SetSingleDataPoint(dataset -> GetDataPoint(index));
00092 }
00093
00094
00095
00096 void BCModelManager::AddModel(BCModel * model, double probability)
00097 {
00098
00099 unsigned int index = fModelContainer -> size();
00100
00101
00102 model -> SetIndex(index);
00103
00104
00105 model -> SetModelAPrioriProbability(probability);
00106
00107
00108 model -> SetDataSet(fDataSet);
00109
00110
00111 fModelContainer -> push_back(model);
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 void BCModelManager::SetIntegrationMethod(BCIntegrate::BCIntegrationMethod method)
00129 {
00130
00131
00132 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00133 this -> GetModel(i) -> SetIntegrationMethod(method);
00134 }
00135
00136
00137
00138 void BCModelManager::SetMarginalizationMethod(BCIntegrate::BCMarginalizationMethod method)
00139 {
00140
00141 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00142 this -> GetModel(i) -> SetMarginalizationMethod(method);
00143 };
00144
00145
00146
00147 void BCModelManager::SetOptimizationMethod(BCIntegrate::BCOptimizationMethod method)
00148 {
00149
00150 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00151 this -> GetModel(i) -> SetOptimizationMethod(method);
00152 }
00153
00154
00155
00156 void BCModelManager::SetNiterationsPerDimension(unsigned int niterations)
00157 {
00158
00159 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00160 this -> GetModel(i) -> SetNiterationsPerDimension(niterations);
00161 }
00162
00163
00164
00165 void BCModelManager::SetNSamplesPer2DBin(unsigned int n)
00166 {
00167
00168 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00169 this -> GetModel(i) -> SetNSamplesPer2DBin(n);
00170 }
00171
00172
00173
00174 void BCModelManager::SetRelativePrecision(double relprecision)
00175 {
00176
00177 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00178 this -> GetModel(i) -> SetRelativePrecision(relprecision);
00179 }
00180
00181
00182
00183 void BCModelManager::SetNbins(unsigned int n)
00184 {
00185
00186 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00187 this -> GetModel(i) -> SetNbins(n);
00188 }
00189
00190
00191
00192 void BCModelManager::SetFillErrorBand(bool flag)
00193 {
00194 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00195 this -> GetModel(i) -> SetFillErrorBand(flag);
00196 }
00197
00198
00199
00200 void BCModelManager::SetFitFunctionIndexX(int index)
00201 {
00202
00203 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00204 this -> GetModel(i) -> SetFitFunctionIndexX(index);
00205 }
00206
00207
00208
00209 void BCModelManager::SetFitFunctionIndexY(int index)
00210 {
00211
00212 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00213 this -> GetModel(i) -> SetFitFunctionIndexY(index);
00214 }
00215
00216
00217
00218 void BCModelManager::SetFitFunctionIndices(int indexx, int indexy)
00219 {
00220
00221 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00222 this -> GetModel(i) -> SetFitFunctionIndices(indexx, indexy);
00223 }
00224
00225
00226
00227 void BCModelManager::SetDataPointLowerBoundaries(BCDataPoint * datasetlowerboundaries)
00228 {
00229
00230 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00231 this -> GetModel(i) -> SetDataPointLowerBoundaries(datasetlowerboundaries);
00232 }
00233
00234
00235
00236 void BCModelManager::SetDataPointUpperBoundaries(BCDataPoint * datasetupperboundaries)
00237 {
00238
00239 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00240 this -> GetModel(i) -> SetDataPointUpperBoundaries(datasetupperboundaries);
00241 }
00242
00243
00244
00245 void BCModelManager::SetDataPointLowerBoundary(int index, double lowerboundary)
00246 {
00247
00248 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00249 this -> GetModel(i) -> SetDataPointLowerBoundary(index, lowerboundary);
00250 }
00251
00252
00253
00254 void BCModelManager::SetDataPointUpperBoundary(int index, double upperboundary)
00255 {
00256
00257 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00258 this -> GetModel(i) -> SetDataPointUpperBoundary(index, upperboundary);
00259 }
00260
00261
00262
00263 void BCModelManager::SetDataBoundaries(int index, double lowerboundary, double upperboundary)
00264 {
00265
00266 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00267 this -> GetModel(i) -> SetDataBoundaries(index, lowerboundary, upperboundary);
00268 }
00269
00270
00271
00272 void BCModelManager::FixDataAxis(int index, bool fixed)
00273 {
00274
00275 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00276 this -> GetModel(i) -> FixDataAxis(index, fixed);
00277 }
00278
00279
00280
00281 void BCModelManager::SetNChains(unsigned int n)
00282 {
00283
00284 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00285 this -> GetModel(i) -> MCMCSetNChains(n);
00286 }
00287
00288
00289
00290 void BCModelManager::SetFlagPCA(bool flag)
00291 {
00292
00293 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00294 this -> GetModel(i) -> MCMCSetFlagPCA(flag);
00295 }
00296
00297
00298
00299 int BCModelManager::ReadDataFromFileTree(const char * filename, const char * treename, const char * branchnames)
00300 {
00301 if (fModelContainer -> size() < 0)
00302 {
00303 BCLog::Out(BCLog::warning, BCLog::warning, "BCModelManager::ReadDataFromFileTree : No model defined.");
00304 return ERROR_NOMODELS;
00305 }
00306
00307
00308 if (!fDataSet)
00309 fDataSet = new BCDataSet();
00310 else
00311 fDataSet -> Reset();
00312
00313
00314 int read_file = fDataSet -> ReadDataFromFileTree(filename, treename, branchnames);
00315
00316 if (read_file >=0)
00317 {
00318 this -> SetDataSet(fDataSet);
00319
00320 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00321 fModelContainer -> at(i) -> SetDataSet(fDataSet);
00322 }
00323 else if (read_file == ERROR_FILENOTFOUND)
00324 {
00325 delete fDataSet;
00326 return ERROR_FILENOTFOUND;
00327 }
00328
00329 return 0;
00330 }
00331
00332
00333
00334 int BCModelManager::ReadDataFromFileTxt(const char * filename, int nbranches)
00335 {
00336 if (fModelContainer -> size() < 0)
00337 {
00338 BCLog::Out(BCLog::warning, BCLog::warning, "BCModelManager::ReadDataFromFileTree. No model defined.");
00339 return ERROR_NOMODELS;
00340 }
00341
00342
00343 if (!fDataSet)
00344 fDataSet = new BCDataSet();
00345 else
00346 fDataSet -> Reset();
00347
00348
00349 int read_file = fDataSet -> ReadDataFromFileTxt(filename, nbranches);
00350
00351 if (read_file >=0)
00352 {
00353 this -> SetDataSet(fDataSet);
00354
00355 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00356 fModelContainer -> at(i) -> SetDataSet(fDataSet);
00357 }
00358 else
00359 {
00360 delete fDataSet;
00361 return ERROR_FILENOTFOUND;
00362 }
00363
00364 return 0;
00365 }
00366
00367
00368
00369 void BCModelManager::Normalize()
00370 {
00371
00372 double normalization = 0.0;
00373
00374 BCLog::Out(BCLog::summary, BCLog::summary, "Running normalization of all models.");
00375
00376 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00377 {
00378 fModelContainer -> at(i) -> Normalize();
00379
00380
00381 normalization += (fModelContainer -> at(i) -> GetNormalization() *
00382 fModelContainer -> at(i) -> GetModelAPrioriProbability());
00383 }
00384
00385
00386 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00387 fModelContainer -> at(i) -> SetModelAPosterioriProbability(
00388 (fModelContainer -> at(i) -> GetNormalization() *
00389 fModelContainer -> at(i) -> GetModelAPrioriProbability()) /
00390 normalization);
00391 }
00392
00393
00394
00395 void BCModelManager::FindMode()
00396 {
00397
00398 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00399 this -> GetModel(i) -> FindMode();
00400 }
00401
00402
00403
00404 void BCModelManager::MarginalizeAll()
00405 {
00406
00407 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00408 this -> GetModel(i) -> MarginalizeAll();
00409 }
00410
00411
00412
00413 void BCModelManager::WriteMarkovChain(bool flag)
00414 {
00415
00416 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00417 this -> GetModel(i) -> WriteMarkovChain(flag);
00418 }
00419
00420
00421
00422 void BCModelManager::CalculatePValue(bool flag_histogram)
00423 {
00424
00425 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00426 this -> GetModel(i) -> CalculatePValue(this -> GetModel(i) -> GetBestFitParameters(), flag_histogram);
00427 }
00428
00429
00430
00431 void BCModelManager::PrintSummary(const char * file)
00432 {
00433 ofstream out;
00434 std::streambuf * old_buffer = 0;
00435
00436 if(file)
00437 {
00438 out.open(file);
00439 if (!out.is_open())
00440 {
00441 std::cerr<<"Couldn't open file "<<file<<std::endl;
00442 return;
00443 }
00444 old_buffer = std::cout.rdbuf(out.rdbuf());
00445 }
00446
00447
00448 int nmodels = fModelContainer -> size();
00449 std::cout
00450 <<std::endl
00451 <<"======================================"<<std::endl
00452 <<" Summary"<<std::endl
00453 <<"======================================"<<std::endl
00454 <<std::endl
00455 <<" Number of models : "<<nmodels<<std::endl
00456 <<std::endl
00457 <<" - Models:"<<std::endl;
00458
00459 for (int i = 0; i < nmodels; i++)
00460 fModelContainer -> at(i) -> PrintSummary();
00461
00462
00463 std::cout
00464 <<" - Data:"<<std::endl
00465 <<std::endl
00466 <<" Number of entries: "<<fDataSet -> GetNDataPoints()<<std::endl
00467 <<std::endl;
00468
00469 std::cout
00470 <<"======================================"<<std::endl
00471 <<" Model comparison"<<std::endl
00472 <<std::endl;
00473
00474
00475 std::cout
00476 <<" - A priori probabilities:"<<std::endl
00477 <<std::endl;
00478
00479 for (int i=0; i<nmodels; i++)
00480 std::cout
00481 <<" p("<< fModelContainer -> at(i) -> GetName()
00482 <<") = "<< fModelContainer -> at(i) -> GetModelAPrioriProbability()
00483 <<std::endl;
00484 std::cout<<std::endl;
00485
00486 std::cout
00487 <<" - A posteriori probabilities:"<<std::endl
00488 <<std::endl;
00489
00490 for (int i = 0; i < nmodels; i++)
00491 std::cout
00492 <<" p("<< fModelContainer -> at(i) -> GetName()
00493 <<"|data) = "<< fModelContainer -> at(i) -> GetModelAPosterioriProbability()
00494 <<std::endl;
00495 std::cout<<std::endl;
00496
00497 std::cout
00498 <<"======================================"<<std::endl
00499 <<std::endl;
00500
00501 if (file)
00502 std::cout.rdbuf(old_buffer);
00503
00504 }
00505
00506
00507
00508 void BCModelManager::PrintModelComparisonSummary(const char * file)
00509 {
00510 ofstream out;
00511 std::streambuf * old_buffer = 0;
00512
00513 if(file)
00514 {
00515 out.open(file);
00516 if (!out.is_open())
00517 {
00518 std::cerr<<"Couldn't open file "<<file<<std::endl;
00519 return;
00520 }
00521 old_buffer = std::cout.rdbuf(out.rdbuf());
00522 }
00523
00524
00525 int nmodels = fModelContainer -> size();
00526 std::cout
00527 <<std::endl
00528 <<"==========================================="<<std::endl
00529 <<" Model Comparison Summary"<<std::endl
00530 <<"==========================================="<<std::endl
00531 <<std::endl
00532 <<" Number of models : "<<nmodels<<std::endl
00533 <<std::endl;
00534
00535
00536 std::cout
00537 <<" - A priori probabilities:"<<std::endl
00538 <<std::endl;
00539
00540 for (int i=0; i<nmodels; i++)
00541 std::cout
00542 <<" p("<< fModelContainer -> at(i) -> GetName()
00543 <<") = "<< fModelContainer -> at(i) -> GetModelAPrioriProbability()
00544 <<std::endl;
00545 std::cout<<std::endl;
00546
00547 std::cout
00548 <<" - A posteriori probabilities:"<<std::endl
00549 <<std::endl;
00550
00551 for (int i = 0; i < nmodels; i++)
00552 std::cout
00553 <<" p("<< fModelContainer -> at(i) -> GetName()
00554 <<"|data) = "<< fModelContainer -> at(i) -> GetModelAPosterioriProbability()
00555 <<std::endl;
00556 std::cout<<std::endl;
00557
00558 std::cout
00559 <<" - p-values:"<<std::endl
00560 <<std::endl;
00561
00562 for (int i = 0; i < nmodels; i++)
00563 {
00564 double p = fModelContainer -> at(i) -> GetPValue();
00565 std::cout
00566 <<" "<< fModelContainer -> at(i) -> GetName();
00567 if(p>=0.)
00568 std::cout<<": p-value = "<< p;
00569 else
00570 std::cout<<": p-value not calculated";
00571 std::cout<<std::endl;
00572 }
00573 std::cout<<std::endl;
00574
00575 std::cout
00576 <<"==========================================="<<std::endl
00577 <<std::endl;
00578
00579 if (file)
00580 std::cout.rdbuf(old_buffer);
00581
00582 }
00583
00584
00585
00586 void BCModelManager::PrintResults()
00587 {
00588
00589 for (unsigned int i = 0; i < this -> GetNModels(); i++)
00590 this -> GetModel(i) -> PrintResults(Form("%s.txt", this -> GetModel(i) -> GetName().data()));
00591 }
00592
00593
00594
00595 void BCModelManager::Copy(BCModelManager & modelmanager) const
00596 {
00597
00598 modelmanager.fModelContainer = this -> fModelContainer;
00599 modelmanager.fDataSet = this -> fDataSet;
00600 }
00601
00602