Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
Timer.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Timer *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation (see header file for description) *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
16  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
17  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
18  * *
19  * Copyright (c) 2005: *
20  * CERN, Switzerland *
21  * MPI-K Heidelberg, Germany *
22  * *
23  * Redistribution and use in source and binary forms, with or without *
24  * modification, are permitted according to the terms listed in LICENSE *
25  * (http://tmva.sourceforge.net/LICENSE) *
26  **********************************************************************************/
27 
28 /*! \class TMVA::Timer
29 \ingroup TMVA
30 Timing information for training and evaluation of MVA methods
31 
32 Usage:
33 
34 ~~~ {.cpp}
35  TMVA::Timer timer( Nloops, "MyClassName" );
36  for (Int_t i=0; i<Nloops; i++) {
37  ... // some code
38 
39  // now, print progress bar:
40  timer.DrawProgressBar( i );
41 
42  // **OR** text output of left time (never both !)
43  fLogger << " time left: " << timer.GetLeftTime( i ) << Endl;
44 
45  }
46  fLogger << "MyClassName" << ": elapsed time: " << timer.GetElapsedTime()
47  << Endl;
48 ~~~
49 
50 Remark: in batch mode, the progress bar is quite ugly; you may
51  want to use the text output then
52 */
53 
54 #include "TMVA/Timer.h"
55 
56 #include "TMVA/Config.h"
57 #include "TMVA/MsgLogger.h"
58 #include "TMVA/Tools.h"
59 
60 #include "TStopwatch.h"
61 
62 #include <iomanip>
63 #ifdef _MSC_VER
64 #include <io.h>
65 #define isatty _isatty
66 #define STDERR_FILENO 2
67 #else
68 #include <unistd.h>
69 #endif
70 
71 const TString TMVA::Timer::fgClassName = "Timer";
72 const Int_t TMVA::Timer::fgNbins = 16;
73 
74 ClassImp(TMVA::Timer);
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// constructor
78 
79 TMVA::Timer::Timer( const char* prefix, Bool_t colourfulOutput )
80  : Timer(0, prefix, colourfulOutput)
81 {
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// standard constructor: ncounts gives the total number of counts that
86 /// the loop will iterate through. At each call of the timer, the current
87 /// number of counts is provided by the user, so that the timer can obtain
88 /// the due time from linearly interpolating the spent time.
89 
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() ) )
98 {
99  fColourfulOutput = fColourfulOutput && !fOutputToFile;
100  Reset();
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// destructor
105 
106 TMVA::Timer::~Timer( void )
107 {
108  delete fLogger;
109 }
110 
111 void TMVA::Timer::Init( Int_t ncounts )
112 {
113  // timer initialisation
114  fNcounts = ncounts;
115  Reset();
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// resets timer
120 
121 void TMVA::Timer::Reset( void )
122 {
123  TStopwatch::Start( kTRUE );
124  fPreviousProgress = -1;
125  fPreviousTimeEstimate.Clear();
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// computes elapsed tim in seconds
130 
131 Double_t TMVA::Timer::ElapsedSeconds( void )
132 {
133  Double_t rt = TStopwatch::RealTime(); TStopwatch::Start( kFALSE );
134  return rt;
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// returns pretty string with elapsed time
139 
140 TString TMVA::Timer::GetElapsedTime( Bool_t Scientific )
141 {
142  return SecToText( ElapsedSeconds(), Scientific );
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// returns pretty string with time left
147 
148 TString TMVA::Timer::GetLeftTime( Int_t icounts )
149 {
150  Double_t leftTime = ( icounts <= 0 ? -1 :
151  icounts > fNcounts ? -1 :
152  Double_t(fNcounts - icounts)/Double_t(icounts)*ElapsedSeconds() );
153 
154  return SecToText( leftTime, kFALSE );
155 }
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 /// draws the progressbar
159 
160 void TMVA::Timer::DrawProgressBar()
161 {
162  fProgressBarStringLength = 0;
163  fNcounts++;
164  if (fNcounts == 1) {
165  std::clog << fLogger->GetPrintedSource();
166  std::clog << "Please wait ";
167  }
168 
169  std::clog << "." << std::flush;
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// draws a string in the progress bar
174 
175 void TMVA::Timer::DrawProgressBar( TString theString )
176 {
177 
178  std::clog << fLogger->GetPrintedSource();
179 
180  std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
181 
182  std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << theString << gTools().Color("reset");
183 
184  std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
185 
186  for (int i = fProgressBarStringLength; i < theString.Length (); ++i)
187  std::cout << " ";
188  std::clog << "\r" << std::flush;
189  fProgressBarStringLength = theString.Length ();
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 /// draws progress bar in color or B&W
194 /// caution:
195 
196 void TMVA::Timer::DrawProgressBar( Int_t icounts, const TString& comment )
197 {
198  if (!gConfig().DrawProgressBar()) return;
199 
200  // sanity check:
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);
204 
205  auto timeLeft = this->GetLeftTime( icounts );
206 
207  // do not redraw progress bar when neither time not ticks are different
208  if (ic == fPreviousProgress && timeLeft == fPreviousTimeEstimate && icounts != fNcounts-1) return;
209  // check if we are redirected to a file
210  if (fOutputToFile) {
211  if (ic != fPreviousProgress) {
212  std::clog << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%, time left: " << timeLeft << std::endl;
213  fPreviousProgress = ic;
214  }
215  return;
216  }
217  fPreviousProgress = ic;
218  fPreviousTimeEstimate = timeLeft;
219 
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 << ">";
226  }
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 << ".";
230  }
231  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
232  else std::clog << "]" ;
233 
234  // timing information
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")
238  << ", "
239  << "time left: "
240  << timeLeft << gTools().Color("reset") << ") ";
241  }
242  else {
243  std::clog << "] " ;
244  std::clog << "(" << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%"
245  << ", " << "time left: " << timeLeft << ") ";
246  }
247  if (comment != "") {
248  std::clog << "[" << comment << "] ";
249  }
250  std::clog << "\r" << std::flush;
251 }
252 
253 ////////////////////////////////////////////////////////////////////////////////
254 /// pretty string output
255 
256 TString TMVA::Timer::SecToText( Double_t seconds, Bool_t Scientific ) const
257 {
258  TString out = "";
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) );
262  else {
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 );
267 
268  seconds = Int_t(seconds)%3600;
269  }
270  Int_t m = Int_t(seconds/60);
271  if (m <= 1) out += Form( "%i min", m );
272  else out += Form( "%i mins", m );
273  }
274 
275  return (fColourfulOutput) ? gTools().Color("red") + out + gTools().Color("reset") : out;
276 }