15 MACH3LOG_ERROR(
"MCMC chain length must be at least 10 steps, otherwise this will result in a floating point exception.");
19 AnnealTemp = GetFromManager<double>(
fitMan->
raw()[
"General"][
"MCMC"][
"AnnealTemp"], -999, __FILE__ , __LINE__);
35 multicanonical = GetFromManager<bool>(
fitMan->
raw()[
"General"][
"MCMC"][
"Multicanonical"][
"Enabled"],
false, __FILE__, __LINE__);
92 if (syst->GetDoAdaption())
94 auto adaptive_handler = syst->GetAdaptiveHandler();
95 adaptive_handler->SaveAdaptiveToFile(adaptive_handler->GetOutFileName(), syst->GetName(),
true);
129 int originalErrorLevel = gErrorIgnoreLevel;
130 gErrorIgnoreLevel = kFatal;
144 gErrorIgnoreLevel = originalErrorLevel;
154 for (
size_t i = 0; i <
samples.size(); ++i) {
159 cov->PrintPreFitCurrPropValues();
164 debugFile <<
"\n-------------------------------------------------------" << std::endl;
177 if(
step -
stepStart < std::min(10.0,
static_cast<double>(StepEnd)/10)) {
187 if (acc_rate < 0.01) {
188 MACH3LOG_WARN(
"Acceptance rate is very low: {:.4f}. This may indicate that the proposal distribution is not well-tuned. Consider reducing the step size.", acc_rate);
189 }
else if (acc_rate > 0.5) {
190 MACH3LOG_WARN(
"Acceptance rate is very high: {:.4f}. This may indicate that the proposal distribution is too narrow. Consider increasing the step sizes.", acc_rate);
201 TFile *infile =
M3::Open(FitName,
"READ", __FILE__, __LINE__);
202 TTree *posts = infile->Get<TTree>(
"posteriors");
203 unsigned int step_val = 0;
205 posts->SetBranchAddress(
"step", &step_val);
206 posts->GetEntry(posts->GetEntries() - 1);
210 for (
unsigned int i = 0; i <
systematics.size(); ++i) {
225 if (syst->GetDoAdaption()){
226 syst->UpdateAdaptiveCovariance();
238 debugFile <<
" logLProp: " <<
logLProp <<
" logLCurr: " <<
logLCurr <<
" acc_prob: " << acc_prob <<
" fRandom: " << fRandom << std::endl;
241 if (fRandom > acc_prob)
266 std::string OutName =
"";
268 OutName +=
"MultiCanonical_";
271 for(
unsigned int i = 0; i <
systematics.size(); ++i){
273 OutName +=
"Adaptive_";
278 OutName += originalName;
#define MACH3LOG_CRITICAL
Base class for implementing fitting algorithms.
std::string GetName() const
Get name of class.
double logLProp
proposed likelihood
void ProcessMCMC()
Process MCMC output.
int accCount
counts accepted steps
void SaveOutput()
Save output and close files.
void SaveSettings()
Save the settings that the MCMC was run with.
unsigned int step
current state
void PrepareOutput()
Prepare the output file.
virtual void StartFromPreviousFit(const std::string &FitName)
Allow to start from previous fit/chain.
std::vector< SampleHandlerInterface * > samples
Sample holder.
double stepTime
Time of single step.
Manager * fitMan
The manager for configuration handling.
unsigned int stepStart
step start, by default 0 if we start from previous chain then it will be different
std::unique_ptr< TStopwatch > stepClock
tells how long single step/fit iteration took
double logLCurr
current likelihood
int auto_save
auto save every N steps
TTree * outTree
Output tree with posteriors.
void SanitiseInputs()
Remove obsolete memory and make other checks before fit starts.
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
std::unique_ptr< MulticanonicalMCMCHandler > multicanonicalHandler
multicanonical handler for umbrella sampling
MCMCBase(Manager *const fitMan)
Constructor.
bool anneal
simulated annealing
void RunMCMC() final
Actual implementation of MCMC fitting algorithm.
unsigned int chainLength
number of steps in chain
void StartFromPreviousFit(const std::string &FitName) final
Allow to start from previous fit/chain.
void AcceptStep()
Accept a step.
double AnnealTemp
simulated annealing temperature
virtual std::string GetFancyName() const final
This name is to get include some special algorithm extensions.
bool out_of_bounds
Do we reject based on hitting boundaries in systs.
bool IsStepAccepted(const double acc_prob)
Is step accepted?
void DoMCMCStep()
The full StartStep->DoStep->EndStep chain.
void PreStepProcess()
Actions before step proposal [start stopwatch].
bool multicanonical
multi-canonical method toggle on/off
virtual void ProposeStep()=0
Propose a step.
void CheckAcceptanceRates()
Check the acceptance rates.
void PrintProgress(const bool StepsPrint=true)
Print the progress.
virtual void DoStep()=0
The MCMC step proposal and acceptance.
void PostStepProcess()
Actions after step proposal [end stopwatch, fill tree].
void AdaptiveStep()
Adaptive MCMC step.
Custom exception class used throughout MaCh3.
The manager class is responsible for managing configurations and settings.
YAML::Node const & raw() const
Return config.
Base class for handling systematic uncertainty parameters.
double Uniform(const double min=0.0, const double max=1.0)
Generate a uniform random number in [min, max).
TFile * Open(const std::string &Name, const std::string &Type, const std::string &File, const int Line)
Opens a ROOT file with the given name and mode.