Analysis-Utilities 26.9.9
C++/ROOT utilities for nuclear measurement data analysis
Loading...
Searching...
No Matches
PlottingUtils Class Reference

All-static helpers for publication-quality ROOT graphics. More...

#include <PlottingUtils.hpp>

Static Public Member Functions

static void SetStylePreferences (PlotSaveFormat save_format=PlotSaveFormat::kPNG)
 Install the global ROOT style and choose the output format.
static void ConfigureAndDrawGraph (TGraph *graph, Int_t color, const TString title="")
 ConfigureGraph() followed by Draw().
static void ConfigureAndDrawHistogram (TH1 *hist, Int_t color, const TString title="")
 ConfigureHistogram() followed by Draw("HIST").
static void ConfigureAndDraw2DHistogram (TH2 *hist, TCanvas *canvas, const TString title="")
 Configure2DHistogram() followed by Draw("COLZ").
static void ConfigureGraph (TGraph *graph, Int_t color, const TString title="")
 Apply the house style to a graph without drawing it.
static void ConfigureGraph (TGraphErrors *graph, Int_t color, const TString title="")
 Apply the house style to a graph with error bars.
static void ConfigureHistogram (TH1 *hist, Int_t color, const TString title="")
 Apply the house style to a 1-D histogram without drawing it.
static void Configure2DHistogram (TH2 *hist, TCanvas *canvas, const TString title="")
 Apply the house style to a 2-D histogram and its canvas.
static TCanvas * GetConfiguredCanvas (Bool_t logy=kFALSE)
 Create a 1200x800 canvas with grid and ticks already set up.
static void SaveFigure (TCanvas *canvas, TString output_filename, TString output_subdirectory="", PlotSaveOptions save_options=PlotSaveOptions::kBOTH)
 Write a canvas to disk under the configured plots base directory.
static void SetPlotsBaseDir (const TString &dir)
 Set the base directory that saved figures are written under.
static TString GetPlotsBaseDir ()
 Current base directory for saved figures, without trailing slash.
static void PlotFitWithResiduals (TH1 *hist, TGraph *total_graph, const std::vector< TGraph * > &component_graphs, Float_t fit_range_low, Float_t fit_range_high, const TString &output_name, const TString &output_subdirectory="fits", const TString &label="", Bool_t logy=kTRUE)
 Draw a fit over its data with a residual panel and save it.
static TLegend * AddLegend (Double_t x1=0.7, Double_t x2=0.9, Double_t y1=0.7, Double_t y2=0.9)
 Create, style and draw a legend on the current pad.
static TLatex * AddText (const TString label, Double_t x=0.9, Double_t y=0.85, Double_t angle=0)
 Create and draw a text annotation on the current pad.
static std::vector< Int_t > GetDefaultColors ()
 A palette of visually distinct ROOT colour indices.
static TString GetRandomName ()
 Generate a name unlikely to collide with existing ROOT objects.
static Width_t GetLineWidth ()
 Line width the Configure* methods apply.

Detailed Description

All-static helpers for publication-quality ROOT graphics.

No instantiation is required or possible in practice — every member is static. SetStylePreferences() must be called before anything else; the other methods print a warning to stdout if it has not been, but still run.

Note
Several methods return heap-allocated ROOT objects. ROOT's own ownership rules apply and are called out per method; in general the objects returned here are owned by the caller or by the current pad, never by this class.

Definition at line 47 of file PlottingUtils.hpp.

Member Function Documentation

◆ AddLegend()

TLegend * PlottingUtils::AddLegend ( Double_t x1 = 0.7,
Double_t x2 = 0.9,
Double_t y1 = 0.7,
Double_t y2 = 0.9 )
static

Create, style and draw a legend on the current pad.

Parameters
x1Left edge, in NDC (0-1).
x2Right edge, in NDC (0-1).
y1Bottom edge, in NDC (0-1).
y2Top edge, in NDC (0-1).
Returns
The drawn TLegend, so entries can be added to it. Owned by the current pad once drawn.
Warning
The argument order is (x1, x2, y1, y2) — both x bounds, then both y bounds — not ROOT's own TLegend(x1, y1, x2, y2).

