Skip to content

Commit

Permalink
Merge pull request #234 from mach3-software/featrue_random
Browse files Browse the repository at this point in the history
Random
  • Loading branch information
KSkwarczynski authored Dec 7, 2024
2 parents f1ceb1b + bfc77ed commit a7acf30
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Diagnostics/DiagMCMC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ void DiagMCMC(const std::string& inputFile, const std::string& config)
auto Processor = std::make_unique<MCMCProcessor>(inputFile);
Processor->SetOutputSuffix("_MCMC_Diag");
//KS:Turn off plotting detector and some other setting
Processor->SetExcludedTypes(GetFromManager<std::vector<std::string>>(Settings["DiagMCMC"]["ExcludedTypes"], {""}));
Processor->SetExcludedNames(GetFromManager<std::vector<std::string>>(Settings["DiagMCMC"]["ExcludedNames"], {""}));
Processor->SetExcludedTypes(GetFromManager<std::vector<std::string>>(Settings["DiagMCMC"]["ExcludedTypes"], {}));
Processor->SetExcludedNames(GetFromManager<std::vector<std::string>>(Settings["DiagMCMC"]["ExcludedNames"], {}));
Processor->SetPlotRelativeToPrior(GetFromManager<bool>(Settings["DiagMCMC"]["PlotRelativeToPrior"], false));
//KS: Use 20 batches for batched means
Processor->SetnBatches(GetFromManager<int>(Settings["DiagMCMC"]["nBatches"], 20));
Expand Down
8 changes: 4 additions & 4 deletions Diagnostics/ProcessMCMC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ void ProcessMCMC(const std::string& inputFile)

const bool PlotCorr = GetFromManager<bool>(Settings["PlotCorr"], false);

Processor->SetExcludedTypes(GetFromManager<std::vector<std::string>>(Settings["ExcludedTypes"], {""}));
Processor->SetExcludedNames(GetFromManager<std::vector<std::string>>(Settings["ExcludedNames"], {""}));
Processor->SetExcludedTypes(GetFromManager<std::vector<std::string>>(Settings["ExcludedTypes"], {}));
Processor->SetExcludedNames(GetFromManager<std::vector<std::string>>(Settings["ExcludedNames"], {}));
//Apply additional cuts to 1D posterior
Processor->SetPosterior1DCut(GetFromManager<std::string>(Settings["Posterior1DCut"], ""));

Expand Down Expand Up @@ -166,8 +166,8 @@ void MultipleProcessMCMC()
Processor[ik] = std::make_unique<MCMCProcessor>(FileNames[ik]);
Processor[ik]->SetOutputSuffix(("_" + std::to_string(ik)).c_str());

Processor[ik]->SetExcludedTypes(GetFromManager<std::vector<std::string>>(Settings["ExcludedTypes"], {""}));
Processor[ik]->SetExcludedNames(GetFromManager<std::vector<std::string>>(Settings["ExcludedNames"], {""}));
Processor[ik]->SetExcludedTypes(GetFromManager<std::vector<std::string>>(Settings["ExcludedTypes"], {}));
Processor[ik]->SetExcludedNames(GetFromManager<std::vector<std::string>>(Settings["ExcludedNames"], {}));

