Logo ROOT   6.08/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 //_______________________________________________________________________
29 //
30 // Timing information for training and evaluation of MVA methods
31 //
32 // Usage:
33 //
34 // TMVA::Timer timer( Nloops, "MyClassName" );
35 // for (Int_t i=0; i<Nloops; i++) {
36 // ... // some code
37 //
38 // // now, print progress bar:
39 // timer.DrawProgressBar( i );
40 //
41 // // **OR** text output of left time (never both !)
42 // fLogger << " time left: " << timer.GetLeftTime( i ) << Endl;
43 //
44 // }
45 // fLogger << "MyClassName" << ": elapsed time: " << timer.GetElapsedTime()
46 // << Endl;
47 //
48 // Remark: in batch mode, the progress bar is quite ugly; you may
49 // want to use the text output then
50 //_______________________________________________________________________
51 
52 #include "TMVA/Timer.h"
53 
54 #include "TMVA/Config.h"
55 #include "TMVA/MsgLogger.h"
56 #include "TMVA/Tools.h"
57 
58 #include "TStopwatch.h"
59 
60 #include <iomanip>
61 
62 const TString TMVA::Timer::fgClassName = "Timer";
63 const Int_t TMVA::Timer::fgNbins = 16;
64 
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// constructor
69 
70 TMVA::Timer::Timer( const char* prefix, Bool_t colourfulOutput )
71 : fNcounts ( 0 ),
72  fPrefix ( strcmp(prefix,"")==0?Timer::fgClassName:TString(prefix) ),
73  fColourfulOutput( colourfulOutput ),
74  fProgressBarStringLength (0),
75  fLogger ( new MsgLogger( fPrefix.Data() ) )
76 {
77  Reset();
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// standard constructor: ncounts gives the total number of counts that
82 /// the loop will iterate through. At each call of the timer, the current
83 /// number of counts is provided by the user, so that the timer can obtain
84 /// the due time from linearly interpolating the spent time.
85 
86 TMVA::Timer::Timer( Int_t ncounts, const char* prefix, Bool_t colourfulOutput )
87  : fNcounts ( ncounts ),
88  fPrefix ( strcmp(prefix,"")==0?Timer::fgClassName:TString(prefix) ),
89  fColourfulOutput( colourfulOutput ),
90  fProgressBarStringLength (0),
91  fLogger ( new MsgLogger( fPrefix.Data() ) )
92 {
93  Reset();
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// destructor
98 
100 {
101  delete fLogger;
102 }
103 
104 void TMVA::Timer::Init( Int_t ncounts )
105 {
106  // timer initialisation
107  fNcounts = ncounts;
108  Reset();
109 }
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// resets timer
113 
114 void TMVA::Timer::Reset( void )
115 {
117 }
118 
119 ////////////////////////////////////////////////////////////////////////////////
120 /// computes elapsed tim in seconds
121 
123 {
125  return rt;
126 }
127 //_______________________________________________________________________
128 
130 {
131  // returns pretty string with elaplsed time
132  return SecToText( ElapsedSeconds(), Scientific );
133 }
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// returns pretty string with time left
137 
139 {
140  Double_t leftTime = ( icounts <= 0 ? -1 :
141  icounts > fNcounts ? -1 :
142  Double_t(fNcounts - icounts)/Double_t(icounts)*ElapsedSeconds() );
143 
144  return SecToText( leftTime, kFALSE );
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// draws the progressbar
149 
151 {
153  fNcounts++;
154  if (fNcounts == 1) {
155  std::clog << fLogger->GetPrintedSource();
156  std::clog << "Please wait ";
157  }
158 
159  std::clog << "." << std::flush;
160 }
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// draws a string in the progress bar
164 
166 {
167 
168  std::clog << fLogger->GetPrintedSource();
169 
170  std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
171 
172  std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << theString << gTools().Color("reset");
173 
174  std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
175 
176  for (int i = fProgressBarStringLength; i < theString.Length (); ++i)
177  std::cout << " ";
178  std::clog << "\r" << std::flush;
179  fProgressBarStringLength = theString.Length ();
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// draws progress bar in color or B&W
184 /// caution:
185 
187 {
188  if (!gConfig().DrawProgressBar()) return;
189 
190  // sanity check:
191  if (icounts > fNcounts-1) icounts = fNcounts-1;
192  if (icounts < 0 ) icounts = 0;
193  Int_t ic = Int_t(Float_t(icounts)/Float_t(fNcounts)*fgNbins);
194 
195  std::clog << fLogger->GetPrintedSource();
196  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
197  else std::clog << "[";
198  for (Int_t i=0; i<ic; i++) {
199  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << ">" << gTools().Color("reset");
200  else std::clog << ">";
201  }
202  for (Int_t i=ic+1; i<fgNbins; i++) {
203  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "." << gTools().Color("reset");
204  else std::clog << ".";
205  }
206  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
207  else std::clog << "]" ;
208 
209  // timing information
210  if (fColourfulOutput) {
211  std::clog << gTools().Color("reset") << " " ;
212  std::clog << "(" << gTools().Color("red") << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%" << gTools().Color("reset")
213  << ", "
214  << "time left: "
215  << this->GetLeftTime( icounts ) << gTools().Color("reset") << ") ";
216  }
217  else {
218  std::clog << "] " ;
219  std::clog << "(" << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%"
220  << ", " << "time left: " << this->GetLeftTime( icounts ) << ") ";
221  }
222  if (comment != "") {
223  std::clog << "[" << comment << "] ";
224  }
225  std::clog << "\r" << std::flush;
226 }
227 
228 ////////////////////////////////////////////////////////////////////////////////
229 /// pretty string output
230 
231 TString TMVA::Timer::SecToText( Double_t seconds, Bool_t Scientific ) const
232 {
233  TString out = "";
234  if (Scientific ) out = Form( "%.3g sec", seconds );
235  else if (seconds < 0 ) out = "unknown";
236  else if (seconds <= 300) out = Form( "%i sec", Int_t(seconds) );
237  else {
238  if (seconds > 3600) {
239  Int_t h = Int_t(seconds/3600);
240  if (h <= 1) out = Form( "%i hr : ", h );
241  else out = Form( "%i hrs : ", h );
242 
243  seconds = Int_t(seconds)%3600;
244  }
245  Int_t m = Int_t(seconds/60);
246  if (m <= 1) out += Form( "%i min", m );
247  else out += Form( "%i mins", m );
248  }
249 
250  return (fColourfulOutput) ? gTools().Color("red") + out + gTools().Color("reset") : out;
251 }
252 
Config & gConfig()
Definition: Config.cxx:43
static const TString fgClassName
Definition: Timer.h:92
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 Init(Int_t ncounts)
Definition: Timer.cxx:104
TString SecToText(Double_t, Bool_t) const
pretty string output
Definition: Timer.cxx:231
float Float_t
Definition: RtypesCore.h:53
static const std::string comment("comment")
TH1 * h
Definition: legend2.C:5
Basic string class.
Definition: TString.h:137
virtual ~Timer(void)
destructor
Definition: Timer.cxx:99
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Bool_t fColourfulOutput
Definition: Timer.h:88
TString GetElapsedTime(Bool_t Scientific=kTRUE)
Definition: Timer.cxx:129
Int_t fProgressBarStringLength
Definition: Timer.h:90
MsgLogger * fLogger
Definition: Timer.h:95
Tools & gTools()
Definition: Tools.cxx:79
std::vector< std::vector< double > > Data
void Reset(void)
resets timer
Definition: Timer.cxx:114
std::string GetPrintedSource() const
the full logger prefix
Definition: MsgLogger.cxx:167
TMarker * m
Definition: textangle.C:8
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:390
void Reset(Detail::TBranchProxy *x)
TString GetLeftTime(Int_t icounts)
returns pretty string with time left
Definition: Timer.cxx:138
Timer(const char *prefix="", Bool_t colourfulOutput=kTRUE)
constructor
Definition: Timer.cxx:70
Double_t ElapsedSeconds(void)
computes elapsed tim in seconds
Definition: Timer.cxx:122
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
static const Int_t fgNbins
Definition: Timer.h:93
Int_t fNcounts
Definition: Timer.h:86
const TString & Color(const TString &)
human readable color strings
Definition: Tools.cxx:837
Abstract ClassifierFactory template that handles arbitrary types.
void DrawProgressBar(void)
draws the progressbar
Definition: Timer.cxx:150
const Bool_t kTRUE
Definition: Rtypes.h:91