Definition at line 242 of file PlottingUtils.cpp.

◆ AddText()

TLatex * PlottingUtils::AddText ( const TString label,
Double_t x = 0.9,
Double_t y = 0.85,
Double_t angle = 0 )
static

Create and draw a text annotation on the current pad.

Useful for subplot labels such as "(a)" and "(b)", or any free annotation. The text is right-top aligned at the given point.

Parameters
labelText to draw. ROOT TLatex markup is honoured.
xHorizontal position, in NDC (0-1).
yVertical position, in NDC (0-1).
angleRotation in degrees, counter-clockwise. 0 by default.
Returns
The drawn TLatex. Owned by the current pad once drawn.

Definition at line 254 of file PlottingUtils.cpp.

Referenced by PlotFitWithResiduals().

◆ Configure2DHistogram()

void PlottingUtils::Configure2DHistogram ( TH2 * hist,
TCanvas * canvas,
const TString title = "" )
static

Apply the house style to a 2-D histogram and its canvas.

Styles the axes as for the 1-D case, then enables log-z on canvas and widens its right margin to 0.15 to make room for the colour axis.

Parameters
histHistogram to configure. Null is tolerated and ignored.
canvasCanvas to adjust. Null is tolerated and ignored.
titleHistogram title, in the usual ROOT "title;x;y;z" form.

Definition at line 109 of file PlottingUtils.cpp.

Referenced by ConfigureAndDraw2DHistogram().

◆ ConfigureAndDraw2DHistogram()

void PlottingUtils::ConfigureAndDraw2DHistogram ( TH2 * hist,
TCanvas * canvas,
const TString title = "" )
static

Configure2DHistogram() followed by Draw("COLZ").

No-op if null.

Parameters
hist2-D histogram to configure and draw.
canvasCanvas whose log-z and right margin are adjusted.
titleHistogram title, in the usual ROOT "title;x;y;z" form.

Definition at line 151 of file PlottingUtils.cpp.

◆ ConfigureAndDrawGraph()

void PlottingUtils::ConfigureAndDrawGraph ( TGraph * graph,
Int_t color,
const TString title = "" )
static

ConfigureGraph() followed by Draw().

No-op if graph is null.

Parameters
graphGraph to configure and draw onto the current pad.
colorROOT colour index for the line.
titleGraph title, in the usual ROOT "title;x;y" form.

Definition at line 133 of file PlottingUtils.cpp.

◆ ConfigureAndDrawHistogram()

void PlottingUtils::ConfigureAndDrawHistogram ( TH1 * hist,
Int_t color,
const TString title = "" )
static

ConfigureHistogram() followed by Draw("HIST").

No-op if null.

Parameters
histHistogram to configure and draw onto the current pad.
colorROOT colour index for the line.
titleHistogram title, in the usual ROOT "title;x;y" form.

Definition at line 142 of file PlottingUtils.cpp.

◆ ConfigureGraph() [1/2]

void PlottingUtils::ConfigureGraph ( TGraph * graph,
Int_t color,
const TString title = "" )
static

Apply the house style to a graph without drawing it.

Sets line colour and width, 0.06 axis title and label sizes, 1.2 title offsets, and 506 x-axis divisions.

Parameters
graphGraph to configure. Must not be null.
colorROOT colour index for the line.
titleGraph title, in the usual ROOT "title;x;y" form.

Definition at line 53 of file PlottingUtils.cpp.

Referenced by ConfigureAndDrawGraph(), and WaveformProcessingUtils::SaveSampleWaveform().

◆ ConfigureGraph() [2/2]

void PlottingUtils::ConfigureGraph ( TGraphErrors * graph,
Int_t color,
const TString title = "" )
static

Apply the house style to a graph with error bars.

