Monarch  v3.8.2
Project 8 Data File Format Library
Monarch2Dump.cc
Go to the documentation of this file.
1 #include "M2Monarch.hh"
2 
3 #include "application.hh"
4 #include "logger.hh"
5 
6 #include <cstdlib>
7 
8 #include <fstream>
9 using std::ofstream;
10 
11 using namespace monarch2;
12 
13 LOGGER( mlog, "Monarch2Dump" );
14 
15 int main( const int argc, const char** argv )
16 {
17  scarab::main_app theMain( false );
18 
19  unsigned tNRecords;
20  std::string tInputFilename, tOutputFilebase;
21 
22  theMain.add_option( "-n,--n-records", tNRecords, "Number of records to dump; use 0 to dump the whole file" )->default_val(0);
23  theMain.add_option( "InputFilename", tInputFilename, "Input egg file" )->required();
24  theMain.add_option( "OutputFilename", tOutputFilebase, "Filename base for output files (will have ch[#].txt appended)" )->required();
25 
26  CLI11_PARSE( theMain, argc, argv );
27 
28  std::shared_ptr< const Monarch2 > tReadTest( Monarch2::OpenForReading( tInputFilename ) );
29  tReadTest->ReadHeader();
30  tReadTest->SetInterface( sInterfaceSeparate );
31 
32  const M2Header* tReadHeader = tReadTest->GetHeader();
33  LINFO( mlog, *tReadHeader );
34 
35  unsigned int tRecordCount = 0;
36  unsigned int tAcquisitionCount = 0;
37 
38  if( tReadHeader->GetFormatMode() == sFormatSingle )
39  {
40  ofstream tOutputOne( (tOutputFilebase + std::string( "_ch1.txt" )).c_str() );
41  if( tOutputOne.is_open() == false )
42  {
43  LERROR( mlog, "could not open channel one output file!" );
44  tReadTest->Close();
45  return RETURN_ERROR;
46  }
47 
48  const unsigned tDataTypeSize = tReadHeader->GetDataTypeSize();
49  const M2RecordBytes* tReadRecord = tReadTest->GetRecordSeparateOne();
50  const M2RecordDataInterface< uint64_t > tData( tReadRecord->fData, tDataTypeSize );
51  unsigned int tRecordsPerChannel = 0;
52  while( tReadTest->ReadRecord() != false )
53  {
54  ++tRecordCount;
55  ++tRecordsPerChannel;
56  if( tReadRecord->fAcquisitionId == tAcquisitionCount )
57  {
58  tAcquisitionCount = tAcquisitionCount + 1;
59  tOutputOne << "\n\n";
60  }
61  for( unsigned int tIndex = 0; tIndex < tReadHeader->GetRecordSize(); tIndex++ )
62  {
63  tOutputOne << tIndex << " " << tData.at( tIndex ) << "\n";
64  }
65  if (tNRecords != 0 && tRecordsPerChannel >= tNRecords) break;
66  }
67 
68  tOutputOne.close();
69  }
70  if( (tReadHeader->GetFormatMode() == sFormatMultiInterleaved) || (tReadHeader->GetFormatMode() == sFormatMultiSeparate) )
71  {
72  ofstream tOutputOne( (tOutputFilebase + std::string( "_ch1.txt" )).c_str() );
73  ofstream tOutputTwo( (tOutputFilebase + std::string( "_ch2.txt" )).c_str() );
74  if( tOutputOne.is_open() == false )
75  {
76  LERROR( mlog, "could not open channel one output file!" );
77  tReadTest->Close();
78  return RETURN_ERROR;
79  }
80  if( tOutputTwo.is_open() == false )
81  {
82  LERROR( mlog, "could not open channel two output file!" );
83  tReadTest->Close();
84  return RETURN_ERROR;
85  }
86 
87  const unsigned tDataTypeSize = tReadHeader->GetDataTypeSize();
88  const M2RecordBytes* tReadRecordOne = tReadTest->GetRecordSeparateOne();
89  const M2RecordDataInterface< uint64_t > tDataOne( tReadRecordOne->fData, tDataTypeSize );
90  const M2RecordBytes* tReadRecordTwo = tReadTest->GetRecordSeparateTwo();
91  const M2RecordDataInterface< uint64_t > tDataTwo( tReadRecordTwo->fData , tDataTypeSize);
92  unsigned int tRecordsPerChannel = 0;
93  while( tReadTest->ReadRecord() != false )
94  {
95  tRecordCount = tRecordCount + 1;
96  ++tRecordsPerChannel;
97  if( tReadRecordOne->fAcquisitionId == tAcquisitionCount )
98  {
99  tAcquisitionCount = tAcquisitionCount + 1;
100  tOutputOne << "\n\n";
101  tOutputTwo << "\n\n";
102  }
103  for( unsigned int tIndex = 0; tIndex < tReadHeader->GetRecordSize(); ++tIndex )
104  {
105  tOutputOne << tIndex << " " << tDataOne.at( tIndex ) << "\n";
106  tOutputTwo << tIndex << " " << tDataTwo.at( tIndex ) << "\n";
107  }
108  if (tNRecords != 0 && tRecordsPerChannel >= tNRecords) break;
109  }
110 
111  tOutputOne.close();
112  tOutputTwo.close();
113  }
114 
115  LINFO( mlog, "record count <" << tRecordCount << ">" );
116  LINFO( mlog, "acquisition count <" << tAcquisitionCount << ">" );
117 
118  tReadTest->Close();
119 
120  return RETURN_SUCCESS;
121 }
122 
unsigned int GetRecordSize() const
Definition: M2Header.cc:226
static scarab::logger mlog("Monarch2Dump")
static const FormatModeType sFormatSingle
Definition: M2Types.hh:52
unsigned GetDataTypeSize() const
Definition: M2Header.cc:236
DataType fData[]
Definition: M2Record.hh:15
FormatModeType GetFormatMode() const
Definition: M2Header.cc:175
static const FormatModeType sFormatMultiSeparate
Definition: M2Types.hh:53
int main(const int argc, const char **argv)
Definition: Monarch2Dump.cc:15
AcquisitionIdType fAcquisitionId
Definition: M2Record.hh:12
ReturnType at(unsigned index) const
Definition: M2Record.hh:33
static const FormatModeType sFormatMultiInterleaved
Definition: M2Types.hh:54
static const Monarch2 * OpenForReading(const std::string &filename)
Definition: M2Monarch.cc:62
static const AccessModeType sInterfaceSeparate
Definition: M2Types.hh:36