//Apply additional cuts to 1D posterior
Processor[ik]->SetPosterior1DCut(GetFromManager<std::string>(Settings["Posterior1DCut"], ""));
Expand Down
25 changes: 13 additions & 12 deletions covariance/covarianceBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ covarianceBase::~covarianceBase(){
// ********************************************
void covarianceBase::ConstructPCA() {
// ********************************************

//Check whether first and last pcadpar are set and if not just PCA everything
if(FirstPCAdpar == -999 || LastPCAdpar == -999){
if(FirstPCAdpar == -999 && LastPCAdpar == -999){
Expand Down Expand Up @@ -122,7 +121,6 @@ void covarianceBase::init(std::string name, std::string file) {
throw MaCh3Exception(__FILE__ , __LINE__ );
}


TMatrixDSym *CovMat = infile->Get<TMatrixDSym>(name.c_str());

if (CovMat == nullptr) {
Expand Down Expand Up @@ -250,6 +248,10 @@ void covarianceBase::init(const std::vector<std::string>& YAMLFile) {
//ETA - now for parameters which are optional and have default values
_fFlatPrior[i] = GetFromManager<bool>(param["Systematic"]["FlatPrior"], false);

// Allow to fix param, this setting should be used only for params which are permanently fixed like baseline, please use global config for fixing param more flexibly
if(GetFromManager<bool>(param["Systematic"]["FixParam"], false)) {
toggleFixParameter(_fFancyNames[i]);
}
//Fill the map to get the correlations later as well
CorrNamesMap[param["Systematic"]["Names"]["FancyName"].as<std::string>()]=i;

Expand Down Expand Up @@ -531,7 +533,7 @@ void covarianceBase::throwParameters() {
void covarianceBase::RandomConfiguration() {
// *************************************
// Have the 1 sigma for each parameter in each covariance class, sweet!
// Don't want to change the nominal array because that's what determines our likelihood
// Don't want to change the prior array because that's what determines our likelihood
// Want to change the fParProp, fParCurr, fParInit
// fParInit and the others will already be set
for (int i = 0; i < _fNumPar; ++i) {
Expand Down Expand Up @@ -769,7 +771,7 @@ void covarianceBase::throwParCurr(const double mag) {
}
}
// ********************************************
// Function to print the nominal values
// Function to print the prior values
void covarianceBase::printNominal() {
// ********************************************
MACH3LOG_INFO("Prior values for {} covarianceBase:", getName());
Expand All @@ -779,7 +781,7 @@ void covarianceBase::printNominal() {
}

// ********************************************
// Function to print the nominal, current and proposed values
// Function to print the prior, current and proposed values
void covarianceBase::printNominalCurrProp() {
// ********************************************
MACH3LOG_INFO("Printing parameters for {}", getName());
Expand Down Expand Up @@ -861,12 +863,12 @@ void covarianceBase::printPars() {
}

// ********************************************
// Sets the proposed parameters to the nominal values
// Sets the proposed parameters to the prior values
void covarianceBase::setParameters(const std::vector<double>& pars) {
// ********************************************
// If empty, set the proposed to nominal
// If empty, set the proposed to prior
if (pars.empty()) {
// For xsec this means setting to the prior (because nominal is the prior)
// For xsec this means setting to the prior (because prior is the prior)
for (int i = 0; i < _fNumPar; i++) {
_fPropVal[i] = _fPreFitValue[i];
}
Expand Down Expand Up @@ -1273,7 +1275,6 @@ void covarianceBase::makeClosestPosDef(TMatrixDSym *cov) {
throw MaCh3Exception(__FILE__ , __LINE__ );
}


TMatrixD cov_sym_v = cov_sym_svd.GetV();
TMatrixD cov_sym_vt = cov_sym_v;
cov_sym_vt.T();
Expand Down Expand Up @@ -1302,12 +1303,12 @@ void covarianceBase::makeClosestPosDef(TMatrixDSym *cov) {
// ********************************************
std::vector<double> covarianceBase::getNominalArray() {
// ********************************************
std::vector<double> nominal(_fNumPar);
std::vector<double> prior(_fNumPar);
for (int i = 0; i < _fNumPar; ++i)
{
nominal[i] = _fPreFitValue[i];
prior[i] = _fPreFitValue[i];
}
return nominal;
return prior;
}

// ********************************************
Expand Down
8 changes: 4 additions & 4 deletions covariance/covarianceBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class covarianceBase {
//Some Getters
/// @brief Get total number of parameters
inline int GetNumParams() {return _fNumPar;}
/// @brief Get the nominal array for parameters.
/// @brief Get the prior array for parameters.
virtual std::vector<double> getNominalArray();
/// @brief Get the pre-fit values of the parameters.
std::vector<double> getPreFitValues(){return _fPreFitValue;}
Expand All @@ -194,7 +194,7 @@ class covarianceBase {
/// @brief Get prior parameter value
/// @param i Parameter index
inline double getParInit(const int i) { return _fPreFitValue[i]; }
/// @brief Return generated value, although is virtual so class inheriting might actual get nominal not generated.
/// @brief Return generated value, although is virtual so class inheriting might actual get prior not generated.
/// @param i Parameter index
virtual double getNominal(const int i) { return getParInit(i); }
/// @brief Return generated value for a given parameter
Expand Down Expand Up @@ -290,9 +290,9 @@ class covarianceBase {
else return _fNumPar;
}

/// @brief Print nominal value for every parameter
/// @brief Print prior value for every parameter
void printNominal();
/// @brief Print nominal, current and proposed value for each parameter
/// @brief Print prior, current and proposed value for each parameter
void printNominalCurrProp();
void printPars();
/// @brief Print step scale for each parameter
Expand Down
10 changes: 5 additions & 5 deletions covariance/covarianceXsec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ void covarianceXsec::Print() {
void covarianceXsec::PrintGlobablInfo() {
// ********************************************
MACH3LOG_INFO("============================================================================================================================================================");
MACH3LOG_INFO("{:<5} {:2} {:<40} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<5} {:2} {:<10}", "#", "|", "Name", "|", "Nom.", "|", "Prior", "|", "Error", "|", "Lower", "|", "Upper", "|", "StepScale", "|", "DetID", "|", "Type");
MACH3LOG_INFO("{:<5} {:2} {:<40} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<5} {:2} {:<10}", "#", "|", "Name", "|", "Gen.", "|", "Prior", "|", "Error", "|", "Lower", "|", "Upper", "|", "StepScale", "|", "DetID", "|", "Type");
MACH3LOG_INFO("------------------------------------------------------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < GetNumParams(); i++) {
std::string ErrString = fmt::format("{:.2f}", _fError[i]);
Expand Down Expand Up @@ -594,7 +594,7 @@ void covarianceXsec::DumpMatrixToFile(const std::string& Name) {
TVectorD* xsec_param_prior = new TVectorD(_fNumPar);
TVectorD* xsec_flat_prior = new TVectorD(_fNumPar);
TVectorD* xsec_stepscale = new TVectorD(_fNumPar);
TVectorD* xsec_param_nom = new TVectorD(_fNumPar);
TVectorD* xsec_param_generated = new TVectorD(_fNumPar);
TVectorD* xsec_param_lb = new TVectorD(_fNumPar);
TVectorD* xsec_param_ub = new TVectorD(_fNumPar);

Expand All @@ -616,7 +616,7 @@ void covarianceXsec::DumpMatrixToFile(const std::string& Name) {
xsec_spline_names->AddLast(splineName);

(*xsec_param_prior)[i] = _fPreFitValue[i];
(*xsec_param_nom)[i] = _fGenerated[i];
(*xsec_param_generated)[i] = _fGenerated[i];
(*xsec_flat_prior)[i] = _fFlatPrior[i];
(*xsec_stepscale)[i] = _fIndivStepScale[i];
(*xsec_error)[i] = _fError[i];
Expand Down Expand Up @@ -656,8 +656,8 @@ void covarianceXsec::DumpMatrixToFile(const std::string& Name) {
delete xsec_flat_prior;
xsec_stepscale->Write("xsec_stepscale");
delete xsec_stepscale;
xsec_param_nom->Write("xsec_param_nom");
delete xsec_param_nom;
xsec_param_generated->Write("xsec_param_nom");
delete xsec_param_generated;
xsec_param_lb->Write("xsec_param_lb");
delete xsec_param_lb;
xsec_param_ub->Write("xsec_param_ub");
Expand Down
11 changes: 5 additions & 6 deletions covariance/covarianceXsec.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,16 @@ class covarianceXsec : public covarianceBase {
/// @brief DB Get norm/func parameters depending on given DetID
const std::vector<XsecNorms4> GetNormParsFromDetID(const int DetID);

/// @brief KS: For most covariances nominal and fparInit (prior) are the same, however for Xsec those can be different
/// For example Sigma Var are done around nominal in ND280, no idea why though...
/// @brief KS: For most covariances prior and fparInit (prior) are the same, however for Xsec those can be different
std::vector<double> getNominalArray() override
{
std::vector<double> nominal(_fNumPar);
std::vector<double> prior(_fNumPar);
for (int i = 0; i < _fNumPar; i++) {
nominal[i] = _fPreFitValue.at(i);
prior[i] = _fPreFitValue.at(i);
}
return nominal;
return prior;
}
/// @brief Get nominal for a given param
/// @brief Get prior for a given param
/// @param i parameter index
inline double getNominal(const int i) override { return _fPreFitValue.at(i); };

Expand Down
9 changes: 1 addition & 8 deletions mcmc/FitterBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ void FitterBase::addSystObj(covarianceBase * const cov) {
return;
}


// *******************
void FitterBase::StartFromPreviousFit(const std::string& FitName) {
// *******************
Expand Down Expand Up @@ -402,7 +401,6 @@ void FitterBase::ProcessMCMC() {
// Run Drag Race
void FitterBase::DragRace(const int NLaps) {
// *************************

MACH3LOG_INFO("Let the Race Begin!");
// Reweight the MC
for(unsigned int ivs = 0; ivs < samples.size(); ivs++ )
Expand Down Expand Up @@ -464,7 +462,6 @@ void FitterBase::DragRace(const int NLaps) {
// Run LLH scan
void FitterBase::RunLLHScan() {
// *************************

// Save the settings into the output file
SaveSettings();

Expand Down Expand Up @@ -1138,11 +1135,7 @@ void FitterBase::RunSigmaVar() {
double paramVal = cov->getParInit(i)+sigmaArray[j]*std::sqrt((*Cov)(i,i));

// Check the bounds on the parameter
if (paramVal > cov->GetUpperBound(i)) {
paramVal = cov->GetUpperBound(i);
} else if (paramVal < cov->GetLowerBound(i)) {
paramVal = cov->GetLowerBound(i);
}
paramVal = std::max(cov->GetLowerBound(i), std::min(paramVal, cov->GetUpperBound(i)));

// Set the parameter
cov->setParProp(i, paramVal);
Expand Down
1 change: 0 additions & 1 deletion samplePDF/Structs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "TList.h"
#include "TObjArray.h"
#include "manager/MaCh3Logger.h"

namespace MaCh3Utils {

Expand Down
Loading

0 comments on commit a7acf30

Please sign in to comment.