SkillAgentSearch skills...

MaCh3Tutorial

Tutorial how to use MaCh3

Install / Use

/learn @mach3-software/MaCh3Tutorial
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Welcome to MaCh3 Tutorial

After this tutorial you should know how to run MCMC, implement systematic uncertainties, new samples, and how to plot standard Bayesian diagnostics.

There are several standard parts of MaCh3 analysis like sample/systematic implementations, validations and plotting. This tutorial will help you get used to different parts of MaCh3.

<img width="700" alt="Workflow example" src="https://github.com/user-attachments/assets/f0197499-a0e4-483e-8e8f-c1deb6d880ea">

MaCh3 is predominantly C++ software, although some functionality is available through python as well. See the table of contents for more.

Code - Documented Container Image

Table of contents

  1. How to Start?
  2. How to Run MCMC
    1. Config Overrides
    2. Processing MCMC Outputs
    3. Plotting MCMC Posteriors
    4. Plotting Correlation Matrix
  3. Posterior Predictive Analysis
    1. Plotting Posterior Predictive Distributions
    2. Prior Predictive Distributions
  4. How to Develop a Model of Systematic Uncertainties
    1. How to Compare Chains
    2. Holding Parameters Fixed
  5. How to Develop New Samples
    1. Modifying Analysis Samples
    2. Adding a New Sample
    3. Changing Oscillation Engine
    4. Atmospheric Sample
    5. Plotting Kinematic Distribution
  6. MCMC Diagnostic
    1. Running Multiple Chains
  7. Useful Settings
  8. Other Useful Plots
    1. LLH scans
    2. Sigma Variations

How to Start?

To compile try

git clone https://github.com/mach3-software/MaCh3Tutorial.git
mkdir build;
cd build;
cmake ../ -DPYTHON_ENABLED=ON [DPYTHON_ENABLED not mandatory]
make -jN [set number of threads]
make install

then

source bin/setup.MaCh3.sh
source bin/setup.MaCh3Tutorial.sh

If this does not work, check out the Requirements for MaCh3 in case you are missing something.

One of the suggestions in Requirements is to use a conda/micromamba environment. You can install everything you need and build the tutorial using these commands:

<details> <summary>Click to see commands</summary>

To install micromamba and the environment needed to build MaCh3:

mkdir MaCh3Things && cd MaCh3Things

echo "" | BIN_FOLDER=".micromamba" INIT_YES="n" CONDA_FORGE_YES="y" "${SHELL}" <(curl -L micro.mamba.pm/install.sh)

cat > env.sh <<'EOF'
export MAMBA_ROOT_PREFIX="$(pwd)/.micromamba"
command="$(${MAMBA_ROOT_PREFIX}/micromamba shell hook -s posix)"
eval "$command"
EOF

source env.sh

micromamba env create -n MaCh3 -c conda-forge root cmake -y
micromamba activate MaCh3

and then to build MaCh3:

git clone https://github.com/mach3-software/MaCh3Tutorial.git
cd MaCh3Tutorial

mkdir build && cd build
cmake ../
make -j8
make install

source bin/setup.MaCh3.sh
source bin/setup.MaCh3Tutorial.sh

Note: To run the tutorial from a fresh terminal after this installation you must source and activate the relevant scripts and environments:

cd MaCh3Things
source env.sh # source micromamba
micromamba activate MaCh3

cd MaCh3Tutorial/build
source bin/setup.MaCh3.sh
source bin/setup.MaCh3Tutorial.sh
</details>

Alternatively you can use containers by

docker pull ghcr.io/mach3-software/mach3tutorial:alma9latest

To read more about how to use containers, check our doxygen here.

How to run MCMC

To run MCMC simply

./bin/MCMCTutorial TutorialConfigs/FitterConfig.yaml

Congratulations! 🎉 You have just finished your first MCMC chain. You can view Test.root for example in TBrowser like in plot below.

<img width="350" alt="Posterior example" src="https://github.com/user-attachments/assets/6d4c97e2-f36b-456e-8e7b-1751f142d2ac">

A single entry in the tree represents a single MCMC step. Other than debug purposes, it is highly recommended to use MaCh3 processing tools for further visualisation.

WARNING Your posterior may look very shaky and slightly different to the one in example. This is because you have run the chain with low number of steps, meaning you don't have enough statistic to build out the posterior distribution. It is good homework to increase the number of steps and see how much smoother the posterior becomes, but at the cost of having to wait more. You can easily test this by modifying TutorialConfigs/FitterConfig.yaml to change the number of MCMC steps:

