Logo ROOT   master
Reference Guide
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::gConfig().SetDrawProgressBar(true);
36 
37  TMVA::Timer timer( Nloops, "MyClassName" );
38  for (Int_t i=0; i<Nloops; i++) {
39  ... // some code
40 
41  // now, print progress bar:
42  timer.DrawProgressBar( i );
43 
44  // **OR** text output of left time (never both !)
45  fLogger << " time left: " << timer.GetLeftTime( i ) << Endl;
46 
47  }
48  fLogger << "MyClassName" << ": elapsed time: " << timer.GetElapsedTime()
49  << Endl;
50 ~~~
51 
52 Remark: in batch mode, the progress bar is quite ugly; you may
53  want to use the text output then
54 
55 Note that by default in TMVA::Config the drawing of the
56 progress bar is switched off. To have the progress bar visible you need
57 to enable it by calling TMVA::gConfig().SetDrawProgressBar(true)
58 
59 */
60 
61 #include "TMVA/Timer.h"
62 
63 #include "TMVA/Config.h"
64 #include "TMVA/MsgLogger.h"
65 #include "TMVA/Tools.h"
66 
67 #include "TStopwatch.h"
68 
69 #include <iomanip>
70 #ifdef _MSC_VER
71 #include <io.h>
72 #define isatty _isatty
73 #define STDERR_FILENO 2
74 #else
75 #include <unistd.h>
76 #endif
77 
78 const TString TMVA::Timer::fgClassName = "Timer";
79 const Int_t TMVA::Timer::fgNbins = 16;
80 
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// constructor
85 
86 TMVA::Timer::Timer( const char* prefix, Bool_t colourfulOutput )
87  : Timer(0, prefix, colourfulOutput)
88 {
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 /// standard constructor: ncounts gives the total number of counts that
93 /// the loop will iterate through. At each call of the timer, the current
94 /// number of counts is provided by the user, so that the timer can obtain
95 /// the due time from linearly interpolating the spent time.
96 
97 TMVA::Timer::Timer( Int_t ncounts, const char* prefix, Bool_t colourfulOutput )
98  : fNcounts ( ncounts ),
99  fPrefix ( strcmp(prefix,"")==0?Timer::fgClassName:TString(prefix) ),
100  fColourfulOutput( colourfulOutput ),
101  fPreviousProgress(-1),
102  fOutputToFile(!isatty(STDERR_FILENO)),
103  fProgressBarStringLength (0),
104  fLogger ( new MsgLogger( fPrefix.Data() ) )
105 {
107  Reset();
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// destructor
112 
114 {
115  delete fLogger;
116 }
117 
118 void TMVA::Timer::Init( Int_t ncounts )
119 {
120  // timer initialisation
121  fNcounts = ncounts;
122  Reset();
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// resets timer
127 
128 void TMVA::Timer::Reset( void )
129 {
131  fPreviousProgress = -1;
132  fPreviousTimeEstimate.Clear();
133 }
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// computes elapsed tim in seconds
137 
139 {
141  return rt;
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// returns pretty string with elapsed time
146 
148 {
149  return SecToText( ElapsedSeconds(), Scientific );
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// returns pretty string with time left
154 
156 {
157  Double_t leftTime = ( icounts <= 0 ? -1 :
158  icounts > fNcounts ? -1 :
159  Double_t(fNcounts - icounts)/Double_t(icounts)*ElapsedSeconds() );
160 
161  return SecToText( leftTime, kFALSE );
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// draws the progressbar
166 
168 {
169  fProgressBarStringLength = 0;
170  fNcounts++;
171  if (fNcounts == 1) {
172  std::clog << fLogger->GetPrintedSource();
173  std::clog << "Please wait ";
174  }
175 
176  std::clog << "." << std::flush;
177 }
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// draws a string in the progress bar
181 
183 {
184 
185  std::clog << fLogger->GetPrintedSource();
186 
187  std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
188 
189  std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << theString << gTools().Color("reset");
190 
191  std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
192 
193  for (int i = fProgressBarStringLength; i < theString.Length (); ++i)
194  std::cout << " ";
195  std::clog << "\r" << std::flush;
196  fProgressBarStringLength = theString.Length ();
197 }
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// draws progress bar in color or B&W
201 /// caution:
202 
204 {
205  if (!gConfig().DrawProgressBar()) return;
206 
207  // sanity check:
208  if (icounts > fNcounts-1) icounts = fNcounts-1;
209  if (icounts < 0 ) icounts = 0;
210  Int_t ic = Int_t(Float_t(icounts)/Float_t(fNcounts)*fgNbins);
211 
212  auto timeLeft = this->GetLeftTime( icounts );
213 
214  // do not redraw progress bar when neither time not ticks are different
215  if (ic == fPreviousProgress && timeLeft == fPreviousTimeEstimate && icounts != fNcounts-1) return;
216  // check if we are redirected to a file
217  if (fOutputToFile) {
218  if (ic != fPreviousProgress) {
219  std::clog << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%, time left: " << timeLeft << std::endl;
220  fPreviousProgress = ic;
221  }
222  return;
223  }
224  fPreviousProgress = ic;
225  fPreviousTimeEstimate = timeLeft;
226 
227  std::clog << fLogger->GetPrintedSource();
228  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
229  else std::clog << "[";
230  for (Int_t i=0; i<ic; i++) {
231  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << ">" << gTools().Color("reset");
232  else std::clog << ">";
233  }
234  for (Int_t i=ic+1; i<fgNbins; i++) {
235  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "." << gTools().Color("reset");
236  else std::clog << ".";
237  }
238  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
239  else std::clog << "]" ;
240 
241  // timing information
242  if (fColourfulOutput) {
243  std::clog << gTools().Color("reset") << " " ;
244  std::clog << "(" << gTools().Color("red") << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%" << gTools().Color("reset")
245  << ", "
246  << "time left: "
247  << timeLeft << gTools().Color("reset") << ") ";
248  }
249  else {
250  std::clog << "] " ;
251  std::clog << "(" << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%"
252  << ", " << "time left: " << timeLeft << ") ";
253  }
254  if (comment != "") {
255  std::clog << "[" << comment << "] ";
256  }
257  std::clog << "\r" << std::flush;
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// pretty string output
262 
263 TString TMVA::Timer::SecToText( Double_t seconds, Bool_t Scientific ) const
264 {
265  TString out = "";
266  if (Scientific ) out = Form( "%.3g sec", seconds );
267  else if (seconds < 0 ) out = "unknown";
268  else if (seconds <= 300) out = Form( "%i sec", Int_t(seconds) );
269  else {
270  if (seconds > 3600) {
271  Int_t h = Int_t(seconds/3600);
272  if (h <= 1) out = Form( "%i hr : ", h );
273  else out = Form( "%i hrs : ", h );
274 
275  seconds = Int_t(seconds)%3600;
276  }
277  Int_t m = Int_t(seconds/60);
278  if (m <= 1) out += Form( "%i min", m );
279  else out += Form( "%i mins", m );
280  }
281 
282  return (fColourfulOutput) ? gTools().Color("red") + out + gTools().Color("reset") : out;
283 }
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
void DrawProgressBar(void)
draws the progressbar
Definition: Timer.cxx:167
auto * m
Definition: textangle.C:8
float Float_t
Definition: RtypesCore.h:53
static const std::string comment("comment")
TString GetLeftTime(Int_t icounts)
returns pretty string with time left
Definition: Timer.cxx:155
Config & gConfig()
void Init(Int_t ncounts)
Definition: Timer.cxx:118
void Reset(void)
resets timer
Definition: Timer.cxx:128
Basic string class.
Definition: TString.h:131
int Int_t
Definition: RtypesCore.h:41
Bool_t fColourfulOutput
Definition: Timer.h:84
virtual ~Timer(void)
destructor
Definition: Timer.cxx:113
TString GetElapsedTime(Bool_t Scientific=kTRUE)
returns pretty string with elapsed time
Definition: Timer.cxx:147
static const Int_t fgNbins
Definition: Timer.h:94
Timer(const char *prefix="", Bool_t colourfulOutput=kTRUE)
constructor
Definition: Timer.cxx:86
Double_t ElapsedSeconds(void)
computes elapsed tim in seconds
Definition: Timer.cxx:138
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:405
Tools & gTools()
#define h(i)
Definition: RSha256.hxx:106
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:365
double Double_t
Definition: RtypesCore.h:55
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
const TString & Color(const TString &)
human readable color strings
Definition: Tools.cxx:840
Bool_t fOutputToFile
Definition: Timer.h:89
static const TString fgClassName
Definition: Timer.h:93
TString SecToText(Double_t, Bool_t) const
pretty string output
Definition: Timer.cxx:263
const Bool_t kTRUE
Definition: RtypesCore.h:87
Timing information for training and evaluation of MVA methods.
Definition: Timer.h:58
const char * Data() const
Definition: TString.h:364