Jpp 20.0.0-195-g190c9e876
the software that should make you happy
Loading...
Searching...
No Matches
JLegend.hh
Go to the documentation of this file.
1#ifndef __JROOT__JLEGEND__
2#define __JROOT__JLEGEND__
3
4#include <string>
5#include <math.h>
6
7#include "TLegend.h"
8#include "TPad.h"
9#include "TStyle.h"
10
11
12/**
13 * \author mdejong
14 */
15
16namespace JROOT {}
17namespace JPP { using namespace JROOT; }
18
19namespace JROOT {
20
21 /**
22 * Get legend.
23 *
24 * \param width width [characters]
25 * \param height height [lines]
26 * \param option position [TR TL BL BR]
27 * \param factor factor font size
28 * \return legend
29 */
30 inline TLegend* getLegend(const Int_t width,
31 const Int_t height,
32 const std::string option,
33 const Double_t factor = 1.0)
34 {
35 using namespace std;
36
37 gPad->Update();
38
39 const Double_t X1 = 0.0 + gPad->GetLeftMargin();
40 const Double_t X2 = 1.0 - gPad->GetRightMargin();
41 const Double_t Y1 = 0.0 + gPad->GetBottomMargin();
42 const Double_t Y2 = 1.0 - gPad->GetTopMargin();
43
44 const Double_t w = gPad->XtoPixel(gPad->GetX2());
45 const Double_t h = gPad->YtoPixel(gPad->GetY1());
46
47 const Double_t w1 = fabs(gPad->XtoPixel(gPad->GetX2()) - gPad->XtoPixel(gPad->GetX1()));
48 const Double_t h1 = fabs(gPad->YtoPixel(gPad->GetY2()) - gPad->YtoPixel(gPad->GetY1()));
49
50 const Double_t mw = 2.0; // extra width
51 const Double_t mh = 0.5; // extra height
52 const Double_t eps = 0.015; // extra space [NDC]
53 const Double_t ts = gStyle->GetStatFontSize() * factor; // text size [NDC]
54 const Double_t ch = ts * (w < h ? w : h); // text size [pixel]
55
56 Double_t W = (width + mw) * (ch/w1) * factor; // width [NDC]
57 Double_t H = (height + mh) * (ch/h1) * factor; // height [NDC]
58
59 Double_t fc = 1.0; // extra scaling factor
60
61 if (fc * W > X2 - X1 - 2*eps) { fc *= (X2 - X1 - 2*eps) / W; }
62 if (fc * H > Y2 - Y1 - 2*eps) { fc *= (Y2 - Y1 - 2*eps) / H; }
63
64 W *= fc;
65 H *= fc;
66
67 // default position is top-right
68
69 Double_t x1 = X2 - W - eps;
70 Double_t y1 = Y2 - H - eps;
71 Double_t x2 = X2 - eps;
72 Double_t y2 = Y2 - eps;
73
74 if (option.find('T') != string::npos && option.find('B') == string::npos) { // top
75 y1 = Y2 - H - eps;
76 y2 = Y2 - eps;
77 }
78
79 if (option.find('B') != string::npos && option.find('T') == string::npos) { // bottom
80 y1 = Y1 + eps;
81 y2 = Y1 + H + eps;
82 }
83
84 if (option.find('R') != string::npos && option.find('L') == string::npos) { // right
85 x1 = X2 - W - eps;
86 x2 = X2 - eps;
87 }
88
89 if (option.find('L') != string::npos && option.find('R') == string::npos) { // left
90 x1 = X1 + eps;
91 x2 = X1 + W + eps;
92 }
93
94 TLegend* lg = new TLegend(x1, y1, x2, y2);
95
96 //lg->SetFillStyle(4000);
97 lg->SetFillColor(kWhite);
98 lg->SetBorderSize(0);
99 lg->SetTextAlign(12);
100 lg->SetTextFont(gStyle->GetStatFont());
101 lg->SetTextSize(ts);
102
103 return lg;
104 }
105}
106
107#endif
#define Y2(d1, c12, d2)
Definition LambertW.hh:82
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
TLegend * getLegend(const Int_t width, const Int_t height, const std::string option, const Double_t factor=1.0)
Get legend.
Definition JLegend.hh:30