00001 #include <iostream>
00002 #include <string>
00003 #include <fstream>
00004 #include <sstream>
00005 #include <inttypes.h>
00006 #include <iomanip>
00007
00008 using namespace std;
00009
00010 const int nChs = 68;
00011 const int nEvts = 2048;
00012 uint16_t mem[nChs][nEvts];
00013
00022 int main(int argc, char* argv[]){
00023 if((argc>=2 && (argv[1]=="-h" | argv[1]=="--help"))
00024 || argc!=3){
00025 cout << "Usage: recycleTccEmu infile outfile\n";
00026 return 1;
00027 }
00028
00029 string ifilename = argv[1];
00030 string ofilename = argv[2];
00031
00032 for(int iCh=0; iCh<nChs; ++iCh){
00033 for(int iEvts = 0; iEvts<nEvts; ++iEvts){
00034 mem[iCh][iEvts] = 0xFFFF;
00035 }
00036 }
00037
00038 ifstream in(ifilename.c_str());
00039 int chnb;
00040 int bcnb;
00041 int val ;
00042 int dummy ;
00043 int oldLineCnt = 0;
00044
00045
00046 if(in){
00047 while(!in.eof()) {
00048 in >>dec>> chnb >> bcnb >>hex>> val >> dummy ;
00049 mem[chnb-1][bcnb] = val&0x7FF;
00050 if(mem[chnb-1][bcnb]!=val){
00051 cout << "Invalid Et value at line " << oldLineCnt+1 << ".\n";
00052 exit(1);
00053 }
00054
00055
00056
00057 ++oldLineCnt;
00058 }
00059 } else{
00060 cout << "Failed to open file " << ifilename << "\n";
00061 }
00062
00063 in.close();
00064 ofstream out(ofilename.c_str());
00065
00066 if(!out){
00067 cout << "Failed to open file '" << ofilename
00068 << "' in write mode.\n";
00069 return 1;
00070 }
00071
00072
00073 bool singleOldEventCnt = true;
00074 int oldEventCnt[nChs];
00075
00076 for(int iCh = 0; iCh<nChs; ++iCh){
00077 int evtcnt = 0;
00078
00079 while(evtcnt<nEvts && mem[iCh][evtcnt]!=0xFFFF){++evtcnt;}
00080
00081 oldEventCnt[iCh] = evtcnt;
00082 if(oldEventCnt[0]!=oldEventCnt[iCh]) singleOldEventCnt = false;
00083 if(evtcnt==0){
00084 cout << "Error: no data found for channel "<< iCh << "\n";
00085 }
00086
00087 for(int ievt = evtcnt; ievt<nEvts; ++ievt){
00088 if(mem[iCh][ievt]!=0xFFFF){
00089 cout << "Error: memory offset of channel " << iCh
00090 << " events are not contiguous.\n";
00091 exit(1);
00092 }
00093 mem[iCh][ievt] = mem[iCh][ievt%evtcnt];
00094 }
00095
00096 for(int ievt=0; ievt<nEvts; ++ievt){
00097 out << iCh+1 << "\t" << ievt
00098 << "\t" << hex << "0x" << setfill('0') << setw(4)
00099 << mem[iCh][ievt]
00100 << setfill(' ') << dec << "\t0"
00101 << "\n";
00102 }
00103 }
00104
00105
00106 if(singleOldEventCnt && (nEvts%oldEventCnt[0]!=0)){
00107 cout << "Warning: ouput event count (2048) is not a mulitple of input "
00108 "event counts\n" ;
00109 }
00110 if(!singleOldEventCnt){
00111 stringstream s;
00112 for(int iCh=0; iCh<nChs; ++iCh){
00113 if(nEvts%oldEventCnt[iCh]){
00114 s << (s.str().size()==0?"":", ") << iCh;
00115 }
00116 }
00117 if(s.str().size()!=0)
00118 cout << "Warning: ouput event count (2048) for channel"
00119 << (s.str().size()>1?"s":"") << " "
00120 << s.str()
00121 << " is not a mulitple of input event counts\n" ;
00122 }
00123
00124 if(!singleOldEventCnt){
00125 cout << "Info: in the input file the event count depends on the channel";
00126 }
00127 }
00128