Monarch  v3.8.2
Project 8 Data File Format Library
M2Header.cc
Go to the documentation of this file.
1 #include "M2Header.hh"
2 #include "MonarchHeader.pb.h"
3 
4 #include "M2Exception.hh"
5 
6 #include <cstdlib> // for atol in parsing timestamp
7 
8 // for parsing timestamp
9 #include <sstream>
10 using std::stringstream;
11 
12 namespace monarch2
13 {
14 
16  fProtobufHeader( new Protobuf::MonarchHeader() )
17  {
18  }
20  {
21  delete fProtobufHeader;
22  }
23 
24  int M2Header::ByteSize() const
25  {
26  return fProtobufHeader->ByteSize();
27  }
28  bool M2Header::MarshalToArray( void* data, int size ) const
29  {
30  return fProtobufHeader->SerializeToArray( data, size );
31  }
32  bool M2Header::MarshalToStream( std::ostream* aStream ) const
33  {
34  return fProtobufHeader->SerializeToOstream( aStream );
35  }
36  bool M2Header::DemarshalFromArray( void* anArray, int aSize ) const
37  {
38  return fProtobufHeader->ParseFromArray( anArray, aSize );
39  }
40  bool M2Header::DemarshalFromStream( std::istream* aStream ) const
41  {
42  return fProtobufHeader->ParseFromIstream( aStream );
43  }
44 
45  void M2Header::SetFilename( const std::string& aFilename )
46  {
47  fProtobufHeader->set_filename( aFilename );
48  return;
49  }
50  const std::string& M2Header::GetFilename() const
51  {
52  return fProtobufHeader->filename();
53  }
54 
55  void M2Header::SetTimestamp( const std::string& aTimestamp )
56  {
57  fProtobufHeader->set_rundate( aTimestamp );
58  return;
59  }
60  const std::string& M2Header::GetTimestamp() const
61  {
62  return fProtobufHeader->rundate();
63  }
64  /*
65 const std::string M2Header::GetDateTime() const
66 {
67  // This shouldn't be called very often, so parsing in place shouldn't be much of a problem
68  // If this needs to be called a lot, this strategy should be rethought
69  stringstream ss(GetTimestamp());
70  std::string item;
71  // first token in the timestamp is the date/time string
72  std::getline(ss, item, sRecordTimeCalSep);
73  return item;
74 }
75 
76 TimeType M2Header::GetRecordTime0() const
77 {
78  // This shouldn't be called very often, so parsing in place shouldn't be much of a problem
79  // If this needs to be called a lot, this strategy should be rethought
80  std::stringstream ss(GetTimestamp());
81  std::string item;
82  // second token in the timestamp is the record time_0
83  std::getline(ss, item, sRecordTimeCalSep);
84  std::getline(ss, item, sRecordTimeCalSep);
85  return (long long int)atol(item.c_str());
86 }
87  */
88  void M2Header::SetDescription( const std::string& aDescription )
89  {
90  fProtobufHeader->set_runinfo( aDescription );
91  return;
92  }
93  const std::string& M2Header::GetDescription() const
94  {
95  return fProtobufHeader->runinfo();
96  }
97 
98 
99  void M2Header::SetRunType( RunType aRunType )
100  {
101  switch( aRunType )
102  {
103  case sRunTypeSignal:
104  fProtobufHeader->set_runtype( Protobuf::MonarchHeader_RunType_Signal );
105  return;
106  case sRunTypeBackground:
107  fProtobufHeader->set_runtype( Protobuf::MonarchHeader_RunType_Background );
108  return;
109  case sRunTypeOther:
110  fProtobufHeader->set_runtype( Protobuf::MonarchHeader_RunType_Other );
111  return;
112  default:
113  throw M2Exception() << "got unknown content mode";
114  }
115  }
117  {
118  switch( fProtobufHeader->runtype() )
119  {
120  case Protobuf::MonarchHeader_RunType_Signal:
121  return sRunTypeSignal;
122  case Protobuf::MonarchHeader_RunType_Background:
123  return sRunTypeBackground;
124  case Protobuf::MonarchHeader_RunType_Other:
125  return sRunTypeOther;
126  default:
127  throw M2Exception() << "has unknown run type";
128  }
129  }
130 
132  {
133  switch( aRunSource )
134  {
135  case sSourceMantis:
136  fProtobufHeader->set_runsource( Protobuf::MonarchHeader_RunSource_Mantis );
137  return;
138  case sSourceSimulation:
139  fProtobufHeader->set_runsource( Protobuf::MonarchHeader_RunSource_Simulation );
140  return;
141  default:
142  throw M2Exception() << "got unknown source mode";
143  }
144  }
146  {
147  switch( fProtobufHeader->runsource() )
148  {
149  case Protobuf::MonarchHeader_RunSource_Mantis:
150  return sSourceMantis;
151  case Protobuf::MonarchHeader_RunSource_Simulation:
152  return sSourceSimulation;
153  default:
154  throw M2Exception() << "has unknown source mode";
155  }
156  }
157 
159  {
160  switch( aFormatMode )
161  {
162  case sFormatSingle:
163  fProtobufHeader->set_formatmode( Protobuf::MonarchHeader_FormatMode_Single );
164  return;
166  fProtobufHeader->set_formatmode( Protobuf::MonarchHeader_FormatMode_MultiSeparate );
167  return;
169  fProtobufHeader->set_formatmode( Protobuf::MonarchHeader_FormatMode_MultiInterleaved );
170  return;
171  default:
172  throw M2Exception() << "got unknown format mode";
173  }
174  }
176  {
177  switch( fProtobufHeader->formatmode() )
178  {
179  case Protobuf::MonarchHeader_FormatMode_Single:
180  return sFormatSingle;
181  case Protobuf::MonarchHeader_FormatMode_MultiSeparate:
182  return sFormatMultiSeparate;
183  case Protobuf::MonarchHeader_FormatMode_MultiInterleaved:
185  default:
186  throw M2Exception() << "has unknown format mode";
187  }
188  }
189 
190  void M2Header::SetAcquisitionMode( unsigned int aMode )
191  {
192  fProtobufHeader->set_acqmode( aMode );
193  return;
194  }
195 
196  unsigned int M2Header::GetAcquisitionMode() const
197  {
198  return fProtobufHeader->acqmode();
199  }
200 
201  void M2Header::SetAcquisitionRate( double aRate )
202  {
203  fProtobufHeader->set_acqrate( aRate );
204  return;
205  }
207  {
208  return fProtobufHeader->acqrate();
209  }
210 
211  void M2Header::SetRunDuration( unsigned int aDuration )
212  {
213  fProtobufHeader->set_acqtime( aDuration );
214  return;
215  }
216  unsigned int M2Header::GetRunDuration() const
217  {
218  return fProtobufHeader->acqtime();
219  }
220 
221  void M2Header::SetRecordSize( unsigned int aSize )
222  {
223  fProtobufHeader->set_recsize( aSize );
224  return;
225  }
226  unsigned int M2Header::GetRecordSize() const
227  {
228  return fProtobufHeader->recsize();
229  }
230 
231  void M2Header::SetDataTypeSize( unsigned aSize )
232  {
233  fProtobufHeader->set_datatypesize( aSize );
234  return;
235  }
236  unsigned M2Header::GetDataTypeSize() const
237  {
238  return fProtobufHeader->datatypesize();
239  }
240 
241  void M2Header::SetBitDepth( unsigned aBitDepth )
242  {
243  fProtobufHeader->set_bitdepth( aBitDepth );
244  return;
245  }
246  unsigned M2Header::GetBitDepth() const
247  {
248  return fProtobufHeader->bitdepth();
249  }
250 
251  void M2Header::SetVoltageMin( double aVoltage )
252  {
253  fProtobufHeader->set_voltagemin( aVoltage );
254  return;
255  }
256  double M2Header::GetVoltageMin() const
257  {
258  return fProtobufHeader->voltagemin();
259  }
260 
261  void M2Header::SetVoltageRange( double aVoltage )
262  {
263  fProtobufHeader->set_voltagerange( aVoltage );
264  return;
265  }
267  {
268  return fProtobufHeader->voltagerange();
269  }
270 }
271 
272 std::ostream& operator<<( std::ostream& out, const monarch2::M2Header& hdr )
273 {
274  out << "Monarch Header Content: " << "\n";
275  out << "\tFilename: " << hdr.GetFilename() << "\n";
276  out << "\tAcquisition Mode (# channels): " << hdr.GetAcquisitionMode() << "\n";
277  out << "\tAcquisition Rate: " << hdr.GetAcquisitionRate() << " MHz\n";
278  out << "\tRun Duration: " << hdr.GetRunDuration() << " ms\n";
279  out << "\tRecord Size: " << hdr.GetRecordSize() << "\n";
280  out << "\tTimestamp: " << hdr.GetTimestamp() << "\n";
281  out << "\tDescription: " << hdr.GetDescription() << "\n";
282  out << "\tRun Type: " << hdr.GetRunType() << "\n";
283  out << "\tRun Source: " << hdr.GetRunSource() << "\n";
284  out << "\tFormat Mode: " << hdr.GetFormatMode() << "\n";
285  out << "\tData Type Size: " << hdr.GetDataTypeSize() << " bytes\n";
286  out << "\tBit Depth: " << hdr.GetBitDepth() << " bits\n";
287  out << "\tVoltage Min: " << hdr.GetVoltageMin() << " V\n";
288  out << "\tVoltage Range: " << hdr.GetVoltageRange() << " V\n";
289  return out;
290 }
unsigned int GetRecordSize() const
Definition: M2Header.cc:226
RunSourceType GetRunSource() const
Definition: M2Header.cc:145
double GetVoltageRange() const
Definition: M2Header.cc:266
const std::string & GetDescription() const
Definition: M2Header.cc:93
Protobuf::MonarchHeader * fProtobufHeader
Definition: M2Header.hh:19
bool DemarshalFromStream(std::istream *aStream) const
Definition: M2Header.cc:40
static const RunType sRunTypeSignal
Definition: M2Types.hh:43
const std::string & GetFilename() const
Definition: M2Header.cc:50
static const FormatModeType sFormatSingle
Definition: M2Types.hh:52
unsigned GetDataTypeSize() const
Definition: M2Header.cc:236
static const RunType sRunTypeOther
Definition: M2Types.hh:45
uint32_t FormatModeType
Definition: M2Types.hh:51
static const RunSourceType sSourceSimulation
Definition: M2Types.hh:49
void SetVoltageMin(double aVoltage)
Definition: M2Header.cc:251
static const RunSourceType sSourceMantis
Definition: M2Types.hh:48
const std::string & GetTimestamp() const
Definition: M2Header.cc:60
void SetFilename(const std::string &aFilename)
Definition: M2Header.cc:45
double GetVoltageMin() const
Definition: M2Header.cc:256
FormatModeType GetFormatMode() const
Definition: M2Header.cc:175
static const FormatModeType sFormatMultiSeparate
Definition: M2Types.hh:53
void SetRecordSize(unsigned int aSize)
Definition: M2Header.cc:221
void SetRunSource(RunSourceType aRunSource)
Definition: M2Header.cc:131
void SetDescription(const std::string &aDescription)
Definition: M2Header.cc:88
unsigned GetBitDepth() const
Definition: M2Header.cc:246
unsigned int GetRunDuration() const
Definition: M2Header.cc:216
void SetDataTypeSize(unsigned aSize)
Definition: M2Header.cc:231
void SetTimestamp(const std::string &aTimestamp)
Definition: M2Header.cc:55
bool DemarshalFromArray(void *anArray, int aSize) const
Definition: M2Header.cc:36
uint32_t RunSourceType
Definition: M2Types.hh:47
uint32_t RunType
Definition: M2Types.hh:42
bool MarshalToArray(void *anArray, int aSize) const
Definition: M2Header.cc:28
static const RunType sRunTypeBackground
Definition: M2Types.hh:44
void SetRunType(RunType aRunType)
Definition: M2Header.cc:99
bool MarshalToStream(std::ostream *aStream) const
Definition: M2Header.cc:32
void SetAcquisitionRate(double aRate)
Definition: M2Header.cc:201
void SetRunDuration(unsigned int aDuration)
Definition: M2Header.cc:211
static const FormatModeType sFormatMultiInterleaved
Definition: M2Types.hh:54
void SetFormatMode(FormatModeType aFormatMode)
Definition: M2Header.cc:158
double GetAcquisitionRate() const
Definition: M2Header.cc:206
AcquisitionModeType GetAcquisitionMode() const
Definition: M2Header.cc:196
void SetVoltageRange(double aVoltage)
Definition: M2Header.cc:261
RunType GetRunType() const
Definition: M2Header.cc:116
int ByteSize() const
Definition: M2Header.cc:24
void SetAcquisitionMode(AcquisitionModeType aMode)
Definition: M2Header.cc:190
std::ostream & operator<<(std::ostream &out, const monarch2::M2Header &hdr)
Definition: M2Header.cc:272
void SetBitDepth(unsigned aBitDepth)
Definition: M2Header.cc:241