Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature tidy #113

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# For more see: https://github.com/actions/labeler?tab=readme-ov-file#pull-request-labeler
MCMC:
- changed-files:
- any-glob-to-any-file:
Expand Down Expand Up @@ -29,3 +30,25 @@ Documentation:
- changed-files:
- any-glob-to-any-file:
- Doc/**
Cmake:
- changed-files:
- any-glob-to-any-file:
- cmake/**
- CMakeLists.txt

GPU:
- changed-files:
- any-glob-to-any-file:
- '**/*.cu'
- '**/*.cuh'

CI/CD:
- changed-files:
- any-glob-to-any-file:
- .github/**

python:
- changed-files:
- any-glob-to-any-file:
- '**/*.py'

11 changes: 3 additions & 8 deletions covariance/AdaptiveMCMCHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ AdaptiveMCMCHandler::~AdaptiveMCMCHandler() {
}
}


// ********************************************
bool AdaptiveMCMCHandler::InitFromConfig(const YAML::Node& adapt_manager, const std::string& matrix_name_str, const int Npars) {
// ********************************************
Expand Down Expand Up @@ -86,7 +85,6 @@ void AdaptiveMCMCHandler::SetAdaptiveBlocks(std::vector<std::vector<int>> block_
int block_lb = block_indices[iblock][isubblock];
int block_ub = block_indices[iblock][isubblock+1];

//std::cout<<block_lb<<" "<<block_ub<<std::endl;
if(block_lb > Npars || block_ub > Npars){
MACH3LOG_ERROR("Cannot set matrix block with edges {}, {} for matrix of size {}",
block_lb, block_ub, Npars);
Expand All @@ -101,15 +99,14 @@ void AdaptiveMCMCHandler::SetAdaptiveBlocks(std::vector<std::vector<int>> block_
}
}


// ********************************************
//HW: Truly adaptive MCMC!
void AdaptiveMCMCHandler::SaveAdaptiveToFile(const TString& outFileName, const TString& systematicName){
// ********************************************
TFile* outFile = new TFile(outFileName, "UPDATE");
if(outFile->IsZombie()){
MACH3LOG_ERROR("Couldn't find {}", outFileName);
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}
TVectorD* outMeanVec = new TVectorD((int)par_means.size());
for(int i = 0; i < (int)par_means.size(); i++){
Expand Down Expand Up @@ -139,14 +136,14 @@ void AdaptiveMCMCHandler::SetThrowMatrixFromFile(const std::string& matrix_file_

if(matrix_file->IsZombie()){
MACH3LOG_ERROR("Couldn't find {}", matrix_file_name);
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}

// Next we grab our matrix
adaptive_covariance = static_cast<TMatrixDSym*>(matrix_file->Get(matrix_name.c_str()));
if(!adaptive_covariance){
MACH3LOG_ERROR("Couldn't find {} in {}", matrix_name, matrix_file_name);
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}

// Finally we grab the means vector
Expand Down Expand Up @@ -178,7 +175,6 @@ void AdaptiveMCMCHandler::SetThrowMatrixFromFile(const std::string& matrix_file_
MACH3LOG_INFO("Set up matrix from external file");
}


// ********************************************
void AdaptiveMCMCHandler::UpdateAdaptiveCovariance(const std::vector<double>& _fCurrVal, const int steps_post_burn, const int Npars) {
// ********************************************
Expand Down Expand Up @@ -226,5 +222,4 @@ void AdaptiveMCMCHandler::Print() {
MACH3LOG_INFO("Steps Between Updates : {}", adaptive_update_step);
}


} //end adaptive_mcmc
2 changes: 1 addition & 1 deletion covariance/AdaptiveMCMCHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace adaptive_mcmc{

/// @brief Contains information about adaptive covariance matrix
/// @see An adaptive Metropolis algorithm, H.Haario et al., 2001 for more info!
///@details struct encapsulating all adaptive MCMC information
/// @details struct encapsulating all adaptive MCMC information
class AdaptiveMCMCHandler{
public:

Expand Down
1 change: 1 addition & 0 deletions covariance/PCAHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#endif

/// @brief Class responsible for handling Principal Component Analysis (PCA) of covariance matrix
class PCAHandler{
public:

Expand Down
61 changes: 31 additions & 30 deletions covariance/covarianceBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void covarianceBase::ConstructPCA() {
if (covMatrix == NULL) {
MACH3LOG_ERROR("Covariance matrix for {} has not yet been set", matrixName);
MACH3LOG_ERROR("Can not construct PCA until it is set");
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}

//Check whether first and last pcadpar are set and if not just PCA everything
Expand All @@ -132,18 +132,18 @@ void covarianceBase::ConstructPCA() {
}
else{
MACH3LOG_ERROR("You must either leave FirstPCAdpar and LastPCAdpar at -999 or set them both to something");
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}
}
if(FirstPCAdpar > covMatrix->GetNrows()-1 || LastPCAdpar>covMatrix->GetNrows()-1){
MACH3LOG_ERROR("FirstPCAdpar and LastPCAdpar are higher than the number of parameters");
MACH3LOG_ERROR("first: {} last: {}, params: {}", FirstPCAdpar, LastPCAdpar, covMatrix->GetNrows()-1);
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}
if(FirstPCAdpar < 0 || LastPCAdpar < 0){
MACH3LOG_ERROR("FirstPCAdpar and LastPCAdpar are less than 0 but not default -999");
MACH3LOG_ERROR("first: {} last: {}", FirstPCAdpar, LastPCAdpar);
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}

PCAObj.ConstructPCA(covMatrix, FirstPCAdpar, LastPCAdpar, eigen_threshold, _fNumParPCA);
Expand Down Expand Up @@ -177,7 +177,7 @@ void covarianceBase::init(const char *name, const char *file) {
if (infile->IsZombie()) {
MACH3LOG_ERROR("Could not open input covariance ROOT file {} !!!", file);
MACH3LOG_ERROR("Was about to retrieve matrix with name {}", name);
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}

// Should put in a
Expand Down Expand Up @@ -223,7 +223,7 @@ void covarianceBase::init(const char *name, const char *file) {
setCovMatrix(CovMat);
if (_fNumPar <= 0) {
MACH3LOG_CRITICAL("Covariance matrix {} has {} entries!", getName(), _fNumPar);
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}
_fNumParPCA = _fNumPar;

Expand Down Expand Up @@ -367,7 +367,7 @@ void covarianceBase::init(const std::vector<std::string>& YAMLFile) {

if (_fNumPar <= 0) {
MACH3LOG_ERROR("Covariance object has {} systematics!", _fNumPar);
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}
_fNumParPCA = _fNumPar;

Expand Down Expand Up @@ -570,7 +570,7 @@ void covarianceBase::throwParameters() {
MACH3LOG_WARN("Setting _fPropVal: {} to {}", _fPropVal[i], _fPreFitValue[i]);
MACH3LOG_WARN("I live at {}:{}", __FILE__, __LINE__);
_fPropVal[i] = _fPreFitValue[i];
//throw;
//throw MaCh3Exception(__FILE__ , __LINE__ );
}
throws++;
}
Expand All @@ -581,7 +581,7 @@ void covarianceBase::throwParameters() {
{
MACH3LOG_CRITICAL("Hold on, you are trying to run Prior Predicitve Code with PCA, which is wrong");
MACH3LOG_CRITICAL("Sorry I have to kill you, I mean your job");
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}
}

Expand Down Expand Up @@ -701,7 +701,6 @@ void covarianceBase::randomize() {
}
}


// ************************************************
// Correlate the steps by setting the proposed step of a parameter to its current value + some correlated throw
void covarianceBase::CorrelateSteps() {
Expand Down Expand Up @@ -841,7 +840,6 @@ void covarianceBase::printNominal() {
}
}


// ********************************************
// Function to print the nominal, current and proposed values
void covarianceBase::printNominalCurrProp() {
Expand Down Expand Up @@ -901,6 +899,7 @@ int covarianceBase::CheckBounds() {
}
return NOutside;
}

// ********************************************
double covarianceBase::GetLikelihood() {
// ********************************************
Expand All @@ -920,7 +919,7 @@ void covarianceBase::printPars() {
MACH3LOG_INFO("Number of pars: {}", _fNumPar);
MACH3LOG_INFO("Current {} parameters:", matrixName);
for(int i = 0; i < _fNumPar; i++) {
std::cout << std::fixed << std::setprecision(5) << _fNames[i].c_str() << " current: \t" << _fCurrVal[i] << " \tproposed: \t" << _fPropVal[i] << std::endl;
MACH3LOG_INFO("{:s} current: \t{:.5f} \tproposed: \t{:.5f}", _fNames[i], _fCurrVal[i], _fPropVal[i]);
}
return;
}
Expand All @@ -937,20 +936,19 @@ void covarianceBase::setParameters(const std::vector<double>& pars) {
}
// If not empty, set the parameters to the specified
} else {
if (pars.size() != size_t(_fNumPar)) {
if (pars.size() != size_t(_fNumPar)) {
MACH3LOG_ERROR("Warning: parameter arrays of incompatible size! Not changing parameters! {} has size {} but was expecting {}", matrixName, pars.size(), _fNumPar);
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}

unsigned int parsSize = pars.size();
for (unsigned int i = 0; i < parsSize; i++) {
//Make sure that you are actually passing a number to set the parameter to
if(std::isnan(pars[i])) {
MACH3LOG_ERROR("Error: trying to set parameter value to a nan for parameter {} in matrix {}. This will not go well!", GetParName(i), matrixName);
throw;
} else {
_fPropVal[i] = pars[i];
}
MACH3LOG_ERROR("Error: trying to set parameter value to a nan for parameter {} in matrix {}. This will not go well!", GetParName(i), matrixName);
throw MaCh3Exception(__FILE__ , __LINE__ );
} else {
_fPropVal[i] = pars[i];
}
}
}
// And if pca make the transfer
Expand All @@ -964,7 +962,6 @@ void covarianceBase::setParameters(const std::vector<double>& pars) {
// ********************************************
void covarianceBase::SetBranches(TTree &tree, bool SaveProposal) {
// ********************************************

// loop over parameters and set a branch
for (int i = 0; i < _fNumPar; ++i) {
tree.Branch(_fNames[i].c_str(), &_fCurrVal[i], Form("%s/D", _fNames[i].c_str()));
Expand Down Expand Up @@ -997,7 +994,7 @@ void covarianceBase::setStepScale(const double scale) {
if(scale == 0)
{
MACH3LOG_ERROR("You are trying so set StepScale to 0 this will not work");
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}
MACH3LOG_INFO("{} setStepScale() = {}", getName(), scale);
_fGlobalStepScale = scale;
Expand All @@ -1023,7 +1020,7 @@ void covarianceBase::toggleFixParameter(const int i) {
if (i > _fNumPar) {
MACH3LOG_ERROR("Can't toggleFixParameter for parameter {} because size of covariance ={}", i, _fNumPar);
MACH3LOG_ERROR("Fix this in your config file please!");
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
} else {
_fError[i] *= -1.0;
MACH3LOG_INFO("Setting {}(parameter {}) to fixed at {}", GetParFancyName(i), i, _fCurrVal[i]);
Expand All @@ -1035,7 +1032,7 @@ void covarianceBase::toggleFixParameter(const int i) {
}
if(isDecom < 0) {
MACH3LOG_ERROR("Parameter {} is PCA decomposed can't fix this", GetParName(i));
//throw;
//throw MaCh3Exception(__FILE__ , __LINE__ );
} else {
fParSigma_PCA[isDecom] *= -1.0;
MACH3LOG_INFO("Setting un-decomposed {}(parameter {}/{} in PCA base) to fixed at {}", GetParName(i), i, isDecom, _fCurrVal[i]);
Expand Down Expand Up @@ -1074,7 +1071,7 @@ void covarianceBase::setFlatPrior(const int i, const bool eL) {
if (i > _fNumPar) {
MACH3LOG_INFO("Can't setEvalLikelihood for Cov={}/Param={} because size of Covariance = {}", getName(), i, _fNumPar);
MACH3LOG_ERROR("Fix this in your config file please!");
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
} else {
if(eL){
MACH3LOG_INFO("Setting {} (parameter {}) to flat prior", GetParName(i), i);
Expand Down Expand Up @@ -1146,6 +1143,7 @@ void covarianceBase::printIndivStepScale() {
}
std::cout << "============================================================" << std::endl;
}

// ********************************************
//Makes sure that matrix is positive-definite by adding a small number to on-diagonal elements
void covarianceBase::MakePosDef(TMatrixDSym *cov) {
Expand Down Expand Up @@ -1183,7 +1181,7 @@ void covarianceBase::MakePosDef(TMatrixDSym *cov) {
if (!CanDecomp) {
MACH3LOG_ERROR("Tried {} times to shift diagonal but still can not decompose the matrix", MaxAttempts);
MACH3LOG_ERROR("This indicates that something is wrong with the input matrix");
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}
if(total_steps < 2) {
MACH3LOG_INFO("Had to shift diagonal {} time(s) to allow the covariance matrix to be decomposed", iAttempt);
Expand All @@ -1193,6 +1191,7 @@ void covarianceBase::MakePosDef(TMatrixDSym *cov) {

return;
}

// ********************************************
void covarianceBase::resetIndivStepScale() {
// ********************************************
Expand All @@ -1204,8 +1203,10 @@ void covarianceBase::resetIndivStepScale() {
setIndivStepScale(stepScales);
}

// ********************************************
// HW: Code for throwing from separate throw matrix, needs to be set after init to ensure pos-def
void covarianceBase::setThrowMatrix(TMatrixDSym *cov){
// ********************************************
if (cov == NULL) {
MACH3LOG_ERROR("Could not find covariance matrix you provided to setThrowMatrix");
throw MaCh3Exception(__FILE__ , __LINE__ );
Expand Down Expand Up @@ -1234,7 +1235,7 @@ void covarianceBase::setThrowMatrix(TMatrixDSym *cov){

//KS: ROOT has bad memory management, using standard double means we can decrease most operation by factor 2 simply due to cache hits
#ifdef MULTITHREAD
#pragma omp parallel for
#pragma omp parallel for collapse(2)
#endif
for (int i = 0; i < _fNumPar; ++i)
{
Expand All @@ -1244,6 +1245,7 @@ void covarianceBase::setThrowMatrix(TMatrixDSym *cov){
}
}
}

// ********************************************
void covarianceBase::updateThrowMatrix(TMatrixDSym *cov){
// ********************************************
Expand Down Expand Up @@ -1332,7 +1334,7 @@ void covarianceBase::updateAdaptiveCovariance(){
resetIndivStepScale();
}

if(total_steps>=AdaptiveHandler.start_adaptive_throw && (total_steps-AdaptiveHandler.start_adaptive_throw)%AdaptiveHandler.adaptive_update_step==0) {
if(total_steps >= AdaptiveHandler.start_adaptive_throw && (total_steps-AdaptiveHandler.start_adaptive_throw)%AdaptiveHandler.adaptive_update_step==0) {
TMatrixDSym* update_matrix = static_cast<TMatrixDSym*>(AdaptiveHandler.adaptive_covariance->Clone());
updateThrowMatrix(update_matrix); //Now we update and continue!
}
Expand Down Expand Up @@ -1360,7 +1362,7 @@ void covarianceBase::makeClosestPosDef(TMatrixDSym *cov) {
TDecompSVD cov_sym_svd=TDecompSVD(cov_sym);
if(!cov_sym_svd.Decompose()){
MACH3LOG_ERROR("Cannot do SVD on input matrix!");
throw;
throw MaCh3Exception(__FILE__ , __LINE__ );
}

TMatrixD cov_sym_v = (TMatrixD)cov_sym_svd.GetV();
Expand Down Expand Up @@ -1454,4 +1456,3 @@ void covarianceBase::SaveUpdatedMatrixConfig() {
fout << copyNode;
fout.close();
}

4 changes: 2 additions & 2 deletions samplePDF/ShiftFunctors.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class BaseFuncPar{
public:
BaseFuncPar(){};
~BaseFuncPar();
virtual ~BaseFuncPar();
double ScaleUncertainty = 1.0;
void SetUncertainty(double sigma){ScaleUncertainty = sigma;}
double *Param_pos;
Expand All @@ -29,7 +29,7 @@ class SKEScale : public BaseFuncPar{
class EnergyScale : public BaseFuncPar{
public:
EnergyScale(){};
~EnergyScale();
virtual ~EnergyScale();
void Apply(){};

protected:
Expand Down
Loading