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