5 #include "Utils/IOUtils.h"
24 void MulticanonicalMCMCHandler::setDebugStream(std::ostream* os,
bool enabled) {
26 debugEnabled = enabled;
31 bool foundVariableOfInterest =
false;
32 bool foundDelm23 =
false;
36 MACH3LOG_INFO(
"Number of systematics: {}", systematics.size());
38 for (
size_t iCov = 0; iCov < systematics.size(); iCov++){
39 auto* syst = systematics[
static_cast<int>(iCov)];
40 for (
int i = 0; i < syst->GetNumParams(); i++) {
46 foundVariableOfInterest =
true;
48 if (syst->GetParName(i) ==
"delm2_23") {
49 MACH3LOG_INFO(
"Found delm2_23 parameter in systematic {} at index {}", syst->GetName(), i);
58 if (!foundVariableOfInterest) {
63 MACH3LOG_ERROR(
"Could not find delm2_23 parameter in osc_cov systematic");
64 throw MaCh3Exception(__FILE__, __LINE__,
"Could not find delm2_23 parameter in osc_cov systematic");
69 const auto mcmcConfig = fitMan->
raw()[
"General"][
"MCMC"];
71 multicanonicalVarName = Get<std::string>(mcmcConfig[
"Multicanonical"][
"VariableName"], __FILE__, __LINE__);
77 multicanonicalBeta = Get<double>(mcmcConfig[
"Multicanonical"][
"Beta"], __FILE__, __LINE__);
82 multicanonicalSpline = GetFromManager<bool>(mcmcConfig[
"Multicanonical"][
"Spline"][
"SplineMode"],
false, __FILE__, __LINE__);
84 const std::string biasFunctionName = GetFromManager<std::string>(mcmcConfig[
"Multicanonical"][
"Umbrella"][
"UmbrellaBiasFunction"],
"", __FILE__, __LINE__);
85 const bool hasBiasFunction = !biasFunctionName.empty();
89 MACH3LOG_ERROR(
"Cannot use multicanonical spline together with umbrella bias function selection.");
90 throw MaCh3Exception(__FILE__, __LINE__,
"Cannot use multicanonical spline together with umbrella bias function selection.");
95 MACH3LOG_ERROR(
"Multicanonical umbrella mode requires UmbrellaBiasFunction to be set (gaussian, vonMises, or generalisedGaussian).");
96 throw MaCh3Exception(__FILE__, __LINE__,
"Multicanonical umbrella mode requires UmbrellaBiasFunction to be set.");
100 if (hasBiasFunction) {
108 auto splineFileName = Get<std::string>(mcmcConfig[
"Multicanonical"][
"Spline"][
"SplineFile"], __FILE__, __LINE__);
109 TFile* splineFile = M3::Open(splineFileName.c_str(),
"READ",__FILE__, __LINE__);
112 TSpline3* dcp_spline_IO_fromfile =
static_cast<TSpline3*
>(splineFile->Get(
"dcp_spline_IO"));
113 dcp_spline_IO =
static_cast<TSpline3*
>(dcp_spline_IO_fromfile->Clone(
"dcp_spline_IO"));
114 MACH3LOG_INFO(
"Using multicanonical spline from file {}", splineFileName);
118 TSpline3* dcp_spline_NO_fromfile =
static_cast<TSpline3*
>(splineFile->Get(
"dcp_spline_NO"));
119 dcp_spline_NO =
static_cast<TSpline3*
>(dcp_spline_NO_fromfile->Clone(
"dcp_spline_NO"));
120 MACH3LOG_INFO(
"Using multicanonical spline from file {}", splineFileName);
125 splineFile =
nullptr;
129 umbrellaMean = GetFromManager<double>(mcmcConfig[
"Multicanonical"][
"Umbrella"][
"UmbrellaMean"], 0, __FILE__, __LINE__);
134 auto umbrellaNumber = GetFromManager<int>(mcmcConfig[
"Multicanonical"][
"Umbrella"][
"UmbrellaNumber"], 5, __FILE__, __LINE__);
139 auto umbrellaOverlapMode = GetFromManager<bool>(mcmcConfig[
"Multicanonical"][
"Umbrella"][
"AutoOverlapMode"],
false, __FILE__, __LINE__);
141 if (umbrellaOverlapMode) {
142 MACH3LOG_INFO(
"Setting width based on # of sigma overlapping between umbrellas");
143 auto umbrellaSigmaOverlap = GetFromManager<double>(mcmcConfig[
"Multicanonical"][
"Umbrella"][
"SigmaOverlap"], 3.0, __FILE__, __LINE__);
144 MACH3LOG_INFO(
"Setting umbrella number to {}", umbrellaNumber);
145 umbrellaWidth = TMath::Pi() / ((umbrellaNumber - 1) * (umbrellaSigmaOverlap));
148 umbrellaWidth = GetFromManager<double>(mcmcConfig[
"Multicanonical"][
"Umbrella"][
"UmbrellaWidth"], (2 * TMath::Pi()) / umbrellaNumber, __FILE__, __LINE__);
154 umbrellaAdjustStepScale = GetFromManager<bool>(mcmcConfig[
"Multicanonical"][
"Umbrella"][
"UmbrellaAdjustStepScale"],
false, __FILE__, __LINE__);
155 umbrellaStepScaleFactor = GetFromManager<double>(mcmcConfig[
"Multicanonical"][
"Umbrella"][
"UmbrellaStepScaleFactor"], 1.0, __FILE__, __LINE__);
168 double temp_vonMises_sigma;
169 temp_vonMises_sigma = GetFromManager<double>(mcmcConfig[
"Multicanonical"][
"Umbrella"][
"UmbrellaWidth"],
umbrellaWidth, __FILE__, __LINE__);
170 vonMises_kappa = 1.0 / (temp_vonMises_sigma * temp_vonMises_sigma);
182 MACH3LOG_INFO(
"Adjusting umbrella step scale to keep ratio of step scale to multicanonical sigma constant");
185 MACH3LOG_INFO(
"Setting individual step scale for multicanonical separate to {}", stepScale);
189 MACH3LOG_INFO(
"Not adjusting umbrella step scale, using value in OscCov config");
196 systematics[
oscCovVar]->PrintPreFitCurrPropValues();
207 systematics[
oscCovVar]->PrintPreFitCurrPropValues();
251 const double inv_sqrt_2pi = 1 / std::sqrt(2 * TMath::Pi());
261 double likelihood = normFactor * std::exp(-std::pow((std::pow(x - mean, 2) / (2 * std::pow(width, 2))), n));
266 return std::atan2(std::sin(x - mean), std::cos(x - mean));
283 constexpr
double inv_sqrt_2pi = 0.3989422804014337;
285 const double neg_half_sigma_sq = -1 / (2 * sigma * sigma);
287 double exp1 = std::exp(neg_half_sigma_sq * (param_value - TMath::Pi()) * (param_value - TMath::Pi()));
288 double exp2 = std::exp(neg_half_sigma_sq * (param_value) * (param_value));
289 double exp3 = std::exp(neg_half_sigma_sq * (param_value + TMath::Pi()) * (param_value + TMath::Pi()));
294 return -std::log(inv_sqrt_2pi * (1 / sigma) * (exp1 + exp2 + exp3)) * (
multicanonicalBeta);
Custom exception class used throughout MaCh3.
The manager class is responsible for managing configurations and settings.
YAML::Node const & raw() const
Return config.
double vonMises_I0_kappa
Cached I0(kappa) value for the von Mises form.
void InitializeMulticanonicalParams(std::vector< ParameterHandlerBase * > &systematics)
Initialise the starting values used by the multicanonical parameter handling.
void InitializeMulticanonicalHandlerConfig(Manager *fitMan, std::vector< ParameterHandlerBase * > &systematics)
Read multicanonical configuration from the yaml via manager and initialise handler state.
double GetMulticanonicalWeightVonMises(double param_value) const
Compute a von Mises multicanonical penalty.
double GetMulticanonicalWeightGaussian(double param_value) const
Compute a Gaussian multicanonical penalty.
double umbrellaWidth
Umbrella width used for the current configuration.
TSpline3 * dcp_spline_IO
Spline for the IO branch, if spline mode is enabled.
void AdjustUmbrellaStepScale(const std::vector< ParameterHandlerBase * > &systematics)
Adjust the parameter-of-interest's step scale according to the width of the umbrella bias function.
int multicanonicalVar_dm23
Parameter index used for the multicanonical delm2_23 weight.
double GetMulticanonicalWeightSpline(double param_value, double delm23) const
Compute the multicanonical penalty using a spline.
M3::BiasFunction umbrellaBiasFunction
Selected bias function for multicanonical weights.
TSpline3 * dcp_spline_NO
Spline for the NO branch, if spline mode is enabled.
std::string multicanonicalVarName
Parameter name used for the multicanonical weight, most often delta_cp.
double GetMulticanonicalWeight() const
Compute the multicanonical penalty for the configured bias mode.
bool multicanonicalSpline
Toggle for spline-based multicanonical weights.
void FindOscCovParams(const std::vector< ParameterHandlerBase * > &systematics)
Locate the systematic object which contains the parameters used by the handler. Stores the parameter ...
double vonMises_kappa
Von Mises kappa parameter. Analogue of sigma for a gaussian.
double circularDistance(double x, double mean) const
Compute the circular distance between two angles.
MulticanonicalMCMCHandler()
Constructor.
std::string umbrellaBiasFunctionName
Configured bias function name for logging.
double GetMulticanonicalWeightTripleGaussian(double param_value) const
Compute a triple-Gaussian multicanonical penalty.
const M3::float_t * multicanonicalVarValue
Pointer to value of parameter vale used for multicanonical weight, most often delta_cp.
double generalisedGaussian2(double x, double mean, double width) const
Wraps the generalised gaussian function for a given x, mean, and width. Required to handle the wrappi...
double umbrellaMean
Umbrella centre used for the current configuration.
double GetMulticanonicalWeightGenGaussian(double param_value) const
Compute a generalised-Gaussian multicanonical penalty.
const M3::float_t * multicanonicalVar_dm23_value
Pointer to value of delm2_23 weight used for multicanonical weight.
virtual ~MulticanonicalMCMCHandler()
Destructor.
double umbrellaStepScaleFactor
Additional scale factor applied when rescaling the step size. This is for fine tuning.
int oscCovVar
Index of the oscillation-covariance systematic in the current fit.
int multicanonicalVar
Parameter index used for the multicanonical weight, most often delta_cp.
bool umbrellaAdjustStepScale
Toggle for rescaling the step size based on the umbrella width.
double multicanonicalBeta
Global scale factor applied to the multicanonical penalty. 1 is full strength, 0 is no penalty.
M3::BiasFunction ParseBiasFunction(const std::string &biasFunctionName)
constexpr static const double _BAD_DOUBLE_
Default value used for double initialisation.
constexpr double UmbrellaGaussianNormFactor
@ kGaussian
Assumes gaussian prior.