MonteCarloEngine.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 HEPfit Collaboration
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  */
7 
8 #ifndef __MONTECARLOENGINE__H
9 #define __MONTECARLOENGINE__H
10 
11 #include "Observable.h"
12 #include "Observable2D.h"
13 #include "CorrelatedGaussianObservables.h"
15 #include "ModelParameter.h"
16 #include "Model.h"
17 #include <BAT/BCModel.h>
18 #include <BAT/BCH1D.h>
19 #include <BAT/BCH2D.h>
20 #include <BAT/BCModelOutput.h>
21 #include <TH1D.h>
22 #include <TH2D.h>
23 #include <TFile.h>
24 #include <TRandom3.h>
25 #include <TPrincipal.h>
26 #include <map>
27 #include <string>
28 #include <sstream>
29 #include <boost/ptr_container/ptr_vector.hpp>
30 
31 #define NBINSMODELPARS 100
32 #define NBINS1D 100
33 #define NBINS2D 100
34 #define NSTEPS 1e5
35 
46 class MonteCarloEngine : public BCModel {
47 public:
48 
56  MonteCarloEngine(const std::vector<ModelParameter>& ModPars_i,
57  boost::ptr_vector<Observable>& Obs_i,
58  std::vector<Observable2D>& Obs2D_i,
59  std::vector<CorrelatedGaussianObservables>& CGO_i,
60  std::vector<CorrelatedGaussianParameters>& CGP_i);
61 
66 
82  void Initialize(Model* Mod_i);
83 
102  void DefineParameters();
103 
115  double LogLikelihood(const std::vector <double>& parameters);
116 
123  void CheckHistogram(const TH1D& hist, const std::string name);
124 
131  void CheckHistogram(const TH2D& hist, const std::string name);
132 
141  void PrintHistogram(BCModelOutput & out, Observable & it, const std::string OutputDir);
142 
150  void PrintHistogram(BCModelOutput& out, const std::string OutputDir);
151 
159  void MCMCIterationInterface();
160 
168  void setNChains(unsigned int i);
169 
176  void AddChains();
177 
182  std::string getHistoLog() const
183  {
184  return HistoLog.str().c_str();
185  }
186 
191  std::string computeStatistics();
192 
197  std::string writePreRunData();
198 
204  void PrintCorrelationMatrix(const std::string filename);
205 
212  {
213  return NumOfDiscardedEvents;
214  }
215 
220  int getNumOfUsedEvents() const
221  {
222  return NumOfUsedEvents;
223  }
224 
225  std::map<std::string, BCH1D * > getHistograms1D() const
226  {
227  return Histo1D;
228  }
229 
230  std::map<std::string, BCH2D * > getHistograms2D() const
231  {
232  return Histo2D;
233  }
234 
235  void setHistogramOverFlow(bool overflow)
236  {
237  if (overflow) TH1::StatOverflows(kTRUE);
238  else TH1::StatOverflows(kFALSE);
239  }
240 
241  double computeNormalization();
242 
243  double SecondDerivative(const BCParameter * par1, const BCParameter * par2, std::vector<double> point);
244 
245  double FirstDerivative(const BCParameter * par, std::vector<double> point);
246 
247  double Function_h(std::vector<double> point);
248 
249  void setDParsFromParameters(const std::vector<double>& parameters, std::map<std::string,double>& DPars_i);
250 
251 private:
252  const std::vector<ModelParameter>& ModPars;
253  const std::vector<CorrelatedGaussianParameters>& CGP;
254  boost::ptr_vector<Observable>& Obs_ALL;
255  std::vector<Observable2D>& Obs2D_ALL;
256  std::vector<CorrelatedGaussianObservables>& CGO;
258  std::map<std::string, double> DPars;
259  std::map<std::string, BCH1D * > Histo1D;
260  std::map<std::string, BCH2D * > Histo2D;
261  std::map<std::string, double> thMin;
262  std::map<std::string, double> thMax;
263  std::map<std::string, TPrincipal *> CorrelationMap;
264  double *obval;
265  double *obweight;
266  unsigned int kwmax;
267  unsigned int kmax;
268  std::ostringstream HistoLog;
271  int rank;
272 
273 };
274 
275 #endif
276 
int NumOfDiscardedEvents
The number of events for which the update of the model fails and these events are not used for the MC...
const std::vector< ModelParameter > & ModPars
A vector of model parameters.
std::map< std::string, double > thMax
A map between the name of a theory observable and its maximum value.
std::vector< Observable2D > & Obs2D_ALL
A vector of all pairs of observable for Observable2D.
A class for the template of models.
Definition: Model.h:24
double FirstDerivative(const BCParameter *par, std::vector< double > point)
boost::ptr_vector< Observable > & Obs_ALL
A vector of all observables.
unsigned int kwmax
The number of observables whose weights are used for the MCMC.
const std::vector< CorrelatedGaussianParameters > & CGP
A vector of correlated Gaussian parameters.
double SecondDerivative(const BCParameter *par1, const BCParameter *par2, std::vector< double > point)
double LogLikelihood(const std::vector< double > &parameters)
This member calculates the loglikelihood for the observables included in the MCMC run...
std::string computeStatistics()
A get method to compute the mean and rms of the computed observables.
std::string getHistoLog() const
A get method to access the stream that stores the log messages coming from histogram printing and che...
int getNumOfDiscardedEvents() const
A get method to access the number of events discarded due to failure to update model. These events are not used for the MCMC run.
std::map< std::string, double > thMin
A map between the name of a theory observable and its minimum value.
double * obval
A pointer to the vector of observable values.
void CheckHistogram(const TH1D &hist, const std::string name)
This member checks if there is overflow of the 1D histogram.
std::map< std::string, double > DPars
A map between parameter names and their values.
void PrintHistogram(BCModelOutput &out, Observable &it, const std::string OutputDir)
Overloaded from PrintHistogram(BCModelOutput&, const std::string) to print histogram for observables...
MonteCarloEngine(const std::vector< ModelParameter > &ModPars_i, boost::ptr_vector< Observable > &Obs_i, std::vector< Observable2D > &Obs2D_i, std::vector< CorrelatedGaussianObservables > &CGO_i, std::vector< CorrelatedGaussianParameters > &CGP_i)
Constructor.
std::map< std::string, BCH1D * > getHistograms1D() const
void AddChains()
A method to add the observable values and weights to the chain information.
A class for observables.
Definition: Observable.h:28
std::map< std::string, BCH2D * > getHistograms2D() const
void setDParsFromParameters(const std::vector< double > &parameters, std::map< std::string, double > &DPars_i)
An engine class for Monte Carlo.
void setNChains(unsigned int i)
A set method to fix the number of chains.
std::string writePreRunData()
A method to write in a text file the best fit parameters and the prerun scale factors.
int NumOfUsedEvents
The number of events for which the model is successfully updated and hence used for the MCMC run...
~MonteCarloEngine()
The default destructor. Some pointers defined in this class are explicitly freed. ...
int rank
Rank of the process for a MPI run. Value is 0 for a serial run.
void DefineParameters()
A member to classify the prior of the model parameters varied in the Monte Carlo. ...
std::map< std::string, TPrincipal * > CorrelationMap
A map between the name of a theory observable and its maximum value.
void setHistogramOverFlow(bool overflow)
std::map< std::string, BCH2D * > Histo2D
A map between pointers to objects of type BCH2D (BAT) and their given names.
void PrintCorrelationMatrix(const std::string filename)
This member generates the correlation matrix using BCH2D from the BAT libraries.
double Function_h(std::vector< double > point)
double * obweight
A pointer to the vector of observable weights.
unsigned int kmax
The number of observables.
void Initialize(Model *Mod_i)
Initialization of the Monte Carlo Engine.
void MCMCIterationInterface()
Overloaded from BCEngineMCMC in BAT
std::map< std::string, BCH1D * > Histo1D
A map between pointers to objects of type BCH1D (BAT) and their given names.
std::ostringstream HistoLog
A stream to store the output messages from printing and checking histograms.
int getNumOfUsedEvents() const
A get method to access the number of events used in the MCMC run.
Model * Mod
A pointer to an abject of type Model.
std::vector< CorrelatedGaussianObservables > & CGO
A vector of correlated Gaussian observables.