ROOT  6.06/09
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 <iomanip>
53 
54 #include "TMVA/Timer.h"
55 
56 #ifndef ROOT_TMVA_Config
57 #include "TMVA/Config.h"
58 #endif
59 #ifndef ROOT_TMVA_Tools
60 #include "TMVA/Tools.h"
61 #endif
62 #ifndef ROOT_TMVA_MsgLogger
63 #include "TMVA/MsgLogger.h"
64 #endif
65 
66 const TString TMVA::Timer::fgClassName = "Timer";
67 const Int_t TMVA::Timer::fgNbins = 24;
68 
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// constructor
73 
74 TMVA::Timer::Timer( const char* prefix, Bool_t colourfulOutput )
75  : fNcounts ( 0 ),
76  fPrefix ( strcmp(prefix,"")==0?Timer::fgClassName:TString(prefix) ),
77  fColourfulOutput( colourfulOutput ),
78  fLogger ( new MsgLogger( fPrefix.Data() ) )
79 {
80  Reset();
81 }
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// standard constructor: ncounts gives the total number of counts that
85 /// the loop will iterate through. At each call of the timer, the current
86 /// number of counts is provided by the user, so that the timer can obtain
87 /// the due time from linearly interpolating the spent time.
88 
89 TMVA::Timer::Timer( Int_t ncounts, const char* prefix, Bool_t colourfulOutput )
90  : fNcounts ( ncounts ),
91  fPrefix ( strcmp(prefix,"")==0?Timer::fgClassName:TString(prefix) ),
92  fColourfulOutput( colourfulOutput ),
93  fLogger ( new MsgLogger( fPrefix.Data() ) )
94 {
95  Reset();
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// destructor
100 
102 {
103  delete fLogger;
104 }
105 
106 void TMVA::Timer::Init( Int_t ncounts )
107 {
108  // timer initialisation
109  fNcounts = ncounts;
110  Reset();
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// resets timer
115 
116 void TMVA::Timer::Reset( void )
117 {
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// computes elapsed tim in seconds
123 
125 {
127  return rt;
128 }
129 //_______________________________________________________________________
130 
132 {
133  // returns pretty string with elaplsed time
134  return SecToText( ElapsedSeconds(), Scientific );
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// returns pretty string with time left
139 
141 {
142  Double_t leftTime = ( icounts <= 0 ? -1 :
143  icounts > fNcounts ? -1 :
144  Double_t(fNcounts - icounts)/Double_t(icounts)*ElapsedSeconds() );
145 
146  return SecToText( leftTime, kFALSE );
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// draws the progressbar
151 
153 {
154  fNcounts++;
155  if (fNcounts == 1) {
156  std::clog << fLogger->GetPrintedSource();
157  std::clog << "Please wait ";
158  }
159 
160  std::clog << "." << std::flush;
161 }
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// draws a string in the progress bar
165 
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  std::clog << "\r" << std::flush;
177 }
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// draws progress bar in color or B&W
181 /// caution:
182 
184 {
185  if (!gConfig().DrawProgressBar()) return;
186 
187  // sanity check:
188  if (icounts > fNcounts-1) icounts = fNcounts-1;
189  if (icounts < 0 ) icounts = 0;
190  Int_t ic = Int_t(Float_t(icounts)/Float_t(fNcounts)*fgNbins);
191 
192  std::clog << fLogger->GetPrintedSource();
193  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "[" << gTools().Color("reset");
194  else std::clog << "[";
195  for (Int_t i=0; i<ic; i++) {
196  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << ">" << gTools().Color("reset");
197  else std::clog << ">";
198  }
199  for (Int_t i=ic+1; i<fgNbins; i++) {
200  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "." << gTools().Color("reset");
201  else std::clog << ".";
202  }
203  if (fColourfulOutput) std::clog << gTools().Color("white_on_green") << gTools().Color("dyellow") << "]" << gTools().Color("reset");
204  else std::clog << "]" ;
205 
206  // timing information
207  if (fColourfulOutput) {
208  std::clog << gTools().Color("reset") << " " ;
209  std::clog << "(" << gTools().Color("red") << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%" << gTools().Color("reset")
210  << ", "
211  << "time left: "
212  << this->GetLeftTime( icounts ) << gTools().Color("reset") << ") ";
213  }
214  else {
215  std::clog << "] " ;
216  std::clog << "(" << Int_t((100*(icounts+1))/Float_t(fNcounts)) << "%"
217  << ", " << "time left: " << this->GetLeftTime( icounts ) << ") ";
218  }
219  if (comment != "") {
220  std::clog << "[" << comment << "] ";
221  }
222  std::clog << "\r" << std::flush;
223 }
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// pretty string output
227 
228 TString TMVA::Timer::SecToText( Double_t seconds, Bool_t Scientific ) const
229 {
230  TString out = "";
231  if (Scientific ) out = Form( "%.3g sec", seconds );
232  else if (seconds < 0 ) out = "unknown";
233  else if (seconds <= 300) out = Form( "%i sec", Int_t(seconds) );
234  else {
235  if (seconds > 3600) {
236  Int_t h = Int_t(seconds/3600);
237  if (h <= 1) out = Form( "%i hr : ", h );
238  else out = Form( "%i hrs : ", h );
239 
240  seconds = Int_t(seconds)%3600;
241  }
242  Int_t m = Int_t(seconds/60);
243  if (m <= 1) out += Form( "%i min", m );
244  else out += Form( "%i mins", m );
245  }
246 
247  return (fColourfulOutput) ? gTools().Color("red") + out + gTools().Color("reset") : out;
248 }
249 
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:108
void DrawProgressBar(void)
draws the progressbar
Definition: Timer.cxx:152
float Float_t
Definition: RtypesCore.h:53
static const std::string comment("comment")
TString GetLeftTime(Int_t icounts)
returns pretty string with time left
Definition: Timer.cxx:140
Config & gConfig()
TH1 * h
Definition: legend2.C:5
void Init(Int_t ncounts)
Definition: Timer.cxx:106
void Reset(void)
resets timer
Definition: Timer.cxx:116
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
ClassImp(TMVA::Timer) TMVA
constructor
Definition: Timer.cxx:69
virtual ~Timer(void)
destructor
Definition: Timer.cxx:101
Tools & gTools()
Definition: Tools.cxx:79
std::vector< std::vector< double > > Data
char * out
Definition: TBase64.cxx:29
TString GetElapsedTime(Bool_t Scientific=kTRUE)
Definition: Timer.cxx:131
static const Int_t fgNbins
Definition: Timer.h:91
Timer(const char *prefix="", Bool_t colourfulOutput=kTRUE)
RooCmdArg Timer(Bool_t flag=kTRUE)
TString SecToText(Double_t, Bool_t) const
pretty string output
Definition: Timer.cxx:228
TMarker * m
Definition: textangle.C:8
Double_t ElapsedSeconds(void)
computes elapsed tim in seconds
Definition: Timer.cxx:124
char * Form(const char *fmt,...)
void Reset(Detail::TBranchProxy *x)
double Double_t
Definition: RtypesCore.h:55
const TString & Color(const TString &)
human readable color strings
Definition: Tools.cxx:837
Abstract ClassifierFactory template that handles arbitrary types.
const Bool_t kTRUE
Definition: Rtypes.h:91