Dear ROOTERS,
When trying to plot data requested from a database I get strange offset when plotting
For example I want to plot data for 2007-08-05 12h, using a 6 hours windows (3Hours before, 3 after)
The TimeStamp interpretation is correct for the title, but there is something strange with the TGraph (or the AXIS ?) and the line which should be at the center.
If someone can explain me the reason for these offset (which are different for data and for the line) I could solve it adding some time by hand but that’s just ugly and not what I want to do
Here is my code (Commented example) The last part is the plotting the first part the request in database
//MYSQLPP is used for the mysql interface,
//I dont show here the DB connection (not interesting and saves space)
//Start by defining a time window
string evtID="Test";
TTimeStamp evttime(1186315200); // 2007 March 5th 12h00 GMT
TTimeStamp evttimemin(1186315200-3*3600);
TTimeStamp evttimemax(1186315200+3*3600);
//Write an SQL request and store time and value into 2 vectors
sql.str("");
evttime.GetDate(0,0,&yy,&mm,&dd);
sql << "SELECT `TimeStamp`,`Field` FROM BLS_Field_1S_";
if (mm<10) sql << "0";
sql <<mm;
if (yy<2010)sql<<"0";
sql <<yy-2000 << " WHERE `Timestamp` BETWEEN \""<< evttimemin.AsString("s") << "\"AND\""
<< evttimemax.AsString("s") <<"\" ORDER BY `Timestamp`;";
cout <<sql.str() <<endl;
try {
mysqlpp::StoreQueryResult res;
if (res=conn->query(sql.str().c_str()).store()){
for (size_t i = 0; i < res.num_rows(); ++i) {
mysqlpp::DateTime dt=mysqlpp::DateTime(res[i][0]);
v_ts.push_back(float(time_t(dt))); // is a std::vector<float>
v_field.push_back(float(res[i][1])); // is a std::vector<float>
}
}
}
catch(mysqlpp::BadQuery e) {
cerr << " Error making the request \n";
cerr <<" \t Error #" << e.errnum() <<"\n";
cerr <<" \t "<<e.what();
cerr <<" The request was : \n";
cerr << sql.str() << endl;
}
if (v_ts.size()==0) {
cout << evtID << "ZERO " << endl;
cout << sql.str() << endl;
}
//Construct a TGraph using the vector's content
TGraph gr_field(v_ts.size(),&v_ts.front(),&v_field.front());
TCanvas can0("plt","plot",800,600);
gr_field.SetTitle(("Event # "+evtID + " " +evttime.AsString("s")).c_str());
gr_field.SetMinimum(-5000);
gr_field.SetMaximum(+10000);
gr_field.GetXaxis()->SetTimeDisplay(1);
gr_field.GetXaxis()->SetRangeUser(*v_ts.begin(),*v_ts.end());
//Set Reference time to 1970 01 01 should be that for a TimeStamp isn't it ?
gr_field.GetXaxis()->SetTimeFormat("%H:%M %F01-01-1970 00:00:00");
gr_field.GetXaxis()->SetTimeFormat("%H:%M");
//Draw a TLine at the event's time position
TLine l(evttime.GetSec(),-5000.,evttime.GetSec(),+10000);
l.SetLineWidth(1);
l.SetLineColor(kBlue);
l.SetLineStyle(2);
gr_field.SetLineColor(kRed);
gr_field.SetMarkerStyle(1);
gr_field.SetMarkerColor(kRed);
gr_field.Draw("AL");
l.Draw("SAME");
can0.SaveAs((evtID+".png").c_str());
exit(0);
}
As you see the TLine is not at all at 12h
moreover the data are plotted between 10h and 16h but should be plotted between 09h and 15h
Last point my SQL request is
SELECT `TimeStamp`,`Field` FROM BLS_Field_1S_0807 WHERE `Timestamp` BETWEEN "2007-08-05 09:00:00"AND"2007-08-05 15:00:00" ORDER BY `Timestamp`;
So it is really between 09h and 15h
What’s the problem with the plotting ?
Thanks for your answers