Monarch  v3.8.2
Project 8 Data File Format Library
Monarch3TimeCheck.cpp
Go to the documentation of this file.
1 /*
2  * MonarchTimeCheck.cpp
3  *
4  * Created on: May 16, 2013
5  * Author: nsoblath
6  */
7 
8 #include "Monarch.hpp"
9 #include "MonarchLogger.hpp"
10 
11 #include <fstream>
12 using std::ofstream;
13 
14 using namespace monarch;
15 
16 MLOGGER( mlog, "MonarchTimeCheck" );
17 
18 int main( const int argc, const char** argv )
19 {
20  if( argc < 3 )
21  {
22  MLINFO( mlog, "usage:\n"
23  << " MonarchTimeCheck <input egg file> <output text file>" );
24  return -1;
25  }
26 
27  ofstream tOutput( argv[ 2 ] );
28  if( tOutput.is_open() == false )
29  {
30  MERROR( mlog, "could not open output file!" );
31  return -1;
32  }
33 
34  const Monarch* tReadTest = Monarch::OpenForReading( argv[1] );
35  tReadTest->ReadHeader();
36 
37  const MonarchHeader* tReadHeader = tReadTest->GetHeader();
38  MLINFO( mlog, *tReadHeader );
39 
40  TimeType tRecordSize = (TimeType)tReadHeader->GetRecordSize();
41  TimeType tBinWidthNS = (TimeType)(1000. / tReadHeader->GetAcquisitionRate()); // in ns
42 
43  const MonarchRecordBytes* tReadRecord;
44  if( tReadHeader->GetAcquisitionMode() == 1 /* the FormatMode is ignored for single-channel data */ )
45  {
46  tReadRecord = tReadTest->GetRecordSeparateOne();
47  }
48  else if( tReadHeader->GetAcquisitionMode() == 2 && tReadHeader->GetFormatMode() == sFormatMultiSeparate )
49  {
50  tReadRecord = tReadTest->GetRecordSeparateOne();
51  }
52  else if( tReadHeader->GetAcquisitionMode() == 2 && tReadHeader->GetFormatMode() == sFormatMultiInterleaved )
53  {
54  tReadRecord = tReadTest->GetRecordInterleaved();
55  }
56  else
57  {
58  MERROR( mlog, "Unable to read a header with acquisition mode <" << tReadHeader->GetAcquisitionMode() << "> and format mode <" << tReadHeader->GetFormatMode() << ">" );
59  return -1;
60  }
61 
62  unsigned long long tRecordCount = 0;
63  unsigned long long tAcquisitionCount = 0;
64 
65  TimeType tNSTimeInRunClock = 0;
66  TimeType tNSTimeInRunBins = 0;
67  TimeType tNSTimeInRunBinsCorr = 0; // corrected to the Clock time when there's a new acquisition
68 
69  // read first record
70  if (! tReadTest->ReadRecord())
71  {
72  MERROR( mlog, "No records in the file" );
73  return -1;
74  }
75  tRecordCount = 1;
76  tAcquisitionCount = 1;
77  tNSTimeInRunClock = tReadRecord->fTime;
78  tNSTimeInRunBinsCorr = tNSTimeInRunClock;
79  tNSTimeInRunBins = tNSTimeInRunClock;
80 
81  tOutput << tRecordCount << '\t' << tNSTimeInRunClock << '\t' << tNSTimeInRunBins << '\t' << tNSTimeInRunBinsCorr << '\n';
82 
83  while( tReadTest->ReadRecord() != false )
84  {
85  tRecordCount = tRecordCount + 1;
86 
87  tNSTimeInRunClock = tReadRecord->fTime;
88 
89  if( tReadRecord->fAcquisitionId == tAcquisitionCount )
90  {
91  cout << "new acquisition" << endl;
92  tAcquisitionCount = tAcquisitionCount + 1;
93  tNSTimeInRunBinsCorr = tNSTimeInRunClock;
94  }
95  else
96  {
97  tNSTimeInRunBinsCorr += tRecordSize * tBinWidthNS;
98  }
99  tNSTimeInRunBins += tRecordSize * tBinWidthNS;
100 
101  tOutput << tRecordCount << '\t' << tNSTimeInRunClock << '\t' << tNSTimeInRunBins << '\t' << tNSTimeInRunBinsCorr << '\n';
102 
103  //cout << " record " << tRecordCount << ": time offset: " << tReadRecord->fTime << " ns" << endl;
104  }
105  MLINFO( mlog, "record count <" << tRecordCount << ">" );
106  MLINFO( mlog, "acquisition count <" << tAcquisitionCount << ">" );
107 
108  tReadTest->Close();
109  delete tReadTest;
110 
111  tOutput.close();
112 
113  return 0;
114 }
115 
116 
MLOGGER(mlog, "MonarchTimeCheck")
static scarab::logger mlog("M3Info")
int main(const int argc, const char **argv)
static const FormatModeType sFormatMultiSeparate
Definition: M2Types.hh:53
uint64_t TimeType
Definition: M3Types.hh:26
static const FormatModeType sFormatMultiInterleaved
Definition: M2Types.hh:54