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