GSE++ library: reading and writing GSE waveforms
gsexx_TCHK2.cc
Go to the documentation of this file.
1 
36 #define TF_GSEXX_TCHK2_CC_VERSION \
37  "TF_GSEXX_TCHK2_CC V1.2"
38 #define TF_GSEXX_TCHK2_CC_CVSID \
39  "$Id$"
40 
41 #include<cmath>
42 #include <gsexx.h>
43 #include <sstream>
44 #include <cstdio>
45 
46 namespace GSE2 {
47 namespace waveform {
48 
50 namespace helper {
51 
53  intT abs(const intT& val)
54  {
55  return(val > 0 ? val : -val);
56  } // int abs(const int* val)
57 
58 } // namespace helper
59 
67 const char* const TCHK2::GSEID="CHK2";
69 
70 /*----------------------------------------------------------------------*/
71 
77 void TCHK2::add(const intT& value)
78 {
79  const intT modulo=100000000; // a one and eight zeros
80  intT sample_value=value;
81 
82  // check for sample value overflow
83  if (helper::abs(sample_value) >= modulo)
84  {
85  sample_value = sample_value - int(sample_value/modulo)*modulo;
86  }
87 
88  // add the sample value to the checksum
89  Msum += sample_value;
90 
91  // check for checksum overflow
92  if (helper::abs(Msum) >= modulo)
93  {
94  Msum = Msum - int(Msum/modulo)*modulo;
95  }
96 }
97 
98 /*----------------------------------------------------------------------*/
99 
103 std::string TCHK2::write() const
104 {
105  std::string retval("CHK2 ");
106  char cvalue[10];
107  std::sprintf(cvalue, "%8i\n", this->value());
108  retval.append(cvalue);
109  return(retval);
110 }
111 
112 /*----------------------------------------------------------------------*/
113 
117 void TCHK2::read(std::istream& fis)
118 {
119  // read lineID first to pass newline after last data character
120  std::string lineID;
121  fis >> lineID;
122  // DEBUG: std::cerr << lineID << std::endl;
123  // now read a full line to chomp newline after checksum too
124  std::string theline;
125  // DEBUG: std::cerr << theline << std::endl;
126  std::getline(fis, theline);
127  std::istringstream is(theline);
128  if (!GSEIDmatch<TCHK2>(lineID)) throw
129  Terror("ERROR (CHK2::read): missing CHK2 line!");
130  is >> this->Msum;
131 /* former code (01/07/2005)
132  std::string lineID;
133  is >> lineID;
134  if (!GSEIDmatch<TCHK2>(lineID)) throw
135  Terror("ERROR (TCHK2::read): missing CHK2 line!");
136  is >> this->Msum;
137 */
138 }
139 
140 } // namespace waveform
141 } // namespace GSE2
142 
143 /* ----- END OF gsexx_TCHK2.cc ----- */
GSE++ library: read and write GSE waveform data (prototypes).
static const char *const GSEID
GSE line idetifier.
Definition: gsexx.h:187
int intT
All GSE2 waveform data is based on 4 byte integers.
Definition: gsexx.h:89
std::string write() const
write CHK2 line to string
void read(std::istream &is)
read CHK2 line from istream
All stuff defined by the GSE2 standard.
intT abs(const intT &val)
abs for intT
Definition: gsexx_TCHK2.cc:53
void add(const intT &value)
Add a value to the checksum.
Definition: gsexx_TCHK2.cc:77
Base class for all exceptions in this module.
Definition: gsexx.h:68
intT Msum
checksum value
Definition: gsexx.h:191
intT value() const
Return the checksum value.
Definition: gsexx.h:181