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