CMS 3D CMS Logo

recycleTccEmu.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <sstream>
4 #include <cinttypes>
5 #include <iomanip>
6 #include <cstdlib>
7 #include <cstdio>
8 #include <cstring>
9 using namespace std;
10 
11 const int nChs = 68;
12 const int nEvts = 2048;
13 uint16_t mem[nChs][nEvts];
14 
23 int main(int argc, char* argv[]) {
24  if ((argc >= 2 && ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0))) || argc != 3) {
25  cout << "Usage: recycleTccEmu infile outfile\n";
26  return 1;
27  }
28 
29  string ifilename = argv[1];
30  string ofilename = argv[2];
31 
32  for (int iCh = 0; iCh < nChs; ++iCh) {
33  for (int iEvts = 0; iEvts < nEvts; ++iEvts) {
34  mem[iCh][iEvts] = 0xFFFF;
35  }
36  }
37 
38  ifstream in(ifilename.c_str());
39  int chnb;
40  int bcnb;
41  int val;
42  int dummy;
43  int oldLineCnt = 0;
44 
45  //reads input file:
46  if (in) {
47  while (!in.eof()) {
48  in >> dec >> chnb >> bcnb >> hex >> val >> dummy;
49  mem[chnb - 1][bcnb] = val & 0x7FF;
50  if (mem[chnb - 1][bcnb] != val) {
51  cout << "Invalid Et value at line " << oldLineCnt + 1 << ".\n";
52  exit(1);
53  }
54  // cout<<"Channel: "<< dec <<chnb <<", BX: "
55  // << dec << bcnb << " filled with val:"<< hex<< mem[chnb-1][bcnb]
56  // << dec << endl;
57  ++oldLineCnt;
58  }
59  } else {
60  cout << "Failed to open file " << ifilename << "\n";
61  }
62 
63  in.close();
64  ofstream out(ofilename.c_str());
65 
66  if (!out) {
67  cout << "Failed to open file '" << ofilename << "' in write mode.\n";
68  return 1;
69  }
70 
71  bool singleOldEventCnt = true;
72  int oldEventCnt[nChs];
73  //fills output file:
74  for (int iCh = 0; iCh < nChs; ++iCh) {
75  int evtcnt = 0;
76  //find first not initialized events:
77  while (evtcnt < nEvts && mem[iCh][evtcnt] != 0xFFFF) {
78  ++evtcnt;
79  }
80  //cout << "ch " << iCh << " event count: " << evtcnt << "\n";
81  oldEventCnt[iCh] = evtcnt;
82  if (oldEventCnt[0] != oldEventCnt[iCh])
83  singleOldEventCnt = false;
84  if (evtcnt == 0) {
85  cout << "Error: no data found for channel " << iCh << "\n";
86  }
87  //clones data of channel iCh
88  for (int ievt = evtcnt; ievt < nEvts; ++ievt) {
89  if (mem[iCh][ievt] != 0xFFFF) {
90  cout << "Error: memory offset of channel " << iCh << " events are not contiguous.\n";
91  exit(1);
92  }
93  mem[iCh][ievt] = mem[iCh][ievt % evtcnt];
94  }
95 
96  for (int ievt = 0; ievt < nEvts; ++ievt) {
97  out << iCh + 1 << "\t" << ievt << "\t" << hex << "0x" << setfill('0') << setw(4) << mem[iCh][ievt] << setfill(' ')
98  << dec << "\t0"
99  << "\n";
100  }
101  }
102 
103  //warning for aperiodic case:
104  if (singleOldEventCnt && (nEvts % oldEventCnt[0] != 0)) {
105  cout << "Warning: ouput event count (2048) is not a mulitple of input "
106  "event counts\n";
107  }
108  if (!singleOldEventCnt) {
109  stringstream s;
110  for (int iCh = 0; iCh < nChs; ++iCh) {
111  if (nEvts % oldEventCnt[iCh]) {
112  s << (s.str().empty() ? "" : ", ") << iCh;
113  }
114  }
115  if (!s.str().empty())
116  cout << "Warning: ouput event count (2048) for channel" << (s.str().size() > 1 ? "s" : "") << " " << s.str()
117  << " is not a mulitple of input event counts\n";
118  }
119 
120  if (!singleOldEventCnt) {
121  cout << "Info: in the input file the event count depends on the channel";
122  }
123 }
int main(int argc, char *argv[])
const int nChs
uint16_t mem[nChs][nEvts]
const int nEvts
def exit(msg="")