PT-Cooling-Log 1.0
PTCoolingLogViewer
src/LogViewer.cc
00001 #include "LogViewer.hh"
00002 #include "ArgParser.hh"
00003 #include "PtEventView.hh"
00004 #include "PtMwView.hh"
00005 #include "PtFlowHe3View.hh"
00006 #include "PtFlowHe4View.hh"
00007 
00008 #include <TObject.h>
00009 #include <TCollection.h>
00010 #include <TList.h>
00011 #include <TPgSQLServer.h>
00012 #include <TSQLResult.h>
00013 #include <TSQLRow.h>
00014 #include <TGraph.h>
00015 #include <TCanvas.h>
00016 #include <TVirtualPad.h>
00017 #include <TH1.h>
00018 #include <TROOT.h>
00019 #include <TDatime.h>
00020 #include <TLatex.h>
00021 #include <TLegend.h>
00022 
00023 #include <iostream>
00024 #include <iomanip>
00025 #include <fstream>
00026 #include <sstream>
00027 #include <cstdlib>
00028 #include <cmath>
00029 #include <algorithm>
00030 #include <cstring>
00031 
00032 #include <sqlite3.h>
00033 
00034 using namespace std;
00035 
00036 const string LogViewer::tgCoolingData = "tbl_coolingdata";
00037 const string LogViewer::tgCryoTmpr    = "view_cryo_tmpr";
00038 const string LogViewer::tgFlwHe4      = "view_flw_he4";
00039 const string LogViewer::tgMw          = "view_mw";
00040 const string LogViewer::tgPrssCryo    = "view_prss_cryo";
00041 const string LogViewer::tgPrssHe3     = "view_prss_he3";
00042 const string LogViewer::tgPrssHe4     = "view_prss_he4";
00043 
00044 const std::string LogViewer::sqlEpoch    = "EXTRACT(EPOCH FROM datetime)";
00045 const std::string LogViewer::sqlTimeZone = "EXTRACT(TIMEZONE FROM datetime)";
00046 
00047 LogViewer::LogViewer( int& argc, char* argv[] ) :
00048   Switch(),
00049   TApplication( "logViewer", &argc, argv ),
00050   server_( NULL ), 
00051   ptViews_( 0 ),
00052   opHost_( NULL ), opPort_( NULL ), opUser_( NULL ), opPass_( NULL ),
00053   opDB_( NULL ), opTable_( NULL ), opStart_( NULL ), opEnd_( NULL ),
00054   opList_( NULL ), opView_( NULL ), 
00055   opTitleSize_( NULL ), opTitleOffset_( NULL ),
00056   opLabelSize_( NULL ), opEventLog_( NULL ),
00057   opTLeftMargin_( NULL ), opTRightMargin_( NULL ),
00058   c_( NULL ),
00059   tmin_( 0 ), tmax_( 0 ), twidth_( 0 ), toffset_( 0 ),
00060   tLeftMargin_( 0.1 ), tRightMargin_( 0.15 )
00061 {
00062   
00063   this->setupOptions( argc, argv );
00064   
00065   if( c_ == NULL ) 
00066     c_ = new TCanvas( "logViewCanvas", "PT Log Viewer", 1169, 827 );
00067 
00068   // To be carefull to call addView or setup..View method.
00069   // At the moment, only 8 pads are created.
00070   // The number of views must be less than 8, including event log view.
00071   // So 7 view at maximum are allowd to be created.
00072   c_->Divide( 1, 8, 1, 0 );
00073   
00074   this->setupCryoTmprView();          // Cryostat temperature 
00075   this->setupPrssCryoView();          // Cryostat pressure
00076   this->setupPrssHe3View();           // He-3 Pressure
00077   this->addView( new PtFlowHe3View ); // He-3 Flow
00078   this->setupPrssHe4View();           // He-4 Pressure
00079   this->addView( new PtFlowHe4View ); // He-4 Flow
00080   this->addView( new PtMwView );
00081   
00082   // event view must be addted at the end  
00083   ptViews_.push_back( new PtEventView );;
00084   ptViews_.back()->pad( c_->cd( 1 ) ); 
00085   
00086   tmin_ = this->parseTimeStr( this->startTime() );
00087   tmax_ = this->parseTimeStr( this->endTime() );
00088   twidth_ = tmax_ - tmin_;
00089 
00090   this->connect();
00091 }
00092 
00093 LogViewer::~LogViewer(){
00094   if( server_ ) server_->Close();
00095   for( int i = 0; i < ptViews_.size(); i++ ) delete ptViews_[ i ];
00096 }
00097 
00098 int LogViewer::parseTimeStr( const std::string& data ){
00099   struct tm t;
00100   memset( &t, 0, sizeof( struct tm ) );
00101   strptime( data.c_str(), "%F %T %z", &t );
00102   return mktime( &t );
00103 }
00104 
00105 void LogViewer::setupOptions( const int& argc, char* argv[] ){
00106   
00107   // option handling preperation
00108   opHost_ = new ArgParser( ArgParser::REQUIRED, "133.24.27.182",
00109                            "server IP address" );
00110   opPort_ = new ArgParser( ArgParser::REQUIRED, "58163",
00111                            "Port number" );
00112   opUser_ = new ArgParser( ArgParser::REQUIRED, "ptlab", "user name" );
00113   
00114   opPass_ = new ArgParser( ArgParser::REQUIRED, "80MHz", "password" );
00115 
00116   opDB_   = new ArgParser( ArgParser::REQUIRED, "ptdata", "DB name" );
00117   
00118   opTable_ = new ArgParser( ArgParser::REQUIRED, "tbl_coolingdata",
00119                             "Table name" );
00120   
00121   opStart_ = new ArgParser( ArgParser::REQUIRED, "2012-03-30 08:00:00+9",
00122                             "Stat time stamp:  ex.) 2012-03-30 08:00:00+9" );
00123   
00124   opEnd_ = new ArgParser( ArgParser::REQUIRED, "2012-03-31 00:00:00+9",
00125                           "End time stamp:  ex.) 2012-03-30 24:00:00+9" );
00126   
00127   opList_ = new ArgParser( ArgParser::OPTIONAL, "", "List data name" );
00128 
00129   opView_ = new ArgParser( ArgParser::REQUIRED, "all",
00130                            tgCryoTmpr + "|" + tgFlwHe4 + "|" + tgMw + "|" + 
00131                            tgPrssCryo + "|" + tgPrssHe3 + "|" + tgPrssHe4 );
00132   
00133   opTitleSize_ = new ArgParser( ArgParser::REQUIRED, "0.11",
00134                                 "Y-axis Title Font Size" );
00135   
00136   opTitleOffset_ = new ArgParser( ArgParser::REQUIRED, "0.3",
00137                                   "Y-axis Title Offset" );
00138   
00139   opLabelSize_ = new ArgParser( ArgParser::REQUIRED, "0.1",
00140                                 "Y-axis Label Font Size" );
00141   
00142   opEventLog_ = new ArgParser( ArgParser::REQUIRED, "ptlog.db3",
00143                                "Event Log DB file name" );
00144   
00145   opTLeftMargin_ = new ArgParser( ArgParser::REQUIRED, "0.05",
00146                                   "Time Window Left Margin" );
00147   
00148   opTRightMargin_ = new ArgParser( ArgParser::REQUIRED, "0.15",
00149                                    "Time Window Right Margin" );
00150   
00151   opOutput_ = new ArgParser( ArgParser::REQUIRED, "PT_LOG",
00152                              "Output file basename" );
00153   
00154   Switch& opt = this->option();
00155   opt[ "host" ]     = opHost_;
00156   opt[ "port" ]     = opPort_; 
00157   opt[ "user" ]     = opUser_;
00158   opt[ "password" ] = opPass_;
00159   opt[ "db" ]       = opDB_;
00160   opt[ "table" ]    = opTable_;
00161   opt[ "start" ]    = opStart_;
00162   opt[ "end" ]      = opEnd_;
00163   opt[ "list" ]     = opList_;
00164   opt[ "view" ]     = opView_;
00165   opt[ "tSize" ]    = opTitleSize_;
00166   opt[ "tOffset" ]  = opTitleOffset_;
00167   opt[ "lSize" ]    = opLabelSize_;
00168   opt[ "eventLog" ] = opEventLog_;
00169   opt[ "margin-left" ] = opTLeftMargin_;
00170   opt[ "margin-right" ] = opTRightMargin_;
00171   opt[ "output" ]   = opOutput_;
00172   
00173   // parse the arguments list
00174   vector< string > args( argc );
00175   for( int i = 0; i < argc; i++ ) args[ i ] = string( argv[ i ] );
00176   this->parse( args );
00177   
00178 }
00179 
00180 // ----------------------------------------------------------------------- //
00181 //
00182 //
00183 //
00184 // ----------------------------------------------------------------------- //
00185 PtView* LogViewer::addView( PtView *view ){
00186   ptViews_.push_back( view );
00187   PtView* v = ptViews_.back();
00188   if( c_ != NULL )  v->pad( c_->cd( ptViews_.size() + 1 ) );
00189   return v;
00190 }
00191 
00192 PtView* LogViewer::addView(){
00193   return this->addView( new PtView );
00194 }
00195 
00196 PtView* LogViewer::setupCryoTmprView(){
00197   
00198   PtView& v = *( this->addView() );
00199   
00200   v.name()  = tgCryoTmpr;
00201   v.title() = "Temperature";
00202   
00203   v.add( "sep",       "Separator",  kMagenta );
00204   v.add( "evap",      "Evaporator", kGreen );
00205   v.add( "still",     "Still",      kCyan );
00206   v.add( "stillref",  "Still(Ref)", kBlue );
00207   v.add( "mc",        "M.C.",       kRed );
00208 
00209   return &v;
00210 }
00211 
00212 PtView* LogViewer::setupPrssCryoView(){
00213   
00214   PtView& v = *( this->addView() );
00215 
00216   v.name() = tgPrssCryo;
00217   v.title() = "Press. (Cryo)";
00218   v.pad()->SetLogy( true );
00219 
00220   v.add( "pirani",    "Pirani", kBlue );
00221   v.add( "cc10",      "CC10",   kRed );
00222 
00223   return &v;
00224 }
00225 
00226 PtView* LogViewer::setupPrssHe3View(){
00227   
00228   PtView& v = *( this->addView() );
00229 
00230   v.name() = tgPrssHe3;
00231   v.title() = "Press. (He3)";
00232   v.pad()->SetLogy( true );
00233   
00234   v.add( "inlet",       "In-let",  kCyan );
00235   v.add( "outlet",      "Out-let", kMagenta );
00236 
00237   return &v;
00238 }
00239 
00240 PtView* LogViewer::setupPrssHe4View(){
00241   
00242   PtView& v = *( this->addView() );
00243 
00244   v.name() = tgPrssHe4;
00245   v.title() = "Press. (He4)";
00246   v.pad()->SetLogy( true );
00247 
00248   v.add( "he4b1",     "Booster", kBlue );
00249   v.add( "he4vp",     "Vapour",  kRed );
00250 
00251   return &v;
00252 }
00253 
00254 std::string LogViewer::url() const {
00255   return 
00256     string( "pgsql://" ) + (*opHost_)() + string(":") +
00257     (*opPort_)() + string("/") + (*opDB_)();
00258 }
00259 
00260 void LogViewer::connect(){
00261   if( server_ == NULL ){
00262     cout << "Connecting: " << this->url() << endl;
00263     server_ = 
00264       new TPgSQLServer( this->url().c_str(), (*opUser_)().c_str(), (*opPass_)().c_str() );
00265   }
00266 }
00267 
00268 std::string LogViewer::startTime(){
00269   return (*opStart_)();
00270 }
00271 
00272 std::string LogViewer::endTime(){
00273   return (*opEnd_)();
00274 }
00275 
00276 void LogViewer::printList(){
00277   
00278   if( server_ == NULL ) this->connect();
00279   
00280   string sql = 
00281     "SELECT column_name FROM information_schema.columns WHERE table_catalog='ptdata' and table_name='tbl_coolingdata' ORDER BY ordinal_position";
00282   
00283   TSQLResult *res = server_->Query( sql.c_str() );
00284   int nField = res->GetFieldCount();
00285   TSQLRow    *row;
00286   while( row = res->Next() ){
00287     for( int i = 0; i < nField; i++ ){
00288       cout << row->GetField( i ) << endl;
00289     }
00290   }
00291 
00292 }
00293 
00294 void LogViewer::run(){
00295   
00296   
00297   Switch& opt = this->option();
00298   if( opt[ "help" ]->exist() ) return;
00299 
00300 
00301   if( opList_->exist() ) this->printList();
00302   else {
00303     if( (*opView_)() != "all" ) {
00304       //      vector< PtView* >::iterator itr
00305       //        std::find( ptViews_.begin(), ptViews_.end(), PtView( (*opView_)() ) );
00306       //      if( itr != ptViews_.end() ) this->query( *itr );
00307     } else {
00308       
00309       // load data at first
00310       cout << "Fetching LOG data... " << endl;
00311       for( int i = 0; i < ptViews_.size(); i++ ) {
00312         ptViews_[ i ]->query( this );
00313         ptViews_[ i ]->draw();
00314         ptViews_[ i ]->analysis();
00315         c_->Draw();
00316       }
00317       
00318       c_->Print( string( (*opOutput_)() + ".png" ).c_str() );
00319       c_->Print( string( (*opOutput_)() + ".pdf" ).c_str() );
00320       
00321     }
00322   }
00323 
00324 }
00325 
00326 double LogViewer::tleft(){
00327   return atof( (*opTLeftMargin_)().c_str() );
00328 }
00329 
00330 double LogViewer::tright(){
00331   return atof( (*opTRightMargin_)().c_str() );
00332 }
00333 
00334 double LogViewer::tmin(){
00335   return tmin_ - this->tleft() * twidth_ - toffset_;
00336 }
00337 
00338 double LogViewer::tmax(){
00339   return tmax_ + this->tright() * twidth_ - toffset_;
00340 }
00341 
00342 double LogViewer::labelSize(){
00343   return atof( (*opLabelSize_)().c_str() );
00344 }
00345 
00346 double LogViewer::titleSize(){
00347   return atof( (*opTitleSize_)().c_str() );
00348 }
00349 
00350 double LogViewer::titleOffset(){
00351   return atof( (*opTitleOffset_)().c_str() );
00352 }
00353 
00354 
00355 std::string LogViewer::output(){
00356   return (*opOutput_)();
00357 }
00358 
00359 std::string LogViewer::eventLog(){
00360   return (*opEventLog_)();
00361 }
 全て クラス ファイル 関数 変数