KAHIBARO
Discord Login Register

8.3. Drawing Multiple Objects

The `SAME` option

When you draw a ROOT object, for example a histogram or a graph, without any special option, ROOT clears the current pad and displays only that object. To place more than one object in the same pad, you must explicitly tell ROOT not to clear the existing drawing. This is the purpose of the SAME draw option.

The basic pattern is that the first object defines the axes and coordinate system, and subsequent objects are drawn with "SAME" so they appear on top of the existing frame. In the ROOT C++ interpreter this typically looks like

cpp
h1->Draw();              // First object, creates axes
h2->Draw("SAME");        // Second object, drawn on top
g1->Draw("SAME");        // Graph on top of histograms

You can combine SAME with other drawing options by writing them together in a single string. For example, "HIST SAME" or "P SAME" or "E1 SAME". The order of options does not matter, as long as SAME is in the string:

cpp
h2->Draw("HIST SAME");
g1->Draw("P SAME");

Rule: The first object in a pad must be drawn without SAME. Every additional object that should appear in the same pad must be drawn with SAME, or it will erase what was there before.

In many cases you will want to control draw order, because later objects are drawn on top of earlier ones. For example, if you want data points with error bars on top of a colored histogram, draw the histogram first, then draw the graph with "SAME". If you get a blank pad after drawing with SAME, it usually means nothing has been drawn in that pad yet or the first draw was made in a different pad than you expected.

Overlaying histograms

Overlaying histograms is a common task when you want to compare distributions directly on the same axes. The general workflow is to prepare histograms with compatible ranges and binning, choose appropriate visual styles, draw one histogram to define the axes, then add the others with SAME.

Suppose you have two one dimensional histograms hData and hMC that both cover the same physical range. A typical overlay sequence is

cpp
hData->SetLineColor(kBlack);
hMC->SetLineColor(kRed);
hData->Draw();                    // Defines axes and scale
hMC->Draw("HIST SAME");           // Overlay as line histogram

You can use different draw options for each histogram. The most common ones for overlays include "HIST" for line outlines of the bins, "E" or "E1" for error bars on data, and "BAR" for vertical bars. They can all be combined with SAME:

cpp
hData->Draw("E1");                // Data with error bars
hMC->Draw("HIST SAME");           // Smooth line on top

If you want to see both histograms clearly, it helps to change colors, line styles, or fill styles. For example, you can draw one histogram with a transparent or hatched fill and the other with only lines. Although the detailed styling is discussed in the dedicated styling chapters, the important point here is that clear visual distinction is necessary when histograms overlap.

Sometimes two histograms have different dynamic ranges. If one is much larger than the other, the smaller one might appear almost flat. A simple workaround is to scale one of the histograms before overlaying:

cpp
hMC->Scale( hData->Integral() / hMC->Integral() );
hData->Draw("E1");
hMC->Draw("HIST SAME");

This makes the integrals equal so that differences in shape rather than normalization become visible. Normalization and scaling are treated systematically in the histogram chapters, but in practice you often use a scaling step before overlaying.

Another practical detail is the axis range. The first drawn histogram fixes both axes, so if you want to show a particular range you either set the range on that first histogram before drawing, or adjust the axis afterward. For example,

cpp
hData->GetXaxis()->SetRangeUser(0.0, 10.0);
hData->Draw();
hMC->Draw("HIST SAME");

If you set the range only on a later histogram, it does not affect the axes that are already defined.

When overlaying many histograms, draw them in the order that makes visual sense. Objects drawn later will be on top, so thin line histograms are easier to see if they are drawn last. In combination with legends, a typical workflow is: draw histograms in the correct order, then create and draw a TLegend so that the legend appears on top of everything.

Rule: The first histogram you draw defines the visible axis range and style of the frame. Set axis ranges and labels on that histogram before overlaying others with SAME.

Overlaying graphs

Graphs represent explicit sets of points, optionally with error bars, and are often used to display measured values as a function of some quantity. Overlaying graphs with each other or with histograms follows the same principle as before: one object defines the axes, all others use SAME.

To overlay two graphs, for example g1 and g2, you can write

cpp
g1->SetMarkerStyle(20);
g1->SetMarkerColor(kBlack);
g2->SetMarkerStyle(21);
g2->SetMarkerColor(kRed);
g1->Draw("AP");            // A: axes, P: markers
g2->Draw("P SAME");        // Points on the same axes

The first graph is usually drawn with an option that includes "A" so that axes are created. The second and subsequent graphs must not use "A", because "A" instructs ROOT to create a new frame and would ignore the existing one. For overlays, remove "A" and keep only the options that set how the points or lines are drawn, for example "P", "L", or "PL", combined with "SAME".

A small comparison of typical draw options for graphs is useful:

Draw optionMeaningUse with SAME?
"A"Draw axes and frameOnly for the first graph
"P"Draw markers at pointsYes, as "P SAME"
"L"Connect points with linesYes, as "L SAME"
"PL"Markers plus connecting linesYes, as "PL SAME"
"E"Symmetric error barsYes, often "PE SAME"
"AP"Axes plus pointsOnly for the first graph

If you overlay graphs with histograms, you need to decide which object defines the axes. A common pattern is to let the histogram define the axis range and then place the data graph on top:

cpp
h->Draw("HIST");           // Defines axes
g->Draw("P SAME");         // Data points over histogram

Alternatively, if your graph covers a wider or more specific range than the histogram, you can draw the graph first with "A" options, then call h->Draw("HIST SAME") to put the histogram on the same axes.

Control over axis limits is important. You can set them on the first drawn object, whether it is a graph or histogram, before overlaying others. For graphs, use

cpp
g1->GetXaxis()->SetLimits(xmin, xmax);
g1->SetMinimum(ymin);
g1->SetMaximum(ymax);
g1->Draw("AP");
g2->Draw("P SAME");

If you place a graph on a very different numerical scale than a histogram, the small scale object might not be visible. In such cases, it is often better to use separate pads or, in more advanced setups, a second axis. The multi pad and axis configuration topics are handled in their own chapters, but when overlaying, it is always your responsibility to choose compatible scales.

Finally, when overlaying several graphs that share x values but have different trends, distinguish them clearly with markers and line styles. Use filled markers for data, open markers or different line types for predictions or models. Adding a legend makes the overlay understandable. For example,

cpp
g1->Draw("AP");
g2->Draw("L SAME");
g3->Draw("P SAME");
// Then create and draw a legend (see the Legends chapter)

Rule: Only the first graph should use axis creating options like "A" or "AP". All additional graphs must drop the "A" and include SAME, for example "P SAME", "L SAME", or "PL SAME".

Views: 12

Comments

Please login to add a comment.

Don't have an account? Register now!