Monarch  v3.8.2
Project 8 Data File Format Library
M2Monarch.cc
Go to the documentation of this file.
1 #include "M2Monarch.hh"
2 #include "M2Exception.hh"
3 
4 namespace monarch2
5 {
6 
8  fState( eClosed ),
9  fIO( NULL ),
10  fHeader( NULL ),
11  fDataTypeSize( 1 ),
12  fDataNBytes( 0 ),
13  fDataSize( 0 ),
14  fInterleavedRecordNBytes( 0 ),
15  fRecordInterleaved( NULL ),
16  fRecordInterleavedBytes( NULL ),
17  fSeparateRecordNBytes( 0 ),
18  fRecordSeparateOne( NULL ),
19  fRecordSeparateOneBytes( NULL ),
20  fRecordSeparateTwo( NULL ),
21  fRecordSeparateTwoBytes( NULL ),
22  fReadFunction( &Monarch2::InterleavedFromInterleaved ),
23  fWriteFunction( &Monarch2::InterleavedToInterleaved )
24  {
25  }
27  {
28  if( fIO != NULL )
29  {
30  delete fIO;
31  fIO = NULL;
32  }
33 
34  if( fHeader != NULL )
35  {
36  delete fHeader;
37  fHeader = NULL;
38  }
39 
40  if( fRecordInterleavedBytes != NULL )
41  {
42  fRecordInterleaved->~M2RecordBytes();
43  delete[] fRecordInterleavedBytes;
45  }
46 
47  if( fRecordSeparateOneBytes != NULL )
48  {
49  fRecordSeparateOne->~M2RecordBytes();
50  delete[] fRecordSeparateOneBytes;
52  }
53 
54  if( fRecordSeparateTwoBytes != NULL )
55  {
56  fRecordSeparateTwo->~M2RecordBytes();
57  delete[] fRecordSeparateTwoBytes;
59  }
60  }
61 
62  const Monarch2* Monarch2::OpenForReading( const std::string& aFilename )
63  {
64  Monarch2* tMonarch = new Monarch2();
65 
66  tMonarch->fIO = new M2IO( sAccessRead );
67  if( tMonarch->fIO->Open( aFilename ) == false )
68  {
69  delete tMonarch;
70  throw M2Exception() << "could not open <" << aFilename << "> for reading";
71  return NULL;
72  }
73 
74  tMonarch->fHeader = new M2Header();
75  tMonarch->fHeader->SetFilename( aFilename );
76 
77  tMonarch->fState = eOpen;
78 
79  return tMonarch;
80  }
81 
82  Monarch2* Monarch2::OpenForWriting( const std::string& aFilename )
83  {
84  Monarch2* tMonarch = new Monarch2();
85 
86  tMonarch->fIO = new M2IO( sAccessWrite );
87  if( tMonarch->fIO->Open( aFilename ) == false )
88  {
89  delete tMonarch;
90  throw M2Exception() << "could not open <" << aFilename << "> for writing";
91  return NULL;
92  }
93 
94  tMonarch->fHeader = new M2Header();
95  tMonarch->fHeader->SetFilename( aFilename );
96 
97  tMonarch->fState = eOpen;
98 
99  return tMonarch;
100  }
101 
102  void Monarch2::ReadHeader() const
103  {
104  PreludeType tPrelude = 0;
105  if( fIO->Read( &tPrelude ) == false )
106  {
107  throw M2Exception() << "prelude was not read properly";
108  return;
109  }
110 
111  char* tHeaderBuffer = new char[ tPrelude ];
112  if( fIO->Read( tHeaderBuffer, tPrelude ) == false )
113  {
114  delete[] tHeaderBuffer;
115  throw M2Exception() << "header was not read properly";
116  return;
117  }
118  if( fHeader->DemarshalFromArray( tHeaderBuffer, tPrelude ) == false )
119  {
120  delete[] tHeaderBuffer;
121  throw M2Exception() << "header was not demarshalled properly";
122  return;
123  }
124  delete[] tHeaderBuffer;
125 
127 
128  if( fHeader->GetAcquisitionMode() == 1 /* the FormatMode is ignored for single-channel data */ )
129  {
132 
136 
140 
141  //cout << " *format is <" << sFormatSingle << ">" << endl;
142  //cout << " *data size is <" << fDataSize << ">" << endl;
143  //cout << " *interleaved size is <" << fInterleavedRecordSize << ">" << endl;
144  //cout << " *separate size is <" << fSeparateRecordSize << ">" << endl;
145 
147  }
149  {
152 
153  fInterleavedRecordNBytes = sizeof(AcquisitionIdType) + sizeof(RecordIdType) + sizeof(TimeType) + 2 * fDataNBytes;
156 
157  fSeparateRecordNBytes = sizeof(AcquisitionIdType) + sizeof(RecordIdType) + sizeof(TimeType) + (size_t)fDataNBytes;
162 
163  //cout << " *format is <" << sFormatSeparateDual << ">" << endl;
164  //cout << " *data size is <" << fDataSize << ">" << endl;
165  //cout << " *interleaved size is <" << fInterleavedRecordSize << ">" << endl;
166  //cout << " *separate size is <" << fSeparateRecordSize << ">" << endl;
167 
169  }
171  {
174 
175  fInterleavedRecordNBytes = sizeof(AcquisitionIdType) + sizeof(RecordIdType) + sizeof(TimeType) + 2 * fDataNBytes;
178 
184 
185  //cout << " *format is <" << sFormatInterleavedDual << ">" << endl;
186  //cout << " *data size is <" << fDataSize << ">" << endl;
187  //cout << " *interleaved size is <" << fInterleavedRecordSize << ">" << endl;
188  //cout << " *separate size is <" << fSeparateRecordSize << ">" << endl;
189 
191  }
192  else
193  {
194  throw M2Exception() << "Unable to read a header with acquisition mode <" << fHeader->GetAcquisitionMode() << "> and format mode <" << fHeader->GetFormatMode() << ">";
195  return;
196  }
197 
198  fState = eReady;
199  return;
200  }
201 
203  {
204  PreludeType tPrelude = fHeader->ByteSize();
205  if( fIO->Write( &tPrelude ) == false )
206  {
207  throw M2Exception() << "prelude was not written properly";
208  return;
209  }
210 
211  char* tHeaderBuffer = new char[ tPrelude ];
212  if( fHeader->MarshalToArray( tHeaderBuffer, tPrelude ) == false )
213  {
214  delete[] tHeaderBuffer;
215  throw M2Exception() << "header was not marshalled properly";
216  return;
217  }
218  if( fIO->Write( tHeaderBuffer, tPrelude ) == false )
219  {
220  delete[] tHeaderBuffer;
221  throw M2Exception() << "header was not written properly";
222  return;
223  }
224  delete[] tHeaderBuffer;
225 
227 
228  if( fHeader->GetAcquisitionMode() == 1 /* the FormatMode is ignored for single-channel data */ )
229  {
232 
236 
240 
241  //cout << " *format is <" << sFormatSingle << ">" << endl;
242  //cout << " *data type size is <" << fDataTypeSize << ">" << endl;
243  //cout << " *data size is <" << fDataSize << "> and # of data bytes is <" << fDataNBytes << ">" << endl;
244  //cout << " *interleaved # of bytes is <" << fInterleavedRecordNBytes << ">" << endl;
245  //cout << " *separate # of bytes is <" << fSeparateRecordNBytes << ">" << endl;
246 
248  }
250  {
253 
254  fInterleavedRecordNBytes = sizeof(AcquisitionIdType) + sizeof(RecordIdType) + sizeof(TimeType) + 2 * fDataNBytes;
257 
263 
264  //cout << " *format is <" << sFormatMultiSeparate << ">" << endl;
265  //cout << " *data size is <" << fDataSize << "> and # of data bytes is <" << fDataNBytes << ">" << endl;
266  //cout << " *interleaved # of bytes is <" << fInterleavedRecordNBytes << ">" << endl;
267  //cout << " *separate # of bytes is <" << fSeparateRecordNBytes << ">" << endl;
268 
270  }
272  {
275 
276  fInterleavedRecordNBytes = sizeof(AcquisitionIdType) + sizeof(RecordIdType) + sizeof(TimeType) + 2 * fDataNBytes;
279 
285 
286  //cout << " *format is <" << sFormatMultiInterleaved << ">" << endl;
287  //cout << " *data size is <" << fDataSize << "> and # of data bytes is <" << fDataNBytes << ">" << endl;
288  //cout << " *interleaved # of bytes is <" << fInterleavedRecordNBytes << ">" << endl;
289  //cout << " *separate # of bytes is <" << fSeparateRecordNBytes << ">" << endl;
290 
292  }
293  else
294  {
295  throw M2Exception() << "unable to write a header with acquisition mode <" << fHeader->GetAcquisitionMode() << "> and format mode <" << fHeader->GetFormatMode() << ">";
296  return;
297  }
298 
299  fState = eReady;
300  return;
301  }
302 
304  {
305  if( aMode == sInterfaceInterleaved )
306  {
307  if( fHeader->GetAcquisitionMode() == 1 /* the FormatMode is ignored for single-channel data */ )
308  {
310  }
312  {
314  }
316  {
318  }
319  }
320  if( aMode == sInterfaceSeparate )
321  {
322  if( fHeader->GetAcquisitionMode() == 1 /* the FormatMode is ignored for single-channel data */ )
323  {
325  }
327  {
329  }
331  {
333  }
334  }
335  return;
336  }
337 
339  {
340  if( aMode == sInterfaceInterleaved )
341  {
342  if( fHeader->GetAcquisitionMode() == 1 /* the FormatMode is ignored for single-channel data */ )
343  {
345  }
347  {
349  }
351  {
353  }
354  }
355  if( aMode == sInterfaceSeparate )
356  {
357  if( fHeader->GetAcquisitionMode() == 1 /* the FormatMode is ignored for single-channel data */ )
358  {
360  }
362  {
364  }
366  {
368  }
369  }
370  return;
371  }
372 
373  bool Monarch2::ReadRecord( int anOffset ) const
374  {
375  return (this->*fReadFunction)( anOffset );
376  }
377 
378  bool Monarch2::InterleavedFromSingle( int anOffset ) const
379  {
380  if( anOffset != 0 )
381  {
382  long int aByteOffset = anOffset * fInterleavedRecordNBytes;
383  if( fIO->Seek( aByteOffset ) == false )
384  {
385  if( fIO->Done() != true )
386  {
387  std::cout << "could not seek to requested position" << std::endl;
388  }
389  return false;
390  }
391  }
392 
394  {
395  if( fIO->Done() != true )
396  {
397  throw M2Exception() << "could not read next single record";
398  }
399  return false;
400  }
401 
402  return true;
403  }
404 
405  bool Monarch2::InterleavedFromSeparate( int anOffset ) const
406  {
407  if( anOffset != 0 )
408  {
409  long int aByteOffset = anOffset * 2 * fSeparateRecordNBytes;
410  if( fIO->Seek( aByteOffset ) == false )
411  {
412  if( fIO->Done() != true )
413  {
414  std::cout << "could not seek to requested position" << std::endl;
415  }
416  return false;
417  }
418  }
419 
421  {
422  if( fIO->Done() != true )
423  {
424  throw M2Exception() << "could not read next channel one record";
425  }
426  return false;
427  }
428 
430  {
431  if( fIO->Done() != true )
432  {
433  throw M2Exception() << "could not read next channel two record";
434  }
435  return false;
436  }
437 
442 
443  return true;
444  }
445 
446  bool Monarch2::InterleavedFromInterleaved( int anOffset ) const
447  {
448  if( anOffset != 0 )
449  {
450  long int aByteOffset = anOffset * fInterleavedRecordNBytes;
451  if( fIO->Seek( aByteOffset ) == false )
452  {
453  if( fIO->Done() != true )
454  {
455  std::cout << "could not seek to requested position" << std::endl;
456  }
457  return false;
458  }
459  }
460 
462  {
463  if( fIO->Done() != true )
464  {
465  throw M2Exception() << "could not read next interleaved record";
466  }
467  return false;
468  }
469 
470  return true;
471  }
472 
473  bool Monarch2::SeparateFromSingle( int anOffset ) const
474  {
475  if( anOffset != 0 )
476  {
477  long int aByteOffset = anOffset * fSeparateRecordNBytes;
478  if( fIO->Seek( aByteOffset ) == false )
479  {
480  if( fIO->Done() != true )
481  {
482  std::cout << "could not seek to requested position" << std::endl;
483  }
484  return false;
485  }
486  }
487 
489  {
490  if( fIO->Done() != true )
491  {
492  throw M2Exception() << "could not read next channel one record";
493  }
494  return false;
495  }
496 
497  return true;
498  }
499 
500  bool Monarch2::SeparateFromSeparate( int anOffset ) const
501  {
502  if( anOffset != 0 )
503  {
504  long int aByteOffset = anOffset * 2 * fSeparateRecordNBytes;
505  if( fIO->Seek( aByteOffset ) == false )
506  {
507  if( fIO->Done() != true )
508  {
509  std::cout << "could not seek to requested position" << std::endl;
510  }
511  return false;
512  }
513  }
514 
516  {
517  if( fIO->Done() != true )
518  {
519  throw M2Exception() << "could not read next channel one record";
520  }
521  return false;
522  }
523 
525  {
526  if( fIO->Done() != true )
527  {
528  throw M2Exception() << "could not read next channel two record";
529  }
530  return false;
531  }
532 
533  return true;
534  }
535 
536  bool Monarch2::SeparateFromInterleaved( int anOffset ) const
537  {
538  if( anOffset != 0 )
539  {
540  long int aByteOffset = anOffset * fInterleavedRecordNBytes;
541  if( fIO->Seek( aByteOffset ) == false )
542  {
543  if( fIO->Done() != true )
544  {
545  std::cout << "could not seek to requested position" << std::endl;
546  }
547  return false;
548  }
549  }
550 
552  {
553  if( fIO->Done() != true )
554  {
555  throw M2Exception() << "could not read next interleaved record";
556  }
557  return false;
558  }
559 
567 
568  return true;
569  }
570 
572  {
573  return (this->*fWriteFunction)();
574  }
575 
577  {
579  {
580  throw M2Exception() << "could not write single record";
581  return false;
582  }
583 
584  return true;
585  }
586 
588  {
594 
596  {
597  throw M2Exception() << "could not write next channel one record";
598  return false;
599  }
600 
602  {
603  throw M2Exception() << "could not write next channel two record";
604  return false;
605  }
606 
607  return true;
608  }
609 
611  {
613  {
614  throw M2Exception() << "could not write interleaved record";
615  return false;
616  }
617 
618  return true;
619  }
620 
622  {
624  {
625  throw M2Exception() << "could not write single record";
626  return false;
627  }
628 
629  return true;
630  }
631 
633  {
635  {
636  throw M2Exception() << "could not write next channel one record";
637  return false;
638  }
639 
641  {
642  throw M2Exception() << "could not write next channel two record";
643  return false;
644  }
645 
646  return true;
647  }
648 
650  {
655 
657  {
658  throw M2Exception() << "could not write interleaved record";
659  return false;
660  }
661 
662  return true;
663  }
664 
665  void Monarch2::Close() const
666  {
667  if( fIO->Close() == false )
668  {
669  throw M2Exception() << "could not close file";
670  }
671  return;
672  }
673 
675  {
676  if( fIO->Close() == false )
677  {
678  throw M2Exception() << "could not close file";
679  }
680  return;
681  }
682 
683 }
size_t fInterleavedRecordNBytes
Definition: M2Monarch.hh:129
unsigned int GetRecordSize() const
Definition: M2Header.cc:226
static const AccessModeType sAccessRead
Definition: M2Types.hh:31
bool SeparateFromSeparate(int anOffset) const
Definition: M2Monarch.cc:500
bool SeparateToSingle()
Definition: M2Monarch.cc:621
static Monarch2 * OpenForWriting(const std::string &filename)
Definition: M2Monarch.cc:82
bool Close()
Definition: M2IO.cc:49
TimeType fTime
Definition: M2Record.hh:14
unsigned GetDataTypeSize() const
Definition: M2Header.cc:236
bool ReadRecord(int anOffset=0) const
Definition: M2Monarch.cc:373
DataType fData[]
Definition: M2Record.hh:15
uint32_t InterfaceModeType
Definition: M2Types.hh:34
bool SeparateFromSingle(int anOffset) const
Definition: M2Monarch.cc:473
bool Seek(long int aCount)
Definition: M2IO.hh:71
bool InterleavedToSeparate()
Definition: M2Monarch.cc:587
void SetFilename(const std::string &aFilename)
Definition: M2Header.cc:45
byte_type * fRecordInterleavedBytes
Definition: M2Monarch.hh:134
M2RecordBytes * fRecordSeparateTwo
Definition: M2Monarch.hh:145
bool SeparateToInterleaved()
Definition: M2Monarch.cc:649
size_t PreludeType
Definition: M2Types.hh:28
FormatModeType GetFormatMode() const
Definition: M2Header.cc:175
static void Unzip(const size_t aSize, const size_t aDataTypeSize, byte_type *aRecordOne, byte_type *aRecordTwo, const byte_type *anInterleavedRecord)
Definition: M2Monarch.hh:239
bool Write(byte_type *anArray, size_t aCount)
Definition: M2IO.hh:53
M2Header * fHeader
Definition: M2Monarch.hh:118
static const FormatModeType sFormatMultiSeparate
Definition: M2Types.hh:53
void SetInterface(InterfaceModeType aMode) const
Definition: M2Monarch.cc:303
bool InterleavedFromSeparate(int anOffset) const
Definition: M2Monarch.cc:405
static const AccessModeType sAccessWrite
Definition: M2Types.hh:32
bool Open(const std::string &aFilename)
Definition: M2IO.cc:20
byte_type * fRecordSeparateOneBytes
Definition: M2Monarch.hh:142
uint64_t AcquisitionIdType
Definition: M2Types.hh:57
bool DemarshalFromArray(void *anArray, int aSize) const
Definition: M2Header.cc:36
M2RecordBytes * fRecordInterleaved
Definition: M2Monarch.hh:132
bool Read(byte_type *anArray, size_t aCount)
Definition: M2IO.hh:77
size_t fSeparateRecordNBytes
Definition: M2Monarch.hh:137
byte_type * fRecordSeparateTwoBytes
Definition: M2Monarch.hh:147
bool MarshalToArray(void *anArray, int aSize) const
Definition: M2Header.cc:28
M2Record< byte_type > M2RecordBytes
Definition: M2Record.hh:18
bool Done()
Definition: M2IO.cc:37
bool InterleavedFromSingle(int anOffset) const
Definition: M2Monarch.cc:378
RecordIdType fRecordId
Definition: M2Record.hh:13
void ReadHeader() const
Definition: M2Monarch.cc:102
bool(Monarch2::* fWriteFunction)()
Definition: M2Monarch.hh:159
bool(Monarch2::* fReadFunction)(int anOffset) const
Definition: M2Monarch.hh:150
bool InterleavedToSingle()
Definition: M2Monarch.cc:576
uint8_t byte_type
Definition: M2Types.hh:10
static const AccessModeType sInterfaceInterleaved
Definition: M2Types.hh:35
static const FormatModeType sFormatMultiInterleaved
Definition: M2Types.hh:54
bool SeparateFromInterleaved(int anOffset) const
Definition: M2Monarch.cc:536
static const Monarch2 * OpenForReading(const std::string &filename)
Definition: M2Monarch.cc:62
void Close() const
Definition: M2Monarch.cc:665
AcquisitionModeType GetAcquisitionMode() const
Definition: M2Header.cc:196
bool SeparateToSeparate()
Definition: M2Monarch.cc:632
int ByteSize() const
Definition: M2Header.cc:24
M2RecordBytes * fRecordSeparateOne
Definition: M2Monarch.hh:140
bool InterleavedFromInterleaved(int anOffset) const
Definition: M2Monarch.cc:446
static const AccessModeType sInterfaceSeparate
Definition: M2Types.hh:36
bool InterleavedToInterleaved()
Definition: M2Monarch.cc:610
uint64_t RecordIdType
Definition: M2Types.hh:58
static void Zip(const size_t aSize, const size_t aDataTypeSize, const byte_type *aRecordOne, const byte_type *aRecordTwo, byte_type *anInterleavedRecord)
Definition: M2Monarch.hh:221
uint64_t TimeType
Definition: M2Types.hh:59