MaCh3  2.8.0
Reference Guide
RHatModule.cpp
Go to the documentation of this file.
1 
15 // *******************
18 
19 namespace M3{
20 RHatModule::~RHatModule() = default;
21 
23  m_parser = std::make_unique<MaCh3ArgumentParser>("rhat", "1.0", argparse::default_arguments::help);
24  m_parser->add_description("Tool for computing the R-hat diagnostic for MCMC chains.");
25  m_parser->add_argument("n-thin")
26  .help("if in high mem mode this is number of toys per chain, for low mem thinning setting")
27  .metavar("NTHINTOYS")
28  .scan<'i', int>()
29  .required();
30  m_parser->add_argument("--high-mem")
31  .help("Enable high memory mode.")
32  .flag();
33  m_parser->add_argument("mcmc-chain")
34  .help("MCMC chain root files")
35  .metavar("MCMC_CHAIN1 [MCMC_CHAIN2 [...]]")
36  .nargs(argparse::nargs_pattern::at_least_one)
37  .required();
38  return m_parser.get();
39 }
40 
44 
45  std::vector<std::string> MCMCFile;
46  auto mcmc_chain_args = m_parser->get<std::vector<std::string>>("mcmc-chain");
47 
48  if (mcmc_chain_args.empty()){ // No MCMC chain files provided, shouldn't be possible with the argparser req.
49  MACH3LOG_ERROR("Wrong arguments");
50  MACH3LOG_ERROR("./RHat NThin MCMCchain_1.root MCMCchain_2.root MCMCchain_3.root ... [how many you like]");
51  throw MaCh3Exception(__FILE__ , __LINE__ );
52  }
53 
54  int NThinToys = m_parser->get<int>("n-thin"); // Number of thinning for MCMC chains, Number of toys for high memory mode
55  std::size_t Nchains = mcmc_chain_args.size();
56 
57  //KS Gelman suggests to diagnose on more than one chain
58  for (const auto& file : mcmc_chain_args){
59  MACH3LOG_INFO("Adding file: {}", file);
60  }
61 
62  if(Nchains == 1){
63  MACH3LOG_WARN("Gelman is going to be sad :(. He suggested you should use more than one chain (at least 4). Code works fine for one chain, however, estimator might be biased.");
64  MACH3LOG_WARN("Multiple chains are more likely to reveal multimodality and poor adaptation or mixing:");
65  }
66 
67 
68  MACH3LOG_INFO("Diagnosing {} chains", Nchains);
69  bool high_mem = m_parser->get<bool>("--high-mem");
70  if (high_mem){
71  MACH3LOG_INFO("With {} toys", NThinToys);
72  if (NThinToys < 1){
73  MACH3LOG_ERROR("You specified {} for NToys, specify larger greater than 0", NThinToys);
74  throw MaCh3Exception(__FILE__ , __LINE__ );
75  }
76  }
77 
78  auto RHatCalc = std::make_unique<RHatCalculator>(high_mem, mcmc_chain_args, NThinToys);
79  RHatCalc->RunDiagnostic();
80 
81  return 0;
82 }
83 }
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
Definition: MaCh3Logger.h:60
#define MACH3LOG_WARN
Definition: MaCh3Logger.h:36
Module for computing the R-hat diagnostic for MCMC chains.
Extended ArgumentParser with MaCh3-specific functionality.
Definition: m3argparse.hpp:17
std::unique_ptr< MaCh3ArgumentParser > m_parser
Argument parser for this plugin.
Definition: plugin.hpp:47
int Run() override
Execute the MCMC diagnostics.
Definition: RHatModule.cpp:41
MaCh3ArgumentParser * get_parser() override
Get the argument parser for this module.
Definition: RHatModule.cpp:22
virtual ~RHatModule()
Destructor.
Custom exception class used throughout MaCh3.
void MaCh3Welcome()
KS: Prints welcome message with MaCh3 logo.
Definition: Monitor.cpp:13
Main namespace for MaCh3 software.