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  // go back to beginning of line:
558  pos = buffer;
559  if (iReadLine < nSupercrystalXBins) { // EE- reading
560  if (read - 1 != nSupercrystalYBins) {
561  cerr << "Error: line " << line << " of file " << srfFilename << " has incorrect length"
562  << " (" << read - 1 << " instead of " << nSupercrystalYBins << ")" << endl;
563  exit(EXIT_FAILURE);
564  }
565  for (int iY = 0; iY < nSupercrystalYBins; ++iY) {
566  endcapSrFlags[0][iXm][iY] = buffer[iY];
567  }
568  ++iXm;
569  } else if (iReadLine < nSupercrystalYBins + nBarrelTTInEta) { // EB
570  // reading
571  if (read - 1 != nTTInPhi) {
572  cerr << "Error: line " << line << " of file " << srfFilename << " has incorrect length"
573  << " (" << read - 1 << " instead of " << nTTInPhi << ")" << endl;
574  exit(EXIT_FAILURE);
575  }
576  for (int iPhi = 0; iPhi < nTTInPhi; ++iPhi) {
577  barrelSrFlags[iEta][iPhi] = buffer[iPhi];
578  }
579  ++iEta;
580  } else if (iReadLine < 2 * nSupercrystalXBins + nBarrelTTInEta) { // EE+ reading
581  if (read - 1 != nSupercrystalYBins) {
582  cerr << "Error: line " << line << " of file " << srfFilename << " has incorrect length"
583  << " (" << read - 1 << " instead of " << nSupercrystalYBins << ")" << endl;
584  exit(EXIT_FAILURE);
585  }
586  for (int iY = 0; iY < nSupercrystalYBins; ++iY) {
587  endcapSrFlags[1][iXp][iY] = buffer[iY];
588  }
589  ++iXp;
590  }
591  ++iReadLine;
592  } // not a comment or empty line
593  }
594  // returns 0 if all TT were read:
595  return (iReadLine == nReadLine) ? true : false;
596 }
597 
598 // void writeEventHeader(ofstream& f, int iEvent, int nPhi){
599 // //event header:
600 // stringstream header;
601 // header.str("");
602 // header << " event " << iEvent << " ";
603 // f << "+";
604 // for(int iPhi = 0; iPhi < nPhi; ++iPhi){
605 // if(iPhi == (int)(nPhi-header.str().size())/2){
606 // f << header.str();
607 // iPhi += header.str().size()-1;
608 // } else{
609 // f << "-";
610 // }
611 // }
612 // f << "+\n";
613 // }
614 
615 // void writeEventTrailer(ofstream& f, int nPhi){
616 // f << "+";
617 // for(int iPhi = 0; iPhi < nPhi; ++iPhi) f << "-";
618 // f << "+\n";
619 // }
620 
621 void writeABTTFFileHeader(ofstream &f, int abNum) {
622  time_t t;
623  time(&t);
624  const char *date = ctime(&t);
625  f << "# TTF flag map covered by AB " << abNum + 1
626  << "\n#\n"
627  "# Generated on : "
628  << date
629  << "#\n"
630  "# +---> Phi "
631  << srpFlagMarker[0]
632  << ": 000 (low interest)\n"
633  "# | "
634  << srpFlagMarker[1]
635  << ": 001 (single)\n"
636  "# | "
637  << srpFlagMarker[2]
638  << ": 010 (neighbour)\n"
639  "# V |Eta| "
640  << srpFlagMarker[3]
641  << ": 011 (center)\n"
642  "#\n";
643 }
644 
645 void writeABSRFFileHeader(ofstream &f, int abNum) {
646  time_t t;
647  time(&t);
648  const char *date = ctime(&t);
649  const char *xLabel;
650  const char *yLabel;
651  if (abNum < 3 || abNum > 8) { // endcap
652  xLabel = "Y ";
653  yLabel = "X ";
654  } else { // barrel
655  xLabel = "Phi";
656  yLabel = "|Eta|";
657  }
658  f << "# SRF flag map covered by AB " << abNum + 1
659  << "\n#\n"
660  "# Generated on : "
661  << date
662  << "#\n"
663  "# +---> "
664  << xLabel << " " << roFlagMarker[0]
665  << ": 000 (suppress)\n"
666  "# | "
667  << roFlagMarker[1]
668  << ": 010 (SR Threshold 2)\n"
669  "# | "
670  << roFlagMarker[2]
671  << ": 001 (SR Threshold 1)\n"
672  "# V "
673  << yLabel << " " << roFlagMarker[3]
674  << ": 011 (Full readout)\n"
675  "#\n"
676  "# action table (when forced):\n"
677  "# LI-> "
678  << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]] << ")"
679  << "\n"
680  "# S -> "
681  << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]] << ")"
682  << "\n"
683  "# N -> "
684  << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]] << ")"
685  << "\n"
686  "# C -> "
687  << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]] << ")"
688  << "\n";
689 }
690 
691 void writeABIOFileHeader(ofstream &f, int abNum) {
692  time_t t;
693  time(&t);
694  const char *date = ctime(&t);
695  f << "# AB " << abNum + 1
696  << " I/O \n#\n"
697  "# Generated on : "
698  << date
699  << "#\n"
700  "# "
701  << srpFlagMarker[0] << ": 000 (low interest) " << tccFlagMarker[0] << ": 000 (low interest) " << roFlagMarker[0]
702  << ": 000 (suppress)\n"
703  "# "
704  << srpFlagMarker[1] << ": 001 (single) " << tccFlagMarker[1] << ": 001 (mid interest) " << roFlagMarker[1]
705  << ": 010 (SR Threshold 2)\n"
706  "# "
707  << srpFlagMarker[2] << ": 010 (neighbour) " << tccFlagMarker[2] << ": 010 (not valid) " << roFlagMarker[2]
708  << ": 001 (SR Threshold 1)\n"
709  "# "
710  << srpFlagMarker[3] << ": 011 (center) " << tccFlagMarker[3] << ": 011 (high interest) " << roFlagMarker[3]
711  << ": 011 (Full readout)\n"
712  "#\n"
713  "# action table (when forced):\n"
714  "# LI-> "
715  << roFlagMarker[actions[0]] << " (" << roFlagMarker[actions[4]] << ")"
716  << "\n"
717  "# S -> "
718  << roFlagMarker[actions[1]] << " (" << roFlagMarker[actions[5]] << ")"
719  << "\n"
720  "# N -> "
721  << roFlagMarker[actions[2]] << " (" << roFlagMarker[actions[6]] << ")"
722  << "\n"
723  "# C -> "
724  << roFlagMarker[actions[3]] << " (" << roFlagMarker[actions[7]] << ")"
725  << "\n"
726  "#\n";
727 }
728 
729 string getFlagStream(const char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax, int iPhiMin, int iPhiMax) {
730  assert(0 <= iEtaMin && iEtaMin <= iEtaMax && iEtaMax < nTTInEta);
731  if (iEtaMin <= nTTInEta / 2 && iEtaMax > nTTInEta) {
732  cerr << "Implementation Errror:" << __FILE__ << ":" << __LINE__
733  << ": A flag stream cannot covers parts of both half-ECAL!" << endl;
734  exit(EXIT_FAILURE);
735  }
736 
737  bool zPos = (iEtaMin >= nTTInEta / 2);
738 
739  stringstream buffer;
740  buffer.str("");
741  for (int jEta = 0; jEta <= iEtaMax - iEtaMin; ++jEta) {
742  // loops on iEta in |eta| increasing order:
743  int iEta;
744  if (zPos) {
745  iEta = iEtaMin + jEta;
746  } else {
747  iEta = iEtaMax - jEta;
748  }
749 
750  for (int iPhi = mod(iPhiMin, nTTInPhi); mod(iPhiMax + 1 - iPhi, nTTInPhi) != 0; iPhi = mod(++iPhi, nTTInPhi)) {
751  buffer << flags[iEta][iPhi];
752  }
753  }
754 
755  return buffer.str();
756 }
757 
758 string getABTCCInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iTCCCh) {
759  // gets eta bounds for this tcc channel:
760  int iTCCEta;
761  if (iABEta == 1 || iABEta == 2) { // barrel
762  if (iTCCCh > 5)
763  return ""; // only 6 TCCs per AB for barrel
764  iTCCEta = 1 + iABEta;
765  } else { // endcap
766  if (iABEta == 0) { // EE-
767  iTCCEta = (iTCCCh < 6) ? 1 : 0;
768  } else { // EE+
769  iTCCEta = (iTCCCh < 6) ? 4 : 5;
770  }
771  }
772  int iEtaMin = iTCCEtaBounds[iTCCEta];
773  int iEtaMax = iTCCEtaBounds[iTCCEta + 1] - 1;
774 
775  // gets phi bounds:
776  int iPhiMin;
777  int iPhiMax;
778  getABTTPhiBounds(iABPhi, iPhiMin, iPhiMax);
779  // phi is increasing with TTC channel number
780  // a TTC covers a 4TT-wide phi-sector
781  //=>iPhiMin(iTTCCh) = iPhiMin(AB) + 4*iTTCCh for iTCCCh<6
782  iPhiMin += 4 * (iTCCCh % 6);
783  iPhiMax = iPhiMin + 4 - 1;
784 
785  return getFlagStream(tccFlags, iEtaMin, iEtaMax, iPhiMin, iPhiMax);
786 }
787 
788 string getABABOutputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh) {
789  stringstream buffer;
790  buffer.str("");
791  bool barrel = (iABEta == 1 || iABEta == 2); // true for barrel, false for endcap
792  switch (iABCh) {
793  case 0:
794  // to AB ch #0 are sent the 16 1st TCC flags received on TCC input Ch. 0
795  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0).substr(0, 16);
796  break;
797  case 1:
798  // to AB ch #1 are sent the 16 1st TCC flags received on TCC input Ch. 0 to
799  // 5:
800  for (int iTCCCh = 0; iTCCCh < 6; ++iTCCCh) {
801  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh).substr(0, 16);
802  }
803  break;
804  case 2:
805  // to AB ch #2 are sent the 16 1st TCC flags received on TCC input Ch. 5:
806  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5).substr(0, 16);
807  break;
808  case 3:
809  // to AB ch #3 are sent TCC flags received on TCC input Ch. 0 and 6:
810  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
811  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 6);
812  break;
813  case 4:
814  // to AB ch #4 are sent TCC flags received on TCC input Ch 5 and 11:
815  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
816  buffer << getABTCCInputStream(tccFlags, iABEta, iABPhi, 11);
817  break;
818  case 5:
819  // for endcaps AB output ch 5 is not used.
820  // for barrel, to AB ch #5 are sent the 16 last TCC flags received on TCC
821  // input Ch. 0:
822  if (barrel) { // in barrel
823  string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 0);
824  assert(s.size() >= 16);
825  buffer << s.substr(s.size() - 16, 16);
826  }
827  break;
828  case 6:
829  // for endcaps AB output ch 6 is not used.
830  // for barrel, to AB ch #6 are sent the 16 last TCC flags received on TCC
831  // input Ch. 0 to 5:
832  if (barrel) { // in barrel
833  for (int iTCCCh = 0; iTCCCh < 6; ++iTCCCh) {
834  string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, iTCCCh);
835  buffer << s.substr(s.size() - 16, 16);
836  }
837  }
838  break;
839  case 7:
840  // for endcaps AB output ch 7 is not used.
841  // for barrel, to AB ch #7 are sent the 16 last TCC flags received on TCC
842  // input Ch. 5:
843  if (barrel) { // in barrel
844  string s = getABTCCInputStream(tccFlags, iABEta, iABPhi, 5);
845  assert(s.size() >= 16);
846  buffer << s.substr(s.size() - 16, 16);
847  }
848  break;
849  default:
850  assert(false);
851  }
852  return buffer.str();
853 }
854 
855 string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh) {
856  int iAB = abNum(iABEta, iABPhi);
857  int iOtherAB; // AB which this channel is connected to
858  int iOtherABCh; // ch # on the other side of the AB-AB link
859  abConnect(iAB, iABCh, iOtherAB, iOtherABCh);
860  int iOtherABEta = iOtherAB / 3;
861  int iOtherABPhi = iOtherAB % 3;
862  return getABABOutputStream(tccFlags, iOtherABEta, iOtherABPhi, iOtherABCh);
863 }
864 
865 void getABTTPhiBounds(int iABPhi, int &iTTPhiMin, int &iTTPhiMax) {
866  iTTPhiMin = mod(-6 + iABPhi * nTTInABAlongPhi, nTTInPhi);
867  iTTPhiMax = mod(iTTPhiMin + nTTInABAlongPhi - 1, nTTInPhi);
868 }
869 
870 void abConnect(int iAB, int iABCh, int &iOtherAB, int &iOtherABCh) {
871  static bool firstCall = true;
872  static int xconnectMap[nAB][nABABCh][2];
873  if (firstCall) {
874  FILE *f = fopen(xconnectFilename, "r");
875  if (f == nullptr) {
876  cerr << "Error. Failed to open xconnect definition file," << xconnectFilename << endl;
877  exit(EXIT_FAILURE);
878  }
879  // skips two first lines:
880  for (int i = 0; i < 2; ++i) {
881  int c;
882  while ((c = getc(f)) != '\n' && c >= 0)
883  ;
884  }
885  int ilink = 0;
886  while (!feof(f)) {
887  int abIn;
888  int pinIn;
889  int abOut;
890  int pinOut;
891  if (4 == fscanf(f, "%d\t%d\t%d\t%d", &abIn, &pinIn, &abOut, &pinOut)) {
892  xconnectMap[abIn][pinIn][0] = abOut;
893  xconnectMap[abIn][pinIn][1] = pinOut;
894  ++ilink;
895  }
896  }
897  if (ilink != nAB * nABABCh) {
898  cerr << "Error cross-connect definition file " << xconnectFilename
899  << " contains an unexpected number of link definition." << endl;
900  exit(EXIT_FAILURE);
901  }
902  firstCall = false;
903  }
904 
905  iOtherAB = xconnectMap[iAB][iABCh][0];
906  iOtherABCh = xconnectMap[iAB][iABCh][1];
907 }
908 
909 string getABDCCOutputStream(const char barrelSrFlags[nBarrelTTInEta][nTTInPhi],
910  const char endcapSrFlags[nEndcaps][nSupercrystalXBins][nSupercrystalYBins],
911  int iABEta,
912  int iABPhi,
913  int iDCCCh) {
914  bool barrel = (iABEta == 1 || iABEta == 2);
915  if (barrel) {
916  // same as TCC with same ch number but with TCC flags replaced by SRP flags:
917  string stream = getABTCCInputStream(barrelSrFlags - nEndcapTTInEta, iABEta, iABPhi, iDCCCh);
918  // converts srp flags to readout flags:
919  for (size_t i = 0; i < stream.size(); ++i) {
920  stream[i] = srp2roFlags[(int)stream[i]];
921  }
922  return stream;
923  } else { // endcap
924  if (iDCCCh < 3) { // used DCC output channel
925  // endcap index:
926  int iEE = (iABEta == 0) ? 0 : 1;
927  stringstream buffer("");
928  // 3 DCC per AB and AB DCC output channel in
929  // increasing DCC phi position:
930  int iDCCPhi = iABPhi * 3 + iDCCCh;
931  for (size_t iSC = 0; iSC < ecalDccSC[iEE][iDCCPhi].size(); ++iSC) {
932  pair<int, int> sc = ecalDccSC[iEE][iDCCPhi][iSC];
933  buffer << srp2roFlags[(int)endcapSrFlags[iEE][sc.first][sc.second]];
934  }
935  return buffer.str();
936  } else { // unused output channel
937  return "";
938  }
939  }
940 }
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:645
roAction_t actions[nactions]
Definition: GenABIO.cc:181
string getABABInputStream(const char tccFlags[nTTInEta][nTTInPhi], int iABEta, int iABPhi, int iABCh)
Definition: GenABIO.cc:855
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
#define NULL
Definition: scimark2.h:8
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:865
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:691
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:788
const int nDCCCh
Definition: GenABIO.cc:139
string getFlagStream(char flags[nTTInEta][nTTInPhi], int iEtaMin, int iEtaMax, int iPhiMin, int iPhiMax)
Definition: GenABIO.cc:729
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:758
static const int nEndcaps
Definition: GenABIO.cc:115
const int nactions
Definition: GenABIO.cc:179
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:621
#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:870
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:909