As the TGraph overload, plus marker style 20 at size 1.2 in the same colour as the line — the usual look for a data series with uncertainties.

Parameters
graphGraph to configure. Must not be null.
colorROOT colour index for both line and markers.
titleGraph title, in the usual ROOT "title;x;y" form.

Definition at line 68 of file PlottingUtils.cpp.

◆ ConfigureHistogram()

void PlottingUtils::ConfigureHistogram ( TH1 * hist,
Int_t color,
const TString title = "" )
static

Apply the house style to a 1-D histogram without drawing it.

Sets line colour and width, hollow fill, 0.06 axis title and label sizes, 1.2 title offsets, and axis divisions tuned for spectra. The x axis is forced out of exponent notation.

Parameters
histHistogram to configure. Null is tolerated and ignored.
colorROOT colour index for the line.
titleHistogram title, in the usual ROOT "title;x;y" form.

Definition at line 86 of file PlottingUtils.cpp.

Referenced by ConfigureAndDrawHistogram().

◆ GetConfiguredCanvas()

TCanvas * PlottingUtils::GetConfiguredCanvas ( Bool_t logy = kFALSE)
static

Create a 1200x800 canvas with grid and ticks already set up.

Parameters
logykTRUE to start with a logarithmic y axis.
Returns
A newly allocated TCanvas with a randomised name (see GetRandomName()). The caller owns this pointer.
Note
The canvas is made current, so subsequent Draw() calls land on it.

Definition at line 162 of file PlottingUtils.cpp.

Referenced by PlotFitWithResiduals(), and WaveformProcessingUtils::SaveSampleWaveform().

◆ GetDefaultColors()

std::vector< Int_t > PlottingUtils::GetDefaultColors ( )
static

A palette of visually distinct ROOT colour indices.

Returns
24 colour indices, intended to be indexed by series number.

Definition at line 234 of file PlottingUtils.cpp.

◆ GetLineWidth()

Width_t PlottingUtils::GetLineWidth ( )
inlinestatic

Line width the Configure* methods apply.

Returns
2 after SetStylePreferences(PlotSaveFormat::kPNG), 1 for kPDF.

Definition at line 279 of file PlottingUtils.hpp.

Referenced by FittingUtils::PlotFitDoublePeak(), RooFitUtils::PlotFitDoublePeak(), FittingUtils::PlotFitSinglePeak(), RooFitUtils::PlotFitSinglePeak(), FittingUtils::PlotFitTriplePeak(), and RooFitUtils::PlotFitTriplePeak().

◆ GetPlotsBaseDir()

TString PlottingUtils::GetPlotsBaseDir ( )
static

Current base directory for saved figures, without trailing slash.

Returns
The directory last set by SetPlotsBaseDir(), or "plots".

Definition at line 15 of file PlottingUtils.cpp.

Referenced by SaveFigure(), and InitUtils::SetROOTPreferences().

◆ GetRandomName()

TString PlottingUtils::GetRandomName ( )
static

Generate a name unlikely to collide with existing ROOT objects.

ROOT keeps named objects in global lists and warns (or misbehaves) on duplicate names; this sidesteps that for throwaway canvases.

Returns
A name of the form nameX.XXXXXXX from a clock-seeded generator.

Definition at line 267 of file PlottingUtils.cpp.

Referenced by RooFitUtils::BuildDisplayHistogramFrom(), and GetConfiguredCanvas().

◆ PlotFitWithResiduals()

void PlottingUtils::PlotFitWithResiduals ( TH1 * hist,
TGraph * total_graph,
const std::vector< TGraph * > & component_graphs,
Float_t fit_range_low,
Float_t fit_range_high,
const TString & output_name,
const TString & output_subdirectory = "fits",
const TString & label = "",
Bool_t logy = kTRUE )
static

Draw a fit over its data with a residual panel and save it.

Produces a two-pad figure: the histogram with the total fit and each component overlaid on top (70% of the height), and a pull panel beneath (30%) marked with dashed guide lines at ±3σ. The displayed x range is padded 10% either side of the fit range.

