Logo ROOT  
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
30Timing information for training and evaluation of MVA methods
31
32Usage:
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
50Remark: 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
71const TString TMVA::Timer::fgClassName = "Timer";
73
75
76////////////////////////////////////////////////////////////////////////////////
77/// constructor
78
79TMVA::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
90TMVA::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{
100 Reset();
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// destructor
105
107{
108 delete fLogger;
109}
110
112{
113 // timer initialisation
114 fNcounts = ncounts;
115 Reset();
116}
117
118////////////////////////////////////////////////////////////////////////////////
119/// resets timer
120
122{
124 fPreviousProgress = -1;
125 fPreviousTimeEstimate.Clear();
126}
127
128////////////////////////////////////////////////////////////////////////////////
129/// computes elapsed tim in seconds
130
132{
134 return rt;
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// returns pretty string with elapsed time
139
141{
142 return SecToText( ElapsedSeconds(), Scientific );
143}
144
145////////////////////////////////////////////////////////////////////////////////
146/// returns pretty string with time left
147
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
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
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
196void 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
256TString 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}
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
float Float_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
char * Form(const char *fmt,...)
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
Timing information for training and evaluation of MVA methods.
Definition: Timer.h:58
Bool_t fColourfulOutput
Definition: Timer.h:84
Double_t ElapsedSeconds(void)
computes elapsed tim in seconds
Definition: Timer.cxx:131
Bool_t fOutputToFile
Definition: Timer.h:89
static const Int_t fgNbins
Definition: Timer.h:94
void DrawProgressBar(void)
draws the progressbar
Definition: Timer.cxx:160
TString SecToText(Double_t, Bool_t) const
pretty string output
Definition: Timer.cxx:256
TString GetLeftTime(Int_t icounts)
returns pretty string with time left
Definition: Timer.cxx:148
Timer(const char *prefix="", Bool_t colourfulOutput=kTRUE)
constructor
Definition: Timer.cxx:79
void Reset(void)
resets timer
Definition: Timer.cxx:121
virtual ~Timer(void)
destructor
Definition: Timer.cxx:106
static const TString fgClassName
Definition: Timer.h:93
TString GetElapsedTime(Bool_t Scientific=kTRUE)
returns pretty string with elapsed time
Definition: Timer.cxx:140
void Init(Int_t ncounts)
Definition: Timer.cxx:111
const TString & Color(const TString &)
human readable color strings
Definition: Tools.cxx:840
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
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
Config & gConfig()
Tools & gTools()
auto * m
Definition: textangle.C:8