Analysis-Utilities 26.10.19
C++/ROOT utilities for nuclear measurement data analysis
Loading...
Searching...
No Matches
PlottingUtils.hpp
Go to the documentation of this file.
1#ifndef PLOTTINGUTILS_H
2#define PLOTTINGUTILS_H
3
4#include <TCanvas.h>
5#include <TF1.h>
6#include <TGaxis.h>
7#include <TGraph.h>
8#include <TGraphErrors.h>
9#include <TH1.h>
10#include <TH2.h>
11#include <TLatex.h>
12#include <TLegend.h>
13#include <TMath.h>
14#include <TPad.h>
15#include <TROOT.h>
16#include <TRandom3.h>
17#include <TStyle.h>
18#include <TSystem.h>
19#include <iostream>
20#include <vector>
21
28
30enum class PlotSaveFormat {
33};
34
48public:
64 static void
66
71 static void ConfigureAndDrawGraph(TGraph *graph, Int_t color,
72 const TString title = "");
73
78 static void ConfigureAndDrawHistogram(TH1 *hist, Int_t color,
79 const TString title = "");
80
85 static void ConfigureAndDraw2DHistogram(TH2 *hist, TCanvas *canvas,
86 const TString title = "");
87
98 static void ConfigureGraph(TGraph *graph, Int_t color,
99 const TString title = "");
100
111 static void ConfigureGraph(TGraphErrors *graph, Int_t color,
112 const TString title = "");
113
125 static void ConfigureHistogram(TH1 *hist, Int_t color,
126 const TString title = "");
127
138 static void Configure2DHistogram(TH2 *hist, TCanvas *canvas,
139 const TString title = "");
140
151 static TCanvas *GetConfiguredCanvas(Bool_t logy = kFALSE);
152
181 static TCanvas *GetConfiguredCanvasWithSideLegend(TPad *&plot,
182 TPad *&legend_pad,
183 Int_t legend_width_px = 600,
184 Int_t plot_height_px = 800,
185 Bool_t logy = kFALSE);
186
210 static void SplitPadForPanel(TVirtualPad *pad, Int_t panel_height_px,
211 TPad *&upper, TPad *&lower);
212
242 static void SaveFigure(TCanvas *canvas, TString output_filename,
243 TString output_subdirectory = "",
245
256 static void SetPlotsBaseDir(const TString &dir);
257
260 static TString GetPlotsBaseDir();
261
286 static void
287 PlotFitWithResiduals(TH1 *hist, TGraph *total_graph,
288 const std::vector<TGraph *> &component_graphs,
289 Float_t fit_range_low, Float_t fit_range_high,
290 const TString &output_name,
291 const TString &output_subdirectory = "fits",
292 const TString &label = "", Bool_t logy = kTRUE);
293
308 static TLegend *AddLegend(Double_t x1 = 0.7, Double_t x2 = 0.9,
309 Double_t y1 = 0.7, Double_t y2 = 0.9);
310
325 static Double_t TextWidthPx(const TString &text, Double_t size_px = 30.0,
326 Int_t font = 43);
327
346 static Double_t LegendWidthPx(const std::vector<TString> &labels,
347 const TString &header = "",
348 Double_t size_px = 30.0, Double_t margin = 0.25,
349 Double_t padding_px = 15.0);
350
368 static Double_t FigureScale(TVirtualPad *pad,
369 Int_t reference_width_px = 1200);
370
392 static void ScaleFigure(TVirtualPad *pad, Int_t reference_width_px = 1200,
393 Int_t reference_height_px = 800);
394
411 static void ScalePad(TVirtualPad *pad, Double_t scale,
412 Int_t reference_width_px = 1200,
413 Int_t reference_height_px = 800);
414
436 static void ScaleAxisFontsToPad(TH1 *frame, TVirtualPad *pad,
437 Double_t scale = 1.0,
438 Int_t reference_width_px = 1200,
439 Int_t reference_height_px = 800);
440
447 static void ScaleLegend(TLegend *legend, Double_t scale);
448
463 static void ScaleText(TText *text, TVirtualPad *pad, Double_t scale,
464 Int_t reference_height_px = 800);
465
483 static TLatex *DrawTitle(TVirtualPad *pad, const TString &title,
484 Double_t scale = 1.0);
485
494 static void ScaleLine(TAttLine *line, Double_t scale);
495
503 static void ScaleMarker(TAttMarker *marker, Double_t scale);
504
518 static TLatex *AddText(const TString label, Double_t x = 0.9,
519 Double_t y = 0.85, Double_t angle = 0);
520
523 static std::vector<Int_t> GetDefaultColors();
524
533 static TString GetRandomName();
534
537 static Width_t GetLineWidth() { return line_width_; };
538
539private:
540 static PlotSaveFormat save_format_;
541 static Bool_t preferences_set_;
542 static Width_t line_width_;
543 static TString plots_base_dir_;
544 static void SetTurboPalette();
545 static void WarnIfNotConfigured(const TString method_name);
546 static void PlotPullHistogram(TGraph *residuals, const TString &output_name,
547 const TString &output_subdirectory);
548};
549
550#endif
PlotSaveOptions
Which axis scalings SaveFigure() should write out.
@ kBOTH
Both, as two separate files.
@ kLOG
Logarithmic y only, written with a log_ filename prefix.
@ kLINEAR
Linear y only.
PlotSaveFormat
Output file format for saved figures.
@ kPNG
PNG raster output; line width 2.
@ kPDF
PDF vector output; line width 1.
All-static helpers for publication-quality ROOT graphics.
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 void ConfigureAndDrawGraph(TGraph *graph, Int_t color, const TString title="")
ConfigureGraph() followed by Draw().
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 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 GetRandomName()
Generate a name unlikely to collide with existing ROOT objects.
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 standa...
static void ConfigureGraph(TGraph *graph, Int_t color, const TString title="")
Apply the preferred style to a graph without drawing it.
static void ScaleLegend(TLegend *legend, Double_t scale)
Set a legend's text to AddLegend()'s 30 px times a scale.
static void SetStylePreferences(PlotSaveFormat save_format=PlotSaveFormat::kPNG)
Install the global ROOT style and choose the output format.
static void Configure2DHistogram(TH2 *hist, TCanvas *canvas, const TString title="")
Apply the preferred style to a 2-D histogram and its canvas.
static void ConfigureAndDrawHistogram(TH1 *hist, Int_t color, const TString title="")
ConfigureHistogram() followed by Draw("HIST").
static void ConfigureHistogram(TH1 *hist, Int_t color, const TString title="")
Apply the preferred style to a 1-D histogram without drawing it.
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 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 TCanvas * GetConfiguredCanvas(Bool_t logy=kFALSE)
Create a 1200x800 canvas with grid and ticks already set up.
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 ScaleLine(TAttLine *line, Double_t scale)
Multiply a line width by a scale, rounded to a whole pixel.
static void ScaleText(TText *text, TVirtualPad *pad, Double_t scale, Int_t reference_height_px=800)
Scale a text object with its pad.
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...
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 Width_t GetLineWidth()
Line width the Configure* methods apply.
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 std::vector< Int_t > GetDefaultColors()
A palette of visually distinct ROOT colour indices.
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 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 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 ScaleMarker(TAttMarker *marker, Double_t scale)
Multiply a marker size by a scale.
static void ConfigureAndDraw2DHistogram(TH2 *hist, TCanvas *canvas, const TString title="")
Configure2DHistogram() followed by Draw("COLZ").
static TString GetPlotsBaseDir()
Current base directory for saved figures, without trailing slash.