Plotting Helper
plottingHelper.h
Go to the documentation of this file.
1 #ifndef __plottingHelper__
2 #define __plottingHelper__
3 
4 #include "TCanvas.h"
5 #include "TROOT.h"
6 #include "TLatex.h"
7 #include "TString.h"
8 #include "TFrame.h"
9 #include "TLegend.h"
10 #include "TLegendEntry.h"
11 #include "TGraph.h"
12 #include "TMarker.h"
13 #include "THStack.h"
14 #include "TMultiGraph.h"
15 #include "TLine.h"
16 
17 #include <iostream>
18 #include <vector>
19 #include <algorithm>
20 #include <utility>
21 #include <numeric>
22 #include <string>
23 #include <cassert>
24 
25 //#define LibraryPath /home/radek/Dropbox/patrick/undolding/PlottingHelper/libPlottingHelper.so
26 
27 //R__LOAD_LIBRARY(/home/radek/Dropbox/patrick/undolding/PlottingHelper/libPlottingHelper.so)
28 
29 
30 
34 namespace PlottingHelper {
35 
36 using namespace std;
37 
38 #ifndef SF
39  #define SF TString::Format
40 #endif
41 
42 
43 
45 struct Rectangle {
46  double fX, fY, fWidth, fHeight;
47 };
48 
49 
50 struct Point {
51  Point(): x(0), y(0) {}
52  Point(double _x, double _y): x(_x), y(_y) {}
53  double x, y;
54 };
55 
56 
57 
65 enum {
66  kPos1 = BIT(1),
67  kPos2 = BIT(2),
68  kPos3 = BIT(3),
69  kPos4 = BIT(4),
70  kPos5 = BIT(5),
71  kPos6 = BIT(6),
72  kPos7 = BIT(7),
73  kPos8 = BIT(8),
74  kPos9 = BIT(9),
75  kPos2c= BIT(10),
76  kPos4c= BIT(11),
77  kPos6c= BIT(12),
78  kPos8c= BIT(13)
79 };
80 
81 
82 TH1 *GetFrame();
83 TAxis *GetXaxis();
84 TAxis *GetYaxis();
85 TAxis *GetZaxis();
86 
87 double PxFontToRel(double px);
88 double RelFontToPx(double rel);
89 double GetAxisFractionX();
90 double GetAxisFractionY();
91 double TickAbsToRelX(double tick);
92 double TickAbsToRelY(double tick);
93 
94 void SetFonts(double pxX, double pxY = -1, double pxT = -1);
95 void SetTicks(double tickX, double tickY = -1);
96 void SetLabelOffsetX(double off);
97 void SetTitleOffsetX(double off);
98 void SetLabelOffsetY(double off);
99 void SetTitleOffsetY(double off);
100 void SetFontsTicks(double px, double tickX, double tickY = -1.);
101 void SetOffsets(double lX, double tX, double lY, double tY);
102 void SetFTO(vector<double> fonts, vector<double> ticks, vector<double> offsets);
103 
104 
105 void DrawLatex(TVirtualPad *pad1, TVirtualPad *pad2, double x, double y, TString text, double fSize=-1.0, TString style="");
106 void DrawLatex(TVirtualPad *pad, double x, double y, TString text, double fSize=-1.0, TString style="");
107 void DrawLatex(double x, double y, TString text, double fSize=-1.0, TString style="");
108 
109 void DrawLatexUp(TVirtualPad *pad1, TVirtualPad *pad2, double Offset, TString text, double fSize=-1.0, TString style="");
110 void DrawLatexDown(TVirtualPad *pad1,TVirtualPad *pad2, double Offset, TString text, double fSize=-1.0, TString style="");
111 void DrawLatexRight(TVirtualPad *pad1, TVirtualPad *pad2, double Offset, TString text, double fSize=-1.0, TString style="");
112 void DrawLatexLeft(TVirtualPad *pad1, TVirtualPad *pad2, double Offset, TString text, double fSize=-1.0, TString style="");
113 
114 void DrawLatexUp(TVirtualPad *pad, double Offset, TString text, double fSize=-1.0, TString style="");
115 void DrawLatexDown(TVirtualPad *pad, double Offset, TString text, double fSize=-1.0, TString style="");
116 void DrawLatexRight(TVirtualPad *pad, double Offset, TString text, double fSize=-1.0, TString style="");
117 void DrawLatexLeft(TVirtualPad *pad, double Offset, TString text, double fSize=-1.0, TString style="");
118 
119 void DrawLatexUp(double Offset, TString text, double fSize=-1.0, TString style="");
120 void DrawLatexDown(double Offset, TString text, double fSize=-1.0, TString style="");
121 void DrawLatexRight(double Offset, TString text, double fSize=-1.0, TString style="");
122 void DrawLatexLeft(double Offset, TString text, double fSize=-1.0, TString style="");
123 
124 
125 Point Px2NDC(Point p);
126 
127 
128 TString SetLayout(unsigned pos);
129 
133 struct RectangleNDC {
134  double fX, fY, fWidth, fHeight;
135  TLatex *lat;
136  TText *tex;
137  RectangleNDC() : lat(nullptr), tex(nullptr) {}
138 };
139 
140 
141 RectangleNDC GetNDC(TLatex *lat);
142 TLegend *newLegend(unsigned pos, int nCols = 1);
143 void GetLegendSizes(TLegend *leg, double &SizeX, double &SizeY, double &SizeYroot);
144 void PlaceLegends(vector<TLegend*> legs, bool keepRange = false);
145 void DrawLegends(vector<TLegend*> legs, bool keepRange=false);
146 
147 Point Abs2Px(Point p, double scaleUp, double scaleDn);
148 Point Px2NDC(Point p);
149 
150 
151 void UpdateFrame();
152 void CalcYaxisRange();
153 void SetLeftRight(double lMargin, double rMargin);
154 void SetTopBottom(double tMargin, double bMargin);
155 void DividePad(vector<double> xDivs, vector<double> yDivs);
156 void DivideTransparent(vector<double> divX, vector<double> divY, bool useMargins = true);
157 vector<double> merge(vector<double> v1, vector<double> v2={}, vector<double> v3={}, vector<double> v4={}, vector<double> v5={}, vector<double> v6={}, vector<double> v7={} );
158 vector<double> repeat(vector<double> x, int n);
159 vector<double> group(double frame, double space, int n);
160 
161 }
162 
163 #endif
TAxis * GetZaxis()
Return z-axis of the active frame.
double GetAxisFractionX()
Fraction of the pad width covered by x-axis.
double TickAbsToRelY(double tick)
Convert tick length in px of y-axis to the relative units.
double GetAxisFractionY()
Fraction of the pad height covered by y-axis.
Point Px2NDC(Point p)
TAxis * GetYaxis()
Return y-axis of the active frame.
Left bottom corner.
TAxis * GetXaxis()
Return x-axis of the active frame.
double RelFontToPx(double rel)
Transform font size from relative units to pixels.
Center of bottom side.
void SetLabelOffsetX(double off)
Set offset of the x-axis labels.
Right bottom corner.
void CalcYaxisRange()
Calculate the automatic range of the vertical axis and apply it.
Center of left side.
void PlaceLegends(vector< TLegend *> legs, bool keepRange=false)
Setup the position of legends provided in legs array.
void DrawLatexUp(TVirtualPad *pad1, TVirtualPad *pad2, double Offset, TString text, double fSize=-1.0, TString style="")
Draw latex up of frame1 and frame2.
void DivideTransparent(vector< double > divX, vector< double > divY, bool useMargins=true)
Construct the lattice of frames according to the provided x and y sizes using transparency trick...
Center of top side.
void SetLeftRight(double lMargin, double rMargin)
Set simultaneously left and right margin of active pad.
void SetOffsets(double lX, double tX, double lY, double tY)
Set offsets of x,y labels and titles.
Center of right side.
void SetFonts(double pxX, double pxY=-1, double pxT=-1)
Set text size of x- and y-axis titles and labels to the frame title.
void DividePad(vector< double > xDivs, vector< double > yDivs)
Construct the lattice of pads according to the provided x and y sizes.
vector< double > group(double frame, double space, int n)
Construct group of frames.
vector< double > repeat(vector< double > x, int n)
Repeat the provided pattern.
Arbitrary position.
void SetFontsTicks(double px, double tickX, double tickY=-1.)
Set of the fonts and ticks sizes.
void UpdateFrame()
Redraw the current frame and axis ticks.
void SetTopBottom(double tMargin, double bMargin)
Set simultaneously top and bottom margin of active pad.
The namespace of whole Plotting Helper utility.
double TickAbsToRelX(double tick)
Convert tick length in px of x-axis to the relative units.
Point Abs2Px(Point p, double scaleUp, double scaleDn)
void DrawLatexRight(TVirtualPad *pad1, TVirtualPad *pad2, double Offset, TString text, double fSize=-1.0, TString style="")
Draw latex right of frame1 and frame2.
void SetFTO(vector< double > fonts, vector< double > ticks, vector< double > offsets)
Set Fonts, Ticks and Offsets.
vector< double > merge(vector< double > v1, vector< double > v2={}, vector< double > v3={}, vector< double > v4={}, vector< double > v5={}, vector< double > v6={}, vector< double > v7={})
Merge vectors into one.
Contains coordinates object rectangle envelope.
void SetTitleOffsetX(double off)
Set offset of the x-axis title.
void SetLabelOffsetY(double off)
Set offset of the y-axis labels.
void DrawLegends(vector< TLegend *> legs, bool keepRange=false)
Draw legends provided in vector legs in such a way that there is no overlap.
void SetTicks(double tickX, double tickY=-1)
Set ticks sizes of x and y-axis in px.
TString SetLayout(unsigned pos)
Convert layout bit to string.
unsigned pos
void DrawLatexDown(TVirtualPad *pad1, TVirtualPad *pad2, double Offset, TString text, double fSize=-1.0, TString style="")
Draw latex down of frame1 and frame2.
void DrawLatexLeft(TVirtualPad *pad1, TVirtualPad *pad2, double Offset, TString text, double fSize=-1.0, TString style="")
Draw latex left of frame1 and frame2.
void SetTitleOffsetY(double off)
Set offset of the y-axis title.
Point(double _x, double _y)
TLegend * newLegend(unsigned pos, int nCols=1)
Define new legend at position pos with nCols columns.
Left top corner.
RectangleNDC GetNDC(TLatex *lat)
Calculates positions and height and width of rectangle which encapsulates TLatex. ...
Struct containing NDC sizes and positions of the corresponding TLatex.
Right top corner.
double PxFontToRel(double px)
Transform font size from px to the relative units.
TH1 * GetFrame()
The method returns current frame on the active pad.
void GetLegendSizes(TLegend *leg, double &SizeX, double &SizeY, double &SizeYroot)
Calculates sizes of the legend in NDC.
void DrawLatex(TVirtualPad *pad1, TVirtualPad *pad2, double x, double y, TString text, double fSize=-1.0, TString style="")
Draw latex in coordinates x,y give a hull of two frames in given TPads.