|
Analysis-Utilities 26.10.19
C++/ROOT utilities for nuclear measurement data analysis
|
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 preferred style to a graph without drawing it. | |
| static void | ConfigureGraph (TGraphErrors *graph, Int_t color, const TString title="") |
| Apply the preferred style to a graph with error bars. | |
| static void | ConfigureHistogram (TH1 *hist, Int_t color, const TString title="") |
| Apply the preferred style to a 1-D histogram without drawing it. | |
| static void | Configure2DHistogram (TH2 *hist, TCanvas *canvas, const TString title="") |
| Apply the preferred 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 TCanvas * | GetConfiguredCanvasWithSideLegend (TPad *&plot, TPad *&legend_pad, Int_t legend_width_px=600, Int_t plot_height_px=800, Bool_t logy=kFALSE) |
| A canvas with the standard plot area and a legend pad to its right. | |
| static void | SplitPadForPanel (TVirtualPad *pad, Int_t panel_height_px, TPad *&upper, TPad *&lower) |
| Split a pad into a main pad above and a panel below, leaving the main pad's frame exactly as it would be on its own. | |
| 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 Double_t | TextWidthPx (const TString &text, Double_t size_px=30.0, Int_t font=43) |
| Rendered width of a text in a pixel-sized font. | |
| static Double_t | LegendWidthPx (const std::vector< TString > &labels, const TString &header="", Double_t size_px=30.0, Double_t margin=0.25, Double_t padding_px=15.0) |
| Width a legend box needs for its labels at AddLegend()'s font. | |
| static Double_t | FigureScale (TVirtualPad *pad, Int_t reference_width_px=1200) |
| How much larger a pad is than the standard plot area, by width. | |
| static void | ScaleFigure (TVirtualPad *pad, Int_t reference_width_px=1200, Int_t reference_height_px=800) |
| Scale everything on a pad, and its sub-pads, to print like the standard figure. | |
| static void | ScalePad (TVirtualPad *pad, Double_t scale, Int_t reference_width_px=1200, Int_t reference_height_px=800) |
| Scale one pad and everything drawn on it, recursively. | |
| static void | ScaleAxisFontsToPad (TH1 *frame, TVirtualPad *pad, Double_t scale=1.0, Int_t reference_width_px=1200, Int_t reference_height_px=800) |
| Size a frame's axis text as on the standard canvas, times a scale. | |
| static void | ScaleLegend (TLegend *legend, Double_t scale) |
| Set a legend's text to AddLegend()'s 30 px times a scale. | |
| static void | ScaleText (TText *text, TVirtualPad *pad, Double_t scale, Int_t reference_height_px=800) |
| Scale a text object with its pad. | |
| static TLatex * | DrawTitle (TVirtualPad *pad, const TString &title, Double_t scale=1.0) |
| Draw a figure title centred in a pad's top margin, at the size ROOT's own pad title has on the standard canvas. | |
| static void | ScaleLine (TAttLine *line, Double_t scale) |
| Multiply a line width by a scale, rounded to a whole pixel. | |
| static void | ScaleMarker (TAttMarker *marker, Double_t scale) |
| Multiply a marker size by a scale. | |
| 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. | |
All-static helpers for publication-quality ROOT graphics.
No instantiation is required or possible. SetStylePreferences() must be called before anything else; the other methods print a warning to stdout if it has not been, but still run.
Definition at line 47 of file PlottingUtils.hpp.
|
static |
Create, style and draw a legend on the current pad.
| x1 | Left edge, in NDC (0-1). |
| x2 | Right edge, in NDC (0-1). |
| y1 | Bottom edge, in NDC (0-1). |
| y2 | Top edge, in NDC (0-1). |
Definition at line 321 of file PlottingUtils.cpp.
|
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.
| label | Text to draw. ROOT TLatex markup is honoured. |
| x | Horizontal position, in NDC (0-1). |
| y | Vertical position, in NDC (0-1). |
| angle | Rotation in degrees, counter-clockwise. 0 by default. |
Definition at line 502 of file PlottingUtils.cpp.
Referenced by PlotFitWithResiduals().
|
static |
Apply the preferred 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.
| hist | Histogram to configure. Null is tolerated and ignored. |
| canvas | Canvas to adjust. Null is tolerated and ignored. |
| title | Histogram title, in the usual ROOT "title;x;y;z" form. |
Definition at line 114 of file PlottingUtils.cpp.
Referenced by ConfigureAndDraw2DHistogram().
|
static |
Configure2DHistogram() followed by Draw("COLZ").
No-op if null.
| hist | 2-D histogram to configure and draw. |
| canvas | Canvas whose log-z and right margin are adjusted. |
| title | Histogram title, in the usual ROOT "title;x;y;z" form. |
Definition at line 156 of file PlottingUtils.cpp.
|
static |
ConfigureGraph() followed by Draw().
No-op if graph is null.
| graph | Graph to configure and draw onto the current pad. |
| color | ROOT colour index for the line. |
| title | Graph title, in the usual ROOT "title;x;y" form. |
Definition at line 138 of file PlottingUtils.cpp.
|
static |
ConfigureHistogram() followed by Draw("HIST").
No-op if null.
| hist | Histogram to configure and draw onto the current pad. |
| color | ROOT colour index for the line. |
| title | Histogram title, in the usual ROOT "title;x;y" form. |
Definition at line 147 of file PlottingUtils.cpp.
|
static |
Apply the preferred 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.
| graph | Graph to configure. Must not be null. |
| color | ROOT colour index for the line. |
| title | Graph title, in the usual ROOT "title;x;y" form. |
Definition at line 58 of file PlottingUtils.cpp.
Referenced by ConfigureAndDrawGraph(), and WaveformProcessingUtils::SaveSampleWaveform().
|
static |
Apply the preferred 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.
| graph | Graph to configure. Must not be null. |
| color | ROOT colour index for both line and markers. |
| title | Graph title, in the usual ROOT "title;x;y" form. |
Definition at line 73 of file PlottingUtils.cpp.
|
static |
Apply the preferred 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.
| hist | Histogram to configure. Null is tolerated and ignored. |
| color | ROOT colour index for the line. |
| title | Histogram title, in the usual ROOT "title;x;y" form. |
Definition at line 91 of file PlottingUtils.cpp.
Referenced by ConfigureAndDrawHistogram().
|
static |
Draw a figure title centred in a pad's top margin, at the size ROOT's own pad title has on the standard canvas.
ROOT sizes a histogram's pad title from the pad height, so on a pad that takes part of a figure, such as the upper pad of SplitPadForPanel(), it comes out small. This draws the title as text in a pixel-sized font of 36 px times scale instead, which matches the standard title; give the frame no title of its own. Call it after ScaleFigure(), with the scale that used.
| pad | Pad to draw on. Null is tolerated and ignored. |
| title | Title text. ROOT TLatex markup is honoured. |
| scale | Overall scale, normally from FigureScale(). |
Definition at line 472 of file PlottingUtils.cpp.
|
static |
How much larger a pad is than the standard plot area, by width.
A figure placed at a fixed column width is shrunk by its pixel width, so everything on a wider plot has to be drawn proportionally larger to print at the same size as on the standard 1200x800 canvas. This is that proportion, the pad's pixel width over reference_width_px. For a canvas it is the canvas width; for the plot pad of GetConfiguredCanvasWithSideLegend() it is 1.
| pad | Pad, or canvas, to measure. Null gives 1. |
| reference_width_px | Width of the standard plot area, in pixels. 1200 by default, the width of GetConfiguredCanvas(). |
Definition at line 361 of file PlottingUtils.cpp.
Referenced by ScaleFigure().
|
static |
Create a 1200x800 canvas with grid and ticks already set up.
| logy | kTRUE to start with a logarithmic y axis. |
Definition at line 167 of file PlottingUtils.cpp.
Referenced by PlotFitWithResiduals(), and WaveformProcessingUtils::SaveSampleWaveform().
|
static |
A canvas with the standard plot area and a legend pad to its right.
The plot pad is exactly the standard 1200 px wide and plot_height_px tall, with the preferred margins, grid and ticks, so everything drawn on it comes out as on GetConfiguredCanvas(); the canvas is wider by legend_width_px. The legend pad is transparent and margin-less and runs from the plot frame's right edge, over the plot pad's blank right margin, to the canvas edge, so a legend placed in it can sit as close to the frame as wanted. LegendWidthPx() gives the width a legend needs, from which the caller picks legend_width_px and the box position. A plot taller than the standard 800 px is for a panel below the plot: see SplitPadForPanel().
| [out] | plot | The plot pad, drawn and made current. |
| [out] | legend_pad | The legend pad, drawn over the plot pad's right margin. |
| legend_width_px | Width added to the canvas for the legend, in pixels; the legend pad is this plus the plot pad's right margin. | |
| plot_height_px | Height of the plot pad and the canvas, in pixels. 800 by default. | |
| logy | kTRUE for a logarithmic y axis on the plot pad. |
Definition at line 181 of file PlottingUtils.cpp.
|
static |
A palette of visually distinct ROOT colour indices.
Definition at line 313 of file PlottingUtils.cpp.
|
inlinestatic |
Line width the Configure* methods apply.
Definition at line 537 of file PlottingUtils.hpp.
Referenced by FittingUtils::PlotFitDoublePeak(), RooFitUtils::PlotFitDoublePeak(), FittingUtils::PlotFitSinglePeak(), RooFitUtils::PlotFitSinglePeak(), FittingUtils::PlotFitTriplePeak(), and RooFitUtils::PlotFitTriplePeak().
|
static |
Current base directory for saved figures, without trailing slash.
Definition at line 20 of file PlottingUtils.cpp.
Referenced by SaveFigure(), and InitUtils::SetROOTPreferences().
|
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.
Definition at line 515 of file PlottingUtils.cpp.
Referenced by RooFitUtils::BuildDisplayHistogramFrom(), GetConfiguredCanvas(), GetConfiguredCanvasWithSideLegend(), SplitPadForPanel(), and TextWidthPx().
|
static |
Width a legend box needs for its labels at AddLegend()'s font.
A TLegend gives the leading margin of its width to the symbol column and draws the labels after it, while a header starts at the box's left edge, so the box must be the widest label over (1 - margin) or the header's width, whichever is larger, plus padding_px each side.
| labels | The entry labels. ROOT TLatex markup is honoured. |
| header | The header, or empty for none. |
| size_px | Font size in pixels. 30 by default. |
| margin | The legend's symbol-column fraction. 0.25 by default, TLegend's own default. |
| padding_px | Clear space inside each side of the box, in pixels. 15 by default. |
Definition at line 351 of file PlottingUtils.cpp.
|
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.
| hist | Data histogram. Must not be null. |
| total_graph | Curve for the summed model. |
| component_graphs | One curve per model component; may be empty. |
| fit_range_low | Lower fit bound, in the histogram's x units. |
| fit_range_high | Upper fit bound, in the histogram's x units. |
| output_name | Base filename, without extension. |
| output_subdirectory | Subdirectory under the plots base. "fits" by default. |
| label | Optional annotation drawn on the plot; empty draws nothing. |
| logy | kTRUE (default) for a logarithmic y axis on the upper pad. |
Definition at line 522 of file PlottingUtils.cpp.
Referenced by FittingUtils::PlotFitDoublePeak(), RooFitUtils::PlotFitDoublePeak(), FittingUtils::PlotFitSinglePeak(), RooFitUtils::PlotFitSinglePeak(), FittingUtils::PlotFitTriplePeak(), and RooFitUtils::PlotFitTriplePeak().
|
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.
| canvas | Canvas to save. Must not be null. |
| output_filename | Base filename, without extension. |
| output_subdirectory | Subdirectory under the plots base; empty means the base itself. |
| save_options | Which axis scalings to write. PlotSaveOptions::kBOTH by default. |
Definition at line 249 of file PlottingUtils.cpp.
Referenced by PlotFitWithResiduals(), and WaveformProcessingUtils::SaveSampleWaveform().
|
static |
Size a frame's axis text as on the standard canvas, times a scale.
The preferred 0.06 title and label sizes are fractions of the pad's smaller pixel dimension, so a pad taking part of a figure renders them smaller than a standard one. This sets every non-zero title and label size of both axes so the text comes out at 0.06 of reference_height_px pixels times scale, whatever the pad's size. A size already at zero, hiding that axis's text, stays zero. ROOT places a title at its offset times the title size measured along the other axis of the pad, so the 1.2 preferred offsets are rescaled by the pad's aspect ratio to put each title the same number of pixels from its axis as on the standard canvas.
| frame | Histogram or frame whose axes are styled. Null is tolerated and ignored. |
| pad | The pad the frame is drawn on. Null is tolerated and ignored. |
| scale | Overall scale, normally from FigureScale(). |
| reference_width_px | Width of the standard plot area, in pixels. |
| reference_height_px | Height of the standard plot area, in pixels. |
Definition at line 420 of file PlottingUtils.cpp.
Referenced by ScalePad().
|
static |
Scale everything on a pad, and its sub-pads, to print like the standard figure.
The one call for a figure of non-standard size or layout, made once, after everything is drawn. Works out the scale of pad with FigureScale() and applies ScalePad() to it and, through it, to every pad inside it: pad margins, axis text, legends, text, line widths and marker sizes all come out as on the standard canvas multiplied by that scale, whatever share of the figure each pad takes. Pass the canvas for a plain figure, or the plot pad of GetConfiguredCanvasWithSideLegend(). Line widths and marker sizes are multiplied in place, so calling this twice doubles the effect.
| pad | Pad, or canvas, to scale. Null is tolerated and ignored. |
| reference_width_px | Width of the standard plot area, in pixels. 1200 by default. |
| reference_height_px | Height of the standard plot area, in pixels. 800 by default. |
Definition at line 369 of file PlottingUtils.cpp.
|
static |
Set a legend's text to AddLegend()'s 30 px times a scale.
| legend | Legend to resize. Null is tolerated and ignored. |
| scale | Overall scale, normally from FigureScale(). |
Definition at line 449 of file PlottingUtils.cpp.
Referenced by ScalePad().
|
static |
Multiply a line width by a scale, rounded to a whole pixel.
A width of zero, meaning no line, stays zero.
| line | Object with line attributes. Null is tolerated and ignored. |
| scale | Overall scale, normally from FigureScale(). |
Definition at line 489 of file PlottingUtils.cpp.
Referenced by ScalePad().
|
static |
Multiply a marker size by a scale.
| marker | Object with marker attributes. Null is tolerated and ignored. |
| scale | Overall scale, normally from FigureScale(). |
Definition at line 496 of file PlottingUtils.cpp.
Referenced by ScalePad().
|
static |
Scale one pad and everything drawn on it, recursively.
Margins are taken as fractions of the standard plot area and multiplied by its size over the pad's own pixel size on each axis, times scale, so they hold the same number of text lines as on the standard canvas. Then each primitive is scaled: a histogram or frame by ScaleAxisFontsToPad(), a legend by ScaleLegend(), text by ScaleText(), anything with line attributes by ScaleLine(), anything with marker attributes by ScaleMarker(), and a nested pad by this function again.
| pad | Pad to scale. Null is tolerated and ignored. |
| scale | Overall scale, normally from FigureScale(). |
| reference_width_px | Width of the standard plot area, in pixels. |
| reference_height_px | Height of the standard plot area, in pixels. |
Definition at line 378 of file PlottingUtils.cpp.
Referenced by ScaleFigure(), and ScalePad().
|
static |
Scale a text object with its pad.
A pixel-sized font (precision 3, such as AddText()'s 43) is multiplied by scale. A relative font is a fraction of the pad's smaller pixel dimension, so it is reset to give the pixels it would have on the standard canvas, times scale.
| text | Text to resize. Null is tolerated and ignored. |
| pad | The pad the text is drawn on. Null is tolerated and ignored. |
| scale | Overall scale, normally from FigureScale(). |
| reference_height_px | Height of the standard plot area, in pixels. |
Definition at line 455 of file PlottingUtils.cpp.
Referenced by ScalePad().
|
static |
Set the base directory that saved figures are written under.
| dir | Base directory. Pass an absolute path so output is anchored to a project root regardless of the current working directory. Trailing slashes are stripped. |
Definition at line 13 of file PlottingUtils.cpp.
Referenced by InitUtils::SetROOTPreferences().
|
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.
| save_format | Output format for SaveFigure(). Defaults to PlotSaveFormat::kPNG. |
Definition at line 28 of file PlottingUtils.cpp.
Referenced by WaveformProcessingUtils::SaveSampleWaveform(), and InitUtils::SetROOTPreferences().
|
static |
Split a pad into a main pad above and a panel below, leaving the main pad's frame exactly as it would be on its own.
For a residual or deviation panel under a plot. The upper pad is the pad less panel_height_px at the bottom, with all four of the pad's margins, grid, ticks and log-y setting, so a frame drawn on it is pixel-for-pixel the frame of a standard figure when the pad is the standard height plus the panel. The lower pad is transparent and reaches up over the upper pad's blank bottom margin to the foot of its frame, so the two frames share the x axis with only a small gap; it keeps the pad's left, right and bottom margins with a small top margin. Margins are the standard fractions; ScaleFigure() on the parent sizes the panel's margins and axis text to match. The caller hides the upper frame's x-axis labels and title.
| pad | Pad to split. Its own margins stay as they were. | |
| panel_height_px | Height added below the main pad for the panel, in pixels: the pad's height less the main pad's. | |
| [out] | upper | The main pad, drawn first. |
| [out] | lower | The panel, drawn over the main pad's bottom margin. |
Definition at line 208 of file PlottingUtils.cpp.
|
static |
Rendered width of a text in a pixel-sized font.
Measured by ROOT on a scratch canvas, so it is what the text will take on any pad, whatever the pad's size.
| text | Text to measure. ROOT TLatex markup is honoured. |
| size_px | Font size in pixels. 30 by default, the size AddLegend() uses. |
| font | ROOT font code, of precision 3 so the size is in pixels. 43 by default, the font AddLegend() uses. |
Definition at line 333 of file PlottingUtils.cpp.
Referenced by LegendWidthPx().