Timer.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: Timer.cxx 31458 2009-11-30 13:58:20Z stelzer $   
00002 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Class  : Timer                                                                 *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Implementation (see header file for description)                          *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
00015  *      Joerg Stelzer   <Joerg.Stelzer@cern.ch>  - CERN, Switzerland              *
00016  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
00017  *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
00018  *                                                                                *
00019  * Copyright (c) 2005:                                                            *
00020  *      CERN, Switzerland                                                         * 
00021  *      MPI-K Heidelberg, Germany                                                 * 
00022  *                                                                                *
00023  * Redistribution and use in source and binary forms, with or without             *
00024  * modification, are permitted according to the terms listed in LICENSE           *
00025  * (http://tmva.sourceforge.net/LICENSE)                                          *
00026  **********************************************************************************/
00027 
00028 //_______________________________________________________________________
00029 //                                                                      
00030 // Timing information for training and evaluation of MVA methods  
00031 // 
00032 // Usage:
00033 //
00034 //    TMVA::Timer timer( Nloops, "MyClassName" ); 
00035 //    for (Int_t i=0; i<Nloops; i++) {
00036 //      ... // some code
00037 //
00038 //      // now, print progress bar:
00039 //      timer.DrawProgressBar( i );
00040 //
00041 //      // **OR** text output of left time (never both !)
00042 //      fLogger << " time left: " << timer.GetLeftTime( i ) << Endl;
00043 //
00044 //    } 
00045 //    fLogger << "MyClassName" << ": elapsed time: " << timer.GetElapsedTime() 
00046 //            << Endl;    
00047 //
00048 // Remark: in batch mode, the progress bar is quite ugly; you may 
00049 //         want to use the text output then
00050 //_______________________________________________________________________
00051 
00052 #include <iomanip>
00053 
00054 #include "TMVA/Timer.h"
00055 
00056 #ifndef ROOT_TMVA_Config
00057 #include "TMVA/Config.h"
00058 #endif
00059 #ifndef ROOT_TMVA_Tools
00060 #include "TMVA/Tools.h"
00061 #endif
00062 #ifndef ROOT_TMVA_MsgLogger
00063 #include "TMVA/MsgLogger.h"
00064 #endif
00065 
00066 const TString TMVA::Timer::fgClassName = "Timer";
00067 const Int_t   TMVA::Timer::fgNbins     = 24;  
00068 
00069 ClassImp(TMVA::Timer)
00070 
00071 //_______________________________________________________________________
00072 TMVA::Timer::Timer( const char* prefix, Bool_t colourfulOutput )
00073    : fNcounts        ( 0 ),
00074      fPrefix         ( strcmp(prefix,"")==0?Timer::fgClassName:TString(prefix) ),
00075      fColourfulOutput( colourfulOutput ),
00076      fLogger         ( new MsgLogger( fPrefix.Data() ) )
00077 {
00078    // constructor
00079    Reset();
00080 }
00081 
00082 //_______________________________________________________________________
00083 TMVA::Timer::Timer( Int_t ncounts, const char* prefix, Bool_t colourfulOutput  )
00084    : fNcounts        ( ncounts ),
00085      fPrefix         ( strcmp(prefix,"")==0?Timer::fgClassName:TString(prefix) ),
00086      fColourfulOutput( colourfulOutput ),
00087      fLogger         ( new MsgLogger( fPrefix.Data() ) )
00088 {
00089    // standard constructor: ncounts gives the total number of counts that 
00090    // the loop will iterate through. At each call of the timer, the current
00091    // number of counts is provided by the user, so that the timer can obtain
00092    // the due time from linearly interpolating the spent time.
00093    Reset();
00094 }
00095 
00096 //_______________________________________________________________________
00097 TMVA::Timer::~Timer( void )
00098 {
00099    // destructor
00100    delete fLogger;
00101 }
00102 
00103 void TMVA::Timer::Init( Int_t ncounts )
00104 {
00105    // timer initialisation
00106    fNcounts = ncounts;  
00107    Reset();
00108 }
00109 
00110 //_______________________________________________________________________
00111 void TMVA::Timer::Reset( void )
00112 {
00113    // resets timer
00114    TStopwatch::Start( kTRUE );
00115 }
00116 
00117 //_______________________________________________________________________
00118 Double_t TMVA::Timer::ElapsedSeconds( void ) 
00119 {
00120    // computes elapsed tim in seconds
00121    Double_t rt = TStopwatch::RealTime(); TStopwatch::Start( kFALSE );
00122    return rt;
00123 }
00124 //_______________________________________________________________________
00125 
00126 TString TMVA::Timer::GetElapsedTime( Bool_t Scientific ) 
00127 {
00128    // returns pretty string with elaplsed time
00129    return SecToText( ElapsedSeconds(), Scientific );
00130 }
00131 
00132 //_______________________________________________________________________
00133 TString TMVA::Timer::GetLeftTime( Int_t icounts ) 
00134 {
00135    // returns pretty string with time left
00136    Double_t leftTime = ( icounts <= 0 ? -1 :
00137                          icounts > fNcounts ? -1 :
00138                          Double_t(fNcounts - icounts)/Double_t(icounts)*ElapsedSeconds() );
00139 
00140    return SecToText( leftTime, kFALSE );
00141 }
00142 
00143 //_______________________________________________________________________
00144 void TMVA::Timer::DrawProgressBar() 
00145 {
00146    // draws the progressbar
00147    fNcounts++;
00148    if (fNcounts == 1) {
00149       std::clog << fLogger->GetPrintedSource();
00150       std::clog << "Please wait ";
00151    }
00152 
00153    std::clog << "." << std::flush;
00154 }
00155 
00156 //_______________________________________________________________________
00157 void TMVA::Timer::DrawProgressBar( TString theString ) 
00158 {
00159    // draws a string in the progress bar
00160    std::clog << fLogger->GetPrintedSource();
00161 
00162    std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
00163 
00164    std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << theString << gTools().Color("reset");
00165 
00166    std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
00167 
00168    std::clog << "\r" << std::flush; 
00169 }
00170 
00171 //_______________________________________________________________________
00172 void TMVA::Timer::DrawProgressBar( Int_t icounts, const TString& comment  ) 
00173 {
00174    // draws progress bar in color or B&W
00175    // caution: 
00176 
00177    if (!gConfig().DrawProgressBar()) return;
00178 
00179    // sanity check:
00180    if (icounts > fNcounts-1) icounts = fNcounts-1;
00181    if (icounts < 0         ) icounts = 0;
00182    Int_t ic = Int_t(Float_t(icounts)/Float_t(fNcounts)*fgNbins);
00183 
00184    std::clog << fLogger->GetPrintedSource();
00185    if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
00186    else                  std::clog << "[";
00187    for (Int_t i=0; i<ic; i++) {
00188       if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << ">" << gTools().Color("reset"); 
00189       else                  std::clog << ">";
00190    }
00191    for (Int_t i=ic+1; i<fgNbins; i++) {
00192       if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "." << gTools().Color("reset"); 
00193       else                  std::clog << ".";
00194    }
00195    if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
00196    else                  std::clog << "]" ;
00197 
00198    // timing information
00199    if (fColourfulOutput) {
00200       std::clog << gTools().Color("reset") << " " ;
00201       std::clog << "(" << gTools().Color("red") << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%" << gTools().Color("reset")
00202                << ", " 
00203                << "time left: "
00204                << this->GetLeftTime( icounts ) << gTools().Color("reset") << ") ";
00205    }
00206    else {
00207       std::clog << "] " ;
00208       std::clog << "(" << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%" 
00209                << ", " << "time left: " << this->GetLeftTime( icounts ) << ") ";
00210    }
00211    if (comment != "") {
00212       std::clog << "[" << comment << "]  ";
00213    }
00214    std::clog << "\r" << std::flush; 
00215 }
00216 
00217 //_______________________________________________________________________
00218 TString TMVA::Timer::SecToText( Double_t seconds, Bool_t Scientific ) const
00219 {
00220    // pretty string output
00221    TString out = "";
00222    if      (Scientific    ) out = Form( "%.3g sec", seconds );
00223    else if (seconds <  0  ) out = "unknown";
00224    else if (seconds <= 300) out = Form( "%i sec", Int_t(seconds) );
00225    else {
00226       if (seconds > 3600) {
00227          Int_t h = Int_t(seconds/3600);
00228          if (h <= 1) out = Form( "%i hr : ", h );
00229          else        out = Form( "%i hrs : ", h );
00230       
00231          seconds = Int_t(seconds)%3600;
00232       }
00233       Int_t m = Int_t(seconds/60);
00234       if (m <= 1) out += Form( "%i min", m );
00235       else        out += Form( "%i mins", m );
00236    }
00237 
00238    return (fColourfulOutput) ? gTools().Color("red") + out + gTools().Color("reset") : out;
00239 }
00240 

Generated on Tue Jul 5 15:25:37 2011 for ROOT_528-00b_version by  doxygen 1.5.1