TkN 2.3
Toolkit for Nuclei
Loading...
Searching...
No Matches
tkn_root_macros.cpp
1#include "tkn_root_macros.h"
2#include "tkn_config.h"
3
4#include "TImage.h"
5#include "TLatex.h"
6#include "TPad.h"
7
8using namespace tkn;
9using namespace std;
10
11namespace tkn{
12
13inline double XUserToNDC(double x) {
14 double umin = gPad->GetUxmin(), umax = gPad->GetUxmax();
15 double L = gPad->GetLeftMargin(), R = gPad->GetRightMargin();
16 double frac = (x - umin) / (umax - umin);
17 return L + frac * (1.0 - L - R);
18}
19
20inline double YUserToNDC(double y) {
21 double vmin = gPad->GetUymin(), vmax = gPad->GetUymax();
22 double B = gPad->GetBottomMargin(), T = gPad->GetTopMargin();
23 double frac = (y - vmin) / (vmax - vmin);
24 return B + frac * (1.0 - B - T);
25}
26
27void DrawLogoAndCredit(double x1,double y1,double width, double text_size, const char* creditTxt, const char *opt)
28{
29 TImage* logo = TImage::Open(Form("%s/icons/logo.png", TKN_SYS));
30 if (!logo) return;
31
32 auto current_pad = gPad;
33 if(!current_pad) return;
34
35 current_pad->Update();
36
37 double x1_ndc, y1_ndc, x2_ndc, y2_ndc;
38 double width_ndc;
39
40 if(!TString(opt).Contains("NDC")) {
41 x1_ndc = XUserToNDC(x1);
42 y1_ndc = YUserToNDC(y1);
43 double x2_ndc_from_user = XUserToNDC(x1 + width);
44 width_ndc = x2_ndc_from_user - x1_ndc;
45 }
46 else {
47 x1_ndc = x1;
48 y1_ndc = y1;
49 width_ndc = width;
50 }
51
52 double pw = abs(gPad->XtoAbsPixel(gPad->GetX2()) - gPad->XtoAbsPixel(gPad->GetX1()));
53 double ph = abs(gPad->YtoAbsPixel(gPad->GetY2()) - gPad->YtoAbsPixel(gPad->GetY1()));
54
55 double px_to_ndc = 1.0 / pw;
56 double py_to_ndc = 1.0 / ph;
57
58 // Get logo dimensions to preserve aspect ratio
59 double imgW = logo->GetWidth() * px_to_ndc;
60 double imgH = logo->GetHeight() * py_to_ndc;
61
62 if (imgW <= 0 || imgH <= 0) return;
63
64 double aspect = double(imgH) / double(imgW);
65
66 x2_ndc = x1_ndc + width_ndc;
67 y2_ndc = y1_ndc + width_ndc * aspect; // keep aspect ratio
68
69 TPad* padLogo = new TPad("padLogo","padLogo",x1_ndc,y1_ndc,x2_ndc,y2_ndc);
70 padLogo->SetMargin(0,0,0,0);
71 padLogo->SetFillColor(0);
72 padLogo->SetFillStyle(0);
73 padLogo->Draw();
74
75 padLogo->cd();
76 logo->Draw("");
77 current_pad->cd();
78
79 // Draw credit text just above the logo, aligned to its left
80 TLatex credit;
81 credit.SetNDC(true);
82 credit.SetTextFont(42);
83 credit.SetTextSize(text_size);
84 credit.SetTextColor(kGray+2);
85 credit.SetTextAlign(11); // left, bottom
86 credit.DrawLatex(x1_ndc, y2_ndc + 0.01, creditTxt);
87}
88
89}
Definition tklog.cpp:39
double XUserToNDC(double x)
double YUserToNDC(double y)
void DrawLogoAndCredit(double x1, double y1, double width, double text_size, const char *creditTxt, const char *opt)