Time axis while reading from external file

Hello,

I am currently trying to change my x-axis to a timeaxis. I’ve read a couple forum posts that discuss this, but I haven’t found a method that has worked while reading data from an external file. My current code is provided below, but all the different methods that I tried have been unfruitful, should I approach this in a different way? My data file “2014ex3.dat” consists of a set of values listed below, and I am trying to convert the date into a time axis, all while keeping values on the y-axis. I was thinking that maybe there is a way to convert the %lg itself into a time string? I am open to any methods and comments. I’ve attached a sample plot of what I would like it to turn into in ROOT. Thank you for your time.

int flux(){
TCanvas* c=new TCanvas();
c->SetGrid();

TGraph graph("2014ex3.dat","%*lg %lg %*lg %*lg %lg");
graph.SetTitle("GOES Normalized Flux;Time;Normalized Flux");
graph.DrawClone("E3AL");
graph.SetMarkerStyle(kCircle);
graph.SetFillColor(0);
graph.DrawClone("PESame");

graph.Print();
}

—Data file (2014ex3.dat)—

//# Date >1 MeV >10 MeV >100 MeV >0.8 MeV >2 MeV % of bkgd //1 2 3 4 5 6 7 8 1 20110101 5.8e+04 1.4e+04 3.6e+03 8.6e+07 8.2e+05 -999.99 2 20110102 1.3e+05 1.4e+04 3.6e+03 1.2e+08 8.7e+05 -999.99 . . .


I don’t know which forums you looked at, but your macro doesn’t do any attempt to create a time axis.
May be you should look at this page:
root.cern.ch/how/how-create-axis-time-units

Thank you for the reply, I learned a lot from it. Changing the axis worked and I my x-axis is now in time, but now I am running into an error that reads my date column from my external file in epoch (20110101 -> 8/21/1970). Is there a way to convert the input via a command? I’ve tried the TDatime, but it only changes the axis. Thank you.

int flux(){
    
TCanvas* c=new TCanvas();
c->SetGrid();

//gStyle->SetTitleH(0.08);
//TDatime da(2011,02,28,12,00,00);
//gStyle->SetTimeOffset(da.Convert());

TGraph graph("2014ex3.dat","%*lg %lg %*lg %*lg %lg"); 
graph.SetMarkerStyle(20); 

graph.Draw("ap"); 

graph.GetXaxis()->SetTimeDisplay(1); 
graph.GetXaxis()->SetNdivisions(-503); 
//graph.GetXaxis()->SetTimeFormat("%Y-%m-%d %H:%M"); 

graph.GetXaxis()->SetTimeFormat("%Y/%m/%d");
graph.GetXaxis()->SetTimeOffset(0,"gmt");

graph.SetTitle("GOES Normalized Flux;Time;Normalized Flux");

graph.DrawClone("E3AL");
graph.SetMarkerStyle(kCircle);
graph.SetFillColor(0);
graph.DrawClone("PESame");

graph.Print();

}

You need to translate the dates on x axis using TDatime. Therefore you should read the file using scanf or cin and convert the vector into dates using TDatime. I let you write the code reading the file. Here is a simple macro showing the result you will get (I just put the 2 dates you have in your at the beginning of the macro).

void flux(){

   TCanvas* c=new TCanvas();
   c->SetGrid();

   TDatime da1(2011,1,1,0,0,0);
   TDatime da2(2011,1,2,0,0,0);

   double x[2],y[2];

   y[0] = 3600.;
   y[1] = 3600.;
   x[0] = da1.Convert();
   x[1] = da2.Convert();

   TGraph graph(2,x,y);
   graph.SetMarkerStyle(20);

   graph.Draw("ap");

   graph.GetXaxis()->SetTimeDisplay(1);
   graph.GetXaxis()->SetNdivisions(-503);

   graph.GetXaxis()->SetTimeFormat("%Y/%m/%d");
   graph.GetXaxis()->SetTimeOffset(0,"gmt");

   graph.SetTitle("GOES Normalized Flux;Time;Normalized Flux");

   graph.DrawClone("E3AL");
   graph.SetMarkerStyle(kCircle);
   graph.SetFillColor(0);
   graph.DrawClone("PESame");

   graph.Print();
}