ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/Utils/src/MeanCounter.cc
Revision: 1.1
Committed: Fri Oct 9 19:56:45 2009 UTC (15 years, 6 months ago) by kukartse
Content type: text/plain
Branch: MAIN
CVS Tags: V00-00-02, V00-00-01_BeamSplash09, gak110409, HEAD
Error occurred while calculating annotation data.
Log Message:
*** empty log message ***

File Contents

# Content
1 //
2 #include "LJMet/Utils/interface/MeanCounter.h"
3
4 #include <iostream>
5 #include <math.h>
6 using std::cout;
7 using std::endl;
8 using std::ostream;
9
10 //ClassImp(MeanCounter)
11
12 MeanCounter::MeanCounter(){
13 init();
14 }
15
16 MeanCounter::MeanCounter( const char *message, unsigned long int theFirst, unsigned long int theDivider ){
17 init();
18 _message = message;
19 if ( _message . length() == 0 ) printCount = false;
20 else printCount = true;
21 _count = theFirst;
22 _firstCount = theFirst;
23 _divider = theDivider;
24 }
25
26 MeanCounter::MeanCounter( unsigned long int theFirst, unsigned long int theDivider ){
27 init();
28 _count = theFirst;
29 _firstCount = theFirst;
30 _divider = theDivider;
31 printCount = true;
32 }
33
34 void MeanCounter::setCounter( unsigned long int theCount ){
35 _count = theCount;
36 }
37
38 void MeanCounter::setDivider( unsigned int theDivider ){
39 _divider = theDivider;
40 }
41
42 void MeanCounter::setPrintCount( bool _printCount ){
43 printCount = _printCount;
44 }
45
46 void MeanCounter::setNewLine( bool newLine ){
47 _newLine = newLine;
48 }
49
50 void MeanCounter::setMessage( const char *message ){
51 _message = message;
52 }
53
54 void MeanCounter::init( void ){
55 _count = 0;
56 _count_double_type = 0.0;
57 _firstCount = 0;
58 _divider = 1;
59 printCount = false;
60 firstCountEntry = true;
61 _message = "processing entry #";
62 _newLine = true;
63
64 initTime = time( NULL );
65 firstTickTime = 1;
66 lastTickTime = 1;
67 lastPrintTime = 1;
68
69 }
70
71 void MeanCounter::count( void ){
72
73 _count++;
74
75 double _number;
76 double _freq;
77 double _limit = 1./(double)_divider;
78
79 _number = (double)_count;
80 _freq = (double)_divider;
81
82 if (firstCountEntry){
83 //if ( printCount ) cout << "Counter is on:" << endl;
84 firstCountEntry = false;
85 firstTickTime = time( NULL );
86 }
87
88 if ( printCount ){
89 if ( fmod( _number, _freq ) < _limit ){
90 double averageTimeSinceLastPrint = 0.0;
91 double averageTimeSinceFirstTick = 0.0;
92 if ( lastPrintTime > 1 )
93 {
94 averageTimeSinceLastPrint = ( time( NULL ) - lastPrintTime ) / (double)_divider;
95 }
96 if ( _count > _firstCount )
97 {
98 averageTimeSinceFirstTick = ( time( NULL ) - firstTickTime ) / (double)( _count - _firstCount );
99 }
100 if ( !_newLine )
101 {
102 cout << char(13) << _message . c_str() << _count;
103 if ( _count > _firstCount ) cout << ", average time per count, sec: " << averageTimeSinceFirstTick;
104 fflush(stdout);
105 }
106 else
107 {
108 cout << _message . c_str() << _count;
109 if ( _count > _firstCount ) cout << ", average time per count, sec: " << averageTimeSinceFirstTick;
110 cout << endl;
111 }
112 lastPrintTime = time( NULL );
113 }
114 }
115
116 lastTickTime = time( NULL );
117 }
118
119 unsigned long int MeanCounter::getCount( void ){
120 return _count;
121 }
122
123 double MeanCounter::getCountDouble( void ){
124 return _count_double_type;
125 }
126
127 void MeanCounter::increment( long int _incr ){
128 _count += _incr;
129 }
130
131 void MeanCounter::incrementDouble( double _incr ){
132 _count_double_type += _incr;
133 }
134
135 MeanCounter::~MeanCounter(){
136 }
137
138 time_t MeanCounter::chime(std::string message){
139 time_t result = time(NULL) - lastTickTime;
140 if (message.size()>0){
141 cout << message << result << endl;
142 }
143 else{
144 cout << "Time passed since the last tick: " << result << endl;
145 }
146 return result;
147 }
148
149 void MeanCounter::set_times_mark(void){
150 static struct tms t_cpu;
151 time_mark = times(&t_cpu);
152 }
153
154 clock_t MeanCounter::get_times_diff(void){
155 static struct tms t_cpu;
156 clock_t result = times(&t_cpu) - time_mark;
157 return result;
158 }
159
160
161 clock_t MeanCounter::get_times(void){
162 static struct tms t_cpu;
163 clock_t result = times(&t_cpu);
164 return result;
165 }
166
167 void MeanCounter::start_clock(){
168 st_time = times(&st_cpu);
169 }
170
171 /* This example assumes that the result of each subtraction
172 is within the range of values that can be represented in
173 an integer type. */
174 void MeanCounter::end_clock(char *msg){
175 en_time = times(&en_cpu);
176
177 fputs(msg,stdout);
178 printf("Real Time: %jd, User Time %jd, System Time %jd\n",
179 (intmax_t)(en_time - st_time),
180 (intmax_t)(en_cpu.tms_utime - st_cpu.tms_utime),
181 (intmax_t)(en_cpu.tms_stime - st_cpu.tms_stime));
182 }