General:
  MCMC:
    NSteps: 10000

and then re-running MCMCTutorial.

Warning: If you modified files in the main MaCh3Tutorial folder instead of build, you will have to call make install for the changes to propagate! Generally speaking, it is good practice to work from the build directory and make your config changes there so that local changes do not have to be tracked by git.

Config Overrides

Instead of changing the config file TutorialConfigs/FitterConfig.yaml above directly, you can instead dynamically override your configurations at the command line like this:

./bin/MCMCTutorial TutorialConfigs/FitterConfig.yaml General:MCMC:NSteps:100000

In this way, you can add as many configuration overrides here as you like, as long as the format is [executable] [config] ([option1] [option2] ...).

Warning This overriding process is only possible for the "main config" (i.e., configs that respond directly to the Manager class in MaCh3 core). This main config is used with the apps in the Tutorial folder here; for the other apps (mainly plotting apps like PredictivePlotting) that read from bin/TutorialDiagConfig.yaml, this is not possible, as further command line arguments are interpreted as input ROOT files, not override directives.

Processing MCMC Outputs

Being able to visualise and analyse the output of the MCMC is standard procedure after a chain has finished. MaCh3 uses ProcessMCMC to transform the raw output from the MCMC into smaller outputs that are more easily digested by downstream plotting macros. You can run it using

./bin/ProcessMCMC bin/TutorialDiagConfig.yaml Test.root

where Test.root is the output of running MCMCTutorial as described here. You can find some quickly-generated plots in Test_drawCorr.pdf. One of plots you will encounter is:

<img width="350" alt="Posterior example" src="https://github.com/user-attachments/assets/1073a76e-5d82-4321-8952-e098d1b0717f">

This is the marginalised posterior of a single parameter. This is the main output of the MCMC.

There are many options in ProcessMCMC that allow you to make many more analysis plots from the MCMC output; we recommend that you see here to get better idea what each plot means. In particular, we recommend comparing 2D posteriors with the correlation matrix, and playing with triangle plots.

Plotting MCMC Posteriors

Once you have the processed MCMC output from ProcessMCMC, which will be called something like <inputName>_Process.root, you can make fancier analysis plots from it using the GetPostfitParamPlots app like:

./bin/GetPostfitParamPlots Test_drawCorr.root

The output should look like plot below. This conveys same information as the individual posteriors from Test_drawCorr.pdf, but is more compact. This is useful especially if you have hundreds of parameters to compare.

<img width="350" alt="Posterior example" src="https://github.com/user-attachments/assets/27e3e4c8-629e-4c05-ac64-f6a7bec85331"> <details> <summary><strong>(Detailed) Additional Plots </strong></summary> `GetPostfitParamPlots` can produce two additional sets of plots:

Ridge Plot - This allow to nicely see non-Gaussian paramters.

<img width="350" alt="Ridge" src="https://github.com/user-attachments/assets/617f5929-b389-495e-ab7b-2ecd6c2d991e">

Violin Plot - This also allow to see nicely non-Gaussian parameters but also is useful in comparing two chains. ProcessMCMC must be run with option "PlotCorr" to be able to produce violin plot.

<img width="350" alt="Violin example" src="https://github.com/user-attachments/assets/4788ab29-f24a-4b09-8b0f-c9b36d069cfe"> </details>

Plotting Correlation Matrix

If you have run ProcessMCMC with option "PlotCorr" you will have a correlation matrix in the outputs. This is a handy tool for viewing how correlated different parameters are. However, mature analyses with hundreds of parameters may run into the problem of having too large of plots to be useful. To combat this, you can plot a subset of parameters using MatrixPlotter:

./bin/MatrixPlotter bin/TutorialDiagConfig.yaml Test_drawCorr.root

This macro will give you MatrixPlot.pdf, where you should see a plot like this:

<img width="350" alt="Posterior example" src="https://github.com/user-attachments/assets/14471069-27e7-4ea3-9d75-232615aa246a">

In this particular example, you can see only two parameters. Using Tutori

View on GitHub
GitHub Stars9
CategoryEducation
Updated7h ago
Forks4

Languages

C++

Security Score

90/100

Audited on Mar 31, 2026

No findings