65 #define isatty _isatty
66 #define STDERR_FILENO 2
71 const TString TMVA::Timer::fgClassName =
"Timer";
72 const Int_t TMVA::Timer::fgNbins = 16;
74 ClassImp(TMVA::Timer);
79 TMVA::Timer::Timer(
const char* prefix, Bool_t colourfulOutput )
80 : Timer(0, prefix, colourfulOutput)
90 TMVA::Timer::Timer( Int_t ncounts,
const char* prefix, Bool_t colourfulOutput )
91 : fNcounts ( ncounts ),
92 fPrefix ( strcmp(prefix,
"")==0?Timer::fgClassName:TString(prefix) ),
93 fColourfulOutput( colourfulOutput ),
94 fPreviousProgress(-1),
95 fOutputToFile(!isatty(STDERR_FILENO)),
96 fProgressBarStringLength (0),
97 fLogger ( new MsgLogger( fPrefix.Data() ) )
99 fColourfulOutput = fColourfulOutput && !fOutputToFile;
106 TMVA::Timer::~Timer(
void )
111 void TMVA::Timer::Init( Int_t ncounts )
121 void TMVA::Timer::Reset(
void )
123 TStopwatch::Start( kTRUE );
124 fPreviousProgress = -1;
125 fPreviousTimeEstimate.Clear();
131 Double_t TMVA::Timer::ElapsedSeconds(
void )
133 Double_t rt = TStopwatch::RealTime(); TStopwatch::Start( kFALSE );
140 TString TMVA::Timer::GetElapsedTime( Bool_t Scientific )
142 return SecToText( ElapsedSeconds(), Scientific );
148 TString TMVA::Timer::GetLeftTime( Int_t icounts )
150 Double_t leftTime = ( icounts <= 0 ? -1 :
151 icounts > fNcounts ? -1 :
152 Double_t(fNcounts - icounts)/Double_t(icounts)*ElapsedSeconds() );
154 return SecToText( leftTime, kFALSE );
160 void TMVA::Timer::DrawProgressBar()
162 fProgressBarStringLength = 0;
165 std::clog << fLogger->GetPrintedSource();
166 std::clog <<
"Please wait ";
169 std::clog <<
"." << std::flush;
175 void TMVA::Timer::DrawProgressBar( TString theString )
178 std::clog << fLogger->GetPrintedSource();
180 std::clog << gTools().Color(
"white_on_green") << gTools().Color(
"dyellow") <<
"[" << gTools().Color(
"reset");
182 std::clog << gTools().Color(
"white_on_green") << gTools().Color(
"dyellow") << theString << gTools().Color(
"reset");
184 std::clog << gTools().Color(
"white_on_green") << gTools().Color(
"dyellow") <<
"]" << gTools().Color(
"reset");
186 for (
int i = fProgressBarStringLength; i < theString.Length (); ++i)
188 std::clog <<
"\r" << std::flush;
189 fProgressBarStringLength = theString.Length ();
196 void TMVA::Timer::DrawProgressBar( Int_t icounts,
const TString& comment )
198 if (!gConfig().DrawProgressBar())
return;
201 if (icounts > fNcounts-1) icounts = fNcounts-1;
202 if (icounts < 0 ) icounts = 0;
203 Int_t ic = Int_t(Float_t(icounts)/Float_t(fNcounts)*fgNbins);
205 auto timeLeft = this->GetLeftTime( icounts );
208 if (ic == fPreviousProgress && timeLeft == fPreviousTimeEstimate && icounts != fNcounts-1)
return;
211 if (ic != fPreviousProgress) {
212 std::clog << Int_t((100*(icounts+1))/Float_t(fNcounts)) <<
"%, time left: " << timeLeft << std::endl;
213 fPreviousProgress = ic;
217 fPreviousProgress = ic;
218 fPreviousTimeEstimate = timeLeft;
220 std::clog << fLogger->GetPrintedSource();
221 if (fColourfulOutput) std::clog << gTools().Color(
"white_on_green") << gTools().Color(
"dyellow") <<
"[" << gTools().Color(
"reset");
222 else std::clog <<
"[";
223 for (Int_t i=0; i<ic; i++) {
224 if (fColourfulOutput) std::clog << gTools().Color(
"white_on_green") << gTools().Color(
"dyellow") <<
">" << gTools().Color(
"reset");
225 else std::clog <<
">";
227 for (Int_t i=ic+1; i<fgNbins; i++) {
228 if (fColourfulOutput) std::clog << gTools().Color(
"white_on_green") << gTools().Color(
"dyellow") <<
"." << gTools().Color(
"reset");
229 else std::clog <<
".";
231 if (fColourfulOutput) std::clog << gTools().Color(
"white_on_green") << gTools().Color(
"dyellow") <<
"]" << gTools().Color(
"reset");
232 else std::clog <<
"]" ;
235 if (fColourfulOutput) {
236 std::clog << gTools().Color(
"reset") <<
" " ;
237 std::clog <<
"(" << gTools().Color(
"red") << Int_t((100*(icounts+1))/Float_t(fNcounts)) <<
"%" << gTools().Color(
"reset")
240 << timeLeft << gTools().Color(
"reset") <<
") ";
244 std::clog <<
"(" << Int_t((100*(icounts+1))/Float_t(fNcounts)) <<
"%"
245 <<
", " <<
"time left: " << timeLeft <<
") ";
248 std::clog <<
"[" << comment <<
"] ";
250 std::clog <<
"\r" << std::flush;
256 TString TMVA::Timer::SecToText( Double_t seconds, Bool_t Scientific )
const
259 if (Scientific ) out = Form(
"%.3g sec", seconds );
260 else if (seconds < 0 ) out =
"unknown";
261 else if (seconds <= 300) out = Form(
"%i sec", Int_t(seconds) );
263 if (seconds > 3600) {
264 Int_t h = Int_t(seconds/3600);
265 if (h <= 1) out = Form(
"%i hr : ", h );
266 else out = Form(
"%i hrs : ", h );
268 seconds = Int_t(seconds)%3600;
270 Int_t m = Int_t(seconds/60);
271 if (m <= 1) out += Form(
"%i min", m );
272 else out += Form(
"%i mins", m );
275 return (fColourfulOutput) ? gTools().Color(
"red") + out + gTools().Color(
"reset") : out;