Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-04 22:55:03

0001 #include <iostream>
0002 #include <fstream>
0003 #include <sstream>
0004 #include <cinttypes>
0005 #include <iomanip>
0006 #include <cstdlib>
0007 #include <cstdio>
0008 #include <cstring>
0009 using namespace std;
0010 
0011 const int nChs = 68;
0012 const int nEvts = 2048;
0013 uint16_t mem[nChs][nEvts];
0014 
0015 /** \file
0016  * The TCC memory for FE data emulation takes a fixed number, 2048, of events.
0017  * This standalone application completes a FE emulation data file with an
0018  * arbitrary number of events (<=2048) in order to have the required 2048
0019  * events. The N initial events are repeated till having 2048 events. In
0020  * general the number of initial events is choosen as a divider of 2048.
0021  */
0022 
0023 int main(int argc, char* argv[]) {
0024   if ((argc >= 2 && ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0))) || argc != 3) {
0025     cout << "Usage: recycleTccEmu infile outfile\n";
0026     return 1;
0027   }
0028 
0029   string ifilename = argv[1];
0030   string ofilename = argv[2];
0031 
0032   for (int iCh = 0; iCh < nChs; ++iCh) {
0033     for (int iEvts = 0; iEvts < nEvts; ++iEvts) {
0034       mem[iCh][iEvts] = 0xFFFF;
0035     }
0036   }
0037 
0038   ifstream in(ifilename.c_str());
0039   int chnb;
0040   int bcnb;
0041   int val;
0042   int dummy;
0043   int oldLineCnt = 0;
0044 
0045   //reads input file:
0046   if (in) {
0047     while (!in.eof()) {
0048       in >> dec >> chnb >> bcnb >> hex >> val >> dummy;
0049       mem[chnb - 1][bcnb] = val & 0x7FF;
0050       if (mem[chnb - 1][bcnb] != val) {
0051         cout << "Invalid Et value at line " << oldLineCnt + 1 << ".\n";
0052         exit(1);
0053       }
0054       // cout<<"Channel: "<< dec <<chnb <<", BX: "
0055       // << dec << bcnb << " filled with val:"<< hex<< mem[chnb-1][bcnb]
0056       // << dec << endl;
0057       ++oldLineCnt;
0058     }
0059   } else {
0060     cout << "Failed to open file " << ifilename << "\n";
0061   }
0062 
0063   in.close();
0064   ofstream out(ofilename.c_str());
0065 
0066   if (!out) {
0067     cout << "Failed to open file '" << ofilename << "' in write mode.\n";
0068     return 1;
0069   }
0070 
0071   bool singleOldEventCnt = true;
0072   int oldEventCnt[nChs];
0073   //fills output file:
0074   for (int iCh = 0; iCh < nChs; ++iCh) {
0075     int evtcnt = 0;
0076     //find first not initialized events:
0077     while (evtcnt < nEvts && mem[iCh][evtcnt] != 0xFFFF) {
0078       ++evtcnt;
0079     }
0080     //cout << "ch " << iCh << " event count: " << evtcnt << "\n";
0081     oldEventCnt[iCh] = evtcnt;
0082     if (oldEventCnt[0] != oldEventCnt[iCh])
0083       singleOldEventCnt = false;
0084     if (evtcnt == 0) {
0085       cout << "Error: no data found for channel " << iCh << "\n";
0086     }
0087     //clones data of channel iCh
0088     for (int ievt = evtcnt; ievt < nEvts; ++ievt) {
0089       if (mem[iCh][ievt] != 0xFFFF) {
0090         cout << "Error: memory offset of channel " << iCh << " events are not contiguous.\n";
0091         exit(1);
0092       }
0093       mem[iCh][ievt] = mem[iCh][ievt % evtcnt];
0094     }
0095 
0096     for (int ievt = 0; ievt < nEvts; ++ievt) {
0097       out << iCh + 1 << "\t" << ievt << "\t" << hex << "0x" << setfill('0') << setw(4) << mem[iCh][ievt] << setfill(' ')
0098           << dec << "\t0"
0099           << "\n";
0100     }
0101   }
0102 
0103   //warning for aperiodic case:
0104   if (singleOldEventCnt && (nEvts % oldEventCnt[0] != 0)) {
0105     cout << "Warning: ouput event count (2048) is not a mulitple of input "
0106             "event counts\n";
0107   }
0108   if (!singleOldEventCnt) {
0109     stringstream s;
0110     for (int iCh = 0; iCh < nChs; ++iCh) {
0111       if (nEvts % oldEventCnt[iCh]) {
0112         s << (s.str().empty() ? "" : ", ") << iCh;
0113       }
0114     }
0115     if (!s.str().empty())
0116       cout << "Warning: ouput event count (2048) for channel" << (s.str().size() > 1 ? "s" : "") << " " << s.str()
0117            << " is not a mulitple of input event counts\n";
0118   }
0119 
0120   if (!singleOldEventCnt) {
0121     cout << "Info: in the input file the event count depends on the channel";
0122   }
0123 }