Parameters
histData histogram. Must not be null.
total_graphCurve for the summed model.
component_graphsOne curve per model component; may be empty.
fit_range_lowLower fit bound, in the histogram's x units.
fit_range_highUpper fit bound, in the histogram's x units.
output_nameBase filename, without extension.
output_subdirectorySubdirectory under the plots base. "fits" by default.
labelOptional annotation drawn on the plot; empty draws nothing.
logykTRUE (default) for a logarithmic y axis on the upper pad.
Note
Saves through SaveFigure(), so the same base-directory and format rules apply. A companion pull histogram is written alongside.

Definition at line 274 of file PlottingUtils.cpp.

Referenced by FittingUtils::PlotFitDoublePeak(), RooFitUtils::PlotFitDoublePeak(), FittingUtils::PlotFitSinglePeak(), RooFitUtils::PlotFitSinglePeak(), FittingUtils::PlotFitTriplePeak(), and RooFitUtils::PlotFitTriplePeak().

◆ SaveFigure()

void PlottingUtils::SaveFigure ( TCanvas * canvas,
TString output_filename,
TString output_subdirectory = "",
PlotSaveOptions save_options = PlotSaveOptions::kBOTH )
static

Write a canvas to disk under the configured plots base directory.

The file lands in <plots_base>/ or <plots_base>/<subdirectory>/, with the extension chosen by SetStylePreferences(). Missing directories are created. For PlotSaveOptions::kLOG and PlotSaveOptions::kBOTH the log-y variant is written with a log_ filename prefix.

Parameters
canvasCanvas to save. Must not be null.
output_filenameBase filename, without extension.
output_subdirectorySubdirectory under the plots base; empty means the base itself.
save_optionsWhich axis scalings to write. PlotSaveOptions::kBOTH by default.
Warning
Requesting a logarithmic variant for a canvas containing a TH2 is treated as a usage error and terminates the process via std::exit(1) after printing an explanation. The log option refers to the y axis and is meaningless for a 2-D colour plot, whose z axis is already logarithmic if it went through Configure2DHistogram(). Use PlotSaveOptions::kLINEAR for those.
Note
The canvas is left with log-y disabled on return, whatever it was set to beforehand.

Definition at line 176 of file PlottingUtils.cpp.

Referenced by PlotFitWithResiduals(), and WaveformProcessingUtils::SaveSampleWaveform().

◆ SetPlotsBaseDir()

void PlottingUtils::SetPlotsBaseDir ( const TString & dir)
static

Set the base directory that saved figures are written under.

Parameters
dirBase directory. Pass an absolute path so output is anchored to a project root regardless of the current working directory. Trailing slashes are stripped.
Note
The default is the CWD-relative "plots". InitUtils::SetROOTPreferences() calls this for you.

Definition at line 8 of file PlottingUtils.cpp.

Referenced by InitUtils::SetROOTPreferences().

◆ SetStylePreferences()

void PlottingUtils::SetStylePreferences ( PlotSaveFormat save_format = PlotSaveFormat::kPNG)
static

Install the global ROOT style and choose the output format.

Sets gStyle globally: no stat or fit box, 0.06 axis title and label sizes, 1.2 title offsets, grids on in a light grey dashed style, ticks on both axes, and a 255-colour turbo palette for 2-D colour scales. Also fixes the line width used by the Configure* methods — 2 for PNG, 1 for PDF.

Parameters
save_formatOutput format for SaveFigure(). Defaults to PlotSaveFormat::kPNG.
Note
Mutates ROOT global state (gStyle) for the whole process. InitUtils::SetROOTPreferences() calls this and is the recommended entry point.

Definition at line 23 of file PlottingUtils.cpp.

Referenced by WaveformProcessingUtils::SaveSampleWaveform(), and InitUtils::SetROOTPreferences().


The documentation for this class was generated from the following files: