Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Histograms tutorials

Examples showing the "histograms' classes" usage.

A histogram is a visual representation of the distribution of quantitative data. TH1 class introduces the basic data structure used in root for histograms.

In a nutshell:

// Open the file to write the histogram to
auto outFile = std::unique_ptr<TFile>(TFile::Open("outfile.root", "RECREATE"));
// Create the histogram object
// There are several constructors you can use (\see TH1). In this example we use the
// simplest one, accepting a number of bins and a range.
int nBins = 30;
double rangeMin = 0.0;
double rangeMax = 10.0;
TH1D histogram("histogram", "My first ROOT histogram", nBins, rangeMin, rangeMax);
// Fill the histogram. In this simple example we use a fake set of data.
// The 'D' in TH1D stands for 'double', so we fill the histogram with doubles.
// In general you should prefer TH1D over TH1F unless you have a very specific reason
// to do otherwise.
const std::array values{1, 2, 3, 3, 3, 4, 3, 2, 1, 0};
for (double val : values) {
histogram.Fill(val);
}
// Write the histogram to `outFile`.
outFile->WriteObject(&histogram, histogram.GetName());
// When the TFile goes out of scope it will close itself and write its contents to disk.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4086
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:682

Explore the examples below for different histogram classes

Tutorials sorted after groups

List of all tutorials

Introduction

Tutorial Description
hist000_TH1_first.C First example.

TH1 Histograms

These examples shows some of the ratioplots

Tutorial Description
hist000_TH1_first.C Hello World example for TH1.
hist001_TH1_fillrandom.C hist001_TH1_fillrandom.py Fill a 1D histogram with random values using predefined functions.
hist002_TH1_fillrandom_userfunc.C Fill a 1D histogram from a user-defined parametric function.
hist003_TH1_draw.C Draw a 1D histogram to a canva.
hist004_TH1_labels.C 1D histograms with alphanumeric labels.
hist005_TH1_palettecolor.C Palette coloring for TH1.
hist006_TH1_bar_charts.C Draw 1D histograms as bar charts.
hist007_TH1_liveupdate.C hist007_TH1_liveupdate.py Histograms filled and drawn in a loop.
hist008_TH1_zoom.C Change the range of an axis in a Histogram.
hist009_TH1_normalize.C Normalizing a Histogram.
hist010_TH1_two_scales.C hist010_TH1_two_scales.py Draw two histograms on one canva using different y-axis scales.
hist011_TH1_legend_autoplaced.C Automatic placing of the legend.
hist012_TH1_hksimple.C Dynamic filling of TH1K histograms.
hist013_TH1_rebin.C Create a variable bin-width histogram and change bin sizes.
hist014_TH1_cumulative.C Illustrate use of the TH1::GetCumulative method.
hist015_TH1_read_and_draw.C hist015_TH1_read_and_draw.py Read a 1D histogram from a ROOT File and draw it.
hist016_TH1_different_scales_canvas.C Draw two histograms on one canva using different y-axis scales.
hist017_TH1_smooth.C Histogram smoothing.
hist060_TH1_Stats.C Edit statistics box.
hist101_TH1_autobinning.C Fill multiple histograms with different functions and automatic binning.

TH2 Histograms

Tutorial Description
hist018_TH2_cutg.C Use TCutG object to select bins for drawing a region of a 2D histogram.
hist019_TH2_projection.C Display a histogram and its two projections.
hist020_TH2_draw.C Display 2D histogram drawing options.
hist021_TH2_reverse_axis.C Histogram with reverse axis.
hist022_TH2_palette.C Automatic placing of a color palette via option COLZ.
hist102_TH2_contour_list.C Get contours from a 2D histogram.

THnSparse

Tutorial Description
hist103_THnSparse_hist.C Evaluate the performance of THnSparse vs TH1/2/3/n.

THStack

Tutorial Description
hist023_THStack_simple.C Stack histograms with class THStack.
hist024_THStack_pads.C Draw stack histograms on subpads.
hist025_THStack_2d_palette_color.C Display multiple 2D histograms picking colors within palette 1.
hist026_THStack_color_scheme.C Use accessible color schemes with THStack.
hist027_THStack_palette_color.C Display multiple 1D histograms picking colors within palette kOcean.
hist028_THStack_multicolor.C Use a THStack to show a 2D histogram with cells with different colors.

Ratio plots

Tutorial Description
hist029_TRatioPlot_simple.C hist029_TRatioPlot_simple.py Create a simple ratio plot of two histograms using the pois division option.
hist030_TRatioPlot_residual.C hist030_TRatioPlot_residual.py Create a fit residual plot.
hist031_TRatioPlot_residual_fit.C hist031_TRatioPlot_residual_fit.py Create a fit residual plot and set the y-axis range for it.
hist032_TRatioPlot_fit_lines.C hist032_TRatioPlot_fit_lines.py Set custom dashed lines specified by a vector of floats.
hist033_TRatioPlot_fit_confidence.C hist033_TRatioPlot_fit_confidence.py Set the colors of the confidence interval bands by using
hist034_TRatioPlot_fit_margin.C hist034_TRatioPlot_fit_margin.py Create a fit residual plot, where the separation margin has been set to 0.

TPoly

Tutorial Description
hist037_TH2Poly_boxes.C Create a histogram with polygonal bins (TH2Poly).
hist038_TH2Poly_honeycomb.C Create a histogram with hexagonal bins (TH2Poly).
hist039_TH2Poly_usa.C Create a histogram with polygonal bins (TH2Poly). The initial data represent the USA map.
hist040_TH2Poly_europe.C Create a histogram with polygonal bins (TH2Poly). The initial data represent the Europe map.
hist041_TProfile2Poly_realistic.C Create a histogram with polygonal bins representing different particle charges in a detectior.
hist042_TProfile2Poly_module_error.C Create a histogram with polygonal bins simulating a faulty detector panel w.r.t. particle charge.
hist056_TPolyMarker_contour.C Make a contour plot and get the first contour in a TPolyMarker.
hist104_TH2Poly_fibonacci.C Create a histogram representing the "Fibonacci spiral".

Graphics

Tutorial Description
hist043_Graphics_highlight.C Use the highlight mechanism to update the title of a histogram in real time.
hist044_Graphics_highlight2D.C Use the highlight mechanism to displaying the X and Y projections at a bin in real time.
hist045_Graphics_highlight_ntuple.C Use the highlight mechanism to display which events of an ntuple contribute to a bin.
hist046_Graphics_highlight1D.C Use the highlight mechanism to zoom on a histogram.
hist047_Graphics_candle_decay.C Candle plot illustrating a time development of a certain value.
hist048_Graphics_candle_hist.C Illustrate candle plot options.
hist049_Graphics_candle_plot.C Create a candle plot with 2-D histograms.
hist050_Graphics_candle_plot_options.C Illustrate more candle plot options.
hist051_Graphics_candle_plot_stack.C Create a THStack with candle plot option.
hist052_Graphics_candle_plot_whiskers.C Create a candle plot showing the whiskers definition.
hist053_Graphics_candle_scaled.C Illustrate what scaling effects on candle and violin charts.
hist054_Graphics_logscales.C Draw parametric functions with log scales.
hist055_Graphics_xyplot.C Draw a curve in an orthogonal axis system.

TExec

Tutorial Description
hist057_TExec_th1.C Echo object at mouse position.
hist058_TExec_th2.C Echo object at mouse position and show a graphics line.
hist105_TExec_dynamic_slice.C hist105_TExec_dynamic_slice.py Show the slice of a TH2 following the mouse position.

Files

file  greyscale.C
  View in nbviewer Open in SWAN
Create grey scale of 200 x 200 boxes.
 
file  hist000_TH1_first.C
 Hello World example for TH1.
 
file  hist000_TH1_first.py
 Hello World example for TH1.
 
file  hist001_TH1_fillrandom.C
  View in nbviewer Open in SWAN
Fill a 1D histogram with random values using predefined functions
 
file  hist001_TH1_fillrandom.py
  View in nbviewer Open in SWAN
Fill a 1D histogram with random values using predefined functions
 
file  hist002_TH1_fillrandom_userfunc.C
  View in nbviewer Open in SWAN
Fill a 1D histogram from a user-defined parametric function.
 
file  hist002_TH1_fillrandom_userfunc.py
  View in nbviewer Open in SWAN
Fill a 1D histogram from a user-defined parametric function.
 
file  hist003_TH1_draw.C
  View in nbviewer Open in SWAN
Draw a 1D histogram to a canvas.
 
file  hist003_TH1_draw.py
  View in nbviewer Open in SWAN
Draw a 1D histogram to a canvas.
 
file  hist004_TH1_labels.C
  View in nbviewer Open in SWAN
1D histograms with alphanumeric labels.
 
file  hist005_TH1_palettecolor.C
  View in nbviewer Open in SWAN
Palette coloring for TH1
 
file  hist006_TH1_bar_charts.C
  View in nbviewer Open in SWAN
Draw 1D histograms as bar charts
 
file  hist007_TH1_liveupdate.C
  View in nbviewer Open in SWAN
Histograms filled and drawn in a loop.
 
file  hist007_TH1_liveupdate.py
  View in nbviewer Open in SWAN
Simple example illustrating how to use the C++ interpreter
 
file  hist008_TH1_zoom.C
  View in nbviewer Open in SWAN
Changing the Range on the X-Axis of a Histogram
 
file  hist009_TH1_normalize.C
  View in nbviewer Open in SWAN
Normalizing a Histogram
 
file  hist010_TH1_two_scales.C
  View in nbviewer Open in SWAN
Example of macro illustrating how to superimpose two histograms with different scales in the "same" pad.
 
file  hist010_TH1_two_scales.py
  View in nbviewer Open in SWAN
Example of macro illustrating how to superimpose two histograms with different scales in the "same" pad.
 
file  hist011_TH1_legend_autoplaced.C
  View in nbviewer Open in SWAN
The legend can be placed automatically in the current pad in an empty space found at painting time.
 
file  hist012_TH1_hksimple.C
  View in nbviewer Open in SWAN
Illustrates the advantages of a TH1K histogram
 
file  hist013_TH1_rebin.C
  View in nbviewer Open in SWAN
Rebin a variable bin-width histogram.
 
file  hist014_TH1_cumulative.C
  View in nbviewer Open in SWAN
Illustrate use of the TH1::GetCumulative method.
 
file  hist015_TH1_read_and_draw.C
  View in nbviewer Open in SWAN
Read a 1-D histogram from a ROOT File and draw it.
 
file  hist015_TH1_read_and_draw.py
  View in nbviewer Open in SWAN
A Simple histogram drawing example
 
file  hist016_TH1_different_scales_canvas.C
  View in nbviewer Open in SWAN
Example of a canvas showing two histograms with different scales.
 
file  hist017_TH1_smooth.C
  View in nbviewer Open in SWAN
Histogram smoothing.
 
file  hist018_TH2_cutg.C
  View in nbviewer Open in SWAN
This example demonstrates how to display a 2D histogram and use TCutG object to select bins for drawing.
 
file  hist019_TH2_projection.C
  View in nbviewer Open in SWAN
This example demonstrates how to display a histogram and its two projections.
 
file  hist020_TH2_draw.C
  View in nbviewer Open in SWAN
Display the various 2-d drawing options
 
file  hist021_TH2_reverse_axis.C
  View in nbviewer Open in SWAN
Example showing an histogram with reverse axis.
 
file  hist022_TH2_palette.C
  View in nbviewer Open in SWAN
When an histogram is drawn with the option COLZ, a palette is automatically drawn vertically on the right side of the plot.
 
file  hist023_THStack_simple.C
  View in nbviewer Open in SWAN
Example of stacked histograms: class THStack.
 
file  hist024_THStack_pads.C
  View in nbviewer Open in SWAN
Drawing stack histograms on subpads.
 
file  hist025_THStack_2d_palette_color.C
  View in nbviewer Open in SWAN
Palette coloring for 2D histograms' stack is activated thanks to the option PFC (Palette Fill Color).
 
file  hist026_THStack_color_scheme.C
  View in nbviewer Open in SWAN
This example demonstrates how to use the accessible color schemes with THStack.
 
file  hist027_THStack_palette_color.C
  View in nbviewer Open in SWAN
Palette coloring for histograms' stack is activated thanks to the options PFC (Palette Fill Color), PLC (Palette Line Color) and AMC (Palette Marker Color).
 
file  hist028_THStack_multicolor.C
  View in nbviewer Open in SWAN
Use a THStack to show a 2-D hist with cells with different colors.
 
file  hist029_TRatioPlot_simple.C
  View in nbviewer Open in SWAN
Example creating a simple ratio plot of two histograms using the pois division option.
 
file  hist029_TRatioPlot_simple.py
  View in nbviewer Open in SWAN
Example creating a simple ratio plot of two histograms using the pois division option.
 
file  hist030_TRatioPlot_residual.C
  View in nbviewer Open in SWAN
Example of a fit residual plot.
 
file  hist030_TRatioPlot_residual.py
  View in nbviewer Open in SWAN
Example of a fit residual plot.
 
file  hist031_TRatioPlot_residual_fit.C
  View in nbviewer Open in SWAN
Example which shows how you can get the graph of the lower plot and set the y axis range for it.
 
file  hist031_TRatioPlot_residual_fit.py
  View in nbviewer Open in SWAN
Example which shows how you can get the graph of the lower plot and set the y axis range for it.
 
file  hist032_TRatioPlot_fit_lines.C
  View in nbviewer Open in SWAN
Example that shows custom dashed lines on the lower plot, specified by a vector of floats.
 
file  hist032_TRatioPlot_fit_lines.py
  View in nbviewer Open in SWAN
Example that shows custom dashed lines on the lower plot, specified by a vector of floats.
 
file  hist033_TRatioPlot_fit_confidence.C
  View in nbviewer Open in SWAN
Example that shows how you can set the colors of the confidence interval bands by using the method TRatioPlot::SetConfidenceIntervalColors.
 
