Skip to content

Commit

Permalink
Now it fully works
Browse files Browse the repository at this point in the history
  • Loading branch information
KSkwarczynski committed Nov 6, 2024
1 parent c7dfa37 commit 42d763b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Diagnostics/ProcessMCMC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ void ProcessMCMC(const std::string& inputFile)
Processor->SetPost2DPlotThreshold(GetFromManager<double>(Settings["Post2DPlotThreshold"], 0.2));

Processor->Initialise();

if(Settings["Thinning"])
{
bool DoThin = Settings["Thinning"][0].as<bool>();
if(DoThin) Processor->ThinMCMC(Settings["Thinning"][1].as<int>());
}
// Make the postfit
Processor->MakePostfit();
Processor->DrawPostfit();
Expand Down
4 changes: 4 additions & 0 deletions mcmc/MCMCProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ class MCMCProcessor {
void ParameterEvolution(const std::vector<std::string>& Names,
const std::vector<int>& NIntervals);

/// @brief Thin MCMC Chain, to save space and maintain low autocorrelations.
/// @param ThinningCut every which entry you want to thin
inline void ThinMCMC(const int ThinningCut) {ThinningMCMC(MCMCFile+".root", ThinningCut);};

/// @brief KS: Perform MCMC diagnostic including Autocorrelation, Trace etc.
void DiagMCMC();

Expand Down
4 changes: 3 additions & 1 deletion mcmc/StatisticalUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ inline void ThinningMCMC(const std::string& FilePath, const int ThinningCut) {
TTree *inTree = static_cast<TTree*>(inFile->Get("posteriors"));
if (!inTree) {
MACH3LOG_ERROR("Error: TTree 'posteriors' not found in file.");
inFile->ls();
inFile->Close();
throw MaCh3Exception(__FILE__, __LINE__);
}
Expand All @@ -566,6 +567,8 @@ inline void ThinningMCMC(const std::string& FilePath, const int ThinningCut) {

// Loop over entries and apply thinning
Long64_t nEntries = inTree->GetEntries();
double retainedPercentage = (double(nEntries) / ThinningCut) / double(nEntries) * 100;
MACH3LOG_INFO("Thinning will retain {:.2f}% of chains", retainedPercentage);
for (Long64_t i = 0; i < nEntries; i++) {
if (i % (nEntries/10) == 0) {
MaCh3Utils::PrintProgressBar(i, nEntries);
Expand All @@ -578,7 +581,6 @@ inline void ThinningMCMC(const std::string& FilePath, const int ThinningCut) {
inFile->WriteTObject(outTree, "posteriors", "kOverwrite");
inFile->Close();
delete inFile;
delete outTree;

MACH3LOG_INFO("Thinned TTree saved and overwrote original in: {}", TempFilePath);
}

0 comments on commit 42d763b

Please sign in to comment.