KAHIBARO
Discord Login Register

7.6 MultiGraph

Plotting several graphs together

When you have several related $x$–$y$ datasets, it is often useful to display them on a single plot. In ROOT this is done with the class TMultiGraph. A TMultiGraph is a container that holds multiple TGraph-like objects and draws them together on the same axes.

You typically create and fill ordinary graphs first, for example several TGraph or TGraphErrors objects, each representing a different dataset. Once these graphs exist, you create one TMultiGraph object and add each graph to it. A typical skeleton in a macro looks like:

cpp
TGraph *g1 = new TGraph(n1, x1, y1);
TGraph *g2 = new TGraph(n2, x2, y2);
TMultiGraph *mg = new TMultiGraph();
mg->Add(g1);
mg->Add(g2);
mg->Draw("ALP");

The option string in Draw is applied to all contained graphs, unless you specify individual draw options when calling Add. For example, you can do

cpp
mg->Add(g1, "LP");  // line + points
mg->Add(g2, "P");   // points only
mg->Draw("A");

Here the "A" in Draw("A") tells ROOT to draw new axes for the multigraph. The per-graph options in Add control how each dataset appears. You can mix different graph types as long as they are all derived from TGraph, such as TGraph, TGraphErrors, or TGraphAsymmErrors.

By default, TMultiGraph computes overall axis ranges from all its member graphs. If one graph has a wider range than the others, the axes will expand to include all points. You can override this behavior by setting axis ranges explicitly after drawing. For example:

cpp
mg->Draw("A");
mg->GetXaxis()->SetLimits(0.0, 10.0);
mg->SetMinimum(0.0);
mg->SetMaximum(100.0);
gPad->Modified();
gPad->Update();

In contrast to drawing graphs one by one with the "SAME" option, TMultiGraph manages a single coherent set of axes and makes it easier to treat all graphs as a group. This becomes especially useful when you want to add a legend, update axis titles, or save the plot without worrying about the order in which individual graphs were drawn.

You should always set titles and labels on the TMultiGraph, not on the individual graphs, when you want them to apply to the entire combined plot. For example:

cpp
mg->SetTitle("Calibration curves;Voltage [V];Current [mA]");

This sets the main title and both axis titles for the combined plot. If you call SetTitle on a single TGraph, it will not change the axis labels of the multigraph display.

Important rule for TMultiGraph:
Use mg->SetTitle("Main title;X title;Y title") to define the axis titles for the whole multigraph. Individual TGraph titles do not control the axes when drawn inside a TMultiGraph.

If you need to style individual graphs, for example giving them different colors or marker styles, you do that directly on each TGraph before or after adding it to the TMultiGraph:

cpp
g1->SetLineColor(kRed);
g1->SetMarkerColor(kRed);
g2->SetLineColor(kBlue);
g2->SetMarkerColor(kBlue);

The TMultiGraph does not overwrite these per-graph style settings, it only handles their combined drawing and common axes. Typically you then create a TLegend referring to each graph pointer in order to label the different datasets, which ties together naturally with a multigraph plot.

Comparing datasets

The main reason to use TMultiGraph is to compare several datasets directly on a shared coordinate system. Once your graphs are added and styled, you can visually inspect differences in shape, normalization, or trends between datasets.

Before drawing, it is useful to decide which visual cues will distinguish your datasets. You can combine line styles, colors, and marker styles so that curves remain readable even in grayscale printing. A simple strategy is to assign each dataset a unique color and marker type:

cpp
g1->SetLineColor(kRed);
g1->SetMarkerColor(kRed);
g1->SetMarkerStyle(20);
g2->SetLineColor(kBlue);
g2->SetMarkerColor(kBlue);
g2->SetMarkerStyle(21);

After styling, you add them to the multigraph and draw:

cpp
TMultiGraph *mg = new TMultiGraph();
mg->Add(g1, "LP");
mg->Add(g2, "LP");
mg->SetTitle("Measured spectra;Energy [MeV];Counts");
mg->Draw("A");

To make the comparison quantitative, you often want a legend. The legend associates each graphical style with a label that describes the dataset, for example different run numbers or experimental conditions. The multigraph itself does not manage legends, but you can create a TLegend and add entries using the original TGraph pointers:

cpp
TLegend *leg = new TLegend(0.6, 0.7, 0.88, 0.88);
leg->AddEntry(g1, "Run 1", "lp");
leg->AddEntry(g2, "Run 2", "lp");
leg->Draw();

This makes it much easier to read the plot and understand which curve corresponds to which dataset.

Axis settings also influence how well you can compare datasets. If one dataset has a much larger range than the others, the smaller one may appear almost flat. In that case, you can manually set the vertical axis range on the TMultiGraph to a region that highlights the differences you care about, or you can switch the pad to a logarithmic scale when appropriate:

cpp
mg->Draw("A");
gPad->SetLogy();  // if comparing datasets that span several orders of magnitude
gPad->Modified();
gPad->Update();

Another useful feature when comparing datasets with uncertainties is to use graphs with error bars, such as TGraphErrors or TGraphAsymmErrors, inside the TMultiGraph. This lets you see both the central values and their uncertainties side by side for multiple measurements.

Sometimes you want to normalize datasets before comparison, for instance to unit area or to the same maximum value. That arithmetic is performed when you construct the graphs and is not specific to TMultiGraph, but once the scaled graphs are ready, drawing them together makes patterns and systematic differences easier to identify.

Key comparison strategy:
Prepare each dataset as a separate TGraph with clear visual style, add all graphs to a single TMultiGraph, and use common axes and a legend to compare shapes, trends, and uncertainties across datasets on one plot.

By combining multiple graphs into one TMultiGraph, you obtain a compact and coherent view of several measurements or models. This is a standard technique in ROOT-based analysis for showing different runs, configurations, or theoretical predictions on a single, easy-to-interpret figure.

Views: 12

Comments

Please login to add a comment.

Don't have an account? Register now!