Setup of TAxis after zoom

Hello,

I have some time dependant data displayed in a TGraph. I created a function which makes sure that the ticks gets on rounded seconds ( for example at 1s , 2s , … and not 1.1s, 2.2s, …)
It is mainly to avoid the ticks labels with ms (such as “10:00:00s15”) when ticks are placed at non-integer seconds.

TGraph* xaxis_time(TGraph *gr)
{
  gr->GetXaxis()->SetTimeDisplay(1);
  time_t xmin=floor(gr->GetXaxis()->GetXmin());
  time_t xmax=ceil(gr->GetXaxis()->GetXmax());
  gr->GetXaxis()->SetLimits(xmin,xmax); 	//set rounded limits
  UInt_t xticks_interval=ceil((xmax-xmin)/10.);//interval between ticks to get ~10 ticks
  UInt_t ndiv=((xmax-xmin)/xticks_interval);	//number of ticks 
  gr->GetXaxis()->SetNdivisions(-ndiv); 		//set number of ticks
  gr->GetXaxis()->SetLabelSize(0.03);
  gr->GetXaxis()->SetTimeFormat("%H:%M:%S");
  return gr;
}

However whenever the axis is zoomed, that setup is obviously lost.
Is there a way to call that function each time the axis get zoomed ?
Otherwise is there a way to insure that the ticks get automatically placed on rounded time ?

Yes, you can add a TExec in your pad:

root.cern.ch/doc/master/classTExec.html

Thank you very much, it looks it is exactly what I needed !
However I have trouble to make it to work. When I do

TGraph *gr_spectra;
TExec *setup_xaxis_time;
gr_spectra=new TGraph(nentries_ploted,this->timestamp_float+first_entry,this->data+first_entry);
setup_xaxis_time= new TExec("xaxis_time","xaxis_time(gr_spectra)");
gr_spectra->GetListOfFunctions()->Add(setup_xaxis_time);
gr_spectra->Draw("AL*");

I get the error: "Error: Symbol gr_spectra is not defined in current scope ".
However from the 1st example in the link you gave me, it looked like local variables could be used within TExec.

can you send a small running reproducer ?

here is the content of file test.C

void plot(void){
  TGraph *gr_spectra;
  TExec *setup_xaxis_time;
  TExec *test;
  const Int_t npoints=3;
  Double_t xaxis[npoints] = {1455805368,1455805369,1455805370};
  Double_t yaxis[npoints] = {10.,20.,30.};
  gr_spectra=new TGraph(npoints,xaxis,yaxis);
  test= new TExec("test","test()");
  setup_xaxis_time= new TExec("xaxis_time","xaxis_time(gr_spectra)");
  gr_spectra->GetListOfFunctions()->Add(test);
  gr_spectra->GetListOfFunctions()->Add(setup_xaxis_time);
  gr_spectra->Draw("AL*");
}

TGraph* xaxis_time(TGraph *gr)
{
  gr->GetXaxis()->SetTimeDisplay(1);
  gr->GetXaxis()->SetTimeFormat("%H:%M:%S");
  return gr;
}

void test(void)
{
  cout<<"it works"<<endl;
}

below is the output when run with root -l test.C:
Processing plot.C…
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
it works
Error: Symbol gr_spectra is not defined in current scope (tmpfile):1:
*** Interpreter error recovered ***

May be this:


TGraph *gr_spectra;

void renier(void){
  TExec *setup_xaxis_time;
  TExec *test;
  const Int_t npoints=3;
  Double_t xaxis[npoints] = {1455805368,1455805369,1455805370};
  Double_t yaxis[npoints] = {10.,20.,30.};
  gr_spectra=new TGraph(npoints,xaxis,yaxis);
  test= new TExec("test","test()");
  setup_xaxis_time= new TExec("xaxis_time","xaxis_time()");
  gr_spectra->GetListOfFunctions()->Add(test);
  gr_spectra->GetListOfFunctions()->Add(setup_xaxis_time);
  gr_spectra->Draw("AL*");
}

void xaxis_time()
{
  gr_spectra->GetXaxis()->SetTimeDisplay(1);
  gr_spectra->GetXaxis()->SetTimeFormat("%H:%M:%S");
}

void test(void)
{
  cout<<"it works"<<endl;
}

It works, thank you.