#ifndef __JROOT__JLEGEND__ #define __JROOT__JLEGEND__ #include #include "TLegend.h" #include "TPad.h" #include "TStyle.h" /** * \author mdejong */ namespace JROOT {} namespace JPP { using namespace JROOT; } namespace JROOT { /** * Get legend. * * \param width width [characters] * \param height height [lines] * \param option position [TR TL BL BR] * \param factor factor font size * \return legend */ inline TLegend* getLegend(const Int_t width, const Int_t height, const std::string option, const Double_t factor = 1.0) { using namespace std; gPad->Update(); const Double_t X1 = 0.0 + gPad->GetLeftMargin(); const Double_t X2 = 1.0 - gPad->GetRightMargin(); const Double_t Y1 = 0.0 + gPad->GetBottomMargin(); const Double_t Y2 = 1.0 - gPad->GetTopMargin(); const Double_t w = gPad->XtoPixel(gPad->GetX2()); const Double_t h = gPad->YtoPixel(gPad->GetY1()); const Double_t w1 = fabs(gPad->XtoPixel(gPad->GetX2()) - gPad->XtoPixel(gPad->GetX1())); const Double_t h1 = fabs(gPad->YtoPixel(gPad->GetY2()) - gPad->YtoPixel(gPad->GetY1())); const Double_t mw = 2.0; // extra width const Double_t mh = 0.5; // extra height const Double_t eps = 0.005; // extra space [NDC] const Double_t ts = gStyle->GetStatFontSize() * factor; // text size [NDC] const Double_t ch = ts * (w < h ? w : h); // text size [pixel] Double_t W = (width + mw) * (ch/w1) * factor; // width [NDC] Double_t H = (height + mh) * (ch/h1) * factor; // height [NDC] Double_t fc = 1.0; // extra scaling factor if (fc * W > X2 - X1 - 2*eps) { fc *= (X2 - X1 - 2*eps) / W; } if (fc * H > Y2 - Y1 - 2*eps) { fc *= (Y2 - Y1 - 2*eps) / H; } W *= fc; H *= fc; // default position is top-right Double_t x1 = X2 - W - eps; Double_t y1 = Y2 - H - eps; Double_t x2 = X2 - eps; Double_t y2 = Y2 - eps; if (option.find('T') != string::npos && option.find('B') == string::npos) { // top y1 = Y2 - H - eps; y2 = Y2 - eps; } if (option.find('B') != string::npos && option.find('T') == string::npos) { // bottom y1 = Y1 + eps; y2 = Y1 + H + eps; } if (option.find('R') != string::npos && option.find('L') == string::npos) { // right x1 = X2 - W - eps; x2 = X2 - eps; } if (option.find('L') != string::npos && option.find('R') == string::npos) { // left x1 = X1 + eps; x2 = X1 + W + eps; } TLegend* lg = new TLegend(x1, y1, x2, y2); //lg->SetFillStyle(4000); lg->SetFillColor(kWhite); lg->SetBorderSize(0); lg->SetTextAlign(12); lg->SetTextFont(gStyle->GetStatFont()); lg->SetTextSize(ts); return lg; } } #endif