Building and Visualizing 2D Histograms in CERN ROOT (TH2F)

In the previous tutorials, you learned how to create 1D histograms and perform Gaussian fits using PyROOT.
Now we move one step ahead and explore 2D histograms, which are extremely useful in physics
for visualizing correlations between two variables β€” for example:

  • Energy vs Time
  • X vs Y detector positions
  • Two correlated detector signals
  • Calibration maps

In ROOT, 2D histograms are created using the TH2F class.
We will learn how to build, visualize, customize, and save them using Python.

πŸ“„ Step 1 β€” Create a Python Script

Open a text editor and save the following code as:
root_2d_histogram.py

import ROOT
import math
import random

# -------------------------------------------------------
# 1) Create a 2D histogram TH2F
# -------------------------------------------------------
h2 = ROOT.TH2F("h2", "2D Correlation; X values; Y values", 
               50, -4, 4,     # X bins, range
               50, -4, 4)     # Y bins, range

# -------------------------------------------------------
# 2) Fill histogram with correlated data
# -------------------------------------------------------
for _ in range(20000):
    x = ROOT.gRandom.Gaus(0, 1)
    y = 0.6 * x + ROOT.gRandom.Gaus(0, 1)  # correlation
    h2.Fill(x, y)

# -------------------------------------------------------
# 3) Create a canvas and draw the histogram
# -------------------------------------------------------
c = ROOT.TCanvas("c", "2D Histogram Example", 900, 700)
c.Divide(1, 2)

# Draw as color (heatmap)
c.cd(1)
h2.Draw("COLZ")

# Draw as scatter plot
c.cd(2)
h2.Draw("SCAT")

# -------------------------------------------------------
# 4) Save the images
# -------------------------------------------------------
c.SaveAs("hist_2d_output.png")
print("Saved: hist_2d_output.png")

# Save to a ROOT file
f = ROOT.TFile("hist_2d_output.root", "RECREATE")
h2.Write()
f.Close()
print("Saved: hist_2d_output.root")
▢️ Step 2 β€” Run the Script

Open a ROOT-enabled terminal:

cd C:\root\root\bin
thisroot.bat

Now run your Python script:

py -3.11 C:\root\root\bin\root_2d_histogram.py

(In my computer, the path of the folder where the code root_2d_histogram.py is saved is C:\root\root\bin. You have to choose your folder path.)

Β 

You will get:

  • hist_2d_output.png β€” the 2D plot as shown below
  • hist_2d_output.root β€” histogram stored for later use
πŸ”½ Detailed Explanation (Click to Expand)
Show / Hide Explanation
1️⃣ What is a TH2F?
TH2F is a 2D histogram class in ROOT. The β€œ2” means it has two dimensions (X and Y), and β€œF” means the bin contents are stored as floats.
2️⃣ Creating the Histogram
h2 = ROOT.TH2F("h2", "2D Correlation; X values; Y values",
               50, -4, 4,
               50, -4, 4)
The parameters are:
  • "h2" β€” internal name
  • "2D Correlation; X; Y" β€” title and axis labels
  • 50, -4, 4 β€” number of X bins + X-range
  • 50, -4, 4 β€” number of Y bins + Y-range
3️⃣ Filling the Histogram
x = ROOT.gRandom.Gaus(0, 1)
y = 0.6 * x + ROOT.gRandom.Gaus(0, 1)
h2.Fill(x, y)
This creates a correlated dataset: if x is large, y tends to be large too.
4️⃣ Drawing the Histogram
c.Divide(1, 2)
c.cd(1); h2.Draw("COLZ")
c.cd(2); h2.Draw("SCAT")
COLZ produces a heatmap, while SCAT shows scatter points.
5️⃣ Saving the Plot
c.SaveAs("hist_2d_output.png")
ROOT can save images in: .png, .pdf, .jpg, .eps, etc.
6️⃣ Saving to a ROOT File
f = ROOT.TFile("hist_2d_output.root", "RECREATE")
h2.Write()
f.Close()
This saves the histogram in a format you can reopen later for analysis or plotting.
🧠 Understanding the Draw Options
  • "COLZ" β€” draws a heatmap with a color scale on the right
  • "SCAT" β€” draws scatter points (useful for sparse data)
  • "BOX" β€” box-style colored squares
  • "TEXT" β€” print numbers inside each bin
πŸ“š Summary
  • TH2F is used for 2D histograms (correlations)
  • Draw("COLZ") creates heatmaps
  • Draw("SCAT") creates scatter plots
  • 2D plots are widely used in detector calibration, coincidences, and machine learning preprocessing

In the next tutorial, we will explore graph objects in ROOT β€” especially
TGraph and TGraphErrors for plotting data points with uncertainties.

References & Further Reading

  1. CERN ROOT β€” Official Website: https://root.cern/
  2. Brun, R. & Rademakers, F. (1997). β€œROOT – An Object Oriented Data Analysis Framework.” Nuclear Instruments and Methods in Physics Research A, 389(1–2), 81–86. DOI: 10.1016/S0168-9002(97)00048-X
  3. CERN Documentation: ROOT User Manual and Tutorials
  4. TMVA Toolkit for Multivariate Data Analysis: https://root.cern/manual/tmva/
  5. GEANT4 Collaboration. β€œGEANT4 – A Simulation Toolkit.” Nuclear Instruments and Methods in Physics Research A, 506(3), 250–303 (2003). DOI: 10.1016/S0168-9002(03)01368-8
  6. FLUKA Simulation Package β€” Official CERN Page: https://fluka.cern/
  7. CERN Open Data Portal β€” Public Datasets for Education and Research: https://opendata.cern.ch/
  8. CERN Scientific Computing Documentation: https://home.cern/science/computing
  9. C++ ROOT Tutorials for Beginners (Official GitHub Examples): https://github.com/root-project/root/tree/master/tutorials

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top