Monarch  v3.8.2
Project 8 Data File Format Library
M3Info.cc
Go to the documentation of this file.
1 #ifdef _WIN32
2 #define NOMINMAX
3 #endif
4 
5 #include "M3DataInterface.hh"
6 #include "M3Monarch.hh"
7 #include "M3Record.hh"
8 
9 #include "application.hh"
10 #include "logger.hh"
11 
12 #include <algorithm>
13 #include <cstring> // for strcmp
14 #include <sstream>
15 
16 using namespace monarch3;
17 using std::stringstream;
18 
19 LOGGER( mlog, "M3Info" );
20 
21 template< typename XDataType >
22 bool PrintChannelsReal( const M3Stream* aStream, uint32_t aDataFormat );
23 
24 template< typename XDataType >
25 bool PrintChannelsComplex( const M3Stream* aStream, uint32_t aDataFormat );
26 
27 int main( const int argc, const char** argv )
28 {
29  scarab::main_app theMain( false );
30 
31  bool tHeaderOnly;
32  std::string tFilename;
33 
34  theMain.add_flag( "-H,--header-only", tHeaderOnly, "Only look at header information; does not check number of records" );
35  theMain.add_option( "Filename", tFilename, "File to read" )->required();
36 
37  CLI11_PARSE( theMain, argc, argv );
38 
39  try
40  {
41  LPROG( mlog, "Opening file <" << tFilename << ">" );
42  std::shared_ptr< const Monarch3 > tReadTest( Monarch3::OpenForReading( tFilename ) );
43 
44  LPROG( mlog, "Reading header" );
45  tReadTest->ReadHeader();
46 
47  const M3Header* tReadHeader = tReadTest->GetHeader();
48  LPROG( mlog, *tReadHeader );
49 
50  if( tHeaderOnly )
51  {
52  tReadTest->FinishReading();
53  return RETURN_SUCCESS;
54  }
55 
56  LPROG( mlog, "Reading data" );
57 
58  unsigned tNStreams = tReadHeader->GetNStreams();
59  for( unsigned iStream = 0; iStream < tNStreams; ++iStream )
60  {
61  const M3StreamHeader& tStrHeader = tReadHeader->StreamHeaders().at( iStream );
62  unsigned tNChannels = tStrHeader.GetNChannels();
63  //unsigned tRecSize = tStrHeader.GetRecordSize();
64  LPROG( mlog, "Stream " << iStream << " has " << tNChannels << " channel(s) stored in format mode " << tStrHeader.GetChannelFormat() );
65 
66  const M3Stream* tStream = tReadTest->GetStream( iStream );
67 
68  if( tStream->GetNAcquisitions() == 0 )
69  {
70  LPROG( mlog, "\tThis stream has no acquisitions" );
71  continue;
72  }
73 
74  const unsigned tMaxRecords = 2;
75  for( unsigned iRec = 0; iRec < tMaxRecords; ++iRec )
76  {
77  if( ! tStream->ReadRecord() )
78  {
79  throw M3Exception() << "There was a problem reading a record from this stream";
80  }
81  switch( tStrHeader.GetDataFormat() )
82  {
83  case sDigitizedUS:
84  if( ! PrintChannelsReal< uint64_t >( tStream, sDigitizedUS ) )
85  {
86  throw M3Exception() << "Problem printing channels (int)";
87  }
88  break;
89  case sDigitizedS:
90  if( ! PrintChannelsReal< int64_t >( tStream, sDigitizedS ) )
91  {
92  throw M3Exception() << "Problem printing channels (int)";
93  }
94  break;
95  case sAnalog:
96  switch( tStream->GetSampleSize() )
97  {
98  case 1:
99  if( ! PrintChannelsReal< double >( tStream, sAnalog ) )
100  {
101  throw M3Exception() << "Problem printing channels (float)" ;
102  }
103  break;
104  case 2:
105  switch( tStream->GetDataTypeSize() )
106  {
107  case 4:
108  if( ! PrintChannelsComplex< f4_complex >( tStream, sAnalog ) )
109  {
110  throw M3Exception() << "Problem printing channels (float-complex)" ;
111  }
112  break;
113  case 8:
114  if( ! PrintChannelsComplex< f8_complex >( tStream, sAnalog ) )
115  {
116  throw M3Exception() << "Problem printing channels (float-complex)";
117  }
118  break;
119  default:
120  throw M3Exception() << "Invalid data type size: " << tStream->GetDataTypeSize();
121  }
122  break;
123  default:
124  throw M3Exception() << "Invalid sample size: " << tStream->GetSampleSize();
125  }
126  break;
127  default:
128  throw M3Exception() << "Invalid data format: "<<tStrHeader.GetDataFormat();
129  }
130  }
131 
132  }
133 
134  tReadTest->FinishReading();
135  }
136  catch( M3Exception& e )
137  {
138  LERROR( mlog, "Exception thrown during file reading:\n" << e.what() );
139  return RETURN_ERROR;
140  }
141 
142  return RETURN_SUCCESS;
143 }
144 
145 template< typename XDataType >
146 bool PrintChannelsReal( const M3Stream* aStream, uint32_t aDataFormat )
147 {
148  const unsigned tMaxSamples = 30;
149  unsigned tRecSize = aStream->GetChannelRecordSize();
150  for( unsigned iChan = 0; iChan < aStream->GetNChannels(); ++iChan )
151  {
152  const M3DataReader< XDataType > tDataInterface( aStream->GetChannelRecord( iChan )->GetData(),
153  aStream->GetDataTypeSize(), aDataFormat );
154  stringstream tDataOut;
155  for( unsigned iSample = 0; iSample < std::min( tMaxSamples, tRecSize ); ++iSample )
156  {
157  tDataOut << tDataInterface.at( iSample );
158  if( iSample != tRecSize - 1 ) tDataOut << ", ";
159  }
160  if( tRecSize > tMaxSamples ) tDataOut << " . . .";
161  LPROG( mlog, "\tChannel " << iChan << ": " << tDataOut.str() );
162  }
163  return true;
164 }
165 
166 template< typename XDataType >
167 bool PrintChannelsComplex( const M3Stream* aStream, uint32_t aDataFormat )
168 {
169  const unsigned tMaxSamples = 30;
170  unsigned tRecSize = aStream->GetChannelRecordSize();
171  for( unsigned iChan = 0; iChan < aStream->GetNChannels(); ++iChan )
172  {
173  const M3ComplexDataReader< XDataType > tDataInterface( aStream->GetChannelRecord( iChan )->GetData(),
174  aStream->GetDataTypeSize(), aDataFormat, aStream->GetSampleSize() );
175  stringstream tDataOut;
176  for( unsigned iSample = 0; iSample < std::min( tMaxSamples, tRecSize ); ++iSample )
177  {
178  tDataOut << tDataInterface.at( iSample );
179  if( iSample != tRecSize - 1 ) tDataOut << ", ";
180  }
181  if( tRecSize > tMaxSamples ) tDataOut << " . . .";
182  LPROG( mlog, "\tChannel " << iChan << ": " << tDataOut.str() );
183  }
184  return true;
185 }
const ReturnType & at(unsigned index) const
static const Monarch3 * OpenForReading(const std::string &filename)
Definition: M3Monarch.cc:56
static const uint32_t sDigitizedUS
Definition: M3Constants.hh:38
bool PrintChannelsComplex(const M3Stream *aStream, uint32_t aDataFormat)
Definition: M3Info.cc:167
unsigned GetSampleSize() const
Definition: M3Stream.hh:119
unsigned GetChannelRecordSize() const
Definition: M3Stream.hh:123
unsigned GetNChannels() const
Definition: M3Stream.hh:124
virtual const char * what() const
Definition: M3Exception.cc:34
Egg file header information.
Definition: M3Header.hh:152
ReturnType at(unsigned index) const
static const uint32_t sDigitizedS
Definition: M3Constants.hh:39
Read/write access for a data stream.
Definition: M3Stream.hh:41
const byte_type * GetData() const
Definition: M3Record.hh:111
Specialized exception class for Monarch3.
Definition: M3Exception.hh:28
Single-stream header information.
Definition: M3Header.hh:33
bool PrintChannelsReal(const M3Stream *aStream, uint32_t aDataFormat)
Definition: M3Info.cc:146
static scarab::logger mlog("M3Header")
const M3Record * GetChannelRecord(unsigned aChannel) const
Get the pointer to a particular channel record.
Definition: M3Stream.cc:304
Interface class for complex data types.
int main(const int argc, const char **argv)
Definition: M3Info.cc:27
Interface class for a variety of data types.
static const uint32_t sAnalog
Definition: M3Constants.hh:40
unsigned GetDataTypeSize() const
Definition: M3Stream.hh:118