Skip to content

Commit

Permalink
Merge pull request #221 from mach3-software/feature_FactoryFix
Browse files Browse the repository at this point in the history
Tidy Factory
  • Loading branch information
KSkwarczynski authored Nov 19, 2024
2 parents 4109fc9 + 082a44a commit 0c8cedc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 66 deletions.
61 changes: 1 addition & 60 deletions mcmc/MaCh3Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


// ********************************************
std::unique_ptr<FitterBase> MaCh3FitterFactory(manager *fitMan, std::vector<samplePDFBase*>& Samples, std::vector<covarianceBase*>& Covariances) {
std::unique_ptr<FitterBase> MaCh3FitterFactory(manager *fitMan) {
// ********************************************
std::unique_ptr<FitterBase> MaCh3Fitter = nullptr;

Expand All @@ -24,68 +24,9 @@ std::unique_ptr<FitterBase> MaCh3FitterFactory(manager *fitMan, std::vector<samp
MACH3LOG_ERROR("You want to use algorithm {}, I don't recognize it, sry", Algorithm);
throw MaCh3Exception(__FILE__ , __LINE__ );
}

//KS: Adding samples and covariances to the Fitter class could be in the factory
for(unsigned int i = 0; Samples.size(); i++)
MaCh3Fitter->addSamplePDF(Samples[i]);
for(unsigned int i = 0; Covariances.size(); i++)
MaCh3Fitter->addSystObj(Covariances[i]);

return MaCh3Fitter;
}

// ********************************************
template <typename CovType>
CovType* MaCh3CovarianceFactory(manager *FitManager, const std::string& PreFix){
// ********************************************

// config for our matrix
YAML::Node Settings = FitManager->raw()["General"]["Systematics"];
auto CovMatrixName = Settings[std::string(PreFix) + "CovName"].as<std::string>();
MACH3LOG_INFO("Initialising {} matrix", CovMatrixName);

// yaml files initialising out matrix
auto CovMatrixFile = Settings[std::string(PreFix) + "CovFile"].as<std::vector<std::string>>();

// PCA threshold, -1 means no pca
auto PCAThreshold = GetFromManager<int>(Settings[std::string(PreFix) + "PCAThreshold"], -1);
// do we pca whole matrix or only submatrix
auto PCAParamRegion = GetFromManager<std::vector<int>>(Settings[std::string(PreFix) + "PCAParams"], {-999, -999});

/// @todo this massive hack with "xsec_cov" is because we have const char * ... will have to fix it later...
CovType* CovObject = new CovType(CovMatrixFile, "xsec_cov", PCAThreshold, PCAParamRegion[0], PCAParamRegion[1]);

// Fill the parameter values with their nominal values
// should _ALWAYS_ be done before overriding with fix or flat
CovObject->setParameters();

auto FixParams = GetFromManager<std::vector<std::string>>(Settings[std::string(PreFix) + "FixParams"], {});

// Fixed CovObject parameters loop
if (FixParams.size() == 1 && FixParams.at(0) == "All") {
for (int j = 0; j < CovObject->GetNumParams(); j++) {
CovObject->toggleFixParameter(j);
}
} else {
for (unsigned int j = 0; j < FixParams.size(); j++) {
CovObject->toggleFixParameter(FixParams.at(j));
}
}
//Global step scale for matrix
auto StepScale = Settings[std::string(PreFix) + "StepScale"].as<double>();

CovObject->setStepScale(StepScale);

// Adaptive MCMC stuff
if(FitManager->raw()["AdaptionOptions"])
CovObject->initialiseAdaption(FitManager->raw());

MACH3LOG_INFO("Factory successful");

return CovObject;
}


// ********************************************
covarianceXsec* MaCh3CovarianceFactory(manager *FitManager, const std::string& PreFix) {
// ********************************************
Expand Down
58 changes: 52 additions & 6 deletions mcmc/MaCh3Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
/// @code
/// General:
/// FittingAlgorithm: ["MCMC"]
std::unique_ptr<FitterBase> MaCh3FitterFactory(manager *fitMan, std::vector<samplePDFBase>& Samples, std::vector<covarianceBase>& Covariances);
std::unique_ptr<FitterBase> MaCh3FitterFactory(manager *fitMan);

/// @brief Factory function for creating a covariance class for systematic handling.
covarianceXsec* MaCh3CovarianceFactory(manager *FitManager, const std::string& PreFix);


// ********************************************
/// @brief Factory function for creating a covariance class for systematic handling.
///
/// @param fitMan Pointer to the manager class that holds the configuration settings.
Expand All @@ -51,12 +55,54 @@ std::unique_ptr<FitterBase> MaCh3FitterFactory(manager *fitMan, std::vector<samp
///
/// @todo add adaptive stuff
template <typename CovType>
CovType* MaCh3CovarianceFactory(manager *FitManager, const std::string& PreFix);
CovType* MaCh3CovarianceFactory(manager *FitManager, const std::string& PreFix){
// ********************************************
// config for our matrix
YAML::Node Settings = FitManager->raw()["General"]["Systematics"];
auto CovMatrixName = Settings[std::string(PreFix) + "CovName"].as<std::string>();
MACH3LOG_INFO("Initialising {} matrix", CovMatrixName);

/// @brief Factory function for creating a covariance class for systematic handling.
covarianceXsec* MaCh3CovarianceFactory(manager *FitManager, const std::string& PreFix);
// yaml files initialising out matrix
auto CovMatrixFile = Settings[std::string(PreFix) + "CovFile"].as<std::vector<std::string>>();

// PCA threshold, -1 means no pca
auto PCAThreshold = GetFromManager<int>(Settings[std::string(PreFix) + "PCAThreshold"], -1);
// do we pca whole matrix or only submatrix
auto PCAParamRegion = GetFromManager<std::vector<int>>(Settings[std::string(PreFix) + "PCAParams"], {-999, -999});

CovType* CovObject = new CovType(CovMatrixFile, CovMatrixName, PCAThreshold, PCAParamRegion[0], PCAParamRegion[1]);

// Fill the parameter values with their nominal values
// should _ALWAYS_ be done before overriding with fix or flat
CovObject->setParameters();

auto FixParams = GetFromManager<std::vector<std::string>>(Settings[std::string(PreFix) + "FixParams"], {});

// Fixed CovObject parameters loop
if (FixParams.size() == 1 && FixParams.at(0) == "All") {
for (int j = 0; j < CovObject->GetNumParams(); j++) {
CovObject->toggleFixParameter(j);
}
} else {
for (unsigned int j = 0; j < FixParams.size(); j++) {
CovObject->toggleFixParameter(FixParams.at(j));
}
}
//Global step scale for matrix
auto StepScale = Settings[std::string(PreFix) + "StepScale"].as<double>();

CovObject->setStepScale(StepScale);

// Adaptive MCMC stuff
if(FitManager->raw()["AdaptionOptions"])
CovObject->initialiseAdaption(FitManager->raw());

MACH3LOG_INFO("Factory successful");

return CovObject;
}

// ********************************************
/// @brief Factory function for creating SamplePDF and initialisation with systematic.
///
/// @tparam SampleType The class type of the sample to create, e.g., `samplePDFTutorial`.
Expand All @@ -72,8 +118,8 @@ covarianceXsec* MaCh3CovarianceFactory(manager *FitManager, const std::string& P
template <typename SampleType>
std::vector<SampleType*> MaCh3SamplePDFFactory(const std::vector<std::string>& SampleConfig,
covarianceXsec* xsec,
covarianceOsc* osc = nullptr)
{
covarianceOsc* osc = nullptr) {
// ********************************************
std::vector<SampleType*> PDFs(SampleConfig.size());
for (size_t i = 0; i < SampleConfig.size(); ++i)
{
Expand Down

0 comments on commit 0c8cedc

Please sign in to comment.