7.1. Introduction to TGraph
Table of Contents
Graphs vs histograms
In ROOT, graphs and histograms are two different ways to represent data. A histogram represents how often values occur in a given range, while a graph represents explicit pairs of numbers, usually written as $(x, y)$ points.
A histogram, such as a TH1F, expects you to fill it with many values of a single variable, for example many measurements of energy. ROOT then counts how many values fall into each bin. The x axis is divided into bins with fixed or user defined bin widths, and the y axis shows counts or weights per bin. The original individual values are not kept explicitly, only their contribution to bin contents.
A TGraph is very different. It stores each point as an explicit coordinate pair, like a list
$(x_0, y_0), (x_1, y_1), \ldots, (x_{N-1}, y_{N-1})$. Nothing is binned by default. The x coordinates are whatever you provide, and can be irregularly spaced. The y coordinates are the corresponding values of some quantity that depends on x. This is closer to the mathematical idea of plotting a function or data points than to a frequency histogram.
Important:
A histogram represents a distribution of a single variable, binned in x.
A TGraph represents explicit $(x, y)$ pairs, without binning.
This difference has practical consequences. If you have raw event-by-event measurements and want to see the distribution of one quantity, you usually choose a histogram. If you already have processed data that relates two quantities, such as time vs voltage, or energy vs angle, you usually choose a graph.
The visual appearance is also different. By default a histogram is drawn as bars or steps that show bin contents across an interval of x, so each bin covers a finite range. A graph connects points with lines or shows them as markers at discrete x positions. You can of course change drawing options for both, for example draw only markers for a graph or draw a histogram as a smooth curve using special options, but their underlying data structure remains different.
Another important conceptual distinction is that histograms are naturally suited for statistical operations based on counts. The bin content often follows counting statistics, so statistical errors are related to the counts in each bin. ROOT histograms know how to store bin errors and can compute quantities such as mean and RMS directly from the binned data. Graphs, on the other hand, do not assume any statistical model. A basic TGraph only knows the coordinates, not their uncertainties. If you need to handle uncertainties on points, you use classes derived from TGraph such as TGraphErrors or TGraphAsymmErrors, which explicitly store error bars.
Table comparison can highlight the key differences between a 1D histogram and a basic TGraph:
| Feature | Histogram (TH1*) | Graph (TGraph) |
|---|---|---|
| Stored quantity | Bin contents vs binned x intervals | Lists of $(x, y)$ points |
| X values | Fixed by binning | Arbitrary, can be irregular |
| Typical use | Distributions, counts, spectra | Curves, trends, explicit measurements |
| Statistical interpretation | Natural for counts per bin | No built in statistics, user defines meaning |
| Errors | Optional bin errors, often $\sqrt{N}$ | Not stored in TGraph, use TGraphErrors classes |
| Drawing style | Bars or steps by default | Markers and lines by default |
In many analyses, you will use both. You might first create a histogram to visualize the distribution of a variable and extract summary parameters, then later build a graph from these parameters as a function of some other variable. For example, in detector calibration you might bin events by energy in histograms, extract peak positions, and then plot those peak positions as a function of channel number with a TGraph to perform a calibration fit.
X-Y datasets
A TGraph is ROOT’s basic tool to represent and display X-Y datasets. An X-Y dataset is simply a collection of values where each x has an associated y. In a physics or experimental context, common examples include:
Time vs position for a moving particle.
Temperature vs time in a slow control system.
Energy vs angle of emitted particles.
Voltage vs current in an electronics measurement.
In all these cases, you do not count how many times something happened in an interval. Instead, you directly measure pairs of quantities. If you write them in C++ style, you might have arrays like
$$
x[i] = \text{time}_i, \quad y[i] = \text{temperature}_i
$$
for $i = 0, \ldots, N - 1$, where $N$ is the number of measurements.
A TGraph takes ownership of such X-Y datasets conceptually and allows you to display and manipulate them. You can create a graph from two C style arrays of doubles, one for x values and one for y values, or you can create an empty TGraph and add points one by one with a method such as SetPoint. The important point is that the graph keeps every individual pair. You do not lose information by binning, so you can later apply different visual representations, use different fit functions, or analyze the dataset further.
In terms of interpretation, each point $(x_i, y_i)$ usually comes from either a direct measurement or a derived quantity. For example, you may read a detector output at fixed time steps and store the resulting waveform as a set of time samples and amplitudes. Or you might analyze many events, compute an average value for each run or for each configuration, and then store the averages as y values with the run index or configuration parameter as x. In both cases, the TGraph is simply a container that remembers all these pairs.
Key idea for X-Y datasets in ROOT:
Use TGraph when your data is naturally a list of $(x, y)$ points, such as measurements of one physical quantity versus another, without any need to bin in x.
When working with X-Y datasets, binning is still possible but is under your control. If you later decide that you want to study the distribution of y values within some x range, you can create a histogram from your graph data by looping over the points, selecting those in the desired range, and filling a histogram. The original graph remains unchanged. This flexibility is one of the reasons graphs and histograms complement each other in ROOT.
Finally, when you think about future topics such as TGraphErrors, fitting, or MultiGraph, remember that they all build on this basic idea of X-Y datasets. Error graphs add uncertainty information to each point of an X-Y dataset. Fitting uses the stored pairs as input to estimate parameters of a model function $f(x; \theta)$ such that $f(x_i; \theta)$ matches the observed $y_i$. MultiGraph combines several separate X-Y datasets into a single, comparative display. A clear mental picture of what an X-Y dataset is will make these later concepts easier to understand.
Views: 11
KAHIBARO