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