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 * *
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 * (see tmva/doc/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
80
81////////////////////////////////////////////////////////////////////////////////
82/// constructor
83
85 : Timer(0, prefix, colourfulOutput)
86{
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// standard constructor: ncounts gives the total number of counts that
91/// the loop will iterate through. At each call of the timer, the current
92/// number of counts is provided by the user, so that the timer can obtain
93/// the due time from linearly interpolating the spent time.
94
96 : fNcounts ( ncounts ),
97 fPrefix ( strcmp(prefix,"")==0?Timer::fgClassName:TString(prefix) ),
98 fColourfulOutput( colourfulOutput ),
99 fPreviousProgress(-1),
100 fOutputToFile(!isatty(STDERR_FILENO)),
101 fProgressBarStringLength (0),
102 fLogger ( new MsgLogger( fPrefix.Data() ) )
103{
105 Reset();
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// destructor
110
112{
113 delete fLogger;
114}
115
117{
118 // timer initialisation
119 fNcounts = ncounts;
120 Reset();
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// resets timer
125
127{
129 fPreviousProgress = -1;
130 fPreviousTimeEstimate.Clear();
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// computes elapsed tim in seconds
135
141
142////////////////////////////////////////////////////////////////////////////////
143/// returns pretty string with elapsed time
144
146{
147 return SecToText( ElapsedSeconds(), Scientific );
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// returns pretty string with time left
152
154{
155 Double_t leftTime = ( icounts <= 0 ? -1 :
156 icounts > fNcounts ? -1 :
157 Double_t(fNcounts - icounts)/Double_t(icounts)*ElapsedSeconds() );
158
159 return SecToText( leftTime, kFALSE );
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// draws the progressbar
164
166{
167 fProgressBarStringLength = 0;
168 fNcounts++;
169 if (fNcounts == 1) {
170 std::clog << fLogger->GetPrintedSource();
171 std::clog << "Please wait ";
172 }
173
174 std::clog << "." << std::flush;
175}
176
177////////////////////////////////////////////////////////////////////////////////
178/// draws a string in the progress bar
179
181{
182
183 std::clog << fLogger->GetPrintedSource();
184
185 std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
186
187 std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << theString << gTools().Color("reset");
188
189 std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
190
191 for (int i = fProgressBarStringLength; i < theString.Length (); ++i)
192 std::cout << " ";
193 std::clog << "\r" << std::flush;
194 fProgressBarStringLength = theString.Length ();
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// draws progress bar in color or B&W
199/// caution:
200
202{
203 if (!gConfig().DrawProgressBar()) return;
204
205 // sanity check:
206 if (icounts > fNcounts-1) icounts = fNcounts-1;
207 if (icounts < 0 ) icounts = 0;
208 Int_t ic = Int_t(Float_t(icounts)/Float_t(fNcounts)*fgNbins);
209
210 auto timeLeft = this->GetLeftTime( icounts );
211
212 // do not redraw progress bar when neither time not ticks are different
213 if (ic == fPreviousProgress && timeLeft == fPreviousTimeEstimate && icounts != fNcounts-1) return;
214 // check if we are redirected to a file
215 if (fOutputToFile) {
216 if (ic != fPreviousProgress) {
217 std::clog << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%, time left: " << timeLeft << std::endl;
218 fPreviousProgress = ic;
219 }
220 return;
221 }
222 fPreviousProgress = ic;
223 fPreviousTimeEstimate = timeLeft;
224
225 std::clog << fLogger->GetPrintedSource();
226 if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
227 else std::clog << "[";
228 for (Int_t i=0; i<ic; i++) {
229 if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << ">" << gTools().Color("reset");
230 else std::clog << ">";
231 }
232 for (Int_t i=ic+1; i<fgNbins; i++) {
233 if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "." << gTools().Color("reset");
234 else std::clog << ".";
235 }
236 if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
237 else std::clog << "]" ;
238
239 // timing information
240 if (fColourfulOutput) {
241 std::clog << gTools().Color("reset") << " " ;
242 std::clog << "(" << gTools().Color("red") << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%" << gTools().Color("reset")
243 << ", "
244 << "time left: "
245 << timeLeft << gTools().Color("reset") << ") ";
246 }
247 else {
248 std::clog << "] " ;
249 std::clog << "(" << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%"
250 << ", " << "time left: " << timeLeft << ") ";
251 }
252 if (comment != "") {
253 std::clog << "[" << comment << "] ";
254 }
255 std::clog << "\r" << std::flush;
256}
257
258////////////////////////////////////////////////////////////////////////////////
259/// pretty string output
260
262{
263 TString out = "";
264 if (Scientific ) out = TString::Format( "%.3g sec", seconds );
265 else if (seconds < 0 ) out = "unknown";
266 else if (seconds <= 300) out = TString::Format( "%i sec", Int_t(seconds) );
267 else {
268 if (seconds > 3600) {
269 Int_t h = Int_t(seconds/3600);
270 if (h <= 1) out = TString::Format( "%i hr : ", h );
271 else out = TString::Format( "%i hrs : ", h );
272
273 seconds = Int_t(seconds)%3600;
274 }
275 Int_t m = Int_t(seconds/60);
276 if (m <= 1) out += TString::Format( "%i min", m );
277 else out += TString::Format( "%i mins", m );
278 }
279
280 return (fColourfulOutput) ? gTools().Color("red") + out + gTools().Color("reset") : out;
281}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Reset(Option_t *option="") override
ostringstream derivative to redirect and format output
Definition MsgLogger.h:57
Timing information for training and evaluation of MVA methods.
Definition Timer.h:58
Bool_t fColourfulOutput
flag for use of colors
Definition Timer.h:84
Double_t ElapsedSeconds(void)
computes elapsed tim in seconds
Definition Timer.cxx:136
Bool_t fOutputToFile
Definition Timer.h:89
static const Int_t fgNbins
number of bins in progress bar
Definition Timer.h:94
void DrawProgressBar(void)
draws the progressbar
Definition Timer.cxx:165
TString SecToText(Double_t, Bool_t) const
pretty string output
Definition Timer.cxx:261
TString GetLeftTime(Int_t icounts)
returns pretty string with time left
Definition Timer.cxx:153
Timer(const char *prefix="", Bool_t colourfulOutput=kTRUE)
constructor
Definition Timer.cxx:84
void Reset(void)
resets timer
Definition Timer.cxx:126
virtual ~Timer(void)
destructor
Definition Timer.cxx:111
static const TString fgClassName
used for output
Definition Timer.h:93
TString GetElapsedTime(Bool_t Scientific=kTRUE)
returns pretty string with elapsed time
Definition Timer.cxx:145
void Init(Int_t ncounts)
Definition Timer.cxx:116
const TString & Color(const TString &)
human readable color strings
Definition Tools.cxx:828
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:138
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384
Config & gConfig()
Tools & gTools()
TMarker m
Definition textangle.C:8