11 #include <BAT/BCParameter.h>
13 #include <BAT/BCAux.h>
14 #include <BAT/BCLog.h>
28 const std::string& ModelConf_i,
29 const std::string& MonteCarloConf_i,
30 const std::string& OutFile_i,
31 const std::string& JobTag_i)
32 : myInputParser(ModelF, ThObsF), MCEngine(ModPars, Obs, Obs2D, CGO, CGP) {
36 if (OutFile_i.compare(
"") == 0)
OutFile =
"MCout" +
JobTag +
".root";
56 boost::ptr_vector<Observable>().swap(
Obs);
61 if (rank == 0)
throw std::runtime_error(
"ERROR: MonteCarlo::TestRun() cannot be called after calling MonteCarlo::Run().\n");
69 std::map<std::string, double> DP;
70 if (
Obs.size() == 0 &&
CGO.size() == 0)
throw std::runtime_error(
"\nMonteCarlo::TestRun(): No observables or correlated Gaussian observables defined in SomeModel.conf file\n");
72 for (std::vector<ModelParameter>::iterator it =
ModPars.begin(); it <
ModPars.end(); it++) {
73 if (it->IsCorrelated()) {
74 for (
unsigned int i = 0; i <
CGP.size(); i++) {
75 if (
CGP[i].getName().compare(it->getCgp_name()) == 0) {
76 std::string index = it->getname().substr(
CGP[i].getName().size());
77 long int lindex = strtol(index.c_str(), NULL, 10);
79 DP[
CGP[i].getPar(lindex - 1).getname()] =
CGP[i].getPar(lindex - 1).getave();
81 std::stringstream out;
83 throw std::runtime_error(
"MonteCarlo::Run(): " + out.str() +
"seems to be part of a CorrelatedGaussianParameters object, but I couldn't find the corresponding object");
88 DP[it->getname()] = it->getave();
93 std::cout <<
"\nPlease set the following parameters in the model configuration files:\n" << std::endl;
95 std::cout <<
"ModelParameter\t" << *it << std::endl;
97 std::cout << std::endl;
100 if (rank == 0)
throw std::runtime_error(
"ERROR: Parameter(s) missing in model initialization. \n");
104 if (unknownParameters.size() > 0 && rank == 0) {
105 std::cout <<
"\n" << std::endl;
106 for (std::vector<std::string>::iterator it = unknownParameters.begin(); it != unknownParameters.end(); it++)
107 std::cout <<
"WARNING: unknown parameter " << *it <<
" not added to MCMC Test Run" << std::endl;
109 if (
Obs.size() > 0) std::cout <<
"\nObservables: \n" << std::endl;
110 for (boost::ptr_vector<Observable>::iterator it =
Obs.begin(); it <
Obs.end(); it++) {
111 double th = it->computeTheoryValue();
112 std::cout << it->getName() <<
" = " << th << std::endl;
115 if (
CGO.size() > 0) std::cout <<
"\nCorrelated Gaussian Observables: \n" << std::endl;
116 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin(); it1 <
CGO.end(); it1++) {
117 std::cout << it1->getName() <<
":" << std::endl;
118 std::vector<Observable> ObsInCGO = it1->getObs();
119 for (std::vector<Observable>::iterator it2 = ObsInCGO.begin(); it2 < ObsInCGO.end(); it2++) {
120 double th = it2->computeTheoryValue();
121 std::cout << it2->getName() <<
" = " << th << std::endl;
123 std::cout << std::endl;
130 if (rank == 0)
throw std::runtime_error(
"ERROR: MonteCarlo::Run() cannot be called after calling MonteCarlo::TestRun().\n");
141 std::map<std::string, double> DP;
142 for (std::vector<ModelParameter>::iterator it =
ModPars.begin(); it <
ModPars.end(); it++) {
143 if (it->geterrg() > 0. || it->geterrf() > 0.)
145 if (it->IsCorrelated()) {
146 for (
unsigned int i = 0; i <
CGP.size(); i++) {
147 if (
CGP[i].getName().compare(it->getCgp_name()) == 0) {
148 std::string index = it->getname().substr(
CGP[i].getName().size());
149 long int lindex = strtol(index.c_str(), NULL, 10);
151 DP[
CGP[i].getPar(lindex - 1).getname()] =
CGP[i].getPar(lindex - 1).getave();
153 std::stringstream out;
154 out << it->getname();
155 throw std::runtime_error(
"MonteCarlo::Run(): " + out.str() +
"seems to be part of a CorrelatedGaussianParameters object, but I couldn't find the corresponding object");
160 DP[it->getname()] = it->getave();
163 if (rank == 0)
throw std::runtime_error(
"No parameters being varied. Aborting MCMC run.\n");
169 if (rank == 0) std::cout <<
"\nPlease set the following parameters in the model configuration files:\n" << std::endl;
171 if (rank == 0) std::cout <<
"ModelParameter\t" << *it << std::endl;
173 std::cout << std::endl;
177 if (rank == 0)
throw std::runtime_error(
"\nERROR: " +
ModelName +
" cannot be initialized.\n");
180 int discardbuffer = 0;
182 for (std::vector<ModelParameter>::iterator it =
ModPars.begin(); it <
ModPars.end(); it++) {
183 if (std::find(unknownParameters.begin(), unknownParameters.end(), it->getname()) != unknownParameters.end()) {
184 if (it->geterrg() > 0. || it->geterrf() > 0.) {
186 if (rank == 0) std::cout <<
"\nWARNING: " << it->getname() <<
" has a flat or Gaussian error in the configuration file even though it is an unknown parameter!!!\n";
190 buffsize = buffsize - discardbuffer;
192 if (rank == 0)
throw std::runtime_error(
"No relevant parameters being varied. Aborting MCMC run.\n");
199 if (gSystem->GetPathInfo(
ObsDirName.c_str(), info) != 0) {
200 if (gSystem->MakeDirectory(
ObsDirName.c_str()) == 0) std::cout <<
ObsDirName <<
" directory has been created." << std::endl;
201 else throw std::runtime_error(
"ERROR: " +
ObsDirName +
" directory cannot be created.\n");
207 MCEngine.SetInitialPositionAttemptLimit(10000);
210 double *recvbuff =
new double[buffsize];
215 double **sendbuff =
new double*[1];
216 sendbuff[0] =
new double[1];
218 std::vector<double> pars;
219 int obsbuffsize =
Obs.size() + 2 *
Obs2D.size();
220 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin(); it1 <
CGO.end(); it1++)
221 obsbuffsize += it1->getObs().size();
224 MPI_Scatter(sendbuff[0], buffsize, MPI_DOUBLE, recvbuff, buffsize, MPI_DOUBLE, 0, MPI_COMM_WORLD);
226 if (recvbuff[0] == 0.) {
228 MPI_Gather(&ll, 1, MPI_DOUBLE, sendbuff[0], 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
229 }
else if (recvbuff[0] == 1.) {
230 pars.assign(recvbuff + 1, recvbuff + buffsize);
232 MPI_Gather(&ll, 1, MPI_DOUBLE, sendbuff[0], 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
233 }
else if (recvbuff[0] == 2.) {
234 double sbuff[obsbuffsize];
235 std::map<std::string, double> DPars;
236 pars.assign(recvbuff + 1, recvbuff + buffsize);
241 for (boost::ptr_vector<Observable>::iterator it =
Obs.begin(); it <
Obs.end(); it++) {
242 sbuff[k++] = it->computeTheoryValue();
244 for (std::vector<Observable2D>::iterator it =
Obs2D.begin(); it <
Obs2D.end(); it++) {
245 sbuff[k++] = it->computeTheoryValue();
246 sbuff[k++] = it->computeTheoryValue2();
249 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin(); it1 <
CGO.end(); it1++) {
250 std::vector<Observable> pino(it1->getObs());
251 for (std::vector<Observable>::iterator it = pino.begin(); it != pino.end(); ++it)
252 sbuff[k++] = it->computeTheoryValue();
254 MPI_Gather(sbuff, obsbuffsize, MPI_DOUBLE, sendbuff[0], obsbuffsize, MPI_DOUBLE, 0, MPI_COMM_WORLD);
255 }
else if (recvbuff[0] == 3.) {
256 double sbuff[obsbuffsize];
257 MPI_Gather(sbuff, obsbuffsize, MPI_DOUBLE, sendbuff[0], obsbuffsize, MPI_DOUBLE, 0, MPI_COMM_WORLD);
258 }
else if (recvbuff[0] == -1.)
261 std::cout <<
"recvbuff = " << recvbuff[0] <<
" rank " << rank << std::endl;
262 throw std::runtime_error(
"MonteCarlo::Run(): error in MPI message!\n");
270 if (!
RunMinuitOnly) std::cout << std::endl <<
"\nRunning in MonteCarlo mode...\n" << std::endl;
271 else std::cout << std::endl <<
"\nRunning Minuit Minimizer..." << std::endl;
272 std::cout << std::endl;
273 if (
ModPars.size() > 0) std::cout <<
ModPars.size() - unknownParameters.size() <<
" parameters defined." << std::endl;
274 if (
Obs.size() > 0) std::cout <<
Obs.size() <<
" observables defined." << std::endl;
275 if (
Obs2D.size() > 0) std::cout <<
Obs2D.size() <<
" 2D observables defined." << std::endl;
276 if (
CGO.size() > 0) std::cout <<
CGO.size() <<
" correlated gaussian observables defined:" << std::endl;
277 for (std::vector<CorrelatedGaussianObservables>::iterator it1 =
CGO.begin();
278 it1 !=
CGO.end(); ++it1)
279 std::cout <<
" " << it1->getName() <<
" containing "
280 << it1->getObs().size() <<
" observables." << std::endl;
281 std::cout << std::endl;
284 BCLog::OpenLog((
"MinuitMinimizationResults" +
JobTag +
".txt").c_str(), BCLog::debug, BCLog::debug);
285 RedirectHandle_t * rHandle = NULL;
286 gSystem->RedirectOutput((
"MinuitMinimizationResults" +
JobTag +
".txt").c_str(),
"a", rHandle);
287 std::time_t ti = std::time(NULL);
288 #if __GNUC__ >= 5 || defined __clang__
289 std::cout <<
"\nMinuit Minimization started at " << std::put_time(std::localtime(&ti),
"%c %Z") <<
" (" << ti <<
"s since the Epoch)" <<
"\n" << std::endl;
292 strftime(mitime,
sizeof(mitime),
"%c %Z", std::localtime(&ti));
293 std::cout <<
"\nMinuit Minimization started at " << mitime <<
" (" << ti <<
"s since the Epoch)" <<
"\n" << std::endl;
295 std::cout <<
" " <<std::endl;
296 std::cout <<
"\t\t*** Minimizer Options ***" << std::endl;
297 MCEngine.GetMinuit().Options().Print();
298 std::cout <<
" " <<std::endl;
300 std::time_t tf = std::time(NULL);
301 #if __GNUC__ >= 5 || defined __clang__
302 std::cout <<
"\nMinuit Minimization ended at " << std::put_time(std::localtime(&tf),
"%c %Z") <<
" (" << tf <<
"s since the Epoch)" <<
"\n" << std::endl;
305 strftime(mitime,
sizeof(mftime),
"%c %Z", std::localtime(&tf));
306 std::cout <<
"\nMinuit Minimization ended at " << mftime <<
" (" << tf <<
"s since the Epoch)" <<
"\n" << std::endl;
308 gSystem->RedirectOutput(0);
309 BCLog::OutSummary((
"Minuit results printed in MinuitMinimizationResults" +
JobTag +
".txt").c_str());
310 BCLog::SetPrefix(
false);
311 BCLog::OutSummary(
"");
312 BCLog::SetPrefix(
true);
314 MCEngine.GetMinuit().PrintResults();
315 std::cout << std::endl;
327 MCEngine.InitializeMarkovChainTree();
328 MCEngine.WriteMarkovChainRun(
false);
329 MCEngine.WriteMarkovChainPreRun(
false);
333 std::time_t ti = std::time(NULL);
334 #if __GNUC__ >= 5 || defined __clang__
335 std::cout <<
"\nMCMC run started at " << std::put_time(std::localtime(&ti),
"%c %Z") <<
" (" << ti <<
"s since the Epoch)" <<
"\n" << std::endl;
338 strftime(mitime,
sizeof(mitime),
"%c %Z", std::localtime(&ti));
339 std::cout <<
"\nMCMC run started at " << mitime <<
" (" << ti <<
"s since the Epoch)" <<
"\n" << std::endl;
342 BCLog::OpenLog((
"log" +
JobTag +
".txt").c_str(), BCLog::debug, BCLog::debug);
344 MCEngine.BCIntegrate::SetNbins(NBINSMODELPARS);
345 MCEngine.SetMarginalizationMethod(BCIntegrate::kMargMetropolis);
347 std::time_t tf = std::time(NULL);
348 #if __GNUC__ >= 5 || defined __clang__
349 std::cout <<
"\nMCMC run ended at " << std::put_time(std::localtime(&tf),
"%c %Z") <<
" (" << tf <<
"s since the Epoch)" << std::endl;
352 strftime(mftime,
sizeof(mftime),
"%c %Z", std::localtime(&tf));
353 std::cout <<
"\nMCMC run ended at " << mftime <<
" (" << tf <<
"s since the Epoch)" <<
"\n" << std::endl;
355 int hour = (tf-ti)/3600;
356 int min = ((tf-ti)%3600) / 60;
357 int sec = ((tf-ti)%3600) % 60;
358 std::cout <<
"Time taken by the MCMC run: ";
359 std::cout << std::setfill(
'0') << std::setw(2) << hour <<
":";
360 std::cout << std::setfill(
'0') << std::setw(2) << min <<
":" ;
361 std::cout << std::setfill(
'0') << std::setw(2) <<
sec <<
"\n" << std::endl;
374 MCEngine.PrintCorrelationMatrix((
"CorrelationMatrix" +
JobTag +
".pdf").c_str());
375 MCEngine.PrintCorrelationMatrix((
"CorrelationMatrix" +
JobTag +
".tex").c_str());
389 std::ofstream outHistoLog;
390 outHistoLog.open((
ObsDirName +
"/HistoLog" +
JobTag +
".txt").c_str(), std::ios::out);
395 std::ofstream outStatLog;
396 outStatLog.open((
ObsDirName +
"/Statistics" +
JobTag +
".txt").c_str(), std::ios::out);
402 std::ofstream outPreRun;
403 outPreRun.open((
"PreRun" +
JobTag +
".txt").c_str(), std::ios::out);
409 std::stringstream ss;
411 BCLog::SetPrefix(
false);
412 BCLog::OutSummary(ss.str().c_str());
413 BCLog::SetPrefix(
true);
415 BCLog::OutSummary(ss.str().c_str());
418 BCLog::OutSummary(ss.str().c_str());
423 BCLog::SetPrefix(
false);
424 BCLog::OpenLog((
"MonteCarlo_results" +
JobTag +
".txt").c_str(), BCLog::summary, BCLog::nothing);
430 std::ofstream outStatLog;
431 outStatLog.open((
ObsDirName +
"/Statistics" +
JobTag +
".txt").c_str(), std::ios::app);
440 outStatLog <<
"\nNormalization for " <<
ModelName.c_str() <<
": " << normalizationMC[0] <<
" +- " << normalizationMC[1] <<
"\n" << std::endl;
443 throw std::runtime_error((
"\n ERROR: Normalization method" +
CalculateNormalization +
" not implemented.\n").c_str());
451 BCLog::OpenLog((
"log_CorrelationPlots" +
JobTag +
".txt").c_str(), BCLog::debug, BCLog::debug);
452 MCEngine.PrintCorrelationPlot(
"CorrelationPlots" +
JobTag +
".pdf");
458 double ** sendbuff =
new double *[
MCEngine.procnum];
459 sendbuff[0] =
new double[
MCEngine.procnum * buffsize];
460 for (
int il = 1; il <
MCEngine.procnum; il++) {
461 sendbuff[il] = sendbuff[il - 1] + buffsize;
462 sendbuff[il][0] = -1.;
464 MPI_Scatter(sendbuff[0], buffsize, MPI_DOUBLE, recvbuff, buffsize, MPI_DOUBLE, 0, MPI_COMM_WORLD);
472 }
catch (std::string message) {
473 std::cerr << message << std::endl;
480 std::ifstream ifile(file.c_str());
481 if (!ifile.is_open())
482 throw std::runtime_error(
"\nERROR: " +
MCMCConf +
" does not exist. Make sure to specify a valid Monte Carlo configuration file.\n");
486 IsEOF = getline(ifile, line).eof();
487 if (*line.rbegin() ==
'\r') line.erase(line.length() - 1);
488 if (line.empty() || line.at(0) ==
'#')
490 boost::char_separator<char> sep(
" \t");
491 boost::tokenizer<boost::char_separator<char> > tok(line, sep);
492 boost::tokenizer<boost::char_separator<char> >::iterator beg = tok.begin();
493 if (beg->compare(
"NChains") == 0) {
495 if (isdigit(beg->at(0)) && atoi((*beg).c_str()) > 0)
MCEngine.
setNChains(atoi((*beg).c_str()));
496 else if (isdigit(beg->at(0)) && atoi((*beg).c_str()) == 0)
498 else throw std::runtime_error(
"\nERROR: NChains in the MonteCarlo configuration file: " +
MCMCConf +
" can only be an integer > 0.\n");
500 throw std::runtime_error(
"\nERROR: NChains in the MonteCarlo configuration file: " +
MCMCConf +
" can only be an integer >= 0.\n");
501 }
else if (beg->compare(
"PrerunMaxIter") == 0) {
503 if (isdigit(beg->at(0)) && atoi((*beg).c_str()) > 0)
MCEngine.SetNIterationsPreRunMax(atoi((*beg).c_str()));
505 throw std::runtime_error(
"\nERROR: PrerunMaxIter in the MonteCarlo configuration file: " +
MCMCConf +
" can only be an integer > 0.\n");
506 }
else if (beg->compare(
"NIterationsUpdateMax") == 0) {
508 if (isdigit(beg->at(0)) && atoi((*beg).c_str()) > 0)
MCEngine.SetNIterationsPreRunCheck(atoi((*beg).c_str()));
510 throw std::runtime_error(
"\nERROR: NIterationsUpdateMax in the MonteCarlo configuration file: " +
MCMCConf +
" can only be an integer > 0.\n");
511 }
else if (beg->compare(
"Seed") == 0) {
513 if (!isdigit(beg->at(0)))
throw std::runtime_error(
"\nERROR: Seed in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a number.\n");
514 int seed = atoi((*beg).c_str());
517 }
else if (beg->compare(
"Iterations") == 0) {
519 if (isdigit(beg->at(0)))
MCEngine.SetNIterationsRun(atoi((*beg).c_str()));
521 throw std::runtime_error(
"\nERROR: Iterations in the MonteCarlo configuration file: " +
MCMCConf +
" can only be an integer.\n");
522 }
else if (beg->compare(
"MinimumEfficiency") == 0) {
524 double efficiency = atof((*beg).c_str());
525 if (efficiency > 0. && efficiency <= 1.)
MCEngine.SetMinimumEfficiency(efficiency);
527 throw std::runtime_error(
"\nERROR: MinimumEfficiency in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a real number greater than 0.0 and less than or equal to 1.0.\n");
528 }
else if (beg->compare(
"MaximumEfficiency") == 0) {
530 double efficiency = atof((*beg).c_str());
531 if (efficiency > 0. && efficiency <= 1.)
MCEngine.SetMaximumEfficiency(efficiency);
533 throw std::runtime_error(
"\nERROR: MaximumEfficiency in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a real number greater than 0.0 and less than or equal to 1.0.\n");
534 }
else if (beg->compare(
"RValueForConvergence") == 0) {
536 double rvalue = atof((*beg).c_str());
537 if (rvalue > 1. )
MCEngine.SetRValueParametersCriterion(rvalue);
539 throw std::runtime_error(
"\nERROR: RValueForConvergence in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a real number greater than 1.0.\n");
540 }
else if (beg->compare(
"WriteChain") == 0) {
542 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
writechains = (beg->compare(
"true") == 0);
544 throw std::runtime_error(
"\nERROR: WriteChain in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
545 }
else if (beg->compare(
"FindModeWithMinuit") == 0) {
547 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
FindModeWithMinuit = (beg->compare(
"true") == 0);
549 throw std::runtime_error(
"\nERROR: FindModeWithMinuit in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
550 }
else if (beg->compare(
"RunMinuitOnly") == 0) {
552 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
RunMinuitOnly = (beg->compare(
"true") == 0);
554 throw std::runtime_error(
"\nERROR: RunMinuitOnly in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
555 }
else if (beg->compare(
"CalculateNormalization") == 0) {
559 throw std::runtime_error(
"\nERROR: CalculateNormalization in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
560 }
else if (beg->compare(
"NIterationNormalizationMC") == 0) {
564 throw std::runtime_error(
"\nERROR: NIterationNormalizationMC in the MonteCarlo configuration file: " +
MCMCConf +
" can only be an integer.\n");
565 }
else if (beg->compare(
"PrintAllMarginalized") == 0) {
567 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
PrintAllMarginalized = (beg->compare(
"true") == 0);
569 throw std::runtime_error(
"\nERROR: PrintAllMarginalized in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
570 }
else if (beg->compare(
"PrintCorrelationMatrix") == 0) {
572 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
PrintCorrelationMatrix = (beg->compare(
"true") == 0);
574 throw std::runtime_error(
"\nERROR: PrintCorrelationMatrix in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
575 }
else if (beg->compare(
"PrintKnowledgeUpdatePlots") == 0) {
579 throw std::runtime_error(
"\nERROR: PrintKnowledgeUpdatePlots in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
580 }
else if (beg->compare(
"PrintParameterPlot") == 0) {
582 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
PrintParameterPlot = (beg->compare(
"true") == 0);
584 throw std::runtime_error(
"\nERROR: PrintParameterPlot in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
585 }
else if (beg->compare(
"PrintTrianglePlot") == 0) {
587 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
PrintTrianglePlot = (beg->compare(
"true") == 0);
589 throw std::runtime_error(
"\nERROR: PrintTrianglePlot in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
590 }
else if (beg->compare(
"WritePreRunData") == 0) {
592 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
WritePreRunData = (beg->compare(
"true") == 0);
594 throw std::runtime_error(
"\nERROR: WritePreRunData in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
595 }
else if (beg->compare(
"ReadPreRunData") == 0) {
598 }
else if (beg->compare(
"MultivariateProposal") == 0) {
600 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
MCEngine.SetProposeMultivariate((beg->compare(
"true") == 0));
602 throw std::runtime_error(
"\nERROR: MultivariateProposal in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
603 }
else if (beg->compare(
"Histogram1DSmooth") == 0) {
605 if (beg->compare(
"true") == 0) {
607 }
else if (beg->compare(
"false") == 0) {
609 }
else if (isdigit(beg->at(0))) {
610 if (atoi((*beg).c_str()) >= 0 && atoi((*beg).c_str()) <= 5)
MCEngine.
setSmooth(atoi((*beg).c_str()));
612 throw std::runtime_error(
"\nERROR: Histogram1DSmooth in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true', 'false' or an integer from 0 to 5.\n");
613 }
else if (beg->compare(
"Histogram2DType") == 0) {
615 if (!isdigit(beg->at(0)))
616 throw std::runtime_error(
"\nERROR: Histogram2DType in the MonteCarlo configuration file : " +
MCMCConf +
"can only be an integer amongst 1001 -> Lego, 101 -> Filled, 1 -> Contour.\n");
617 int type = atoi((*beg).c_str());
618 if (type == 1 || type == 101 || type == 1001) {
621 throw std::runtime_error(
"\nERROR: Histogram2DType in the MonteCarlo configuration file : " +
MCMCConf +
"can only be an integer amongst 1001 -> Lego, 101 -> Filled, 1 -> Contour.\n");
622 }
else if (beg->compare(
"MCMCInitialPosition") == 0) {
624 if (beg->compare(
"Center") == 0) {
625 MCEngine.SetInitialPositionScheme(BCEngineMCMC::kInitCenter);
626 }
else if (beg->compare(
"RandomUniform") == 0) {
627 MCEngine.SetInitialPositionScheme(BCEngineMCMC::kInitRandomUniform);
628 }
else if (beg->compare(
"RandomPrior") == 0) {
629 MCEngine.SetInitialPositionScheme(BCEngineMCMC::kInitRandomPrior);
631 throw std::runtime_error(
"\nERROR: MCMCInitialPosition in MonteCarlo config file: " +
MCMCConf +
" can only be 'Center', 'RandomUniform' or 'RandomPrior'.\n");
632 }
else if (beg->compare(
"PrintLogo") == 0) {
634 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
MCEngine.
setPrintLogo((beg->compare(
"true") == 0));
636 throw std::runtime_error(
"\nERROR: PrintLogo in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
637 }
else if (beg->compare(
"NoHistogramLegend") == 0) {
639 if (beg->compare(
"true") == 0 || beg->compare(
"false") == 0)
MCEngine.
setNoLegend((beg->compare(
"true") == 0));
641 throw std::runtime_error(
"\nERROR: PrintLogo in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
642 }
else if (beg->compare(
"PrintLoglikelihoodPlots") == 0) {
646 throw std::runtime_error(
"\nERROR: PrintLoglikelihoodPlots in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
647 if (beg->compare(
"true") == 0) gSystem->MakeDirectory((
ObsDirName +
"/LogLikelihoodPlots").c_str());
648 }
else if (beg->compare(
"WriteLogLikelihoodChain") == 0) {
652 throw std::runtime_error(
"\nERROR: WriteLogLikelihoodChain in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
653 }
else if (beg->compare(
"WriteParametersChains") == 0) {
657 throw std::runtime_error(
"\nERROR: WriteParametersChain in the MonteCarlo configuration file: " +
MCMCConf +
" can only be 'true' or 'false'.\n");
658 }
else if (beg->compare(
"Histogram2DAlpha") == 0) {
660 double alpha = atof((*beg).c_str());
663 throw std::runtime_error(
"\nERROR: Histogram2DAlpha in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a real number greater than 0.0 and less than or equal to 1.0.\n");
664 }
else if (beg->compare(
"NBinsHistogram1D") == 0) {
666 unsigned int nBins1D = atoi((*beg).c_str());
668 else throw std::runtime_error(
"\nERROR: NBinsHistogram1D in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a integer greater than 0 or 0 to set to default value (100).\n");
669 }
else if (beg->compare(
"NBinsHistogram2D") == 0) {
671 unsigned int nBins2D = atoi((*beg).c_str());
673 else throw std::runtime_error(
"\nERROR: NBinsHistogram2D in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a integer greater than 0 or 0 to set to default value (100).\n");
674 }
else if (beg->compare(
"InitialPositionAttemptLimit") == 0) {
676 unsigned int max_tries = atoi((*beg).c_str());
677 if (max_tries > 0)
MCEngine.SetInitialPositionAttemptLimit(max_tries);
678 else if (max_tries == 0)
MCEngine.SetInitialPositionAttemptLimit(10000);
679 else throw std::runtime_error(
"\nERROR: InitialPositionAttemptLimit in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a integer greater than 0 or 0 to set to default value (100).\n");
680 }
else if (beg->compare(
"SignificantDigits") == 0) {
682 unsigned int significants = atoi((*beg).c_str());
684 else throw std::runtime_error(
"\nERROR: SignificantDigits in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a integer greater than 0 or 0 to set to default values.\n");
685 }
else if (beg->compare(
"HistogramBufferSize") == 0) {
687 unsigned int histogramBufferSize = atoi((*beg).c_str());
689 else throw std::runtime_error(
"\nERROR: HistogramBufferSize in the MonteCarlo configuration file: " +
MCMCConf +
" can only be a integer greater than 0 or 0 to set to default values.\n");
691 throw std::runtime_error(
"\nERROR: Wrong keyword in MonteCarlo config file: " +
MCMCConf +
"\n Make sure to specify a valid Monte Carlo configuration file.\n");
694 MCEngine.SetOptimizationMethod(BCIntegrate::kOptMinuit);
696 throw std::runtime_error(
"\nERROR: MaximumEfficiency (default 0.5) must be greater than MaximumEfficiency (default 0.15) in the MonteCarlo configuration file: " +
MCMCConf +
".\n");
698 throw std::runtime_error((
"\nMonteCarlo ERROR: CalculateNormalization cannot be set to MC without setting NIterationNormalizationMC > 0 in " +
MCMCConf +
" .\n").c_str());
703 std::ifstream ifile(file.c_str());
704 if (!ifile.is_open())
705 throw std::runtime_error(
"\nMonteCarlo::ReadPreRunData ERROR: " + file +
" does not exist.\n");
708 std::vector<double> mode;
709 std::vector<double> scale;
711 IsEOF = getline(ifile, line).eof();
714 boost::char_separator<char> sep(
" ");
715 boost::tokenizer<boost::char_separator<char> > tok(line, sep);
716 boost::tokenizer<boost::char_separator<char> >::iterator beg = tok.begin();
718 mode.push_back(atof((*beg).c_str()));
720 scale.push_back(atof((*beg).c_str()));
722 if (mode.size() !=
MCEngine.GetNParameters())
723 throw std::runtime_error(
"\nMonteCarlo::ReadPreRunData ERROR: wrong data size.\n");
724 std::vector<double> mode_all;
725 std::vector<double> scale_all;
726 for (
unsigned int i = 0; i <
MCEngine.GetNChains(); i++){
727 mode_all.insert(mode_all.end(), mode.begin(), mode.end());
728 scale_all.insert(scale_all.end(), scale.begin(), scale.end());
730 MCEngine.SetInitialPositions(mode_all);
731 MCEngine.SetInitialScaleFactors(scale_all);