CARMA C++
MonitorPointAccumulatorT.h
Go to the documentation of this file.
1 #ifndef CARMA_MONITOR_ACCUMULATORT_H
2 #define CARMA_MONITOR_ACCUMULATORT_H
3 
4 /*
5  * MonitorPointAccumulatorT.h - Template file for accumulating monitor point
6  * values of various types, all numeric.
7  */
8 
21 #include "carma/monitor/types.h"
25 #include "carma/dbms/dbFFIO.h"
27 
28 
29 namespace carma {
30 namespace monitor {
31 
32 
33 class MonitorPointAccumulatorBase {
34  protected:
35  static void valueToFile( const bool & avgAcc,
36  FILE * file );
37 
38  static void valueToFile( const ::std::complex< float > & avgAcc,
39  FILE * file );
40 
41  static void valueToFile( const double & avgAcc,
42  FILE * file );
43 
44  static void valueToFile( const float & avgAcc,
45  FILE * file );
46 
47  static void valueToFile( const int & avgAcc,
48  FILE * file );
49 
50  static void valueToFile( const long & avgAcc,
51  FILE * file );
52 
53  static void valueToFile( const short & avgAcc,
54  FILE * file );
55 
56  static void valueToFile( const ::std::string & avgAcc,
57  FILE * file );
58 
59  static void valueToFileThrice( const bool & avgAcc,
60  FILE * file );
61 
62  static void valueToFileThrice( const ::std::complex< float > & avgAcc,
63  FILE * file );
64 
65  static void valueToFileThrice( const double & avgAcc,
66  FILE * file );
67 
68  static void valueToFileThrice( const float & avgAcc,
69  FILE * file );
70 
71  static void valueToFileThrice( const int & avgAcc,
72  FILE * file );
73 
74  static void valueToFileThrice( const long & avgAcc,
75  FILE * file );
76 
77  static void valueToFileThrice( const short & avgAcc,
78  FILE * file );
79 
80  static void valueToFileThrice( const ::std::string & avgAcc,
81  FILE * file );
82 
83  static void cstringToFileThrice( const char * b,
84  FILE * file );
85 };
86 
87 
100 template < typename T, typename U >
101 class MonitorPointAccumulatorT : public MonitorPointAccumulatorBase {
102  public:
112  explicit MonitorPointAccumulatorT( T & typedPoint );
113 
114 
120 
124  void swap( MonitorPointAccumulatorT & rhs );
125 
130  void resetAccumulator( );
131 
137  void accumulate( );
138 
144  void accumulateAverage( );
145 
146 
154  const typename T::AccumReportType getAverage( int index = 0 ) const;
155 
156 
166  bool operator==( const MonitorPointAccumulatorT & rhs ) const;
167 
168 
178  bool operator<( const MonitorPointAccumulatorT & rhs ) const;
179 
180 
188  void dumpInstAveragesToFile( long frameCount,
189  FILE * file ) const;
190 
198  void dumpInstAveragesToFile( long frameCount,
199  carma::dbms::dbFFIO & file ) const;
200 
207  void dumpLongAveragesToFile( const char * const frameCountText,
208  FILE * const file ) const;
209 
210 
211  private:
212  typedef ::std::vector< MonitorPointAverageT< U > > AverageVec;
213 
214  T * typedPoint_;
215  AverageVec averageVec_;
216 }; // template MonitorPointAccumulatorT
217 
218 
219 } // end namespace monitor
220 } // end namespace carma
221 
222 
223 inline void
224 carma::monitor::MonitorPointAccumulatorBase::
225 cstringToFileThrice( const char * const b,
226  FILE * const file )
227 {
228  fputs( b, file );
229  fputc( '\t', file );
230  fputs( b, file );
231  fputc( '\t', file );
232  fputs( b, file );
233 }
234 
235 
236 inline void
237 carma::monitor::MonitorPointAccumulatorBase::
238 valueToFile( const bool & v,
239  FILE * const file )
240 {
241  fprintf( file, "%2d", v );
242 }
243 
244 
245 inline void
246 carma::monitor::MonitorPointAccumulatorBase::
247 valueToFileThrice( const bool & v,
248  FILE * const file )
249 {
250  char b[ 32 ];
251 
252  sprintf( b, "%2d", v );
253 
254  cstringToFileThrice( b, file );
255 }
256 
257 
258 inline void
259 carma::monitor::MonitorPointAccumulatorBase::
260 valueToFile( const ::std::complex< float > & v,
261  FILE * const file )
262 {
263  fprintf( file, "%15.8e\t%15.8e", v.real(), v.imag() );
264 }
265 
266 
267 inline void
268 carma::monitor::MonitorPointAccumulatorBase::
269 valueToFileThrice( const ::std::complex< float > & v,
270  FILE * const file )
271 {
272  char b[ 64 ];
273 
274  sprintf( b, "%15.8e\t%15.8e", v.real(), v.imag() );
275 
276  cstringToFileThrice( b, file );
277 }
278 
279 
280 inline void
281 carma::monitor::MonitorPointAccumulatorBase::
282 valueToFile( const double & v,
283  FILE * const file )
284 {
285  fprintf( file, "%23.16e", v );
286 }
287 
288 
289 inline void
290 carma::monitor::MonitorPointAccumulatorBase::
291 valueToFileThrice( const double & v,
292  FILE * const file )
293 {
294  char b[ 64 ];
295 
296  sprintf( b, "%23.16e", v );
297 
298  cstringToFileThrice( b, file );
299 }
300 
301 
302 inline void
303 carma::monitor::MonitorPointAccumulatorBase::
304 valueToFile( const float & v,
305  FILE * const file )
306 {
307  fprintf( file, "%15.8e", v );
308 }
309 
310 
311 inline void
312 carma::monitor::MonitorPointAccumulatorBase::
313 valueToFileThrice( const float & v,
314  FILE * const file )
315 {
316  char b[ 32 ];
317 
318  sprintf( b, "%15.8e", v );
319 
320  cstringToFileThrice( b, file );
321 }
322 
323 
324 inline void
325 carma::monitor::MonitorPointAccumulatorBase::
326 valueToFile( const int & v,
327  FILE * const file )
328 {
329  fprintf( file, "%12d", v );
330 }
331 
332 
333 inline void
334 carma::monitor::MonitorPointAccumulatorBase::
335 valueToFileThrice( const int & v,
336  FILE * const file )
337 {
338  char b[ 32 ];
339 
340  sprintf( b, "%12d", v );
341 
342  cstringToFileThrice( b, file );
343 }
344 
345 
346 inline void
347 carma::monitor::MonitorPointAccumulatorBase::
348 valueToFile( const long & v,
349  FILE * const file )
350 {
351  fprintf( file, "%12ld", v );
352 }
353 
354 
355 inline void
356 carma::monitor::MonitorPointAccumulatorBase::
357 valueToFileThrice( const long & v,
358  FILE * const file )
359 {
360  char b[ 32 ];
361 
362  sprintf( b, "%12ld", v );
363 
364  cstringToFileThrice( b, file );
365 }
366 
367 
368 inline void
369 carma::monitor::MonitorPointAccumulatorBase::
370 valueToFile( const short & v,
371  FILE * const file )
372 {
373  fprintf( file, "%7d", v );
374 }
375 
376 
377 inline void
378 carma::monitor::MonitorPointAccumulatorBase::
379 valueToFileThrice( const short & v,
380  FILE * const file )
381 {
382  char b[ 32 ];
383 
384  sprintf( b, "%7d", v );
385 
386  cstringToFileThrice( b, file );
387 }
388 
389 
390 inline void
391 carma::monitor::MonitorPointAccumulatorBase::
392 valueToFile( const ::std::string & v,
393  FILE * const file )
394 {
395  fputs( v.c_str(), file );
396 }
397 
398 
399 inline void
400 carma::monitor::MonitorPointAccumulatorBase::
401 valueToFileThrice( const ::std::string & v,
402  FILE * const file )
403 {
404  cstringToFileThrice( v.c_str(), file );
405 }
406 
407 
408 template < typename T, typename U >
409 inline
411  MonitorPointAccumulatorT( T & typedPoint ) :
412 typedPoint_( &typedPoint ),
413 averageVec_()
414 {
415  size_t numAccumulators;
416 
417  if ( typedPoint_->isTimeSeries() )
418  numAccumulators = 1;
419  else
420  numAccumulators = ::std::max( 1, typedPoint_->getNumSamples() );
421 
422  averageVec_.resize( numAccumulators );
423 
425 }
426 
427 
428 template < typename T, typename U >
429 inline
431 {
432 }
433 
434 
435 template < typename T, typename U >
436 inline void
439 {
440  ::std::swap( typedPoint_, rhs.typedPoint_ );
441  averageVec_.swap( rhs.averageVec_ );
442 }
443 
444 
445 template < typename T, typename U >
446 inline void
448 {
449  typename AverageVec::iterator i = averageVec_.begin();
450  const typename AverageVec::iterator iEnd = averageVec_.end();
451 
452  for ( ; i != iEnd; ++i )
453  typedPoint_->resetAccumulator( *i );
454 }
455 
456 
457 template < typename T, typename U >
458 inline void
460 {
461  typename AverageVec::iterator i = averageVec_.begin();
462  const typename AverageVec::iterator iEnd = averageVec_.end();
463 
464  if ( typedPoint_->isTimeSeries() ) {
465  for ( ; i != iEnd; ++i )
466  typedPoint_->accumulate( *i );
467  } else {
468  for ( int averageIndex = 0; i != iEnd; ++i, ++averageIndex )
469  typedPoint_->accumulateSample( *i, averageIndex );
470  }
471 }
472 
473 
474 template < typename T, typename U >
475 inline void
477 {
478  typename AverageVec::iterator i = averageVec_.begin();
479  const typename AverageVec::iterator iEnd = averageVec_.end();
480 
481  if ( typedPoint_->isTimeSeries() ) {
482  for ( ; i != iEnd; ++i )
483  typedPoint_->accumulateAverage( *i );
484  } else {
485  for ( int averageIndex = 0; i != iEnd; ++i, ++averageIndex )
486  typedPoint_->accumulateSample( *i, averageIndex );
487  }
488 }
489 
490 
491 template < typename T, typename U >
492 inline const typename T::AccumReportType
494  const int index ) const
495 {
496  return typedPoint_->getAccumulatedAverage( averageVec_[ index ] );
497 }
498 
499 
500 template < typename T, typename U >
501 inline bool
503  const MonitorPointAccumulatorT & rhs ) const
504 {
505  return (typedPoint_->getTagID() == rhs.getTagID());
506 }
507 
508 
509 template < typename T, typename U >
510 inline bool
512  const MonitorPointAccumulatorT & rhs ) const
513 {
514  return (typedPoint_->getTagID() < rhs.getTagID());
515 }
516 
517 
518 template < typename T, typename U >
519 inline void
521  const long frameCount,
522  FILE * const file ) const
523 {
524  const tagIDType tagID = typedPoint_->getTagID();
525  const bool isNotString =
526  (typedPoint_->getValuetype() != MONITOR_VALUE_TYPE_STRING);
527 
528  typename AverageVec::const_iterator i = averageVec_.begin();
529  const typename AverageVec::const_iterator iEnd = averageVec_.end();
530 
531  for ( int averageIndex = 0; i != iEnd; ++i, ++averageIndex ) {
532  fprintf( file, "%11ld\t%11ld\t", frameCount, tagID );
533 
534  i->writeAvePropsToFile( file );
535 
536  valueToFile( typedPoint_->getAccumulatedAverage( *i ), file );
537 
538  if ( isNotString ) {
539  // string values have no iSamples
540  fprintf( file, "\t%2d", averageIndex );
541  }
542 
543  fputc( '\n', file );
544  }
545 }
546 
547 
548 template < typename T, typename U >
549 inline void
551  const long frameCount,
552  carma::dbms::dbFFIO & file ) const
553 {
554  const long fc = frameCount;
555  const long tid = typedPoint_->getTagID();
556 
557  typename AverageVec::const_iterator i = averageVec_.begin();
558  const typename AverageVec::const_iterator iEnd = averageVec_.end();
559 
560  for ( int averageIndex = 0; i != iEnd; ++i, ++averageIndex ) {
561  const ushort blanking = i->getDbBlanking();
562  const ushort validity = i->getDbValidity();
563 
564  file.dumpInstAverage( fc,
565  tid,
566  blanking,
567  validity,
568  typedPoint_->getAccumulatedAverage( *i ),
569  averageIndex );
570  }
571 }
572 
573 
574 template < typename T, typename U >
575 inline void
577  const char * const frameCountText,
578  FILE * const file ) const
579 {
580  char numTotalSampsText[ 16 ];
581  char tagIdText[ 16 ];
582 
583  const bool isString =
584  (typedPoint_->getValuetype() == MONITOR_VALUE_TYPE_STRING);
585 
586  sprintf( tagIdText, "\t%11ld\t", typedPoint_->getTagID() );
587 
588  typename AverageVec::const_iterator i = averageVec_.begin();
589  const typename AverageVec::const_iterator iEnd = averageVec_.end();
590 
591  for ( int averageIndex = 0; i != iEnd; ++i, ++averageIndex ) {
592  fputs( frameCountText, file );
593  fputs( tagIdText, file );
594 
595  i->writeAvePropsToFile( file );
596 
597  if ( isString ) {
598  // string values have no iSamples
599  valueToFile( typedPoint_->getAccumulatedAverage( *i ),
600  file );
601  } else {
602  const typename T::AccumReportType avgVal =
603  typedPoint_->getAccumulatedAverage( *i );
604 
605  const typename T::AccumReportType maxVal =
606  typedPoint_->getMaxValue( *i );
607 
608  const typename T::AccumReportType minVal =
609  typedPoint_->getMinValue( *i );
610 
611  if ( (avgVal == maxVal) && (avgVal == minVal) )
612  valueToFileThrice( avgVal, file ); // Pretty common case
613  else {
614  valueToFile( avgVal, file );
615  fputc( '\t', file );
616  valueToFile( maxVal, file );
617  fputc( '\t', file );
618  valueToFile( minVal, file );
619  }
620 
621  if ( averageIndex == 0 )
622  fputs( "\t 0", file ); // Very common case
623  else
624  fprintf( file, "\t%2d", averageIndex );
625  }
626 
627  const int numTotalSamps = i->getNumTotalSamples();
628 
629  sprintf( numTotalSampsText, "\t%2d", numTotalSamps );
630 
631  const int numValidSamps = i->getNumValidSamples();
632 
633  if ( numValidSamps == numTotalSamps )
634  fputs( numTotalSampsText, file ); // Very common case
635  else if ( numValidSamps == 0 )
636  fputs( "\t 0", file ); // Pretty common case
637  else
638  fprintf( file, "\t%2d", numValidSamps );
639 
640  fputs( numTotalSampsText, file );
641  fputc( '\n', file );
642  }
643 }
644 
645 
646 #endif
void accumulate()
Method to accumulate sample values in associated average objects - walks through samples and accumula...
Classes that provide the pecializations of monitor points and sense poiints for different datatypes...
Abstract base class for all monitor points.
~MonitorPointAccumulatorT()
Destructor Destroys the created set of average objects.
bool operator==(const MonitorPointAccumulatorT &rhs) const
Method to test for equality of two MonitorPointAccumulator objects.
const T::AccumReportType getAverage(int index=0) const
Method to compute average value from accumulated sample values in the associated average objects...
void swap(MonitorPointAccumulatorT &rhs)
swap two instances
void dumpLongAveragesToFile(const char *const frameCountText, FILE *const file) const
dump long (minute,subarray) averages to a file for the dbloader to read
void accumulateAverage()
Method to accumulate sample averages in associated average objects - uses pre-computed average values...
Class(es) to read &amp; write dbms flat file information in ASCII or binary.
void dumpInstAveragesToFile(long frameCount, FILE *file) const
dump instantaneous (frame) averages to a file for the dbloader to read
void resetAccumulator()
Method to reset associated average objects so they&#39;re initialized for a fresh average calculation...
type definitions for monitor system
bool operator<(const MonitorPointAccumulatorT &rhs) const
Method to test ordering of two MonitorPointAccumulator objects.
MonitorPointAccumulatorT&lt; T, U &gt;
This is the interface file for extra APIs for program logging.
MonitorPointAccumulatorT(T &typedPoint)
Constructor Creates an array of average objects for a monitor point with spectral data...