#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()) * (X2 - X1) / 1.0; const Double_t h = gPad->YtoPixel(gPad->GetY1()) * (Y2 - Y1) / 1.0; const Double_t ch = gStyle->GetStatFontSize() * (w < h ? w : h); const Double_t fx = 1.00 * factor * ch/w; // estimated font size [NDC] const Double_t fy = 1.00 * factor * ch/h; // estimated font size [NDC] const Double_t eps = 0.005; // extra space [NDC] const Int_t nc = 2; // number of characters reserved for margin Int_t wd = width; if (wd < 2) { wd = 2; } Double_t W = (wd) * fx; // width [NDC] Double_t H = (height) * fy; // height [NDC] Double_t fc = factor; // 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; int halign = 1; int valign = 2; if (option.find('T') != string::npos && option.find('B') == string::npos) { // top y1 = Y2 - H - eps; y2 = Y2 - eps; //valign = 1; } if (option.find('B') != string::npos && option.find('T') == string::npos) { // bottom y1 = Y1 + eps; y2 = Y1 + H + eps; //valign = 3; } if (option.find('R') != string::npos && option.find('L') == string::npos) { // right x1 = X2 - W - eps; x2 = X2 - eps; //halign = 3; } if (option.find('L') != string::npos && option.find('R') == string::npos) { // left x1 = X1 + eps; x2 = X1 + W + eps; //halign = 1; } TLegend* lg = new TLegend(x1, y1, x2, y2); //lg->SetFillStyle(4000); lg->SetFillColor(kWhite); lg->SetBorderSize(0); lg->SetTextAlign(halign*10 + valign); lg->SetTextFont(gStyle->GetStatFont()); lg->SetTextSize(gStyle->GetStatFontSize() * factor); lg->SetMargin((Double_t) nc / (Double_t) (wd + nc)); return lg; } } #endif