Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TGaxis with function is not properly drawn #282

Closed
AlbertoPdRF opened this issue Jan 5, 2024 · 8 comments
Closed

TGaxis with function is not properly drawn #282

AlbertoPdRF opened this issue Jan 5, 2024 · 8 comments
Assignees

Comments

@AlbertoPdRF
Copy link

AlbertoPdRF commented Jan 5, 2024

I'm trying to add a secondary axis to one of my figures. This secondary axis is a non-linear transformation of the main one. After quite a bit of work, I got it working with:

#include "TCanvas.h"
#include "TF1.h"
#include "TGaxis.h"
#include "TH1.h"
#include "TROOT.h"
#include "TStyle.h"

void tgaxis() {
  gROOT->SetStyle("ATLAS");
  gStyle->SetOptStat(0);

  const auto c = new TCanvas();
  const auto min = 42;
  const auto max = 108;
  const auto h = new TH1F("", "", max - min, min, max);
  const auto xa = h->GetXaxis();
  xa->SetTitle("Axis");
  h->Draw();
  c->SetTickx(0);
  c->GetPad(0)->SetTopMargin(0.15375);
  c->Update();

  const auto f = new TF1("f", "(72.289/x)**2", min, max);
  const auto f_min = f->Eval(max);
  const auto f_max = f->Eval(min);
  const auto inv_f = new TF1("inv_f", "72.289/sqrt(x)", f_min, f_max);
  const auto y_max = gPad->GetUymax();
  const auto a = new TGaxis(min, y_max, max, y_max, f_min, f_max, 510, "-");
  a->SetFunction("inv_f");
  a->ImportAxisAttributes(xa);
  a->SetTitle("Secondary axis");
  a->Draw("same");

  c->SaveAs("axis.png");
  c->SaveAs("axis.root");
}

While this works with the old graphics, the result with JSROOT is not the expected one, as can be seen in the images below.

Old graphics (correct result):

Old graphics

New graphics (incorrect result):

New graphics

@linev linev self-assigned this Jan 16, 2024
@linev linev added the wontfix label Jan 16, 2024
@linev
Copy link
Member

linev commented Jan 16, 2024

Hi,

It is very special usecase.
Function "inv_f" is not stored with TGaxis and therefore cannot be used on JSROOT side.

The only I can do is try to support such example in TWebCanvas, where custom I/O rules can be applied.

@AlbertoPdRF
Copy link
Author

Thanks for the explanation! Wouldn't it make sense to modify TGaxis so the function is saved though? It seems like something necessary.

@linev
Copy link
Member

linev commented Jan 16, 2024

It will be difficult to change TGaxis class - such change will have strong side effects with I/O.

Let keep issue open - may be later I will provide some workaround in JSROOT.

linev added a commit that referenced this issue Feb 8, 2024
If TGaxis configured with function
and it not provided within pad,
try to load id from the file
@linev
Copy link
Member

linev commented Feb 8, 2024

I add support of function loading from the file. But function must be stored together with canvas.
Code should look like:

  TFile::Open("gaxis.root", "recreate");
  c->Write("c");
  inv_f->Write("inv_f");
  delete gFile;

It is part of dev branch: https://jsroot.gsi.de/dev/examples.htm?more#misc_gaxis_func

Relatively soon it will be release as 7.6.0 version

@AlbertoPdRF
Copy link
Author

Awesome! This should work for me, thanks! 🙂

However, the current axis appearing on https://jsroot.gsi.de/dev/examples.htm?more#misc_gaxis_func is still the incorrect one.

@linev
Copy link
Member

linev commented Feb 9, 2024

@AlbertoPdRF
Copy link
Author

Ok, just tested https://jsroot.gsi.de/dev/?file=https://jsroot.gsi.de/files/gaxis_func.root&item=c;1 and it works too. Awesome! 😄

@linev
Copy link
Member

linev commented Feb 23, 2024

Now supported in https://github.com/root-project/jsroot/releases/tag/7.6.0

@linev linev closed this as completed Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants