00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
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
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
00090
00091
00092
00093 Reset();
00094 }
00095
00096
00097 TMVA::Timer::~Timer( void )
00098 {
00099
00100 delete fLogger;
00101 }
00102
00103 void TMVA::Timer::Init( Int_t ncounts )
00104 {
00105
00106 fNcounts = ncounts;
00107 Reset();
00108 }
00109
00110
00111 void TMVA::Timer::Reset( void )
00112 {
00113
00114 TStopwatch::Start( kTRUE );
00115 }
00116
00117
00118 Double_t TMVA::Timer::ElapsedSeconds( void )
00119 {
00120
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
00129 return SecToText( ElapsedSeconds(), Scientific );
00130 }
00131
00132
00133 TString TMVA::Timer::GetLeftTime( Int_t icounts )
00134 {
00135
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
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
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
00175
00176
00177 if (!gConfig().DrawProgressBar()) return;
00178
00179
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
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
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