TGaxis drawing second axis

ROOT version: 6.26.04

Dear ROOT experts

I have detector data which I want to display with two x-axis, one on the bottom in energy, and one on the top in channels. However, my code seems to not want to do this. Even though I plug in the user coordinates when drawing the axis, it does not appear in the correct position.
So far looking around tutorials and forum questions have not been able to solve my problem, so I made my own post.

Thanks in advance to anyone who might be able to help!

My code and the data file used:
Second_x_axis.c (1.4 KB)
data.root (635.6 KB)

The way the output of my code looks when I run it:

Welcome to the ROOT forum.
You need:

    TGaxis *axis = new TGaxis(gPad->GetUxmin(), TMath::Power(10, gPad->GetUymax()),
                              gPad->GetUxmax(), TMath::Power(10, gPad->GetUymax()),beginPoint ,endPoint, 10, "-"); //drawing the Gaxis using the user coordinates

Ah yes, it works now, thank you!

A small follow up question, when I zoom on the x-axis, the top axis does zoom in like the original axis. Is there a way to fix this? (Besides already choosing the right interval in my code).

double endPoint   = 800000;
double beginPoint = 0;
double nBins      = 200000;
double xmin, xmax;
TGaxis *ax;

void draw_axis()
   int ndiv = 10;
   double r = (endPoint-beginPoint)/(xmax-xmin);
   double wmin = r*(gPad->GetUxmin()-xmin)+beginPoint;
   double wmax = r*(gPad->GetUxmax()-xmin)+beginPoint;
                 gPad->GetUymax(),wmin ,wmax,ndiv,"-");

void Second_x_axis() {
   auto canv = new TCanvas("s1","second axis");
   auto f1  = new TFile("data1.root");
   auto *h1 = new TH1D("sim1", "current", nBins, beginPoint, endPoint);
   TTreeReader myReader1("DataTree", f1);
   TTreeReaderValue<double> myInjEnergy1(myReader1, "TRAP2");
   while (myReader1.Next()) { h1->Fill(*myInjEnergy1);}
   double p_0 = 1.21545e-02;  double p_1 = 6.48653e-05;  // 04-03-2024
   auto low_bin = h1->GetXaxis()->GetXmin();
   auto up_bin  = h1->GetXaxis()->GetXmax();
   xmin = low_bin*p_1+p_0;
   xmax = up_bin*p_1+p_0;
   h1->GetXaxis()->Set(nBins, xmin, xmax); //calibrating the x-axis
   TExec *draw_axis = new TExec("draw_axis","draw_axis()");
   ax = new TGaxis();
   h1->GetXaxis()->SetTitle("Energy [MeV]");

This works perfectly, thank you for taking the time to help me!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.