CMS 3D CMS Logo

GenABIO.cc
Go to the documentation of this file.
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 8; -*-
2 
10 #include <cassert>
11 #include <cstdlib>
12 #include <cstring>
13 #include <fstream>
14 #include <iostream>
15 #include <sstream>
16 #include <vector>
17 
18 #include "ecalDccMap.h"
19 
20 #if !defined(__linux__) && !(defined(__APPLE__) && __DARWIN_C_LEVEL >= 200809L)
21 #include <errno.h>
22 /* getline implementation is copied from glibc. */
23 
24 #ifndef SIZE_MAX
25 #define SIZE_MAX ((size_t) - 1)
26 #endif
27 #ifndef SSIZE_MAX
28 #define SSIZE_MAX ((ssize_t)(SIZE_MAX / 2))
29 #endif
30 namespace {
31  ssize_t getline(char **lineptr, size_t *n, FILE *fp) {
32  ssize_t result;
33  size_t cur_len = 0;
34 
35  if (lineptr == NULL || n == NULL || fp == NULL) {
36  errno = EINVAL;
37  return -1;
38  }
39 
40  if (*lineptr == NULL || *n == 0) {
41  *n = 120;
42  *lineptr = (char *)malloc(*n);
43  if (*lineptr == NULL) {
44  result = -1;
45  goto end;
46  }
47  }
48 
49  for (;;) {
50  int i;
51 
52  i = getc(fp);
53  if (i == EOF) {
54  result = -1;
55  break;
56  }
57 
58  /* Make enough space for len+1 (for final NUL) bytes. */
59  if (cur_len + 1 >= *n) {
60  size_t needed_max = SSIZE_MAX < SIZE_MAX ? (size_t)SSIZE_MAX + 1 : SIZE_MAX;
61  size_t needed = 2 * *n + 1; /* Be generous. */
62  char *new_lineptr;
63 
64  if (needed_max < needed)
65  needed = needed_max;
66  if (cur_len + 1 >= needed) {
67  result = -1;
68  goto end;
69  }
70 
71  new_lineptr = (char *)realloc(*lineptr, needed);
72  if (new_lineptr == NULL) {
73  result = -1;
74  goto end;
75  }
76 
77  *lineptr = new_lineptr;
78  *n = needed;
79  }
80 
81  (*lineptr)[cur_len] = i;
82  cur_len++;
83 
84  if (i == '\n')
85  break;
86  }
87  (*lineptr)[cur_len] = '\0';
88  result = cur_len ? (ssize_t)cur_len : result;
89 
90  end:
91  return result;
92  }
93 } // namespace
94 #endif
95 
96 using namespace std;
97 
100 const static int nEndcapXBins = 100;
103 const static int nEndcapYBins = 100;
106 const static int supercrystalEdge = 5;
115 const static int nEndcaps = 2;
118 const static int nEndcapTTInEta = 11;
121 const static int nBarrelTTInEta = 34;
124 const static int nTTInEta = 2 * nEndcapTTInEta + nBarrelTTInEta;
127 const static int nTTInPhi = 72;
130 const int nABInEta = 4;
133 const int nABInPhi = 3;
136 const int nDCCEE = 9;
137 const int nABABCh = 8; // nbr of AB input/output ch. on an AB
138 const int nABTCCCh = 12; // nbr of TCC inputs on an AB
139 const int nDCCCh = 12; // nbr of DCC outputs on an AB
140 const int nTCCInEta = 6; // nbr of TCC bins along eta
141 const int nAB = nABInPhi * nABInEta;
143 const int iTTEtaMin[nABInEta] = {0, 11, 28, 45};
144 const int iTTEtaMax[nABInEta] = {10, 27, 44, 55};
145 const int iTTEtaSign[nABInEta] = {-1, -1, 1, 1};
146 
147 // Eta bounds for TCC partionning
148 // a TCC covers from iTCCEtaBounds[iTCCEta] included to
149 // iTCCEtaBounds[iTCCEta+1] excluded.
150 const int iTCCEtaBounds[nTCCInEta + 1] = {0, 7, 11, 28, 45, 49, 56};
151 
152 const char *abTTFFilePrefix = "TTF_AB";
153 const char *abTTFFilePostfix = ".txt";
154 const char *abSRFFilePrefix = "AF_AB";
155 const char *abSRFFilePostfix = ".txt";
156 const char *abIOFilePrefix = "IO_AB";
157 const char *abIOFilePostfix = ".txt";
158 
159 const char *srfFilename = "SRF.txt";
160 const char *ttfFilename = "TTF.txt";
161 const char *xconnectFilename = "xconnect_universal.txt";
162 
163 const char srpFlagMarker[] = {'.', 'S', 'N', 'C', '4', '5', '6', '7'};
164 const char tccFlagMarker[] = {'.', 'S', '?', 'C', '4', '5', '6', '7'};
165 
166 char srp2roFlags[128];
167 
168 typedef enum { suppress = 0, sr2, sr1, full, fsuppress, fsr2, fsr1, ffull } roAction_t;
169 char roFlagMarker[] = {
170  /*suppress*/ '.',
171  /*sr1*/ 'z',
172  /*sr1*/ 'Z',
173  /*full*/ 'F',
174  /*fsuppress*/ '4',
175  /*fsr2*/ '5',
176  /*fsr1*/ '6',
177  /*ffull*/ '7'};
178 
179 const int nactions = 8;
180 // can be overwritten according by cmd line arguments
182  /*LI->*/ sr2,
183  /*S->*/ full,
184  /*N->*/ full,
185  /*C->*/ full,
186  /*fLI->*/ sr2,
187  /*fS->*/ sr2,
188  /*fN->*/ sr2,
189  /*fC->*/ sr2};
190 
191 // list of SC deserves by an endcap DCC [0(EE-)|1(EE+)][iDCCPhi]
192 vector<pair<int, int>> ecalDccSC[nEndcaps][nDCCEE];
193 
194 void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi], ofstream files[]);
195 void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
196  const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
197  ofstream files[]);
198 void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi],
199  const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
200  const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
201  ofstream files[]);
202 inline int abNum(int iABEta, int iABPhi) { return 3 * iABEta + iABPhi; }
203 
204 bool readTTF(FILE *file, char ttFlags[nTTInEta][nTTInPhi]);
205 bool readSRF(FILE *file,
206  char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
207  char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins]);
208 
209 void writeABTTFFileHeader(ofstream &f, int abNum);
210 void writeABSRFFileHeader(ofstream &f, int abNum);
211 void writeABIOFileHeader(ofstream &f, int abNum);
212 string getFlagStream(char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax, int iPhiMin, int iPhiMax);
213 string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iTCCCh);
214 void getABTTPhiBounds(int iABPhi, int &iTTPhiMin, int &iTTPhiMax);
215 string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh);
216 string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh);
217 string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
218  const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
219  int iABEta,
220  int iABPhi,
221  int DCCCh);
222 void abConnect(int iAB, int iABCh, int &iOtherAB, int &iOtherABCh);
223 
224 int iEvent = 0;
225 
226 int theAB = -1;
227 
228 int main(int argc, char *argv[]) {
229  char barrelSrFlags[nBarrelTTInEta][nTTInPhi];
230  char endcapSrFlags[nEndcaps][nEndcapXBins / 5][nEndcapYBins / 5];
231  char ttFlags[nTTInEta][nTTInPhi];
232  ofstream abTTFFiles[nAB];
233  ofstream abSRFFiles[nAB];
234  ofstream abIOFiles[nAB];
235 
236  int iarg = 0;
237  while (++iarg < argc) {
238  if (strcmp(argv[iarg], "-h") == 0 || strcmp(argv[iarg], "--help") == 0) {
239  cout << "Usage: GenABIO [OPTIONS]\n\n"
240  "Produces TT and SR flag files for each SRP board from TTF.txt "
241  "and "
242  "SRF.txt global flag files. Requires the SRP cross-connect "
243  "description"
244  " description file (xconnect_universal.txt). TTF.txt, SRF.txt and "
245  "xconnect_universal.txt must be in the directory the command is "
246  "launched.\n\n"
247  "OPTIONS:\n"
248  " -A, --actions IJKLMNOP. IJKLMNOP I..P integers from 0 to 7.\n"
249  " I: action flag for low interest RUs\n"
250  " J: action flag for single RUs\n"
251  " K: action flag for neighbour RUs\n"
252  " L: action flag for centers RUs\n"
253  " M: action flag for forced low interest RUs\n"
254  " N: action flag for forced single RUs\n"
255  " O: action flag for forced neighbour RUs\n"
256  " P: action flag for forced centers RUs\n\n"
257  " -h, --help display this help\n"
258  " -a n, --ab n specifies indices of the AB whose file must be "
259  "produced. The ab number runs from 1 to 12. Use -1 to produce "
260  "files "
261  "for every AB\n\n";
262 
263  return 0;
264  }
265 
266  if (!strcmp(argv[iarg], "-A") || !strcmp(argv[iarg], "-A")) { // actions
267  if (++iarg >= argc) {
268  cout << "Option error. Try -h\n";
269  return 1;
270  }
271  for (int i = 0; i < 8; ++i) {
272  int act = argv[iarg][i] - '0';
273  if (act < 0 || act >= nactions) {
274  cout << "Error. Action argument is invalid.\n";
275  return 1;
276  } else {
277  actions[i] = (roAction_t)act;
278  }
279  }
280  continue;
281  }
282  if (!strcmp(argv[iarg], "-a") || !strcmp(argv[iarg], "--ab")) {
283  if (++iarg >= argc) {
284  cout << "Option error. Try -h\n";
285  return 1;
286  }
287  theAB = strtoul(argv[iarg], nullptr, 0);
288  if (theAB >= 0)
289  --theAB;
290  if (theAB < -1 || theAB > 11) {
291  cout << "AB number is incorrect. Try -h option to get help.\n";
292  }
293  continue;
294  }
295  }
296 
297  for (size_t i = 0; i < sizeof(srp2roFlags) / sizeof(srp2roFlags[0]); srp2roFlags[i++] = '?')
298  ;
299  for (size_t i = 0; i < sizeof(actions) / sizeof(actions[0]); ++i) {
301  }
302 
303  for (int iEE = 0; iEE < nEndcaps; ++iEE) {
304  for (int iY = 0; iY < nSupercrystalXBins; ++iY) {
305  for (int iX = 0; iX < nSupercrystalYBins; ++iX) {
306  int iDCCPhi = dccPhiIndexOfRU(iEE == 0 ? 0 : 2, iX, iY);
307  if (iDCCPhi >= 0) { // SC exists
308  ecalDccSC[iEE][iDCCPhi].push_back(pair<int, int>(iX, iY));
309  }
310  }
311  }
312  }
313 
314  stringstream s;
315  for (int iAB = 0; iAB < nAB; ++iAB) {
316  if (theAB != -1 && theAB != iAB)
317  continue;
318  s.str("");
319  s << abTTFFilePrefix << (iAB < 9 ? "0" : "") << iAB + 1 << abTTFFilePostfix;
320  abTTFFiles[iAB].open(s.str().c_str(), ios::out);
321  writeABTTFFileHeader(abTTFFiles[iAB], iAB);
322  s.str("");
323  s << abSRFFilePrefix << (iAB < 9 ? "0" : "") << iAB + 1 << abSRFFilePostfix;
324  abSRFFiles[iAB].open(s.str().c_str(), ios::out);
325  writeABSRFFileHeader(abSRFFiles[iAB], iAB);
326  s.str("");
327  s << abIOFilePrefix << (iAB < 9 ? "0" : "") << iAB + 1 << abIOFilePostfix;
328  abIOFiles[iAB].open(s.str().c_str(), ios::out);
329  writeABIOFileHeader(abIOFiles[iAB], iAB);
330  }
331 
332  FILE *srfFile = fopen(srfFilename, "r");
333  if (srfFile == nullptr) {
334  cerr << "Failed to open SRF file, " << srfFilename << endl;
335  exit(EXIT_FAILURE);
336  }
337 
338  FILE *ttfFile = fopen(ttfFilename, "r");
339  if (ttfFile == nullptr) {
340  cerr << "Failed to open TTF file, " << ttfFilename << endl;
341  exit(EXIT_FAILURE);
342  }
343 
344  iEvent = 0;
345  while (readSRF(srfFile, barrelSrFlags, endcapSrFlags) && readTTF(ttfFile, ttFlags)) {
346  if (iEvent % 100 == 0) {
347  cout << "Event " << iEvent << endl;
348  }
349  fillABTTFFiles(ttFlags, abTTFFiles);
350  fillABSRPFiles(barrelSrFlags, endcapSrFlags, abSRFFiles);
351  fillABIOFiles(ttFlags, barrelSrFlags, endcapSrFlags, abIOFiles);
352  ++iEvent;
353  }
354 
355  return 0;
356 }
357 
361 void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi], ofstream files[]) {
362  for (int iABEta = 0; iABEta < nABInEta; ++iABEta) {
363  for (int iABPhi = 0; iABPhi < nABInPhi; ++iABPhi) {
364  int iAB = abNum(iABEta, iABPhi);
365  int iTTPhiMin;
366  int iTTPhiMax;
367  getABTTPhiBounds(iABPhi, iTTPhiMin, iTTPhiMax);
368  // writeEventHeader(files[iAB], iEvent, nTTInABAlongPhi);
369  files[iAB] << "# Event " << iEvent << "\n";
370 
371  for (int i = 0; i <= iTTEtaMax[iABEta] - iTTEtaMin[iABEta]; ++i) {
372  int iTTEta;
373  if (iTTEtaSign[iABEta] > 0) {
374  iTTEta = iTTEtaMin[iABEta] + i;
375  } else {
376  iTTEta = iTTEtaMax[iABEta] - i;
377  }
378  for (int iTTPhi = iTTPhiMin; mod(iTTPhiMax - iTTPhi, nTTInPhi) < nTTInABAlongPhi;
379  iTTPhi = mod(++iTTPhi, nTTInPhi)) {
380  files[iAB] << ttFlags[iTTEta][iTTPhi];
381  }
382  files[iAB] << "\n";
383  }
384  files[iAB] << "#\n";
385  // writeEventTrailer(files[iAB], nTTInABAlongPhi);
386  }
387  }
388 }
389 
390 void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
391  const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
392  ofstream files[]) {
393  // event headers:
394  for (int iAB = 0; iAB < nAB; ++iAB) {
395  files[iAB] << "# Event " << iEvent << "\n";
396  }
397 
398  bool lineAppended[nAB];
399  for (int i = 0; i < nAB; lineAppended[i++] = false) /*empty*/
400  ;
401 
402  // EE:
403  for (int iEE = 0; iEE < nEndcaps; ++iEE) {
404  for (int iX = 0; iX < nSupercrystalXBins; ++iX) {
405  for (int iY = 0; iY < nSupercrystalYBins; ++iY) {
406  // int iDCC = dccIndex(iEE==0?0:2,iX*5,iY*5);
407  int iDCC = dccIndexOfRU(iEE == 0 ? 0 : 2, iX, iY);
408  if (iDCC >= 0) {
409  int iAB = abOfDcc(iDCC);
410  if (!lineAppended[iAB]) {
411  for (int i = 0; i < iY; ++i)
412  files[iAB] << ' ';
413  }
414  files[iAB] << srp2roFlags[(int)endcapSrFlags[iEE][iX][iY]];
415  lineAppended[iAB] = true;
416  }
417  } // next iY
418  for (int iFile = 0; iFile < nAB; ++iFile) {
419  if (lineAppended[iFile]) {
420  files[iFile] << "\n";
421  lineAppended[iFile] = false;
422  }
423  }
424  } // next iX
425  }
426 
427  // EB:
428  for (int iABEta = 1; iABEta < 3; ++iABEta) {
429  for (int iABPhi = 0; iABPhi < nABInPhi; ++iABPhi) {
430  int iAB = abNum(iABEta, iABPhi);
431  int iTTPhiMin;
432  int iTTPhiMax;
433  getABTTPhiBounds(iABPhi, iTTPhiMin, iTTPhiMax);
434  // writeEventHeader(files[iAB], iEvent, nTTInABAlongPhi);
435  for (int i = 0; i <= iTTEtaMax[iABEta] - iTTEtaMin[iABEta]; ++i) {
436  int iTTEta;
437  if (iTTEtaSign[iABEta] > 0) {
438  iTTEta = iTTEtaMin[iABEta] + i;
439  } else {
440  iTTEta = iTTEtaMax[iABEta] - i;
441  }
442  for (int iTTPhi = iTTPhiMin; mod(iTTPhiMax - iTTPhi, nTTInPhi) < nTTInABAlongPhi;
443  iTTPhi = mod(++iTTPhi, nTTInPhi)) {
444  files[iAB] << srp2roFlags[(int)barrelSrFlags[iTTEta - nEndcapTTInEta][iTTPhi]];
445  }
446  files[iAB] << "\n";
447  }
448  // writeEventTrailer(files[iAB], nTTInABAlongPhi);
449  files[iAB] << "#\n";
450  }
451  }
452 
453  // file trailers
454  for (int iAB = 0; iAB < nAB; ++iAB) {
455  files[iAB] << "#\n";
456  }
457 }
458 
459 void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi],
460  const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
461  const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
462  ofstream files[]) {
463  for (int iABEta = 0; iABEta < nABInEta; ++iABEta) {
464  for (int iABPhi = 0; iABPhi < nABInPhi; ++iABPhi) {
465  int iAB = abNum(iABEta, iABPhi);
466  // writeABIOFileHeader(files[iAB], iAB);
467  files[iAB] << "# Event " << iEvent << "\n";
468  // TCC inputs:
469  for (int iTCC = 0; iTCC < nABTCCCh; ++iTCC) {
470  files[iAB] << "ITCC" << iTCC + 1 << ":" << getABTCCInputStream(ttFlags, iABEta, iABPhi, iTCC) << "\n";
471  }
472  // AB inputs:
473  for (int iABCh = 0; iABCh < nABABCh; ++iABCh) {
474  files[iAB] << "IAB" << iABCh + 1 << ":" << getABABInputStream(ttFlags, iABEta, iABPhi, iABCh) << "\n";
475  }
476  // AB outputs:
477  for (int iABCh = 0; iABCh < nABABCh; ++iABCh) {
478  files[iAB] << "OAB" << iABCh + 1 << ":" << getABABOutputStream(ttFlags, iABEta, iABPhi, iABCh) << "\n";
479  }
480  // DCC output:
481  for (int iDCCCh = 0; iDCCCh < nDCCCh; ++iDCCCh) {
482  files[iAB] << "ODCC";
483  files[iAB] << (iDCCCh <= 8 ? "0" : "") << iDCCCh + 1 << ":"
484  << getABDCCOutputStream(barrelSrFlags, endcapSrFlags, iABEta, iABPhi, iDCCCh) << "\n";
485  }
486  files[iAB] << "#\n";
487  }
488  }
489 }
490 
491 /*
492  stringstream filename;
493  filename.str("");
494  filename << abTTFFilePrefix << abNum(iABEta, iABPhi) <<abTTFFilePostfix;
495  ofstream file(filename.str(), ios::ate);
496 
497 */
498 
499 bool readTTF(FILE *f, char ttFlags[nTTInEta][nTTInPhi]) {
500  char *buffer = nullptr;
501  size_t bufferSize = 0;
502  int read;
503  if (f == nullptr)
504  exit(EXIT_FAILURE);
505  int line = 0;
506  int iEta = 0;
507  while (iEta < nTTInEta && (read = getline(&buffer, &bufferSize, f)) != -1) {
508  ++line;
509  char *pos = buffer;
510  while (*pos == ' ' || *pos == '\t')
511  ++pos; // skip spaces
512  if (*pos != '#' && *pos != '\n') { // not a comment line nor an empty line
513  if (read - 1 != nTTInPhi) {
514  cerr << "Error: line " << line << " of file " << ttfFilename
515  << " has incorrect length"
516  // << " (" << read-1 << " instead of " << nTTInPhi <<
517  // ")"
518  << endl;
519  exit(EXIT_FAILURE);
520  }
521  for (int iPhi = 0; iPhi < nTTInPhi; ++iPhi) {
522  ttFlags[iEta][iPhi] = buffer[iPhi];
523  // if(ttFlags[iEta][iPhi]!='.'){
524  // cout << __FILE__ << ":" << __LINE__ << ": "
525  // << iEta << "," << iPhi
526  // << " " << ttFlags[iEta][iPhi] << "\n";
527  // }
528  }
529  ++iEta;
530  }
531  }
532  // returns true if all TT were read (not at end of file)
533  return (iEta == nTTInEta) ? true : false;
534 }
535 
536 bool readSRF(FILE *f,
537  char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
538  char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins]) {
539  char *buffer = nullptr;
540  size_t bufferSize = 0;
541  int read;
542  if (f == nullptr)
543  exit(EXIT_FAILURE);
544  int line = 0;
545  int iEta = 0;
546  int iXm = 0;
547  int iXp = 0;
548  int iReadLine = 0; // number of read line, comment lines excluded
549  // number of non-comment lines to read:
550  const int nReadLine = nBarrelTTInEta + nEndcaps * nSupercrystalXBins;
551  while (iReadLine < nReadLine && (read = getline(&buffer, &bufferSize, f)) != -1) {
552  ++line;
553  char *pos = buffer;
554  while (*pos == ' ' || *pos == '\t')
555  ++pos; // skip spaces
556  if (*pos != '#' && *pos != '\n') { // not a comment line nor an empty line
557  if (iReadLine < nSupercrystalXBins) { // EE- reading
558  if (read - 1 != nSupercrystalYBins) {
559  cerr << "Error: line " << line << " of file " << srfFilename << " has incorrect length"
560  << " (" << read - 1 << " instead of " << nSupercrystalYBins << ")" << endl;
561  exit(EXIT_FAILURE);
562  }
563  for (int iY = 0; iY < nSupercrystalYBins; ++iY) {
564  endcapSrFlags[0][iXm][iY] = buffer[iY];
565  }
566  ++iXm;
567  } else if (iReadLine < nSupercrystalYBins + nBarrelTTInEta) { // EB
568  // reading
569  if (read - 1 != nTTInPhi) {
570  cerr << "Error: line " << line << " of file " << srfFilename << " has incorrect length"
571  << " (" << read - 1 << " instead of " << nTTInPhi << ")" << endl;
572  exit(EXIT_FAILURE);
573  }
574  for (int iPhi = 0; iPhi < nTTInPhi; ++iPhi) {
575  barrelSrFlags[iEta][iPhi] = buffer[iPhi];
576  }
577  ++iEta;
578  } else if (iReadLine < 2 * nSupercrystalXBins + nBarrelTTInEta) { // EE+ reading
579  if (read - 1 != nSupercrystalYBins) {
580  cerr << "Error: line " << line << " of file " << srfFilename << " has incorrect length"
581  << " (" << read - 1 << " instead of " << nSupercrystalYBins << ")" << endl;
582  exit(EXIT_FAILURE);
583  }
584  for (int iY = 0; iY < nSupercrystalYBins; ++iY) {
585  endcapSrFlags[1][iXp][iY] = buffer[iY];
586  }
587  ++iXp;
588  }
589  ++iReadLine;
590  } // not a comment or empty line
591  }
592  // returns 0 if all TT were read:
593  return (iReadLine == nReadLine) ? true : false;
594 }
595 
596 // void writeEventHeader(ofstream& f, int iEvent, int nPhi){
597 // //event header:
598 // stringstream header;
599 // header.str("");
600 // header << " event " << iEvent << " ";
601 // f << "+";
602 // for(int iPhi = 0; iPhi < nPhi; ++iPhi){
603 // if(iPhi == (int)(nPhi-header.str().size())/2){
604 // f << header.str();
605 // iPhi += header.str().size()-1;
606 // } else{
607 // f << "-";
608 // }
609 // }
610 // f << "+\n";
611 // }
612 
613 // void writeEventTrailer(ofstream& f, int nPhi){
614 // f << "+";
615 // for(int iPhi = 0; iPhi < nPhi; ++iPhi) f << "-";
616 // f << "+\n";
617 // }
618 
619 void writeABTTFFileHeader(ofstream &f, int abNum) {
620  time_t t;
621  time(&t);
622  const char *date = ctime(&t);
623  f << "# TTF flag map covered by AB " << abNum + 1
624  << "\n#\n"
625  "# Generated on : "
626  << date
627  << "#\n"
628  "# +---> Phi "
629  << srpFlagMarker[0]
630  << ": 000 (low interest)\n"
631  "# | "
632  << srpFlagMarker[1]
633  << ": 001 (single)\n"
634  "# | "
635  << srpFlagMarker[2]
636  << ": 010 (neighbour)\n"
637  "# V |Eta| "
638  << srpFlagMarker[3]
639  << ": 011 (center)\n"
640  "#\n";
641 }
642 
643 void writeABSRFFileHeader(ofstream &f, int abNum) {
644  time_t t;
645  time(&t);
646  const char *date = ctime(&t);
647  const char *xLabel;
648  const char *yLabel;
649  if (abNum < 3 || abNum > 8) { // endcap
650  xLabel = "Y ";
651  yLabel = "X ";
652  } else { // barrel
653  xLabel = "Phi";
654  yLabel = "|Eta|";
655  }
656  f << "# SRF flag map covered by AB " << abNum + 1
657  << "\n#\n"
658  "# Generated on : "
659  << date
660  << "#\n"
661  "# +---> "
662  << xLabel << " " << roFlagMarker[0]
663  << ": 000 (suppress)\n"
664  "# | "
665  << roFlagMarker[1]
666  << ": 010 (SR Threshold 2)\n"
667  "# | "
668  << roFlagMarker[2]
669  << ": 001 (SR Threshold 1)\n"
670  "# V "
671  << yLabel << " " << roFlagMarker[3]
672  << ": 011 (Full readout)\n"
673  "#\n"
674  "# action table (when forced):\n"
675  "# LI-> "
676  << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]] << ")"
677  << "\n"
678  "# S -> "
679  << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]] << ")"
680  << "\n"
681  "# N -> "
682  << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]] << ")"
683  << "\n"
684  "# C -> "
685  << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]] << ")"
686  << "\n";
687 }
688 
689 void writeABIOFileHeader(ofstream &f, int abNum) {
690  time_t t;
691  time(&t);
692  const char *date = ctime(&t);
693  f << "# AB " << abNum + 1
694  << " I/O \n#\n"
695  "# Generated on : "
696  << date
697  << "#\n"
698  "# "
699  << srpFlagMarker[0] << ": 000 (low interest) " << tccFlagMarker[0] << ": 000 (low interest) " << roFlagMarker[0]
700  << ": 000 (suppress)\n"
701  "# "
702  << srpFlagMarker[1] << ": 001 (single) " << tccFlagMarker[1] << ": 001 (mid interest) " << roFlagMarker[1]
703  << ": 010 (SR Threshold 2)\n"
704  "# "
705  << srpFlagMarker[2] << ": 010 (neighbour) " << tccFlagMarker[2] << ": 010 (not valid) " << roFlagMarker[2]
706  << ": 001 (SR Threshold 1)\n"
707  "# "
708  << srpFlagMarker[3] << ": 011 (center) " << tccFlagMarker[3] << ": 011 (high interest) " << roFlagMarker[3]
709  << ": 011 (Full readout)\n"
710  "#\n"
711  "# action table (when forced):\n"
712  "# LI-> "
713  << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]] << ")"
714  << "\n"
715  "# S -> "
716  << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]] << ")"
717  << "\n"
718  "# N -> "
719  << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]] << ")"
720  << "\n"
721  "# C -> "
722  << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]] << ")"
723  << "\n"
724  "#\n";
725 }
726 
727 string getFlagStream(const char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax, int iPhiMin, int iPhiMax) {
728  assert(0 <= iEtaMin && iEtaMin <= iEtaMax && iEtaMax < nTTInEta);
729  if (iEtaMin <= nTTInEta / 2 && iEtaMax > nTTInEta) {
730  cerr << "Implementation Errror:" << __FILE__ << ":" << __LINE__
731  << ": A flag stream cannot covers parts of both half-ECAL!" << endl;
732  exit(EXIT_FAILURE);
733  }
734 
735  bool zPos = (iEtaMin >= nTTInEta / 2);
736 
737  stringstream buffer;
738  buffer.str("");
739  for (int jEta = 0; jEta <= iEtaMax - iEtaMin; ++jEta) {
740  // loops on iEta in |eta| increasing order:
741  int iEta;
742  if (zPos) {
743  iEta = iEtaMin + jEta;
744  } else {
745  iEta = iEtaMax - jEta;
746  }
747 
748  for (int iPhi = mod(iPhiMin, nTTInPhi); mod(iPhiMax + 1 - iPhi, nTTInPhi) != 0; iPhi = mod(++iPhi, nTTInPhi)) {
749  buffer << flags[iEta][iPhi];
750  }
751  }
752 
753  return buffer.str();
754 }
755 
756 string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iTCCCh) {
757  // gets eta bounds for this tcc channel:
758  int iTCCEta;
759  if (iABEta == 1 || iABEta == 2) { // barrel
760  if (iTCCCh > 5)
761  return ""; // only 6 TCCs per AB for barrel
762  iTCCEta = 1 + iABEta;
763  } else { // endcap
764  if (iABEta == 0) { // EE-
765  iTCCEta = (iTCCCh < 6) ? 1 : 0;
766  } else { // EE+
767  iTCCEta = (iTCCCh < 6) ? 4 : 5;
768  }
769  }
770  int iEtaMin = iTCCEtaBounds[iTCCEta];
771  int iEtaMax = iTCCEtaBounds[iTCCEta + 1] - 1;
772 
773  // gets phi bounds:
774  int iPhiMin;
775  int iPhiMax;
776  getABTTPhiBounds(iABPhi, iPhiMin, iPhiMax);
777  // phi is increasing with TTC channel number
778  // a TTC covers a 4TT-wide phi-sector
779  //=>iPhiMin(iTTCCh) = iPhiMin(AB) + 4*iTTCCh for iTCCCh<6
780  iPhiMin += 4 * (iTCCCh % 6);
781  iPhiMax = iPhiMin + 4 - 1;
782 
783  return getFlagStream(tccFlags, iEtaMin, iEtaMax, iPhiMin, iPhiMax);
784 }
785 
786 string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh) {
787  stringstream buffer;
788  buffer.str("");
789  bool barrel = (iABEta == 1 || iABEta == 2); // true for barrel, false for endcap
790  switch (iABCh) {
791  case 0:
792  // to AB ch #0 are sent the 16 1st TCC flags received on TCC input Ch. 0
793  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0).substr(0, 16);
794  break;
795  case 1:
796  // to AB ch #1 are sent the 16 1st TCC flags received on TCC input Ch. 0 to
797  // 5:
798  for (int iTCCCh = 0; iTCCCh < 6; ++iTCCCh) {
799  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh).substr(0, 16);
800  }
801  break;
802  case 2:
803  // to AB ch #2 are sent the 16 1st TCC flags received on TCC input Ch. 5:
804  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5).substr(0, 16);
805  break;
806  case 3:
807  // to AB ch #3 are sent TCC flags received on TCC input Ch. 0 and 6:
808  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
809  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 6);
810  break;
811  case 4:
812  // to AB ch #4 are sent TCC flags received on TCC input Ch 5 and 11:
813  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
814  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 11);
815  break;
816  case 5:
817  // for endcaps AB output ch 5 is not used.
818  // for barrel, to AB ch #5 are sent the 16 last TCC flags received on TCC
819  // input Ch. 0:
820  if (barrel) { // in barrel
821  string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
822  assert(s.size() >= 16);
823  buffer << s.substr(s.size() - 16, 16);
824  }
825  break;
826  case 6:
827  // for endcaps AB output ch 6 is not used.
828  // for barrel, to AB ch #6 are sent the 16 last TCC flags received on TCC
829  // input Ch. 0 to 5:
830  if (barrel) { // in barrel
831  for (int iTCCCh = 0; iTCCCh < 6; ++iTCCCh) {
832  string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh);
833  buffer << s.substr(s.size() - 16, 16);
834  }
835  }
836  break;
837  case 7:
838  // for endcaps AB output ch 7 is not used.
839  // for barrel, to AB ch #7 are sent the 16 last TCC flags received on TCC
840  // input Ch. 5:
841  if (barrel) { // in barrel
842  string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
843  assert(s.size() >= 16);
844  buffer << s.substr(s.size() - 16, 16);
845  }
846  break;
847  default:
848  assert(false);
849  }
850  return buffer.str();
851 }
852 
853 string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh) {
854  int iAB = abNum(iABEta, iABPhi);
855  int iOtherAB; // AB which this channel is connected to
856  int iOtherABCh; // ch # on the other side of the AB-AB link
857  abConnect(iAB, iABCh, iOtherAB, iOtherABCh);
858  int iOtherABEta = iOtherAB / 3;
859  int iOtherABPhi = iOtherAB % 3;
860  return getABABOutputStream(tccFlags, iOtherABEta, iOtherABPhi, iOtherABCh);
861 }
862 
863 void getABTTPhiBounds(int iABPhi, int &iTTPhiMin, int &iTTPhiMax) {
864  iTTPhiMin = mod(-6 + iABPhi * nTTInABAlongPhi, nTTInPhi);
865  iTTPhiMax = mod(iTTPhiMin + nTTInABAlongPhi - 1, nTTInPhi);
866 }
867 
868 void abConnect(int iAB, int iABCh, int &iOtherAB, int &iOtherABCh) {
869  static bool firstCall = true;
870  static int xconnectMap[nAB][nABABCh][2];
871  if (firstCall) {
872  FILE *f = fopen(xconnectFilename, "r");
873  if (f == nullptr) {
874  cerr << "Error. Failed to open xconnect definition file," << xconnectFilename << endl;
875  exit(EXIT_FAILURE);
876  }
877  // skips two first lines:
878  for (int i = 0; i < 2; ++i) {
879  int c;
880  while ((c = getc(f)) != '\n' && c >= 0)
881  ;
882  }
883  int ilink = 0;
884  while (!feof(f)) {
885  int abIn;
886  int pinIn;
887  int abOut;
888  int pinOut;
889  if (4 == fscanf(f, "%d\t%d\t%d\t%d", &abIn, &pinIn, &abOut, &pinOut)) {
890  xconnectMap[abIn][pinIn][0] = abOut;
891  xconnectMap[abIn][pinIn][1] = pinOut;
892  ++ilink;
893  }
894  }
895  if (ilink != nAB * nABABCh) {
896  cerr << "Error cross-connect definition file " << xconnectFilename
897  << " contains an unexpected number of link definition." << endl;
898  exit(EXIT_FAILURE);
899  }
900  firstCall = false;
901  }
902 
903  iOtherAB = xconnectMap[iAB][iABCh][0];
904  iOtherABCh = xconnectMap[iAB][iABCh][1];
905 }
906 
907 string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
908  const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
909  int iABEta,
910  int iABPhi,
911  int iDCCCh) {
912  bool barrel = (iABEta == 1 || iABEta == 2);
913  if (barrel) {
914  // same as TCC with same ch number but with TCC flags replaced by SRP flags:
915  string stream = getABTCCInputStream(barrelSrFlags - nEndcapTTInEta, iABEta, iABPhi, iDCCCh);
916  // converts srp flags to readout flags:
917  for (size_t i = 0; i < stream.size(); ++i) {
918  stream[i] = srp2roFlags[(int)stream[i]];
919  }
920  return stream;
921  } else { // endcap
922  if (iDCCCh < 3) { // used DCC output channel
923  // endcap index:
924  int iEE = (iABEta == 0) ? 0 : 1;
925  stringstream buffer("");
926  // 3 DCC per AB and AB DCC output channel in
927  // increasing DCC phi position:
928  int iDCCPhi = iABPhi * 3 + iDCCCh;
929  for (size_t iSC = 0; iSC < ecalDccSC[iEE][iDCCPhi].size(); ++iSC) {
930  pair<int, int> sc = ecalDccSC[iEE][iDCCPhi][iSC];
931  buffer << srp2roFlags[(int)endcapSrFlags[iEE][sc.first][sc.second]];
932  }
933  return buffer.str();
934  } else { // unused output channel
935  return "";
936  }
937  }
938 }
const int iTTEtaMin[nABInEta]
Definition: GenABIO.cc:143
bool readTTF(FILE *file, char ttFlags[nTTInEta][nTTInPhi])
Definition: GenABIO.cc:499
Definition: GenABIO.cc:168
Definition: GenABIO.cc:168
static const int nEndcapYBins
Definition: GenABIO.cc:103
void writeABSRFFileHeader(ofstream &f, int abNum)
Definition: GenABIO.cc:643
roAction_t actions[nactions]
Definition: GenABIO.cc:181
string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh)
Definition: GenABIO.cc:853
static const int nSupercrystalXBins
Definition: GenABIO.cc:109
const int nDCCEE
Definition: GenABIO.cc:136
static const int nEndcapXBins
Definition: GenABIO.cc:100
const char * abTTFFilePrefix
Definition: GenABIO.cc:152
const char tccFlagMarker[]
Definition: GenABIO.cc:164
const char * abSRFFilePrefix
Definition: GenABIO.cc:154
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
const char * abIOFilePostfix
Definition: GenABIO.cc:157
assert(be >=bs)
char srp2roFlags[128]
Definition: GenABIO.cc:166
const int nABInEta
Definition: GenABIO.cc:130
void getABTTPhiBounds(int iABPhi, int &iTTPhiMin, int &iTTPhiMax)
Definition: GenABIO.cc:863
int iEvent
Definition: GenABIO.cc:224
const int nTCCInEta
Definition: GenABIO.cc:140
Definition: GenABIO.cc:168
const int iTTEtaMax[nABInEta]
Definition: GenABIO.cc:144
void writeABIOFileHeader(ofstream &f, int abNum)
Definition: GenABIO.cc:689
void fillABTTFFiles(const char ttFlags[nTTInEta][nTTInPhi], ofstream files[])
Definition: GenABIO.cc:361
int theAB
Definition: GenABIO.cc:226
Definition: GenABIO.cc:168
int abOfDcc(int iDCC)
Definition: ecalDccMap.h:103
int dccIndexOfRU(int iDet, int i, int j)
Definition: ecalDccMap.h:86
Definition: GenABIO.cc:168
roAction_t
Definition: GenABIO.cc:168
double f[11][100]
static const int nTTInEta
Definition: GenABIO.cc:124
static const int nSupercrystalYBins
Definition: GenABIO.cc:112
const char * abIOFilePrefix
Definition: GenABIO.cc:156
const int iTTEtaSign[nABInEta]
Definition: GenABIO.cc:145
const int nABTCCCh
Definition: GenABIO.cc:138
char roFlagMarker[]
Definition: GenABIO.cc:169
string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh)
Definition: GenABIO.cc:786
const int nDCCCh
Definition: GenABIO.cc:139
string getFlagStream(char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax, int iPhiMin, int iPhiMax)
Definition: GenABIO.cc:727
static const int nBarrelTTInEta
Definition: GenABIO.cc:121
void fillABIOFiles(const char ttFlags[nTTInEta][nTTInPhi], const char barrelSrFlags[nBarrelTTInEta][nTTInPhi], const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins], ofstream files[])
Definition: GenABIO.cc:459
string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iTCCCh)
Definition: GenABIO.cc:756
static const int nEndcaps
Definition: GenABIO.cc:115
const int nactions
Definition: GenABIO.cc:179
void * malloc(size_t size) noexcept
void * realloc(void *ptr, size_t size) noexcept
const int nTTInABAlongPhi
Definition: GenABIO.cc:142
#define SIZE_MAX
Definition: GenABIO.cc:25
const char * xconnectFilename
Definition: GenABIO.cc:161
static const int supercrystalEdge
Definition: GenABIO.cc:106
static const int nTTInPhi
Definition: GenABIO.cc:127
const char * abTTFFilePostfix
Definition: GenABIO.cc:153
void writeABTTFFileHeader(ofstream &f, int abNum)
Definition: GenABIO.cc:619
#define SSIZE_MAX
Definition: GenABIO.cc:28
const int nABInPhi
Definition: GenABIO.cc:133
const char * abSRFFilePostfix
Definition: GenABIO.cc:155
void abConnect(int iAB, int iABCh, int &iOtherAB, int &iOtherABCh)
Definition: GenABIO.cc:868
static const int nEndcapTTInEta
Definition: GenABIO.cc:118
const char * srfFilename
Definition: GenABIO.cc:159
vector< pair< int, int > > ecalDccSC[nEndcaps][nDCCEE]
Definition: GenABIO.cc:192
const int iTCCEtaBounds[nTCCInEta+1]
Definition: GenABIO.cc:150
const char * ttfFilename
Definition: GenABIO.cc:160
const int nAB
Definition: GenABIO.cc:141
int abNum(int iABEta, int iABPhi)
Definition: GenABIO.cc:202
int main(int argc, char *argv[])
Definition: GenABIO.cc:228
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
void fillABSRPFiles(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi], const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins], ofstream files[])
Definition: GenABIO.cc:390
const int nABABCh
Definition: GenABIO.cc:137
int dccPhiIndexOfRU(int iDet, int i, int j)
Definition: ecalDccMap.h:38
bool readSRF(FILE *file, char barrelSrFlags[nBarrelTTInEta][nTTInPhi], char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins])
Definition: GenABIO.cc:536
const char srpFlagMarker[]
Definition: GenABIO.cc:163
def exit(msg="")
string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi], const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins], int iABEta, int iABPhi, int DCCCh)
Definition: GenABIO.cc:907