ProgressBar.cxx
1 #include "ProgressBar.h"
2 
3 #define ANSI_COLOR_RED "\x1b[31m"
4 #define ANSI_COLOR_GREEN "\x1b[32m"
5 #define ANSI_COLOR_YELLOW "\x1b[33m"
6 #define ANSI_COLOR_BLUE "\x1b[34m"
7 #define ANSI_COLOR_MAGENTA "\x1b[35m"
8 #define ANSI_COLOR_CYAN "\x1b[36m"
9 #define ANSI_COLOR_RESET "\x1b[0m"
10 
11 
12 
13 Int_t Acclaim::ProgressBar::progState = 0;
14 
15 //---------------------------------------------------------------------------------------------------------
20  std::cerr << "Assuming 100 events in ProgressBar" << std::endl;
21  fMaxEntry = 100;
22  fCounter = 0;
23  fPercentage = 0;
24  fWatch.Start(kTRUE);
25  fSetHandler = 0;
26  fNumBreakTries = 0;
27  fLastProgState = 0;
28 }
29 
30 
31 
32 
33 
34 
35 //---------------------------------------------------------------------------------------------------------
40 Acclaim::ProgressBar::ProgressBar(Long64_t maxEntryInit){
41  fMaxEntry = maxEntryInit > 1 ? maxEntryInit - 1 : 1;
42  fCounter = 0;
43  fPercentage = 0;
44  fWatch.Start(kTRUE);
45  fSetHandler = 0;
46  fNumBreakTries = 0;
47  fLastProgState = 0;
48 }
49 
50 
51 
52 
53 
54 
55 //---------------------------------------------------------------------------------------------------------
60 
61  if(fPercentage>=100) return;
62 
63  // Stops the fWatch
64  Int_t seconds = Int_t(fWatch.RealTime());
65  Int_t hours = seconds / 3600;
66  hours = hours < 0 ? 0 : hours;
67  seconds = seconds - hours * 3600;
68  Int_t mins = seconds / 60;
69  mins = mins < 0 ? 0 : mins;
70 
71  seconds = seconds - mins * 60;
72 
73  fCounter++;
74  double ratio = double(fCounter)/fMaxEntry;
75  // std::cout << ratio << "\t" << fCounter << "\t" << maxEntry << std::endl;
76 
77  if(ratio*100 > fPercentage){
78 
79  while(ratio*100 > fPercentage){
80  fPercentage++;
81  }
82 
83  // fprintf(stderr, "\n\033[F\033[J");
84  // std::cout << std::endl;
85  // fprintf(stderr, "\033[F\033[J");
86  fprintf(stderr, "\r");
87 
88  // Show the fPercentage complete.
89  fprintf(stderr, ANSI_COLOR_RED);
90  fprintf(stderr, "%3u%%", (UInt_t)(fPercentage) );
91  fprintf(stderr, ANSI_COLOR_RESET);
92  fprintf(stderr, " [");
93 
94  // Show the load bar.
95  fprintf(stderr, ANSI_COLOR_BLUE);
96  fprintf(stderr, "%02d:%02d:%02d", hours, mins, seconds);
97  for (UInt_t i=8; i<fPercentage; i++){
98  fprintf(stderr, "=");
99  }
100  fprintf(stderr, ANSI_COLOR_RESET);
101 
102  Int_t startSpace = fPercentage > 8 ? fPercentage : 8;
103  for (Int_t i=startSpace; i<100; i++){
104  fprintf(stderr, " ");
105  }
106 
107  fprintf(stderr, "]");
108  }
109 
110  if(fPercentage>=100) fprintf(stderr, "\n");
111  fWatch.Start(kFALSE);
112  return;
113 
114 }
115 
116 
117 
118 //---------------------------------------------------------------------------------------------------------
127 void Acclaim::ProgressBar::inc(UInt_t& entry, Long64_t numEntries){
128  Long64_t entryL64 = entry;
129  inc(entryL64, numEntries);
130  entry = entryL64;
131 }
132 
133 
134 //---------------------------------------------------------------------------------------------------------
143 void Acclaim::ProgressBar::inc(Int_t& entry, Long64_t numEntries){
144  Long64_t entryL64 = entry;
145  inc(entryL64, numEntries);
146  entry = entryL64;
147 }
148 
149 
150 
151 //---------------------------------------------------------------------------------------------------------
160 void Acclaim::ProgressBar::inc(Long64_t& entry, Long64_t numEntries){
161  numEntries = numEntries < 0 ? fMaxEntry : numEntries;
162 
163  if(fSetHandler==0){
165  fSetHandler = 1;
166  }
167 
168  if(fSetHandler==1 && fLastProgState!=0){
169  if(fNumBreakTries==0)
170  {
171  std::cerr << "Program with ProgressBar received SIGINT, will try and exit main loop gracefully. " << std::endl;
172  entry=numEntries;
173  }
174  else
175  {
176  std::cerr << "Unable to exit main loop gracefully, raising SIGINT properly." << std::endl;
177  signal(SIGINT, SIG_DFL); // now sigint points to the default handler again
178  raise(SIGINT); // raise sigint
179  }
180  fNumBreakTries++;
181  }
182 
183  int diff = entry - fCounter;
184  for(int i=0; i < diff; i++){
185  // std::cout << fCounter << "\t" << entry << std::endl;
186  (*this)++;
187  }
188  fLastProgState = progState;
189 }
190 
191 
192 
193 
194 
195 //---------------------------------------------------------------------------------------------------------
200  std::cout << fPercentage << "\t" << fCounter << "\t" << fMaxEntry << std::endl;
201 }
202 
203 
204 
205 
206 
207 
208 //---------------------------------------------------------------------------------------------------------
215  progState = param;
216 }
ProgressBar()
Default constructor - don&#39;t use this.
Definition: ProgressBar.cxx:19
void status()
For debugging, prints state of internal variables.
static void mainLoopSigintHandle(int param)
Custom handler, sets variable when signal is received.
void inc(Long64_t &entry, Long64_t numEntries=-1)
New primary function to move through main for loop in analysis program.
void operator++(int)
Increment operator, use when you have completed one iteration of the main loop.
Definition: ProgressBar.cxx:59