PT-Cooling-Log 1.0
PTCoolingLogViewer
|
00001 #include "PtView.hh" 00002 #include "LogViewer.hh" 00003 00004 #include <TVirtualPad.h> 00005 #include <TH1.h> 00006 #include <TAxis.h> 00007 #include <TLegend.h> 00008 #include <TPgSQLServer.h> 00009 #include <TSQLResult.h> 00010 #include <TSQLRow.h> 00011 00012 #include <cmath> 00013 #include <cstdlib> 00014 #include <iostream> 00015 #include <fstream> 00016 #include <sstream> 00017 #include <iomanip> 00018 00019 using namespace std; 00020 00021 PtView::PtView() : 00022 vector< PtViewEntry >( 0 ), 00023 name_( "" ), title_( "" ), lv_( NULL ), 00024 pad_( NULL ), frame_( NULL ), 00025 ymin_( 0.0 ), ymax_( 1.0 ), xmin_( 0.0 ), xmax_( 100.0 ), 00026 startIndex_( 0 ) 00027 { 00028 this->addTimeEntries(); 00029 } 00030 00031 PtView::PtView( const string& name ) : 00032 vector< PtViewEntry >( 0 ), 00033 name_( name ), title_( "" ), lv_( NULL ), 00034 pad_( NULL ), frame_( NULL ), 00035 ymin_( 0.0 ), ymax_( 1.0 ), xmin_( 0.0 ), xmax_( 100.0 ), 00036 startIndex_( 0 ) 00037 { 00038 this->addTimeEntries(); 00039 } 00040 00041 PtView::PtView( const PtView& view ) : 00042 vector< PtViewEntry >( view ), 00043 name_( view.name_ ), title_( view.title_ ), lv_( view.lv_ ), 00044 pad_( view.pad_ ), frame_( view.frame_ ), 00045 ymin_( view.ymin_ ), ymax_( view.ymax_ ), xmin_( view.xmin_ ), xmax_( view.xmax_ ), 00046 startIndex_( view.startIndex_ ) 00047 { 00048 } 00049 00050 PtView::~PtView() { 00051 } 00052 00053 PtView& PtView::operator=( const PtView& view ){ 00054 if( this != &view ){ 00055 name_ = view.name_; 00056 title_ = view.title_; 00057 lv_ = view.lv_; 00058 pad_ = view.pad_; 00059 frame_ = view.frame_; 00060 ymin_ = view.ymin_; 00061 ymax_ = view.ymax_; 00062 xmin_ = view.xmin_; 00063 xmax_ = view.xmax_; 00064 startIndex_ = view.startIndex_; 00065 this->resize( view.size() ); 00066 for( int i = 0; i < view.size(); i++ ) (*this)[ i ] = view[ i ]; 00067 } 00068 return *this; 00069 } 00070 00071 void PtView::addTimeEntries(){ 00072 00073 this->add( ( name_ == LogViewer::tgCoolingData ? 00074 "EXTRACT( EPOCH FROM ( to_timestamp( tbl_coolingdata.datetime ) ) )" : 00075 LogViewer::sqlEpoch ), 00076 "epoch", kBlack ); 00077 00078 this->add( ( name_ == LogViewer::tgCoolingData ? 00079 "EXTRACT( TIMEZONE FROM ( to_timestamp( tbl_coolingdata.datetime ) ) )" : 00080 LogViewer::sqlTimeZone ), 00081 "TZ", kBlack ); 00082 00083 startIndex_ = this->size(); 00084 } 00085 00086 void PtView::setRange(){ 00087 00088 double x; 00089 ymin_ = ( pad_->GetLogy() ? 0.1 : 0.0 ); 00090 ymax_ = 1.0; 00091 00092 if( this->size() > startIndex_ ){ 00093 00094 TGraph& g = (*this)[ startIndex_ ].graph(); 00095 00096 int nPoint = g.GetN(); 00097 g.GetPoint( 0, x, ymin_ ); 00098 ymax_ = ymin_; 00099 00100 for( int i = startIndex_; i < this->size(); i++ ){ 00101 for( int j = 0; j < nPoint; j++ ){ 00102 double y; 00103 (*this)[ i ].graph().GetPoint( j, x, y ); 00104 ymin_ = ( y < ymin_ ? y : ymin_ ); 00105 ymax_ = ( y > ymax_ ? y : ymax_ ); 00106 } 00107 } 00108 } 00109 00110 double yh = ( pad_->GetLogy() ? 00111 log10( ymax_ ) - log10( ymin_ ) : 00112 ymax_ - ymin_ ); 00113 00114 ymin_ = ( pad_->GetLogy() ? 00115 pow( 10, log10( ymin_ ) - 0.1 * yh ) : 00116 ymin_ - 0.1 * yh ); 00117 00118 ymax_ = ( pad_->GetLogy() ? 00119 pow( 10, log10( ymax_ ) + 0.1 * yh ) : 00120 ymax_ + 0.1 * ( ymax_ - ymin_ ) ); 00121 00122 } 00123 00124 void PtView::drawLegend(){ 00125 if( this->size() <= startIndex_ ) return; 00126 TLegend *leg = new TLegend( 0.92, 0.02, 1.0, 0.97 ); 00127 leg->SetFillColor( kWhite ); 00128 for( int i = startIndex_; i < this->size(); i++ ){ 00129 TGraph& g = (*this)[ i ].graph(); 00130 leg->AddEntry( &g, (*this)[ i ].name().c_str(), "L" ); 00131 } 00132 leg->Draw(); 00133 } 00134 00135 void PtView::drawFrame(){ 00136 00137 xmin_ = lv_->tmin(); 00138 xmax_ = lv_->tmax(); 00139 00140 frame_ = pad_->DrawFrame( xmin_, ymin_, xmax_, ymax_ ); 00141 00142 double lsize = lv_->labelSize(); 00143 double tsize = lv_->titleSize(); 00144 double toffset = lv_->titleOffset(); 00145 00146 TAxis* xaxis = frame_->GetXaxis(); 00147 xaxis->SetTimeDisplay( 1 ); 00148 xaxis->SetTimeFormat( "%H:%M" ); 00149 xaxis->SetLabelSize( lsize ); 00150 00151 TAxis* yaxis = frame_->GetYaxis(); 00152 yaxis->CenterTitle( true ); 00153 yaxis->SetTitle( title_.c_str() ); 00154 yaxis->SetTitleSize( tsize ); 00155 yaxis->SetTitleOffset( toffset ); 00156 yaxis->SetLabelSize( lsize ); 00157 00158 } 00159 00160 void PtView::drawGraph(){ 00161 for( int i = startIndex_; i < this->size(); i++ ){ 00162 TGraph& g = (*this)[ i ].graph(); 00163 g.Draw( "L" ); 00164 } 00165 } 00166 00167 void PtView::draw(){ 00168 00169 if( pad_ == NULL ) return; 00170 pad_->cd(); 00171 00172 this->setRange(); 00173 this->drawFrame(); 00174 this->drawLegend(); 00175 this->drawGraph(); 00176 00177 } 00178 00179 00180 void PtView::query( LogViewer *lv ){ 00181 00182 lv_ = lv; 00183 00184 TSQLServer* srv = lv_->server(); 00185 if( srv == NULL ){ 00186 cout << "server has not been initialized yet." << endl; 00187 return; 00188 } 00189 00190 cout << "# loading data for " << this->name() << endl; 00191 00192 int nField = this->size(); 00193 if( this->size() < startIndex_ + 1 ) return; 00194 00195 vector< PtViewEntry >& ent = (*this); 00196 00197 00198 string sql( "SELECT " ); 00199 00200 for( int i = 0; i < nField; i++ ) 00201 sql += ent[ i ].field() + string( i != nField - 1 ? "," : " " ); 00202 00203 sql += "FROM " + this->name() + " "; 00204 00205 string tstamp = 00206 ( this->name() != LogViewer::tgCoolingData ? 00207 "datetime" : 00208 "to_timestamp( tbl_coolingdata.datetime )" ); 00209 00210 sql += 00211 string( "WHERE " + tstamp + " >='" ) + lv_->startTime() + 00212 string( "' AND " + tstamp + " <= '" ) + lv_->endTime() + 00213 string( "'" ); 00214 00215 // cout << "SQL: " << sql << endl; 00216 00217 TSQLResult *res = srv->Query( sql.c_str() ); 00218 if( res == NULL ) { 00219 cerr << "Failed to get Query results from Server." << endl; 00220 return; 00221 } 00222 00223 int nRow = res->GetRowCount(); 00224 for( int i = startIndex_; i < nField; i++ ) ent[ i ].graph().Set( nRow ); 00225 00226 // -- output file 00227 string output = string( lv_->output() + "_" + this->name() + ".dat" ); 00228 cout << "Opening: " << output << endl; 00229 ofstream ofs( output.c_str() ); 00230 00231 // -- header 00232 ofs << "# Table View: " << this->name() << endl; 00233 ofs << "# Start: " << lv_->startTime() << endl; 00234 ofs << "# End: " << lv_->endTime() << endl; 00235 ofs << "#" << flush; 00236 ofs << setw(12) << "datetime" << endl; 00237 for( int i = startIndex_; i < nField; i++ ) ofs << setw(12) << ent[ i ].name() << flush; 00238 ofs << endl; 00239 00240 TSQLRow *row; 00241 int counter( 0 ); 00242 while( row = res->Next() ){ 00243 ofs << " " << flush; 00244 00245 lv_->toffset() = atof( row->GetField( 1 ) ); 00246 double t = atof( row->GetField( 0 ) ) - lv_->toffset(); 00247 00248 ofs << setw(12) << (int)t << flush; 00249 for( int i = 2; i < nField; i++ ){ 00250 ofs << setw(12) << row->GetField( i ) << flush; 00251 ent[ i ].graph().SetPoint( counter, 00252 t, atof( row->GetField( i ) ) ); 00253 } 00254 ofs << endl; 00255 counter++; 00256 } 00257 00258 ofs.close(); 00259 } 00260 00261 void PtView::analysis(){ 00262 } 00263 00264 void PtView::add( const string& field, const string& title, const int& color ){ 00265 this->push_back( PtViewEntry() ); 00266 this->back().set( field, title, color ); 00267 }