file  hist033_TRatioPlot_fit_confidence.py
  View in nbviewer Open in SWAN
Example that shows how you can set the colors of the confidence interval bands by using the method TRatioPlot::SetConfidenceIntervalColors.
 
file  hist034_TRatioPlot_fit_margin.C
  View in nbviewer Open in SWAN
Example showing a fit residual plot, where the separation margin has been set to 0.
 
file  hist034_TRatioPlot_fit_margin.py
  View in nbviewer Open in SWAN
Example showing a fit residual plot, where the separation margin has been set to 0.
 
file  hist036_TH2_labels.C
  View in nbviewer Open in SWAN
2-D histograms with alphanumeric labels.
 
file  hist037_TH2Poly_boxes.C
  View in nbviewer Open in SWAN
This tutorial illustrates how to create an histogram with polygonal bins (TH2Poly).
 
file  hist038_TH2Poly_honeycomb.C
  View in nbviewer Open in SWAN
This tutorial illustrates how to create an histogram with hexagonal bins (TH2Poly).
 
file  hist039_TH2Poly_usa.C
  View in nbviewer Open in SWAN
This tutorial illustrates how to create an histogram with polygonal bins (TH2Poly), fill it and draw it using the col option.
 
file  hist040_TH2Poly_europe.C
  View in nbviewer Open in SWAN
This tutorial illustrates how to create an histogram with polygonal bins (TH2Poly), fill it and draw it.
 
file  hist041_TProfile2Poly_realistic.C
  View in nbviewer Open in SWAN
Different charges depending on region
 
file  hist042_TProfile2Poly_module_error.C
  View in nbviewer Open in SWAN
Simulate faulty detector panel w.r.t.
 
file  hist043_Graphics_highlight.C
 This tutorial demonstrates how the highlight mechanism can be used on an histogram.
 
file  hist044_Graphics_highlight2D.C
 This tutorial demonstrates how the highlight mechanism can be used on an histogram.
 
file  hist045_Graphics_highlight_ntuple.C
 This tutorial demonstrates how the highlight mechanism can be used on a ntuple.
 
file  hist046_Graphics_highlight1D.C
 This tutorial demonstrates how the highlight mechanism can be used on an histogram.
 
file  hist047_Graphics_candle_decay.C
  View in nbviewer Open in SWAN
Candle Decay, illustrate a time development of a certain value.
 
file  hist048_Graphics_candle_hist.C
  View in nbviewer Open in SWAN
Example showing how to combine the various candle plot options.
 
file  hist049_Graphics_candle_plot.C
  View in nbviewer Open in SWAN
Example of candle plot with 2-D histograms.
 
file  hist050_Graphics_candle_plot_options.C
  View in nbviewer Open in SWAN
Example showing how to combine the various candle plot options.
 
file  hist051_Graphics_candle_plot_stack.C
  View in nbviewer Open in SWAN
Example showing how a THStack with candle plot option.
 
file  hist052_Graphics_candle_plot_whiskers.C
  View in nbviewer Open in SWAN
Example of candle plot showing the whiskers definition.
 
file  hist053_Graphics_candle_scaled.C
  View in nbviewer Open in SWAN
Candle Scaled, illustrates what scaling does on candle and violin charts.
 
file  hist054_Graphics_logscales.C
  View in nbviewer Open in SWAN
Draw parametric functions with log scales.
 
file  hist055_Graphics_xyplot.C
  View in nbviewer Open in SWAN
Example showing how to produce a plot with an orthogonal axis system centered at (0,0).
 
file  hist056_TPolyMarker_contour.C
  View in nbviewer Open in SWAN
Make a contour plot and get the first contour in a TPolyMarker.
 
file  hist057_TExec_th1.C
 Echo object at mouse position.
 
file  hist058_TExec_th2.C
 Echo object at mouse position and show a graphics line.
 
file  hist060_TH1_Stats.C
  View in nbviewer Open in SWAN
Edit statistics box.
 
file  hist101_TH1_autobinning.C
 Fill multiple histograms with different functions and automatic binning.
 
file  hist102_TH2_contour_list.C
  View in nbviewer Open in SWAN
Getting Contours From TH2D.
 
file  hist103_THnSparse_hist.C
 Evaluate the performance of THnSparse vs TH1/2/3/nF for different numbers of dimensions and bins per dimension.
 
file  hist104_TH2Poly_fibonacci.C
  View in nbviewer Open in SWAN
A TH2Poly build with Fibonacci numbers.
 
file  hist105_TExec_dynamic_slice.C
  View in nbviewer Open in SWAN
Show the slice of a TH2 following the mouse position.
 
file  hist105_TExec_dynamic_slice.py
  View in nbviewer Open in SWAN
Example of function called when a mouse event occurs in a pad.